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,338 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This code is refer from:
|
|
3
|
+
https://github.com/THU-MIG/RepViT
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import torch.nn as nn
|
|
7
|
+
import torch
|
|
8
|
+
from torch.nn.init import constant_
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _make_divisible(v, divisor, min_value=None):
|
|
12
|
+
"""
|
|
13
|
+
This function is taken from the original tf repo.
|
|
14
|
+
It ensures that all layers have a channel number that is divisible by 8
|
|
15
|
+
It can be seen here:
|
|
16
|
+
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
|
|
17
|
+
:param v:
|
|
18
|
+
:param divisor:
|
|
19
|
+
:param min_value:
|
|
20
|
+
:return:
|
|
21
|
+
"""
|
|
22
|
+
if min_value is None:
|
|
23
|
+
min_value = divisor
|
|
24
|
+
new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
|
|
25
|
+
# Make sure that round down does not go down by more than 10%.
|
|
26
|
+
if new_v < 0.9 * v:
|
|
27
|
+
new_v += divisor
|
|
28
|
+
return new_v
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def make_divisible(v, divisor=8, min_value=None, round_limit=0.9):
|
|
32
|
+
min_value = min_value or divisor
|
|
33
|
+
new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
|
|
34
|
+
# Make sure that round down does not go down by more than 10%.
|
|
35
|
+
if new_v < round_limit * v:
|
|
36
|
+
new_v += divisor
|
|
37
|
+
return new_v
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class SEModule(nn.Module):
|
|
41
|
+
"""SE Module as defined in original SE-Nets with a few additions
|
|
42
|
+
Additions include:
|
|
43
|
+
* divisor can be specified to keep channels % div == 0 (default: 8)
|
|
44
|
+
* reduction channels can be specified directly by arg (if rd_channels is set)
|
|
45
|
+
* reduction channels can be specified by float rd_ratio (default: 1/16)
|
|
46
|
+
* global max pooling can be added to the squeeze aggregation
|
|
47
|
+
* customizable activation, normalization, and gate layer
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
def __init__(
|
|
51
|
+
self,
|
|
52
|
+
channels,
|
|
53
|
+
rd_ratio=1.0 / 16,
|
|
54
|
+
rd_channels=None,
|
|
55
|
+
rd_divisor=8,
|
|
56
|
+
act_layer=nn.ReLU,
|
|
57
|
+
):
|
|
58
|
+
super(SEModule, self).__init__()
|
|
59
|
+
if not rd_channels:
|
|
60
|
+
rd_channels = make_divisible(channels * rd_ratio,
|
|
61
|
+
rd_divisor,
|
|
62
|
+
round_limit=0.0)
|
|
63
|
+
self.fc1 = nn.Conv2d(channels, rd_channels, kernel_size=1, bias=True)
|
|
64
|
+
self.act = act_layer()
|
|
65
|
+
self.fc2 = nn.Conv2d(rd_channels, channels, kernel_size=1, bias=True)
|
|
66
|
+
|
|
67
|
+
def forward(self, x):
|
|
68
|
+
x_se = x.mean((2, 3), keepdim=True)
|
|
69
|
+
x_se = self.fc1(x_se)
|
|
70
|
+
x_se = self.act(x_se)
|
|
71
|
+
x_se = self.fc2(x_se)
|
|
72
|
+
return x * torch.sigmoid(x_se)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class Conv2D_BN(nn.Sequential):
|
|
76
|
+
|
|
77
|
+
def __init__(
|
|
78
|
+
self,
|
|
79
|
+
a,
|
|
80
|
+
b,
|
|
81
|
+
ks=1,
|
|
82
|
+
stride=1,
|
|
83
|
+
pad=0,
|
|
84
|
+
dilation=1,
|
|
85
|
+
groups=1,
|
|
86
|
+
bn_weight_init=1,
|
|
87
|
+
resolution=-10000,
|
|
88
|
+
):
|
|
89
|
+
super().__init__()
|
|
90
|
+
self.add_module(
|
|
91
|
+
'c', nn.Conv2d(a, b, ks, stride, pad, dilation, groups,
|
|
92
|
+
bias=False))
|
|
93
|
+
self.add_module('bn', nn.BatchNorm2d(b))
|
|
94
|
+
constant_(self.bn.weight, bn_weight_init)
|
|
95
|
+
constant_(self.bn.bias, 0)
|
|
96
|
+
|
|
97
|
+
@torch.no_grad()
|
|
98
|
+
def fuse(self):
|
|
99
|
+
c, bn = self._modules.values()
|
|
100
|
+
w = bn.weight / (bn.running_var + bn.eps)**0.5
|
|
101
|
+
w = c.weight * w[:, None, None, None]
|
|
102
|
+
b = bn.bias - bn.running_mean * bn.weight / \
|
|
103
|
+
(bn.running_var + bn.eps)**0.5
|
|
104
|
+
m = nn.Conv2d(w.size(1) * self.c.groups,
|
|
105
|
+
w.size(0),
|
|
106
|
+
w.shape[2:],
|
|
107
|
+
stride=self.c.stride,
|
|
108
|
+
padding=self.c.padding,
|
|
109
|
+
dilation=self.c.dilation,
|
|
110
|
+
groups=self.c.groups,
|
|
111
|
+
device=c.weight.device)
|
|
112
|
+
m.weight.data.copy_(w)
|
|
113
|
+
m.bias.data.copy_(b)
|
|
114
|
+
return m
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class Residual(torch.nn.Module):
|
|
118
|
+
|
|
119
|
+
def __init__(self, m, drop=0.):
|
|
120
|
+
super().__init__()
|
|
121
|
+
self.m = m
|
|
122
|
+
self.drop = drop
|
|
123
|
+
|
|
124
|
+
def forward(self, x):
|
|
125
|
+
if self.training and self.drop > 0:
|
|
126
|
+
return x + self.m(x) * torch.rand(
|
|
127
|
+
x.size(0), 1, 1, 1, device=x.device).ge_(
|
|
128
|
+
self.drop).div(1 - self.drop).detach()
|
|
129
|
+
else:
|
|
130
|
+
return x + self.m(x)
|
|
131
|
+
|
|
132
|
+
@torch.no_grad()
|
|
133
|
+
def fuse(self):
|
|
134
|
+
if isinstance(self.m, Conv2D_BN):
|
|
135
|
+
m = self.m.fuse()
|
|
136
|
+
assert (m.groups == m.in_channels)
|
|
137
|
+
identity = torch.ones(m.weight.shape[0], m.weight.shape[1], 1, 1)
|
|
138
|
+
identity = nn.functional.pad(identity, [1, 1, 1, 1])
|
|
139
|
+
m.weight += identity.to(m.weight.device)
|
|
140
|
+
return m
|
|
141
|
+
elif isinstance(self.m, nn.Conv2d):
|
|
142
|
+
m = self.m
|
|
143
|
+
assert (m.groups != m.in_channels)
|
|
144
|
+
identity = torch.ones(m.weight.shape[0], m.weight.shape[1], 1, 1)
|
|
145
|
+
identity = nn.functional.pad(identity, [1, 1, 1, 1])
|
|
146
|
+
m.weight += identity.to(m.weight.device)
|
|
147
|
+
return m
|
|
148
|
+
else:
|
|
149
|
+
return self
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
class RepVGGDW(nn.Module):
|
|
153
|
+
|
|
154
|
+
def __init__(self, ed) -> None:
|
|
155
|
+
super().__init__()
|
|
156
|
+
self.conv = Conv2D_BN(ed, ed, 3, 1, 1, groups=ed)
|
|
157
|
+
self.conv1 = nn.Conv2d(ed, ed, 1, 1, 0, groups=ed)
|
|
158
|
+
self.dim = ed
|
|
159
|
+
self.bn = nn.BatchNorm2d(ed)
|
|
160
|
+
|
|
161
|
+
def forward(self, x):
|
|
162
|
+
return self.bn((self.conv(x) + self.conv1(x)) + x)
|
|
163
|
+
|
|
164
|
+
@torch.no_grad()
|
|
165
|
+
def fuse(self):
|
|
166
|
+
conv = self.conv.fuse()
|
|
167
|
+
conv1 = self.conv1
|
|
168
|
+
|
|
169
|
+
conv_w = conv.weight
|
|
170
|
+
conv_b = conv.bias
|
|
171
|
+
conv1_w = conv1.weight
|
|
172
|
+
conv1_b = conv1.bias
|
|
173
|
+
|
|
174
|
+
conv1_w = nn.functional.pad(conv1_w, [1, 1, 1, 1])
|
|
175
|
+
|
|
176
|
+
identity = nn.functional.pad(
|
|
177
|
+
torch.ones(conv1_w.shape[0],
|
|
178
|
+
conv1_w.shape[1],
|
|
179
|
+
1,
|
|
180
|
+
1,
|
|
181
|
+
device=conv1_w.device), [1, 1, 1, 1])
|
|
182
|
+
|
|
183
|
+
final_conv_w = conv_w + conv1_w + identity
|
|
184
|
+
final_conv_b = conv_b + conv1_b
|
|
185
|
+
|
|
186
|
+
conv.weight.data.copy_(final_conv_w)
|
|
187
|
+
conv.bias.data.copy_(final_conv_b)
|
|
188
|
+
|
|
189
|
+
bn = self.bn
|
|
190
|
+
w = bn.weight / (bn.running_var + bn.eps)**0.5
|
|
191
|
+
w = conv.weight * w[:, None, None, None]
|
|
192
|
+
b = bn.bias + (conv.bias - bn.running_mean) * bn.weight / \
|
|
193
|
+
(bn.running_var + bn.eps)**0.5
|
|
194
|
+
conv.weight.data.copy_(w)
|
|
195
|
+
conv.bias.data.copy_(b)
|
|
196
|
+
return conv
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class RepViTBlock(nn.Module):
|
|
200
|
+
|
|
201
|
+
def __init__(self, inp, hidden_dim, oup, kernel_size, stride, use_se,
|
|
202
|
+
use_hs):
|
|
203
|
+
super(RepViTBlock, self).__init__()
|
|
204
|
+
|
|
205
|
+
self.identity = stride == 1 and inp == oup
|
|
206
|
+
assert hidden_dim == 2 * inp
|
|
207
|
+
|
|
208
|
+
if stride != 1:
|
|
209
|
+
self.token_mixer = nn.Sequential(
|
|
210
|
+
Conv2D_BN(inp,
|
|
211
|
+
inp,
|
|
212
|
+
kernel_size,
|
|
213
|
+
stride, (kernel_size - 1) // 2,
|
|
214
|
+
groups=inp),
|
|
215
|
+
SEModule(inp, 0.25) if use_se else nn.Identity(),
|
|
216
|
+
Conv2D_BN(inp, oup, ks=1, stride=1, pad=0),
|
|
217
|
+
)
|
|
218
|
+
self.channel_mixer = Residual(
|
|
219
|
+
nn.Sequential(
|
|
220
|
+
# pw
|
|
221
|
+
Conv2D_BN(oup, 2 * oup, 1, 1, 0),
|
|
222
|
+
nn.GELU() if use_hs else nn.GELU(),
|
|
223
|
+
# pw-linear
|
|
224
|
+
Conv2D_BN(2 * oup, oup, 1, 1, 0, bn_weight_init=0),
|
|
225
|
+
))
|
|
226
|
+
else:
|
|
227
|
+
assert self.identity
|
|
228
|
+
self.token_mixer = nn.Sequential(
|
|
229
|
+
RepVGGDW(inp),
|
|
230
|
+
SEModule(inp, 0.25) if use_se else nn.Identity(),
|
|
231
|
+
)
|
|
232
|
+
self.channel_mixer = Residual(
|
|
233
|
+
nn.Sequential(
|
|
234
|
+
# pw
|
|
235
|
+
Conv2D_BN(inp, hidden_dim, 1, 1, 0),
|
|
236
|
+
nn.GELU() if use_hs else nn.GELU(),
|
|
237
|
+
# pw-linear
|
|
238
|
+
Conv2D_BN(hidden_dim, oup, 1, 1, 0, bn_weight_init=0),
|
|
239
|
+
))
|
|
240
|
+
|
|
241
|
+
def forward(self, x):
|
|
242
|
+
return self.channel_mixer(self.token_mixer(x))
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
class RepViT(nn.Module):
|
|
246
|
+
|
|
247
|
+
def __init__(self, cfgs, in_channels=3, out_indices=None):
|
|
248
|
+
super(RepViT, self).__init__()
|
|
249
|
+
# setting of inverted residual blocks
|
|
250
|
+
self.cfgs = cfgs
|
|
251
|
+
|
|
252
|
+
# building first layer
|
|
253
|
+
input_channel = self.cfgs[0][2]
|
|
254
|
+
patch_embed = nn.Sequential(
|
|
255
|
+
Conv2D_BN(in_channels, input_channel // 2, 3, 2, 1),
|
|
256
|
+
nn.GELU(),
|
|
257
|
+
Conv2D_BN(input_channel // 2, input_channel, 3, 2, 1),
|
|
258
|
+
)
|
|
259
|
+
layers = [patch_embed]
|
|
260
|
+
# building inverted residual blocks
|
|
261
|
+
block = RepViTBlock
|
|
262
|
+
for k, t, c, use_se, use_hs, s in self.cfgs:
|
|
263
|
+
output_channel = _make_divisible(c, 8)
|
|
264
|
+
exp_size = _make_divisible(input_channel * t, 8)
|
|
265
|
+
layers.append(
|
|
266
|
+
block(input_channel, exp_size, output_channel, k, s, use_se,
|
|
267
|
+
use_hs))
|
|
268
|
+
input_channel = output_channel
|
|
269
|
+
self.features = nn.ModuleList(layers)
|
|
270
|
+
self.out_indices = out_indices
|
|
271
|
+
if out_indices is not None:
|
|
272
|
+
self.out_channels = [self.cfgs[ids - 1][2] for ids in out_indices]
|
|
273
|
+
else:
|
|
274
|
+
self.out_channels = self.cfgs[-1][2]
|
|
275
|
+
|
|
276
|
+
def forward(self, x):
|
|
277
|
+
if self.out_indices is not None:
|
|
278
|
+
return self.forward_det(x)
|
|
279
|
+
return self.forward_rec(x)
|
|
280
|
+
|
|
281
|
+
def forward_det(self, x):
|
|
282
|
+
outs = []
|
|
283
|
+
for i, f in enumerate(self.features):
|
|
284
|
+
x = f(x)
|
|
285
|
+
if i in self.out_indices:
|
|
286
|
+
outs.append(x)
|
|
287
|
+
return outs
|
|
288
|
+
|
|
289
|
+
def forward_rec(self, x):
|
|
290
|
+
for f in self.features:
|
|
291
|
+
x = f(x)
|
|
292
|
+
return x
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def RepSVTREncoder(in_channels=3):
|
|
296
|
+
"""
|
|
297
|
+
Constructs a MobileNetV3-Large model
|
|
298
|
+
"""
|
|
299
|
+
# k, t, c, SE, HS, s
|
|
300
|
+
cfgs = [
|
|
301
|
+
[3, 2, 96, 1, 0, 1],
|
|
302
|
+
[3, 2, 96, 0, 0, 1],
|
|
303
|
+
[3, 2, 96, 0, 0, 1],
|
|
304
|
+
[3, 2, 192, 0, 1, (2, 1)],
|
|
305
|
+
[3, 2, 192, 1, 1, 1],
|
|
306
|
+
[3, 2, 192, 0, 1, 1],
|
|
307
|
+
[3, 2, 192, 1, 1, 1],
|
|
308
|
+
[3, 2, 192, 0, 1, 1],
|
|
309
|
+
[3, 2, 192, 1, 1, 1],
|
|
310
|
+
[3, 2, 192, 0, 1, 1],
|
|
311
|
+
[3, 2, 384, 0, 1, (2, 1)],
|
|
312
|
+
[3, 2, 384, 1, 1, 1],
|
|
313
|
+
[3, 2, 384, 0, 1, 1],
|
|
314
|
+
]
|
|
315
|
+
return RepViT(cfgs, in_channels=in_channels)
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def RepSVTR_det(in_channels=3, out_indices=[2, 5, 10, 13]):
|
|
319
|
+
"""
|
|
320
|
+
Constructs a MobileNetV3-Large model
|
|
321
|
+
"""
|
|
322
|
+
# k, t, c, SE, HS, s
|
|
323
|
+
cfgs = [
|
|
324
|
+
[3, 2, 48, 1, 0, 1],
|
|
325
|
+
[3, 2, 48, 0, 0, 1],
|
|
326
|
+
[3, 2, 96, 0, 0, 2],
|
|
327
|
+
[3, 2, 96, 1, 0, 1],
|
|
328
|
+
[3, 2, 96, 0, 0, 1],
|
|
329
|
+
[3, 2, 192, 0, 1, 2],
|
|
330
|
+
[3, 2, 192, 1, 1, 1],
|
|
331
|
+
[3, 2, 192, 0, 1, 1],
|
|
332
|
+
[3, 2, 192, 1, 1, 1],
|
|
333
|
+
[3, 2, 192, 0, 1, 1],
|
|
334
|
+
[3, 2, 384, 0, 1, 2],
|
|
335
|
+
[3, 2, 384, 1, 1, 1],
|
|
336
|
+
[3, 2, 384, 0, 1, 1],
|
|
337
|
+
]
|
|
338
|
+
return RepViT(cfgs, in_channels=in_channels, out_indices=out_indices)
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import torch.nn as nn
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def conv3x3(in_planes, out_planes, stride=1):
|
|
5
|
+
"""3x3 convolution with padding."""
|
|
6
|
+
return nn.Conv2d(in_planes,
|
|
7
|
+
out_planes,
|
|
8
|
+
kernel_size=3,
|
|
9
|
+
stride=stride,
|
|
10
|
+
padding=1,
|
|
11
|
+
bias=False)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def conv1x1(in_planes, out_planes, stride=1):
|
|
15
|
+
"""1x1 convolution."""
|
|
16
|
+
return nn.Conv2d(in_planes,
|
|
17
|
+
out_planes,
|
|
18
|
+
kernel_size=1,
|
|
19
|
+
stride=stride,
|
|
20
|
+
bias=False)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class AsterBlock(nn.Module):
|
|
24
|
+
|
|
25
|
+
def __init__(self, inplanes, planes, stride=1, downsample=None):
|
|
26
|
+
super(AsterBlock, self).__init__()
|
|
27
|
+
self.conv1 = conv1x1(inplanes, planes, stride)
|
|
28
|
+
self.bn1 = nn.BatchNorm2d(planes)
|
|
29
|
+
self.relu = nn.ReLU(inplace=True)
|
|
30
|
+
self.conv2 = conv3x3(planes, planes)
|
|
31
|
+
self.bn2 = nn.BatchNorm2d(planes)
|
|
32
|
+
self.downsample = downsample
|
|
33
|
+
self.stride = stride
|
|
34
|
+
|
|
35
|
+
def forward(self, x):
|
|
36
|
+
residual = x
|
|
37
|
+
out = self.conv1(x)
|
|
38
|
+
out = self.bn1(out)
|
|
39
|
+
out = self.relu(out)
|
|
40
|
+
out = self.conv2(out)
|
|
41
|
+
out = self.bn2(out)
|
|
42
|
+
|
|
43
|
+
if self.downsample is not None:
|
|
44
|
+
residual = self.downsample(x)
|
|
45
|
+
out += residual
|
|
46
|
+
out = self.relu(out)
|
|
47
|
+
return out
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class ResNet_ASTER(nn.Module):
|
|
51
|
+
"""For aster or crnn."""
|
|
52
|
+
|
|
53
|
+
def __init__(self, in_channels, with_lstm=True, n_group=1):
|
|
54
|
+
super(ResNet_ASTER, self).__init__()
|
|
55
|
+
self.with_lstm = with_lstm
|
|
56
|
+
self.n_group = n_group
|
|
57
|
+
|
|
58
|
+
self.out_channels = 512
|
|
59
|
+
if with_lstm:
|
|
60
|
+
self.out_channels = 512
|
|
61
|
+
|
|
62
|
+
self.layer0 = nn.Sequential(
|
|
63
|
+
nn.Conv2d(in_channels,
|
|
64
|
+
32,
|
|
65
|
+
kernel_size=(3, 3),
|
|
66
|
+
stride=1,
|
|
67
|
+
padding=1,
|
|
68
|
+
bias=False), nn.BatchNorm2d(32), nn.ReLU(inplace=True))
|
|
69
|
+
|
|
70
|
+
self.inplanes = 32
|
|
71
|
+
self.layer1 = self._make_layer(32, 3, [2, 2]) # [16, 50]
|
|
72
|
+
self.layer2 = self._make_layer(64, 4, [2, 2]) # [8, 25]
|
|
73
|
+
self.layer3 = self._make_layer(128, 6, [2, 1]) # [4, 25]
|
|
74
|
+
self.layer4 = self._make_layer(256, 6, [2, 1]) # [2, 25]
|
|
75
|
+
self.layer5 = self._make_layer(512, 3, [2, 1]) # [1, 25]
|
|
76
|
+
|
|
77
|
+
if with_lstm:
|
|
78
|
+
self.rnn = nn.LSTM(512,
|
|
79
|
+
256,
|
|
80
|
+
bidirectional=True,
|
|
81
|
+
num_layers=2,
|
|
82
|
+
batch_first=True)
|
|
83
|
+
self.out_planes = 2 * 256
|
|
84
|
+
else:
|
|
85
|
+
self.out_planes = 512
|
|
86
|
+
|
|
87
|
+
for m in self.modules():
|
|
88
|
+
if isinstance(m, nn.Conv2d):
|
|
89
|
+
nn.init.kaiming_normal_(m.weight,
|
|
90
|
+
mode='fan_out',
|
|
91
|
+
nonlinearity='relu')
|
|
92
|
+
elif isinstance(m, nn.BatchNorm2d):
|
|
93
|
+
nn.init.constant_(m.weight, 1)
|
|
94
|
+
nn.init.constant_(m.bias, 0)
|
|
95
|
+
|
|
96
|
+
def _make_layer(self, planes, blocks, stride):
|
|
97
|
+
downsample = None
|
|
98
|
+
if stride != [1, 1] or self.inplanes != planes:
|
|
99
|
+
downsample = nn.Sequential(conv1x1(self.inplanes, planes, stride),
|
|
100
|
+
nn.BatchNorm2d(planes))
|
|
101
|
+
|
|
102
|
+
layers = []
|
|
103
|
+
layers.append(AsterBlock(self.inplanes, planes, stride, downsample))
|
|
104
|
+
self.inplanes = planes
|
|
105
|
+
for _ in range(1, blocks):
|
|
106
|
+
layers.append(AsterBlock(self.inplanes, planes))
|
|
107
|
+
return nn.Sequential(*layers)
|
|
108
|
+
|
|
109
|
+
def forward(self, x):
|
|
110
|
+
x0 = self.layer0(x)
|
|
111
|
+
x1 = self.layer1(x0)
|
|
112
|
+
x2 = self.layer2(x1)
|
|
113
|
+
x3 = self.layer3(x2)
|
|
114
|
+
x4 = self.layer4(x3)
|
|
115
|
+
x5 = self.layer5(x4)
|
|
116
|
+
|
|
117
|
+
cnn_feat = x5.squeeze(2) # [N, c, w]
|
|
118
|
+
cnn_feat = cnn_feat.transpose(2, 1).contiguous()
|
|
119
|
+
if self.with_lstm:
|
|
120
|
+
rnn_feat, _ = self.rnn(cnn_feat)
|
|
121
|
+
return rnn_feat
|
|
122
|
+
else:
|
|
123
|
+
return cnn_feat
|