def main(args): # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # test a single image model_results = model_inference(model, args.img) model.show_result( args.img, model_results, win_name='model_results', show=True, score_thr=args.score_thr) url = 'http://' + args.inference_addr + '/predictions/' + args.model_name with open(args.img, 'rb') as image: response = requests.post(url, image) serve_results = response.json() model.show_result( args.img, serve_results, show=True, win_name='serve_results', score_thr=args.score_thr) assert serve_results.keys() == model_results.keys() for key in serve_results.keys(): for model_result, serve_result in zip(model_results[key], serve_results[key]): if isinstance(model_result[0], (int, float)): assert np.allclose(model_result, serve_result) elif isinstance(model_result[0], str): assert model_result == serve_result else: raise TypeError
def main(): parser = ArgumentParser() parser.add_argument('img_root', type=str, help='Image root path') parser.add_argument('img_list', type=str, help='Image path list file') parser.add_argument('config', type=str, help='Config file') parser.add_argument('checkpoint', type=str, help='Checkpoint file') parser.add_argument('--score-thr', type=float, default=0.5, help='Bbox score threshold') parser.add_argument('--out-dir', type=str, default='./results', help='Dir to save ' 'visualize images ' 'and bbox') parser.add_argument('--device', default='cuda:0', help='Device used for inference.') args = parser.parse_args() assert 0 < args.score_thr < 1 # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) if hasattr(model, 'module'): model = model.module if model.cfg.data.test['type'] == 'ConcatDataset': model.cfg.data.test.pipeline = model.cfg.data.test['datasets'][ 0].pipeline # Start Inference out_vis_dir = osp.join(args.out_dir, 'out_vis_dir') mmcv.mkdir_or_exist(out_vis_dir) out_txt_dir = osp.join(args.out_dir, 'out_txt_dir') mmcv.mkdir_or_exist(out_txt_dir) lines = list_from_file(args.img_list) progressbar = ProgressBar(task_num=len(lines)) for line in lines: progressbar.update() img_path = osp.join(args.img_root, line.strip()) if not osp.exists(img_path): raise FileNotFoundError(img_path) # Test a single image result = model_inference(model, img_path) img_name = osp.basename(img_path) # save result save_results(result, out_txt_dir, img_name, score_thr=args.score_thr) # show result out_file = osp.join(out_vis_dir, img_name) kwargs_dict = { 'score_thr': args.score_thr, 'show': False, 'out_file': out_file } model.show_result(img_path, result, **kwargs_dict) print(f'\nInference done, and results saved in {args.out_dir}\n')
def initialize(self, context): properties = context.system_properties self.map_location = 'cuda' if torch.cuda.is_available() else 'cpu' self.device = torch.device(self.map_location + ':' + str(properties.get('gpu_id')) if torch.cuda. is_available() else self.map_location) self.manifest = context.manifest model_dir = properties.get('model_dir') serialized_file = self.manifest['model']['serializedFile'] checkpoint = os.path.join(model_dir, serialized_file) self.config_file = os.path.join(model_dir, 'config.py') self.model = init_detector(self.config_file, checkpoint, self.device) self.initialized = True
def main(): parser = ArgumentParser() parser.add_argument('config', help='Config file.') parser.add_argument('checkpoint', help='Checkpoint file.') parser.add_argument('--device', default='cuda:0', help='Device used for inference.') args = parser.parse_args() # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) # test a single text input_sentence = input('Please enter a sentence you want to test: ') result = text_model_inference(model, input_sentence) # show the results for pred_entities in result: for entity in pred_entities: print(f'{entity[0]}: {input_sentence[entity[1]:entity[2] + 1]}')
def main(): args = parse_args() device = torch.device(args.device) model = init_detector(args.config, args.checkpoint, device=device) camera = cv2.VideoCapture(args.camera_id) print('Press "Esc", "q" or "Q" to exit.') while True: ret_val, img = camera.read() result = model_inference(model, img) ch = cv2.waitKey(1) if ch == 27 or ch == ord('q') or ch == ord('Q'): break model.show_result(img, result, score_thr=args.score_thr, wait_time=1, show=True)
def main(): parser = ArgumentParser( description='Convert MMOCR models from pytorch to ONNX') parser.add_argument('model_config', type=str, help='Config file.') parser.add_argument('model_ckpt', type=str, help='Checkpint file (local or url).') parser.add_argument('model_type', type=str, help='Detection or recognition model to deploy.', choices=['recog', 'det']) parser.add_argument('image_path', type=str, help='Input Image file.') parser.add_argument('--output-file', type=str, help='Output file name of the onnx model.', default='tmp.onnx') parser.add_argument('--device-id', default=0, help='Device used for inference.') parser.add_argument('--opset-version', type=int, help='ONNX opset version, default to 11.', default=11) parser.add_argument( '--verify', action='store_true', help='Whether verify the outputs of onnx and pytorch are same.', default=False) parser.add_argument('--verbose', action='store_true', help='Whether print the computation graph.', default=False) parser.add_argument('--show', action='store_true', help='Whether visualize final output.', default=False) parser.add_argument('--dynamic-export', action='store_true', help='Whether dynamically export onnx model.', default=False) args = parser.parse_args() device = torch.device(type='cuda', index=args.device_id) # build model model = init_detector(args.model_config, args.model_ckpt, device=device) if hasattr(model, 'module'): model = model.module if model.cfg.data.test['type'] == 'ConcatDataset': model.cfg.data.test.pipeline = \ model.cfg.data.test['datasets'][0].pipeline pytorch2onnx(model, model_type=args.model_type, output_file=args.output_file, img_path=args.image_path, opset_version=args.opset_version, verify=args.verify, verbose=args.verbose, show=args.show, device_id=args.device_id, dynamic_export=args.dynamic_export)
def main(): parser = ArgumentParser() parser.add_argument('img_root_path', type=str, help='Image root path') parser.add_argument('img_list', type=str, help='Image path list file') parser.add_argument('config', type=str, help='Config file') parser.add_argument('checkpoint', type=str, help='Checkpoint file') parser.add_argument( '--out-dir', type=str, default='./results', help='Dir to save results') parser.add_argument( '--show', action='store_true', help='show image or save') parser.add_argument( '--device', default='cuda:0', help='Device used for inference.') args = parser.parse_args() # init the logger before other steps timestamp = time.strftime('%Y%m%d_%H%M%S', time.localtime()) log_file = osp.join(args.out_dir, f'{timestamp}.log') logger = get_root_logger(log_file=log_file, log_level='INFO') # build the model from a config file and a checkpoint file model = init_detector(args.config, args.checkpoint, device=args.device) if hasattr(model, 'module'): model = model.module # Start Inference out_vis_dir = osp.join(args.out_dir, 'out_vis_dir') mmcv.mkdir_or_exist(out_vis_dir) correct_vis_dir = osp.join(args.out_dir, 'correct') mmcv.mkdir_or_exist(correct_vis_dir) wrong_vis_dir = osp.join(args.out_dir, 'wrong') mmcv.mkdir_or_exist(wrong_vis_dir) img_paths, pred_labels, gt_labels = [], [], [] lines = list_from_file(args.img_list) progressbar = ProgressBar(task_num=len(lines)) num_gt_label = 0 for line in lines: progressbar.update() item_list = line.strip().split() img_file = item_list[0] gt_label = '' if len(item_list) >= 2: gt_label = item_list[1] num_gt_label += 1 img_path = osp.join(args.img_root_path, img_file) if not osp.exists(img_path): raise FileNotFoundError(img_path) # Test a single image result = model_inference(model, img_path) pred_label = result['text'] out_img_name = '_'.join(img_file.split('/')) out_file = osp.join(out_vis_dir, out_img_name) kwargs_dict = { 'gt_label': gt_label, 'show': args.show, 'out_file': '' if args.show else out_file } model.show_result(img_path, result, **kwargs_dict) if gt_label != '': if gt_label == pred_label: dst_file = osp.join(correct_vis_dir, out_img_name) else: dst_file = osp.join(wrong_vis_dir, out_img_name) shutil.copy(out_file, dst_file) img_paths.append(img_path) gt_labels.append(gt_label) pred_labels.append(pred_label) # Save results save_results(img_paths, pred_labels, gt_labels, args.out_dir) if num_gt_label == len(pred_labels): # eval eval_results = eval_ocr_metric(pred_labels, gt_labels) logger.info('\n' + '-' * 100) info = ('eval on testset with img_root_path ' f'{args.img_root_path} and img_list {args.img_list}\n') logger.info(info) logger.info(eval_results) print(f'\nInference done, and results saved in {args.out_dir}\n')
def init_detector_skip_ckpt(config, ckpt, device): return init_detector(config, device=device)
def __init__(self, det='PANet_IC15', det_config='', det_ckpt='', recog='SEG', recog_config='', recog_ckpt='', kie='', kie_config='', kie_ckpt='', config_dir=os.path.join(str(Path.cwd()), 'configs/'), device=None, **kwargs): textdet_models = { 'DB_r18': { 'config': 'dbnet/dbnet_r18_fpnc_1200e_icdar2015.py', 'ckpt': 'dbnet/' 'dbnet_r18_fpnc_sbn_1200e_icdar2015_20210329-ba3ab597.pth' }, 'DB_r50': { 'config': 'dbnet/dbnet_r50dcnv2_fpnc_1200e_icdar2015.py', 'ckpt': 'dbnet/' 'dbnet_r50dcnv2_fpnc_sbn_1200e_icdar2015_20211025-9fe3b590.pth' }, 'DBPP_r50': { 'config': 'dbnetpp/dbnetpp_r50dcnv2_fpnc_1200e_icdar2015.py', 'ckpt': 'dbnet/' 'dbnetpp_r50dcnv2_fpnc_1200e_icdar2015-20220502-d7a76fff.pth' }, 'DRRG': { 'config': 'drrg/drrg_r50_fpn_unet_1200e_ctw1500.py', 'ckpt': 'drrg/drrg_r50_fpn_unet_1200e_ctw1500_20211022-fb30b001.pth' }, 'FCE_IC15': { 'config': 'fcenet/fcenet_r50_fpn_1500e_icdar2015.py', 'ckpt': 'fcenet/fcenet_r50_fpn_1500e_icdar2015_20211022-daefb6ed.pth' }, 'FCE_CTW_DCNv2': { 'config': 'fcenet/fcenet_r50dcnv2_fpn_1500e_ctw1500.py', 'ckpt': 'fcenet/' + 'fcenet_r50dcnv2_fpn_1500e_ctw1500_20211022-e326d7ec.pth' }, 'MaskRCNN_CTW': { 'config': 'maskrcnn/mask_rcnn_r50_fpn_160e_ctw1500.py', 'ckpt': 'maskrcnn/' 'mask_rcnn_r50_fpn_160e_ctw1500_20210219-96497a76.pth' }, 'MaskRCNN_IC15': { 'config': 'maskrcnn/mask_rcnn_r50_fpn_160e_icdar2015.py', 'ckpt': 'maskrcnn/' 'mask_rcnn_r50_fpn_160e_icdar2015_20210219-8eb340a3.pth' }, 'MaskRCNN_IC17': { 'config': 'maskrcnn/mask_rcnn_r50_fpn_160e_icdar2017.py', 'ckpt': 'maskrcnn/' 'mask_rcnn_r50_fpn_160e_icdar2017_20210218-c6ec3ebb.pth' }, 'PANet_CTW': { 'config': 'panet/panet_r18_fpem_ffm_600e_ctw1500.py', 'ckpt': 'panet/' 'panet_r18_fpem_ffm_sbn_600e_ctw1500_20210219-3b3a9aa3.pth' }, 'PANet_IC15': { 'config': 'panet/panet_r18_fpem_ffm_600e_icdar2015.py', 'ckpt': 'panet/' 'panet_r18_fpem_ffm_sbn_600e_icdar2015_20210219-42dbe46a.pth' }, 'PS_CTW': { 'config': 'psenet/psenet_r50_fpnf_600e_ctw1500.py', 'ckpt': 'psenet/psenet_r50_fpnf_600e_ctw1500_20210401-216fed50.pth' }, 'PS_IC15': { 'config': 'psenet/psenet_r50_fpnf_600e_icdar2015.py', 'ckpt': 'psenet/psenet_r50_fpnf_600e_icdar2015_pretrain-eefd8fe6.pth' }, 'TextSnake': { 'config': 'textsnake/textsnake_r50_fpn_unet_1200e_ctw1500.py', 'ckpt': 'textsnake/textsnake_r50_fpn_unet_1200e_ctw1500-27f65b64.pth' }, 'Tesseract': {} } textrecog_models = { 'CRNN': { 'config': 'crnn/crnn_academic_dataset.py', 'ckpt': 'crnn/crnn_academic-a723a1c5.pth' }, 'SAR': { 'config': 'sar/sar_r31_parallel_decoder_academic.py', 'ckpt': 'sar/sar_r31_parallel_decoder_academic-dba3a4a3.pth' }, 'SAR_CN': { 'config': 'sar/sar_r31_parallel_decoder_chinese.py', 'ckpt': 'sar/sar_r31_parallel_decoder_chineseocr_20210507-b4be8214.pth' }, 'NRTR_1/16-1/8': { 'config': 'nrtr/nrtr_r31_1by16_1by8_academic.py', 'ckpt': 'nrtr/nrtr_r31_1by16_1by8_academic_20211124-f60cebf4.pth' }, 'NRTR_1/8-1/4': { 'config': 'nrtr/nrtr_r31_1by8_1by4_academic.py', 'ckpt': 'nrtr/nrtr_r31_1by8_1by4_academic_20211123-e1fdb322.pth' }, 'RobustScanner': { 'config': 'robust_scanner/robustscanner_r31_academic.py', 'ckpt': 'robustscanner/robustscanner_r31_academic-5f05874f.pth' }, 'SATRN': { 'config': 'satrn/satrn_academic.py', 'ckpt': 'satrn/satrn_academic_20211009-cb8b1580.pth' }, 'SATRN_sm': { 'config': 'satrn/satrn_small.py', 'ckpt': 'satrn/satrn_small_20211009-2cf13355.pth' }, 'ABINet': { 'config': 'abinet/abinet_academic.py', 'ckpt': 'abinet/abinet_academic-f718abf6.pth' }, 'SEG': { 'config': 'seg/seg_r31_1by16_fpnocr_academic.py', 'ckpt': 'seg/seg_r31_1by16_fpnocr_academic-72235b11.pth' }, 'CRNN_TPS': { 'config': 'tps/crnn_tps_academic_dataset.py', 'ckpt': 'tps/crnn_tps_academic_dataset_20210510-d221a905.pth' }, 'Tesseract': {}, 'MASTER': { 'config': 'master/master_academic.py', 'ckpt': 'master/master_r31_12e_ST_MJ_SA-787edd36.pth' } } kie_models = { 'SDMGR': { 'config': 'sdmgr/sdmgr_unet16_60e_wildreceipt.py', 'ckpt': 'sdmgr/sdmgr_unet16_60e_wildreceipt_20210520-7489e6de.pth' } } self.td = det self.tr = recog self.kie = kie self.device = device if self.device is None: self.device = torch.device( 'cuda' if torch.cuda.is_available() else 'cpu') # Check if the det/recog model choice is valid if self.td and self.td not in textdet_models: raise ValueError(self.td, 'is not a supported text detection algorthm') elif self.tr and self.tr not in textrecog_models: raise ValueError(self.tr, 'is not a supported text recognition algorithm') elif self.kie: if self.kie not in kie_models: raise ValueError( self.kie, 'is not a supported key information extraction' ' algorithm') elif not (self.td and self.tr): raise NotImplementedError( self.kie, 'has to run together' ' with text detection and recognition algorithms.') self.detect_model = None if self.td and self.td == 'Tesseract': if tesserocr is None: raise ImportError('Please install tesserocr first. ' 'Check out the installation guide at ' 'https://github.com/sirfz/tesserocr') self.detect_model = 'Tesseract_det' elif self.td: # Build detection model if not det_config: det_config = os.path.join(config_dir, 'textdet/', textdet_models[self.td]['config']) if not det_ckpt: det_ckpt = 'https://download.openmmlab.com/mmocr/textdet/' + \ textdet_models[self.td]['ckpt'] self.detect_model = init_detector(det_config, det_ckpt, device=self.device) self.detect_model = revert_sync_batchnorm(self.detect_model) self.recog_model = None if self.tr and self.tr == 'Tesseract': if tesserocr is None: raise ImportError('Please install tesserocr first. ' 'Check out the installation guide at ' 'https://github.com/sirfz/tesserocr') self.recog_model = 'Tesseract_recog' elif self.tr: # Build recognition model if not recog_config: recog_config = os.path.join( config_dir, 'textrecog/', textrecog_models[self.tr]['config']) if not recog_ckpt: recog_ckpt = 'https://download.openmmlab.com/mmocr/' + \ 'textrecog/' + textrecog_models[self.tr]['ckpt'] self.recog_model = init_detector(recog_config, recog_ckpt, device=self.device) self.recog_model = revert_sync_batchnorm(self.recog_model) self.kie_model = None if self.kie: # Build key information extraction model if not kie_config: kie_config = os.path.join(config_dir, 'kie/', kie_models[self.kie]['config']) if not kie_ckpt: kie_ckpt = 'https://download.openmmlab.com/mmocr/' + \ 'kie/' + kie_models[self.kie]['ckpt'] kie_cfg = Config.fromfile(kie_config) self.kie_model = build_detector(kie_cfg.model, test_cfg=kie_cfg.get('test_cfg')) self.kie_model = revert_sync_batchnorm(self.kie_model) self.kie_model.cfg = kie_cfg load_checkpoint(self.kie_model, kie_ckpt, map_location=self.device) # Attribute check for model in list(filter(None, [self.recog_model, self.detect_model])): if hasattr(model, 'module'): model = model.module