def main(): config = program.load_config(FLAGS.config) program.merge_config(FLAGS.opt) logger.info(config) # check if set use_gpu=True in paddlepaddle cpu version use_gpu = config['Global']['use_gpu'] program.check_gpu(True) alg = config['Global']['algorithm'] assert alg in ['EAST', 'DB', 'Rosetta', 'CRNN', 'STARNet', 'RARE'] if alg in ['Rosetta', 'CRNN', 'STARNet', 'RARE']: config['Global']['char_ops'] = CharacterOps(config['Global']) place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() startup_prog = fluid.Program() eval_program = fluid.Program() 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) init_model(config, eval_program, exe) fluid.io.save_inference_model(dirname="./output/", feeded_var_names=feeded_var_names, main_program=eval_program, target_vars=target_vars, executor=exe, model_filename='model', params_filename='params') print("save success, output_name_list:", fetches_var_name)
def main(): config = program.load_config(FLAGS.config) program.merge_config(FLAGS.opt) logger.info(config) # check if set use_gpu=True in paddlepaddle cpu version use_gpu = config['Global']['use_gpu'] program.check_gpu(use_gpu) alg = config['Global']['algorithm'] assert alg in ['EAST', 'DB', 'Rosetta', 'CRNN', 'STARNet', 'RARE'] if alg in ['Rosetta', 'CRNN', 'STARNet', 'RARE']: config['Global']['char_ops'] = CharacterOps(config['Global']) place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() startup_prog = fluid.Program() eval_program = fluid.Program() 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 alg in ['EAST', 'DB']: 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 load_model(): config = program.load_config('./configs/det/det_r18_vd_db_v1.1.yml') # check if set use_gpu=True in paddlepaddle cpu version use_gpu = config['Global']['use_gpu'] program.check_gpu(use_gpu) place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) det_model = create_module( config['Architecture']['function'])(params=config) startup_prog = fluid.Program() eval_prog = fluid.Program() with fluid.program_guard(eval_prog, startup_prog): with fluid.unique_name.guard(): _, eval_outputs = det_model(mode="test") fetch_name_list = list(eval_outputs.keys()) eval_fetch_list = [eval_outputs[v].name for v in fetch_name_list] eval_prog = eval_prog.clone(for_test=True) exe.run(startup_prog) # load checkpoints checkpoints = config['Global'].get('checkpoints') if checkpoints: path = checkpoints fluid.load(eval_prog, path, exe) logger.info("Finish initing model from {}".format(path)) else: raise Exception("{} not exists!".format(checkpoints)) config_ocr = Cfg.load_config_from_name('vgg_seq2seq') config_ocr['weights'] = './my_weights/transformer.pth' config_ocr['cnn']['pretrained'] = False config_ocr['device'] = 'cpu' config_ocr['predictor']['beamsearch'] = False detector = Predictor(config_ocr) return detector, exe, config, eval_prog, eval_fetch_list
def main(): config = program.load_config(FLAGS.config) program.merge_config(FLAGS.opt) print(config) # check if set use_gpu=True in paddlepaddle cpu version use_gpu = config['Global']['use_gpu'] program.check_gpu(use_gpu) place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) det_model = create_module( config['Architecture']['function'])(params=config) startup_prog = fluid.Program() eval_prog = fluid.Program() with fluid.program_guard(eval_prog, startup_prog): with fluid.unique_name.guard(): _, eval_outputs = det_model(mode="test") fetch_name_list = list(eval_outputs.keys()) eval_fetch_list = [eval_outputs[v].name for v in fetch_name_list] eval_prog = eval_prog.clone(for_test=True) exe.run(startup_prog) # load checkpoints checkpoints = config['Global'].get('checkpoints') if checkpoints: path = checkpoints fluid.load(eval_prog, path, exe) logger.info("Finish initing model from {}".format(path)) else: raise Exception("{} not exists!".format(checkpoints)) save_res_path = config['Global']['save_res_path'] if not os.path.exists(os.path.dirname(save_res_path)): os.makedirs(os.path.dirname(save_res_path)) with open(save_res_path, "wb") as fout: test_reader = reader_main(config=config, mode='test') tackling_num = 0 for data in test_reader(): img_num = len(data) tackling_num = tackling_num + img_num logger.info("tackling_num:%d", tackling_num) img_list = [] ratio_list = [] img_name_list = [] for ino in range(img_num): img_list.append(data[ino][0]) ratio_list.append(data[ino][1]) img_name_list.append(data[ino][2]) img_list = np.concatenate(img_list, axis=0) outs = exe.run(eval_prog,\ feed={'image': img_list},\ fetch_list=eval_fetch_list) global_params = config['Global'] postprocess_params = deepcopy(config["PostProcess"]) postprocess_params.update(global_params) postprocess = create_module(postprocess_params['function'])\ (params=postprocess_params) if config['Global']['algorithm'] == 'EAST': dic = {'f_score': outs[0], 'f_geo': outs[1]} elif config['Global']['algorithm'] == 'DB': dic = {'maps': outs[0]} else: raise Exception("only support algorithm: ['EAST', 'DB']") dt_boxes_list = postprocess(dic, ratio_list) for ino in range(img_num): dt_boxes = dt_boxes_list[ino] img_name = img_name_list[ino] dt_boxes_json = [] for box in dt_boxes: tmp_json = {"transcription": ""} tmp_json['points'] = box.tolist() dt_boxes_json.append(tmp_json) otstr = img_name + "\t" + json.dumps(dt_boxes_json) + "\n" fout.write(otstr.encode()) src_img = cv2.imread(img_name) draw_det_res(dt_boxes, config, src_img, img_name) logger.info("success!")
def main(): config = program.load_config(FLAGS.config) program.merge_config(FLAGS.opt) logger.info(config) # check if set use_gpu=True in paddlepaddle cpu version use_gpu = config['Global']['use_gpu'] program.check_gpu(True) alg = config['Global']['algorithm'] assert alg in ['EAST', 'DB', 'Rosetta', 'CRNN', 'STARNet', 'RARE'] if alg in ['Rosetta', 'CRNN', 'STARNet', 'RARE']: config['Global']['char_ops'] = CharacterOps(config['Global']) place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() startup_program = fluid.Program() train_program = fluid.Program() train_build_outputs = program.build(config, train_program, startup_program, mode='train') train_loader = train_build_outputs[0] train_fetch_name_list = train_build_outputs[1] train_fetch_varname_list = train_build_outputs[2] train_opt_loss_name = train_build_outputs[3] eval_program = fluid.Program() eval_build_outputs = program.build(config, eval_program, startup_program, mode='eval') eval_fetch_name_list = eval_build_outputs[1] eval_fetch_varname_list = eval_build_outputs[2] eval_program = eval_program.clone(for_test=True) train_reader = reader_main(config=config, mode="train") train_loader.set_sample_list_generator(train_reader, places=place) eval_reader = reader_main(config=config, mode="eval") exe = fluid.Executor(place) exe.run(startup_program) # compile program for multi-devices train_compile_program = program.create_multi_devices_program( train_program, train_opt_loss_name) init_model(config, train_program, exe) train_info_dict = {'compile_program':train_compile_program,\ 'train_program':train_program,\ 'reader':train_loader,\ 'fetch_name_list':train_fetch_name_list,\ 'fetch_varname_list':train_fetch_varname_list} eval_info_dict = {'program':eval_program,\ 'reader':eval_reader,\ 'fetch_name_list':eval_fetch_name_list,\ 'fetch_varname_list':eval_fetch_varname_list} if alg in ['EAST', 'DB']: program.train_eval_det_run(config, exe, train_info_dict, eval_info_dict) else: program.train_eval_rec_run(config, exe, train_info_dict, eval_info_dict)
def main(): config = program.load_config(FLAGS.config) program.merge_config(FLAGS.opt) logger.info(config) char_ops = CharacterOps(config['Global']) config['Global']['char_ops'] = char_ops # check if set use_gpu=True in paddlepaddle cpu version use_gpu = config['Global']['use_gpu'] # check_gpu(use_gpu) place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace() exe = fluid.Executor(place) rec_model = create_module(config['Architecture']['function'])(params=config) startup_prog = fluid.Program() eval_prog = fluid.Program() with fluid.program_guard(eval_prog, startup_prog): with fluid.unique_name.guard(): _, outputs = rec_model(mode="test") fetch_name_list = list(outputs.keys()) fetch_varname_list = [outputs[v].name for v in fetch_name_list] eval_prog = eval_prog.clone(for_test=True) exe.run(startup_prog) init_model(config, eval_prog, exe) blobs = reader_main(config, 'test') imgs = next(blobs()) for img in imgs: predict = exe.run(program=eval_prog, feed={"image": img}, fetch_list=fetch_varname_list, return_numpy=False) preds = np.array(predict[0]) if preds.shape[1] == 1: preds = preds.reshape(-1) preds_lod = predict[0].lod()[0] preds_text = char_ops.decode(preds) else: end_pos = np.where(preds[0, :] == 1)[0] if len(end_pos) <= 1: preds_text = preds[0, 1:] else: preds_text = preds[0, 1:end_pos[1]] preds_text = preds_text.reshape(-1) preds_text = char_ops.decode(preds_text) print(preds) print(preds_text) # save for inference model target_var = [] for key, values in outputs.items(): target_var.append(values) fluid.io.save_inference_model( "./output/", feeded_var_names=['image'], target_vars=target_var, executor=exe, main_program=eval_prog, model_filename="model", params_filename="params")