This repository has been archived by the owner on Apr 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
train.py
176 lines (144 loc) · 5.95 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
import torch
from torchvision import datasets, models, transforms
import torch.optim as optim
import model
import utils
import time
import argparse
import os
import csv
# from tensorboardX import SummaryWriter
parser = argparse.ArgumentParser()
parser.add_argument("--model", type=str, default='FashionSimpleNet', help="model")
parser.add_argument("--patience", type=int, default=3, help="early stopping patience")
parser.add_argument("--batch_size", type=int, default=256, help="batch size")
parser.add_argument("--nepochs", type=int, default=200, help="max epochs")
parser.add_argument("--nworkers", type=int, default=4, help="number of workers")
parser.add_argument("--seed", type=int, default=1, help="random seed")
parser.add_argument("--data", type=str, default='MNIST', help="MNIST, or FashionMNIST")
args = parser.parse_args()
#viz
# tsboard = SummaryWriter()
# Set up the device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print('Training on {}'.format(device))
# Set seeds. If using numpy this must be seeded too.
torch.manual_seed(args.seed)
if device== 'cuda:0':
torch.cuda.manual_seed(args.seed)
# Setup folders for saved models and logs
if not os.path.exists('saved-models/'):
os.mkdir('saved-models/')
if not os.path.exists('logs/'):
os.mkdir('logs/')
# Setup folders. Each run must have it's own folder. Creates
# a logs folder for each model and each run.
out_dir = 'logs/{}'.format(args.model)
if not os.path.exists(out_dir):
os.mkdir(out_dir)
run = 0
current_dir = '{}/run-{}'.format(out_dir, run)
while os.path.exists(current_dir):
run += 1
current_dir = '{}/run-{}'.format(out_dir, run)
os.mkdir(current_dir)
logfile = open('{}/log.txt'.format(current_dir), 'w')
print(args, file=logfile)
# Define transforms.
train_transforms = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
val_transforms = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
# Create dataloaders. Use pin memory if cuda.
if args.data == 'FashionMNIST':
trainset = datasets.FashionMNIST('./data', train=True, download=True, transform=train_transforms)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size,
shuffle=True, num_workers=args.nworkers)
valset = datasets.FashionMNIST('./data', train=False, transform=val_transforms)
val_loader = torch.utils.data.DataLoader(valset, batch_size=args.batch_size,
shuffle=True, num_workers=args.nworkers)
print('Training on FashionMNIST')
else:
trainset = datasets.MNIST('./data-mnist', train=True, download=True, transform=train_transforms)
train_loader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size,
shuffle=True, num_workers=args.nworkers)
valset = datasets.MNIST('./data-mnist', train=False, transform=val_transforms)
val_loader = torch.utils.data.DataLoader(valset, batch_size=args.batch_size,
shuffle=True, num_workers=args.nworkers)
print('Training on MNIST')
def run_model(net, loader, criterion, optimizer, train = True):
running_loss = 0
running_accuracy = 0
# Set mode
if train:
net.train()
else:
net.eval()
for i, (X, y) in enumerate(loader):
# Pass to gpu or cpu
X, y = X.to(device), y.to(device)
# Zero the gradient
optimizer.zero_grad()
with torch.set_grad_enabled(train):
output = net(X)
_, pred = torch.max(output, 1)
loss = criterion(output, y)
# If on train backpropagate
if train:
loss.backward()
optimizer.step()
# Calculate stats
running_loss += loss.item()
running_accuracy += torch.sum(pred == y.detach())
return running_loss / len(loader), running_accuracy.double() / len(loader.dataset)
if __name__ == '__main__':
# Init network, criterion and early stopping
net = model.__dict__[args.model]().to(device)
criterion = torch.nn.CrossEntropyLoss()
# Define optimizer
optimizer = optim.Adam(net.parameters())
# Train the network
patience = args.patience
best_loss = 1e4
writeFile = open('{}/stats.csv'.format(current_dir), 'a')
writer = csv.writer(writeFile)
writer.writerow(['Epoch', 'Train Loss', 'Train Accuracy', 'Validation Loss', 'Validation Accuracy'])
for e in range(args.nepochs):
start = time.time()
train_loss, train_acc = run_model(net, train_loader,
criterion, optimizer)
val_loss, val_acc = run_model(net, val_loader,
criterion, optimizer, False)
end = time.time()
# print stats
stats = """Epoch: {}\t train loss: {:.3f}, train acc: {:.3f}\t
val loss: {:.3f}, val acc: {:.3f}\t
time: {:.1f}s""".format(e+1, train_loss, train_acc, val_loss,
val_acc, end - start)
print(stats)
# viz
# tsboard.add_scalar('data/train-loss',train_loss,e)
# tsboard.add_scalar('data/val-loss',val_loss,e)
# tsboard.add_scalar('data/val-accuracy',val_acc.item(),e)
# tsboard.add_scalar('data/train-accuracy',train_acc.item(),e)
# Write to csv file
writer.writerow([e+1, train_loss, train_acc.item(), val_loss, val_acc.item()])
# early stopping and save best model
if val_loss < best_loss:
best_loss = val_loss
patience = args.patience
utils.save_model({
'arch': args.model,
'state_dict': net.state_dict()
}, 'saved-models/{}-run-{}.pth.tar'.format(args.model, run))
else:
patience -= 1
if patience == 0:
print('Run out of patience!')
writeFile.close()
# tsboard.close()
break