-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·248 lines (193 loc) · 8.12 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
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
import torch
import torch.nn as nn
import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler
import torch.utils.data as data
import torchvision.models as models
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import time
import copy
import argparse
# Define command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, help='Path to dataset ')
parser.add_argument('--gpu', action='store_true', help='Use GPU if available')
parser.add_argument('--epochs', type=int, help='Number of epochs')
parser.add_argument('--arch', type=str, help='Model architecture')
parser.add_argument('--learning_rate', type=float, help='Learning rate')
parser.add_argument('--hidden_units', type=int, help='Number of hidden units')
parser.add_argument('--checkpoint', type=str, help='Save trained model checkpoint to file')
args, _ = parser.parse_known_args()
# This method loads and tunes in a model
def load_model(arch='vgg19', num_labels=102, hidden_units=4096):
# Load a pre-trained model
if arch=='vgg19':
# Load a pre-trained model
model = models.vgg19(pretrained=True)
elif arch=='alexnet':
model = models.alexnet(pretrained=True)
else:
raise ValueError('Unexpected network architecture', arch)
# Freeze its parameters
for param in model.parameters():
param.requires_grad = False
# Features, removing the last layer
features = list(model.classifier.children())[:-1]
# Number of filters in the bottleneck layer
num_filters = model.classifier[len(features)].in_features
# Extend the existing architecture with new layers
features.extend([
nn.Dropout(),
nn.Linear(num_filters, hidden_units),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(hidden_units, hidden_units),
nn.ReLU(True),
nn.Linear(hidden_units, num_labels),
##nn.Softmax(dim=1)
# Please, notice Softmax layer has not been added as per Pytorch answer:
# https://github.com/pytorch/vision/issues/432#issuecomment-368330817
# It is not either included in its transfer learning tutorial:
# https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html
])
model.classifier = nn.Sequential(*features)
return model
# This method trains a model
def train_model(image_datasets, arch='vgg19', hidden_units=4096, epochs=25, learning_rate=0.001, gpu=False, checkpoint=''):
# Use command line values when specified
if args.arch:
arch = args.arch
if args.hidden_units:
hidden_units = args.hidden_units
if args.epochs:
epochs = args.epochs
if args.learning_rate:
learning_rate = args.learning_rate
if args.gpu:
gpu = args.gpu
if args.checkpoint:
checkpoint = args.checkpoint
# Using the image datasets, define the dataloaders
dataloaders = {
x: data.DataLoader(image_datasets[x], batch_size=4, shuffle=True, num_workers=2)
for x in list(image_datasets.keys())
}
# Calculate dataset sizes.
dataset_sizes = {
x: len(dataloaders[x].dataset)
for x in list(image_datasets.keys())
}
print('Network architecture:', arch)
print('Number of hidden units:', hidden_units)
print('Number of epochs:', epochs)
print('Learning rate:', learning_rate)
# Load the model
num_labels = len(image_datasets['train'].classes)
model = load_model(arch=arch, num_labels=num_labels, hidden_units=hidden_units)
# Use gpu if selected and available
if gpu and torch.cuda.is_available():
print('Using GPU for training')
device = torch.device("cuda:0")
model.cuda()
else:
print('Using CPU for training')
device = torch.device("cpu")
# Defining criterion, optimizer and scheduler
# Observe that only parameters that require gradients are optimized
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(list(filter(lambda p: p.requires_grad, model.parameters())), lr=learning_rate, momentum=0.9)
scheduler = lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
for epoch in range(epochs):
print('Epoch {}/{}'.format(epoch + 1, epochs))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ['train', 'valid']:
if phase == 'train':
scheduler.step()
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device)
labels = labels.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward
# track history if only in train
with torch.set_grad_enabled(phase == 'train'):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
# backward + optimize only if in training phase
if phase == 'train':
loss.backward()
optimizer.step()
# statistics
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects.double() / dataset_sizes[phase]
print('{} Loss: {:.4f} Acc: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
# deep copy the model
if phase == 'valid' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = copy.deepcopy(model.state_dict())
print()
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
# Load best model weights
model.load_state_dict(best_model_wts)
# Store class_to_idx into a model property
model.class_to_idx = image_datasets['train'].class_to_idx
# Save checkpoint if requested
if checkpoint:
print ('Saving checkpoint to:', checkpoint)
checkpoint_dict = {
'arch': arch,
'class_to_idx': model.class_to_idx,
'state_dict': model.state_dict(),
'hidden_units': hidden_units
}
torch.save(checkpoint_dict, checkpoint)
# Return the model
return model
# Train model if invoked from command line
if args.data_dir:
# Default transforms for the training, validation, and testing sets
data_transforms = {
'train': transforms.Compose([
transforms.RandomRotation(45),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
]),
'valid': 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])
]),
'test': 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])
])
}
# Load the datasets with ImageFolder
image_datasets = {
x: datasets.ImageFolder(root=args.data_dir + '/' + x, transform=data_transforms[x])
for x in list(data_transforms.keys())
}
train_model(image_datasets)