forked from ZJUCDSYangKaifan/GEVit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_vit.py
More file actions
250 lines (215 loc) · 9.02 KB
/
Copy pathtrain_vit.py
File metadata and controls
250 lines (215 loc) · 9.02 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
import copy
import os
import torch
import torch.nn as nn
import wandb
from torchvision import transforms
from datasets import MNIST_rot
from g_selfatt.utils import num_params
# https://lightning.ai/docs/pytorch/stable/notebooks/course_UvA-DL/11-vision-transformer.html
def img_to_patch(x, patch_size, flatten_channels=True):
"""
Args:
x: Tensor representing the image of shape [B, C, H, W]
patch_size: Number of pixels per dimension of the patches (integer)
flatten_channels: If True, the patches will be returned in a flattened format
as a feature vector instead of a image grid.
"""
B, C, H, W = x.shape
x = x.reshape(B, C, H // patch_size, patch_size, W // patch_size, patch_size)
x = x.permute(0, 2, 4, 1, 3, 5) # [B, H', W', C, p_H, p_W]
x = x.flatten(1, 2) # [B, H'*W', C, p_H, p_W]
if flatten_channels:
x = x.flatten(2, 4) # [B, H'*W', C*p_H*p_W]
return x
class AttentionBlock(nn.Module):
def __init__(self, embed_dim, hidden_dim, num_heads, dropout=0.0):
"""Attention Block.
Args:
embed_dim: Dimensionality of input and attention feature vectors
hidden_dim: Dimensionality of hidden layer in feed-forward network
(usually 2-4x larger than embed_dim)
num_heads: Number of heads to use in the Multi-Head Attention block
dropout: Amount of dropout to apply in the feed-forward network
"""
super().__init__()
self.layer_norm_1 = nn.LayerNorm(embed_dim)
self.attn = nn.MultiheadAttention(embed_dim, num_heads)
self.layer_norm_2 = nn.LayerNorm(embed_dim)
self.linear = nn.Sequential(
nn.Linear(embed_dim, hidden_dim),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, embed_dim),
nn.Dropout(dropout),
)
def forward(self, x):
inp_x = self.layer_norm_1(x)
x = x + self.attn(inp_x, inp_x, inp_x)[0]
x = x + self.linear(self.layer_norm_2(x))
return x
class VisionTransformer(nn.Module):
def __init__(
self,
embed_dim,
hidden_dim,
num_channels,
num_heads,
num_layers,
num_classes,
patch_size,
num_patches,
dropout=0.0,
):
"""Vision Transformer.
Args:
embed_dim: Dimensionality of the input feature vectors to the Transformer
hidden_dim: Dimensionality of the hidden layer in the feed-forward networks
within the Transformer
num_channels: Number of channels of the input (3 for RGB)
num_heads: Number of heads to use in the Multi-Head Attention block
num_layers: Number of layers to use in the Transformer
num_classes: Number of classes to predict
patch_size: Number of pixels that the patches have per dimension
num_patches: Maximum number of patches an image can have
dropout: Amount of dropout to apply in the feed-forward network and
on the input encoding
"""
super().__init__()
self.patch_size = patch_size
# Layers/Networks
self.input_layer = nn.Linear(num_channels * (patch_size**2), embed_dim)
self.transformer = nn.Sequential(
*(AttentionBlock(embed_dim, hidden_dim, num_heads, dropout=dropout) for _ in range(num_layers))
)
self.mlp_head = nn.Sequential(nn.LayerNorm(embed_dim), nn.Linear(embed_dim, num_classes))
self.dropout = nn.Dropout(dropout)
# Parameters/Embeddings
self.cls_token = nn.Parameter(torch.randn(1, 1, embed_dim))
self.pos_embedding = nn.Parameter(torch.randn(1, 1 + num_patches, embed_dim))
def forward(self, x, output_cls=False):
# Preprocess input
x = img_to_patch(x, self.patch_size)
B, T, _ = x.shape
x = self.input_layer(x)
# Add CLS token and positional encoding
cls_token = self.cls_token.repeat(B, 1, 1)
x = torch.cat([cls_token, x], dim=1)
x = x + self.pos_embedding[:, : T + 1]
# Apply Transforrmer
x = self.dropout(x)
x = x.transpose(0, 1)
x = self.transformer(x)
# Perform classification prediction
cls = x[0]
if output_cls:
return cls
out = self.mlp_head(cls)
return out
def main():
os.environ["WANDB_API_KEY"] = "691777d26bb25439a75be52632da71d865d3a671" # TODO change this if we are doing serious runs
wandb.init(
project="non-equivariant-vit",
entity="equivatt_team",
)
data_mean = (0.1307,)
data_stddev = (0.3081,)
transform_train = transforms.Compose([
transforms.RandomRotation(degrees=(-180, 180)), # Random rotation
transforms.RandomHorizontalFlip(), # Random horizontal flip with a probability of 0.5
transforms.RandomVerticalFlip(),
transforms.ToTensor(),
transforms.Normalize(data_mean, data_stddev)
])
transform_test = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(data_mean, data_stddev),
]
)
train_set = MNIST_rot(root="../data", stage="train", download=True, transform=transform_train, data_fraction=1, only_3_and_8=False)
validation_set = MNIST_rot(root="../data", stage="validation", download=True, transform=transform_test, data_fraction=1, only_3_and_8=False)
test_set = MNIST_rot(root="../data", stage="test", download=True, transform=transform_test, data_fraction=1, only_3_and_8=False)
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=128,
shuffle=True,
num_workers=4,
)
val_loader = torch.utils.data.DataLoader(
validation_set,
batch_size=128,
shuffle=True,
num_workers=4,
)
test_loader = torch.utils.data.DataLoader(
test_set,
batch_size=128,
shuffle=False,
num_workers=4,
)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = VisionTransformer(embed_dim=64,
hidden_dim=512,
num_heads=4,
num_layers=6,
patch_size=4,
num_channels=1,
num_patches=49,
num_classes=10,
dropout=0.1).to(device)
print(f"Number of parameters in the model: {num_params(model)}")
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), 0.001)
best_model = copy.deepcopy(model.state_dict())
best_val_acc = 0
for epoch in range(500):
model.train()
losses = []
for inputs, labels in train_loader:
inputs, labels = inputs.to(device), labels.to(device) # Move inputs and labels to device
optimizer.zero_grad()
out = model(inputs)
loss = criterion(out, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step() # Update weights
losses.append(loss.item())
wandb.log({"loss_train":sum(losses)/len(losses)}, step=epoch+1)
# Validate on the validation set
if epoch % 10 == 0:
model.eval() # Set the model to evaluation mode
correct = 0
total = 0
with torch.no_grad(): # Disable gradient calculation during inference
for inputs, labels in val_loader:
inputs, labels = inputs.to(device), labels.to(device) # Move inputs and labels to device
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
if accuracy > best_val_acc:
best_model = copy.deepcopy(model.state_dict())
best_val_acc = accuracy
wandb.log({"validation_accuracy":accuracy}, step=epoch+1)
wandb.run.summary["best_validation_accuracy"] = best_val_acc
# Test on the test set
model.eval() # Set the model to evaluation mode
correct = 0
total = 0
with torch.no_grad(): # Disable gradient calculation during inference
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device) # Move inputs and labels to device
outputs = model(inputs)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
test_acc = 100 * correct / total
wandb.run.summary["test_acc"] = test_acc
# save model and log it
model.load_state_dict(best_model)
torch.save(model.state_dict(), "saved/model.pt")
torch.save(model.state_dict(), os.path.join(wandb.run.dir, "model.pt"))
if __name__ == "__main__":
main()