forked from ahmdtaha/knowledge_evolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_KE_cls.py
315 lines (267 loc) · 9.63 KB
/
train_KE_cls.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
import os
import sys
import data
import torch
import getpass
import KE_model
import importlib
import os.path as osp
import torch.nn as nn
from utils import os_utils
from utils import net_utils
from utils import csv_utils
from layers import conv_type
from utils import path_utils
from utils import model_profile
from configs.base_config import Config
def get_trainer(args):
print(f"=> Using trainer from trainers.{args.trainer}")
trainer = importlib.import_module(f"trainers.{args.trainer}")
return trainer.train, trainer.validate
def train_dense(cfg, generation):
model = net_utils.get_model(cfg)
if cfg.pretrained and cfg.pretrained != "imagenet":
net_utils.load_pretrained(cfg.pretrained, cfg.gpu, model, cfg)
model = net_utils.move_model_to_gpu(cfg, model)
net_utils.split_reinitialize(cfg, model, reset_hypothesis=cfg.reset_hypothesis)
else:
model = net_utils.move_model_to_gpu(cfg, model)
cfg.trainer = "default_cls"
cfg.pretrained = None
ckpt_path = KE_model.ke_cls_train(cfg, model, generation)
return ckpt_path
def eval_slim(cfg, generation):
original_num_epos = cfg.epochs
# cfg.epochs = 0
softmax_criterion = nn.CrossEntropyLoss().cuda()
epoch = 1
writer = None
model = net_utils.get_model(cfg)
net_utils.load_pretrained(cfg.pretrained, cfg.gpu, model, cfg)
# if cfg.reset_mask:
# net_utils.reset_mask(cfg, model)
model = net_utils.move_model_to_gpu(cfg, model)
save_filter_stats = cfg.arch in ["split_alexnet", "split_vgg11_bn"]
if save_filter_stats:
for n, m in model.named_modules():
if hasattr(m, "weight") and m.weight is not None:
if hasattr(m, "mask"):
layer_mask = m.mask
if m.__class__ == conv_type.SplitConv:
# filter_state = [''.join(map(str, ((score_mask == True).type(torch.int).squeeze().tolist())))]
filter_mag = [
"{},{}".format(
float(
torch.mean(
torch.abs(m.weight[layer_mask.type(torch.bool)])
)
),
float(
torch.mean(
torch.abs(
m.weight[(1 - layer_mask).type(torch.bool)]
)
)
),
)
]
os_utils.txt_write(
osp.join(
cfg.exp_dir, n.replace(".", "_") + "_mean_magnitude.txt"
),
filter_mag,
mode="a+",
)
dummy_input_tensor = torch.zeros((1, 3, 224, 224)).cuda()
total_ops, total_params = model_profile.profile(model, dummy_input_tensor)
cfg.logger.info("Dense #Ops: %f GOps" % (total_ops / 1e9))
cfg.logger.info(
"Dense #Parameters: %f M (Split-Mask included)" % (total_params / 1e6)
)
original_split_rate = cfg.split_rate
original_bias_split_rate = cfg.bias_split_rate
if cfg.split_mode == "kels":
cfg.slim_factor = cfg.split_rate
cfg.split_rate = 1.0
cfg.bias_split_rate = 1.0
split_model = net_utils.get_model(cfg)
split_model = net_utils.move_model_to_gpu(cfg, split_model)
total_ops, total_params = model_profile.profile(split_model, dummy_input_tensor)
cfg.logger.info("Split #Ops: %f GOps" % (total_ops / 1e9))
cfg.logger.info(
"Split #Parameters: %f M (Split-Mask included)" % (total_params / 1e6)
)
net_utils.extract_slim(split_model, model)
dataset = getattr(data, cfg.set)(cfg)
train, validate = get_trainer(cfg)
last_val_acc1, last_val_acc5 = validate(
dataset.tst_loader, split_model, softmax_criterion, cfg, writer, epoch
)
cfg.logger.info("Split Model : {} , {}".format(last_val_acc1, last_val_acc5))
else:
last_val_acc1 = 0
last_val_acc5 = 0
csv_utils.write_cls_result_to_csv(
## Validation
curr_acc1=0,
curr_acc5=0,
best_acc1=0,
best_acc5=0,
## Test
last_tst_acc1=last_val_acc1,
last_tst_acc5=last_val_acc5,
best_tst_acc1=0,
best_tst_acc5=0,
## Train
best_train_acc1=0,
best_train_acc5=0,
split_rate="slim",
bias_split_rate="slim",
base_config=cfg.name,
name=cfg.name,
)
cfg.epochs = original_num_epos
cfg.slim_factor = 1
cfg.split_rate = original_split_rate
cfg.bias_split_rate = original_bias_split_rate
def clean_dir(ckpt_dir, num_epochs):
# print(ckpt_dir)
if "0000" in str(
ckpt_dir
): ## Always keep the first model -- Help reproduce results
return
rm_path = ckpt_dir / "model_best.pth"
if rm_path.exists():
os.remove(rm_path)
rm_path = ckpt_dir / "epoch_{}.state".format(num_epochs - 1)
if rm_path.exists():
os.remove(rm_path)
rm_path = ckpt_dir / "initial.state"
if rm_path.exists():
os.remove(rm_path)
def start_KE(cfg):
# assert cfg.epochs % 10 == 0 or 'debug' in cfg.name, 'Epoch should be divisible by 10'
assert (
cfg.cs_kd == False
), "CS-KD requires a different data loader, not available in this repos"
ckpt_queue = []
for gen in range(cfg.num_generations):
cfg.start_epoch = 0
# cfg.name = original_name + 'task'
task_ckpt = train_dense(cfg, gen)
ckpt_queue.append(task_ckpt)
# cfg.name = original_name + 'mask'
cfg.pretrained = task_ckpt / "epoch_{}.state".format(cfg.epochs - 1)
if cfg.num_generations == 1:
break
eval_slim(cfg, gen)
cfg.pretrained = task_ckpt / "epoch_{}.state".format(cfg.epochs - 1)
if len(ckpt_queue) > 4:
oldest_ckpt = ckpt_queue.pop(0)
clean_dir(oldest_ckpt, cfg.epochs)
def main(arg_num_threads=16):
print("Starting with {} threads".format(arg_num_threads))
# arg_dataset = 'CUB200' # Flower102, CUB200,HAM,Dog120,MIT67,Aircraft100,MINI_MIT67,FCAM
for arg_dataset in ["Flower102Pytorch"]:
arg_epochs = str(200)
arg_evolve_mode = "rand"
arg_reset_hypothesis = False
arg_enable_cs_kd = False
arg_enable_label_smoothing = True
arg_arch = "Split_ResNet18" # Split_ResNet18,Split_ResNet34,Split_ResNet50,split_googlenet,split_densenet169,split_vgg11_bn,split_densenet121
arg_split_top = "0.5"
arg_bias_split_top = arg_split_top
arg_num_generations = "5"
arg_split_mode = "kels" # wels , kels
exp_name_suffix = "single_gpu_test3"
arg_exp_name = (
"SPLT_CLS_{}_{}_cskd{}_smth{}_k{}_G{}_e{}_ev{}_hReset{}_sm{}_{}/".format(
arg_dataset,
arg_arch,
arg_enable_cs_kd,
arg_enable_label_smoothing,
arg_split_top,
arg_num_generations,
arg_epochs,
arg_evolve_mode,
arg_reset_hypothesis,
arg_split_mode,
exp_name_suffix,
)
)
if arg_arch in ["split_alexnet", "split_vgg11", "split_vgg11_bn"]:
arg_weight_decay = "5e-4"
arg_init = "kaiming_normal"
else:
arg_weight_decay = "1e-4"
arg_init = "kaiming_normal"
argv = [
"--name",
arg_exp_name,
"--evolve_mode",
arg_evolve_mode,
"--num_threads",
"16",
"--gpu",
"0",
"--epochs",
arg_epochs,
"--arch",
arg_arch,
# '--trainer', 'default', #'default', #lottery, # supermask
"--data",
"/mnt/data/datasets/",
"--set",
arg_dataset, # Flower102, CUB200
"--optimizer",
"sgd",
# '--lr', '0.1',
# '--lr_policy', 'step_lr',
# '--warmup_length', '5',
"--lr_policy",
"cosine_lr",
"--warmup_length",
"5",
"--weight_decay",
arg_weight_decay,
"--momentum",
"0.9",
"--batch_size",
"32",
"--conv_type",
"SplitConv", # 'SubnetConv','StrictSubnetConv
"--bn_type",
"SplitBatchNorm",
"--linear_type",
"SplitLinear",
"--split_rate",
arg_split_top,
"--bias_split_rate",
arg_bias_split_top,
"--init",
arg_init, # xavier_normal, kaiming_normal
"--mode",
"fan_in",
"--nonlinearity",
"relu",
"--num_generations",
arg_num_generations,
"--split_mode",
arg_split_mode,
]
if arg_enable_cs_kd:
argv.extend(["--cs_kd"])
if arg_enable_label_smoothing:
argv.extend(["--label_smoothing", "0.1"])
argv.extend(["--lr", "0.256"])
if arg_reset_hypothesis:
argv.extend(["--reset_hypothesis"])
cfg = Config().parse(argv)
start_KE(cfg)
if __name__ == "__main__":
if len(sys.argv) == 1:
main()
else:
cfg = Config().parse(None)
# print(cfg.name)
start_KE(cfg)