File size: 15,613 Bytes
906c9b0 c02513c 3ea3100 c02513c 3ea3100 3690a76 c02513c 3ffa6ad 3690a76 c02513c 3ffa6ad c02513c 3690a76 c02513c 3690a76 c02513c 3ea3100 3690a76 c02513c 3690a76 c02513c 3690a76 c02513c 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 c02513c 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 c02513c 3ea3100 3690a76 3ea3100 3690a76 c02513c 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 c02513c 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 3ea3100 3690a76 e897bc2 3ea3100 3690a76 e897bc2 3ea3100 3690a76 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
import gradio as gr
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torchvision import models
from transformers import BertTokenizer, BertModel
import pandas as pd
from datasets import load_dataset
from torch.utils.data import DataLoader, Dataset, random_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
import os
import logging
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('model_training.log'),
logging.StreamHandler()
]
)
# Create output directory for results
os.makedirs('output', exist_ok=True)
# Load dataset and filter out null/none values
logging.info("Loading and filtering dataset...")
dataset = load_dataset('thefcraft/civitai-stable-diffusion-337k', split='train[:10000]')
dataset = dataset.filter(lambda example: example['Model'] is not None and example['Model'].strip() != '')
if len(dataset) == 0:
raise ValueError("Dataset is empty after filtering!")
logging.info(f"Dataset size after filtering: {len(dataset)}")
# Preprocess text data
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
class CustomDataset(Dataset):
def __init__(self, dataset):
self.dataset = dataset
self.transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
self.label_encoder = LabelEncoder()
self.labels = self.label_encoder.fit_transform(dataset['Model'])
self.unique_models = self.label_encoder.classes_
logging.info(f"Number of unique models: {len(self.unique_models)}")
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
try:
image = self.transform(self.dataset[idx]['image'])
text = tokenizer(
self.dataset[idx]['prompt'],
padding='max_length',
truncation=True,
max_length=512,
return_tensors='pt'
)
label = self.labels[idx]
return image, text, label
except Exception as e:
logging.error(f"Error processing item {idx}: {str(e)}")
raise
class ImageModel(nn.Module):
def __init__(self):
super(ImageModel, self).__init__()
self.model = models.resnet18(pretrained=True)
self.model.fc = nn.Linear(self.model.fc.in_features, 512)
def forward(self, x):
x = self.model(x)
return nn.functional.relu(x)
class TextModel(nn.Module):
def __init__(self):
super(TextModel, self).__init__()
self.bert = BertModel.from_pretrained('bert-base-uncased')
self.fc = nn.Linear(768, 512)
def forward(self, x):
outputs = self.bert(**x)
x = outputs.pooler_output
x = self.fc(x)
return nn.functional.relu(x)
class CombinedModel(nn.Module):
def __init__(self, num_classes):
super(CombinedModel, self).__init__()
self.image_model = ImageModel()
self.text_model = TextModel()
self.dropout = nn.Dropout(0.2)
self.fc = nn.Linear(1024, num_classes)
def forward(self, image, text):
image_features = self.image_model(image)
text_features = self.text_model(text)
combined = torch.cat((image_features, text_features), dim=1)
combined = self.dropout(combined)
return self.fc(combined)
class ModelTrainerEvaluator:
def __init__(self, model, dataset, batch_size=32, learning_rate=0.001):
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
logging.info(f"Using device: {self.device}")
self.model = model.to(self.device)
self.batch_size = batch_size
self.criterion = nn.CrossEntropyLoss()
self.optimizer = torch.optim.AdamW(
model.parameters(),
lr=learning_rate,
weight_decay=0.01
)
self.scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
self.optimizer,
mode='min',
factor=0.1,
patience=2,
verbose=True
)
# Split dataset
total_size = len(dataset)
train_size = int(0.7 * total_size)
val_size = int(0.15 * total_size)
test_size = total_size - train_size - val_size
train_dataset, val_dataset, test_dataset = random_split(
dataset, [train_size, val_size, test_size]
)
self.train_loader = DataLoader(
train_dataset,
batch_size=batch_size,
shuffle=True,
num_workers=4
)
self.val_loader = DataLoader(
val_dataset,
batch_size=batch_size,
num_workers=4
)
self.test_loader = DataLoader(
test_dataset,
batch_size=batch_size,
num_workers=4
)
self.unique_models = dataset.unique_models
def train_epoch(self):
self.model.train()
total_loss = 0
predictions = []
actual_labels = []
progress_bar = tqdm(self.train_loader, desc="Training")
for batch_idx, batch in enumerate(progress_bar):
try:
images, texts, labels = batch
images = images.to(self.device)
labels = labels.to(self.device)
# Move text tensors to device
texts = {k: v.squeeze(1).to(self.device) for k, v in texts.items()}
self.optimizer.zero_grad()
outputs = self.model(images, texts)
loss = self.criterion(outputs, labels)
loss.backward()
# Gradient clipping
torch.nn.utils.clip_grad_norm_(self.model.parameters(), max_norm=1.0)
self.optimizer.step()
total_loss += loss.item()
_, preds = torch.max(outputs, 1)
predictions.extend(preds.cpu().numpy())
actual_labels.extend(labels.cpu().numpy())
# Update progress bar
progress_bar.set_postfix({
'loss': f'{loss.item():.4f}',
'avg_loss': f'{total_loss/(batch_idx+1):.4f}'
})
except Exception as e:
logging.error(f"Error in batch {batch_idx}: {str(e)}")
continue
return total_loss / len(self.train_loader), predictions, actual_labels
def evaluate(self, loader, mode="Validation"):
self.model.eval()
total_loss = 0
predictions = []
actual_labels = []
with torch.no_grad():
progress_bar = tqdm(loader, desc=mode)
for batch_idx, batch in enumerate(progress_bar):
try:
images, texts, labels = batch
images = images.to(self.device)
labels = labels.to(self.device)
texts = {k: v.squeeze(1).to(self.device) for k, v in texts.items()}
outputs = self.model(images, texts)
loss = self.criterion(outputs, labels)
total_loss += loss.item()
_, preds = torch.max(outputs, 1)
predictions.extend(preds.cpu().numpy())
actual_labels.extend(labels.cpu().numpy())
progress_bar.set_postfix({
'loss': f'{loss.item():.4f}',
'avg_loss': f'{total_loss/(batch_idx+1):.4f}'
})
except Exception as e:
logging.error(f"Error in {mode} batch {batch_idx}: {str(e)}")
continue
return total_loss / len(loader), predictions, actual_labels
def plot_confusion_matrix(self, y_true, y_pred, title):
cm = confusion_matrix(y_true, y_pred)
plt.figure(figsize=(15, 15))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.title(title)
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
# Save plot
filename = f'output/{title.lower().replace(" ", "_")}.png'
plt.savefig(filename)
plt.close()
logging.info(f"Saved confusion matrix to {filename}")
def generate_evaluation_report(self, y_true, y_pred, title):
report = classification_report(
y_true,
y_pred,
target_names=self.unique_models,
output_dict=True
)
df_report = pd.DataFrame(report).transpose()
# Save report
filename = f'output/{title.lower().replace(" ", "_")}_report.csv'
df_report.to_csv(filename)
logging.info(f"Saved classification report to {filename}")
accuracy = accuracy_score(y_true, y_pred)
logging.info(f"\n{title} Results:")
logging.info(f"Accuracy: {accuracy:.4f}")
logging.info("\nClassification Report:")
logging.info("\n" + classification_report(y_true, y_pred, target_names=self.unique_models))
return accuracy, df_report
def train_and_evaluate(self, num_epochs=5):
best_val_loss = float('inf')
train_accuracies = []
val_accuracies = []
train_losses = []
val_losses = []
logging.info(f"Starting training for {num_epochs} epochs...")
for epoch in range(num_epochs):
logging.info(f"\nEpoch {epoch+1}/{num_epochs}")
# Training
train_loss, train_preds, train_labels = self.train_epoch()
train_accuracy, _ = self.generate_evaluation_report(
train_labels,
train_preds,
f"Training_Epoch_{epoch+1}"
)
self.plot_confusion_matrix(
train_labels,
train_preds,
f"Training_Confusion_Matrix_Epoch_{epoch+1}"
)
# Validation
val_loss, val_preds, val_labels = self.evaluate(self.val_loader)
val_accuracy, _ = self.generate_evaluation_report(
val_labels,
val_preds,
f"Validation_Epoch_{epoch+1}"
)
self.plot_confusion_matrix(
val_labels,
val_preds,
f"Validation_Confusion_Matrix_Epoch_{epoch+1}"
)
# Update learning rate scheduler
self.scheduler.step(val_loss)
train_accuracies.append(train_accuracy)
val_accuracies.append(val_accuracy)
train_losses.append(train_loss)
val_losses.append(val_loss)
logging.info(f"\nTraining Loss: {train_loss:.4f}")
logging.info(f"Validation Loss: {val_loss:.4f}")
# Save best model
if val_loss < best_val_loss:
best_val_loss = val_loss
torch.save({
'epoch': epoch,
'model_state_dict': self.model.state_dict(),
'optimizer_state_dict': self.optimizer.state_dict(),
'val_loss': val_loss,
}, 'output/best_model.pth')
logging.info(f"Saved new best model with validation loss: {val_loss:.4f}")
# Plot training history
plt.figure(figsize=(12, 4))
# Plot accuracies
plt.subplot(1, 2, 1)
plt.plot(train_accuracies, label='Training Accuracy')
plt.plot(val_accuracies, label='Validation Accuracy')
plt.title('Model Accuracy over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
# Plot losses
plt.subplot(1, 2, 2)
plt.plot(train_losses, label='Training Loss')
plt.plot(val_losses, label='Validation Loss')
plt.title('Model Loss over Epochs')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.tight_layout()
plt.savefig('output/training_history.png')
plt.close()
# Final test evaluation using best model
logging.info("\nPerforming final evaluation on test set...")
checkpoint = torch.load('output/best_model.pth')
self.model.load_state_dict(checkpoint['model_state_dict'])
test_loss, test_preds, test_labels = self.evaluate(self.test_loader, "Test")
self.generate_evaluation_report(test_labels, test_preds, "Final_Test")
self.plot_confusion_matrix(test_labels, test_preds, "Final_Test_Confusion_Matrix")
def predict(image):
model.eval()
with torch.no_grad():
image = transforms.ToTensor()(image).unsqueeze(0)
image = transforms.Resize((224, 224))(image)
text_input = tokenizer(
"Sample prompt",
return_tensors='pt',
padding=True,
truncation=True
)
output = model(image, text_input)
_, indices = torch.topk(output, 5)
recommended_models = [dataset['Model'][i] for i in indices[0]]
return recommended_models
def main():
try:
# Create dataset
logging.info("Creating custom dataset...")
custom_dataset = CustomDataset(dataset)
# Create model
logging.info("Initializing model...")
model = CombinedModel(num_classes=len(custom_dataset.unique_models))
# Create trainer/evaluator
logging.info("Setting up trainer/evaluator...")
trainer = ModelTrainerEvaluator(
model=model,
dataset=custom_dataset,
batch_size=32,
learning_rate=0.001
)
# Train and evaluate
logging.info("Starting training process...")
trainer.train_and_evaluate(num_epochs=5)
# Create Gradio interface
logging.info("Setting up Gradio interface...")
interface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=gr.Textbox(label="Recommended Models"),
title="AI Image Model Recommender",
description="Upload an AI-generated image to receive model recommendations.",
examples=[
["example_image1.jpg"],
["example_image2.jpg"]
],
analytics_enabled=False
)
# Launch the interface
logging.info("Launching Gradio interface...")
interface.launch(share=True)
except Exception as e:
logging.error(f"Error in main function: {str(e)}")
raise
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
logging.info("Process interrupted by user")
except Exception as e:
logging.error(f"Fatal error: {str(e)}")
raise
|