-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
161 lines (141 loc) · 6.44 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*
# Example call:
# python train.py data_dir
# python train.py data_dir --save_dir out --arch "vgg13" --epochs 10 --gpu
##
import torch
from torch import nn, optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
from time import monotonic
from parse_args import parse_train_args
MEAN_NORM = [0.485, 0.456, 0.406]
STD_NORM = [0.229, 0.224, 0.225]
def get_test_transforms():
return transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(MEAN_NORM, STD_NORM)])
def get_train_transforms():
return transforms.Compose([transforms.RandomRotation(30),
transforms.Resize(256),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(MEAN_NORM, STD_NORM)])
def get_model(args):
if args.arch == "alexnet":
model = models.alexnet(pretrained=True)
elif args.arch == "densenet":
model = models.densenet121(pretrained=True)
elif args.arch == "vgg11":
model = models.vgg11(pretrained=True)
elif args.arch == "vgg19":
model = models.vgg19(pretrained=True)
else:
model = models.vgg16(pretrained=True)
for param in model.parameters():
param.requires_grad = False
# Retrieve expected number of input features from current classifier
in_features = model.classifier[0].in_features
model.classifier = nn.Sequential(nn.Linear(in_features, args.hidden_1_size),
nn.ReLU(),
nn.Dropout(p=args.dropout_rate),
nn.Linear(args.hidden_1_size, args.hidden_2_size),
nn.ReLU(),
nn.Dropout(p=args.dropout_rate),
nn.Linear(args.hidden_2_size, args.output_size),
nn.LogSoftmax(dim=1))
return model
def train_model(model, optimizer, criterion, device, args):
# Load training and validation data
train_data = datasets.ImageFolder(args.data_dir + '/train', transform=get_train_transforms())
trainloader = torch.utils.data.DataLoader(train_data, batch_size=args.batch_size, shuffle=True)
validation_data = datasets.ImageFolder(args.data_dir + '/valid', transform=get_test_transforms())
validloader = torch.utils.data.DataLoader(validation_data, batch_size=args.batch_size)
model.class_to_idx = train_data.class_to_idx
model.to(device)
steps = 1
running_loss = 0
print_every = 5
for epoch in range(args.epochs):
for images, labels in trainloader:
steps += 1
images, labels = images.to(device), labels.to(device)
optimizer.zero_grad()
logps = model(images)
loss = criterion(logps, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if steps % print_every == 0:
model.eval()
validation_loss = 0
accuracy = 0
for images, labels in validloader:
images, labels = images.to(device), labels.to(device)
logps = model(images)
loss = criterion(logps, labels)
validation_loss += loss.item()
ps = torch.exp(logps)
top_p, top_class = ps.topk(1, dim=1)
equality = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equality.type(torch.FloatTensor)).item()
print("Epoch: {}/{}..".format(epoch+1, args.epochs),
"Training Loss: {:.3f}..".format(running_loss/len(trainloader)),
"Validation Loss: {:.3f}..".format(validation_loss/len(validloader)),
"Validation Accuracy: {:.3f}".format(accuracy/len(validloader)))
running_loss = 0
model.train()
def test_model(model, criterion, device, args):
model.eval()
test_data = datasets.ImageFolder(args.data_dir + '/test', transform=get_test_transforms())
testloader = torch.utils.data.DataLoader(test_data, batch_size=args.batch_size)
test_loss = 0
test_accuracy = 0
with torch.no_grad():
for images, labels in testloader:
images, labels = images.to(device), labels.to(device)
logps = model(images)
loss = criterion(logps, labels)
test_loss += loss.item()
ps = torch.exp(logps)
top_p, top_class = ps.topk(1, dim=1)
equality = top_class == labels.view(*top_class.shape)
test_accuracy += torch.mean(equality.type(torch.FloatTensor)).item()
print("Test Loss: {:.3f}..".format(test_loss/len(testloader)),
"Test Accuracy: {:.3f}".format(test_accuracy/len(testloader)))
def save_checkpoint(model, optimizer, args):
checkpoint = {
'epochs': args.epochs,
'learning_rate': args.learning_rate,
'batch_size': args.batch_size,
'input_size': model.classifier[0].in_features,
'hidden_1_size': args.hidden_1_size,
'hidden_2_size': args.hidden_2_size,
'dropout': args.dropout_rate,
'output_size': args.output_size,
'class_to_idx': model.class_to_idx,
'classifier_state': model.classifier.state_dict(),
'optimizer_state': optimizer.state_dict()
}
torch.save(checkpoint, args.save_dir + '/checkpoint.' + args.arch)
def main():
start_time = monotonic()
args = parse_train_args()
model = get_model(args)
optimizer = optim.Adam(model.classifier.parameters(), lr=args.learning_rate)
criterion = nn.NLLLoss()
# Enable GPU only if available and configured
device = torch.device("cuda:0" if torch.cuda.is_available() and args.gpu else "cpu")
train_model(model, optimizer, criterion, device, args)
test_model(model, criterion, device, args)
save_checkpoint(model, optimizer, args)
end_time = monotonic()
tot_time = end_time - start_time
print("\n** Total Elapsed Runtime:",
str(int((tot_time/3600)))+":"+str(int((tot_time%3600)/60))+":"
+str(int((tot_time%3600)%60)))
if __name__ == "__main__":
main()