-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
338 lines (303 loc) · 11.9 KB
/
Copy pathutils.py
File metadata and controls
338 lines (303 loc) · 11.9 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
'''
Some helper functions, e.g. compute parameters, progress bar, etc.
'''
import os
import sys
import time
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.init as init
import skimage
import skimage.io
import skimage.transform
import os.path
import hashlib
import errno
from tqdm import tqdm
def format_time(seconds):
days = int(seconds / 3600/24)
seconds = seconds - days*3600*24
hours = int(seconds / 3600)
seconds = seconds - hours*3600
minutes = int(seconds / 60)
seconds = seconds - minutes*60
secondsf = int(seconds)
seconds = seconds - secondsf
millis = int(seconds*1000)
f = ''
i = 1
if days > 0:
f += str(days) + 'D'
i += 1
if hours > 0 and i <= 2:
f += str(hours) + 'h'
i += 1
if minutes > 0 and i <= 2:
f += str(minutes) + 'm'
i += 1
if secondsf > 0 and i <= 2:
f += str(secondsf) + 's'
i += 1
if millis > 0 and i <= 2:
f += str(millis) + 'ms'
i += 1
if f == '':
f = '0ms'
return f
_, term_width = os.popen('stty size', 'r').read().split()
term_width = int(term_width)
last_time = time.time()
begin_time = last_time
def progress_bar(current, total, msg=None, TOTAL_BAR_LENGTH = 60.):
global last_time, begin_time
if current == 0:
begin_time = time.time() # Reset for new bar.
cur_len = int(TOTAL_BAR_LENGTH*current/total)
rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1
sys.stdout.write(' [')
for i in range(cur_len):
sys.stdout.write('=')
sys.stdout.write('>')
for i in range(rest_len):
sys.stdout.write('.')
sys.stdout.write(']')
cur_time = time.time()
step_time = cur_time - last_time
last_time = cur_time
tot_time = cur_time - begin_time
L = []
L.append(' Step: %s' % format_time(step_time))
L.append(' | Tot: %s' % format_time(tot_time))
if msg:
L.append(' | ' + msg)
msg = ''.join(L)
sys.stdout.write(msg)
for i in range(term_width-int(TOTAL_BAR_LENGTH)-len(msg)-3):
sys.stdout.write(' ')
# Go back to the center of the bar.
for i in range(term_width-int(TOTAL_BAR_LENGTH/2)+2):
sys.stdout.write('\b')
sys.stdout.write(' %d/%d ' % (current+1, total))
if current < total-1:
sys.stdout.write('\r')
else:
sys.stdout.write('\n')
sys.stdout.flush()
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
"""Saves checkpoint to disk"""
try:
torch.save(state, filename)
if is_best:
torch.save(state, filename.replace("checkpoint", "best_model"))
except:
print("didn't save checkpoint file")
def adjust_learning_rate(args, optimizer, epoch, search=False, warmup=10):
"""Sets the learning rate to the initial LR decayed by 10 after 150 and 225 epochs"""
if search == True:
if epoch == warmup:
args.lr = lr = 0.01
print("learning rate adjusted: {}".format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
if epoch>warmup and (epoch-warmup)%10 == 0:
lr = args.lr / 1.5**((epoch-warmup)//10)
print("learning rate adjusted: {}".format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
else:
if epoch==1:
lr = args.lr # 0.1
elif epoch==100:
lr = args.lr * 0.1**1 # 0.01
elif epoch==200:
lr = args.lr * 0.1**2 # 0.001
elif epoch==250:
lr = args.lr * 0.1**3 # 0.0001
if epoch==1 or epoch==100 or epoch==200 or epoch==250:
print("learning rate adjusted: {}".format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def adjust_learning_rate_finetune(args, optimizer, epoch, dataset, search=False, warmup=10):
"""Decay only once at epoch 30 and 15 for CIFAR and tinyimagenet respectively"""
if dataset!='tinyimagenet':
if epoch == 20:
lr = args.lr * 0.1
elif epoch == 30:
lr = args.lr * 0.01
if epoch==20 or epoch==30:
print("learning rate adjusted: {}".format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
elif dataset=='tinyimagenet':
if epoch == 10:
lr = args.lr * 0.1
elif epoch == 15:
lr = args.lr * 0.01
if epoch==10 or epoch==15:
print("learning rate adjusted: {}".format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def adjust_learning_rate_standard(args, optimizer, epoch, dataset, search=False, warmup=10):
"""Decay only once at epoch 30 and 15 for CIFAR and tinyimagenet respectively"""
if dataset!='tinyimagenet':
if epoch == 80:
lr = args.lr * 0.1
elif epoch == 120:
lr = args.lr * 0.01
if epoch==80 or epoch==120:
print("learning rate adjusted: {}".format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
elif dataset=='tinyimagenet':
if epoch == 10:
lr = args.lr * 0.1
elif epoch == 15:
lr = args.lr * 0.01
if epoch==10 or epoch==15:
print("learning rate adjusted: {}".format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def adjust_learning_rate_imagenet(args, optimizer, epoch, search=False, warmup=10):
"""Sets the learning rate to the initial LR decayed by 10 after 150 and 225 epochs"""
if search == True:
if epoch == warmup:
args.lr = lr = 0.01
print("learning rate adjusted: {}".format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
if epoch>warmup and epoch%10 == 0:
lr = args.lr / 1.5**((epoch-warmup)//10)
print("learning rate adjusted: {}".format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
else:
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
if epoch%50 == 0:
lr = args.lr * (0.1 ** (epoch // 50))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
print("learning rate adjusted: {}".format(lr))
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def compute_params_(model):
return sum(p.numel() for name, p in model.named_parameters() if p.requires_grad and "gate" not in name)
def compute_params(config, classes=10, model='vgg', in_channel=3, kernel=3, last=True):
"""Computes the number of parameters of CNNs"""
param_cnt = 0
if model=='vgg':
for i, cfg in enumerate(config):
if i == 0: # first layer
param_cnt = in_channel * cfg * kernel * kernel + cfg + cfg*2 # weight + bias + BN
prev_filt = cfg
else:
param_cnt += prev_filt * cfg * kernel * kernel + cfg + cfg*2
prev_filt = cfg
param_cnt += prev_filt * classes + classes # output/linear layer + bias
elif model=='resnet18':
# feed forward
for i, cfg in enumerate(config):
if i == 0: # first layer
param_cnt = in_channel * cfg * kernel * kernel + cfg*2 # weight + BN
prev_filt = cfg
else:
param_cnt += prev_filt * cfg * kernel * kernel + cfg*2
prev_filt = cfg
param_cnt += prev_filt * classes + classes # output/linear layer + bias
# skip connection
for i in range(2,len(config),2):
if config[i-2] != config[i]:
param_cnt += config[i-2] * config[i] # weight
elif model=='effnetb0':
filters = [[32],[16],[24,24],[40,40],[80,80,80],[112,112,112],[192,192,192,192],[320],[1280]]
kernel = [3,3,3,5,3,5,5,3,1]
expand = [0,1,6,6,6,6,6,6,0]
last_idx = len(expand) - 1
filt_cnt = 0
# feed forward
for filt,kern,expd in zip(filters,kernel,expand):
if filt_cnt == 0: # first layer
param_cnt = in_channel * config[filt_cnt] * kern * kern + config[filt_cnt]*2 # weight + BN
prev_filt = config[filt_cnt]
filt_cnt += 1
elif filt_cnt == last_idx: # output layer
param_cnt += prev_filt * config[filt_cnt] + config[filt_cnt]*2
prev_filt = config[filt_cnt]
else:
for filt_ in filt:
mid_ = prev_filt
# expansion
if expd != 1:
mid_ = prev_filt*expd
param_cnt += prev_filt * mid_ + mid_*2
# depthwise
param_cnt += mid_ * kern*kern + mid_*2
# S&E
param_cnt += mid_*int(0.25*mid_)*2
# conv
param_cnt += mid_*config[filt_cnt] + config[filt_cnt]*2
prev_filt = config[filt_cnt]
filt_cnt += 1
param_cnt += prev_filt * classes # output/linear layer
elif model=='mobilenet':
filters = [32,64,128,128,256,256,512,512,512,512,512,512,1024,1024]
# feed forward
for idx, _ in enumerate(filters):
if idx == 0: # first layer
param_cnt = in_channel * config[idx] * 3 * 3 + config[idx]*2 # weight + BN
prev_filt = config[idx]
else:
# depthwise
param_cnt += prev_filt*3*3 + prev_filt*2
# pointwise
param_cnt += prev_filt*config[idx] + config[idx]*2
prev_filt = config[idx]
param_cnt += prev_filt * classes + classes # linear layer
elif model=='mobilenetv2':
if last:
filters = [[32],[16],[24,24],[32,32,32],[64,64,64,64],[96,96,96],[160,160,160],[320],[1280]]
expand = [0,1,6,6,6,6,6,6,0]
else:
filters = [[32],[16],[24,24],[32,32,32],[64,64,64,64],[96,96,96],[160,160,160],[320]]
expand = [0,1,6,6,6,6,6,6]
filt_cnt = 0
# feed forward
for filt,expd in zip(filters,expand):
if filt_cnt == 0: # first layer
param_cnt = in_channel * config[filt_cnt] + config[filt_cnt] + config[filt_cnt]*2 # weight + bias + BN
prev_filt = config[filt_cnt]
filt_cnt += 1
elif expd==0 and last:
param_cnt += prev_filt * config[filt_cnt] + config[filt_cnt] + config[filt_cnt]*2
prev_filt = config[filt_cnt]
else:
for filt_ in filt:
# shortcut
if prev_filt != config[filt_cnt]:
param_cnt += prev_filt * config[filt_cnt] + config[filt_cnt] + config[filt_cnt]*2
# expansion
mid_ = prev_filt*expd
param_cnt += prev_filt * mid_ + mid_*2 + mid_
# depthwise
param_cnt += mid_ * 3*3 + mid_*2 + mid_
# conv
param_cnt += mid_*config[filt_cnt] + config[filt_cnt] + config[filt_cnt]*2
prev_filt = config[filt_cnt]
filt_cnt += 1
if last:
param_cnt += prev_filt * classes + classes # output/conv layer
else:
param_cnt += prev_filt * 1280 + 1280 + 1280*2
param_cnt += 1280 * classes + classes # output/conv layer
return param_cnt