-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathmain.py
170 lines (138 loc) · 5.96 KB
/
main.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import os
from tqdm import tqdm
import shutil
from config import get_args, get_logger
from model import ResNet50, ResNet38, ResNet26
from preprocess import load_data
def adjust_learning_rate(optimizer, epoch, args):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
lr = args.lr * (0.1 ** (epoch // 30))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def train(model, train_loader, optimizer, criterion, epoch, args, logger):
model.train()
train_acc = 0.0
step = 0
for data, target in train_loader:
adjust_learning_rate(optimizer, epoch, args)
if args.cuda:
data, target = data.cuda(), target.cuda()
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
y_pred = output.data.max(1)[1]
acc = float(y_pred.eq(target.data).sum()) / len(data) * 100.
train_acc += acc
step += 1
if step % args.print_interval == 0:
# print("[Epoch {0:4d}] Loss: {1:2.3f} Acc: {2:.3f}%".format(epoch, loss.data, acc), end='')
logger.info("[Epoch {0:4d}] Loss: {1:2.3f} Acc: {2:.3f}%".format(epoch, loss.data, acc))
for param_group in optimizer.param_groups:
# print(", Current learning rate is: {}".format(param_group['lr']))
logger.info("Current learning rate is: {}".format(param_group['lr']))
def eval(model, test_loader, args):
print('evaluation ...')
model.eval()
correct = 0
with torch.no_grad():
for data, target in test_loader:
if args.cuda:
data, target = data.cuda(), target.cuda()
output = model(data)
prediction = output.data.max(1)[1]
correct += prediction.eq(target.data).sum()
acc = 100. * float(correct) / len(test_loader.dataset)
print('Test acc: {0:.2f}'.format(acc))
return acc
def get_model_parameters(model):
total_parameters = 0
for layer in list(model.parameters()):
layer_parameter = 1
for l in list(layer.size()):
layer_parameter *= l
total_parameters += layer_parameter
return total_parameters
def main(args, logger):
train_loader, test_loader = load_data(args)
if args.dataset == 'CIFAR10':
num_classes = 10
elif args.dataset == 'CIFAR100':
num_classes = 100
elif args.dataset == 'IMAGENET':
num_classes = 1000
print('img_size: {}, num_classes: {}, stem: {}'.format(args.img_size, num_classes, args.stem))
if args.model_name == 'ResNet26':
print('Model Name: {0}'.format(args.model_name))
model = ResNet26(num_classes=num_classes, stem=args.stem)
elif args.model_name == 'ResNet38':
print('Model Name: {0}'.format(args.model_name))
model = ResNet38(num_classes=num_classes, stem=args.stem)
elif args.model_name == 'ResNet50':
print('Model Name: {0}'.format(args.model_name))
model = ResNet50(num_classes=num_classes, stem=args.stem)
if args.pretrained_model:
filename = 'best_model_' + str(args.dataset) + '_' + str(args.model_name) + '_' + str(args.stem) + '_ckpt.tar'
print('filename :: ', filename)
file_path = os.path.join('./checkpoint', filename)
checkpoint = torch.load(file_path)
model.load_state_dict(checkpoint['state_dict'])
start_epoch = checkpoint['epoch']
best_acc = checkpoint['best_acc']
model_parameters = checkpoint['parameters']
print('Load model, Parameters: {0}, Start_epoch: {1}, Acc: {2}'.format(model_parameters, start_epoch, best_acc))
logger.info('Load model, Parameters: {0}, Start_epoch: {1}, Acc: {2}'.format(model_parameters, start_epoch, best_acc))
else:
start_epoch = 1
best_acc = 0.0
if args.cuda:
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
model = model.cuda()
print("Number of model parameters: ", get_model_parameters(model))
logger.info("Number of model parameters: {0}".format(get_model_parameters(model)))
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
for epoch in range(start_epoch, args.epochs + 1):
train(model, train_loader, optimizer, criterion, epoch, args, logger)
eval_acc = eval(model, test_loader, args)
is_best = eval_acc > best_acc
best_acc = max(eval_acc, best_acc)
if not os.path.isdir('checkpoint'):
os.mkdir('checkpoint')
filename = 'model_' + str(args.dataset) + '_' + str(args.model_name) + '_' + str(args.stem) + '_ckpt.tar'
print('filename :: ', filename)
parameters = get_model_parameters(model)
if torch.cuda.device_count() > 1:
save_checkpoint({
'epoch': epoch,
'arch': args.model_name,
'state_dict': model.module.state_dict(),
'best_acc': best_acc,
'optimizer': optimizer.state_dict(),
'parameters': parameters,
}, is_best, filename)
else:
save_checkpoint({
'epoch': epoch,
'arch': args.model_name,
'state_dict': model.state_dict(),
'best_acc': best_acc,
'optimizer': optimizer.state_dict(),
'parameters': parameters,
}, is_best, filename)
def save_checkpoint(state, is_best, filename):
file_path = os.path.join('./checkpoint', filename)
torch.save(state, file_path)
best_file_path = os.path.join('./checkpoint', 'best_' + filename)
if is_best:
print('best Model Saving ...')
shutil.copyfile(file_path, best_file_path)
if __name__ == '__main__':
args, logger = get_args()
main(args, logger)