-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
276 lines (209 loc) · 9.79 KB
/
train.py
File metadata and controls
276 lines (209 loc) · 9.79 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
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
import numpy as np
import torch
import json
import argparse
from torch import nn
from torch import optim
from torchvision import datasets, transforms, models
from collections import OrderedDict
#from time import time
def train_model(model, classifier = None, criterion = None, optimizer = None,
trainloader = None, validloader = None, device = None, epochs = 10,
valid_frecuency = 5):
if (classifier is not None and
criterion is not None and
optimizer is not None and
trainloader is not None and
validloader is not None):
for param in model.parameters():
param.requires_grad = False
model.classifier = classifier
model.to(device)
running_loss = 0
steps = 0
print("Training...")
for epoch in range(epochs):
for inputs, labels in trainloader:
steps += 1
#Prepare enviroment
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
#Train
logps = model.forward(inputs)
loss = criterion(logps, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if steps % valid_frecuency == 0:
#Test
test_loss = 0
accuracy = 0
model.eval()
with torch.no_grad():
for inputs, labels in validloader:
#Prepare enviroment
inputs, labels = inputs.to(device), labels.to(device)
#Evaluate
logps = model.forward(inputs)
batch_loss = criterion(logps, labels)
test_loss += batch_loss.item()
# Calculate accuracy
ps = torch.exp(logps)
_ , top_class = ps.topk(1, dim=1)
equals = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor)).item()
#Logging
print(f"Epoch {epoch+1}/{epochs}.. "
f"Train loss: {running_loss/(valid_frecuency)}.. "
f"Test loss: {test_loss/len(validloader)}.. "
f"Test accuracy: {accuracy/len(validloader)}")
#Reset
running_loss = 0
model.train()
def accuracy(model, testloader, device = None):
#Define device
device = torch.device("cpu") if device is None else device
accuracy = 0
total = 0
if torch.cuda.is_available():
torch.cuda.empty_cache()
model.eval()
with torch.no_grad():
for images, labels in testloader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_ , predicted = torch.max(outputs.data, 1)
total += labels.size(0)
accuracy += (predicted == labels).sum().item()
del images, labels, outputs, predicted
if torch.cuda.is_available():
torch.cuda.empty_cache()
print(f'Accuracy of the network : {100 * accuracy / total}%')
return accuracy / total
def save_model(model, pre_trained_model = None, dataset = None, input_size = None,
output_size = None, path = "./" ,filename = "checkpoint", device = None):
if dataset is not None:
model.class_to_idx = dataset.class_to_idx
device = torch.device("cpu") if device is None else device
model.to(device)
checkpoint = {
'features': model.features,
'classifier': model.classifier,
'class_to_idx': model.class_to_idx,
'model_state_dict': model.state_dict(),
'opt_state_dict': optimizer.state_dict
} if (pre_trained_model is None or
input_size is None or output_size is None) else {
'transfer_model' : pre_trained_model,
'input_size': input_size,
'output_size': output_size,
'features': model.features,
'classifier': model.classifier,
'class_to_idx': model.class_to_idx,
'model_state_dict': model.state_dict(),
'opt_state_dict': optimizer.state_dict
}
torch.save(checkpoint, f'{path}{filename}.pth')
print(f"Saved as: {filename}.pth")
return checkpoint
if __name__ == "__main__":
# Argument Parser
parser = argparse.ArgumentParser(description='Train a neural network to classify images')
# Data directory
parser.add_argument('--data_dir', type = str,
help = 'Provide the data directory, mandatory')
parser.add_argument('--save_dir', type = str, default = './',
help = 'Provide the save directory')
parser.add_argument('--category_names', type = str, default='cat_to_name.json',
help = 'Provide the class labels file in json, mandatory')
# Image Transformations
parser.add_argument('--image_size', type = int, default = 256,
help = 'Image size, default value is 256')
parser.add_argument('--batch_size', type = int, default = 64,
help = 'Batch size, default value is 64')
parser.add_argument('--rescale', type = int, default = 224,
help = 'Rescale image, default value is 224')
parser.add_argument('--rotation', type = int, default = 30,
help = 'Rotation angle, default value is 30')
# Model architecture
parser.add_argument('--arch', type = str, default = 'vgg19',
help = 'densenet121 or vgg19')
# Model hyperparameters
parser.add_argument('--learning_rate', type = float, default = 0.001,
help = 'Learning rate, default value 0.001')
parser.add_argument('--hidden_units', type = int, default = 0,
help = 'Number of hidden units, default value is 0')
parser.add_argument('--dropout', type = float, default = 0.2,
help = 'Dropout, default value 0.2')
parser.add_argument('--epochs', type = int, default = 10,
help = 'Number of epochs, default value is 10')
parser.add_argument('--print_every', type = int, default = 5,
help = 'Print every, default value is 5')
# GPU
parser.add_argument('--gpu', action='store_true',
help = "Add to activate CUDA")
args_in = parser.parse_args()
if args_in.gpu:
device = torch.device("cuda")
else:
device = torch.device("cpu")
# Data loading
data_dir = args_in.data_dir
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
test_dir = data_dir + '/test'
# Image transformations
size = args_in.image_size
resize_min = args_in.rescale
means, stds = (0.485, 0.456, 0.406), (0.229, 0.224, 0.225)
rotation = args_in.rotation
batch_size = args_in.batch_size
data_train_transforms = transforms.Compose([
transforms.Resize(size),
transforms.CenterCrop(resize_min),
transforms.RandomRotation(rotation),
transforms.ToTensor(),
transforms.Normalize(means, stds)
])
data_test_valid_transforms = transforms.Compose([
transforms.Resize(size),
transforms.CenterCrop(resize_min),
transforms.ToTensor(),
transforms.Normalize(means, stds)
])
print("Data loading...")
# Data loading
train_dataset = datasets.ImageFolder(train_dir, transform = data_train_transforms)
test_dataset = datasets.ImageFolder(test_dir, transform = data_test_valid_transforms)
valid_dataset = datasets.ImageFolder(valid_dir, transform = data_test_valid_transforms)
trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
testloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size)
validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=batch_size)
with open(args_in.category_names, 'r') as f:
classes = json.load(f)
print("Data loaded\n\nBuilding Model...")
class_long = len(classes)
# Model
model, size_output = models.densenet121(weights= models.densenet.DenseNet121_Weights.IMAGENET1K_V1), (16,1024) if args_in.arch == 'densenet121' else models.vgg19(weights=models.vgg.VGG19_Weights.IMAGENET1K_V1), (32,25088)
size_input_layer = int((size_output[1] - class_long) * (1/2)) if args_in.hidden_units == 0 else args_in.hidden_units
p = args_in.dropout
# Classifier
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr = args_in.learning_rate)
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(size_output[1], size_input_layer)),
('relu1', nn.ReLU()),
('dropout1', nn.Dropout(p)),
('fc2', nn.Linear(size_input_layer, class_long)),
('output', nn.LogSoftmax(dim=1))
]))
print("Model built\n\n")
train_model(model, classifier, criterion,
optimizer, trainloader, validloader,
device, args_in.epochs, args_in.print_every)
print("Training completed\n\nTesting...")
accuracy(model, testloader, device)
print("Testing completed\n\nSaving model...")
save_model(model, args_in.arch , size_output[1],
class_long, path = args_in.save_dir, device = device)
print("Model saved")