-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
371 lines (311 loc) · 12.5 KB
/
utils.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import torch
from Operation import Operations_11_name, Operations_11
# import pygraphviz as pgv
import os
import gc
import threading
from io import BytesIO
from PIL import Image
import torch.utils.data as data
from utils_package import *
from misc.flops_counter import add_flops_counting_methods
import shutil
#========================================= save & load =================================
def save(model_path, args, model, epoch, step, optimizer, best_acc_top1, is_best=True):
if hasattr(model, 'module'):
model = model.module
state_dict = {
'args': args,
'model': model.state_dict() if model else {},
'epoch': epoch,
'step': step,
'optimizer': optimizer.state_dict(),
'best_acc_top1': best_acc_top1,
}
filename = os.path.join(model_path, 'checkpoint{}.pt'.format(epoch))
torch.save(state_dict, filename)
newest_filename = os.path.join(model_path, 'checkpoint.pt')
shutil.copyfile(filename, newest_filename)
if is_best:
best_filename = os.path.join(model_path, 'checkpoint_best.pt')
shutil.copyfile(filename, best_filename)
def load(model_path):
if model_path is None:
return None, None, 0, 0, None, 0
else:
newest_filename = os.path.join(model_path, 'checkpoint.pt')
if not os.path.exists(newest_filename):
return None, None, 0, 0, None, 0
state_dict = torch.load(newest_filename)
args = state_dict['args']
model_state_dict = state_dict['model']
epoch = state_dict['epoch']
step = state_dict['step']
optimizer_state_dict = state_dict['optimizer']
best_acc_top1 = state_dict.get('best_acc_top1')
return args, model_state_dict, epoch, step, optimizer_state_dict, best_acc_top1
#=================================================ImageNet==============================
IMG_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif']
def has_file_allowed_extension(filename, extensions):
filename_lower = filename.lower()
return any(filename_lower.endswith(ext) for ext in extensions)
def convert_to_pil(bytes_obj):
img = Image.open(BytesIO(bytes_obj))
return img.convert('RGB')
class ReadImageThread(threading.Thread):
def __init__(self, root, fnames, class_id, target_list):
threading.Thread.__init__(self)
self.root = root
self.fnames = fnames
self.class_id = class_id
self.target_list = target_list
def run(self):
for fname in self.fnames:
if has_file_allowed_extension(fname, IMG_EXTENSIONS):
path = os.path.join(self.root, fname)
with open(path, 'rb') as f:
image = f.read()
item = (image, self.class_id)
self.target_list.append(item)
class InMemoryDataset(data.Dataset):
def __init__(self, path, transform=None, num_workers=1):
super(InMemoryDataset, self).__init__()
self.path = path
self.transform = transform
self.samples = []
classes, class_to_idx = self.find_classes(self.path)
dir = os.path.expanduser(self.path)
for target in sorted(os.listdir(dir)):
d = os.path.join(dir, target)
if not os.path.isdir(d):
continue
for root, _, fnames in sorted(os.walk(d)):
if num_workers == 1:
for fname in sorted(fnames):
if has_file_allowed_extension(fname, IMG_EXTENSIONS):
path = os.path.join(root, fname)
with open(path, 'rb') as f:
image = f.read()
item = (image, class_to_idx[target])
self.samples.append(item)
else:
fnames = sorted(fnames)
num_files = len(fnames)
threads = []
res = [[] for i in range(num_workers)]
num_per_worker = num_files // num_workers
for i in range(num_workers):
start_index = num_per_worker * i
end_index = num_files if i == num_workers - 1 else num_per_worker * (i+1)
thread = ReadImageThread(root, fnames[start_index:end_index], class_to_idx[target], res[i])
threads.append(thread)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
for item in res:
self.samples += item
del res, threads
gc.collect()
def __len__(self):
return len(self.samples)
def __getitem__(self, index):
sample, target = self.samples[index]
sample = convert_to_pil(sample)
if self.transform is not None:
sample = self.transform(sample)
return sample, target
def __repr__(self):
fmt_str = 'Dataset ' + self.__class__.__name__ + '\n'
fmt_str += ' Number of datapoints: {}\n'.format(self.__len__())
fmt_str += ' Root Location: {}\n'.format(self.path)
tmp = ' Transforms (if any): '
fmt_str += '{0}{1}\n'.format(tmp, self.transform.__repr__().replace('\n', '\n' + ' ' * len(tmp)))
tmp = ' Target Transforms (if any): '
fmt_str += '{0}{1}'.format(tmp, self.target_transform.__repr__().replace('\n', '\n' + ' ' * len(tmp)))
return fmt_str
@staticmethod
def find_classes(root):
classes = [d for d in os.listdir(root) if os.path.isdir(os.path.join(root, d))]
classes.sort()
class_to_idx = {classes[i]: i for i in range(len(classes))}
return classes, class_to_idx
#========================================= AvgrageMeter & Accuracy =================================
def accuracy(output, target, topk=(1,)):
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
class AvgrageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.avg = 0
self.sum = 0
self.cnt = 0
def update(self, val, n=1):
self.sum += val * n
self.cnt += n
self.avg = self.sum / self.cnt
self._print = '{time:.6f} ({avg:.6f})'.format(time=val,avg=self.avg)
#========================================= creat dir =================================
def create__dir(path):
if not os.path.exists(path):
try :
os.mkdir(path)
except Exception:
os.makedirs(path)
print('Make Dir : {}'.format(path))
#========================================= DAG Node ===========================================
class dagnode():
#Usage:
# dag = collections.defaultdict(list)
# dag[-1] = dagnode(-1, [], None)
# dag[0] = dagnode(0, [0], None)
# dag[1] = dagnode(1, [1, 1], 1)
# dag[2] = dagnode(2, [1, 0, 1], 4)
# dag[3] = dagnode(3, [0, 0, 1, 0], 7)
# dag[4] = dagnode(4, [1, 1, 0, 1, 1], 9)
def __init__(self, node_id, adj_node, op_id):
self.node_id = node_id
self.adj_node = adj_node
if node_id<1:
self.op_id = 'cell_'+str(node_id)
self.op_name = 'Cell operation '+str(node_id)
else:
self.op_id = op_id
self.op_name = Operations_11_name[op_id]
#========================================= Calculate Network's parameters ===========================================
def count_parameters_in_MB(model):
return np.sum(np.prod(v.size()) for name, v in model.named_parameters() if "auxiliary" not in name)/1e6
def Calculate_flops(model):
# copy from NSGA-Net
model = add_flops_counting_methods(model)
model.eval()
model.start_flops_count()
random_data = torch.randn(1, 3, 32, 32).cuda()
model(random_data)
flops = np.round(model.compute_average_flops_cost() / 1e6, 4) # MB
return flops
#========================================= Draw Network ===========================================
def construct_plot_dags(cell_dag):
# which is different to cell_dag, representation in 'Successor'
# but cell_dag in 'Precursor'
# Note :
#cell_dag's index start with [-1, 0](denoting first two cell output) to end
#plot_dags's index start with [-2, -1]
# construct adjacent matrix
Num_nodes = len(cell_dag)
Adj = Get_Adjmatrix_from_celldag(cell_dag)
dag = collections.defaultdict(list)
# add first node (cell) and second node (cell)
for i in range(Num_nodes):
Successor_i = Adj[i]
for node_j, flag in enumerate(Successor_i):
if flag and node_j>i:
dag[i-2].append( Node(node_j-2, cell_dag[node_j-1].op_name) )
#node_j-2 is plot_dag, node_j-1 is cell_dag
leaf_nodes = set(range(-2,Num_nodes-2)) - dag.keys()
#leaf_nodes have done to be consistent with plot_dag
for idx in leaf_nodes:
dag[idx] = [Node(Num_nodes-2, 'Concat')]
# leaf_nodes need to connect to 'Concat' Node
# 'Concat' Node then to 'Output' Node
dag[Num_nodes-2] = [Node(Num_nodes-2 +1, 'Output')]
return dag
def add_node(graph, node_id, label, shape='box', style='filled'):
if label.startswith('h[t]'):
color = 'white'
elif label.startswith('h[t-1]'):
color = 'skyblue'
elif label.startswith('Conv') or label.startswith('SepConv') or label.startswith('DilConv') :
color = 'seagreen3'
elif label.startswith('MaxPool'):
color = 'pink'
elif label.startswith('Identity'):
color = 'yellow'
elif label.startswith('AvgPool'):
color = 'greenyellow'
elif label == 'Concat':
color = 'orange'
elif label == 'SELayer':
color = 'tomato'
else:
color = 'cyan'
if not any(label.startswith(word) for word in ['Concat', 'Output', 'h']):
label = f"{label}\n({node_id})"
graph.add_node(
node_id, label=label, color='black', fillcolor=color,
shape=shape, style=style,
)
# def draw_network(dag, path):
# # Here dag is in the form of plot_dag
#
# create__dir(os.path.dirname(path))
#
# graph = pgv.AGraph(directed=True, strict=True,
# fontname='Helvetica', arrowtype='open')
#
#
# checked_ids = [-2, -1 ]
#
# if -1 in dag:
# add_node(graph, -1, 'h[t]')
# if -2 in dag:
# add_node(graph, -2, 'h[t-1]')
#
# # add_node(graph, 0, dag[-1][0].name)
#
# for idx in dag:
# for node in dag[idx]:
# if node.id not in checked_ids:
# add_node(graph, node.id, node.name)
# checked_ids.append(node.id)
# graph.add_edge(idx, node.id)
#
# graph.layout(prog='dot')
# graph.draw(path)
# del graph
#
# def Plot_network(dag, path):
# plot_dag = construct_plot_dags(dag)
#
# draw_network(plot_dag, path)
## ================================TensorboardX====================
# from tensorboardX import SummaryWriter
# input_2 = torch.rand([1, 3, 32, 32])
#
# with SummaryWriter(comment='NetworkCIFAR')as w:
# w.add_graph(model, (input_2,))
#
## =========================================使用GRAPHVIZ+TORCHVIZ来可视化模型====================================
# from torchviz import make_dot
# model.eval()
# out = model(input_2)
# g = make_dot(out)
# g.render('espnet_model', view=True)
# =========================================使用GRAPHVIZ+TORCHVIZ来可视化模型====================================
#
# if __name__ == '__main__':
#
#
# dag = collections.defaultdict(list)
# dag[-1] = dagnode(-1, [], None)
# dag[0] = dagnode(0, [0], None)
# dag[1] = dagnode(1, [0, 1], 1)
# dag[2] = dagnode(2, [1, 0, 1], 4)
# dag[3] = dagnode(3, [0, 0, 1, 0], 7)
# dag[4] = dagnode(4, [1, 1, 0, 1, 0], 9)
# dag[5] = dagnode(5, [0, 1, 1, 0, 0, 0], 10)
# dag[6] = dagnode(6, [0, 1, 0, 1, 0, 0, 1], 3)
#
#
# plot_dag = construct_plot_dags(dag)
#
# draw_network(plot_dag,'logs1/test_6.png')