-
Notifications
You must be signed in to change notification settings - Fork 33
/
quant_aware.py
63 lines (51 loc) · 2.32 KB
/
quant_aware.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
# Code for "APQ: Joint Search for Network Architecture, Pruning and Quantization Policy"
# CVPR 2020
# Tianzhe Wang, Kuan Wang, Han Cai, Ji Lin, Zhijian Liu, Song Han
# {usedtobe, kuanwang, hancai, jilin, zhijian, songhan}@mit.edu
from imagenet_codebase.run_manager import ImagenetRunConfig, RunManager
import os
import copy
import torch
from elastic_nn.modules.dynamic_op import DynamicSeparableConv2d, DynamicSeparableQConv2d
from elastic_nn.networks.dynamic_quantized_proxyless import DynamicQuantizedProxylessNASNets
import json
import argparse
parser = argparse.ArgumentParser(description='Quantization-aware Finetuning')
parser.add_argument('--exp_name', type=str, default='test')
parser.add_argument('--id', type=int, default=-1)
args, _ = parser.parse_known_args()
print(args)
if __name__ == '__main__':
exp_dir = 'exps/{}'.format(args.exp_name)
arch_path = '{}/arch'.format(exp_dir)
tmp_lst = json.load(open(arch_path, 'r'))
info, q_info = tmp_lst
print(info)
print(q_info)
DynamicSeparableConv2d.KERNEL_TRANSFORM_MODE = 1
DynamicSeparableQConv2d.KERNEL_TRANSFORM_MODE = 1
dynamic_proxyless = DynamicQuantizedProxylessNASNets(
ks_list=[3, 5, 7], expand_ratio_list=[4, 6], depth_list=[2, 3, 4], base_stage_width='proxyless',
width_mult_list=1.0, dropout_rate=0, n_classes=1000
)
proxylessnas_init = torch.load(
'./models/imagenet-OFA',
map_location='cpu'
)['state_dict']
dynamic_proxyless.load_weights_from_proxylessnas(proxylessnas_init)
init_lr = 1e-3
run_config = ImagenetRunConfig(
test_batch_size=1000, image_size=224, n_worker=16, valid_size=5000, dataset='imagenet', train_batch_size=256,
init_lr=init_lr, n_epochs=30,
)
tmp_dynamic_proxyless = copy.deepcopy(dynamic_proxyless)
run_manager = RunManager(exp_dir, tmp_dynamic_proxyless, run_config, init=False)
tmp_dynamic_proxyless.set_active_subnet(**info)
tmp_dynamic_proxyless.set_quantization_policy(**q_info)
run_manager.reset_running_statistics()
acc = run_manager.finetune()
acc_list = []
acc_list.append((json.dumps(info), json.dumps(q_info), acc))
output_dir = '{}/acc'.format(exp_dir)
json.dump(acc_list, open(output_dir, 'w'))
print('[Finished] Acc: {}'.format(acc))