openocr-python 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- openocr/__init__.py +11 -0
- openocr/configs/det/dbnet/repvit_db.yml +173 -0
- openocr/configs/rec/abinet/resnet45_trans_abinet_lang.yml +94 -0
- openocr/configs/rec/abinet/resnet45_trans_abinet_wo_lang.yml +93 -0
- openocr/configs/rec/abinet/svtrv2_abinet_lang.yml +130 -0
- openocr/configs/rec/abinet/svtrv2_abinet_wo_lang.yml +128 -0
- openocr/configs/rec/aster/resnet31_lstm_aster_tps_on.yml +93 -0
- openocr/configs/rec/aster/svtrv2_aster.yml +127 -0
- openocr/configs/rec/aster/svtrv2_aster_tps_on.yml +102 -0
- openocr/configs/rec/autostr/autostr_lstm_aster_tps_on.yml +95 -0
- openocr/configs/rec/busnet/svtrv2_busnet.yml +135 -0
- openocr/configs/rec/busnet/svtrv2_busnet_pretraining.yml +134 -0
- openocr/configs/rec/busnet/vit_busnet.yml +104 -0
- openocr/configs/rec/busnet/vit_busnet_pretraining.yml +104 -0
- openocr/configs/rec/cam/convnextv2_cam_tps_on.yml +118 -0
- openocr/configs/rec/cam/convnextv2_tiny_cam_tps_on.yml +118 -0
- openocr/configs/rec/cam/svtrv2_cam_tps_on.yml +123 -0
- openocr/configs/rec/cdistnet/resnet45_trans_cdistnet.yml +93 -0
- openocr/configs/rec/cdistnet/svtrv2_cdistnet.yml +139 -0
- openocr/configs/rec/cppd/svtr_base_cppd.yml +123 -0
- openocr/configs/rec/cppd/svtr_base_cppd_ch.yml +126 -0
- openocr/configs/rec/cppd/svtr_base_cppd_h8.yml +123 -0
- openocr/configs/rec/cppd/svtr_base_cppd_syn.yml +124 -0
- openocr/configs/rec/cppd/svtrv2_cppd.yml +150 -0
- openocr/configs/rec/dan/resnet45_fpn_dan.yml +98 -0
- openocr/configs/rec/dan/svtrv2_dan.yml +130 -0
- openocr/configs/rec/focalsvtr/focalsvtr_ctc.yml +137 -0
- openocr/configs/rec/gtc/svtrv2_lnconv_nrtr_gtc.yml +168 -0
- openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_long_infer.yml +151 -0
- openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_smtr_long.yml +150 -0
- openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_stream.yml +152 -0
- openocr/configs/rec/igtr/svtr_base_ds_igtr.yml +157 -0
- openocr/configs/rec/lister/focalsvtr_lister_wo_fem_maxratio12.yml +133 -0
- openocr/configs/rec/lister/svtrv2_lister_wo_fem_maxratio12.yml +138 -0
- openocr/configs/rec/lpv/svtr_base_lpv.yml +124 -0
- openocr/configs/rec/lpv/svtr_base_lpv_wo_glrm.yml +123 -0
- openocr/configs/rec/lpv/svtrv2_lpv.yml +147 -0
- openocr/configs/rec/lpv/svtrv2_lpv_wo_glrm.yml +146 -0
- openocr/configs/rec/maerec/vit_nrtr.yml +116 -0
- openocr/configs/rec/matrn/resnet45_trans_matrn.yml +95 -0
- openocr/configs/rec/matrn/svtrv2_matrn.yml +130 -0
- openocr/configs/rec/mgpstr/svtrv2_mgpstr_only_char.yml +140 -0
- openocr/configs/rec/mgpstr/vit_base_mgpstr_only_char.yml +111 -0
- openocr/configs/rec/mgpstr/vit_large_mgpstr_only_char.yml +110 -0
- openocr/configs/rec/mgpstr/vit_mgpstr.yml +110 -0
- openocr/configs/rec/mgpstr/vit_mgpstr_only_char.yml +110 -0
- openocr/configs/rec/moran/resnet31_lstm_moran.yml +92 -0
- openocr/configs/rec/nrtr/focalsvtr_nrtr_maxraio12.yml +145 -0
- openocr/configs/rec/nrtr/nrtr.yml +107 -0
- openocr/configs/rec/nrtr/svtr_base_nrtr.yml +118 -0
- openocr/configs/rec/nrtr/svtr_base_nrtr_syn.yml +119 -0
- openocr/configs/rec/nrtr/svtrv2_nrtr.yml +146 -0
- openocr/configs/rec/ote/svtr_base_h8_ote.yml +117 -0
- openocr/configs/rec/ote/svtr_base_ote.yml +116 -0
- openocr/configs/rec/parseq/focalsvtr_parseq_maxratio12.yml +140 -0
- openocr/configs/rec/parseq/svrtv2_parseq.yml +136 -0
- openocr/configs/rec/parseq/vit_parseq.yml +100 -0
- openocr/configs/rec/robustscanner/resnet31_robustscanner.yml +102 -0
- openocr/configs/rec/robustscanner/svtrv2_robustscanner.yml +134 -0
- openocr/configs/rec/sar/resnet31_lstm_sar.yml +94 -0
- openocr/configs/rec/sar/svtrv2_sar.yml +128 -0
- openocr/configs/rec/seed/resnet31_lstm_seed_tps_on.yml +96 -0
- openocr/configs/rec/smtr/focalsvtr_smtr.yml +150 -0
- openocr/configs/rec/smtr/focalsvtr_smtr_long.yml +133 -0
- openocr/configs/rec/smtr/svtrv2_smtr.yml +150 -0
- openocr/configs/rec/smtr/svtrv2_smtr_bi.yml +136 -0
- openocr/configs/rec/srn/resnet50_fpn_srn.yml +97 -0
- openocr/configs/rec/srn/svtrv2_srn.yml +131 -0
- openocr/configs/rec/svtrs/convnextv2_ctc.yml +105 -0
- openocr/configs/rec/svtrs/convnextv2_h8_ctc.yml +105 -0
- openocr/configs/rec/svtrs/convnextv2_h8_rctc.yml +106 -0
- openocr/configs/rec/svtrs/convnextv2_rctc.yml +106 -0
- openocr/configs/rec/svtrs/convnextv2_tiny_h8_ctc.yml +105 -0
- openocr/configs/rec/svtrs/convnextv2_tiny_h8_rctc.yml +106 -0
- openocr/configs/rec/svtrs/crnn_ctc.yml +99 -0
- openocr/configs/rec/svtrs/crnn_ctc_long.yml +116 -0
- openocr/configs/rec/svtrs/focalnet_base_ctc.yml +108 -0
- openocr/configs/rec/svtrs/focalnet_base_rctc.yml +109 -0
- openocr/configs/rec/svtrs/focalsvtr_ctc.yml +106 -0
- openocr/configs/rec/svtrs/focalsvtr_rctc.yml +107 -0
- openocr/configs/rec/svtrs/resnet45_trans_ctc.yml +103 -0
- openocr/configs/rec/svtrs/resnet45_trans_rctc.yml +104 -0
- openocr/configs/rec/svtrs/svtr_base_ctc.yml +110 -0
- openocr/configs/rec/svtrs/svtr_base_rctc.yml +111 -0
- openocr/configs/rec/svtrs/svtrnet_ctc_syn.yml +111 -0
- openocr/configs/rec/svtrs/vit_ctc.yml +103 -0
- openocr/configs/rec/svtrs/vit_rctc.yml +103 -0
- openocr/configs/rec/svtrv2/repsvtr_ch.yml +121 -0
- openocr/configs/rec/svtrv2/svtrv2_ch.yml +133 -0
- openocr/configs/rec/svtrv2/svtrv2_ctc.yml +136 -0
- openocr/configs/rec/svtrv2/svtrv2_rctc.yml +135 -0
- openocr/configs/rec/svtrv2/svtrv2_small_rctc.yml +135 -0
- openocr/configs/rec/svtrv2/svtrv2_smtr_gtc_rctc.yml +162 -0
- openocr/configs/rec/svtrv2/svtrv2_smtr_gtc_rctc_ch.yml +153 -0
- openocr/configs/rec/svtrv2/svtrv2_tiny_rctc.yml +135 -0
- openocr/configs/rec/visionlan/resnet45_trans_visionlan_LA.yml +103 -0
- openocr/configs/rec/visionlan/resnet45_trans_visionlan_LF_1.yml +102 -0
- openocr/configs/rec/visionlan/resnet45_trans_visionlan_LF_2.yml +103 -0
- openocr/configs/rec/visionlan/svtrv2_visionlan_LA.yml +112 -0
- openocr/configs/rec/visionlan/svtrv2_visionlan_LF_1.yml +111 -0
- openocr/configs/rec/visionlan/svtrv2_visionlan_LF_2.yml +112 -0
- openocr/demo_gradio.py +128 -0
- openocr/opendet/modeling/__init__.py +11 -0
- openocr/opendet/modeling/backbones/__init__.py +14 -0
- openocr/opendet/modeling/backbones/repvit.py +340 -0
- openocr/opendet/modeling/base_detector.py +69 -0
- openocr/opendet/modeling/heads/__init__.py +14 -0
- openocr/opendet/modeling/heads/db_head.py +73 -0
- openocr/opendet/modeling/necks/__init__.py +14 -0
- openocr/opendet/modeling/necks/db_fpn.py +609 -0
- openocr/opendet/postprocess/__init__.py +18 -0
- openocr/opendet/postprocess/db_postprocess.py +273 -0
- openocr/opendet/preprocess/__init__.py +154 -0
- openocr/opendet/preprocess/crop_resize.py +121 -0
- openocr/opendet/preprocess/db_resize_for_test.py +135 -0
- openocr/openrec/losses/__init__.py +62 -0
- openocr/openrec/losses/abinet_loss.py +42 -0
- openocr/openrec/losses/ar_loss.py +23 -0
- openocr/openrec/losses/cam_loss.py +48 -0
- openocr/openrec/losses/cdistnet_loss.py +34 -0
- openocr/openrec/losses/ce_loss.py +68 -0
- openocr/openrec/losses/cppd_loss.py +77 -0
- openocr/openrec/losses/ctc_loss.py +33 -0
- openocr/openrec/losses/igtr_loss.py +12 -0
- openocr/openrec/losses/lister_loss.py +14 -0
- openocr/openrec/losses/lpv_loss.py +30 -0
- openocr/openrec/losses/mgp_loss.py +34 -0
- openocr/openrec/losses/parseq_loss.py +12 -0
- openocr/openrec/losses/robustscanner_loss.py +20 -0
- openocr/openrec/losses/seed_loss.py +46 -0
- openocr/openrec/losses/smtr_loss.py +12 -0
- openocr/openrec/losses/srn_loss.py +40 -0
- openocr/openrec/losses/visionlan_loss.py +58 -0
- openocr/openrec/metrics/__init__.py +19 -0
- openocr/openrec/metrics/rec_metric.py +270 -0
- openocr/openrec/metrics/rec_metric_gtc.py +58 -0
- openocr/openrec/metrics/rec_metric_long.py +142 -0
- openocr/openrec/metrics/rec_metric_mgp.py +93 -0
- openocr/openrec/modeling/__init__.py +11 -0
- openocr/openrec/modeling/base_recognizer.py +69 -0
- openocr/openrec/modeling/common.py +238 -0
- openocr/openrec/modeling/decoders/__init__.py +109 -0
- openocr/openrec/modeling/decoders/abinet_decoder.py +283 -0
- openocr/openrec/modeling/decoders/aster_decoder.py +170 -0
- openocr/openrec/modeling/decoders/bus_decoder.py +133 -0
- openocr/openrec/modeling/decoders/cam_decoder.py +43 -0
- openocr/openrec/modeling/decoders/cdistnet_decoder.py +334 -0
- openocr/openrec/modeling/decoders/cppd_decoder.py +393 -0
- openocr/openrec/modeling/decoders/ctc_decoder.py +203 -0
- openocr/openrec/modeling/decoders/dan_decoder.py +203 -0
- openocr/openrec/modeling/decoders/igtr_decoder.py +815 -0
- openocr/openrec/modeling/decoders/lister_decoder.py +535 -0
- openocr/openrec/modeling/decoders/lpv_decoder.py +119 -0
- openocr/openrec/modeling/decoders/matrn_decoder.py +236 -0
- openocr/openrec/modeling/decoders/mgp_decoder.py +99 -0
- openocr/openrec/modeling/decoders/nrtr_decoder.py +439 -0
- openocr/openrec/modeling/decoders/ote_decoder.py +205 -0
- openocr/openrec/modeling/decoders/parseq_decoder.py +504 -0
- openocr/openrec/modeling/decoders/rctc_decoder.py +70 -0
- openocr/openrec/modeling/decoders/robustscanner_decoder.py +749 -0
- openocr/openrec/modeling/decoders/sar_decoder.py +236 -0
- openocr/openrec/modeling/decoders/smtr_decoder.py +621 -0
- openocr/openrec/modeling/decoders/smtr_decoder_nattn.py +521 -0
- openocr/openrec/modeling/decoders/srn_decoder.py +283 -0
- openocr/openrec/modeling/decoders/visionlan_decoder.py +321 -0
- openocr/openrec/modeling/encoders/__init__.py +39 -0
- openocr/openrec/modeling/encoders/autostr_encoder.py +327 -0
- openocr/openrec/modeling/encoders/cam_encoder.py +760 -0
- openocr/openrec/modeling/encoders/convnextv2.py +213 -0
- openocr/openrec/modeling/encoders/focalsvtr.py +631 -0
- openocr/openrec/modeling/encoders/nrtr_encoder.py +28 -0
- openocr/openrec/modeling/encoders/rec_hgnet.py +346 -0
- openocr/openrec/modeling/encoders/rec_lcnetv3.py +488 -0
- openocr/openrec/modeling/encoders/rec_mobilenet_v3.py +132 -0
- openocr/openrec/modeling/encoders/rec_mv1_enhance.py +254 -0
- openocr/openrec/modeling/encoders/rec_nrtr_mtb.py +37 -0
- openocr/openrec/modeling/encoders/rec_resnet_31.py +213 -0
- openocr/openrec/modeling/encoders/rec_resnet_45.py +183 -0
- openocr/openrec/modeling/encoders/rec_resnet_fpn.py +216 -0
- openocr/openrec/modeling/encoders/rec_resnet_vd.py +252 -0
- openocr/openrec/modeling/encoders/repvit.py +338 -0
- openocr/openrec/modeling/encoders/resnet31_rnn.py +123 -0
- openocr/openrec/modeling/encoders/svtrnet.py +574 -0
- openocr/openrec/modeling/encoders/svtrnet2dpos.py +616 -0
- openocr/openrec/modeling/encoders/svtrv2.py +470 -0
- openocr/openrec/modeling/encoders/svtrv2_lnconv.py +503 -0
- openocr/openrec/modeling/encoders/svtrv2_lnconv_two33.py +517 -0
- openocr/openrec/modeling/encoders/vit.py +120 -0
- openocr/openrec/modeling/transforms/__init__.py +15 -0
- openocr/openrec/modeling/transforms/aster_tps.py +262 -0
- openocr/openrec/modeling/transforms/moran.py +136 -0
- openocr/openrec/modeling/transforms/tps.py +246 -0
- openocr/openrec/optimizer/__init__.py +73 -0
- openocr/openrec/optimizer/lr.py +227 -0
- openocr/openrec/postprocess/__init__.py +72 -0
- openocr/openrec/postprocess/abinet_postprocess.py +37 -0
- openocr/openrec/postprocess/ar_postprocess.py +63 -0
- openocr/openrec/postprocess/ce_postprocess.py +43 -0
- openocr/openrec/postprocess/char_postprocess.py +108 -0
- openocr/openrec/postprocess/cppd_postprocess.py +42 -0
- openocr/openrec/postprocess/ctc_postprocess.py +119 -0
- openocr/openrec/postprocess/igtr_postprocess.py +100 -0
- openocr/openrec/postprocess/lister_postprocess.py +59 -0
- openocr/openrec/postprocess/mgp_postprocess.py +143 -0
- openocr/openrec/postprocess/nrtr_postprocess.py +75 -0
- openocr/openrec/postprocess/smtr_postprocess.py +73 -0
- openocr/openrec/postprocess/srn_postprocess.py +80 -0
- openocr/openrec/postprocess/visionlan_postprocess.py +81 -0
- openocr/openrec/preprocess/__init__.py +173 -0
- openocr/openrec/preprocess/abinet_aug.py +473 -0
- openocr/openrec/preprocess/abinet_label_encode.py +36 -0
- openocr/openrec/preprocess/ar_label_encode.py +36 -0
- openocr/openrec/preprocess/auto_augment.py +1012 -0
- openocr/openrec/preprocess/cam_label_encode.py +141 -0
- openocr/openrec/preprocess/ce_label_encode.py +116 -0
- openocr/openrec/preprocess/char_label_encode.py +36 -0
- openocr/openrec/preprocess/cppd_label_encode.py +173 -0
- openocr/openrec/preprocess/ctc_label_encode.py +124 -0
- openocr/openrec/preprocess/ep_label_encode.py +38 -0
- openocr/openrec/preprocess/igtr_label_encode.py +360 -0
- openocr/openrec/preprocess/mgp_label_encode.py +95 -0
- openocr/openrec/preprocess/parseq_aug.py +150 -0
- openocr/openrec/preprocess/rec_aug.py +211 -0
- openocr/openrec/preprocess/resize.py +534 -0
- openocr/openrec/preprocess/smtr_label_encode.py +125 -0
- openocr/openrec/preprocess/srn_label_encode.py +37 -0
- openocr/openrec/preprocess/visionlan_label_encode.py +67 -0
- openocr/tools/create_lmdb_dataset.py +118 -0
- openocr/tools/data/__init__.py +94 -0
- openocr/tools/data/collate_fn.py +100 -0
- openocr/tools/data/lmdb_dataset.py +142 -0
- openocr/tools/data/lmdb_dataset_test.py +166 -0
- openocr/tools/data/multi_scale_sampler.py +177 -0
- openocr/tools/data/ratio_dataset.py +217 -0
- openocr/tools/data/ratio_dataset_test.py +273 -0
- openocr/tools/data/ratio_dataset_tvresize.py +213 -0
- openocr/tools/data/ratio_dataset_tvresize_test.py +276 -0
- openocr/tools/data/ratio_sampler.py +190 -0
- openocr/tools/data/simple_dataset.py +263 -0
- openocr/tools/data/strlmdb_dataset.py +143 -0
- openocr/tools/engine/__init__.py +5 -0
- openocr/tools/engine/config.py +158 -0
- openocr/tools/engine/trainer.py +621 -0
- openocr/tools/eval_rec.py +41 -0
- openocr/tools/eval_rec_all_ch.py +184 -0
- openocr/tools/eval_rec_all_en.py +206 -0
- openocr/tools/eval_rec_all_long.py +119 -0
- openocr/tools/eval_rec_all_long_simple.py +122 -0
- openocr/tools/export_rec.py +118 -0
- openocr/tools/infer/onnx_engine.py +65 -0
- openocr/tools/infer/predict_rec.py +140 -0
- openocr/tools/infer/utility.py +234 -0
- openocr/tools/infer_det.py +449 -0
- openocr/tools/infer_e2e.py +462 -0
- openocr/tools/infer_e2e_parallel.py +184 -0
- openocr/tools/infer_rec.py +371 -0
- openocr/tools/train_rec.py +37 -0
- openocr/tools/utility.py +45 -0
- openocr/tools/utils/EN_symbol_dict.txt +94 -0
- openocr/tools/utils/__init__.py +0 -0
- openocr/tools/utils/ckpt.py +87 -0
- openocr/tools/utils/dict/ar_dict.txt +117 -0
- openocr/tools/utils/dict/arabic_dict.txt +161 -0
- openocr/tools/utils/dict/be_dict.txt +145 -0
- openocr/tools/utils/dict/bg_dict.txt +140 -0
- openocr/tools/utils/dict/chinese_cht_dict.txt +8421 -0
- openocr/tools/utils/dict/cyrillic_dict.txt +163 -0
- openocr/tools/utils/dict/devanagari_dict.txt +167 -0
- openocr/tools/utils/dict/en_dict.txt +63 -0
- openocr/tools/utils/dict/fa_dict.txt +136 -0
- openocr/tools/utils/dict/french_dict.txt +136 -0
- openocr/tools/utils/dict/german_dict.txt +143 -0
- openocr/tools/utils/dict/hi_dict.txt +162 -0
- openocr/tools/utils/dict/it_dict.txt +118 -0
- openocr/tools/utils/dict/japan_dict.txt +4399 -0
- openocr/tools/utils/dict/ka_dict.txt +153 -0
- openocr/tools/utils/dict/kie_dict/xfund_class_list.txt +4 -0
- openocr/tools/utils/dict/korean_dict.txt +3688 -0
- openocr/tools/utils/dict/latex_symbol_dict.txt +111 -0
- openocr/tools/utils/dict/latin_dict.txt +185 -0
- openocr/tools/utils/dict/layout_dict/layout_cdla_dict.txt +10 -0
- openocr/tools/utils/dict/layout_dict/layout_publaynet_dict.txt +5 -0
- openocr/tools/utils/dict/layout_dict/layout_table_dict.txt +1 -0
- openocr/tools/utils/dict/mr_dict.txt +153 -0
- openocr/tools/utils/dict/ne_dict.txt +153 -0
- openocr/tools/utils/dict/oc_dict.txt +96 -0
- openocr/tools/utils/dict/pu_dict.txt +130 -0
- openocr/tools/utils/dict/rs_dict.txt +91 -0
- openocr/tools/utils/dict/rsc_dict.txt +134 -0
- openocr/tools/utils/dict/ru_dict.txt +125 -0
- openocr/tools/utils/dict/spin_dict.txt +68 -0
- openocr/tools/utils/dict/ta_dict.txt +128 -0
- openocr/tools/utils/dict/table_dict.txt +277 -0
- openocr/tools/utils/dict/table_master_structure_dict.txt +39 -0
- openocr/tools/utils/dict/table_structure_dict.txt +28 -0
- openocr/tools/utils/dict/table_structure_dict_ch.txt +48 -0
- openocr/tools/utils/dict/te_dict.txt +151 -0
- openocr/tools/utils/dict/ug_dict.txt +114 -0
- openocr/tools/utils/dict/uk_dict.txt +142 -0
- openocr/tools/utils/dict/ur_dict.txt +137 -0
- openocr/tools/utils/dict/xi_dict.txt +110 -0
- openocr/tools/utils/dict90.txt +90 -0
- openocr/tools/utils/e2e_metric/Deteval.py +802 -0
- openocr/tools/utils/e2e_metric/polygon_fast.py +70 -0
- openocr/tools/utils/e2e_utils/extract_batchsize.py +86 -0
- openocr/tools/utils/e2e_utils/extract_textpoint_fast.py +479 -0
- openocr/tools/utils/e2e_utils/extract_textpoint_slow.py +582 -0
- openocr/tools/utils/e2e_utils/pgnet_pp_utils.py +159 -0
- openocr/tools/utils/e2e_utils/visual.py +152 -0
- openocr/tools/utils/en_dict.txt +95 -0
- openocr/tools/utils/gen_label.py +68 -0
- openocr/tools/utils/ic15_dict.txt +36 -0
- openocr/tools/utils/logging.py +56 -0
- openocr/tools/utils/poly_nms.py +132 -0
- openocr/tools/utils/ppocr_keys_v1.txt +6623 -0
- openocr/tools/utils/stats.py +58 -0
- openocr/tools/utils/utility.py +165 -0
- openocr/tools/utils/visual.py +117 -0
- openocr_python-0.0.2.dist-info/LICENCE +201 -0
- openocr_python-0.0.2.dist-info/METADATA +98 -0
- openocr_python-0.0.2.dist-info/RECORD +323 -0
- openocr_python-0.0.2.dist-info/WHEEL +5 -0
- openocr_python-0.0.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
|
|
3
|
+
__all__ = ['build_metric']
|
|
4
|
+
|
|
5
|
+
from .rec_metric import RecMetric
|
|
6
|
+
from .rec_metric_gtc import RecGTCMetric
|
|
7
|
+
from .rec_metric_long import RecMetricLong
|
|
8
|
+
from .rec_metric_mgp import RecMPGMetric
|
|
9
|
+
|
|
10
|
+
support_dict = ['RecMetric', 'RecMetricLong', 'RecGTCMetric', 'RecMPGMetric']
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def build_metric(config):
|
|
14
|
+
config = copy.deepcopy(config)
|
|
15
|
+
module_name = config.pop('name')
|
|
16
|
+
assert module_name in support_dict, Exception(
|
|
17
|
+
'metric only support {}'.format(support_dict))
|
|
18
|
+
module_class = eval(module_name)(**config)
|
|
19
|
+
return module_class
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import string
|
|
2
|
+
import numpy as np
|
|
3
|
+
from rapidfuzz.distance import Levenshtein
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def match_ss(ss1, ss2):
|
|
7
|
+
s1_len = len(ss1)
|
|
8
|
+
for c_i in range(s1_len):
|
|
9
|
+
if ss1[c_i:] == ss2[:s1_len - c_i]:
|
|
10
|
+
return ss2[s1_len - c_i:]
|
|
11
|
+
return ss2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def stream_match(text):
|
|
15
|
+
bs = len(text)
|
|
16
|
+
s_list = []
|
|
17
|
+
conf_list = []
|
|
18
|
+
for s_conf in text:
|
|
19
|
+
s_list.append(s_conf[0])
|
|
20
|
+
conf_list.append(s_conf[1])
|
|
21
|
+
s_n = bs
|
|
22
|
+
s_start = s_list[0][:-1]
|
|
23
|
+
s_new = s_start
|
|
24
|
+
for s_i in range(1, s_n):
|
|
25
|
+
s_start = match_ss(
|
|
26
|
+
s_start, s_list[s_i][1:-1] if s_i < s_n - 1 else s_list[s_i][1:])
|
|
27
|
+
s_new += s_start
|
|
28
|
+
return s_new, sum(conf_list) / bs
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class RecMetric(object):
|
|
32
|
+
|
|
33
|
+
def __init__(self,
|
|
34
|
+
main_indicator='acc',
|
|
35
|
+
is_filter=False,
|
|
36
|
+
is_lower=True,
|
|
37
|
+
ignore_space=True,
|
|
38
|
+
stream=False,
|
|
39
|
+
with_ratio=False,
|
|
40
|
+
max_len=25,
|
|
41
|
+
max_ratio=4,
|
|
42
|
+
**kwargs):
|
|
43
|
+
self.main_indicator = main_indicator
|
|
44
|
+
self.is_filter = is_filter
|
|
45
|
+
self.is_lower = is_lower
|
|
46
|
+
self.ignore_space = ignore_space
|
|
47
|
+
self.stream = stream
|
|
48
|
+
self.eps = 1e-5
|
|
49
|
+
self.with_ratio = with_ratio
|
|
50
|
+
self.max_len = max_len
|
|
51
|
+
self.max_ratio = max_ratio
|
|
52
|
+
self.reset()
|
|
53
|
+
|
|
54
|
+
def _normalize_text(self, text):
|
|
55
|
+
text = ''.join(
|
|
56
|
+
filter(lambda x: x in (string.digits + string.ascii_letters),
|
|
57
|
+
text))
|
|
58
|
+
return text
|
|
59
|
+
|
|
60
|
+
def __call__(self,
|
|
61
|
+
pred_label,
|
|
62
|
+
batch=None,
|
|
63
|
+
training=False,
|
|
64
|
+
*args,
|
|
65
|
+
**kwargs):
|
|
66
|
+
if self.with_ratio and not training:
|
|
67
|
+
return self.eval_all_metric(pred_label, batch)
|
|
68
|
+
else:
|
|
69
|
+
return self.eval_metric(pred_label)
|
|
70
|
+
|
|
71
|
+
def eval_metric(self, pred_label, *args, **kwargs):
|
|
72
|
+
preds, labels = pred_label
|
|
73
|
+
correct_num = 0
|
|
74
|
+
all_num = 0
|
|
75
|
+
norm_edit_dis = 0.0
|
|
76
|
+
for (pred, pred_conf), (target, _) in zip(preds, labels):
|
|
77
|
+
if self.stream:
|
|
78
|
+
assert len(labels) == 1
|
|
79
|
+
pred, _ = stream_match(preds)
|
|
80
|
+
if self.ignore_space:
|
|
81
|
+
pred = pred.replace(' ', '')
|
|
82
|
+
target = target.replace(' ', '')
|
|
83
|
+
if self.is_filter:
|
|
84
|
+
pred = self._normalize_text(pred)
|
|
85
|
+
target = self._normalize_text(target)
|
|
86
|
+
if self.is_lower:
|
|
87
|
+
pred = pred.lower()
|
|
88
|
+
target = target.lower()
|
|
89
|
+
norm_edit_dis += Levenshtein.normalized_distance(pred, target)
|
|
90
|
+
if pred == target:
|
|
91
|
+
correct_num += 1
|
|
92
|
+
all_num += 1
|
|
93
|
+
self.correct_num += correct_num
|
|
94
|
+
self.all_num += all_num
|
|
95
|
+
self.norm_edit_dis += norm_edit_dis
|
|
96
|
+
return {
|
|
97
|
+
'acc': correct_num / (all_num + self.eps),
|
|
98
|
+
'norm_edit_dis': 1 - norm_edit_dis / (all_num + self.eps),
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
def eval_all_metric(self, pred_label, batch=None, *args, **kwargs):
|
|
102
|
+
if self.with_ratio:
|
|
103
|
+
ratio = batch[-1]
|
|
104
|
+
preds, labels = pred_label
|
|
105
|
+
correct_num = 0
|
|
106
|
+
correct_num_real = 0
|
|
107
|
+
correct_num_lower = 0
|
|
108
|
+
correct_num_ignore_space = 0
|
|
109
|
+
correct_num_ignore_space_lower = 0
|
|
110
|
+
correct_num_ignore_space_symbol = 0
|
|
111
|
+
all_num = 0
|
|
112
|
+
norm_edit_dis = 0.0
|
|
113
|
+
each_len_num = [0 for _ in range(self.max_len)]
|
|
114
|
+
each_len_correct_num = [0 for _ in range(self.max_len)]
|
|
115
|
+
each_len_norm_edit_dis = [0 for _ in range(self.max_len)]
|
|
116
|
+
each_ratio_num = [0 for _ in range(self.max_ratio)]
|
|
117
|
+
each_ratio_correct_num = [0 for _ in range(self.max_ratio)]
|
|
118
|
+
each_ratio_norm_edit_dis = [0 for _ in range(self.max_ratio)]
|
|
119
|
+
for (pred, pred_conf), (target, _) in zip(preds, labels):
|
|
120
|
+
if self.stream:
|
|
121
|
+
assert len(labels) == 1
|
|
122
|
+
pred, _ = stream_match(preds)
|
|
123
|
+
if pred == target:
|
|
124
|
+
correct_num_real += 1
|
|
125
|
+
|
|
126
|
+
if pred.lower() == target.lower():
|
|
127
|
+
correct_num_lower += 1
|
|
128
|
+
|
|
129
|
+
if self.ignore_space:
|
|
130
|
+
pred = pred.replace(' ', '')
|
|
131
|
+
target = target.replace(' ', '')
|
|
132
|
+
if pred == target:
|
|
133
|
+
correct_num_ignore_space += 1
|
|
134
|
+
|
|
135
|
+
if pred.lower() == target.lower():
|
|
136
|
+
correct_num_ignore_space_lower += 1
|
|
137
|
+
|
|
138
|
+
if self.is_filter:
|
|
139
|
+
pred = self._normalize_text(pred)
|
|
140
|
+
target = self._normalize_text(target)
|
|
141
|
+
if pred == target:
|
|
142
|
+
correct_num_ignore_space_symbol += 1
|
|
143
|
+
|
|
144
|
+
if self.is_lower:
|
|
145
|
+
pred = pred.lower()
|
|
146
|
+
target = target.lower()
|
|
147
|
+
dis = Levenshtein.normalized_distance(pred, target)
|
|
148
|
+
norm_edit_dis += dis
|
|
149
|
+
ratio_i = ratio[all_num] - 1 if ratio[
|
|
150
|
+
all_num] < self.max_ratio else self.max_ratio - 1
|
|
151
|
+
len_i = max(0, min(self.max_len, len(target)) - 1)
|
|
152
|
+
if pred == target:
|
|
153
|
+
correct_num += 1
|
|
154
|
+
each_len_correct_num[len_i] += 1
|
|
155
|
+
each_ratio_correct_num[ratio_i] += 1
|
|
156
|
+
each_len_num[len_i] += 1
|
|
157
|
+
each_len_norm_edit_dis[len_i] += dis
|
|
158
|
+
|
|
159
|
+
each_ratio_num[ratio_i] += 1
|
|
160
|
+
each_ratio_norm_edit_dis[ratio_i] += dis
|
|
161
|
+
all_num += 1
|
|
162
|
+
self.correct_num += correct_num
|
|
163
|
+
self.correct_num_real += correct_num_real
|
|
164
|
+
self.correct_num_lower += correct_num_lower
|
|
165
|
+
self.correct_num_ignore_space += correct_num_ignore_space
|
|
166
|
+
self.correct_num_ignore_space_lower += correct_num_ignore_space_lower
|
|
167
|
+
self.correct_num_ignore_space_symbol += correct_num_ignore_space_symbol
|
|
168
|
+
self.all_num += all_num
|
|
169
|
+
self.norm_edit_dis += norm_edit_dis
|
|
170
|
+
self.each_len_num = self.each_len_num + np.array(each_len_num)
|
|
171
|
+
self.each_len_correct_num = self.each_len_correct_num + np.array(
|
|
172
|
+
each_len_correct_num)
|
|
173
|
+
self.each_len_norm_edit_dis = self.each_len_norm_edit_dis + np.array(
|
|
174
|
+
each_len_norm_edit_dis)
|
|
175
|
+
self.each_ratio_num = self.each_ratio_num + np.array(each_ratio_num)
|
|
176
|
+
self.each_ratio_correct_num = self.each_ratio_correct_num + np.array(
|
|
177
|
+
each_ratio_correct_num)
|
|
178
|
+
self.each_ratio_norm_edit_dis = self.each_ratio_norm_edit_dis + np.array(
|
|
179
|
+
each_ratio_norm_edit_dis)
|
|
180
|
+
return {
|
|
181
|
+
'acc': correct_num / (all_num + self.eps),
|
|
182
|
+
'norm_edit_dis': 1 - norm_edit_dis / (all_num + self.eps),
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
def get_metric(self, training=False):
|
|
186
|
+
"""
|
|
187
|
+
return metrics {
|
|
188
|
+
'acc': 0,
|
|
189
|
+
'norm_edit_dis': 0,
|
|
190
|
+
}
|
|
191
|
+
"""
|
|
192
|
+
if self.with_ratio and not training:
|
|
193
|
+
return self.get_all_metric()
|
|
194
|
+
acc = 1.0 * self.correct_num / (self.all_num + self.eps)
|
|
195
|
+
norm_edit_dis = 1 - self.norm_edit_dis / (self.all_num + self.eps)
|
|
196
|
+
num_samples = self.all_num
|
|
197
|
+
self.reset()
|
|
198
|
+
return {
|
|
199
|
+
'acc': acc,
|
|
200
|
+
'norm_edit_dis': norm_edit_dis,
|
|
201
|
+
'num_samples': num_samples
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
def get_all_metric(self):
|
|
205
|
+
"""
|
|
206
|
+
return metrics {
|
|
207
|
+
'acc': 0,
|
|
208
|
+
'norm_edit_dis': 0,
|
|
209
|
+
}
|
|
210
|
+
"""
|
|
211
|
+
acc = 1.0 * self.correct_num / (self.all_num + self.eps)
|
|
212
|
+
acc_real = 1.0 * self.correct_num_real / (self.all_num + self.eps)
|
|
213
|
+
acc_lower = 1.0 * self.correct_num_lower / (self.all_num + self.eps)
|
|
214
|
+
acc_ignore_space = 1.0 * self.correct_num_ignore_space / (
|
|
215
|
+
self.all_num + self.eps)
|
|
216
|
+
acc_ignore_space_lower = 1.0 * self.correct_num_ignore_space_lower / (
|
|
217
|
+
self.all_num + self.eps)
|
|
218
|
+
acc_ignore_space_symbol = 1.0 * self.correct_num_ignore_space_symbol / (
|
|
219
|
+
self.all_num + self.eps)
|
|
220
|
+
|
|
221
|
+
norm_edit_dis = 1 - self.norm_edit_dis / (self.all_num + self.eps)
|
|
222
|
+
num_samples = self.all_num
|
|
223
|
+
each_len_acc = (self.each_len_correct_num /
|
|
224
|
+
(self.each_len_num + self.eps)).tolist()
|
|
225
|
+
each_len_norm_edit_dis = (1 -
|
|
226
|
+
((self.each_len_norm_edit_dis) /
|
|
227
|
+
((self.each_len_num) + self.eps))).tolist()
|
|
228
|
+
each_len_num = self.each_len_num.tolist()
|
|
229
|
+
each_ratio_acc = (self.each_ratio_correct_num /
|
|
230
|
+
(self.each_ratio_num + self.eps)).tolist()
|
|
231
|
+
each_ratio_norm_edit_dis = (1 - ((self.each_ratio_norm_edit_dis) / (
|
|
232
|
+
(self.each_ratio_num) + self.eps))).tolist()
|
|
233
|
+
each_ratio_num = self.each_ratio_num.tolist()
|
|
234
|
+
self.reset()
|
|
235
|
+
return {
|
|
236
|
+
'acc': acc,
|
|
237
|
+
'acc_real': acc_real,
|
|
238
|
+
'acc_lower': acc_lower,
|
|
239
|
+
'acc_ignore_space': acc_ignore_space,
|
|
240
|
+
'acc_ignore_space_lower': acc_ignore_space_lower,
|
|
241
|
+
'acc_ignore_space_symbol': acc_ignore_space_symbol,
|
|
242
|
+
'acc_ignore_space_lower_symbol': acc,
|
|
243
|
+
'each_len_num': each_len_num,
|
|
244
|
+
'each_len_acc': each_len_acc,
|
|
245
|
+
'each_len_norm_edit_dis': each_len_norm_edit_dis,
|
|
246
|
+
'each_ratio_num': each_ratio_num,
|
|
247
|
+
'each_ratio_acc': each_ratio_acc,
|
|
248
|
+
'each_ratio_norm_edit_dis': each_ratio_norm_edit_dis,
|
|
249
|
+
'norm_edit_dis': norm_edit_dis,
|
|
250
|
+
'num_samples': num_samples
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
def reset(self):
|
|
254
|
+
self.correct_num = 0
|
|
255
|
+
self.all_num = 0
|
|
256
|
+
self.norm_edit_dis = 0
|
|
257
|
+
self.correct_num_real = 0
|
|
258
|
+
self.correct_num_lower = 0
|
|
259
|
+
self.correct_num_ignore_space = 0
|
|
260
|
+
self.correct_num_ignore_space_lower = 0
|
|
261
|
+
self.correct_num_ignore_space_symbol = 0
|
|
262
|
+
self.each_len_num = np.array([0 for _ in range(self.max_len)])
|
|
263
|
+
self.each_len_correct_num = np.array([0 for _ in range(self.max_len)])
|
|
264
|
+
self.each_len_norm_edit_dis = np.array(
|
|
265
|
+
[0. for _ in range(self.max_len)])
|
|
266
|
+
self.each_ratio_num = np.array([0 for _ in range(self.max_ratio)])
|
|
267
|
+
self.each_ratio_correct_num = np.array(
|
|
268
|
+
[0 for _ in range(self.max_ratio)])
|
|
269
|
+
self.each_ratio_norm_edit_dis = np.array(
|
|
270
|
+
[0. for _ in range(self.max_ratio)])
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from .rec_metric import RecMetric
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class RecGTCMetric(object):
|
|
5
|
+
|
|
6
|
+
def __init__(self,
|
|
7
|
+
main_indicator='acc',
|
|
8
|
+
is_filter=False,
|
|
9
|
+
ignore_space=True,
|
|
10
|
+
stream=False,
|
|
11
|
+
with_ratio=False,
|
|
12
|
+
max_len=25,
|
|
13
|
+
max_ratio=4,
|
|
14
|
+
**kwargs):
|
|
15
|
+
self.main_indicator = main_indicator
|
|
16
|
+
self.is_filter = is_filter
|
|
17
|
+
self.ignore_space = ignore_space
|
|
18
|
+
self.eps = 1e-5
|
|
19
|
+
self.gtc_metric = RecMetric(main_indicator=main_indicator,
|
|
20
|
+
is_filter=is_filter,
|
|
21
|
+
ignore_space=ignore_space,
|
|
22
|
+
stream=stream,
|
|
23
|
+
with_ratio=with_ratio,
|
|
24
|
+
max_len=max_len,
|
|
25
|
+
max_ratio=max_ratio)
|
|
26
|
+
self.ctc_metric = RecMetric(main_indicator=main_indicator,
|
|
27
|
+
is_filter=is_filter,
|
|
28
|
+
ignore_space=ignore_space,
|
|
29
|
+
stream=stream,
|
|
30
|
+
with_ratio=with_ratio,
|
|
31
|
+
max_len=max_len,
|
|
32
|
+
max_ratio=max_ratio)
|
|
33
|
+
|
|
34
|
+
def __call__(self,
|
|
35
|
+
pred_label,
|
|
36
|
+
batch=None,
|
|
37
|
+
training=False,
|
|
38
|
+
*args,
|
|
39
|
+
**kwargs):
|
|
40
|
+
|
|
41
|
+
ctc_metric = self.ctc_metric(pred_label[1], batch, training=training)
|
|
42
|
+
gtc_metric = self.gtc_metric(pred_label[0], batch, training=training)
|
|
43
|
+
ctc_metric['gtc_acc'] = gtc_metric['acc']
|
|
44
|
+
ctc_metric['gtc_norm_edit_dis'] = gtc_metric['norm_edit_dis']
|
|
45
|
+
return ctc_metric
|
|
46
|
+
|
|
47
|
+
def get_metric(self):
|
|
48
|
+
"""
|
|
49
|
+
return metrics {
|
|
50
|
+
'acc': 0,
|
|
51
|
+
'norm_edit_dis': 0,
|
|
52
|
+
}
|
|
53
|
+
"""
|
|
54
|
+
ctc_metric = self.ctc_metric.get_metric()
|
|
55
|
+
gtc_metric = self.gtc_metric.get_metric()
|
|
56
|
+
ctc_metric['gtc_acc'] = gtc_metric['acc']
|
|
57
|
+
ctc_metric['gtc_norm_edit_dis'] = gtc_metric['norm_edit_dis']
|
|
58
|
+
return ctc_metric
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import string
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from rapidfuzz.distance import Levenshtein
|
|
5
|
+
|
|
6
|
+
from .rec_metric import stream_match
|
|
7
|
+
|
|
8
|
+
# f_pred = open('pred_focal_subs_rand1_h2_bi_first.txt', 'w')
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class RecMetricLong(object):
|
|
12
|
+
|
|
13
|
+
def __init__(self,
|
|
14
|
+
main_indicator='acc',
|
|
15
|
+
is_filter=False,
|
|
16
|
+
ignore_space=True,
|
|
17
|
+
stream=False,
|
|
18
|
+
**kwargs):
|
|
19
|
+
self.main_indicator = main_indicator
|
|
20
|
+
self.is_filter = is_filter
|
|
21
|
+
self.ignore_space = ignore_space
|
|
22
|
+
self.stream = stream
|
|
23
|
+
self.eps = 1e-5
|
|
24
|
+
self.max_len = 201
|
|
25
|
+
self.reset()
|
|
26
|
+
|
|
27
|
+
def _normalize_text(self, text):
|
|
28
|
+
text = ''.join(
|
|
29
|
+
filter(lambda x: x in (string.digits + string.ascii_letters),
|
|
30
|
+
text))
|
|
31
|
+
return text.lower()
|
|
32
|
+
|
|
33
|
+
def __call__(self, pred_label, *args, **kwargs):
|
|
34
|
+
preds, labels = pred_label
|
|
35
|
+
correct_num = 0
|
|
36
|
+
correct_num_slice = 0
|
|
37
|
+
f_l_acc = 0
|
|
38
|
+
all_num = 0
|
|
39
|
+
norm_edit_dis = 0.0
|
|
40
|
+
len_acc = 0
|
|
41
|
+
each_len_num = [0 for _ in range(self.max_len)]
|
|
42
|
+
each_len_correct_num = [0 for _ in range(self.max_len)]
|
|
43
|
+
each_len_norm_edit_dis = [0 for _ in range(self.max_len)]
|
|
44
|
+
for (pred, pred_conf), (target, _) in zip(preds, labels):
|
|
45
|
+
if self.stream:
|
|
46
|
+
assert len(labels) == 1
|
|
47
|
+
pred, _ = stream_match(preds)
|
|
48
|
+
if self.ignore_space:
|
|
49
|
+
pred = pred.replace(' ', '')
|
|
50
|
+
target = target.replace(' ', '')
|
|
51
|
+
if self.is_filter:
|
|
52
|
+
pred = self._normalize_text(pred)
|
|
53
|
+
target = self._normalize_text(target)
|
|
54
|
+
dis = Levenshtein.normalized_distance(pred, target)
|
|
55
|
+
norm_edit_dis += dis
|
|
56
|
+
# print(pred, target)
|
|
57
|
+
if pred == target:
|
|
58
|
+
correct_num += 1
|
|
59
|
+
each_len_correct_num[len(target)] += 1
|
|
60
|
+
each_len_num[len(target)] += 1
|
|
61
|
+
each_len_norm_edit_dis[len(target)] += dis
|
|
62
|
+
# f_pred.write(pred+'\t'+target+'\t1'+'\n')
|
|
63
|
+
# print(pred, target, 1)
|
|
64
|
+
# else:
|
|
65
|
+
# f_pred.write(pred+'\t'+target+'\t0'+'\n')
|
|
66
|
+
# print(pred, target, 0)
|
|
67
|
+
if len(pred) >= 1 and len(target) >= 1:
|
|
68
|
+
if pred[0] == target[0] and pred[-1] == target[-1]:
|
|
69
|
+
f_l_acc += 1
|
|
70
|
+
if len(pred) == len(target):
|
|
71
|
+
len_acc += 1
|
|
72
|
+
if pred == target[:len(pred)]:
|
|
73
|
+
# if pred == target[-len(pred):]:
|
|
74
|
+
correct_num_slice += 1
|
|
75
|
+
all_num += 1
|
|
76
|
+
self.correct_num += correct_num
|
|
77
|
+
self.correct_num_slice += correct_num_slice
|
|
78
|
+
self.f_l_acc += f_l_acc
|
|
79
|
+
self.all_num += all_num
|
|
80
|
+
self.len_acc += len_acc
|
|
81
|
+
self.each_len_num = self.each_len_num + np.array(each_len_num)
|
|
82
|
+
self.each_len_correct_num = self.each_len_correct_num + np.array(
|
|
83
|
+
each_len_correct_num)
|
|
84
|
+
self.each_len_norm_edit_dis = self.each_len_norm_edit_dis + np.array(
|
|
85
|
+
each_len_norm_edit_dis)
|
|
86
|
+
self.norm_edit_dis += norm_edit_dis
|
|
87
|
+
return {
|
|
88
|
+
'acc': correct_num / (all_num + self.eps),
|
|
89
|
+
'norm_edit_dis': 1 - norm_edit_dis / (all_num + self.eps),
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
def get_metric(self):
|
|
93
|
+
"""
|
|
94
|
+
return metrics {
|
|
95
|
+
'acc': 0,
|
|
96
|
+
'norm_edit_dis': 0,
|
|
97
|
+
}
|
|
98
|
+
"""
|
|
99
|
+
acc = 1.0 * self.correct_num / (self.all_num + self.eps)
|
|
100
|
+
acc_slice = 1.0 * self.correct_num_slice / (self.all_num + self.eps)
|
|
101
|
+
f_l_acc = 1.0 * self.f_l_acc / (self.all_num + self.eps)
|
|
102
|
+
len_acc = 1.0 * self.len_acc / (self.all_num + self.eps)
|
|
103
|
+
norm_edit_dis = 1 - self.norm_edit_dis / (self.all_num + self.eps)
|
|
104
|
+
each_len_acc = (self.each_len_correct_num /
|
|
105
|
+
(self.each_len_num + self.eps)).tolist()
|
|
106
|
+
# each_len_acc_25 = each_len_acc[:26]
|
|
107
|
+
# each_len_acc_26 = each_len_acc[26:]
|
|
108
|
+
each_len_norm_edit_dis = (1 -
|
|
109
|
+
((self.each_len_norm_edit_dis) /
|
|
110
|
+
((self.each_len_num) + self.eps))).tolist()
|
|
111
|
+
# each_len_norm_edit_dis_25 = each_len_norm_edit_dis[:26]
|
|
112
|
+
# each_len_norm_edit_dis_26 = each_len_norm_edit_dis[26:]
|
|
113
|
+
each_len_num = self.each_len_num.tolist()
|
|
114
|
+
all_num = self.all_num
|
|
115
|
+
self.reset()
|
|
116
|
+
return {
|
|
117
|
+
'acc': acc,
|
|
118
|
+
'norm_edit_dis': norm_edit_dis,
|
|
119
|
+
'acc_slice': acc_slice,
|
|
120
|
+
'f_l_acc': f_l_acc,
|
|
121
|
+
'len_acc': len_acc,
|
|
122
|
+
'each_len_num': each_len_num,
|
|
123
|
+
'each_len_acc': each_len_acc,
|
|
124
|
+
# "each_len_acc_25": each_len_acc_25,
|
|
125
|
+
# "each_len_acc_26": each_len_acc_26,
|
|
126
|
+
'each_len_norm_edit_dis': each_len_norm_edit_dis,
|
|
127
|
+
# "each_len_norm_edit_dis_25":each_len_norm_edit_dis_25,
|
|
128
|
+
# "each_len_norm_edit_dis_26":each_len_norm_edit_dis_26,
|
|
129
|
+
'all_num': all_num
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
def reset(self):
|
|
133
|
+
self.correct_num = 0
|
|
134
|
+
self.all_num = 0
|
|
135
|
+
self.norm_edit_dis = 0
|
|
136
|
+
self.correct_num_slice = 0
|
|
137
|
+
self.each_len_num = np.array([0 for _ in range(self.max_len)])
|
|
138
|
+
self.each_len_correct_num = np.array([0 for _ in range(self.max_len)])
|
|
139
|
+
self.each_len_norm_edit_dis = np.array(
|
|
140
|
+
[0. for _ in range(self.max_len)])
|
|
141
|
+
self.f_l_acc = 0
|
|
142
|
+
self.len_acc = 0
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from .rec_metric import RecMetric
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class RecMPGMetric(object):
|
|
5
|
+
|
|
6
|
+
def __init__(self,
|
|
7
|
+
main_indicator='acc',
|
|
8
|
+
is_filter=False,
|
|
9
|
+
ignore_space=True,
|
|
10
|
+
stream=False,
|
|
11
|
+
with_ratio=False,
|
|
12
|
+
max_len=25,
|
|
13
|
+
max_ratio=4,
|
|
14
|
+
**kwargs):
|
|
15
|
+
self.main_indicator = main_indicator
|
|
16
|
+
self.is_filter = is_filter
|
|
17
|
+
self.ignore_space = ignore_space
|
|
18
|
+
self.eps = 1e-5
|
|
19
|
+
self.char_metric = RecMetric(main_indicator=main_indicator,
|
|
20
|
+
is_filter=is_filter,
|
|
21
|
+
ignore_space=ignore_space,
|
|
22
|
+
stream=stream,
|
|
23
|
+
with_ratio=with_ratio,
|
|
24
|
+
max_len=max_len,
|
|
25
|
+
max_ratio=max_ratio)
|
|
26
|
+
self.bpe_metric = RecMetric(main_indicator=main_indicator,
|
|
27
|
+
is_filter=is_filter,
|
|
28
|
+
ignore_space=ignore_space,
|
|
29
|
+
stream=stream,
|
|
30
|
+
with_ratio=with_ratio,
|
|
31
|
+
max_len=max_len,
|
|
32
|
+
max_ratio=max_ratio)
|
|
33
|
+
|
|
34
|
+
self.wp_metric = RecMetric(main_indicator=main_indicator,
|
|
35
|
+
is_filter=is_filter,
|
|
36
|
+
ignore_space=ignore_space,
|
|
37
|
+
stream=stream,
|
|
38
|
+
with_ratio=with_ratio,
|
|
39
|
+
max_len=max_len,
|
|
40
|
+
max_ratio=max_ratio)
|
|
41
|
+
self.final_metric = RecMetric(main_indicator=main_indicator,
|
|
42
|
+
is_filter=is_filter,
|
|
43
|
+
ignore_space=ignore_space,
|
|
44
|
+
stream=stream,
|
|
45
|
+
with_ratio=with_ratio,
|
|
46
|
+
max_len=max_len,
|
|
47
|
+
max_ratio=max_ratio)
|
|
48
|
+
|
|
49
|
+
def __call__(self,
|
|
50
|
+
pred_label,
|
|
51
|
+
batch=None,
|
|
52
|
+
training=False,
|
|
53
|
+
*args,
|
|
54
|
+
**kwargs):
|
|
55
|
+
|
|
56
|
+
char_metric = self.char_metric((pred_label[0], pred_label[-1]),
|
|
57
|
+
batch,
|
|
58
|
+
training=training)
|
|
59
|
+
bpe_metric = self.bpe_metric((pred_label[1], pred_label[-1]),
|
|
60
|
+
batch,
|
|
61
|
+
training=training)
|
|
62
|
+
wp_metric = self.wp_metric((pred_label[2], pred_label[-1]),
|
|
63
|
+
batch,
|
|
64
|
+
training=training)
|
|
65
|
+
final_metric = self.final_metric((pred_label[3], pred_label[-1]),
|
|
66
|
+
batch,
|
|
67
|
+
training=training)
|
|
68
|
+
final_metric['char_acc'] = char_metric['acc']
|
|
69
|
+
final_metric['char_norm_edit_dis'] = char_metric['norm_edit_dis']
|
|
70
|
+
final_metric['bpe_acc'] = bpe_metric['acc']
|
|
71
|
+
final_metric['bpe_norm_edit_dis'] = bpe_metric['norm_edit_dis']
|
|
72
|
+
final_metric['wp_acc'] = wp_metric['acc']
|
|
73
|
+
final_metric['wp_norm_edit_dis'] = wp_metric['norm_edit_dis']
|
|
74
|
+
return final_metric
|
|
75
|
+
|
|
76
|
+
def get_metric(self):
|
|
77
|
+
"""
|
|
78
|
+
return metrics {
|
|
79
|
+
'acc': 0,
|
|
80
|
+
'norm_edit_dis': 0,
|
|
81
|
+
}
|
|
82
|
+
"""
|
|
83
|
+
char_metric = self.char_metric.get_metric()
|
|
84
|
+
bpe_metric = self.bpe_metric.get_metric()
|
|
85
|
+
wp_metric = self.wp_metric.get_metric()
|
|
86
|
+
final_metric = self.final_metric.get_metric()
|
|
87
|
+
final_metric['char_acc'] = char_metric['acc']
|
|
88
|
+
final_metric['char_norm_edit_dis'] = char_metric['norm_edit_dis']
|
|
89
|
+
final_metric['bpe_acc'] = bpe_metric['acc']
|
|
90
|
+
final_metric['bpe_norm_edit_dis'] = bpe_metric['norm_edit_dis']
|
|
91
|
+
final_metric['wp_acc'] = wp_metric['acc']
|
|
92
|
+
final_metric['wp_norm_edit_dis'] = wp_metric['norm_edit_dis']
|
|
93
|
+
return final_metric
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from torch import nn
|
|
3
|
+
|
|
4
|
+
from openrec.modeling.decoders import build_decoder
|
|
5
|
+
from openrec.modeling.encoders import build_encoder
|
|
6
|
+
from openrec.modeling.transforms import build_transform
|
|
7
|
+
|
|
8
|
+
__all__ = ['BaseRecognizer']
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class BaseRecognizer(nn.Module):
|
|
12
|
+
|
|
13
|
+
def __init__(self, config):
|
|
14
|
+
"""the module for OCR.
|
|
15
|
+
|
|
16
|
+
args:
|
|
17
|
+
config (dict): the super parameters for module.
|
|
18
|
+
"""
|
|
19
|
+
super(BaseRecognizer, self).__init__()
|
|
20
|
+
in_channels = config.get('in_channels', 3)
|
|
21
|
+
self.use_wd = config.get('use_wd', True)
|
|
22
|
+
# build transfrom,
|
|
23
|
+
# for rec, transfrom can be TPS,None
|
|
24
|
+
if 'Transform' not in config or config['Transform'] is None:
|
|
25
|
+
self.use_transform = False
|
|
26
|
+
else:
|
|
27
|
+
self.use_transform = True
|
|
28
|
+
config['Transform']['in_channels'] = in_channels
|
|
29
|
+
self.transform = build_transform(config['Transform'])
|
|
30
|
+
in_channels = self.transform.out_channels
|
|
31
|
+
|
|
32
|
+
# build backbone
|
|
33
|
+
if 'Encoder' not in config or config['Encoder'] is None:
|
|
34
|
+
self.use_encoder = False
|
|
35
|
+
else:
|
|
36
|
+
self.use_encoder = True
|
|
37
|
+
config['Encoder']['in_channels'] = in_channels
|
|
38
|
+
self.encoder = build_encoder(config['Encoder'])
|
|
39
|
+
in_channels = self.encoder.out_channels
|
|
40
|
+
|
|
41
|
+
# build decoder
|
|
42
|
+
if 'Decoder' not in config or config['Decoder'] is None:
|
|
43
|
+
self.use_decoder = False
|
|
44
|
+
else:
|
|
45
|
+
self.use_decoder = True
|
|
46
|
+
config['Decoder']['in_channels'] = in_channels
|
|
47
|
+
self.decoder = build_decoder(config['Decoder'])
|
|
48
|
+
|
|
49
|
+
@torch.jit.ignore
|
|
50
|
+
def no_weight_decay(self):
|
|
51
|
+
if self.use_wd:
|
|
52
|
+
if hasattr(self.encoder, 'no_weight_decay'):
|
|
53
|
+
no_weight_decay = self.encoder.no_weight_decay()
|
|
54
|
+
else:
|
|
55
|
+
no_weight_decay = {}
|
|
56
|
+
if hasattr(self.decoder, 'no_weight_decay'):
|
|
57
|
+
no_weight_decay.update(self.decoder.no_weight_decay())
|
|
58
|
+
return no_weight_decay
|
|
59
|
+
else:
|
|
60
|
+
return {}
|
|
61
|
+
|
|
62
|
+
def forward(self, x, data=None):
|
|
63
|
+
if self.use_transform:
|
|
64
|
+
x = self.transform(x)
|
|
65
|
+
if self.use_encoder:
|
|
66
|
+
x = self.encoder(x)
|
|
67
|
+
if self.use_decoder:
|
|
68
|
+
x = self.decoder(x, data=data)
|
|
69
|
+
return x
|