This repository was archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
274 lines (167 loc) · 7.42 KB
/
loader.py
File metadata and controls
274 lines (167 loc) · 7.42 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
import pandas as pd
import numpy as np
import yaml
import torch
from syngem_utils import *
############################
## Load Gem-Miner Models ##
############################
def load_gemini_model(model_name, sparse, seed):
from gem_miner_args_helper import gem_miner_parser_args
gem_yaml_txt = open(f'Configs/gemini_{model_name}.yml').read()
gem_loaded_yaml = yaml.load(gem_yaml_txt, Loader=yaml.FullLoader)
gem_miner_parser_args.__dict__.update(gem_loaded_yaml)
if model_name == "FC":
from Models.mlp import FC as gem_fc
input_shape, num_classes = (1, 28, 28), 10
model = gem_fc(input_shape, num_classes)
else:
from Models.resnet_kaiming import resnet20 as gem_resnet20
model = gem_resnet20()
model.load_state_dict(torch.load(f"All_Results/{model_name}/{sparse}/gem_{sparse}_{seed}/model_after_finetune.pth", map_location=torch.device('cpu')))
model.eval()
return model
##########################
## Load Synflow Models ##
##########################
def load_synflow_model(model_name, sparse, seed):
from synflow_args_helper import synflow_parser_args
if model_name == "FC":
syn_yaml_txt = open(f'Configs/synflow_{model_name}.yml').read()
else:
syn_yaml_txt = open(f'Configs/multi_synflow_{model_name}.yml').read()
syn_loaded_yaml = yaml.load(syn_yaml_txt, Loader=yaml.FullLoader)
synflow_parser_args.__dict__.update(syn_loaded_yaml)
if model_name == "FC":
from Models.mlp import fc as syn_fc
input_shape, num_classes = (1, 28, 28), 10
model = syn_fc(input_shape, num_classes)
else:
from Models.lottery_resnet import resnet20 as syn_resnet20
D = 20
W = 16
plan = [(W, D), (2*W, D), (4*W, D)]
model = syn_resnet20(plan, 10)
model.load_state_dict(torch.load(f"All_Results/{model_name}/{sparse}/syn_{sparse}_{seed}/post-model.pt", map_location=torch.device('cpu')))
model.eval()
return model
##########################
## Load Random Models ##
##########################
def load_random_model(model_name, sparse, seed):
from synflow_args_helper import synflow_parser_args
if model_name == "FC":
syn_yaml_txt = open(f'Configs/synflow_{model_name}.yml').read()
else:
syn_yaml_txt = open(f'Configs/multi_synflow_{model_name}.yml').read()
syn_loaded_yaml = yaml.load(syn_yaml_txt, Loader=yaml.FullLoader)
synflow_parser_args.__dict__.update(syn_loaded_yaml)
if model_name == "FC":
from Models.mlp import fc as syn_fc
input_shape, num_classes = (1, 28, 28), 10
model = syn_fc(input_shape, num_classes)
else:
from Models.lottery_resnet import resnet20 as syn_resnet20
D = 20
W = 16
plan = [(W, D), (2*W, D), (4*W, D)]
model = syn_resnet20(plan, 10)
model.load_state_dict(torch.load(f"All_Results/{model_name}/{sparse}/rnd_{sparse}_{seed}/post-model.pt", map_location=torch.device('cpu')))
model.eval()
return model
#############################
## Load Gem-Miner Accuracy ##
#############################
def load_gem_acc(model, sparse):
seed_21 = pd.read_csv(f"All_Results/{model}/{sparse}/gem_{sparse}_21/acc_and_sparsity.csv")
seed_42 = pd.read_csv(f"All_Results/{model}/{sparse}/gem_{sparse}_42/acc_and_sparsity.csv")
seed_63 = pd.read_csv(f"All_Results/{model}/{sparse}/gem_{sparse}_63/acc_and_sparsity.csv")
df_add = seed_21.add(seed_42, fill_value=0)
df_add = df_add.add(seed_63, fill_value=0)
df_div = df_add.div(3)
df_div = df_div.drop(["epoch", "test_acc_before_rounding", "val_acc","train_acc", "regularization_loss", "model_sparsity"], axis = 1)
if model == "FC":
df_div = df_div.drop(np.arange(0,25,1))
else:
df_div = df_div.drop(np.arange(0,150,1))
df_div = df_div.reset_index()
df_div = df_div.drop("index", axis=1)
return df_div
###########################
## Load Synflow Accuracy ##
###########################
def load_syn_acc(model, sparse):
seed_21 = pd.read_pickle(f"All_Results/{model}/{sparse}/syn_{sparse}_21/post-train.pkl")
seed_42 = pd.read_pickle(f"All_Results/{model}/{sparse}/syn_{sparse}_42/post-train.pkl")
seed_63 = pd.read_pickle(f"All_Results/{model}/{sparse}/syn_{sparse}_63/post-train.pkl")
df_add = seed_21.add(seed_42, fill_value=0)
df_add = df_add.add(seed_63, fill_value=0)
df_div = df_add.div(3)
df_div = df_div.drop(["train_loss", "test_loss", "top5_accuracy"], axis = 1)
return df_div
###########################
## Load Random Accuracy ##
###########################
def load_rnd_acc(model, sparse):
seed_21 = pd.read_pickle(f"All_Results/{model}/{sparse}/rnd_{sparse}_21/post-train.pkl")
seed_42 = pd.read_pickle(f"All_Results/{model}/{sparse}/rnd_{sparse}_42/post-train.pkl")
seed_63 = pd.read_pickle(f"All_Results/{model}/{sparse}/rnd_{sparse}_63/post-train.pkl")
df_add = seed_21.add(seed_42, fill_value=0)
df_add = df_add.add(seed_63, fill_value=0)
df_div = df_add.div(3)
df_div = df_div.drop(["train_loss", "test_loss", "top5_accuracy"], axis = 1)
return df_div
######################################
## Load Gem-Miner First Layer Units ##
######################################
def load_gem_first_layer_units(sparse):
gem_model_50_21 = load_gemini_model("FC", sparse, 21)
gem_model_50_42 = load_gemini_model("FC", sparse, 42)
gem_model_50_63 = load_gemini_model("FC", sparse, 63)
gem_fil_50_21 = get_filters(gem_model_50_21)
gem_fil_50_42 = get_filters(gem_model_50_42)
gem_fil_50_63 = get_filters(gem_model_50_63)
all_models = [gem_fil_50_21[0] , gem_fil_50_42[0], gem_fil_50_63[0]]
all_units = []
for model in all_models:
model_units = 0
for unit in model:
model_units += unit
all_units.append(model_units)
return all_units
#####################################
## Load Synflow First Layer Units ##
#####################################
def load_syn_first_layer_units(sparse):
syn_model_50_21 = load_synflow_model("FC", sparse, 21)
syn_model_50_42 = load_synflow_model("FC", sparse, 42)
syn_model_50_63 = load_synflow_model("FC", sparse, 63)
syn_fil_50_21 = get_filters(syn_model_50_21)
syn_fil_50_42 = get_filters(syn_model_50_42)
syn_fil_50_63 = get_filters(syn_model_50_63)
all_models = [syn_fil_50_21[0] , syn_fil_50_42[0], syn_fil_50_63[0]]
all_units = []
for model in all_models:
model_units = 0
for unit in model:
model_units += unit
all_units.append(model_units)
return all_units
##################################
## Load RandomFirst Layer Units ##
##################################
def load_rnd_first_layer_units(sparse):
rnd_model_50_21 = load_random_model("FC", sparse, 21)
rnd_model_50_42 = load_random_model("FC", sparse, 21)
rnd_model_50_63 = load_random_model("FC", sparse, 21)
rnd_fil_50_21 = get_filters(rnd_model_50_21)
rnd_fil_50_42 = get_filters(rnd_model_50_42)
rnd_fil_50_63 = get_filters(rnd_model_50_63)
all_models = [rnd_fil_50_21[0] , rnd_fil_50_42[0], rnd_fil_50_63[0]]
all_units = []
for model in all_models:
model_units = 0
for unit in model:
model_units += unit
all_units.append(model_units)
return all_units