-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlook_at_feature_maps.py
257 lines (203 loc) · 7.86 KB
/
look_at_feature_maps.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
#%%
import torch
import torch.nn as nn
import numpy as np
import json
import re
from matplotlib import pyplot as plt
from pathlib import Path
import seaborn as sns
from cnn_model import CGP2CNN
from cgp_config import CgpInfoConvSet
from cgp import CGP
from my_data_loader import get_train_valid_loader
from datastuff import get_test_loader
MODEL_SAVE_DIR = 'save_dir'
#%%
def get_model_files_complete(model_folder_name, save_dir='save_dir'):
folder_path = Path(f'{save_dir}/{model_folder_name}')
model_state = torch.load(folder_path / "model_0.pth")
config_pth = folder_path / "config.json"
with open(config_pth, 'r') as config_f:
config_dict = json.load(config_f)
layer_cfg_pth = folder_path / "log.txt"
with open(layer_cfg_pth, 'r') as layer_cfg_f:
lines = layer_cfg_f.readlines()
line = lines[-1]
idx = line.index(',,')
idx_depth_end = line.index(',', idx+2)
arch_str = line[idx_depth_end+2:]
num_depth = line[idx+2:idx_depth_end]
config_dict['num_depth'] = int(num_depth)
flat_gene = arch_str
gene_list = flat_gene.split(',')
even_cnt = 0
pop = {}
pop[0] = {}
gene = []
for idx, gene_i in enumerate(gene_list):
if even_cnt % 2 == 0:
gene.append([gene_i, gene_list[idx+1]])
even_cnt += 1
if len(gene_list) == idx+1:
break
pop[0]['gene'] = gene
pop[0]['is_active'] = [True for _ in range(len(gene))]
pop[0]['is_pool'] = [True for _ in range(len(gene))]
return model_state, config_dict, pop
def get_model_files(model_folder_name, save_dir='save_dir'):
folder_path = Path(f'{save_dir}/{model_folder_name}')
model_state = torch.load(folder_path / "model_0.pth")
config_pth = folder_path / "config.json"
with open(config_pth, 'r') as config_f:
config_dict = json.load(config_f)
layer_cfg_pth = folder_path / "log-active.txt"
with open(layer_cfg_pth, 'r') as layer_cfg_f:
lines = layer_cfg_f.readlines()
line = lines[-1]
idx = line.index(',,')
idx_depth_end = line.index(',', idx+2)
arch_str = line[idx_depth_end+2:]
num_depth = line[idx+2:idx_depth_end]
config_dict['num_depth'] = int(num_depth)
gene_list = arch_str.split('],')
cgp_genes = []
for idx, item in enumerate(gene_list):
layer = item.split(',')[0]
layer_start_idx = layer.find("'")
layer_end_idx = layer.find("'", layer_start_idx+1)
layer = layer[layer_start_idx+1:layer_end_idx]
number = int(re.sub(r'[^0-9]', '', item.split(',')[1]))
if 'resnet' in model_folder_name:
number2 = int(re.sub(r'[^0-9]', '', item.split(',')[2]))
cgp_genes.append([layer, number, number2])
else:
cgp_genes.append([layer, number])
return model_state, config_dict, cgp_genes
def load_model(model_folder_pth, num_layer_eig, layer_eig_spacing):
# gpuID = 0
model_state, config, gene = get_model_files(model_folder_pth)
# recreate the model
# network_info = CgpInfoConvSet(
# arch_type=config['arch_type'], rows=1,
# cols=config['num_depth'], level_back=2,
# min_active_num=config['num_min_depth'],
# max_active_num=config['num_max_depth'])
# cgp = CGP(network_info, None, arch_type=config['arch_type'],
# lam=1, img_size=32, init=config['init'])
# cgp.pop[0].gene = np.array(gene[0]['gene'])
# cgp.pop[0].is_active = np.array(gene[0]['is_active'])
# cgp.pop[0].is_pool = np.array(gene[0]['is_pool'])
# fix cgp with the loaded configuration
torch.backends.cudnn.benchmark = True
model = CGP2CNN(gene, in_channel=3, n_class=10, img_size=config['img_size'],
arch_type=config['arch_type'], register_hook=True,
num_layer_eig=num_layer_eig,
layer_eig_spacing=layer_eig_spacing)
# criterion = nn.CrossEntropyLoss()
# criterion = criterion.cuda(gpuID)
# optimizer = torch.optim.Adam(
# model.parameters(), lr=0.001, betas=(0.5, 0.999))
# scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
# optimizer, float(config['num_epoch']))
model.load_state_dict(model_state)
model.eval()
return model
def get_dataloaders(num_train=5000):
return get_train_valid_loader(
data_dir='./', batch_size=2, augment=True,
random_seed=2018, num_workers=0, pin_memory=True,
data_num=num_train)
def get_folder_names(net_type='vgg', num_train=500, save_dir='save_dir'):
if isinstance(save_dir, str):
save_dir = Path(save_dir)
base_str = f'{net_type}-*-{num_train}-*'
folder_names = sorted(save_dir.glob(base_str))
return [(i.stem + i.suffix) for i in folder_names], folder_names
def split_eig_vectors(eig, num_eig_vec):
eig_vec_dict = {}
ctr = 0
for idx, vec in enumerate(eig):
if ctr % num_eig_vec == 0:
ctr = 0
if idx < num_eig_vec:
eig_vec_dict[ctr] = []
eig_vec_dict[ctr].append(vec)
ctr += 1
return eig_vec_dict
def visualise_eig_rank(eig_dict):
"""Visualise the dict containing the eigenvectors."""
same_pos_eig_val = []
for layer_id, eig_vals in eig_dict.items():
for idx, val in enumerate(eig_vals.tolist()):
if 0 == layer_id:
same_pos_eig_val.append([val])
else:
same_pos_eig_val[idx].append(val)
for vals in same_pos_eig_val:
if 10 > vals[0]:
sns_plt = sns.lineplot(x=list(range(len(eig_dict))), y=vals)
# print(same_pos_eig_val)
plt.show()
return plt, sns_plt
#%%
ex_path = "densenet-2019-08-08-20-16-57.800772-1000-38-id0"
# ex_path = 'resnet-2019-07-26-10-54-57.660333-25000-20-id2'
# ex_path = "vgg-2019-07-24-18-24-07.952185-10000-20-id2"
num_train = 500
num_test = num_train
net_type = 'resnet'
num_layer_eig = 3
layer_eig_spacing = 2
# f_names, _ = get_folder_names(net_type, num_train)
#%%
avg_eigenvalues = []
# for ex_path in f_names:
model = load_model(ex_path, num_layer_eig, layer_eig_spacing)
train_dl, valid_dl = get_dataloaders(num_train=num_train)
test_dl = get_test_loader('test-distortions/impulse_noise.npy', batch_size=16, num_test=num_test)
# test_dl = torch.utils.data.DataLoader(
# test_dataset, batch_size=128, shuffle=True, num_workers=int(4), drop_last=True)
for _, (data, target) in enumerate(valid_dl):
__ = model(data)
print(f'len: {len(model.eigenvalues)}')
eig_vecs = model.eigenvalues
eig_vec_dict = split_eig_vectors(eig_vecs, num_layer_eig)
mean_real_dict = {}
for idx, eigenvalues in eig_vec_dict.items():
# covariance_matrices = model.covariance_matrices
real_stack = torch.stack([i[0] for i in eigenvalues], dim=0)
# complex_stack = torch.stack([i[1] for i in eigenvalues], dim=1)
mean_real_dict[idx] = torch.mean(real_stack, dim=0)
# mean_complex = torch.mean(complex_stack, dim=0)
print(f'mean: {mean_real_dict[idx]}')
# print(f'mean_complex: {mean_complex}')
# avg_eigenvalues.append(mean_real)
#%%
# visualise_eig_rank(mean_real_dict)
#%%
eig_dict = mean_real_dict
same_pos_eig_val = []
for layer_id, eig_vals in eig_dict.items():
for idx, val in enumerate(eig_vals.tolist()):
if 0 == layer_id:
same_pos_eig_val.append([val])
else:
same_pos_eig_val[idx].append(val)
#%%
totsum = 0
for vals in same_pos_eig_val:
if 10 > vals[0]:
for val in vals:
totsum += val
print(f'sum of all minus the largest vals is: {totsum}')
totsums = 0
for vals in same_pos_eig_val:
totsums += sum(vals)
print(f'sum of all vals is: {totsums}')
# Example outputs on the same datasets, model, etc. but different runs:
# sum of all minus the largest vals is: -2.3169869720063296e-07
# sum of all vals is: 113.578864819570844
# sum of all minus the largest vals is: 3.2816332751206545e-07
# sum of all vals is: 108.76333650980395
#%%