-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
246 lines (195 loc) · 8.09 KB
/
train.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
import argparse
import numpy as np
import matplotlib.pyplot as plt
import torch
from torchvision import datasets, transforms, models
from torch import nn
from torch import optim
import seaborn as sns
from PIL import Image
from collections import OrderedDict
#%config InlineBackend.figure_format = 'retina'
def argument_parser():
parser = argparse.ArgumentParser(description='Train the model.')
parser.add_argument('--arch',type=str,help='Choose the architecture')
parser.add_argument('--epochs',type=int,help='Number of epochs for training purpose')
parser.add_argument('--learning_rate',type=float,help='Learning rate')
parser.add_argument('--hidden_units',type=int,help='Number of hidden units')
parser.add_argument('--device',type=str)
args=parser.parse_args()
return args
def define_transforms(data_dir):
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
test_dir = data_dir + '/test'
data_transforms = {
'training_t': transforms.Compose([
transforms.RandomRotation(45),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
]),
'validation_t': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
]),
'testing_t': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])
]),
}
# TODO: Load the datasets with ImageFolder
image_datasets = {
'training': datasets.ImageFolder(train_dir, transform = data_transforms['training_t']),
'validation': datasets.ImageFolder(valid_dir, transform = data_transforms['validation_t']),
'testing': datasets.ImageFolder(test_dir, transform = data_transforms['testing_t'])
}
# TODO: Using the image datasets and the trainforms, define the dataloaders
dataloaders = {
'training': torch.utils.data.DataLoader(image_datasets['training'], batch_size=64, shuffle=True),
'validation': torch.utils.data.DataLoader(image_datasets['validation'], batch_size=64),
'testing': torch.utils.data.DataLoader(image_datasets['testing'], batch_size=64)
}
return data_transforms,image_datasets,dataloaders
def primaryloader_model(architecture="vgg13"):
# Load Defaults if none specified
if type(architecture) == type(None):
model = models.vgg13(pretrained=True)
model.name = "vgg13"
print("Network architecture specified as vgg13.")
else:
a={}
exec("model = models.{}(pretrained=True)".format(architecture),globals(),a)
model=a["model"]
print(model)
model.name=architecture
# Freeze parameters so we don't backprop through them
for param in model.parameters():
param.requires_grad = False
return model
def classifier_initialise(model,hidden):
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(25088, hidden)),
('relu1', nn.ReLU()),
('dropout1', nn.Dropout(p=0.5)),
('fc2', nn.Linear(4096, 102)),
('output', nn.LogSoftmax(dim=1))
]))
model.classifier=classifier
return classifier
def Validation(model, dataloaders, criterion):
loss=0
accuracy=0
model.to('cuda')
for images, labels in dataloaders['validation']:
#images = Variable(images)
#labels = Variable(labels)
images, labels = images.to('cuda'), labels.to('cuda')
output = model.forward(images)
loss += criterion(output, labels).item()
ps = torch.exp(output)
equality = (labels.data == ps.max(1)[1])
accuracy += equality.type_as(torch.FloatTensor()).mean()
return loss, accuracy
def train_network(model,epochs,steps,print_every,dataloaders,optimizer,criterion,device):
for e in range(epochs):
#model.train()
running_loss = 0
model.to(device)
for images, labels in dataloaders['training']:
#images, labels = Variable(images), Variable(labels)
steps += 1
images = images.to(device)
labels = labels.to(device)
optimizer.zero_grad()
output = model.forward(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if steps % print_every == 0:
model.eval()
model.to(device)
with torch.no_grad():
actual_loss, accuracy = Validation(model, dataloaders, criterion)
print("Epoch: {}/{}.. ".format(e+1, epochs),
"Training Loss: {:.3f}.. ".format(running_loss/print_every),
"Test Loss: {:.3f}.. ".format(actual_loss/len(dataloaders['validation'])),
"Test Accuracy: {:.3f}".format(accuracy/len(dataloaders['validation'])))
running_loss = 0
model.train()
print("Training Process Completed!!! Now Proceed for Testing Process")
return model
def check_accuracy(model,dataloaders):
total=0
c=0
with torch.no_grad():
for images,labels in dataloaders['testing']:
images=images.to('cuda')
labels=labels.to('cuda')
output=model(images)
_,pred=torch.max(output.data,1)
total += labels.size(0)
c=c+(pred==labels).sum().item()
temp=c/total
print('Accuracy is: %d%%' % (100 * temp))
def imshow(image, ax=None, title=None):
"""Imshow for Tensor."""
if ax is None:
fig, ax = plt.subplots()
image = image.numpy().transpose((1, 2, 0))
# Undo preprocessing
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
image = std * image + mean
# Image needs to be clipped between 0 and 1 or it looks like noise when displayed
image = np.clip(image, 0, 1)
ax.imshow(image)
return ax
def save_checkpoint(model,image_datasets,optimizer,save_directory,epochs,struc):
model.class_to_idx = image_datasets['training'].class_to_idx
checkpoint = {'state_dict': model.state_dict(),
'classifier': model.classifier,
'opt_state': optimizer.state_dict,
'num_epochs': epochs,
'class_to_idx': model.class_to_idx,
'architecture':struc
}
torch.save(checkpoint, 'checkpoint.pth')
def main():
data_dir = 'flowers'
data_transforms,image_datasets,dataloaders=define_transforms(data_dir)
args=argument_parser()
struc=args.arch
model = primaryloader_model(struc)
hidden=args.hidden_units
model.classifier=classifier_initialise(model,hidden)
lr=args.learning_rate
#args.learning_rate
model.to('cuda')
criterion=nn.NLLLoss()
optimizer=optim.Adam(model.classifier.parameters(),lr)
if type(args.epochs) == type(None):
epoch = 6
print("Epoch is set to 6")
else:
epoch = args.epochs
print(epoch)
steps=0
print_every=35
if type(args.device)==type(None):
device='cuda'
else:
device=args.device
temp_model=train_network(model,epoch,steps,print_every,dataloaders,optimizer,criterion,device)
check_accuracy(temp_model,dataloaders)
save_checkpoint(temp_model,image_datasets,optimizer,"checkpoint.pth",epoch,struc)
if __name__ == '__main__': main()