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,263 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import math
|
|
3
|
+
import os
|
|
4
|
+
import random
|
|
5
|
+
import traceback
|
|
6
|
+
|
|
7
|
+
import cv2
|
|
8
|
+
import numpy as np
|
|
9
|
+
from torch.utils.data import Dataset
|
|
10
|
+
|
|
11
|
+
from openrec.preprocess import create_operators, transform
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class SimpleDataSet(Dataset):
|
|
15
|
+
|
|
16
|
+
def __init__(self, config, mode, logger, seed=None, epoch=0):
|
|
17
|
+
super(SimpleDataSet, self).__init__()
|
|
18
|
+
self.logger = logger
|
|
19
|
+
self.mode = mode.lower()
|
|
20
|
+
|
|
21
|
+
global_config = config['Global']
|
|
22
|
+
dataset_config = config[mode]['dataset']
|
|
23
|
+
loader_config = config[mode]['loader']
|
|
24
|
+
|
|
25
|
+
self.delimiter = dataset_config.get('delimiter', '\t')
|
|
26
|
+
label_file_list = dataset_config.pop('label_file_list')
|
|
27
|
+
data_source_num = len(label_file_list)
|
|
28
|
+
ratio_list = dataset_config.get('ratio_list', 1.0)
|
|
29
|
+
if isinstance(ratio_list, (float, int)):
|
|
30
|
+
ratio_list = [float(ratio_list)] * int(data_source_num)
|
|
31
|
+
|
|
32
|
+
assert len(
|
|
33
|
+
ratio_list
|
|
34
|
+
) == data_source_num, 'The length of ratio_list should be the same as the file_list.'
|
|
35
|
+
self.data_dir = dataset_config['data_dir']
|
|
36
|
+
self.do_shuffle = loader_config['shuffle']
|
|
37
|
+
self.seed = seed
|
|
38
|
+
logger.info(f'Initialize indexs of datasets: {label_file_list}')
|
|
39
|
+
self.data_lines = self.get_image_info_list(label_file_list, ratio_list)
|
|
40
|
+
self.data_idx_order_list = list(range(len(self.data_lines)))
|
|
41
|
+
if self.mode == 'train' and self.do_shuffle:
|
|
42
|
+
self.shuffle_data_random()
|
|
43
|
+
|
|
44
|
+
self.set_epoch_as_seed(self.seed, dataset_config)
|
|
45
|
+
|
|
46
|
+
self.ops = create_operators(dataset_config['transforms'],
|
|
47
|
+
global_config)
|
|
48
|
+
self.ext_op_transform_idx = dataset_config.get('ext_op_transform_idx',
|
|
49
|
+
2)
|
|
50
|
+
self.need_reset = True in [x < 1 for x in ratio_list]
|
|
51
|
+
|
|
52
|
+
def set_epoch_as_seed(self, seed, dataset_config):
|
|
53
|
+
if self.mode == 'train':
|
|
54
|
+
try:
|
|
55
|
+
border_map_id = [
|
|
56
|
+
index for index, dictionary in enumerate(
|
|
57
|
+
dataset_config['transforms'])
|
|
58
|
+
if 'MakeBorderMap' in dictionary
|
|
59
|
+
][0]
|
|
60
|
+
shrink_map_id = [
|
|
61
|
+
index for index, dictionary in enumerate(
|
|
62
|
+
dataset_config['transforms'])
|
|
63
|
+
if 'MakeShrinkMap' in dictionary
|
|
64
|
+
][0]
|
|
65
|
+
dataset_config['transforms'][border_map_id]['MakeBorderMap'][
|
|
66
|
+
'epoch'] = seed if seed is not None else 0
|
|
67
|
+
dataset_config['transforms'][shrink_map_id]['MakeShrinkMap'][
|
|
68
|
+
'epoch'] = seed if seed is not None else 0
|
|
69
|
+
except Exception:
|
|
70
|
+
return
|
|
71
|
+
|
|
72
|
+
def get_image_info_list(self, file_list, ratio_list):
|
|
73
|
+
if isinstance(file_list, str):
|
|
74
|
+
file_list = [file_list]
|
|
75
|
+
data_lines = []
|
|
76
|
+
for idx, file in enumerate(file_list):
|
|
77
|
+
with open(file, 'rb') as f:
|
|
78
|
+
lines = f.readlines()
|
|
79
|
+
if self.mode == 'train' or ratio_list[idx] < 1.0:
|
|
80
|
+
random.seed(self.seed)
|
|
81
|
+
lines = random.sample(lines,
|
|
82
|
+
round(len(lines) * ratio_list[idx]))
|
|
83
|
+
data_lines.extend(lines)
|
|
84
|
+
return data_lines
|
|
85
|
+
|
|
86
|
+
def shuffle_data_random(self):
|
|
87
|
+
random.seed(self.seed)
|
|
88
|
+
random.shuffle(self.data_lines)
|
|
89
|
+
return
|
|
90
|
+
|
|
91
|
+
def _try_parse_filename_list(self, file_name):
|
|
92
|
+
# multiple images -> one gt label
|
|
93
|
+
if len(file_name) > 0 and file_name[0] == '[':
|
|
94
|
+
try:
|
|
95
|
+
info = json.loads(file_name)
|
|
96
|
+
file_name = random.choice(info)
|
|
97
|
+
except:
|
|
98
|
+
pass
|
|
99
|
+
return file_name
|
|
100
|
+
|
|
101
|
+
def get_ext_data(self):
|
|
102
|
+
ext_data_num = 0
|
|
103
|
+
for op in self.ops:
|
|
104
|
+
if hasattr(op, 'ext_data_num'):
|
|
105
|
+
ext_data_num = getattr(op, 'ext_data_num')
|
|
106
|
+
break
|
|
107
|
+
load_data_ops = self.ops[:self.ext_op_transform_idx]
|
|
108
|
+
ext_data = []
|
|
109
|
+
|
|
110
|
+
while len(ext_data) < ext_data_num:
|
|
111
|
+
file_idx = self.data_idx_order_list[np.random.randint(
|
|
112
|
+
self.__len__())]
|
|
113
|
+
data_line = self.data_lines[file_idx]
|
|
114
|
+
data_line = data_line.decode('utf-8')
|
|
115
|
+
substr = data_line.strip('\n').split(self.delimiter)
|
|
116
|
+
file_name = substr[0]
|
|
117
|
+
file_name = self._try_parse_filename_list(file_name)
|
|
118
|
+
label = substr[1]
|
|
119
|
+
img_path = os.path.join(self.data_dir, file_name)
|
|
120
|
+
data = {'img_path': img_path, 'label': label}
|
|
121
|
+
if not os.path.exists(img_path):
|
|
122
|
+
continue
|
|
123
|
+
with open(data['img_path'], 'rb') as f:
|
|
124
|
+
img = f.read()
|
|
125
|
+
data['image'] = img
|
|
126
|
+
data = transform(data, load_data_ops)
|
|
127
|
+
|
|
128
|
+
if data is None:
|
|
129
|
+
continue
|
|
130
|
+
if 'polys' in data.keys():
|
|
131
|
+
if data['polys'].shape[1] != 4:
|
|
132
|
+
continue
|
|
133
|
+
ext_data.append(data)
|
|
134
|
+
return ext_data
|
|
135
|
+
|
|
136
|
+
def __getitem__(self, idx):
|
|
137
|
+
file_idx = self.data_idx_order_list[idx]
|
|
138
|
+
data_line = self.data_lines[file_idx]
|
|
139
|
+
try:
|
|
140
|
+
data_line = data_line.decode('utf-8')
|
|
141
|
+
substr = data_line.strip('\n').split(self.delimiter)
|
|
142
|
+
file_name = substr[0]
|
|
143
|
+
file_name = self._try_parse_filename_list(file_name)
|
|
144
|
+
label = substr[1]
|
|
145
|
+
img_path = os.path.join(self.data_dir, file_name)
|
|
146
|
+
data = {'img_path': img_path, 'label': label}
|
|
147
|
+
|
|
148
|
+
if not os.path.exists(img_path):
|
|
149
|
+
raise Exception('{} does not exist!'.format(img_path))
|
|
150
|
+
with open(data['img_path'], 'rb') as f:
|
|
151
|
+
img = f.read()
|
|
152
|
+
data['image'] = img
|
|
153
|
+
data['ext_data'] = self.get_ext_data()
|
|
154
|
+
outs = transform(data, self.ops)
|
|
155
|
+
except:
|
|
156
|
+
self.logger.error(
|
|
157
|
+
'When parsing line {}, error happened with msg: {}'.format(
|
|
158
|
+
data_line, traceback.format_exc()))
|
|
159
|
+
outs = None
|
|
160
|
+
if outs is None:
|
|
161
|
+
# during evaluation, we should fix the idx to get same results for many times of evaluation.
|
|
162
|
+
rnd_idx = np.random.randint(self.__len__(
|
|
163
|
+
)) if self.mode == 'train' else (idx + 1) % self.__len__()
|
|
164
|
+
return self.__getitem__(rnd_idx)
|
|
165
|
+
return outs
|
|
166
|
+
|
|
167
|
+
def __len__(self):
|
|
168
|
+
return len(self.data_idx_order_list)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
class MultiScaleDataSet(SimpleDataSet):
|
|
172
|
+
|
|
173
|
+
def __init__(self, config, mode, logger, seed=None):
|
|
174
|
+
super(MultiScaleDataSet, self).__init__(config, mode, logger, seed)
|
|
175
|
+
self.ds_width = config[mode]['dataset'].get('ds_width', False)
|
|
176
|
+
if self.ds_width:
|
|
177
|
+
self.wh_aware()
|
|
178
|
+
|
|
179
|
+
def wh_aware(self):
|
|
180
|
+
data_line_new = []
|
|
181
|
+
wh_ratio = []
|
|
182
|
+
for lins in self.data_lines:
|
|
183
|
+
data_line_new.append(lins)
|
|
184
|
+
lins = lins.decode('utf-8')
|
|
185
|
+
name, label, w, h = lins.strip('\n').split(self.delimiter)
|
|
186
|
+
wh_ratio.append(float(w) / float(h))
|
|
187
|
+
|
|
188
|
+
self.data_lines = data_line_new
|
|
189
|
+
self.wh_ratio = np.array(wh_ratio)
|
|
190
|
+
self.wh_ratio_sort = np.argsort(self.wh_ratio)
|
|
191
|
+
self.data_idx_order_list = list(range(len(self.data_lines)))
|
|
192
|
+
|
|
193
|
+
def resize_norm_img(self, data, imgW, imgH, padding=True):
|
|
194
|
+
img = data['image']
|
|
195
|
+
h = img.shape[0]
|
|
196
|
+
w = img.shape[1]
|
|
197
|
+
if not padding:
|
|
198
|
+
resized_image = cv2.resize(img, (imgW, imgH),
|
|
199
|
+
interpolation=cv2.INTER_LINEAR)
|
|
200
|
+
resized_w = imgW
|
|
201
|
+
else:
|
|
202
|
+
ratio = w / float(h)
|
|
203
|
+
if math.ceil(imgH * ratio) > imgW:
|
|
204
|
+
resized_w = imgW
|
|
205
|
+
else:
|
|
206
|
+
resized_w = int(math.ceil(imgH * ratio))
|
|
207
|
+
resized_image = cv2.resize(img, (resized_w, imgH))
|
|
208
|
+
resized_image = resized_image.astype('float32')
|
|
209
|
+
|
|
210
|
+
resized_image = resized_image.transpose((2, 0, 1)) / 255
|
|
211
|
+
resized_image -= 0.5
|
|
212
|
+
resized_image /= 0.5
|
|
213
|
+
padding_im = np.zeros((3, imgH, imgW), dtype=np.float32)
|
|
214
|
+
padding_im[:, :, :resized_w] = resized_image
|
|
215
|
+
valid_ratio = min(1.0, float(resized_w / imgW))
|
|
216
|
+
data['image'] = padding_im
|
|
217
|
+
data['valid_ratio'] = valid_ratio
|
|
218
|
+
return data
|
|
219
|
+
|
|
220
|
+
def __getitem__(self, properties):
|
|
221
|
+
# properites is a tuple, contains (width, height, index)
|
|
222
|
+
img_height = properties[1]
|
|
223
|
+
idx = properties[2]
|
|
224
|
+
if self.ds_width and properties[3] is not None:
|
|
225
|
+
wh_ratio = properties[3]
|
|
226
|
+
img_width = img_height * (1 if int(round(wh_ratio)) == 0 else int(
|
|
227
|
+
round(wh_ratio)))
|
|
228
|
+
file_idx = self.wh_ratio_sort[idx]
|
|
229
|
+
else:
|
|
230
|
+
file_idx = self.data_idx_order_list[idx]
|
|
231
|
+
img_width = properties[0]
|
|
232
|
+
wh_ratio = None
|
|
233
|
+
|
|
234
|
+
data_line = self.data_lines[file_idx]
|
|
235
|
+
try:
|
|
236
|
+
data_line = data_line.decode('utf-8')
|
|
237
|
+
substr = data_line.strip('\n').split(self.delimiter)
|
|
238
|
+
file_name = substr[0]
|
|
239
|
+
file_name = self._try_parse_filename_list(file_name)
|
|
240
|
+
label = substr[1]
|
|
241
|
+
img_path = os.path.join(self.data_dir, file_name)
|
|
242
|
+
data = {'img_path': img_path, 'label': label}
|
|
243
|
+
if not os.path.exists(img_path):
|
|
244
|
+
raise Exception('{} does not exist!'.format(img_path))
|
|
245
|
+
with open(data['img_path'], 'rb') as f:
|
|
246
|
+
img = f.read()
|
|
247
|
+
data['image'] = img
|
|
248
|
+
data['ext_data'] = self.get_ext_data()
|
|
249
|
+
outs = transform(data, self.ops[:-1])
|
|
250
|
+
if outs is not None:
|
|
251
|
+
outs = self.resize_norm_img(outs, img_width, img_height)
|
|
252
|
+
outs = transform(outs, self.ops[-1:])
|
|
253
|
+
except:
|
|
254
|
+
self.logger.error(
|
|
255
|
+
'When parsing line {}, error happened with msg: {}'.format(
|
|
256
|
+
data_line, traceback.format_exc()))
|
|
257
|
+
outs = None
|
|
258
|
+
if outs is None:
|
|
259
|
+
# during evaluation, we should fix the idx to get same results for many times of evaluation.
|
|
260
|
+
rnd_idx = np.random.randint(self.__len__(
|
|
261
|
+
)) if self.mode == 'train' else (idx + 1) % self.__len__()
|
|
262
|
+
return self.__getitem__([img_width, img_height, rnd_idx, wh_ratio])
|
|
263
|
+
return outs
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
import cv2
|
|
4
|
+
import lmdb
|
|
5
|
+
import numpy as np
|
|
6
|
+
from torch.utils.data import Dataset
|
|
7
|
+
|
|
8
|
+
from openrec.preprocess import create_operators, transform
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class STRLMDBDataSet(Dataset):
|
|
12
|
+
|
|
13
|
+
def __init__(self, config, mode, logger, seed=None, epoch=1, gpu_i=0):
|
|
14
|
+
super(STRLMDBDataSet, self).__init__()
|
|
15
|
+
|
|
16
|
+
global_config = config['Global']
|
|
17
|
+
dataset_config = config[mode]['dataset']
|
|
18
|
+
loader_config = config[mode]['loader']
|
|
19
|
+
loader_config['batch_size_per_card']
|
|
20
|
+
# data_dir = dataset_config['data_dir']
|
|
21
|
+
data_dir = '../training_aug_lmdb_noerror/ep' + str(epoch)
|
|
22
|
+
self.do_shuffle = loader_config['shuffle']
|
|
23
|
+
|
|
24
|
+
self.lmdb_sets = self.load_hierarchical_lmdb_dataset(data_dir)
|
|
25
|
+
logger.info('Initialize indexs of datasets:%s' % data_dir)
|
|
26
|
+
self.data_idx_order_list = self.dataset_traversal()
|
|
27
|
+
if self.do_shuffle:
|
|
28
|
+
np.random.shuffle(self.data_idx_order_list)
|
|
29
|
+
self.ops = create_operators(dataset_config['transforms'],
|
|
30
|
+
global_config)
|
|
31
|
+
self.ext_op_transform_idx = dataset_config.get('ext_op_transform_idx',
|
|
32
|
+
1)
|
|
33
|
+
|
|
34
|
+
dataset_config.get('ratio_list', [1.0])
|
|
35
|
+
self.need_reset = True # in [x < 1 for x in ratio_list]
|
|
36
|
+
|
|
37
|
+
def load_hierarchical_lmdb_dataset(self, data_dir):
|
|
38
|
+
lmdb_sets = {}
|
|
39
|
+
dataset_idx = 0
|
|
40
|
+
for dirpath, dirnames, filenames in os.walk(data_dir + '/'):
|
|
41
|
+
if not dirnames:
|
|
42
|
+
env = lmdb.open(
|
|
43
|
+
dirpath,
|
|
44
|
+
max_readers=32,
|
|
45
|
+
readonly=True,
|
|
46
|
+
lock=False,
|
|
47
|
+
readahead=False,
|
|
48
|
+
meminit=False,
|
|
49
|
+
)
|
|
50
|
+
txn = env.begin(write=False)
|
|
51
|
+
num_samples = int(txn.get('num-samples'.encode()))
|
|
52
|
+
lmdb_sets[dataset_idx] = {
|
|
53
|
+
'dirpath': dirpath,
|
|
54
|
+
'env': env,
|
|
55
|
+
'txn': txn,
|
|
56
|
+
'num_samples': num_samples,
|
|
57
|
+
}
|
|
58
|
+
dataset_idx += 1
|
|
59
|
+
return lmdb_sets
|
|
60
|
+
|
|
61
|
+
def dataset_traversal(self):
|
|
62
|
+
lmdb_num = len(self.lmdb_sets)
|
|
63
|
+
total_sample_num = 0
|
|
64
|
+
for lno in range(lmdb_num):
|
|
65
|
+
total_sample_num += self.lmdb_sets[lno]['num_samples']
|
|
66
|
+
data_idx_order_list = np.zeros((total_sample_num, 2))
|
|
67
|
+
beg_idx = 0
|
|
68
|
+
for lno in range(lmdb_num):
|
|
69
|
+
tmp_sample_num = self.lmdb_sets[lno]['num_samples']
|
|
70
|
+
end_idx = beg_idx + tmp_sample_num
|
|
71
|
+
data_idx_order_list[beg_idx:end_idx, 0] = lno
|
|
72
|
+
data_idx_order_list[beg_idx:end_idx,
|
|
73
|
+
1] = list(range(tmp_sample_num))
|
|
74
|
+
data_idx_order_list[beg_idx:end_idx, 1] += 1
|
|
75
|
+
beg_idx = beg_idx + tmp_sample_num
|
|
76
|
+
return data_idx_order_list
|
|
77
|
+
|
|
78
|
+
def get_img_data(self, value):
|
|
79
|
+
"""get_img_data."""
|
|
80
|
+
if not value:
|
|
81
|
+
return None
|
|
82
|
+
imgdata = np.frombuffer(value, dtype='uint8')
|
|
83
|
+
if imgdata is None:
|
|
84
|
+
return None
|
|
85
|
+
imgori = cv2.imdecode(imgdata, 1)
|
|
86
|
+
if imgori is None:
|
|
87
|
+
return None
|
|
88
|
+
return imgori
|
|
89
|
+
|
|
90
|
+
def get_ext_data(self):
|
|
91
|
+
ext_data_num = 0
|
|
92
|
+
for op in self.ops:
|
|
93
|
+
if hasattr(op, 'ext_data_num'):
|
|
94
|
+
ext_data_num = getattr(op, 'ext_data_num')
|
|
95
|
+
break
|
|
96
|
+
load_data_ops = self.ops[:self.ext_op_transform_idx]
|
|
97
|
+
ext_data = []
|
|
98
|
+
|
|
99
|
+
while len(ext_data) < ext_data_num:
|
|
100
|
+
lmdb_idx, file_idx = self.data_idx_order_list[np.random.randint(
|
|
101
|
+
len(self))]
|
|
102
|
+
lmdb_idx = int(lmdb_idx)
|
|
103
|
+
file_idx = int(file_idx)
|
|
104
|
+
sample_info = self.get_lmdb_sample_info(
|
|
105
|
+
self.lmdb_sets[lmdb_idx]['txn'], file_idx)
|
|
106
|
+
if sample_info is None:
|
|
107
|
+
continue
|
|
108
|
+
img, label = sample_info
|
|
109
|
+
data = {'image': img, 'label': label}
|
|
110
|
+
data = transform(data, load_data_ops)
|
|
111
|
+
if data is None:
|
|
112
|
+
continue
|
|
113
|
+
ext_data.append(data)
|
|
114
|
+
return ext_data
|
|
115
|
+
|
|
116
|
+
def get_lmdb_sample_info(self, txn, index):
|
|
117
|
+
label_key = 'label-%09d'.encode() % index
|
|
118
|
+
label = txn.get(label_key)
|
|
119
|
+
if label is None:
|
|
120
|
+
return None
|
|
121
|
+
label = label.decode('utf-8')
|
|
122
|
+
img_key = 'image-%09d'.encode() % index
|
|
123
|
+
imgbuf = txn.get(img_key)
|
|
124
|
+
return imgbuf, label
|
|
125
|
+
|
|
126
|
+
def __getitem__(self, idx):
|
|
127
|
+
lmdb_idx, file_idx = self.data_idx_order_list[idx]
|
|
128
|
+
lmdb_idx = int(lmdb_idx)
|
|
129
|
+
file_idx = int(file_idx)
|
|
130
|
+
sample_info = self.get_lmdb_sample_info(
|
|
131
|
+
self.lmdb_sets[lmdb_idx]['txn'], file_idx)
|
|
132
|
+
if sample_info is None:
|
|
133
|
+
return self.__getitem__(np.random.randint(self.__len__()))
|
|
134
|
+
img, label = sample_info
|
|
135
|
+
data = {'image': img, 'label': label}
|
|
136
|
+
data['ext_data'] = self.get_ext_data()
|
|
137
|
+
outs = transform(data, self.ops)
|
|
138
|
+
if outs is None:
|
|
139
|
+
return self.__getitem__(np.random.randint(self.__len__()))
|
|
140
|
+
return outs
|
|
141
|
+
|
|
142
|
+
def __len__(self):
|
|
143
|
+
return self.data_idx_order_list.shape[0]
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from argparse import ArgumentParser, RawDescriptionHelpFormatter
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
|
|
5
|
+
import yaml
|
|
6
|
+
|
|
7
|
+
__all__ = ['Config']
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ArgsParser(ArgumentParser):
|
|
11
|
+
|
|
12
|
+
def __init__(self):
|
|
13
|
+
super(ArgsParser,
|
|
14
|
+
self).__init__(formatter_class=RawDescriptionHelpFormatter)
|
|
15
|
+
self.add_argument('-o',
|
|
16
|
+
'--opt',
|
|
17
|
+
nargs='*',
|
|
18
|
+
help='set configuration options')
|
|
19
|
+
self.add_argument('--local_rank')
|
|
20
|
+
|
|
21
|
+
def parse_args(self, argv=None):
|
|
22
|
+
args = super(ArgsParser, self).parse_args(argv)
|
|
23
|
+
assert args.config is not None, 'Please specify --config=configure_file_path.'
|
|
24
|
+
args.opt = self._parse_opt(args.opt)
|
|
25
|
+
return args
|
|
26
|
+
|
|
27
|
+
def _parse_opt(self, opts):
|
|
28
|
+
config = {}
|
|
29
|
+
if not opts:
|
|
30
|
+
return config
|
|
31
|
+
for s in opts:
|
|
32
|
+
s = s.strip()
|
|
33
|
+
k, v = s.split('=', 1)
|
|
34
|
+
if '.' not in k:
|
|
35
|
+
config[k] = yaml.load(v, Loader=yaml.Loader)
|
|
36
|
+
else:
|
|
37
|
+
keys = k.split('.')
|
|
38
|
+
if keys[0] not in config:
|
|
39
|
+
config[keys[0]] = {}
|
|
40
|
+
cur = config[keys[0]]
|
|
41
|
+
for idx, key in enumerate(keys[1:]):
|
|
42
|
+
if idx == len(keys) - 2:
|
|
43
|
+
cur[key] = yaml.load(v, Loader=yaml.Loader)
|
|
44
|
+
else:
|
|
45
|
+
cur[key] = {}
|
|
46
|
+
cur = cur[key]
|
|
47
|
+
return config
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class AttrDict(dict):
|
|
51
|
+
"""Single level attribute dict, NOT recursive."""
|
|
52
|
+
|
|
53
|
+
def __init__(self, **kwargs):
|
|
54
|
+
super(AttrDict, self).__init__()
|
|
55
|
+
super(AttrDict, self).update(kwargs)
|
|
56
|
+
|
|
57
|
+
def __getattr__(self, key):
|
|
58
|
+
if key in self:
|
|
59
|
+
return self[key]
|
|
60
|
+
raise AttributeError("object has no attribute '{}'".format(key))
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _merge_dict(config, merge_dct):
|
|
64
|
+
"""Recursive dict merge. Inspired by :meth:``dict.update()``, instead of
|
|
65
|
+
updating only top-level keys, dict_merge recurses down into dicts nested to
|
|
66
|
+
an arbitrary depth, updating keys. The ``merge_dct`` is merged into
|
|
67
|
+
``dct``.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
config: dict onto which the merge is executed
|
|
71
|
+
merge_dct: dct merged into config
|
|
72
|
+
|
|
73
|
+
Returns: dct
|
|
74
|
+
"""
|
|
75
|
+
for key, value in merge_dct.items():
|
|
76
|
+
sub_keys = key.split('.')
|
|
77
|
+
key = sub_keys[0]
|
|
78
|
+
if key in config and len(sub_keys) > 1:
|
|
79
|
+
_merge_dict(config[key], {'.'.join(sub_keys[1:]): value})
|
|
80
|
+
elif key in config and isinstance(config[key], dict) and isinstance(
|
|
81
|
+
value, Mapping):
|
|
82
|
+
_merge_dict(config[key], value)
|
|
83
|
+
else:
|
|
84
|
+
config[key] = value
|
|
85
|
+
return config
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def print_dict(cfg, print_func=print, delimiter=0):
|
|
89
|
+
"""Recursively visualize a dict and indenting acrrording by the
|
|
90
|
+
relationship of keys."""
|
|
91
|
+
for k, v in sorted(cfg.items()):
|
|
92
|
+
if isinstance(v, dict):
|
|
93
|
+
print_func('{}{} : '.format(delimiter * ' ', str(k)))
|
|
94
|
+
print_dict(v, print_func, delimiter + 4)
|
|
95
|
+
elif isinstance(v, list) and len(v) >= 1 and isinstance(v[0], dict):
|
|
96
|
+
print_func('{}{} : '.format(delimiter * ' ', str(k)))
|
|
97
|
+
for value in v:
|
|
98
|
+
print_dict(value, print_func, delimiter + 4)
|
|
99
|
+
else:
|
|
100
|
+
print_func('{}{} : {}'.format(delimiter * ' ', k, v))
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class Config(object):
|
|
104
|
+
|
|
105
|
+
def __init__(self, config_path, BASE_KEY='_BASE_'):
|
|
106
|
+
self.BASE_KEY = BASE_KEY
|
|
107
|
+
self.cfg = self._load_config_with_base(config_path)
|
|
108
|
+
|
|
109
|
+
def _load_config_with_base(self, file_path):
|
|
110
|
+
"""Load config from file.
|
|
111
|
+
|
|
112
|
+
Args:
|
|
113
|
+
file_path (str): Path of the config file to be loaded.
|
|
114
|
+
|
|
115
|
+
Returns: global config
|
|
116
|
+
"""
|
|
117
|
+
_, ext = os.path.splitext(file_path)
|
|
118
|
+
assert ext in ['.yml', '.yaml'], 'only support yaml files for now'
|
|
119
|
+
|
|
120
|
+
with open(file_path) as f:
|
|
121
|
+
file_cfg = yaml.load(f, Loader=yaml.Loader)
|
|
122
|
+
|
|
123
|
+
# NOTE: cfgs outside have higher priority than cfgs in _BASE_
|
|
124
|
+
if self.BASE_KEY in file_cfg:
|
|
125
|
+
all_base_cfg = AttrDict()
|
|
126
|
+
base_ymls = list(file_cfg[self.BASE_KEY])
|
|
127
|
+
for base_yml in base_ymls:
|
|
128
|
+
if base_yml.startswith('~'):
|
|
129
|
+
base_yml = os.path.expanduser(base_yml)
|
|
130
|
+
if not base_yml.startswith('/'):
|
|
131
|
+
base_yml = os.path.join(os.path.dirname(file_path),
|
|
132
|
+
base_yml)
|
|
133
|
+
|
|
134
|
+
with open(base_yml) as f:
|
|
135
|
+
base_cfg = self._load_config_with_base(base_yml)
|
|
136
|
+
all_base_cfg = _merge_dict(all_base_cfg, base_cfg)
|
|
137
|
+
|
|
138
|
+
del file_cfg[self.BASE_KEY]
|
|
139
|
+
file_cfg = _merge_dict(all_base_cfg, file_cfg)
|
|
140
|
+
file_cfg['filename'] = os.path.splitext(
|
|
141
|
+
os.path.split(file_path)[-1])[0]
|
|
142
|
+
return file_cfg
|
|
143
|
+
|
|
144
|
+
def merge_dict(self, args):
|
|
145
|
+
self.cfg = _merge_dict(self.cfg, args)
|
|
146
|
+
|
|
147
|
+
def print_cfg(self, print_func=print):
|
|
148
|
+
"""Recursively visualize a dict and indenting acrrording by the
|
|
149
|
+
relationship of keys."""
|
|
150
|
+
print_func('----------- Config -----------')
|
|
151
|
+
print_dict(self.cfg, print_func)
|
|
152
|
+
print_func('---------------------------------------------')
|
|
153
|
+
|
|
154
|
+
def save(self, p, cfg=None):
|
|
155
|
+
if cfg is None:
|
|
156
|
+
cfg = self.cfg
|
|
157
|
+
with open(p, 'w') as f:
|
|
158
|
+
yaml.dump(dict(cfg), f, default_flow_style=False, sort_keys=False)
|