def main(): startup_prog, eval_program, place, config, _ = program.preprocess() feeded_var_names, target_vars, fetches_var_name = program.build_export( config, eval_program, startup_prog) eval_program = eval_program.clone(for_test=True) exe = fluid.Executor(place) exe.run(startup_prog) if config['Global']['checkpoints'] is not None: path = config['Global']['checkpoints'] else: path = config['Global']['pretrain_weights'] load_model(exe, eval_program, path) save_inference_dir = config['Global']['save_inference_dir'] if not os.path.exists(save_inference_dir): os.makedirs(save_inference_dir) fluid.io.save_inference_model(dirname=save_inference_dir, feeded_var_names=feeded_var_names, main_program=eval_program, target_vars=target_vars, executor=exe, model_filename='model', params_filename='params') print("inference model saved in {}/model and {}/params".format( save_inference_dir, save_inference_dir)) print("save success, output_name_list:", fetches_var_name)
def main(): startup_prog, eval_program, place, config, train_alg_type = program.preprocess() eval_build_outputs = program.build( config, eval_program, startup_prog, mode='test') eval_fetch_name_list = eval_build_outputs[1] eval_fetch_varname_list = eval_build_outputs[2] eval_program = eval_program.clone(for_test=True) exe = fluid.Executor(place) exe.run(startup_prog) init_model(config, eval_program, exe) if train_alg_type == 'det': eval_reader = reader_main(config=config, mode="eval") eval_info_dict = {'program':eval_program,\ 'reader':eval_reader,\ 'fetch_name_list':eval_fetch_name_list,\ 'fetch_varname_list':eval_fetch_varname_list} metrics = eval_det_run(exe, config, eval_info_dict, "eval") logger.info("Eval result: {}".format(metrics)) else: reader_type = config['Global']['reader_yml'] if "benchmark" not in reader_type: eval_reader = reader_main(config=config, mode="eval") eval_info_dict = {'program': eval_program, \ 'reader': eval_reader, \ 'fetch_name_list': eval_fetch_name_list, \ 'fetch_varname_list': eval_fetch_varname_list} metrics = eval_rec_run(exe, config, eval_info_dict, "eval") logger.info("Eval result: {}".format(metrics)) else: eval_info_dict = {'program':eval_program,\ 'fetch_name_list':eval_fetch_name_list,\ 'fetch_varname_list':eval_fetch_varname_list} test_rec_benchmark(exe, config, eval_info_dict)
def main(): # 1. quantization configs quant_config = { # weight quantize type, default is 'channel_wise_abs_max' 'weight_quantize_type': 'channel_wise_abs_max', # activation quantize type, default is 'moving_average_abs_max' 'activation_quantize_type': 'moving_average_abs_max', # weight quantize bit num, default is 8 'weight_bits': 8, # activation quantize bit num, default is 8 'activation_bits': 8, # ops of name_scope in not_quant_pattern list, will not be quantized 'not_quant_pattern': ['skip_quant'], # ops of type in quantize_op_types, will be quantized 'quantize_op_types': ['conv2d', 'depthwise_conv2d', 'mul'], # data type after quantization, such as 'uint8', 'int8', etc. default is 'int8' 'dtype': 'int8', # window size for 'range_abs_max' quantization. defaulf is 10000 'window_size': 10000, # The decay coefficient of moving average, default is 0.9 'moving_rate': 0.9, } startup_prog, eval_program, place, config, alg_type = program.preprocess() feeded_var_names, target_vars, fetches_var_name = program.build_export( config, eval_program, startup_prog) eval_program = eval_program.clone(for_test=True) exe = fluid.Executor(place) exe.run(startup_prog) eval_program = quant_aware( eval_program, place, quant_config, scope=None, for_test=True) init_model(config, eval_program, exe) # 2. Convert the program before save inference program # The dtype of eval_program's weights is float32, but in int8 range. eval_program = convert(eval_program, place, quant_config, scope=None) eval_fetch_name_list = fetches_var_name eval_fetch_varname_list = [v.name for v in target_vars] eval_reader = reader_main(config=config, mode="eval") quant_info_dict = {'program':eval_program,\ 'reader':eval_reader,\ 'fetch_name_list':eval_fetch_name_list,\ 'fetch_varname_list':eval_fetch_varname_list} if alg_type == 'det': final_metrics = eval_det_run(exe, config, quant_info_dict, "eval") else: final_metrics = eval_rec_run(exe, config, quant_info_dict, "eval") print(final_metrics) # 3. Save inference model model_path = "./quant_model" if not os.path.isdir(model_path): os.makedirs(model_path) fluid.io.save_inference_model( dirname=model_path, feeded_var_names=feeded_var_names, target_vars=target_vars, executor=exe, main_program=eval_program, model_filename=model_path + '/model', params_filename=model_path + '/params') print("model saved as {}".format(model_path))