-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfutils.py
301 lines (216 loc) · 9.71 KB
/
futils.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
import matplotlib.pyplot as plt
import numpy as np
import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
import torchvision.models as models
from PIL import Image
import json
from matplotlib.ticker import FormatStrFormatter
data_directory = 'flowers'
train_directory = data_directory + '/train'
validation_directory = data_directory + '/valid'
test_directory = data_directory + '/test'
# TODO: Define your transforms for the training, validation, and testing sets
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])])
test_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])])
validation_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])])
# TODO: Load the datasets with ImageFolder
train_data = datasets.ImageFolder(train_directory, transform=train_transforms)
validation_data = datasets.ImageFolder(validation_directory, transform=validation_transforms)
test_data = datasets.ImageFolder(test_directory ,transform = test_transforms)
# TODO: Using the image datasets and the trainforms, define the dataloaders
trainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)
vloader = torch.utils.data.DataLoader(validation_data, batch_size =32,shuffle = True)
testloader = torch.utils.data.DataLoader(test_data, batch_size = 20, shuffle = True)
with open('cat_to_name.json', 'r') as f:
cat_to_name = json.load(f)
main_structures = {"vgg16":25088,
"densenet121" : 1024,
"alexnet" : 9216 }
def setup(structure='alexnet',dropout=0.5, hidden_layer1 = 120,lr = 0.001):
if structure == 'vgg16':
model = models.vgg16(pretrained=True)
elif structure == 'densenet121':
model = models.densenet121(pretrained=True)
elif structure == 'alexnet':
model = models.alexnet(pretrained = True)
else:
print("Im sorry but {} is not a valid model.Did you mean vgg16,densenet121,or alexnet?".format(structure))
for param in model.parameters():
param.requires_grad = False
from collections import OrderedDict
classifier = nn.Sequential(OrderedDict([
('relu1', nn.ReLU()),
('dropout1', nn.Dropout(p=0.5)),
('fc2', nn.Linear(4096, 102, bias=True)),
('output', nn.LogSoftmax(dim=1))
]))
model.classifier = classifier
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr )
if torch.cuda.is_available() and power = 'gpu':
model.cuda()
return model , optimizer ,criterion
model,optimizer,criterion = setup('densenet121')
def training_network(model, criterion, optimizer, epochs = 3, print_every=20, loader=trainloader, power='gpu'):
steps = 0
running_loss = 0
for e in range(epochs):
running_loss = 0
for ii, (inputs, labels) in enumerate(trainloader):
steps += 1
inputs,labels = inputs.to('cuda'), labels.to('cuda')
optimizer.zero_grad()
# Forward and backward passes
outputs = model.forward(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if steps % print_every == 0:
model.eval()
vlost = 0
accuracy=0
for ii, (inputs2,labels2) in enumerate(vloader):
optimizer.zero_grad()
inputs2, labels2 = inputs2.to('cuda:0') , labels2.to('cuda:0')
model.to('cuda:0')
with torch.no_grad():
outputs = model.forward(inputs2)
vlost = criterion(outputs,labels2)
ps = torch.exp(outputs).data
equality = (labels2.data == ps.max(1)[1])
accuracy += equality.type_as(torch.FloatTensor()).mean()
vlost = vlost / len(vloader)
accuracy = accuracy /len(vloader)
print("Epoch: {}/{}... ".format(e+1, epochs),
"Loss: {:.4f}".format(running_loss/print_every),
"Validation Lost {:.4f}".format(vlost),
"Accuracy: {:.4f}".format(accuracy))
running_loss = 0
# TODO: Do validation on the test set
def check_accuracy_testdata(testloader):
correct = 0
total = 0
model.to('cuda:0')
with torch.no_grad():
for data in testloader:
images, labels = data
images, labels = images.to('cuda'), labels.to('cuda')
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the test images: %d %%' % (100 * correct / total))
check_accuracy_testdata(testloader)
# TODO: Save the checkpoint
model.class_to_idx = train_data.class_to_idx
model.cpu
torch.save({'structure' :'vgg16',
'hidden_layer1':120,
'state_dict':model.state_dict(),
'class_to_idx':model.class_to_idx},
'checkpoint.pth')
# TODO: Write a function that loads a checkpoint and rebuilds the model
def model_load(path):
checkpoint = torch.load('checkpoint.pth')
structure = checkpoint['structure']
hidden_layer1 = checkpoint['hidden_layer1']
model,_,_ = nn_setup(structure , 0.5,hidden_layer1)
model.class_to_idx = checkpoint['class_to_idx']
model.load_state_dict(checkpoint['state_dict'])
model_load('checkpoint.pth')
print(model)
def image_process(image):
image_pil = Image.open(image)
adjustment = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
image_tensor = adjustment(image_pil)
return image_tensor
# TODO: Process a PIL image for use in a PyTorch model
image = (data_directory + '/test' + '/1/' + 'image_06752.jpg')
image = image_process(image)
print(image.shape)
def imshow_image(image, ax=None, title=None):
"""Imshow for Tensor."""
if ax is None:
fig, ax = plt.subplots()
# PyTorch tensors assume the color channel is the first dimension
# but matplotlib assumes is the third dimension
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
imshow_image(image_process("flowers/test/1/image_06743.jpg"))
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
model.class_to_idx =train_data.class_to_idx
ctx = model.class_to_idx
def predict_model(image_path, model, topk=5):
if torch.cuda.is_available() and power = 'gpu':
model.to('cuda:0')
img_torch = image_process(image_path)
img_torch = img_torch.unsqueeze_(0)
img_torch = img_torch.float()
with torch.no_grad():
output = model.forward(img_torch.cuda())
probability = F.softmax(output.data,dim=1)
return probability.topk(topk)
# TODO: Implement the code to predict the class from an image file
image = (data_directory + '/test' + '/10/' + 'image_07104.jpg')
val1, val2 = predict_model(image, model)
print(val1)
print(val2)
def sanity_check():
plt.rcParams["figure.figsize"] = (10,5)
plt.subplot(211)
index = 1
path = test_directory + '/1/image_06743.jpg'
probabilities = predict_model(path, model)
image = process_image(path)
probabilities = probabilities
axs = imshow_image(image, ax = plt)
axs.axis('off')
axs.title(cat_to_name[str(index)])
axs.show()
a = np.array(probabilities[0][0])
b = [cat_to_name[str(index + 1)] for index in np.array(probabilities[1][0])]
N=float(len(b))
fig,ax = plt.subplots(figsize=(8,3))
width = 0.8
tickLocations = np.arange(N)
ax.bar(tickLocations, a, width, linewidth=4.0, align = 'center')
ax.set_xticks(ticks = tickLocations)
ax.set_xticklabels(b)
ax.set_xlim(min(tickLocations)-0.6,max(tickLocations)+0.6)
ax.set_yticks([0.2,0.4,0.6,0.8,1,1.2])
ax.set_ylim((0,1))
ax.yaxis.grid(True)
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
plt.show()
sanity_check()