-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_1.py
More file actions
190 lines (167 loc) · 7.17 KB
/
Copy pathparser_1.py
File metadata and controls
190 lines (167 loc) · 7.17 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
import re
from functools import reduce
from torch import nn as nn
from thop import profile
import torch
from torch.nn import Sequential, ReLU, LeakyReLU, SELU, Linear, Conv2d, BatchNorm1d, BatchNorm2d, Dropout, Dropout2d, Tanh, Softmax, MaxPool2d, Flatten
import pandas as pd
import numpy as np
def parse_struct(archline):
ReLU = archline.count(": ReLU(")
LeakyReLU = archline.count(": LeakyReLU(")
SELU = archline.count(": SELU(")
Linear = archline.count(": Linear(")
Conv2d = archline.count(": Conv2d(")
BatchNorm1d = archline.count(": BatchNorm1d(")
BatchNorm2d = archline.count(": BatchNorm2d(")
Flatten = archline.count(": Flatten(")
Dropout = archline.count(": Dropout(")
Dropout2d = archline.count(": Dropout2d(")
Tanh = archline.count(": Tanh(")
Softmax = archline.count(": Softmax(")
MaxPool2d = archline.count(": MaxPool2d(")
return ReLU, LeakyReLU, SELU, Linear, Conv2d, BatchNorm1d, BatchNorm2d, Flatten, Dropout, Dropout2d, Tanh, Softmax, MaxPool2d
def parse_order(archline):
x = [m.start() for m in re.finditer('Conv2d', archline)]
Conv2d = list(zip(x, np.full(len(x), 0)))
x = [m.start() for m in re.finditer('Linear', archline)]
Linear = list(zip(x, np.full(len(x), 1)))
x = [m.start() for m in re.finditer('BatchNorm1d', archline)]
BatchNorm1d = list(zip(x, np.full(len(x), 2)))
x = [m.start() for m in re.finditer('BatchNorm2d', archline)]
BatchNorm2d = list(zip(x, np.full(len(x), 3)))
result = Conv2d+Linear+BatchNorm1d+BatchNorm2d
result = sorted(result, key=lambda x: x[0])
ret = list(zip(*result))
if len(ret) != 0:
ret = ret[1]
return np.array(ret)
def calculate_flop(archline):
result = 0
Conv2dstart = archline.find(': Conv2d(')
while(Conv2dstart != -1):
strstart = Conv2dstart + len(": Conv2d(")
Conv2dend = archline.find('))', Conv2dstart) + 1
toparse = archline[strstart:Conv2dend]
# print(toparse)
temp1 = re.findall(r'\d+', toparse)
res = list(map(int, temp1))
result += reduce(lambda x, y: x*y, res)
# print(result)
temp = archline.find(': Conv2d(', Conv2dstart + 1)
Conv2dstart = temp
def parse_layer(archline):
Conv2dstart = archline.find('): ')
flag = 0
while(Conv2dstart != -1):
strstart = Conv2dstart
while(archline[strstart] != '('):
strstart -= 1
strstart -= 1
target = archline[strstart:Conv2dstart+2]
if (flag == 0):
archline = archline.replace(target, '')
flag = 1
else:
archline = archline.replace(target, ',')
Conv2dstart = archline.find('): ')
return archline
def flops_param_calculator(arch_hp):
try:
model = eval(arch_hp)
except Exception:
print('Fail to evalute string as nn: ', Exception)
return
return profile(model, inputs=(torch.randn(1, 3, 32, 32), ), verbose=False)
# eval to nn
# arch = "Sequential( Conv2d(3, 28, ernel_size=(1, 1), stride=(1, 1)) ,Flatten(), Linear(in_features=28672, out_features=29, bias=True) , BatchNorm1d(29, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) , Tanh() , Tanh() , Tanh() , Dropout(p=0.714631919301416), Linear(in_features=29, out_features=47, bias=True),Dropout(p=0.97788680890307) , Linear(in_features=47, out_features=10, bias=True) ,Tanh() ,Softmax() )"
# model = eval(arch)
# input = torch.randn(1, 3, 32, 32) # CIFAR10 dim
# flops, params = profile(model, inputs=(input, ))
# print(flops, params)
def apply_flops(df_):
flops_ex = []
params_ex = []
archs = (df_['arch_and_hp'])
for i in range(archs.shape[0]):
nn_str = parse_layer(archs[i])
flops, params = flops_param_calculator(nn_str)
flops_ex.append(flops)
params_ex.append(params)
return pd.DataFrame({'tot_flops': params_ex})
def apply_ops_hist(df_):
ret = np.empty((df_.shape[0], 13))
archs = (df_['arch_and_hp'])
for i in range(archs.shape[0]):
ret[i] = parse_struct(archs[i])
df_ret = pd.DataFrame(ret)
df_ret.columns = ['ReLU', 'LeakyReLU', 'SELU', 'Linear', 'Conv2d', 'BatchNorm1d', 'BatchNorm2d', 'Flatten', 'Dropout', 'Dropout2d', 'Tanh', 'Softmax', 'MaxPool2d']
return df_ret
# print(len(flops_ex), len(params_ex))
# print(flops_ex)
# print(params_ex)
def apply_init_params(df_, missing=0.0):
archs = (df_['arch_and_hp'])
im = np.array(df_['init_params_mu'], dtype=str)
ex1 = np.zeros((4, len(im), 2))
for i in range(len(im)):
init_Ab = np.array(eval(im[i])).reshape([-1, 2])
op_list = parse_order(archs[i])
for op in range(4):
ex1[op][i] = np.sum(init_Ab[op_list == op], axis=0) / np.sum(op_list == op) if (op_list == op).any() else [missing, missing]
im = np.array(df_['init_params_std'], dtype=str)
ex2 = np.zeros((4, len(im), 2))
for i in range(len(im)):
init_Ab = np.array(eval(im[i])).reshape([-1, 2])
op_list = parse_order(archs[i])
for op in range(4):
ex2[op][i] = np.sum(init_Ab[op_list == op], axis=0) / np.sum(op_list == op) if (op_list == op).any() else [missing, missing]
im = np.array(df_['init_params_l2'], dtype=str)
ex3 = np.zeros((4, len(im), 2))
for i in range(len(im)):
init_Ab = np.array(eval(im[i])).reshape([-1, 2])
op_list = parse_order(archs[i])
for op in range(4):
ex3[op][i] = np.sum(init_Ab[op_list == op], axis=0) / np.sum(op_list == op) if (op_list == op).any() else [missing, missing]
return pd.DataFrame(
{'init_A_mu_Conv2d': ex1[0][:, 0],
'init_b_mu_Conv2d': ex1[0][:, 1],
'init_A_std_Conv2d': ex2[0][:, 0],
'init_b_std_Conv2d': ex2[0][:, 1],
'init_A_l2_Conv2d': ex3[0][:, 0],
'init_b_l2_Conv2d': ex3[0][:, 1],
'init_A_mu_Linear': ex1[1][:, 0],
'init_b_mu_Linear': ex1[1][:, 1],
'init_A_std_Linear': ex2[1][:, 0],
'init_b_std_Linear': ex2[1][:, 1],
'init_A_l2_Linear': ex3[1][:, 0],
'init_b_l2_Linear': ex3[1][:, 1],
'init_A_mu_BatchNorm1d': ex1[2][:, 0],
'init_b_mu_BatchNorm1d': ex1[2][:, 1],
'init_A_std_BatchNorm1d': ex2[2][:, 0],
'init_b_std_BatchNorm1d': ex2[2][:, 1],
'init_A_l2_BatchNorm1d': ex3[2][:, 0],
'init_b_l2_BatchNorm1d': ex3[2][:, 1],
'init_A_mu_BatchNorm2d': ex1[3][:, 0],
'init_b_mu_BatchNorm2d': ex1[3][:, 1],
'init_A_std_BatchNorm2d': ex2[3][:, 0],
'init_b_std_BatchNorm2d': ex2[3][:, 1],
'init_A_l2_BatchNorm2d': ex3[3][:, 0],
'init_b_l2_BatchNorm2d': ex3[3][:, 1]})
def diff_avg(arr, header):
dif = np.diff(arr)
ret = np.empty((dif.shape[0], 7))
for j in range(dif.shape[0]):
ret[j] = np.mean(dif[j].reshape((7, 7)), axis=1)
df_ret = pd.DataFrame(ret)
df_ret.columns = [header+str(i) for i in range(7)]
return df_ret
# df = pd.read_csv("data/train-1185.csv")
# apply_init_params(df)
# feature_all = list(df.columns)
# for i in range(len(feature_all)):
# print(i, feature_all[i])
# print(diff_avg(df[feature_all[17:67]]))
# diff_avg(df[feature_all[67:117]])
# diff_avg(df[feature_all[117:167]])
# diff_avg(df[feature_all[167:217]])