-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainResnet18.py
167 lines (122 loc) · 5.13 KB
/
trainResnet18.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
import os
import os.path as osp
import time
%matplotlib inline
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torchvision.models as models
import torch.optim as optim
from torchvision import datasets
def train(model, optimizer, criterion, epoch, num_epochs):
model.train()
epoch_loss = 0.0
epoch_acc = 0.0
for batch_idx, (images, labels) in enumerate(dataloaders['train']):
#zero the parameter gradients
optimizer.zero_grad()
#move to GPU
images, labels = images.cuda(), labels.cuda()
#forward
outputs = model.forward(images)
loss = criterion(outputs, labels)
_, preds = torch.max(outputs.data, 1)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
epoch_acc += torch.sum(preds == labels).item()
epoch_loss /= dataset_sizes['train']
epoch_acc /= dataset_sizes['train']
print('TRAINING Epoch %d/%d Loss %.4f Accuracy %.4f' % (epoch, num_epochs, epoch_loss, epoch_acc))
def test(model, criterion, repeats=2):
model.eval()
test_loss = 0.0
test_acc = 0.0
with torch.no_grad():
for itr in range(repeats):
for batch_idx, (images, labels) in enumerate(dataloaders['test']):
#move to GPU
images, labels = images.cuda(), labels.cuda()
#forward
outputs = model.forward(images)
loss = criterion(outputs, labels)
_, preds = torch.max(outputs.data, 1)
test_loss += loss.item()
test_acc += torch.sum(preds == labels).item()
test_loss /= (dataset_sizes['test']*repeats)
test_acc /= (dataset_sizes['test']*repeats)
print('Test Loss: %.4f Test Accuracy %.4f' % (test_loss, test_acc))
def imshow(inp, title=None):
"""Imshow for Tensor."""
inp = inp.numpy().transpose((1, 2, 0))
inp = np.clip(inp, 0, 1)
plt.imshow(inp)
if title is not None:
plt.title(title)
plt.pause(1) # pause a bit so that plots are updated
def visualize_model(model, num_images=8):
images_so_far = 0
fig = plt.figure()
for batch_idx, (images, labels) in enumerate(dataloaders['test']):
#move to GPU
images, labels = images.cuda(), labels.cuda()
outputs = model(images)
_, preds = torch.max(outputs.data, 1)
for j in range(images.size()[0]):
images_so_far += 1
ax = plt.subplot(num_images//2, 2, images_so_far)
ax.axis('off')
ax.set_title('class: {} predicted: {}'.format(class_names[labels.data[j]], class_names[preds[j]]))
imshow(images.cpu().data[j])
if images_so_far == num_images:
return
if __name__ == '__main__':
NUM_EPOCHS = 40
#LEARNING_RATE = 0.001
LEARNING_RATE = 0.001
BATCH_SIZE = 32
RESNET_LAST_ONLY = False #Fine tunes only the last layer. Set to False to fine tune entire network
root_path = '/data/' #If your data is in a different folder, set the path accodordingly
data_transforms = {
'train': transforms.Compose([
transforms.Resize(256),
#transforms.CenterCrop(224),
transforms.RandomResizedCrop(224),
#transforms.RandomHorizontalFlip(),
#TODO: Transforms.RandomResizedCrop() instead of CenterCrop(), RandomRoate() and Horizontal Flip()
transforms.ToTensor(),
#TODO: Transforms.Normalize()
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'test': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
#transforms.RandomResizedCrop(224),
transforms.ToTensor(),
#TODO: Transforms.Normalize()
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
# loading datasets with PyTorch ImageFolder
image_datasets = {x: datasets.ImageFolder(os.path.join(root_path, x),
data_transforms[x])
for x in ['train', 'test']}
# defining data loaders to load data using image_datasets and transforms, here we also specify batch size for the mini batch
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=BATCH_SIZE,
shuffle=True, num_workers=4)
for x in ['train', 'test']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'test']}
class_names = image_datasets['train'].classes
#Initialize the model
model = PreTrainedResNet(len(class_names), RESNET_LAST_ONLY)
model = model.cuda()
#Setting the optimizer and loss criterion
optimizer = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=0.9)
criterion = nn.CrossEntropyLoss()
#Begin Train
for epoch in range(NUM_EPOCHS):
train(model, optimizer, criterion, epoch+1, NUM_EPOCHS)
print("Finished Training")
print("-"*10)
test(model, criterion)
visualize_model(model)