Exemplo n.º 1
0
def cac_opt(quantized_prototxt):
    net = caffe_pb2.NetParameter()
    with open(quantized_prototxt) as f:
        s = f.read()
        txtf.Merge(s,net)
    base_net = caffe_pb2.NetParameter()
    compiled_net_str = caffe.compile_net(quantized_prototxt, caffe.TEST, "MKLDNN")
    txtf.Merge(compiled_net_str, base_net)
    new_net = copy.deepcopy(net)
    avg_pool_layers = [index for index, value in enumerate(base_net.layer) if value.type == 'Pooling' and value.pooling_param.pool == 1] #max=0 ave=1
    layer_infos = [(value, index) for index, value in enumerate(new_net.layer)]
    for index in avg_pool_layers:
        up_quantize, has_concat = find_up_quantize(base_net, index)
        down_quantize = find_down_quantize(base_net, index)
        if up_quantize and down_quantize:
            new_net_up_index = find_index_by_name(base_net.layer[up_quantize].name, layer_infos)
            new_net_down_index = find_index_by_name(base_net.layer[down_quantize].name, layer_infos)
            if not has_concat:
                new_net.layer[new_net_down_index].quantization_param.scale_in[:]=new_net.layer[new_net_up_index].quantization_param.scale_out[:]
            else:
                new_net.layer[new_net_down_index].quantization_param.scale_in[:]=new_net.layer[new_net_up_index].quantization_param.scale_in[:]
            print([new_net.layer[new_net_up_index].name,new_net.layer[new_net_down_index].name])
    with open(quantized_prototxt, 'w') as f:
         f.write(str(new_net))
    print('cac opt done')
Exemplo n.º 2
0
def force_fp32_opt(quantized_prototxt):
    net = caffe_pb2.NetParameter()
    with open(quantized_prototxt) as f:
        s = f.read()
        txtf.Merge(s, net)
    base_net = caffe_pb2.NetParameter()
    compiled_net_str = caffe.compile_net(quantized_prototxt, caffe.TEST,
                                         "MKLDNN")
    txtf.Merge(compiled_net_str, base_net)
    new_net = copy.deepcopy(net)
    quantize_layers_indexes = [
        index for index, value in enumerate(base_net.layer)
        if value.type in quantize_layers
    ]
    layer_infos = [(value, index) for index, value in enumerate(new_net.layer)]
    layer_bottom_name_map = {}
    for index, layer in enumerate(base_net.layer):
        for bottom in layer.bottom:
            if bottom not in layer_bottom_name_map.keys():
                layer_bottom_name_map[bottom] = [index]
            else:
                layer_bottom_name_map[bottom].append(index)
    for index in quantize_layers_indexes:
        if int(base_net.layer[index].quantization_param.bw_layer_out) != 32:
            force_fp32 = True
            if base_net.layer[index].top[0] in layer_bottom_name_map.keys():
                bottom_layer_indexes = layer_bottom_name_map[
                    base_net.layer[index].top[0]]
                for bottom_layer_index in bottom_layer_indexes:
                    next_layer = base_net.layer[bottom_layer_index]
                    if next_layer.top == next_layer.bottom and next_layer.type not in int8_layers:
                        force_fp32 = True
                        break
                    if next_layer.type in int8_layers:
                        force_fp32 = False
            if force_fp32 or index == np.max(quantize_layers_indexes):
                new_net_index = find_index_by_name(base_net.layer[index].name,
                                                   layer_infos)
                new_net.layer[
                    new_net_index].quantization_param.scale_out[:] = [1.0]
                new_net.layer[
                    new_net_index].quantization_param.bw_layer_out = 32
                print(new_net.layer[new_net_index].name)
    with open(quantized_prototxt, 'w') as f:
        f.write(str(new_net))
    print('force_fp32 done')
Exemplo n.º 3
0
    pprint.pprint(cfg)

    while not os.path.exists(args.caffemodel) and args.wait:
        print('Waiting for {} to exist...'.format(args.caffemodel))
        time.sleep(10)

    #caffe.set_mode_gpu()
    #caffe.set_device(args.gpu_id)
    net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST)
    net.name = os.path.splitext(os.path.basename(args.caffemodel))[0]

    imdb = get_imdb(args.imdb_name)
    imdb.competition_mode(args.comp_mode)
    if not cfg.TEST.HAS_RPN:
        imdb.set_proposal_method(cfg.TEST.PROPOSAL_METHOD)

    if args.quantized_prototxt == None:
	test_net(net, imdb, max_per_image=args.max_per_image, vis=args.vis)
    else:
        (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, args.enable_1st_conv_layer)

        (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)
        params_max = sampling.calibrate_parameters(params, winograd_convolutions, "DIRECT", args.quant_mode.upper(), args.conv_algo)
        calibrator.generate_sample_impl(args.prototxt, args.quantized_prototxt, inputs_max, outputs_max, inputs_min, params_max, args.enable_1st_conv_layer)
        compiled_net_str = caffe.compile_net(args.prototxt, caffe.TEST, "MKLDNN")
        raw_net_basename = os.path.basename(args.prototxt)
        compile_net_path = "./compiled_" + raw_net_basename
        with open(compile_net_path, "w") as f:
            f.write(compiled_net_str)
        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, args.enable_1st_conv_layer)
Exemplo n.º 4
0
    while not os.path.exists(args.caffemodel) and args.wait:
        print('Waiting for {} to exist...'.format(args.caffemodel))
        time.sleep(10)

    #caffe.set_mode_gpu()
    #caffe.set_device(args.gpu_id)
    net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST)
    net.name = os.path.splitext(os.path.basename(args.caffemodel))[0]

    imdb = get_imdb(args.imdb_name)
    imdb.competition_mode(args.comp_mode)
    if not cfg.TEST.HAS_RPN:
        imdb.set_proposal_method(cfg.TEST.PROPOSAL_METHOD)
        if cfg.TEST.PROPOSAL_METHOD == 'rpn':
            imdb.config['rpn_file'] = args.rpn_file

    if args.quantized_prototxt == None:
	test_net(net, imdb, max_per_image=args.max_per_image, vis=args.vis)
    else:
        (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, args.enable_1st_conv_layer)
        
        (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)
        params_max = sampling.calibrate_parameters(params, winograd_convolutions, "DIRECT", args.quant_mode.upper(), args.conv_algo)
        calibrator.generate_sample_impl(args.prototxt, args.quantized_prototxt, inputs_max, outputs_max, inputs_min, params_max, args.enable_1st_conv_layer)
        compiled_net_str = caffe.compile_net(args.prototxt, caffe.TEST, "MKLDNN")
        raw_net_basename = os.path.basename(args.prototxt)
        compile_net_path = "./compiled_" + raw_net_basename
        with open(compile_net_path, "w") as f:
            f.write(compiled_net_str)
        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, args.enable_1st_conv_layer)