-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
214 lines (159 loc) · 6.84 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
import torch
import numpy as np
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
import json
import time
import train_args
args = train_args.get_args()
print(args)
############### Label mapping ###################
with open('cat_to_name.json', 'r') as f:
cat_to_name = json.load(f)
################ Load the data ###################
data_dir = args.data_dir
train_dir = data_dir + '/train'
val_dir = data_dir + '/valid'
test_dir = data_dir + '/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])])
val_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])])
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])])
# TODO: Load the datasets with ImageFolder
train_data = datasets.ImageFolder(train_dir, transform = train_transforms)
val_data = datasets.ImageFolder(val_dir, transform = val_transforms)
test_data = datasets.ImageFolder(test_dir, transform = test_transforms)
# TODO: Using the image datasets and the trainforms, define the dataloaders
train_loader = torch.utils.data.DataLoader(train_data, batch_size = 64, shuffle = True)
val_loader = torch.utils.data.DataLoader(val_data, batch_size = 64)
test_loader = torch.utils.data.DataLoader(test_data, batch_size = 64)
################ Building and Training the Classifier ###################
# My initial variables
arch = args.arch
lr = args.learning_rate
hidden_layer = args.hidden_units
gpu = args.gpu #torch.device('cuda' if torch.cuda.is_available() else 'cpu')
epochs = args.epochs
dropout = args.dropout
save_dir = args.save_dir
# Built my classifier
def Classifier(arch = 'vgg16', dropout = 0.5, hidden_layer = 1024):
if arch == 'vgg16':
model = models.vgg16(pretrained = True)
input_layer = 25088
elif arch == 'vgg19_bn':
model = models.vgg19_bn(pretrained = True)
input_layer = 25088
elif arch == 'densenet121':
model = models.densenet121(pretrained = True)
input_layer = 1024
# Freeze parameters
for param in model.parameters():
param.requires_grad = False
My_Classifier = nn.Sequential(nn.Linear(input_layer, hidden_layer),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(hidden_layer, 512),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(512, 102),
nn.LogSoftmax(dim = 1)
)
model.classifier = My_Classifier
return model
model = Classifier()
# Set criterion and optimizer
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr = 0.001)
if gpu == True:
device = 'cuda'
else:
device = 'cpu'
print("Now the device is: {}".format(device))
model.to(device);
# Training and Validation
steps = 0
print_every = 32
running_loss = 0
start = time.time()
train_losses, val_losses = [], []
for epoch in range(epochs):
model.train()
for inputs, labels in train_loader:
steps += 1
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()
if steps % print_every == 0:
val_loss = 0
acc = 0
model.eval()
with torch.no_grad():
for inputs, labels in val_loader:
inputs, labels = inputs.to(device), labels.to(device)
logps = model.forward(inputs)
batch_loss = criterion(logps, labels)
val_loss += batch_loss.item()
# compute accuracy
ps = torch.exp(logps)
top_p, top_class = ps.topk(1, dim = 1)
equals = top_class == labels.view(*top_class.shape)
acc += torch.mean(equals.type(torch.FloatTensor)).item()
print(f"Epoch {epoch+1}/{epochs}.. "
f"Train loss: {running_loss/len(train_loader):.3f} | "
f"Valdation loss: {val_loss/len(val_loader):.3f} | "
f"Valdation accuracy: {acc/len(val_loader):.3f} | "
f"During: {time.time() - start:.3f} sec")
train_losses.append(running_loss/len(train_loader))
val_losses.append(val_loss/len(val_loader))
running_loss = 0
model.train()
start = time.time()
################## Testing my newwork ####################
# TODO: Do validation on the test set
model.eval()
with torch.no_grad():
model.to(device)
test_loss = 0
test_acc = 0
for inputs, labels in test_loader:
inputs, labels = inputs.to(device), labels.to(device)
logps = model.forward(inputs)
test_loss += criterion(logps, labels)
ps = torch.exp(logps)
top_p, top_class = ps.topk(1, dim = 1)
equals = top_class == labels.view(*top_class.shape)
test_acc += torch.mean(equals.type(torch.FloatTensor)).item()
print(f"Testset accuracy: {test_acc/len(test_loader):.3f}")
################## Save the checkpoint ####################
# TODO: Save the checkpoint
model.class_to_idx = train_data.class_to_idx
checkpoint = {'arch' : arch,
'lr' : lr,
'hidden_layer' : hidden_layer,
'device' : device,
'epochs' : epochs,
'dropout' : dropout,
'classifier' : model.classifier,
'state_dict' : model.state_dict(),
'class_to_idx' : model.class_to_idx}
torch.save(checkpoint, 'checkpoint.pth')