-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
342 lines (237 loc) · 11.5 KB
/
utility.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
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
import json
from collections import OrderedDict
from PIL import Image
import argparse
def get_input_args():
'''
This function parses the arguments for train.py.
'''
# Create Parse using ArgumentParser
parser = argparse.ArgumentParser()
# Create command line arguments as mentioned above using add_argument() from ArguementParser method
parser.add_argument('data_dir', type = str,
help = 'Data directory is a mandatory argument')
parser.add_argument('--save_dir', type = str, default = './',
help = 'directory to save checkpoint of the trained model')
parser.add_argument('--arch', type = str, default = 'vgg13',
help = 'the CNN model architecture to use (default: vgg13)')
parser.add_argument('--learning_rate', type = float, default = 0.01,
help = 'the learning rate for model training')
parser.add_argument('--hidden_units', type = int, default = 512,
help = 'the number of hidden units for model training')
parser.add_argument('--epochs', type = int, default = 20,
help = 'the number of epochs for model training')
parser.add_argument('--gpu', action = 'store_true',
help = 'whether or not use GPU for training')
return parser.parse_args()
def get_input_args_predict():
'''
This function parses the arguments for predict.py.
'''
# Create Parse using ArgumentParser
parser = argparse.ArgumentParser()
# Create command line arguments as mentioned above using add_argument() from ArguementParser method
parser.add_argument('image_path', type = str,
help = 'Image path is a mandatory argument')
parser.add_argument('checkpoint', type = str, default = 'checkpoint.pth',
help = 'Checkpoint is a mandatory argument')
parser.add_argument('--top_k', type = int, default = 3,
help = 'The top K most likely classes will be provided.')
parser.add_argument('--category_names', type = str, default = 'cat_to_name.json',
help = 'The json file of the mapping between categories and corresponding names.')
parser.add_argument('--gpu', action = 'store_true',
help = 'whether or not use GPU for training')
return parser.parse_args()
def data_setup(data_dir):
'''
Arg: directory of data
Return: datasets and data loaders
'''
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
train_transforms = transforms.Compose([transforms.RandomRotation(30),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
valid_transforms = transforms.Compose([transforms.Resize(255),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
# Load the datasets with ImageFolder
train_data = datasets.ImageFolder(train_dir, transform=train_transforms)
valid_data = datasets.ImageFolder(valid_dir, transform=valid_transforms)
# Using the image datasets and the trainforms, define the dataloaders
trainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)
validloader = torch.utils.data.DataLoader(valid_data, batch_size=64)
return train_data, valid_data, trainloader, validloader
def model_setup(arch, hidden_units, learning_rate):
'''
Arg: architecture of the model (vgg13 or alexnet), and the hyperparameters for the network
(hidden units and learning rate)
Return: the model modified for the training purpose
'''
if arch == 'vgg13':
model = models.vgg13(pretrained=True)
# Freeze parameters so we don't backprop through them
for param in model.parameters():
param.requires_grad = False
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(25088, 4096)),
('relu1', nn.ReLU()),
('dropout1', nn.Dropout(0.5)),
('fc2', nn.Linear(4096, hidden_units)),
('relu2', nn.ReLU()),
('dropout2', nn.Dropout(0.5)),
('fc3', nn.Linear(hidden_units, 102)),
('output', nn.LogSoftmax(dim=1))
]))
model.classifier = classifier
elif arch == 'alexnet':
model = models.alexnet(pretrained=True)
# Freeze parameters so we don't backprop through them
for param in model.parameters():
param.requires_grad = False
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(9216, 4096)),
('relu1', nn.ReLU()),
('dropout1', nn.Dropout(0.5)),
('fc2', nn.Linear(4096, hidden_units)),
('relu2', nn.ReLU()),
('dropout2', nn.Dropout(0.5)),
('fc3', nn.Linear(hidden_units, 102)),
('output', nn.LogSoftmax(dim=1))
]))
model.classifier = classifier
else:
return None
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr=learning_rate)
return model, criterion, optimizer
def model_training(model, criterion, optimizer, trainloader, validloader, device, epochs = 10, print_every = 20):
'''
This function trains the model over a certain number of epochs and print out training loss, validation loss
and accuracy every certain steps.
Arg: model setups, train and validation data, device (GPU or CPU), epochs, and number of steps for the printout.
Return: None
'''
steps = 0
# train_losses, valid_losses, accuracies = [], [], []
for epoch in range(epochs):
train_loss = 0
for inputs, labels in trainloader:
steps += 1
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model.forward(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
train_loss += loss.item()
if steps % print_every == 0:
valid_loss, accuracy = 0, 0
model.eval()
with torch.no_grad():
for inputs, labels in validloader:
inputs, labels = inputs.to(device), labels.to(device)
log_ps = model.forward(inputs)
loss = criterion(log_ps, labels)
valid_loss += loss.item()
ps = torch.exp(log_ps)
top_p, top_class = ps.topk(1, dim=1)
equals = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor))
train_loss = train_loss/print_every
valid_loss = valid_loss/len(validloader)
accuracy = accuracy/len(validloader)
print('Epoch: {}/{}..'.format(epoch+1, epochs),
'Training Loss: {:.3f}..'.format(train_loss),
'Validation Loss: {:.3f}..'.format(valid_loss),
'Validation Accuracy: {:.1f}%'.format(accuracy*100))
train_loss = 0
model.train()
return model
def save_checkpoint(arch, model, save_dir, train_data):
'''
This function saves the trained model as a checkpoint file.
Arguments: Parameters of the model and saving path.
Returns: None.
'''
path = save_dir + 'checkpoint.pth'
model.class_to_idx = train_data.class_to_idx
checkpoint = {'architecture': arch,
'classifier': model.classifier,
'state_dict': model.state_dict(),
'class_to_idx': model.class_to_idx}
torch.save(checkpoint, path)
def load_checkpoint(filepath = 'checkpoint.pth'):
'''
This function loads the saved checkpoint file.
Arguments: the path of the checkpoint file.
Returns: None.
'''
checkpoint = torch.load(filepath)
if checkpoint['architecture'] == 'vgg13':
model = models.vgg13(pretrained=True)
elif checkpoint['architecture'] == 'alexnet':
model = models.alexnet(pretrained=True)
for param in model.parameters():
param.requires_grad = False
model.classifier = checkpoint['classifier']
model.class_to_idx = checkpoint['class_to_idx']
model.load_state_dict(checkpoint['state_dict'])
return model
def process_image(image_path):
'''
This function open an image as a PIL image, scales, crops, and normalizes it for a PyTorch model.
Arguments: the path of the image file.
Returns: processed image in a PyTorch model.
'''
image = Image.open(image_path)
image_transforms = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
transformed_image = image_transforms(image)
return transformed_image
def predict(image_path, model, device, topk, cat_to_name):
'''
This function predicts the class (or top k classes) of an image using a trained deep learning model.
Arg: path of image, model, device (GPU or CPU), topk, cat_to_name mapping.
Returns: None.
'''
processed_image = process_image(image_path)
processed_image = processed_image.unsqueeze_(0)
processed_image = processed_image.float()
model.eval()
if device == torch.device('cuda'):
with torch.no_grad():
output = model.forward(processed_image.cuda())
ps = torch.exp(output)
probs, indices = ps.topk(topk)
probs = probs.cpu()
indices = indices.cpu()
else:
with torch.no_grad():
output = model.forward(processed_image)
ps = torch.exp(output)
probs, indices = ps.topk(topk)
probs = probs.numpy()[0]
indices = indices.numpy()[0]
mapping = {val: key for key, val in model.class_to_idx.items()}
classes = [mapping[index] for index in indices]
class_names = [cat_to_name[str(key)] for key in classes]
print('\nPrediction results:')
for i in range(len(probs)):
print('This image is predicted to be {} (class {}) with a probility of {}%.'.format(class_names[i], classes[i], round(probs[i]*100, 2)))