-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViTmod.py
More file actions
406 lines (331 loc) · 14.8 KB
/
ViTmod.py
File metadata and controls
406 lines (331 loc) · 14.8 KB
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
# ===== IMPORTS =====
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
import numpy as np
import random
import matplotlib.pyplot as plt
from tqdm.auto import tqdm
from sklearn.metrics import confusion_matrix
import seaborn as sns
from matplotlib.colors import LinearSegmentedColormap
# ===== REPRODUCIBILITY & DEVICE =====
# Fix random seeds for reproducibility
torch.manual_seed(42)
torch.cuda.manual_seed(42)
np.random.seed(42)
random.seed(42)
# Set device to GPU if available, else CPU
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using Device: {device}")
# ===== HYPERPARAMETERS =====
BATCH_SIZE = 128 # Number of samples per batch
EPOCHS = 100 # Total training epochs
LEARNING_RATE = 5e-4 # Learning rate for optimizer
PATCH_SIZE = 4 # Patch size for ViT
NUM_CLASSES = 10 # CIFAR-10 has 10 classes
IMAGE_SIZE = 32 # CIFAR-10 image size
CHANNELS = 3 # RGB images
EMBED_DIM = 384 # Embedding dimension
NUM_HEADS = 8 # Number of attention heads
DEPTH = 12 # Number of transformer layers
MLP_RATIO = 4 # Expansion ratio in MLP
DROP_RATE = 0.1 # Dropout probability
ATTN_DROP_RATE = 0.1 # Attention dropout probability
WEIGHT_DECAY = 0.05 # Regularization for optimizer
WARMUP_EPOCHS = 5 # Warm-up period for LR scheduler
# ===== DATA AUGMENTATION & NORMALIZATION =====
# Augmentations for training data
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(15),
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2470, 0.2435, 0.2616))
])
# Normalization only for test data
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2470, 0.2435, 0.2616))
])
# ===== LOAD CIFAR-10 DATASETS =====
train_dataset = datasets.CIFAR10(root="data", train=True, download=True, transform=transform_train)
test_dataset = datasets.CIFAR10(root="data", train=False, download=True, transform=transform_test)
# Create data loaders
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2, pin_memory=True)
test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=2, pin_memory=True)
# ===== CUSTOM COLOR MAP FOR VISUALIZATION =====
def create_custom_cmap():
colors = ["#2E86AB", "#A23B72", "#F18F01", "#C73E1D", "#3B1F2B"]
return LinearSegmentedColormap.from_list("custom", colors)
custom_cmap = create_custom_cmap()
# ===== PATCH EMBEDDING LAYER =====
class PatchEmbedding(nn.Module):
def __init__(self, img_size, patch_size, in_channels, embed_dim):
super().__init__()
self.img_size = img_size
self.patch_size = patch_size
self.num_patches = (img_size // patch_size) ** 2
self.proj = nn.Conv2d(in_channels, embed_dim, kernel_size=patch_size, stride=patch_size)
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) # Learnable CLS token
self.pos_embed = nn.Parameter(torch.zeros(1, self.num_patches + 1, embed_dim)) # Learnable position embedding
def forward(self, x):
B, C, H, W = x.shape
x = self.proj(x).flatten(2).transpose(1, 2) # Shape: (B, Num_Patches, Embed_Dim)
cls_tokens = self.cls_token.expand(B, -1, -1) # Repeat CLS token for each item in batch
x = torch.cat((cls_tokens, x), dim=1) # Concatenate CLS token
x = x + self.pos_embed # Add position embedding
return x
# ===== MLP BLOCK =====
class Mlp(nn.Module):
def __init__(self, in_features, hidden_features=None, out_features=None, drop=0.1):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = nn.Linear(in_features, hidden_features)
self.act = nn.GELU()
self.fc2 = nn.Linear(hidden_features, out_features)
self.drop = nn.Dropout(drop)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
# ===== TRANSFORMER ENCODER BLOCK =====
class TransformerEncoderLayer(nn.Module):
def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=True, drop=0., attn_drop=0., drop_path=0.):
super().__init__()
self.norm1 = nn.LayerNorm(dim)
self.attn = nn.MultiheadAttention(dim, num_heads, dropout=attn_drop, batch_first=True)
self.drop_path = nn.Dropout(drop_path) if drop_path > 0. else nn.Identity()
self.norm2 = nn.LayerNorm(dim)
mlp_hidden_dim = int(dim * mlp_ratio)
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, drop=drop)
self.attention_weights = None # For visualization
def forward(self, x):
x_norm = self.norm1(x)
attn_output, attn_weights = self.attn(x_norm, x_norm, x_norm)
self.attention_weights = attn_weights # Save attention for visualization
x = x + self.drop_path(attn_output)
x = x + self.drop_path(self.mlp(self.norm2(x)))
return x
# ===== VISION TRANSFORMER MODEL =====
class VisionTransformer(nn.Module):
def __init__(self, img_size=32, patch_size=4, in_chans=3, num_classes=10,
embed_dim=384, depth=12, num_heads=8, mlp_ratio=4,
drop_rate=0.1, attn_drop_rate=0.1):
super().__init__()
self.num_classes = num_classes
self.embed_dim = embed_dim
self.patch_embed = PatchEmbedding(img_size, patch_size, in_chans, embed_dim)
self.pos_drop = nn.Dropout(p=drop_rate)
dpr = [x.item() for x in torch.linspace(0, drop_rate, depth)] # Stochastic depth rates
self.blocks = nn.Sequential(*[
TransformerEncoderLayer(embed_dim, num_heads, mlp_ratio, True,
drop_rate, attn_drop_rate, dpr[i]) for i in range(depth)
])
self.norm = nn.LayerNorm(embed_dim)
self.head = nn.Linear(embed_dim, num_classes) # Final classifier head
self.apply(self._init_weights)
def _init_weights(self, m):
if isinstance(m, nn.Linear):
nn.init.trunc_normal_(m.weight, std=.02)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1.0)
def forward(self, x):
x = self.patch_embed(x)
x = self.pos_drop(x)
x = self.blocks(x)
x = self.norm(x)
return self.head(x[:, 0]) # Return only CLS token output
# ===== MODEL, LOSS, OPTIMIZER, SCHEDULER =====
model = VisionTransformer(...).to(device)
print(model)
print(f"Total parameters: {sum(p.numel() for p in model.parameters()):,}")
criterion = nn.CrossEntropyLoss(label_smoothing=0.1)
optimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)
scheduler = optim.lr_scheduler.SequentialLR(
optimizer,
[
optim.lr_scheduler.LinearLR(optimizer, start_factor=0.01, total_iters=WARMUP_EPOCHS),
optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=EPOCHS - WARMUP_EPOCHS)
],
milestones=[WARMUP_EPOCHS]
)
# ===== TRAINING FUNCTION =====
def train(model, loader, optimizer, criterion):
model.train() # Set model to training mode
total_loss, correct = 0, 0 # Initialize accumulators
for x, y in loader:
x, y = x.to(device), y.to(device) # Move data to device
optimizer.zero_grad() # Reset gradients
out = model(x) # Forward pass
loss = criterion(out, y) # Compute loss
loss.backward() # Backpropagate
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0) # Clip gradients to avoid explosion
optimizer.step() # Update weights
total_loss += loss.item() * x.size(0) # Accumulate weighted loss
correct += (out.argmax(1) == y).sum().item() # Count correct predictions
scheduler.step() # Update learning rate schedule
return total_loss / len(loader.dataset), correct / len(loader.dataset) # Average loss and accuracy
# ===== EVALUATION FUNCTION =====
def evaluate(model, loader, return_predictions=False):
model.eval() # Evaluation mode
correct = 0
all_preds = []
all_targets = []
with torch.inference_mode(): # Disable gradient computation
for x, y in loader:
x, y = x.to(device), y.to(device)
out = model(x)
preds = out.argmax(dim=1)
correct += (preds == y).sum().item()
if return_predictions:
all_preds.extend(preds.cpu().numpy())
all_targets.extend(y.cpu().numpy())
if return_predictions:
return correct / len(loader.dataset), (all_preds, all_targets)
return correct / len(loader.dataset)
# ===== TRAINING CURVE PLOT =====
def plot_training_curves(train_losses, train_accs, test_accs, lr_history):
plt.figure(figsize=(18, 6))
# Loss plot
plt.subplot(1, 3, 1)
plt.plot(train_losses, color='#2E86AB', linewidth=2)
plt.title('Training Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.grid(True)
# Accuracy plot
plt.subplot(1, 3, 2)
plt.plot(train_accs, color='#A23B72', linewidth=2, label='Train')
plt.plot(test_accs, color='#F18F01', linewidth=2, label='Test')
plt.title('Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.grid(True)
# Learning rate schedule
plt.subplot(1, 3, 3)
plt.plot(lr_history, color='#3B1F2B', linewidth=2)
plt.title('Learning Rate')
plt.xlabel('Epoch')
plt.ylabel('LR')
plt.grid(True)
plt.tight_layout()
plt.show()
# ===== CONFUSION MATRIX PLOT =====
def plot_confusion_matrix(model, loader, class_names):
model.eval()
_, (all_preds, all_targets) = evaluate(model, loader, return_predictions=True)
cm = confusion_matrix(all_targets, all_preds) # Compute confusion matrix
plt.figure(figsize=(12, 10))
sns.heatmap(cm, annot=True, fmt='d', cmap=custom_cmap,
xticklabels=class_names, yticklabels=class_names,
cbar_kws={'label': 'Count'})
plt.xlabel('Predicted')
plt.ylabel('True')
plt.title('Confusion Matrix')
plt.xticks(rotation=45)
plt.yticks(rotation=0)
plt.show()
# ===== PREDICTION VISUALIZATION =====
def visualize_predictions(model, dataset, classes, n_images=9):
model.eval()
plt.figure(figsize=(12, 12))
indices = random.sample(range(len(dataset)), n_images)
for i, idx in enumerate(indices):
img, true_label = dataset[idx]
input_tensor = img.unsqueeze(0).to(device)
with torch.inference_mode():
output = model(input_tensor)
probs = F.softmax(output, dim=1)
confidence, predicted = torch.max(probs, 1)
# Unnormalize image
img = img.cpu().numpy()
img = np.transpose(img, (1, 2, 0))
img = img * np.array([0.2470, 0.2435, 0.2616]) + np.array([0.4914, 0.4822, 0.4465])
img = np.clip(img, 0, 1)
plt.subplot(3, 3, i + 1)
plt.imshow(img)
truth = classes[true_label] == classes[predicted.item()]
color = "#2E86AB" if truth else "#C73E1D"
title = f"True: {classes[true_label]}\nPred: {classes[predicted.item()]}\nConf: {confidence.item():.2f}"
plt.title(title, color=color, fontsize=10)
plt.axis("off")
plt.tight_layout()
plt.show()
# ===== ATTENTION MAP VISUALIZATION =====
def visualize_attention(model, dataset, idx=0, layer=0):
model.eval()
img, label = dataset[idx]
input_tensor = img.unsqueeze(0).to(device)
with torch.no_grad():
x = model.patch_embed(input_tensor)
for i, blk in enumerate(model.blocks):
x = blk(x)
if i == layer:
attn_weights = blk.attention_weights
# Process attention weights
attn_weights = attn_weights.mean(dim=1) # Average over heads
cls_attn = attn_weights[0, 0, 1:] # Get attention from CLS token to patches
grid_size = int(np.sqrt(cls_attn.size(0)))
attn_map = cls_attn.reshape(grid_size, grid_size).cpu().numpy()
# Plot original image and attention
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
# Unnormalize image
img = img.cpu().numpy()
img = np.transpose(img, (1, 2, 0))
img = img * np.array([0.2470, 0.2435, 0.2616]) + np.array([0.4914, 0.4822, 0.4465])
img = np.clip(img, 0, 1)
ax1.imshow(img)
ax1.set_title(f"Original Image\nLabel: {dataset.classes[label]}", fontsize=12)
ax1.axis('off')
im = ax2.imshow(attn_map, cmap=custom_cmap, interpolation='lanczos')
ax2.set_title(f"Attention Map (Layer {layer})", fontsize=12)
fig.colorbar(im, ax=ax2, fraction=0.046, pad=0.04)
ax2.axis('off')
plt.tight_layout()
plt.show()
# ===== MAIN TRAINING LOOP =====
def train_model():
train_losses = []
train_accs = []
test_accs = []
lr_history = []
best_acc = 0.0
best_model = None
for epoch in tqdm(range(EPOCHS), desc="Training"):
train_loss, train_acc = train(model, train_loader, optimizer, criterion)
test_acc = evaluate(model, test_loader)
train_losses.append(train_loss)
train_accs.append(train_acc)
test_accs.append(test_acc)
lr_history.append(optimizer.param_groups[0]['lr'])
# Save best model
if test_acc > best_acc:
best_acc = test_acc
best_model = model.state_dict().copy()
# Log every 10 epochs
if (epoch + 1) % 10 == 0:
print(f"Epoch {epoch+1}/{EPOCHS}: "
f"Train Loss: {train_loss:.4f}, "
f"Train Acc: {train_acc*100:.2f}%, "
f"Test Acc: {test_acc*100:.2f}%")
model.load_state_dict(best_model) # Load best weights
plot_training_curves(train_losses, train_accs, test_accs, lr_history)
return train_losses, train_accs, test_accs
# ===== RUN TRAINING AND VISUALIZATION =====
train_losses, train_accs, test_accs = train_model() # Train the model
plot_confusion_matrix(model, test_loader, train_dataset.classes) # Show confusion matrix
visualize_predictions(model, test_dataset, train_dataset.classes) # Show sample predictions