-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_vit_hugg.py
166 lines (146 loc) · 5.48 KB
/
train_vit_hugg.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
# %%
import matplotlib.pyplot as plt
import numpy as np
from torch.autograd import Variable
import torch.utils.data as data
import torch
from torchvision import transforms
from transformers import ViTFeatureExtractor
import torch.nn.functional as F
import torch.nn as nn
from transformers import ViTModel
import torchvision
from PIL import Image
# %%
transform = transforms.Compose(
[transforms.Resize(224), transforms.ToTensor()])
train_ds = torchvision.datasets.ImageFolder(
'tiny-imagenet-200/train/', transform=transform)
# %%
def default_loader(path):
return Image.open(path).convert('RGB')
class EvalDataset(data.Dataset):
def __init__(self, folder, transform=None, target_transform=None, loader=default_loader):
fh = open(folder + 'val_annotations.txt', 'r')
imgs = []
for line in fh:
line = line.strip('\n')
line = line.rstrip()
words = line.split()
label = list(train_ds.class_to_idx.keys()).index(words[1])
imgs.append((folder + 'images/' + words[0], label))
self.imgs = imgs
self.transform = transform
self.target_transform = target_transform
self.loader = loader
def __getitem__(self, index):
fn, label = self.imgs[index]
img = self.loader(fn)
if self.transform is not None:
img = self.transform(img)
return img, label
def __len__(self):
return len(self.imgs)
eval_ds=EvalDataset(folder='tiny-imagenet-200/val/', transform=transform)
test_ds=EvalDataset(folder='tiny-imagenet-200/val/', transform=transform)
# %%
class ViTForImageClassification(nn.Module):
def __init__(self, num_labels=3):
super(ViTForImageClassification, self).__init__()
self.vit = ViTModel.from_pretrained(
'google/vit-base-patch16-224-in21k')
self.dropout = nn.Dropout(0.1)
self.classifier = nn.Linear(self.vit.config.hidden_size, num_labels)
self.num_labels = num_labels
def forward(self, pixel_values, labels):
outputs = self.vit(pixel_values=pixel_values)
output = self.dropout(outputs.last_hidden_state[:, 0])
logits = self.classifier(output)
loss = None
if labels is not None:
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
if loss is not None:
return logits, loss.item()
else:
return logits, None
# %%
EPOCHS = 3
BATCH_SIZE = 32
LEARNING_RATE = 2e-5
# %%
# %%
# Define Model
model = ViTForImageClassification(len(train_ds.classes))
# Feature Extractor
feature_extractor = ViTFeatureExtractor.from_pretrained(
'google/vit-base-patch16-224-in21k')
# Adam Optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)
# Cross Entropy Loss
loss_func = nn.CrossEntropyLoss()
# Use GPU if available
device = 3
if torch.cuda.is_available():
model.to(3)
# %%
torch.cuda.is_available()
# %%
print("Number of train samples: ", len(train_ds))
print("Number of test samples: ", len(test_ds))
print("Detected Classes are: ", train_ds.class_to_idx)
train_loader = data.DataLoader(
train_ds, batch_size=BATCH_SIZE, shuffle=True)
test_loader = data.DataLoader(
test_ds, batch_size=BATCH_SIZE, shuffle=True)
eval_loader = data.DataLoader(
eval_ds, batch_size=1, shuffle=False)
# %%
# Train the model
for epoch in range(EPOCHS):
for step, (x, y) in enumerate(train_loader):
# Change input array into list with each batch being one element
x = np.split(np.squeeze(np.array(x)), BATCH_SIZE)
# Remove unecessary dimension
for index, array in enumerate(x):
x[index] = np.squeeze(array)
# Apply feature extractor, stack back into 1 tensor and then convert to tensor
x = torch.tensor(
np.stack(feature_extractor(x)['pixel_values'], axis=0))
# Send to GPU if available
if torch.cuda.is_available():
x, y = x.to(device), y.to(device)
b_x = Variable(x) # batch x (image)
b_y = Variable(y) # batch y (target)
# Feed through model
output, loss = model(b_x, None)
print(output.shape, b_y.shape)
# Calculate loss
if loss is None:
loss = loss_func(output, b_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step % 50 == 0:
# Get the next batch for testing purposes
test = next(iter(test_loader))
test_x = test[0]
# Reshape and get feature matrices as needed
test_x = np.split(np.squeeze(np.array(test_x)), BATCH_SIZE)
for index, array in enumerate(test_x):
test_x[index] = np.squeeze(array)
test_x = torch.tensor(
np.stack(feature_extractor(test_x)['pixel_values'], axis=0))
# Send to appropirate computing device
test_y = torch.tensor(test[1])
if torch.cuda.is_available():
test_x = test_x.to(device)
test_y = test_y.to(device)
# Get output (+ respective class) and compare to target
test_output, loss = model(test_x, test_y)
test_output = test_output.argmax(1)
# Calculate Accuracy
accuracy = (test_output == test_y).sum().item() / BATCH_SIZE
print('Epoch: ', epoch, '| train loss: %.4f' % loss, '| test accuracy: %.2f' % accuracy)
torch.save(model.state_dict(), "checkpoints/vit_pretrained.pth")
# %%