-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtorchvision_to_ours.py
218 lines (180 loc) · 7.99 KB
/
torchvision_to_ours.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
'''
Usage:
python3 torchvision_to_ours.py -a resnet --layers 152 -C -g 0 -E
'''
import time
import pathlib
from os.path import isfile
import math
import numpy as np
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.optim as optim
import torch.backends.cudnn as cudnn
from utils import build_model, AverageMeter, ProgressMeter, accuracy, set_arch_name
from config import config
from data import DataLoader
# for ignore imagenet PIL EXIF UserWarning
import warnings
warnings.filterwarnings("ignore", "(Possibly )?corrupt EXIF data", UserWarning)
model_urls = {
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
'wideresnet50_2': 'https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pth',
'wideresnet101_2': 'https://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth',
'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth',
'vgg13': 'https://download.pytorch.org/models/vgg13-c768596a.pth',
'vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth',
'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth',
'vgg11_bn': 'https://download.pytorch.org/models/vgg11_bn-6002323d.pth',
'vgg13_bn': 'https://download.pytorch.org/models/vgg13_bn-abd245e5.pth',
'vgg16_bn': 'https://download.pytorch.org/models/vgg16_bn-6c64b313.pth',
'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth',
}
def config():
r"""configuration settings
"""
parser = argparse.ArgumentParser(description='Change torchvision checkpoint to ours')
parser.add_argument('-a', '--arch', metavar='ARCH', default='mobilenet',
choices=model_names,
help='model architecture: ' +
' | '.join(model_names) +
' (default: mobilenet)')
parser.add_argument('--layers', default=16, type=int, metavar='N',
help='number of layers in VGG/ResNet/WideResNet (default: 16)')
parser.add_argument('--bn', '--batch-norm', dest='bn', action='store_true',
help='Use batch norm in VGG?')
parser.add_argument('--width-mult', default=1.0, type=float, metavar='WM',
help='width multiplier to thin a network '
'uniformly at each layer (default: 1.0)')
parser.add_argument('-E', '--evaluate', dest='evaluate', action='store_true',
help='Test model?')
parser.add_argument('-C', '--cuda', dest='cuda', action='store_true',
help='Use cuda?')
parser.add_argument('-g', '--gpuids', default=[0], nargs='+',
help='GPU IDs for using (Default: 0)')
cfg = parser.parse_args()
cfg.gpuids = list(map(int, cfg.gpuids))
return cfg
def main():
global opt
opt = config()
if opt.cuda and not torch.cuda.is_available():
raise Exception('No GPU found, please run without --cuda')
# set model name
arch_name = set_arch_name(opt)
dir_ckpt = pathlib.Path('checkpoint')
dir_path = dir_ckpt / arch_name / opt.dataset
dir_path.mkdir(parents=True, exist_ok=True)
torch.utils.model_zoo.load_url(model_urls[arch_name], dir_path.as_posix())
print('\n=> creating model \'{}\''.format(arch_name))
model = build_model(opt)
if model is None:
print('==> unavailable model parameters!! exit...\n')
exit()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=opt.lr,
momentum=opt.momentum, weight_decay=opt.weight_decay,
nesterov=True)
start_epoch = 0
if opt.cuda:
torch.cuda.set_device(opt.gpuids[0])
with torch.cuda.device(opt.gpuids[0]):
model = model.cuda()
criterion = criterion.cuda()
model = nn.DataParallel(model, device_ids=opt.gpuids,
output_device=opt.gpuids[0])
cudnn.benchmark = True
# checkpoint file
ckpt_file = dir_path / model_urls[arch_name][36:]
# for evaluation
if opt.evaluate:
if isfile(ckpt_file):
# Data loading
print('==> Load data..')
train_loader, val_loader = DataLoader(opt.batch_size, opt.workers,
opt.dataset, opt.datapath,
opt.cuda)
# Checkpoint loading
print('==> Loading Checkpoint...')
checkpoint = load_model(model, ckpt_file,
main_gpu=opt.gpuids[0], use_cuda=opt.cuda)
print('==> Loaded Checkpoint...')
# evaluate on validation set
print('\n===> [ Evaluation ]')
acc1, acc5 = validate(val_loader, model, criterion)
best_acc1 = max(acc1_valid, best_acc1)
print('==> Saving Checkpoint...')
state = {'epoch': 1,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'n_retrain': 0,
'new': False}
torch.save(state, dir_path / 'ckpt_best_torchvision.pth')
print('==> Saved Checkpoint at ckpt_best_torchvision.pth')
return
else:
print('==> no checkpoint found...')
exit()
def validate(val_loader, model, criterion):
batch_time = AverageMeter('Time', ':6.3f')
losses = AverageMeter('Loss', ':.4e')
top1 = AverageMeter('Acc@1', ':6.2f')
top5 = AverageMeter('Acc@5', ':6.2f')
progress = ProgressMeter(len(val_loader), batch_time, losses, top1, top5,
prefix='Test: ')
# switch to evaluate mode
model.eval()
with torch.no_grad():
end = time.time()
for i, (input, target) in enumerate(val_loader):
if opt.cuda:
target = target.cuda(non_blocking=True)
# compute output
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
acc1, acc5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(acc1[0], input.size(0))
top5.update(acc5[0], input.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
if i % opt.print_freq == 0:
progress.print(i)
end = time.time()
print('====> Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'
.format(top1=top1, top5=top5))
return top1.avg, top5.avg
def load_model(model, ckpt_file, main_gpu, use_cuda=True):
if use_cuda:
checkpoint = torch.load(ckpt_file, map_location=lambda storage, loc: storage.cuda(main_gpu))
try:
model.load_state_dict(checkpoint)
except:
model.module.load_state_dict(checkpoint)
else:
checkpoint = torch.load(ckpt_file, map_location=lambda storage, loc: storage)
try:
model.load_state_dict(checkpoint)
except:
# create new OrderedDict that does not contain `module.`
new_state_dict = OrderedDict()
for k, v in checkpoint.items():
if k[:7] == 'module.':
name = k[7:] # remove `module.`
else:
name = k[:]
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
return checkpoint
if __name__ == '__main__':
start_time = time.time()
main()
elapsed_time = time.time() - start_time
print('====> total time: {}h {}m {:.2f}s'.format(
int(elapsed_time//3600), int((elapsed_time%3600)//60), elapsed_time%60))