-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
377 lines (311 loc) · 14.2 KB
/
utils.py
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
import torch
import torch.nn as nn
from torchvision import models, datasets, transforms
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score
import numpy as np
from PIL import Image
import gradio as gr
# Define Model Architectures
class VisionTransformer(nn.Module):
def __init__(self,
in_channels=3,
embed_dim=128,
num_layers=6,
num_heads=4,
mlp_dim=256,
image_size=32,
patch_size=4,
num_classes=10,
dropout=0.1):
super(VisionTransformer, self).__init__()
assert image_size % patch_size == 0, "Image size must be divisible by patch size."
self.patch_size = patch_size
self.num_patches = (image_size // patch_size) ** 2
self.embed_dim = embed_dim
# Patch embedding
self.patch_embed = nn.Conv2d(in_channels, embed_dim,
kernel_size=patch_size,
stride=patch_size)
# Positional embedding
self.position_embed = nn.Parameter(torch.zeros(1, self.num_patches, embed_dim))
# Transformer encoder layers
encoder_layer = nn.TransformerEncoderLayer(d_model=embed_dim,
nhead=num_heads,
dim_feedforward=mlp_dim,
dropout=dropout)
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
# Classification head
self.cls_head = nn.Sequential(
nn.LayerNorm(embed_dim),
nn.Linear(embed_dim, num_classes)
)
def forward(self, x):
# x: [batch_size, channels, height, width]
batch_size = x.size(0)
# Patch embedding
x = self.patch_embed(x) # [batch_size, embed_dim, num_patches_sqrt, num_patches_sqrt]
x = x.flatten(2) # [batch_size, embed_dim, num_patches]
x = x.transpose(1, 2) # [batch_size, num_patches, embed_dim]
# Add positional embedding
x = x + self.position_embed # [batch_size, num_patches, embed_dim]
# Transformer expects [sequence_length, batch_size, embed_dim]
x = x.transpose(0, 1) # [num_patches, batch_size, embed_dim]
# Transformer encoder
x = self.transformer(x) # [num_patches, batch_size, embed_dim]
# Aggregate features (mean pooling)
x = x.mean(dim=0) # [batch_size, embed_dim]
# Classification head
logits = self.cls_head(x) # [batch_size, num_classes]
return logits
class HybridCNNMLP(nn.Module):
def __init__(self,
in_channels=3,
num_classes=10):
super(HybridCNNMLP, self).__init__()
# CNN for feature extraction
self.cnn = nn.Sequential(
nn.Conv2d(in_channels, 64, kernel_size=3, padding=1), # [batch, 64, 32, 32]
nn.ReLU(),
nn.MaxPool2d(2, 2), # [batch, 64, 16, 16]
nn.Conv2d(64, 128, kernel_size=3, padding=1), # [batch, 128, 16, 16]
nn.ReLU(),
nn.MaxPool2d(2, 2), # [batch, 128, 8, 8]
nn.Conv2d(128, 256, kernel_size=3, padding=1), # [batch, 256, 8, 8]
nn.ReLU(),
nn.MaxPool2d(2, 2), # [batch, 256, 4, 4]
)
# MLP for classification
self.mlp = nn.Sequential(
nn.Flatten(), # [batch, 256*4*4]
nn.Linear(256*4*4, 512),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512, num_classes)
)
def forward(self, x):
features = self.cnn(x)
logits = self.mlp(features)
return logits
class ResNetTransferLearning(nn.Module):
def __init__(self, num_classes=10):
super(ResNetTransferLearning, self).__init__()
# Load pretrained ResNet50 with updated weights parameter
self.resnet = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1)
# Freeze all layers
for param in self.resnet.parameters():
param.requires_grad = False
# Modify the final fully connected layer
num_ftrs = self.resnet.fc.in_features
self.resnet.fc = nn.Sequential(
nn.Linear(num_ftrs, 256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, num_classes)
)
def forward(self, x):
logits = self.resnet(x)
return logits
def evaluate_model(model, dataloader, device):
"""
Evaluates the model on the test dataset and calculates metrics.
Parameters:
- model: The trained model to evaluate.
- dataloader: DataLoader for the test dataset.
- device: Device to perform computations on.
Returns:
- metrics: Dictionary containing accuracy, precision, recall, and F1-score.
- all_preds: List of all predicted labels.
- all_labels: List of all true labels.
"""
model.eval()
all_preds = []
all_labels = []
with torch.no_grad():
for images, labels in dataloader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
preds = torch.argmax(outputs, dim=1)
all_preds.extend(preds.cpu().numpy())
all_labels.extend(labels.cpu().numpy())
# Calculate metrics
accuracy = (np.array(all_preds) == np.array(all_labels)).mean()
precision = precision_score(all_labels, all_preds, average='weighted', zero_division=0)
recall = recall_score(all_labels, all_preds, average='weighted', zero_division=0)
f1 = f1_score(all_labels, all_preds, average='weighted', zero_division=0)
metrics = {
'Accuracy': accuracy,
'Precision': precision,
'Recall': recall,
'F1-Score': f1
}
return metrics, all_preds, all_labels
def plot_confusion_matrix(true_labels, preds, classes, model_name):
"""
Plots and displays the confusion matrix.
Parameters:
- true_labels: List of true class labels.
- preds: List of predicted class labels.
- classes: Tuple of class names.
- model_name: Name of the model (for title).
"""
cm = confusion_matrix(true_labels, preds)
plt.figure(figsize=(10,8))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
xticklabels=classes, yticklabels=classes)
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title(f'{model_name} - Confusion Matrix')
plt.show()
def display_sample_predictions(model, dataloader, classes, num_samples=16):
"""
Displays a grid of sample predictions with true and predicted labels.
Parameters:
- model: The trained model for inference.
- dataloader: DataLoader for the test dataset.
- classes: Tuple of class names.
- num_samples: Number of samples to display.
"""
model.eval()
images_shown = 0
plt.figure(figsize=(12, 12))
with torch.no_grad():
for images, labels in dataloader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
preds = torch.argmax(outputs, dim=1)
for i in range(images.size(0)):
if images_shown >= num_samples:
break
img = images[i].cpu()
img = img * 0.5 + 0.5 # Unnormalize
npimg = img.numpy()
plt.subplot(int(np.sqrt(num_samples)), int(np.sqrt(num_samples)), images_shown+1)
plt.imshow(np.transpose(npimg, (1, 2, 0)))
true_label = classes[labels[i]]
pred_label = classes[preds[i]]
color = 'green' if pred_label == true_label else 'red'
plt.title(f"True: {true_label}\nPred: {pred_label}", color=color)
plt.axis('off')
images_shown += 1
if images_shown >= num_samples:
break
plt.tight_layout()
plt.show()
def predict_image(image):
"""
Predicts the class of an input image using all three models.
Parameters:
- image: Input image in PIL format.
Returns:
- Dictionary containing predictions from ResNet, ViT, and Hybrid CNN + MLP.
"""
# Define transformation
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])
# Preprocess the image
image = transform(image).unsqueeze(0).to(device) # Add batch dimension
# Get predictions from ResNet
with torch.no_grad():
resnet_output = resnet_model(image)
resnet_pred = torch.argmax(resnet_output, dim=1).item()
resnet_confidence = torch.softmax(resnet_output, dim=1)[0][resnet_pred].item()
# Get predictions from ViT
with torch.no_grad():
vit_output = vit_model(image)
vit_pred = torch.argmax(vit_output, dim=1).item()
vit_confidence = torch.softmax(vit_output, dim=1)[0][vit_pred].item()
# Get predictions from Hybrid CNN + MLP
with torch.no_grad():
hybrid_output = hybrid_model(image)
hybrid_pred = torch.argmax(hybrid_output, dim=1).item()
hybrid_confidence = torch.softmax(hybrid_output, dim=1)[0][hybrid_pred].item()
# Prepare results
results = {
"ResNet Transfer Learning": f"{classes[resnet_pred]} ({resnet_confidence*100:.2f}%)",
"Vision Transformer (ViT)": f"{classes[vit_pred]} ({vit_confidence*100:.2f}%)",
"Hybrid CNN + MLP": f"{classes[hybrid_pred]} ({hybrid_confidence*100:.2f}%)"
}
return results
if __name__ == "__main__":
# Define device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# Instantiate the models
resnet_model = ResNetTransferLearning().to(device)
vit_model = VisionTransformer().to(device)
hybrid_model = HybridCNNMLP().to(device)
# Paths to the saved model files
resnet_model_path = 'D:\\Genai_a2\\models\\resnet-transfer-cifar10-best.pt'
vit_model_path = 'D:\\Genai_a2\\models\\vit-layer6-32-cifar10-best.pt'
hybrid_model_path = 'D:\\Genai_a2\\models\\hybrid-cnn-mlp-cifar10-best.pt'
# Load the saved state dictionaries
resnet_model.load_state_dict(torch.load(resnet_model_path, map_location=device))
vit_model.load_state_dict(torch.load(vit_model_path, map_location=device))
hybrid_model.load_state_dict(torch.load(hybrid_model_path, map_location=device))
# Set models to evaluation mode
resnet_model.eval()
vit_model.eval()
hybrid_model.eval()
print("All models loaded and set to evaluation mode.")
# Define transformations for test data
test_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # Ensure same normalization as training
])
# Load CIFAR-10 test dataset
test_dataset = datasets.CIFAR10(root='./data', train=False, download=True, transform=test_transform)
test_loader = DataLoader(test_dataset, batch_size=100, shuffle=False, num_workers=2)
print(f"Test Dataset Size: {len(test_loader.dataset)}")
# Define CIFAR-10 classes
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# Evaluate models
resnet_metrics, resnet_preds, resnet_labels = evaluate_model(resnet_model, test_loader, device)
vit_metrics, vit_preds, vit_labels = evaluate_model(vit_model, test_loader, device)
hybrid_metrics, hybrid_preds, hybrid_labels = evaluate_model(hybrid_model, test_loader, device)
# Print metrics
print("ResNet with Transfer Learning Metrics:")
for metric, value in resnet_metrics.items():
print(f"{metric}: {value:.4f}")
print("\n")
print("Vision Transformer (ViT) Metrics:")
for metric, value in vit_metrics.items():
print(f"{metric}: {value:.4f}")
print("\n")
print("Hybrid CNN + MLP Metrics:")
for metric, value in hybrid_metrics.items():
print(f"{metric}: {value:.4f}")
print("\n")
# Plot Confusion Matrices for All Models
plot_confusion_matrix(resnet_labels, resnet_preds, classes, "ResNet with Transfer Learning")
plot_confusion_matrix(vit_labels, vit_preds, classes, "Vision Transformer (ViT)")
plot_confusion_matrix(hybrid_labels, hybrid_preds, classes, "Hybrid CNN + MLP")
# Display Sample Predictions for All Models
print("ResNet with Transfer Learning - Sample Predictions:")
display_sample_predictions(resnet_model, test_loader, classes, num_samples=16)
print("Vision Transformer (ViT) - Sample Predictions:")
display_sample_predictions(vit_model, test_loader, classes, num_samples=16)
print("Hybrid CNN + MLP - Sample Predictions:")
display_sample_predictions(hybrid_model, test_loader, classes, num_samples=16)
# Define the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# CIFAR-10 Image Classification")
gr.Markdown("### Upload an image from the CIFAR-10 test set to see predictions from ResNet, ViT, and Hybrid CNN + MLP models.")
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="Input Image")
predict_btn = gr.Button("Predict")
with gr.Column():
resnet_output = gr.Textbox(label="ResNet Transfer Learning Prediction")
vit_output = gr.Textbox(label="Vision Transformer (ViT) Prediction")
hybrid_output = gr.Textbox(label="Hybrid CNN + MLP Prediction")
predict_btn.click(fn=predict_image, inputs=image_input, outputs=[resnet_output, vit_output, hybrid_output])
# Launch the Gradio app
demo.launch()