Repository navigation
Expand file tree
/
Copy pathtrain.py
More file actions
224 lines (195 loc) · 8.6 KB
/
Copy pathtrain.py
File metadata and controls
224 lines (195 loc) · 8.6 KB
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
# All the imports
import matplotlib.pyplot as plt
import torch, time, sys
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
from collections import OrderedDict
import json, os, argparse
# CLI inputs
def process_arguments(args):
parser = argparse.ArgumentParser(description="Command Line App For Image Classifier")
parser.add_argument('-a',
'--arch',
default='densenet121',
choices=['densenet121','vgg16'],
help="Architecture to use from torchvision.models [Available: densenet121, vgg16]"
)
parser.add_argument('-e',
'--epochs',
required=True,
type=int,
help="Number Of Epochs"
)
parser.add_argument('-l',
'--learning_rate',
type=float,
default=0.003,
help="Learning Rate For Model"
)
parser.add_argument('-u',
'--hidden_units',
required=True,
type=int,
help="Number Of Hidden Units"
)
parser.add_argument('-s',
'--save_dir',
#required=True,
default='/trained_model_jasmeet/',
help="Directory To Save Checkpoints"
)
parser.add_argument('-g',
'--gpu',
action='store_true',
help="Device To Use i.e. CPU or GPU"
)
options = parser.parse_args(args)
return vars(options)
if len(sys.argv) < 2:
process_arguments(['-h'])
userOptions = process_arguments(sys.argv[1:])
print (userOptions)
data_dir = 'flowers'
cwd = os.getcwd()
checkpoint_dir = str(cwd) + userOptions.get('save_dir')
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
test_dir = data_dir + '/test'
# Datasets
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])])
test_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])])
# TODO: Load the datasets with ImageFolder
#image_datasets =
train_data = datasets.ImageFolder(train_dir, transform=train_transforms)
valid_data = datasets.ImageFolder(valid_dir, transform=valid_transforms)
test_data = datasets.ImageFolder(test_dir, transform=test_transforms)
# TODO: Using the image datasets and the trainforms, define the dataloaders
#dataloaders =
trainloader = torch.utils.data.DataLoader(train_data, batch_size=96, shuffle=True)
validloader = torch.utils.data.DataLoader(valid_data, batch_size=96)
testloader = torch.utils.data.DataLoader(test_data, batch_size=96)
# cat to name mapping
with open('/home/workspace/aipnd-project/cat_to_name.json', 'r') as f:
cat_to_name = json.load(f)
#device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device = torch.device("cuda" if userOptions.get('gpu')==True else "cpu")
hidden_value = userOptions.get('hidden_units')
# prebuilt model classifiers to import from torchview
densenet_class = nn.Sequential(nn.Linear(1024, hidden_value),
nn.ReLU(),
nn.Dropout(0.2),
#nn.Linear(512, 256),
#nn.ReLU(),
#nn.Dropout(0.2),
nn.Linear(hidden_value, 102),
nn.LogSoftmax(dim=1))
vgg_class = nn.Sequential(nn.Linear(25088, 12544),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(12544, 6272),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(6272, hidden_value),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(1024, 102),
nn.LogSoftmax(dim=1))
if userOptions.get('arch') == 'densenet121':
model = models.densenet121(pretrained=True)
model.classifier = densenet_class
elif userOptions.get('arch') == 'vgg16':
model = models.vgg16(pretrained=True)
model.classifier = vgg_class
for param in model.parameters():
param.requires_grad = False
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr=userOptions.get('learning_rate'))
model.to(device);
# Some variables
epochs = userOptions.get('epochs')
steps = 0
running_loss = 0
print_every = 5
print("Using Device: {}".format(device))
train_losses, valid_losses = [], []
# Train the model while validating
for epoch in range(epochs):
for inputs, labels in trainloader:
steps += 1
# Move input and label tensors to the default device
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
logps = model.forward(inputs)
loss = criterion(logps, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
else:
test_loss = 0
accuracy = 0
with torch.no_grad():
model.eval()
for images, labels in validloader:
images, labels = images.to(device), labels.to(device)
log_ps = model(images)
test_loss += criterion(log_ps, labels)
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))
model.train()
train_losses.append(running_loss/len(trainloader))
valid_losses.append(test_loss/len(validloader))
print("Epoch: {}/{}.. ".format(epoch+1, epochs),
"Training Loss: {:.3f}.. ".format(running_loss/len(trainloader)),
"Test Loss: {:.3f}.. ".format(test_loss/len(validloader)),
"Test Accuracy: {:.3f}".format(accuracy/len(validloader)))
print ("Complete")
# testing the model
# TODO: Do validation on the test set
test_loss = 0
accuracy = 0
model.eval()
with torch.no_grad():
for inputs, labels in testloader:
inputs, labels = inputs.to(device), labels.to(device)
logps = model.forward(inputs)
batch_loss = criterion(logps, labels)
test_loss += batch_loss.item()
# Calculate accuracy
ps = torch.exp(logps)
top_p, top_class = ps.topk(1, dim=1)
equals = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor)).item()
print(f"Epoch {epoch+1}/{epochs}.. "
f"Train loss: {running_loss/print_every:.3f}.. "
f"Test loss: {test_loss/len(testloader):.3f}.. "
f"Test accuracy: {accuracy/len(testloader):.3f}")
# TODO: Save the checkpoint
checkpoint = {'input_size': 1024,
'output_size': 102,
'epochs': epochs,
'batch_size': 96,
'model': models.densenet121(pretrained=True),
'classifier': model.classifier,
'optimizer': optimizer.state_dict(),
'state_dict': model.state_dict(),
'class_to_idx': train_data.class_to_idx
}
torch.save(checkpoint, checkpoint_dir+'checkpoint_jasmeet.pth')