|
11 | 11 |
|
12 | 12 | import _init_paths |
13 | 13 | from fast_rcnn.test import test_net |
| 14 | +from fast_rcnn.test import sample_net |
| 15 | +from fast_rcnn import net_sample_utils |
14 | 16 | from fast_rcnn.config import cfg, cfg_from_file, cfg_from_list |
15 | 17 | from datasets.factory import get_imdb |
16 | 18 | import caffe |
| 19 | + |
| 20 | +import time, os, sys |
| 21 | +utilpath = os.path.split(os.path.realpath(__file__))[0] + "/../../../scripts/" |
| 22 | +sys.path.insert(0, utilpath) |
| 23 | +import calibrator |
| 24 | +import sampling |
17 | 25 | import argparse |
18 | 26 | import pprint |
19 | | -import time, os, sys |
20 | 27 |
|
21 | 28 | def parse_args(): |
22 | 29 | """ |
@@ -49,7 +56,27 @@ def parse_args(): |
49 | 56 | parser.add_argument('--num_dets', dest='max_per_image', |
50 | 57 | help='max number of detections per image', |
51 | 58 | default=100, type=int) |
| 59 | + parser.add_argument('--quantized_net', action='store', dest='quantized_prototxt', |
| 60 | + default=None, type=str) |
| 61 | + parser.add_argument('--sample_iters', action='store', dest='sample_iters', |
| 62 | + default=100, type=int) |
| 63 | + parser.add_argument('--quant_mode', action='store', dest='quant_mode', |
| 64 | + default='single', type=str) |
| 65 | + |
| 66 | + parser.add_argument('-u', '--unsigned_range', dest='unsigned_range', action="store_true", default=False, |
| 67 | + help='to quantize using unsigned range for activation') |
52 | 68 |
|
| 69 | + parser.add_argument('-t', '--concat_use_fp32', dest='concat_use_fp32', action="store_true", default=False, |
| 70 | + help='to use fp32 for concat') |
| 71 | + |
| 72 | + parser.add_argument('-f', '--unify_concat_scales', dest='unify_concat_scales', action="store_true", default=False, |
| 73 | + help='to unify concat scales') |
| 74 | + |
| 75 | + parser.add_argument('-a', '--calibration_algos', dest='calibration_algos', action='store', default="DIRECT", |
| 76 | + help='to choose the calibration alogorithm') |
| 77 | + |
| 78 | + parser.add_argument('-wi', '--conv_algo', dest='conv_algo', action="store_true", default=False, |
| 79 | + help='to choose the convolution algorithm') |
53 | 80 | if len(sys.argv) == 1: |
54 | 81 | parser.print_help() |
55 | 82 | sys.exit(1) |
@@ -87,4 +114,17 @@ def parse_args(): |
87 | 114 | if not cfg.TEST.HAS_RPN: |
88 | 115 | imdb.set_proposal_method(cfg.TEST.PROPOSAL_METHOD) |
89 | 116 |
|
90 | | - test_net(net, imdb, max_per_image=args.max_per_image, vis=args.vis) |
| 117 | + if args.quantized_prototxt == None: |
| 118 | + test_net(net, imdb, max_per_image=args.max_per_image, vis=args.vis) |
| 119 | + else: |
| 120 | + (blobs, params, top_blobs_map, bottom_blobs_map, conv_top_blob_layer_map, conv_bottom_blob_layer_map, winograd_bottoms, winograd_convolutions) = sample_net(args.prototxt, net, imdb, args.sample_iters, args.quant_mode) |
| 121 | + |
| 122 | + (inputs_max, outputs_max, inputs_min) = sampling.calibrate_activations(blobs, conv_top_blob_layer_map, conv_bottom_blob_layer_map, winograd_bottoms, args.calibration_algos, "SINGLE", args.conv_algo) |
| 123 | + params_max = sampling.calibrate_parameters(params, winograd_convolutions, "DIRECT", args.quant_mode.upper(), args.conv_algo) |
| 124 | + calibrator.generate_sample_impl(args.prototxt, args.quantized_prototxt, inputs_max, outputs_max, inputs_min, params_max, False) |
| 125 | + compiled_net_str = caffe.compile_net(args.prototxt, caffe.TEST, "MKLDNN") |
| 126 | + raw_net_basename = os.path.basename(args.prototxt) |
| 127 | + compile_net_path = "./compiled_" + raw_net_basename |
| 128 | + with open(compile_net_path, "w") as f: |
| 129 | + f.write(compiled_net_str) |
| 130 | + calibrator.transform_convolutions(args.quantized_prototxt, compile_net_path, top_blobs_map, bottom_blobs_map, args.unsigned_range, args.concat_use_fp32, args.unify_concat_scales, args.conv_algo, False) |
0 commit comments