|
5 | 5 | cudnn.benchmark =True
|
6 | 6 | from collections import OrderedDict
|
7 | 7 |
|
8 |
| -parser = argparse.ArgumentParser(description='PyTorch SVHN Example') |
9 |
| -parser.add_argument('--type', default='cifar10', help='|'.join(selector.known_models)) |
10 |
| -parser.add_argument('--quant_method', default='linear', help='linear|minmax|log|tanh') |
11 |
| -parser.add_argument('--batch_size', type=int, default=100, help='input batch size for training (default: 64)') |
12 |
| -parser.add_argument('--gpu', default=None, help='index of gpus to use') |
13 |
| -parser.add_argument('--ngpu', type=int, default=8, help='number of gpus to use') |
14 |
| -parser.add_argument('--seed', type=int, default=117, help='random seed (default: 1)') |
15 |
| -parser.add_argument('--model_root', default='~/.torch/models/', help='folder to save the model') |
16 |
| -parser.add_argument('--data_root', default='/tmp/public_dataset/pytorch/', help='folder to save the model') |
17 |
| -parser.add_argument('--logdir', default='log/default', help='folder to save to the log') |
| 8 | +def main(): |
| 9 | + parser = argparse.ArgumentParser(description='PyTorch SVHN Example') |
| 10 | + parser.add_argument('--type', default='cifar10', help='|'.join(selector.known_models)) |
| 11 | + parser.add_argument('--quant_method', default='linear', help='linear|minmax|log|tanh') |
| 12 | + parser.add_argument('--batch_size', type=int, default=100, help='input batch size for training (default: 64)') |
| 13 | + parser.add_argument('--gpu', default=None, help='index of gpus to use') |
| 14 | + parser.add_argument('--ngpu', type=int, default=8, help='number of gpus to use') |
| 15 | + parser.add_argument('--seed', type=int, default=117, help='random seed (default: 1)') |
| 16 | + parser.add_argument('--model_root', default='~/.torch/models/', help='folder to save the model') |
| 17 | + parser.add_argument('--data_root', default='/data/public_dataset/pytorch/', help='folder to save the model') |
| 18 | + parser.add_argument('--logdir', default='log/default', help='folder to save to the log') |
18 | 19 |
|
19 |
| -parser.add_argument('--input_size', type=int, default=224, help='input size of image') |
20 |
| -parser.add_argument('--n_sample', type=int, default=20, help='number of samples to infer the scaling factor') |
21 |
| -parser.add_argument('--param_bits', type=int, default=8, help='bit-width for parameters') |
22 |
| -parser.add_argument('--bn_bits', type=int, default=32, help='bit-width for running mean and std') |
23 |
| -parser.add_argument('--fwd_bits', type=int, default=8, help='bit-width for layer output') |
24 |
| -parser.add_argument('--overflow_rate', type=float, default=0.0, help='overflow rate') |
25 |
| -args = parser.parse_args() |
| 20 | + parser.add_argument('--input_size', type=int, default=224, help='input size of image') |
| 21 | + parser.add_argument('--n_sample', type=int, default=20, help='number of samples to infer the scaling factor') |
| 22 | + parser.add_argument('--param_bits', type=int, default=8, help='bit-width for parameters') |
| 23 | + parser.add_argument('--bn_bits', type=int, default=32, help='bit-width for running mean and std') |
| 24 | + parser.add_argument('--fwd_bits', type=int, default=8, help='bit-width for layer output') |
| 25 | + parser.add_argument('--overflow_rate', type=float, default=0.0, help='overflow rate') |
| 26 | + args = parser.parse_args() |
26 | 27 |
|
27 |
| -args.gpu = misc.auto_select_gpu(utility_bound=0, num_gpu=args.ngpu, selected_gpus=args.gpu) |
28 |
| -args.ngpu = len(args.gpu) |
29 |
| -misc.ensure_dir(args.logdir) |
30 |
| -args.model_root = misc.expand_user(args.model_root) |
31 |
| -args.data_root = misc.expand_user(args.data_root) |
32 |
| -args.input_size = 299 if 'inception' in args.type else args.input_size |
33 |
| -assert args.quant_method in ['linear', 'minmax', 'log', 'tanh'] |
34 |
| -print("=================FLAGS==================") |
35 |
| -for k, v in args.__dict__.items(): |
36 |
| - print('{}: {}'.format(k, v)) |
37 |
| -print("========================================") |
| 28 | + args.gpu = misc.auto_select_gpu(utility_bound=0, num_gpu=args.ngpu, selected_gpus=args.gpu) |
| 29 | + args.ngpu = len(args.gpu) |
| 30 | + misc.ensure_dir(args.logdir) |
| 31 | + args.model_root = misc.expand_user(args.model_root) |
| 32 | + args.data_root = misc.expand_user(args.data_root) |
| 33 | + args.input_size = 299 if 'inception' in args.type else args.input_size |
| 34 | + assert args.quant_method in ['linear', 'minmax', 'log', 'tanh'] |
| 35 | + print("=================FLAGS==================") |
| 36 | + for k, v in args.__dict__.items(): |
| 37 | + print('{}: {}'.format(k, v)) |
| 38 | + print("========================================") |
38 | 39 |
|
39 |
| -assert torch.cuda.is_available(), 'no cuda' |
40 |
| -torch.manual_seed(args.seed) |
41 |
| -torch.cuda.manual_seed(args.seed) |
| 40 | + assert torch.cuda.is_available(), 'no cuda' |
| 41 | + torch.manual_seed(args.seed) |
| 42 | + torch.cuda.manual_seed(args.seed) |
42 | 43 |
|
43 |
| -# load model and dataset fetcher |
44 |
| -model_raw, ds_fetcher, is_imagenet = selector.select(args.type, model_root=args.model_root) |
45 |
| -args.ngpu = args.ngpu if is_imagenet else 1 |
| 44 | + # load model and dataset fetcher |
| 45 | + model_raw, ds_fetcher, is_imagenet = selector.select(args.type, model_root=args.model_root) |
| 46 | + args.ngpu = args.ngpu if is_imagenet else 1 |
46 | 47 |
|
47 |
| -# quantize parameters |
48 |
| -if args.param_bits < 32: |
49 |
| - state_dict = model_raw.state_dict() |
50 |
| - state_dict_quant = OrderedDict() |
51 |
| - sf_dict = OrderedDict() |
52 |
| - for k, v in state_dict.items(): |
53 |
| - if 'running' in k: |
54 |
| - if args.bn_bits >=32: |
55 |
| - print("Ignoring {}".format(k)) |
56 |
| - state_dict_quant[k] = v |
57 |
| - continue |
| 48 | + # quantize parameters |
| 49 | + if args.param_bits < 32: |
| 50 | + state_dict = model_raw.state_dict() |
| 51 | + state_dict_quant = OrderedDict() |
| 52 | + sf_dict = OrderedDict() |
| 53 | + for k, v in state_dict.items(): |
| 54 | + if 'running' in k: |
| 55 | + if args.bn_bits >=32: |
| 56 | + print("Ignoring {}".format(k)) |
| 57 | + state_dict_quant[k] = v |
| 58 | + continue |
| 59 | + else: |
| 60 | + bits = args.bn_bits |
58 | 61 | else:
|
59 |
| - bits = args.bn_bits |
60 |
| - else: |
61 |
| - bits = args.param_bits |
| 62 | + bits = args.param_bits |
62 | 63 |
|
63 |
| - if args.quant_method == 'linear': |
64 |
| - sf = bits - 1. - quant.compute_integral_part(v, overflow_rate=args.overflow_rate) |
65 |
| - v_quant = quant.linear_quantize(v, sf, bits=bits) |
66 |
| - elif args.quant_method == 'log': |
67 |
| - v_quant = quant.log_minmax_quantize(v, bits=bits) |
68 |
| - elif args.quant_method == 'minmax': |
69 |
| - v_quant = quant.min_max_quantize(v, bits=bits) |
70 |
| - else: |
71 |
| - v_quant = quant.tanh_quantize(v, bits=bits) |
72 |
| - state_dict_quant[k] = v_quant |
73 |
| - print(k, bits) |
74 |
| - model_raw.load_state_dict(state_dict_quant) |
75 |
| - |
76 |
| -# quantize forward activation |
77 |
| -if args.fwd_bits < 32: |
78 |
| - model_raw = quant.duplicate_model_with_quant(model_raw, bits=args.fwd_bits, overflow_rate=args.overflow_rate, |
79 |
| - counter=args.n_sample, type=args.quant_method) |
80 |
| - print(model_raw) |
81 |
| - val_ds_tmp = ds_fetcher(10, data_root=args.data_root, train=False, input_size=args.input_size) |
82 |
| - misc.eval_model(model_raw, val_ds_tmp, ngpu=1, n_sample=args.n_sample, is_imagenet=is_imagenet) |
| 64 | + if args.quant_method == 'linear': |
| 65 | + sf = bits - 1. - quant.compute_integral_part(v, overflow_rate=args.overflow_rate) |
| 66 | + v_quant = quant.linear_quantize(v, sf, bits=bits) |
| 67 | + elif args.quant_method == 'log': |
| 68 | + v_quant = quant.log_minmax_quantize(v, bits=bits) |
| 69 | + elif args.quant_method == 'minmax': |
| 70 | + v_quant = quant.min_max_quantize(v, bits=bits) |
| 71 | + else: |
| 72 | + v_quant = quant.tanh_quantize(v, bits=bits) |
| 73 | + state_dict_quant[k] = v_quant |
| 74 | + print(k, bits) |
| 75 | + model_raw.load_state_dict(state_dict_quant) |
83 | 76 |
|
84 |
| -# eval model |
85 |
| -val_ds = ds_fetcher(args.batch_size, data_root=args.data_root, train=False, input_size=args.input_size) |
86 |
| -acc1, acc5 = misc.eval_model(model_raw, val_ds, ngpu=args.ngpu, is_imagenet=is_imagenet) |
| 77 | + # quantize forward activation |
| 78 | + if args.fwd_bits < 32: |
| 79 | + model_raw = quant.duplicate_model_with_quant(model_raw, bits=args.fwd_bits, overflow_rate=args.overflow_rate, |
| 80 | + counter=args.n_sample, type=args.quant_method) |
| 81 | + print(model_raw) |
| 82 | + val_ds_tmp = ds_fetcher(10, data_root=args.data_root, train=False, input_size=args.input_size) |
| 83 | + misc.eval_model(model_raw, val_ds_tmp, ngpu=1, n_sample=args.n_sample, is_imagenet=is_imagenet) |
87 | 84 |
|
88 |
| -# print sf |
89 |
| -print(model_raw) |
90 |
| -res_str = "type={}, quant_method={}, param_bits={}, bn_bits={}, fwd_bits={}, overflow_rate={}, acc1={:.4f}, acc5={:.4f}".format( |
91 |
| - args.type, args.quant_method, args.param_bits, args.bn_bits, args.fwd_bits, args.overflow_rate, acc1, acc5) |
92 |
| -print(res_str) |
93 |
| -with open('acc1_acc5.txt', 'a') as f: |
94 |
| - f.write(res_str + '\n') |
| 85 | + # eval model |
| 86 | + val_ds = ds_fetcher(args.batch_size, data_root=args.data_root, train=False, input_size=args.input_size) |
| 87 | + acc1, acc5 = misc.eval_model(model_raw, val_ds, ngpu=args.ngpu, is_imagenet=is_imagenet) |
95 | 88 |
|
| 89 | + # print sf |
| 90 | + print(model_raw) |
| 91 | + res_str = "type={}, quant_method={}, param_bits={}, bn_bits={}, fwd_bits={}, overflow_rate={}, acc1={:.4f}, acc5={:.4f}".format( |
| 92 | + args.type, args.quant_method, args.param_bits, args.bn_bits, args.fwd_bits, args.overflow_rate, acc1, acc5) |
| 93 | + print(res_str) |
| 94 | + with open('acc1_acc5.txt', 'a') as f: |
| 95 | + f.write(res_str + '\n') |
96 | 96 |
|
97 | 97 |
|
| 98 | +if __name__ == '__main__': |
| 99 | + main() |
0 commit comments