-
Notifications
You must be signed in to change notification settings - Fork 13
/
train_scratch.py
257 lines (198 loc) · 9.44 KB
/
train_scratch.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
249
250
251
252
253
254
255
# train a baseline model from scratch
import torch
import torch.nn as nn
from torch.optim import SGD, Adam
from tqdm import tqdm
import argparse
import os
import logging
import numpy as np
from utils.utils import RunningAverage, set_logger, Params
from model import *
from data_loader import fetch_dataloader
# ************************** random seed **************************
seed = 0
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# ************************** parameters **************************
parser = argparse.ArgumentParser()
parser.add_argument('--save_path', default='experiments/CIFAR10/baseline/resnet18', type=str)
parser.add_argument('--resume', default=None, type=str)
parser.add_argument('--gpu_id', default=[0], type=int, nargs='+', help='id(s) for CUDA_VISIBLE_DEVICES')
args = parser.parse_args()
device_ids = args.gpu_id
torch.cuda.set_device(device_ids[0])
# ************************** training function **************************
def train_epoch(model, optim, loss_fn, data_loader, params):
model.train()
loss_avg = RunningAverage()
with tqdm(total=len(data_loader)) as t: # Use tqdm for progress bar
for i, (train_batch, labels_batch) in enumerate(data_loader):
if params.cuda:
train_batch = train_batch.cuda() # (B,3,32,32)
labels_batch = labels_batch.cuda() # (B,)
# compute model output and loss
output_batch = model(train_batch) # logit without softmax
loss = loss_fn(output_batch, labels_batch)
optim.zero_grad()
loss.backward()
optim.step()
# update the average loss
loss_avg.update(loss.item())
# tqdm setting
t.set_postfix(loss='{:05.3f}'.format(loss_avg()))
t.update()
return loss_avg()
def evaluate(model, loss_fn, data_loader, params):
model.eval()
# summary for current eval loop
summ = []
with torch.no_grad():
# compute metrics over the dataset
for data_batch, labels_batch in data_loader:
if params.cuda:
data_batch = data_batch.cuda() # (B,3,32,32)
labels_batch = labels_batch.cuda() # (B,)
# compute model output
output_batch = model(data_batch)
loss = loss_fn(output_batch, labels_batch)
# extract data from torch Variable, move to cpu, convert to numpy arrays
output_batch = output_batch.cpu().numpy()
labels_batch = labels_batch.cpu().numpy()
# calculate accuracy
output_batch = np.argmax(output_batch, axis=1)
acc = 100.0 * np.sum(output_batch == labels_batch) / float(labels_batch.shape[0])
summary_batch = {'acc': acc, 'loss': loss.item()}
summ.append(summary_batch)
# compute mean of all metrics in summary
metrics_mean = {metric: np.mean([x[metric] for x in summ]) for metric in summ[0]}
return metrics_mean
def train_and_eval(model, optim, loss_fn, train_loader, dev_loader, params):
best_val_acc = -1
best_epo = -1
lr = params.learning_rate
for epoch in range(params.num_epochs):
# LR schedule *****************
lr = adjust_learning_rate(optim, epoch, lr, params)
logging.info("Epoch {}/{}".format(epoch + 1, params.num_epochs))
logging.info('Learning Rate {}'.format(lr))
# ********************* one full pass over the training set *********************
train_loss = train_epoch(model, optim, loss_fn, train_loader, params)
logging.info("- Train loss : {:05.3f}".format(train_loss))
# ********************* Evaluate for one epoch on validation set *********************
val_metrics = evaluate(model, loss_fn, dev_loader, params) # {'acc':acc, 'loss':loss}
metrics_string = " ; ".join("{}: {:05.3f}".format(k, v) for k, v in val_metrics.items())
logging.info("- Eval metrics : " + metrics_string)
# save last epoch model
save_name = os.path.join(args.save_path, 'last_model.tar')
torch.save({
'epoch': epoch + 1, 'state_dict': model.state_dict(), 'optim_dict': optim.state_dict()},
save_name)
# ********************* get the best validation accuracy *********************
val_acc = val_metrics['acc']
if val_acc >= best_val_acc:
best_epo = epoch + 1
best_val_acc = val_acc
logging.info('- New best model ')
# save best model
save_name = os.path.join(args.save_path, 'best_model.tar')
torch.save({
'epoch': epoch + 1, 'state_dict': model.state_dict(), 'optim_dict': optim.state_dict()},
save_name)
logging.info('- So far best epoch: {}, best acc: {:05.3f}'.format(best_epo, best_val_acc))
def adjust_learning_rate(opt, epoch, lr, params):
if epoch in params.schedule:
lr = lr * params.gamma
for param_group in opt.param_groups:
param_group['lr'] = lr
return lr
if __name__ == "__main__":
# ************************** set log **************************
set_logger(os.path.join(args.save_path, 'training.log'))
# #################### Load the parameters from json file #####################################
json_path = os.path.join(args.save_path, 'params.json')
assert os.path.isfile(json_path), "No json configuration file found at {}".format(json_path)
params = Params(json_path)
params.cuda = torch.cuda.is_available() # use GPU if available
for k, v in params.__dict__.items():
logging.info('{}:{}'.format(k, v))
# ########################################## Dataset ##########################################
trainloader = fetch_dataloader('train', params)
devloader = fetch_dataloader('dev', params)
# ############################################ Model ############################################
if params.dataset == 'cifar10':
num_class = 10
elif params.dataset == 'cifar100':
num_class = 100
elif params.dataset == 'tiny_imagenet':
num_class = 200
else:
num_class = 10
logging.info('Number of class: ' + str(num_class))
logging.info('Create Model --- ' + params.model_name)
# ResNet 18 / 34 / 50 ****************************************
if params.model_name == 'resnet18':
model = ResNet18(num_class=num_class)
elif params.model_name == 'resnet34':
model = ResNet34(num_class=num_class)
elif params.model_name == 'resnet50':
model = ResNet50(num_class=num_class)
# PreResNet(ResNet for CIFAR-10) 20/32/56/110 ***************
elif params.model_name.startswith('preresnet20'):
model = PreResNet(depth=20, num_classes=num_class)
elif params.model_name.startswith('preresnet32'):
model = PreResNet(depth=32, num_classes=num_class)
elif params.model_name.startswith('preresnet44'):
model = PreResNet(depth=44, num_classes=num_class)
elif params.model_name.startswith('preresnet56'):
model = PreResNet(depth=56, num_classes=num_class)
elif params.model_name.startswith('preresnet110'):
model = PreResNet(depth=110, num_classes=num_class)
# DenseNet *********************************************
elif params.model_name == 'densenet121':
model = densenet121(num_class=num_class)
elif params.model_name == 'densenet161':
model = densenet161(num_class=num_class)
elif params.model_name == 'densenet169':
model = densenet169(num_class=num_class)
# ResNeXt *********************************************
elif params.model_name == 'resnext29':
model = CifarResNeXt(cardinality=8, depth=29, num_classes=num_class)
elif params.model_name == 'mobilenetv2':
model = MobileNetV2(class_num=num_class)
elif params.model_name == 'shufflenetv2':
model = shufflenetv2(class_num=num_class)
# Basic neural network ********************************
elif params.model_name == 'net':
model = Net(num_class, params)
elif params.model_name == 'mlp':
model = MLP(num_class=num_class)
else:
model = None
print('Not support for model ' + str(params.model_name))
exit()
if params.cuda:
model = model.cuda()
if len(args.gpu_id) > 1:
model = nn.DataParallel(model, device_ids=device_ids)
# checkpoint ********************************
if args.resume:
logging.info('- Load checkpoint model from {}'.format(args.resume))
checkpoint = torch.load(args.resume, map_location=lambda storage, loc: storage)
model.load_state_dict(checkpoint['state_dict'])
else:
logging.info('- Train from scratch ')
# ############################### Optimizer ###############################
if params.model_name == 'net' or params.model_name == 'mlp':
optimizer = Adam(model.parameters(), lr=params.learning_rate)
logging.info('Optimizer: Adam')
else:
optimizer = SGD(model.parameters(), lr=params.learning_rate, momentum=0.9, weight_decay=5e-4)
logging.info('Optimizer: SGD')
# ************************** LOSS **************************
criterion = nn.CrossEntropyLoss()
# ################################# train and evaluate #################################
train_and_eval(model, optimizer, criterion, trainloader, devloader, params)