def __init__(self, args): pre_process_list = [{ 'ResizeTableImage': { 'max_len': args.table_max_len } }, { 'NormalizeImage': { 'std': [0.229, 0.224, 0.225], 'mean': [0.485, 0.456, 0.406], 'scale': '1./255.', 'order': 'hwc' } }, { 'PaddingTableImage': None }, { 'ToCHWImage': None }, { 'KeepKeys': { 'keep_keys': ['image'] } }] postprocess_params = { 'name': 'TableLabelDecode', "character_type": args.table_char_type, "character_dict_path": args.table_char_dict_path, } self.preprocess_op = create_operators(pre_process_list) self.postprocess_op = build_post_process(postprocess_params) self.predictor, self.input_tensor, self.output_tensors, self.config = \ utility.create_predictor(args, 'table', logger)
def __init__(self, args): self.args = args self.det_algorithm = args.det_algorithm self.use_zero_copy_run = args.use_zero_copy_run pre_process_list = [{ 'DetResizeForTest': { 'limit_side_len': args.det_limit_side_len, 'limit_type': args.det_limit_type } }, { 'NormalizeImage': { 'std': [0.229, 0.224, 0.225], 'mean': [0.485, 0.456, 0.406], 'scale': '1./255.', 'order': 'hwc' } }, { 'ToCHWImage': None }, { 'KeepKeys': { 'keep_keys': ['image', 'shape'] } }] postprocess_params = {} if self.det_algorithm == "DB": postprocess_params['name'] = 'DBPostProcess' postprocess_params["thresh"] = args.det_db_thresh postprocess_params["box_thresh"] = args.det_db_box_thresh postprocess_params["max_candidates"] = 1000 postprocess_params["unclip_ratio"] = args.det_db_unclip_ratio postprocess_params["use_dilation"] = True elif self.det_algorithm == "EAST": postprocess_params['name'] = 'EASTPostProcess' postprocess_params["score_thresh"] = args.det_east_score_thresh postprocess_params["cover_thresh"] = args.det_east_cover_thresh postprocess_params["nms_thresh"] = args.det_east_nms_thresh elif self.det_algorithm == "SAST": pre_process_list[0] = { 'DetResizeForTest': {'resize_long': args.det_limit_side_len} } postprocess_params['name'] = 'SASTPostProcess' postprocess_params["score_thresh"] = args.det_sast_score_thresh postprocess_params["nms_thresh"] = args.det_sast_nms_thresh self.det_sast_polygon = args.det_sast_polygon if self.det_sast_polygon: postprocess_params["sample_pts_num"] = 6 postprocess_params["expand_scale"] = 1.2 postprocess_params["shrink_ratio_of_width"] = 0.2 else: postprocess_params["sample_pts_num"] = 2 postprocess_params["expand_scale"] = 1.0 postprocess_params["shrink_ratio_of_width"] = 0.3 else: logger.info("unknown det_algorithm:{}".format(self.det_algorithm)) sys.exit(0) self.preprocess_op = create_operators(pre_process_list) self.postprocess_op = build_post_process(postprocess_params) self.predictor, self.input_tensor, self.output_tensors = utility.create_predictor( args, 'det', logger) # paddle.jit.load(args.det_model_dir)
def main(config, device, logger, vdl_writer): global_config = config['Global'] # build post process post_process_class = build_post_process(config['PostProcess'], global_config) # build model if hasattr(post_process_class, 'character'): config['Architecture']["Head"]['out_channels'] = len( getattr(post_process_class, 'character')) model = build_model(config['Architecture']) init_model(config, model, logger) # create data ops transforms = [] use_padding = False for op in config['Eval']['dataset']['transforms']: op_name = list(op)[0] if 'Label' in op_name: continue if op_name == 'KeepKeys': op[op_name]['keep_keys'] = ['image'] if op_name == "ResizeTableImage": use_padding = True padding_max_len = op['ResizeTableImage']['max_len'] transforms.append(op) global_config['infer_mode'] = True ops = create_operators(transforms, global_config) model.eval() for file in get_image_file_list(config['Global']['infer_img']): logger.info("infer_img: {}".format(file)) with open(file, 'rb') as f: img = f.read() data = {'image': img} batch = transform(data, ops) images = np.expand_dims(batch[0], axis=0) images = paddle.to_tensor(images) preds = model(images) post_result = post_process_class(preds) res_html_code = post_result['res_html_code'] res_loc = post_result['res_loc'] img = cv2.imread(file) imgh, imgw = img.shape[0:2] res_loc_final = [] for rno in range(len(res_loc[0])): x0, y0, x1, y1 = res_loc[0][rno] left = max(int(imgw * x0), 0) top = max(int(imgh * y0), 0) right = min(int(imgw * x1), imgw - 1) bottom = min(int(imgh * y1), imgh - 1) cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2) res_loc_final.append([left, top, right, bottom]) res_loc_str = json.dumps(res_loc_final) logger.info("result: {}, {}".format(res_html_code, res_loc_final)) logger.info("success!")
def main(): global_config = config['Global'] # build model model = build_model(config['Architecture']) load_model(config, model) # create data ops transforms = [] for op in config['Eval']['dataset']['transforms']: transforms.append(op) data_dir = config['Eval']['dataset']['data_dir'] ops = create_operators(transforms, global_config) save_res_path = config['Global']['save_res_path'] class_path = config['Global']['class_path'] idx_to_cls = read_class_list(class_path) if not os.path.exists(os.path.dirname(save_res_path)): os.makedirs(os.path.dirname(save_res_path)) model.eval() warmup_times = 0 count_t = [] with open(save_res_path, "wb") as fout: with open(config['Global']['infer_img'], "rb") as f: lines = f.readlines() for index, data_line in enumerate(lines): if index == 10: warmup_t = time.time() data_line = data_line.decode('utf-8') substr = data_line.strip("\n").split("\t") img_path, label = data_dir + "/" + substr[0], substr[1] data = {'img_path': img_path, 'label': label} with open(data['img_path'], 'rb') as f: img = f.read() data['image'] = img st = time.time() batch = transform(data, ops) batch_pred = [0] * len(batch) for i in range(len(batch)): batch_pred[i] = paddle.to_tensor( np.expand_dims(batch[i], axis=0)) st = time.time() node, edge = model(batch_pred) node = F.softmax(node, -1) count_t.append(time.time() - st) draw_kie_result(batch, node, idx_to_cls, index) logger.info("success!") logger.info("It took {} s for predict {} images.".format( np.sum(count_t), len(count_t))) ips = len(count_t[warmup_times:]) / np.sum(count_t[warmup_times:]) logger.info("The ips is {} images/s".format(ips))
def main(): global_config = config['Global'] # build model model = build_model(config['Architecture']) init_model(config, model, logger) # build post process post_process_class = build_post_process(config['PostProcess'], global_config) # create data ops transforms = [] for op in config['Eval']['dataset']['transforms']: op_name = list(op)[0] if 'Label' in op_name: continue elif op_name == 'KeepKeys': op[op_name]['keep_keys'] = ['image', 'shape'] transforms.append(op) ops = create_operators(transforms, global_config) 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)) model.eval() with open(save_res_path, "wb") as fout: for file in get_image_file_list(config['Global']['infer_img']): logger.info("infer_img: {}".format(file)) with open(file, 'rb') as f: img = f.read() data = {'image': img} batch = transform(data, ops) images = np.expand_dims(batch[0], axis=0) shape_list = np.expand_dims(batch[1], axis=0) images = paddle.to_tensor(images) preds = model(images) post_result = post_process_class(preds, shape_list) points, strs = post_result['points'], post_result['texts'] # write resule dt_boxes_json = [] for poly, str in zip(points, strs): tmp_json = {"transcription": str} tmp_json['points'] = poly.tolist() dt_boxes_json.append(tmp_json) otstr = file + "\t" + json.dumps(dt_boxes_json) + "\n" fout.write(otstr.encode()) src_img = cv2.imread(file) draw_e2e_res(points, strs, config, src_img, file) logger.info("success!")
def main(): global_config = config['Global'] # build post process post_process_class = build_post_process(config['PostProcess'], global_config) # build model if hasattr(post_process_class, 'character'): config['Architecture']["Head"]['out_channels'] = len( getattr(post_process_class, 'character')) model = build_model(config['Architecture']) init_model(config, model, logger) # create data ops transforms = [] for op in config['Eval']['dataset']['transforms']: op_name = list(op)[0] if 'Label' in op_name: continue elif op_name in ['RecResizeImg']: op[op_name]['infer_mode'] = True elif op_name == 'KeepKeys': op[op_name]['keep_keys'] = ['image'] transforms.append(op) global_config['infer_mode'] = True ops = create_operators(transforms, global_config) model.eval() for file in get_image_file_list(config['Global']['infer_img']): logger.info("infer_img: {}".format(file)) with open(file, 'rb') as f: img = f.read() data = {'image': img} batch = transform(data, ops) images = np.expand_dims(batch[0], axis=0) images = paddle.to_tensor(images) preds = model(images) post_result = post_process_class(preds) for rec_reuslt in post_result: logger.info('\t result: {}'.format(rec_reuslt)) logger.info("success!")
def __init__(self, args): self.args = args self.e2e_algorithm = args.e2e_algorithm pre_process_list = [{ 'E2EResizeForTest': {} }, { 'NormalizeImage': { 'std': [0.229, 0.224, 0.225], 'mean': [0.485, 0.456, 0.406], 'scale': '1./255.', 'order': 'hwc' } }, { 'ToCHWImage': None }, { 'KeepKeys': { 'keep_keys': ['image', 'shape'] } }] postprocess_params = {} if self.e2e_algorithm == "PGNet": pre_process_list[0] = { 'E2EResizeForTest': { 'max_side_len': args.e2e_limit_side_len, 'valid_set': 'totaltext' } } postprocess_params['name'] = 'PGPostProcess' postprocess_params["score_thresh"] = args.e2e_pgnet_score_thresh postprocess_params["character_dict_path"] = args.e2e_char_dict_path postprocess_params["valid_set"] = args.e2e_pgnet_valid_set postprocess_params["mode"] = args.e2e_pgnet_mode self.e2e_pgnet_polygon = args.e2e_pgnet_polygon else: logger.info("unknown e2e_algorithm:{}".format(self.e2e_algorithm)) sys.exit(0) self.preprocess_op = create_operators(pre_process_list) self.postprocess_op = build_post_process(postprocess_params) self.predictor, self.input_tensor, self.output_tensors = utility.create_predictor( args, 'e2e', logger) # paddle.jit.load(args.det_model_dir)
def __init__(self, args): self.args = args self.det_algorithm = args.det_algorithm pre_process_list = [{ 'DetResizeForTest': { 'limit_side_len': args.det_limit_side_len, 'limit_type': args.det_limit_type, } }, { 'NormalizeImage': { 'std': [0.229, 0.224, 0.225], 'mean': [0.485, 0.456, 0.406], 'scale': '1./255.', 'order': 'hwc' } }, { 'ToCHWImage': None }, { 'KeepKeys': { 'keep_keys': ['image', 'shape'] } }] postprocess_params = {} if self.det_algorithm == "DB": postprocess_params['name'] = 'DBPostProcess' postprocess_params["thresh"] = args.det_db_thresh postprocess_params["box_thresh"] = args.det_db_box_thresh postprocess_params["max_candidates"] = 1000 postprocess_params["unclip_ratio"] = args.det_db_unclip_ratio postprocess_params["use_dilation"] = args.use_dilation postprocess_params["score_mode"] = args.det_db_score_mode elif self.det_algorithm == "EAST": postprocess_params['name'] = 'EASTPostProcess' postprocess_params["score_thresh"] = args.det_east_score_thresh postprocess_params["cover_thresh"] = args.det_east_cover_thresh postprocess_params["nms_thresh"] = args.det_east_nms_thresh elif self.det_algorithm == "SAST": pre_process_list[0] = { 'DetResizeForTest': { 'resize_long': args.det_limit_side_len } } postprocess_params['name'] = 'SASTPostProcess' postprocess_params["score_thresh"] = args.det_sast_score_thresh postprocess_params["nms_thresh"] = args.det_sast_nms_thresh self.det_sast_polygon = args.det_sast_polygon if self.det_sast_polygon: postprocess_params["sample_pts_num"] = 6 postprocess_params["expand_scale"] = 1.2 postprocess_params["shrink_ratio_of_width"] = 0.2 else: postprocess_params["sample_pts_num"] = 2 postprocess_params["expand_scale"] = 1.0 postprocess_params["shrink_ratio_of_width"] = 0.3 else: logger.info("unknown det_algorithm:{}".format(self.det_algorithm)) sys.exit(0) self.preprocess_op = create_operators(pre_process_list) self.postprocess_op = build_post_process(postprocess_params) self.predictor, self.input_tensor, self.output_tensors, self.config = utility.create_predictor( args, 'det', logger) if args.benchmark: import auto_log pid = os.getpid() self.autolog = auto_log.AutoLogger(model_name="det", model_precision=args.precision, batch_size=1, data_shape="dynamic", save_path=args.save_log_path, inference_config=self.config, pids=pid, process_name=None, gpu_ids=0, time_keys=[ 'preprocess_time', 'inference_time', 'postprocess_time' ], warmup=10)
def main(): global_config = config['Global'] # build post process post_process_class = build_post_process(config['PostProcess'], global_config) # build model if hasattr(post_process_class, 'character'): config['Architecture']["Head"]['out_channels'] = len( getattr(post_process_class, 'character')) model = build_model(config['Architecture']) init_model(config, model, logger) # create data ops transforms = [] for op in config['Eval']['dataset']['transforms']: op_name = list(op)[0] if 'Label' in op_name: continue elif op_name in ['RecResizeImg']: op[op_name]['infer_mode'] = True elif op_name == 'KeepKeys': if config['Architecture']['algorithm'] == "SRN": op[op_name]['keep_keys'] = [ 'image', 'encoder_word_pos', 'gsrm_word_pos', 'gsrm_slf_attn_bias1', 'gsrm_slf_attn_bias2' ] else: op[op_name]['keep_keys'] = ['image'] transforms.append(op) global_config['infer_mode'] = True ops = create_operators(transforms, global_config) save_res_path = config['Global'].get('save_res_path', "./output/rec/predicts_rec.txt") if not os.path.exists(os.path.dirname(save_res_path)): os.makedirs(os.path.dirname(save_res_path)) model.eval() with open(save_res_path, "w") as fout: for file in get_image_file_list(config['Global']['infer_img']): logger.info("infer_img: {}".format(file)) with open(file, 'rb') as f: img = f.read() data = {'image': img} batch = transform(data, ops) if config['Architecture']['algorithm'] == "SRN": encoder_word_pos_list = np.expand_dims(batch[1], axis=0) gsrm_word_pos_list = np.expand_dims(batch[2], axis=0) gsrm_slf_attn_bias1_list = np.expand_dims(batch[3], axis=0) gsrm_slf_attn_bias2_list = np.expand_dims(batch[4], axis=0) others = [ paddle.to_tensor(encoder_word_pos_list), paddle.to_tensor(gsrm_word_pos_list), paddle.to_tensor(gsrm_slf_attn_bias1_list), paddle.to_tensor(gsrm_slf_attn_bias2_list) ] images = np.expand_dims(batch[0], axis=0) images = paddle.to_tensor(images) if config['Architecture']['algorithm'] == "SRN": preds = model(images, others) else: preds = model(images) post_result = post_process_class(preds) for rec_result in post_result: logger.info('\t result: {}'.format(rec_result)) if len(rec_result) >= 2: fout.write(file + "\t" + rec_result[0] + "\t" + str(rec_result[1]) + "\n") logger.info("success!")
def main(): global_config = config['Global'] # build post process post_process_class = build_post_process(config['PostProcess'], global_config) # build model if hasattr(post_process_class, 'character'): char_num = len(getattr(post_process_class, 'character')) if config['Architecture']["algorithm"] in [ "Distillation", ]: # distillation model for key in config['Architecture']["Models"]: config['Architecture']["Models"][key]["Head"][ 'out_channels'] = char_num else: # base rec model config['Architecture']["Head"]['out_channels'] = char_num model = build_model(config['Architecture']) init_model(config, model) # create data ops transforms = [] for op in config['Eval']['dataset']['transforms']: op_name = list(op)[0] if 'Label' in op_name: continue elif op_name in ['RecResizeImg']: op[op_name]['infer_mode'] = True elif op_name == 'KeepKeys': if config['Architecture']['algorithm'] == "SRN": op[op_name]['keep_keys'] = [ 'image', 'encoder_word_pos', 'gsrm_word_pos', 'gsrm_slf_attn_bias1', 'gsrm_slf_attn_bias2' ] else: op[op_name]['keep_keys'] = ['image'] transforms.append(op) global_config['infer_mode'] = True ops = create_operators(transforms, global_config) save_res_path = config['Global'].get('save_res_path', "./output/rec/predicts_rec.txt") if not os.path.exists(os.path.dirname(save_res_path)): os.makedirs(os.path.dirname(save_res_path)) model.eval() with open(save_res_path, "w") as fout: for file in get_image_file_list(config['Global']['infer_img']): logger.info("infer_img: {}".format(file)) with open(file, 'rb') as f: img = f.read() data = {'image': img} batch = transform(data, ops) if config['Architecture']['algorithm'] == "SRN": encoder_word_pos_list = np.expand_dims(batch[1], axis=0) gsrm_word_pos_list = np.expand_dims(batch[2], axis=0) gsrm_slf_attn_bias1_list = np.expand_dims(batch[3], axis=0) gsrm_slf_attn_bias2_list = np.expand_dims(batch[4], axis=0) others = [ paddle.to_tensor(encoder_word_pos_list), paddle.to_tensor(gsrm_word_pos_list), paddle.to_tensor(gsrm_slf_attn_bias1_list), paddle.to_tensor(gsrm_slf_attn_bias2_list) ] images = np.expand_dims(batch[0], axis=0) images = paddle.to_tensor(images) if config['Architecture']['algorithm'] == "SRN": preds = model(images, others) else: preds = model(images) post_result = post_process_class(preds) info = None if isinstance(post_result, dict): rec_info = dict() for key in post_result: if len(post_result[key][0]) >= 2: rec_info[key] = { "label": post_result[key][0][0], "score": float(post_result[key][0][1]), } info = json.dumps(rec_info) else: if len(post_result[0]) >= 2: info = post_result[0][0] + "\t" + str(post_result[0][1]) if info is not None: logger.info("\t result: {}".format(info)) fout.write(file + "\t" + info) logger.info("success!")
def main(): global_config = config['Global'] # build model model = build_model(config['Architecture']) load_model(config, model) # build post process post_process_class = build_post_process(config['PostProcess']) # create data ops transforms = [] for op in config['Eval']['dataset']['transforms']: op_name = list(op)[0] if 'Label' in op_name: continue elif op_name == 'KeepKeys': op[op_name]['keep_keys'] = ['image', 'shape'] transforms.append(op) ops = create_operators(transforms, global_config) 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)) model.eval() with open(save_res_path, "wb") as fout: for file in get_image_file_list(config['Global']['infer_img']): logger.info("infer_img: {}".format(file)) with open(file, 'rb') as f: img = f.read() data = {'image': img} batch = transform(data, ops) images = np.expand_dims(batch[0], axis=0) shape_list = np.expand_dims(batch[1], axis=0) images = paddle.to_tensor(images) preds = model(images) post_result = post_process_class(preds, shape_list) src_img = cv2.imread(file) dt_boxes_json = [] # parser boxes if post_result is dict if isinstance(post_result, dict): det_box_json = {} for k in post_result.keys(): boxes = post_result[k][0]['points'] dt_boxes_list = [] for box in boxes: tmp_json = {"transcription": ""} tmp_json['points'] = box.tolist() dt_boxes_list.append(tmp_json) det_box_json[k] = dt_boxes_list save_det_path = os.path.dirname( config['Global'] ['save_res_path']) + "/det_results_{}/".format(k) draw_det_res(boxes, config, src_img, file, save_det_path) else: boxes = post_result[0]['points'] dt_boxes_json = [] # write result for box in boxes: tmp_json = {"transcription": ""} tmp_json['points'] = box.tolist() dt_boxes_json.append(tmp_json) save_det_path = os.path.dirname( config['Global']['save_res_path']) + "/det_results/" draw_det_res(boxes, config, src_img, file, save_det_path) otstr = file + "\t" + json.dumps(dt_boxes_json) + "\n" fout.write(otstr.encode()) logger.info("success!")