-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvision_transformers.py
697 lines (604 loc) · 28.1 KB
/
vision_transformers.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# -*- coding: utf-8 -*-
"""Vision Transformers.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/10AYlqsACfMiuMiMSVQjcW8NnkGiJrHLh
"""
#@title ViT Implementation 🔥
import math
import torch
from torch import nn
class NewGELUActivation(nn.Module):
"""
Implementation of the GELU activation function currently in Google BERT repo (identical to OpenAI GPT). Also see
the Gaussian Error Linear Units paper: https://arxiv.org/abs/1606.08415
Taken from https://github.com/huggingface/transformers/blob/main/src/transformers/activations.py
"""
def forward(self, input):
return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi) * (input + 0.044715 * torch.pow(input, 3.0))))
class PatchEmbeddings(nn.Module):
"""
Convert the image into patches and then project them into a vector space.
"""
def __init__(self, config):
super().__init__()
self.image_size = config["image_size"]
self.patch_size = config["patch_size"]
self.num_channels = config["num_channels"]
self.hidden_size = config["hidden_size"]
# Calculate the number of patches from the image size and patch size
self.num_patches = (self.image_size // self.patch_size) ** 2
# Create a projection layer to convert the image into patches
# The layer projects each patch into a vector of size hidden_size
self.projection = nn.Conv2d(self.num_channels, self.hidden_size, kernel_size=self.patch_size, stride=self.patch_size)
def forward(self, x):
# (batch_size, num_channels, image_size, image_size) -> (batch_size, num_patches, hidden_size)
x = self.projection(x)
x = x.flatten(2).transpose(1, 2)
return x
class Embeddings(nn.Module):
"""
Combine the patch embeddings with the class token and position embeddings.
"""
def __init__(self, config):
super().__init__()
self.config = config
self.patch_embeddings = PatchEmbeddings(config)
# Create a learnable [CLS] token
# Similar to BERT, the [CLS] token is added to the beginning of the input sequence
# and is used to classify the entire sequence
self.cls_token = nn.Parameter(torch.randn(1, 1, config["hidden_size"]))
# Create position embeddings for the [CLS] token and the patch embeddings
# Add 1 to the sequence length for the [CLS] token
self.position_embeddings = \
nn.Parameter(torch.randn(1, self.patch_embeddings.num_patches + 1, config["hidden_size"]))
self.dropout = nn.Dropout(config["hidden_dropout_prob"])
def forward(self, x):
x = self.patch_embeddings(x)
batch_size, _, _ = x.size()
# Expand the [CLS] token to the batch size
# (1, 1, hidden_size) -> (batch_size, 1, hidden_size)
cls_tokens = self.cls_token.expand(batch_size, -1, -1)
# Concatenate the [CLS] token to the beginning of the input sequence
# This results in a sequence length of (num_patches + 1)
x = torch.cat((cls_tokens, x), dim=1)
x = x + self.position_embeddings
x = self.dropout(x)
return x
class AttentionHead(nn.Module):
"""
A single attention head.
This module is used in the MultiHeadAttention module.
"""
def __init__(self, hidden_size, attention_head_size, dropout, bias=True):
super().__init__()
self.hidden_size = hidden_size
self.attention_head_size = attention_head_size
# Create the query, key, and value projection layers
self.query = nn.Linear(hidden_size, attention_head_size, bias=bias)
self.key = nn.Linear(hidden_size, attention_head_size, bias=bias)
self.value = nn.Linear(hidden_size, attention_head_size, bias=bias)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
# Project the input into query, key, and value
# The same input is used to generate the query, key, and value,
# so it's usually called self-attention.
# (batch_size, sequence_length, hidden_size) -> (batch_size, sequence_length, attention_head_size)
query = self.query(x)
key = self.key(x)
value = self.value(x)
# Calculate the attention scores
# softmax(Q*K.T/sqrt(head_size))*V
attention_scores = torch.matmul(query, key.transpose(-1, -2))
attention_scores = attention_scores / math.sqrt(self.attention_head_size)
attention_probs = nn.functional.softmax(attention_scores, dim=-1)
attention_probs = self.dropout(attention_probs)
# Calculate the attention output
attention_output = torch.matmul(attention_probs, value)
return (attention_output, attention_probs)
class MultiHeadAttention(nn.Module):
"""
Multi-head attention module.
This module is used in the TransformerEncoder module.
"""
def __init__(self, config):
super().__init__()
self.hidden_size = config["hidden_size"]
self.num_attention_heads = config["num_attention_heads"]
# The attention head size is the hidden size divided by the number of attention heads
self.attention_head_size = self.hidden_size // self.num_attention_heads
self.all_head_size = self.num_attention_heads * self.attention_head_size
# Whether or not to use bias in the query, key, and value projection layers
self.qkv_bias = config["qkv_bias"]
# Create a list of attention heads
self.heads = nn.ModuleList([])
for _ in range(self.num_attention_heads):
head = AttentionHead(
self.hidden_size,
self.attention_head_size,
config["attention_probs_dropout_prob"],
self.qkv_bias
)
self.heads.append(head)
# Create a linear layer to project the attention output back to the hidden size
# In most cases, all_head_size and hidden_size are the same
self.output_projection = nn.Linear(self.all_head_size, self.hidden_size)
self.output_dropout = nn.Dropout(config["hidden_dropout_prob"])
def forward(self, x, output_attentions=False):
# Calculate the attention output for each attention head
attention_outputs = [head(x) for head in self.heads]
# Concatenate the attention outputs from each attention head
attention_output = torch.cat([attention_output for attention_output, _ in attention_outputs], dim=-1)
# Project the concatenated attention output back to the hidden size
attention_output = self.output_projection(attention_output)
attention_output = self.output_dropout(attention_output)
# Return the attention output and the attention probabilities (optional)
if not output_attentions:
return (attention_output, None)
else:
attention_probs = torch.stack([attention_probs for _, attention_probs in attention_outputs], dim=1)
return (attention_output, attention_probs)
class FasterMultiHeadAttention(nn.Module):
"""
Multi-head attention module with some optimizations.
All the heads are processed simultaneously with merged query, key, and value projections.
"""
def __init__(self, config):
super().__init__()
self.hidden_size = config["hidden_size"]
self.num_attention_heads = config["num_attention_heads"]
# The attention head size is the hidden size divided by the number of attention heads
self.attention_head_size = self.hidden_size // self.num_attention_heads
self.all_head_size = self.num_attention_heads * self.attention_head_size
# Whether or not to use bias in the query, key, and value projection layers
self.qkv_bias = config["qkv_bias"]
# Create a linear layer to project the query, key, and value
self.qkv_projection = nn.Linear(self.hidden_size, self.all_head_size * 3, bias=self.qkv_bias)
self.attn_dropout = nn.Dropout(config["attention_probs_dropout_prob"])
# Create a linear layer to project the attention output back to the hidden size
# In most cases, all_head_size and hidden_size are the same
self.output_projection = nn.Linear(self.all_head_size, self.hidden_size)
self.output_dropout = nn.Dropout(config["hidden_dropout_prob"])
def forward(self, x, output_attentions=False):
# Project the query, key, and value
# (batch_size, sequence_length, hidden_size) -> (batch_size, sequence_length, all_head_size * 3)
qkv = self.qkv_projection(x)
# Split the projected query, key, and value into query, key, and value
# (batch_size, sequence_length, all_head_size * 3) -> (batch_size, sequence_length, all_head_size)
query, key, value = torch.chunk(qkv, 3, dim=-1)
# Resize the query, key, and value to (batch_size, num_attention_heads, sequence_length, attention_head_size)
batch_size, sequence_length, _ = query.size()
query = query.view(batch_size, sequence_length, self.num_attention_heads, self.attention_head_size).transpose(1, 2)
key = key.view(batch_size, sequence_length, self.num_attention_heads, self.attention_head_size).transpose(1, 2)
value = value.view(batch_size, sequence_length, self.num_attention_heads, self.attention_head_size).transpose(1, 2)
# Calculate the attention scores
# softmax(Q*K.T/sqrt(head_size))*V
attention_scores = torch.matmul(query, key.transpose(-1, -2))
attention_scores = attention_scores / math.sqrt(self.attention_head_size)
attention_probs = nn.functional.softmax(attention_scores, dim=-1)
attention_probs = self.attn_dropout(attention_probs)
# Calculate the attention output
attention_output = torch.matmul(attention_probs, value)
# Resize the attention output
# from (batch_size, num_attention_heads, sequence_length, attention_head_size)
# To (batch_size, sequence_length, all_head_size)
attention_output = attention_output.transpose(1, 2) \
.contiguous() \
.view(batch_size, sequence_length, self.all_head_size)
# Project the attention output back to the hidden size
attention_output = self.output_projection(attention_output)
attention_output = self.output_dropout(attention_output)
# Return the attention output and the attention probabilities (optional)
if not output_attentions:
return (attention_output, None)
else:
return (attention_output, attention_probs)
class MLP(nn.Module):
"""
A multi-layer perceptron module.
"""
def __init__(self, config):
super().__init__()
self.dense_1 = nn.Linear(config["hidden_size"], config["intermediate_size"])
self.activation = NewGELUActivation()
self.dense_2 = nn.Linear(config["intermediate_size"], config["hidden_size"])
self.dropout = nn.Dropout(config["hidden_dropout_prob"])
def forward(self, x):
x = self.dense_1(x)
x = self.activation(x)
x = self.dense_2(x)
x = self.dropout(x)
return x
class Block(nn.Module):
"""
A single transformer block.
"""
def __init__(self, config):
super().__init__()
self.use_faster_attention = config.get("use_faster_attention", False)
if self.use_faster_attention:
self.attention = FasterMultiHeadAttention(config)
else:
self.attention = MultiHeadAttention(config)
self.layernorm_1 = nn.LayerNorm(config["hidden_size"])
self.mlp = MLP(config)
self.layernorm_2 = nn.LayerNorm(config["hidden_size"])
def forward(self, x, output_attentions=False):
# Self-attention
attention_output, attention_probs = \
self.attention(self.layernorm_1(x), output_attentions=output_attentions)
# Skip connection
x = x + attention_output
# Feed-forward network
mlp_output = self.mlp(self.layernorm_2(x))
# Skip connection
x = x + mlp_output
# Return the transformer block's output and the attention probabilities (optional)
if not output_attentions:
return (x, None)
else:
return (x, attention_probs)
class Encoder(nn.Module):
"""
The transformer encoder module.
"""
def __init__(self, config):
super().__init__()
# Create a list of transformer blocks
self.blocks = nn.ModuleList([])
for _ in range(config["num_hidden_layers"]):
block = Block(config)
self.blocks.append(block)
def forward(self, x, output_attentions=False):
# Calculate the transformer block's output for each block
all_attentions = []
for block in self.blocks:
x, attention_probs = block(x, output_attentions=output_attentions)
if output_attentions:
all_attentions.append(attention_probs)
# Return the encoder's output and the attention probabilities (optional)
if not output_attentions:
return (x, None)
else:
return (x, all_attentions)
class ViTForClassfication(nn.Module):
"""
The ViT model for classification.
"""
def __init__(self, config):
super().__init__()
self.config = config
self.image_size = config["image_size"]
self.hidden_size = config["hidden_size"]
self.num_classes = config["num_classes"]
# Create the embedding module
self.embedding = Embeddings(config)
# Create the transformer encoder module
self.encoder = Encoder(config)
# Create a linear layer to project the encoder's output to the number of classes
self.classifier = nn.Linear(self.hidden_size, self.num_classes)
# Initialize the weights
self.apply(self._init_weights)
def forward(self, x, output_attentions=False):
# Calculate the embedding output
embedding_output = self.embedding(x)
# Calculate the encoder's output
encoder_output, all_attentions = self.encoder(embedding_output, output_attentions=output_attentions)
# Calculate the logits, take the [CLS] token's output as features for classification
logits = self.classifier(encoder_output[:, 0, :])
# Return the logits and the attention probabilities (optional)
if not output_attentions:
return (logits, None)
else:
return (logits, all_attentions)
def _init_weights(self, module):
if isinstance(module, (nn.Linear, nn.Conv2d)):
torch.nn.init.normal_(module.weight, mean=0.0, std=self.config["initializer_range"])
if module.bias is not None:
torch.nn.init.zeros_(module.bias)
elif isinstance(module, nn.LayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
elif isinstance(module, Embeddings):
module.position_embeddings.data = nn.init.trunc_normal_(
module.position_embeddings.data.to(torch.float32),
mean=0.0,
std=self.config["initializer_range"],
).to(module.position_embeddings.dtype)
module.cls_token.data = nn.init.trunc_normal_(
module.cls_token.data.to(torch.float32),
mean=0.0,
std=self.config["initializer_range"],
).to(module.cls_token.dtype)
#@title Prepare Data 📊
# Import libraries
import torch
import torchvision
import torchvision.transforms as transforms
def prepare_data(batch_size=4, num_workers=2, train_sample_size=None, test_sample_size=None):
train_transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Resize((32, 32)),
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomResizedCrop((32, 32), scale=(0.8, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=2),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True, transform=train_transform)
if train_sample_size is not None:
# Randomly sample a subset of the training set
indices = torch.randperm(len(trainset))[:train_sample_size]
trainset = torch.utils.data.Subset(trainset, indices)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
shuffle=True, num_workers=num_workers)
test_transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Resize((32, 32)),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
testset = torchvision.datasets.CIFAR10(root='./data', train=False,
download=True, transform=test_transform)
if test_sample_size is not None:
# Randomly sample a subset of the test set
indices = torch.randperm(len(testset))[:test_sample_size]
testset = torch.utils.data.Subset(testset, indices)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
shuffle=False, num_workers=num_workers)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
return trainloader, testloader, classes
#@title Utils 🛠️
import json, os, math
import matplotlib.pyplot as plt
import numpy as np
import torch
from torch.nn import functional as F
import torchvision
import torchvision.transforms as transforms
def save_experiment(experiment_name, config, model, train_losses, test_losses, accuracies, base_dir="experiments"):
outdir = os.path.join(base_dir, experiment_name)
os.makedirs(outdir, exist_ok=True)
# Save the config
configfile = os.path.join(outdir, 'config.json')
with open(configfile, 'w') as f:
json.dump(config, f, sort_keys=True, indent=4)
# Save the metrics
jsonfile = os.path.join(outdir, 'metrics.json')
with open(jsonfile, 'w') as f:
data = {
'train_losses': train_losses,
'test_losses': test_losses,
'accuracies': accuracies,
}
json.dump(data, f, sort_keys=True, indent=4)
# Save the model
save_checkpoint(experiment_name, model, "final", base_dir=base_dir)
def save_checkpoint(experiment_name, model, epoch, base_dir="experiments"):
outdir = os.path.join(base_dir, experiment_name)
os.makedirs(outdir, exist_ok=True)
cpfile = os.path.join(outdir, f'model_{epoch}.pt')
torch.save(model.state_dict(), cpfile)
def load_experiment(experiment_name, checkpoint_name="model_final.pt", base_dir="experiments"):
outdir = os.path.join(base_dir, experiment_name)
# Load the config
configfile = os.path.join(outdir, 'config.json')
with open(configfile, 'r') as f:
config = json.load(f)
# Load the metrics
jsonfile = os.path.join(outdir, 'metrics.json')
with open(jsonfile, 'r') as f:
data = json.load(f)
train_losses = data['train_losses']
test_losses = data['test_losses']
accuracies = data['accuracies']
# Load the model
model = ViTForClassfication(config)
cpfile = os.path.join(outdir, checkpoint_name)
# Use map_location to load the model on CPU if CUDA is not available
model.load_state_dict(torch.load(cpfile, map_location=torch.device('cpu')))
return config, model, train_losses, test_losses, accuracies
def visualize_images():
trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
download=True)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# Pick 30 samples randomly
indices = torch.randperm(len(trainset))[:30]
images = [np.asarray(trainset[i][0]) for i in indices]
labels = [trainset[i][1] for i in indices]
# Visualize the images using matplotlib
fig = plt.figure(figsize=(10, 10))
for i in range(30):
ax = fig.add_subplot(6, 5, i+1, xticks=[], yticks=[])
ax.imshow(images[i])
ax.set_title(classes[labels[i]])
@torch.no_grad()
def visualize_attention(model, output=None, device="cpu"):
"""
Visualize the attention maps of the first 4 images.
"""
model.eval()
# Load random images
num_images = 30
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True)
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
# Pick 30 samples randomly
indices = torch.randperm(len(testset))[:num_images]
raw_images = [np.asarray(testset[i][0]) for i in indices]
labels = [testset[i][1] for i in indices]
# Convert the images to tensors
test_transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Resize((32, 32)),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
images = torch.stack([test_transform(image) for image in raw_images])
# Move the images to the device
images = images.to(device)
model = model.to(device)
# Get the attention maps from the last block
logits, attention_maps = model(images, output_attentions=True)
# Get the predictions
predictions = torch.argmax(logits, dim=1)
# Concatenate the attention maps from all blocks
attention_maps = torch.cat(attention_maps, dim=1)
# select only the attention maps of the CLS token
attention_maps = attention_maps[:, :, 0, 1:]
# Then average the attention maps of the CLS token over all the heads
attention_maps = attention_maps.mean(dim=1)
# Reshape the attention maps to a square
num_patches = attention_maps.size(-1)
size = int(math.sqrt(num_patches))
attention_maps = attention_maps.view(-1, size, size)
# Resize the map to the size of the image
attention_maps = attention_maps.unsqueeze(1)
attention_maps = F.interpolate(attention_maps, size=(32, 32), mode='bilinear', align_corners=False)
attention_maps = attention_maps.squeeze(1)
# Plot the images and the attention maps
fig = plt.figure(figsize=(20, 10))
mask = np.concatenate([np.ones((32, 32)), np.zeros((32, 32))], axis=1)
for i in range(num_images):
ax = fig.add_subplot(6, 5, i+1, xticks=[], yticks=[])
img = np.concatenate((raw_images[i], raw_images[i]), axis=1)
ax.imshow(img)
# Mask out the attention map of the left image
extended_attention_map = np.concatenate((np.zeros((32, 32)), attention_maps[i].cpu()), axis=1)
extended_attention_map = np.ma.masked_where(mask==1, extended_attention_map)
ax.imshow(extended_attention_map, alpha=0.5, cmap='jet')
# Show the ground truth and the prediction
gt = classes[labels[i]]
pred = classes[predictions[i]]
ax.set_title(f"gt: {gt} / pred: {pred}", color=("green" if gt==pred else "red"))
if output is not None:
plt.savefig(output)
plt.show()
#@title Train ViT 🧠 🏋🏽
#@title String fields
exp_name = 'vit-with-100-epochs' #@param {type:"string"}
batch_size = 256 #@param {type: "integer"}
epochs = 100 #@param {type: "integer"}
lr = 1e-2 #@param {type: "number"}
save_model_every = 10 #@param {type: "integer"}
import torch
from torch import nn, optim
device = "cuda" if torch.cuda.is_available() else "cpu"
config = {
"patch_size": 4, # Input image size: 32x32 -> 8x8 patches
"hidden_size": 48,
"num_hidden_layers": 4,
"num_attention_heads": 4,
"intermediate_size": 4 * 48, # 4 * hidden_size
"hidden_dropout_prob": 0.0,
"attention_probs_dropout_prob": 0.0,
"initializer_range": 0.02,
"image_size": 32,
"num_classes": 10, # num_classes of CIFAR10
"num_channels": 3,
"qkv_bias": True,
"use_faster_attention": True,
}
# These are not hard constraints, but are used to prevent misconfigurations
assert config["hidden_size"] % config["num_attention_heads"] == 0
assert config['intermediate_size'] == 4 * config['hidden_size']
assert config['image_size'] % config['patch_size'] == 0
class Trainer:
"""
The simple trainer.
"""
def __init__(self, model, optimizer, loss_fn, exp_name, device):
self.model = model.to(device)
self.optimizer = optimizer
self.loss_fn = loss_fn
self.exp_name = exp_name
self.device = device
def train(self, trainloader, testloader, epochs, save_model_every_n_epochs=0):
"""
Train the model for the specified number of epochs.
"""
# Keep track of the losses and accuracies
train_losses, test_losses, accuracies = [], [], []
# Train the model
for i in range(epochs):
train_loss = self.train_epoch(trainloader)
accuracy, test_loss = self.evaluate(testloader)
train_losses.append(train_loss)
test_losses.append(test_loss)
accuracies.append(accuracy)
print(f"Epoch: {i+1}, Train loss: {train_loss:.4f}, Test loss: {test_loss:.4f}, Accuracy: {accuracy:.4f}")
if save_model_every_n_epochs > 0 and (i+1) % save_model_every_n_epochs == 0 and i+1 != epochs:
print('\tSave checkpoint at epoch', i+1)
save_checkpoint(self.exp_name, self.model, i+1)
# Save the experiment
save_experiment(self.exp_name, config, self.model, train_losses, test_losses, accuracies)
def train_epoch(self, trainloader):
"""
Train the model for one epoch.
"""
self.model.train()
total_loss = 0
for batch in trainloader:
# Move the batch to the device
batch = [t.to(self.device) for t in batch]
images, labels = batch
# Zero the gradients
self.optimizer.zero_grad()
# Calculate the loss
loss = self.loss_fn(self.model(images)[0], labels)
# Backpropagate the loss
loss.backward()
# Update the model's parameters
self.optimizer.step()
total_loss += loss.item() * len(images)
return total_loss / len(trainloader.dataset)
@torch.no_grad()
def evaluate(self, testloader):
self.model.eval()
total_loss = 0
correct = 0
with torch.no_grad():
for batch in testloader:
# Move the batch to the device
batch = [t.to(self.device) for t in batch]
images, labels = batch
# Get predictions
logits, _ = self.model(images)
# Calculate the loss
loss = self.loss_fn(logits, labels)
total_loss += loss.item() * len(images)
# Calculate the accuracy
predictions = torch.argmax(logits, dim=1)
correct += torch.sum(predictions == labels).item()
accuracy = correct / len(testloader.dataset)
avg_loss = total_loss / len(testloader.dataset)
return accuracy, avg_loss
def main():
# Training parameters
save_model_every_n_epochs = save_model_every
# Load the CIFAR10 dataset
trainloader, testloader, _ = prepare_data(batch_size=batch_size)
# Create the model, optimizer, loss function and trainer
model = ViTForClassfication(config)
optimizer = optim.AdamW(model.parameters(), lr=lr, weight_decay=1e-2)
loss_fn = nn.CrossEntropyLoss()
trainer = Trainer(model, optimizer, loss_fn, exp_name, device=device)
trainer.train(trainloader, testloader, epochs, save_model_every_n_epochs=save_model_every_n_epochs)
if __name__ == '__main__':
main()
#@title Visualize Dataset
# Show some training images
visualize_images()
#@title Plot training Results
config, model, train_losses, test_losses, accuracies = load_experiment(f"{exp_name}/")
import matplotlib.pyplot as plt
# Create two subplots of train/test losses and accuracies
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5))
ax1.plot(train_losses, label="Train loss")
ax1.plot(test_losses, label="Test loss")
ax1.set_xlabel("Epoch")
ax1.set_ylabel("Loss")
ax1.legend()
ax2.plot(accuracies)
ax2.set_xlabel("Epoch")
ax2.set_ylabel("Accuracy")
plt.savefig("metrics.png")
plt.show()
#@title Visualize Attetion
visualize_attention(model, "attention.png")