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.
Files changed (323) hide show
  1. openocr/__init__.py +11 -0
  2. openocr/configs/det/dbnet/repvit_db.yml +173 -0
  3. openocr/configs/rec/abinet/resnet45_trans_abinet_lang.yml +94 -0
  4. openocr/configs/rec/abinet/resnet45_trans_abinet_wo_lang.yml +93 -0
  5. openocr/configs/rec/abinet/svtrv2_abinet_lang.yml +130 -0
  6. openocr/configs/rec/abinet/svtrv2_abinet_wo_lang.yml +128 -0
  7. openocr/configs/rec/aster/resnet31_lstm_aster_tps_on.yml +93 -0
  8. openocr/configs/rec/aster/svtrv2_aster.yml +127 -0
  9. openocr/configs/rec/aster/svtrv2_aster_tps_on.yml +102 -0
  10. openocr/configs/rec/autostr/autostr_lstm_aster_tps_on.yml +95 -0
  11. openocr/configs/rec/busnet/svtrv2_busnet.yml +135 -0
  12. openocr/configs/rec/busnet/svtrv2_busnet_pretraining.yml +134 -0
  13. openocr/configs/rec/busnet/vit_busnet.yml +104 -0
  14. openocr/configs/rec/busnet/vit_busnet_pretraining.yml +104 -0
  15. openocr/configs/rec/cam/convnextv2_cam_tps_on.yml +118 -0
  16. openocr/configs/rec/cam/convnextv2_tiny_cam_tps_on.yml +118 -0
  17. openocr/configs/rec/cam/svtrv2_cam_tps_on.yml +123 -0
  18. openocr/configs/rec/cdistnet/resnet45_trans_cdistnet.yml +93 -0
  19. openocr/configs/rec/cdistnet/svtrv2_cdistnet.yml +139 -0
  20. openocr/configs/rec/cppd/svtr_base_cppd.yml +123 -0
  21. openocr/configs/rec/cppd/svtr_base_cppd_ch.yml +126 -0
  22. openocr/configs/rec/cppd/svtr_base_cppd_h8.yml +123 -0
  23. openocr/configs/rec/cppd/svtr_base_cppd_syn.yml +124 -0
  24. openocr/configs/rec/cppd/svtrv2_cppd.yml +150 -0
  25. openocr/configs/rec/dan/resnet45_fpn_dan.yml +98 -0
  26. openocr/configs/rec/dan/svtrv2_dan.yml +130 -0
  27. openocr/configs/rec/focalsvtr/focalsvtr_ctc.yml +137 -0
  28. openocr/configs/rec/gtc/svtrv2_lnconv_nrtr_gtc.yml +168 -0
  29. openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_long_infer.yml +151 -0
  30. openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_smtr_long.yml +150 -0
  31. openocr/configs/rec/gtc/svtrv2_lnconv_smtr_gtc_stream.yml +152 -0
  32. openocr/configs/rec/igtr/svtr_base_ds_igtr.yml +157 -0
  33. openocr/configs/rec/lister/focalsvtr_lister_wo_fem_maxratio12.yml +133 -0
  34. openocr/configs/rec/lister/svtrv2_lister_wo_fem_maxratio12.yml +138 -0
  35. openocr/configs/rec/lpv/svtr_base_lpv.yml +124 -0
  36. openocr/configs/rec/lpv/svtr_base_lpv_wo_glrm.yml +123 -0
  37. openocr/configs/rec/lpv/svtrv2_lpv.yml +147 -0
  38. openocr/configs/rec/lpv/svtrv2_lpv_wo_glrm.yml +146 -0
  39. openocr/configs/rec/maerec/vit_nrtr.yml +116 -0
  40. openocr/configs/rec/matrn/resnet45_trans_matrn.yml +95 -0
  41. openocr/configs/rec/matrn/svtrv2_matrn.yml +130 -0
  42. openocr/configs/rec/mgpstr/svtrv2_mgpstr_only_char.yml +140 -0
  43. openocr/configs/rec/mgpstr/vit_base_mgpstr_only_char.yml +111 -0
  44. openocr/configs/rec/mgpstr/vit_large_mgpstr_only_char.yml +110 -0
  45. openocr/configs/rec/mgpstr/vit_mgpstr.yml +110 -0
  46. openocr/configs/rec/mgpstr/vit_mgpstr_only_char.yml +110 -0
  47. openocr/configs/rec/moran/resnet31_lstm_moran.yml +92 -0
  48. openocr/configs/rec/nrtr/focalsvtr_nrtr_maxraio12.yml +145 -0
  49. openocr/configs/rec/nrtr/nrtr.yml +107 -0
  50. openocr/configs/rec/nrtr/svtr_base_nrtr.yml +118 -0
  51. openocr/configs/rec/nrtr/svtr_base_nrtr_syn.yml +119 -0
  52. openocr/configs/rec/nrtr/svtrv2_nrtr.yml +146 -0
  53. openocr/configs/rec/ote/svtr_base_h8_ote.yml +117 -0
  54. openocr/configs/rec/ote/svtr_base_ote.yml +116 -0
  55. openocr/configs/rec/parseq/focalsvtr_parseq_maxratio12.yml +140 -0
  56. openocr/configs/rec/parseq/svrtv2_parseq.yml +136 -0
  57. openocr/configs/rec/parseq/vit_parseq.yml +100 -0
  58. openocr/configs/rec/robustscanner/resnet31_robustscanner.yml +102 -0
  59. openocr/configs/rec/robustscanner/svtrv2_robustscanner.yml +134 -0
  60. openocr/configs/rec/sar/resnet31_lstm_sar.yml +94 -0
  61. openocr/configs/rec/sar/svtrv2_sar.yml +128 -0
  62. openocr/configs/rec/seed/resnet31_lstm_seed_tps_on.yml +96 -0
  63. openocr/configs/rec/smtr/focalsvtr_smtr.yml +150 -0
  64. openocr/configs/rec/smtr/focalsvtr_smtr_long.yml +133 -0
  65. openocr/configs/rec/smtr/svtrv2_smtr.yml +150 -0
  66. openocr/configs/rec/smtr/svtrv2_smtr_bi.yml +136 -0
  67. openocr/configs/rec/srn/resnet50_fpn_srn.yml +97 -0
  68. openocr/configs/rec/srn/svtrv2_srn.yml +131 -0
  69. openocr/configs/rec/svtrs/convnextv2_ctc.yml +105 -0
  70. openocr/configs/rec/svtrs/convnextv2_h8_ctc.yml +105 -0
  71. openocr/configs/rec/svtrs/convnextv2_h8_rctc.yml +106 -0
  72. openocr/configs/rec/svtrs/convnextv2_rctc.yml +106 -0
  73. openocr/configs/rec/svtrs/convnextv2_tiny_h8_ctc.yml +105 -0
  74. openocr/configs/rec/svtrs/convnextv2_tiny_h8_rctc.yml +106 -0
  75. openocr/configs/rec/svtrs/crnn_ctc.yml +99 -0
  76. openocr/configs/rec/svtrs/crnn_ctc_long.yml +116 -0
  77. openocr/configs/rec/svtrs/focalnet_base_ctc.yml +108 -0
  78. openocr/configs/rec/svtrs/focalnet_base_rctc.yml +109 -0
  79. openocr/configs/rec/svtrs/focalsvtr_ctc.yml +106 -0
  80. openocr/configs/rec/svtrs/focalsvtr_rctc.yml +107 -0
  81. openocr/configs/rec/svtrs/resnet45_trans_ctc.yml +103 -0
  82. openocr/configs/rec/svtrs/resnet45_trans_rctc.yml +104 -0
  83. openocr/configs/rec/svtrs/svtr_base_ctc.yml +110 -0
  84. openocr/configs/rec/svtrs/svtr_base_rctc.yml +111 -0
  85. openocr/configs/rec/svtrs/svtrnet_ctc_syn.yml +111 -0
  86. openocr/configs/rec/svtrs/vit_ctc.yml +103 -0
  87. openocr/configs/rec/svtrs/vit_rctc.yml +103 -0
  88. openocr/configs/rec/svtrv2/repsvtr_ch.yml +121 -0
  89. openocr/configs/rec/svtrv2/svtrv2_ch.yml +133 -0
  90. openocr/configs/rec/svtrv2/svtrv2_ctc.yml +136 -0
  91. openocr/configs/rec/svtrv2/svtrv2_rctc.yml +135 -0
  92. openocr/configs/rec/svtrv2/svtrv2_small_rctc.yml +135 -0
  93. openocr/configs/rec/svtrv2/svtrv2_smtr_gtc_rctc.yml +162 -0
  94. openocr/configs/rec/svtrv2/svtrv2_smtr_gtc_rctc_ch.yml +153 -0
  95. openocr/configs/rec/svtrv2/svtrv2_tiny_rctc.yml +135 -0
  96. openocr/configs/rec/visionlan/resnet45_trans_visionlan_LA.yml +103 -0
  97. openocr/configs/rec/visionlan/resnet45_trans_visionlan_LF_1.yml +102 -0
  98. openocr/configs/rec/visionlan/resnet45_trans_visionlan_LF_2.yml +103 -0
  99. openocr/configs/rec/visionlan/svtrv2_visionlan_LA.yml +112 -0
  100. openocr/configs/rec/visionlan/svtrv2_visionlan_LF_1.yml +111 -0
  101. openocr/configs/rec/visionlan/svtrv2_visionlan_LF_2.yml +112 -0
  102. openocr/demo_gradio.py +128 -0
  103. openocr/opendet/modeling/__init__.py +11 -0
  104. openocr/opendet/modeling/backbones/__init__.py +14 -0
  105. openocr/opendet/modeling/backbones/repvit.py +340 -0
  106. openocr/opendet/modeling/base_detector.py +69 -0
  107. openocr/opendet/modeling/heads/__init__.py +14 -0
  108. openocr/opendet/modeling/heads/db_head.py +73 -0
  109. openocr/opendet/modeling/necks/__init__.py +14 -0
  110. openocr/opendet/modeling/necks/db_fpn.py +609 -0
  111. openocr/opendet/postprocess/__init__.py +18 -0
  112. openocr/opendet/postprocess/db_postprocess.py +273 -0
  113. openocr/opendet/preprocess/__init__.py +154 -0
  114. openocr/opendet/preprocess/crop_resize.py +121 -0
  115. openocr/opendet/preprocess/db_resize_for_test.py +135 -0
  116. openocr/openrec/losses/__init__.py +62 -0
  117. openocr/openrec/losses/abinet_loss.py +42 -0
  118. openocr/openrec/losses/ar_loss.py +23 -0
  119. openocr/openrec/losses/cam_loss.py +48 -0
  120. openocr/openrec/losses/cdistnet_loss.py +34 -0
  121. openocr/openrec/losses/ce_loss.py +68 -0
  122. openocr/openrec/losses/cppd_loss.py +77 -0
  123. openocr/openrec/losses/ctc_loss.py +33 -0
  124. openocr/openrec/losses/igtr_loss.py +12 -0
  125. openocr/openrec/losses/lister_loss.py +14 -0
  126. openocr/openrec/losses/lpv_loss.py +30 -0
  127. openocr/openrec/losses/mgp_loss.py +34 -0
  128. openocr/openrec/losses/parseq_loss.py +12 -0
  129. openocr/openrec/losses/robustscanner_loss.py +20 -0
  130. openocr/openrec/losses/seed_loss.py +46 -0
  131. openocr/openrec/losses/smtr_loss.py +12 -0
  132. openocr/openrec/losses/srn_loss.py +40 -0
  133. openocr/openrec/losses/visionlan_loss.py +58 -0
  134. openocr/openrec/metrics/__init__.py +19 -0
  135. openocr/openrec/metrics/rec_metric.py +270 -0
  136. openocr/openrec/metrics/rec_metric_gtc.py +58 -0
  137. openocr/openrec/metrics/rec_metric_long.py +142 -0
  138. openocr/openrec/metrics/rec_metric_mgp.py +93 -0
  139. openocr/openrec/modeling/__init__.py +11 -0
  140. openocr/openrec/modeling/base_recognizer.py +69 -0
  141. openocr/openrec/modeling/common.py +238 -0
  142. openocr/openrec/modeling/decoders/__init__.py +109 -0
  143. openocr/openrec/modeling/decoders/abinet_decoder.py +283 -0
  144. openocr/openrec/modeling/decoders/aster_decoder.py +170 -0
  145. openocr/openrec/modeling/decoders/bus_decoder.py +133 -0
  146. openocr/openrec/modeling/decoders/cam_decoder.py +43 -0
  147. openocr/openrec/modeling/decoders/cdistnet_decoder.py +334 -0
  148. openocr/openrec/modeling/decoders/cppd_decoder.py +393 -0
  149. openocr/openrec/modeling/decoders/ctc_decoder.py +203 -0
  150. openocr/openrec/modeling/decoders/dan_decoder.py +203 -0
  151. openocr/openrec/modeling/decoders/igtr_decoder.py +815 -0
  152. openocr/openrec/modeling/decoders/lister_decoder.py +535 -0
  153. openocr/openrec/modeling/decoders/lpv_decoder.py +119 -0
  154. openocr/openrec/modeling/decoders/matrn_decoder.py +236 -0
  155. openocr/openrec/modeling/decoders/mgp_decoder.py +99 -0
  156. openocr/openrec/modeling/decoders/nrtr_decoder.py +439 -0
  157. openocr/openrec/modeling/decoders/ote_decoder.py +205 -0
  158. openocr/openrec/modeling/decoders/parseq_decoder.py +504 -0
  159. openocr/openrec/modeling/decoders/rctc_decoder.py +70 -0
  160. openocr/openrec/modeling/decoders/robustscanner_decoder.py +749 -0
  161. openocr/openrec/modeling/decoders/sar_decoder.py +236 -0
  162. openocr/openrec/modeling/decoders/smtr_decoder.py +621 -0
  163. openocr/openrec/modeling/decoders/smtr_decoder_nattn.py +521 -0
  164. openocr/openrec/modeling/decoders/srn_decoder.py +283 -0
  165. openocr/openrec/modeling/decoders/visionlan_decoder.py +321 -0
  166. openocr/openrec/modeling/encoders/__init__.py +39 -0
  167. openocr/openrec/modeling/encoders/autostr_encoder.py +327 -0
  168. openocr/openrec/modeling/encoders/cam_encoder.py +760 -0
  169. openocr/openrec/modeling/encoders/convnextv2.py +213 -0
  170. openocr/openrec/modeling/encoders/focalsvtr.py +631 -0
  171. openocr/openrec/modeling/encoders/nrtr_encoder.py +28 -0
  172. openocr/openrec/modeling/encoders/rec_hgnet.py +346 -0
  173. openocr/openrec/modeling/encoders/rec_lcnetv3.py +488 -0
  174. openocr/openrec/modeling/encoders/rec_mobilenet_v3.py +132 -0
  175. openocr/openrec/modeling/encoders/rec_mv1_enhance.py +254 -0
  176. openocr/openrec/modeling/encoders/rec_nrtr_mtb.py +37 -0
  177. openocr/openrec/modeling/encoders/rec_resnet_31.py +213 -0
  178. openocr/openrec/modeling/encoders/rec_resnet_45.py +183 -0
  179. openocr/openrec/modeling/encoders/rec_resnet_fpn.py +216 -0
  180. openocr/openrec/modeling/encoders/rec_resnet_vd.py +252 -0
  181. openocr/openrec/modeling/encoders/repvit.py +338 -0
  182. openocr/openrec/modeling/encoders/resnet31_rnn.py +123 -0
  183. openocr/openrec/modeling/encoders/svtrnet.py +574 -0
  184. openocr/openrec/modeling/encoders/svtrnet2dpos.py +616 -0
  185. openocr/openrec/modeling/encoders/svtrv2.py +470 -0
  186. openocr/openrec/modeling/encoders/svtrv2_lnconv.py +503 -0
  187. openocr/openrec/modeling/encoders/svtrv2_lnconv_two33.py +517 -0
  188. openocr/openrec/modeling/encoders/vit.py +120 -0
  189. openocr/openrec/modeling/transforms/__init__.py +15 -0
  190. openocr/openrec/modeling/transforms/aster_tps.py +262 -0
  191. openocr/openrec/modeling/transforms/moran.py +136 -0
  192. openocr/openrec/modeling/transforms/tps.py +246 -0
  193. openocr/openrec/optimizer/__init__.py +73 -0
  194. openocr/openrec/optimizer/lr.py +227 -0
  195. openocr/openrec/postprocess/__init__.py +72 -0
  196. openocr/openrec/postprocess/abinet_postprocess.py +37 -0
  197. openocr/openrec/postprocess/ar_postprocess.py +63 -0
  198. openocr/openrec/postprocess/ce_postprocess.py +43 -0
  199. openocr/openrec/postprocess/char_postprocess.py +108 -0
  200. openocr/openrec/postprocess/cppd_postprocess.py +42 -0
  201. openocr/openrec/postprocess/ctc_postprocess.py +119 -0
  202. openocr/openrec/postprocess/igtr_postprocess.py +100 -0
  203. openocr/openrec/postprocess/lister_postprocess.py +59 -0
  204. openocr/openrec/postprocess/mgp_postprocess.py +143 -0
  205. openocr/openrec/postprocess/nrtr_postprocess.py +75 -0
  206. openocr/openrec/postprocess/smtr_postprocess.py +73 -0
  207. openocr/openrec/postprocess/srn_postprocess.py +80 -0
  208. openocr/openrec/postprocess/visionlan_postprocess.py +81 -0
  209. openocr/openrec/preprocess/__init__.py +173 -0
  210. openocr/openrec/preprocess/abinet_aug.py +473 -0
  211. openocr/openrec/preprocess/abinet_label_encode.py +36 -0
  212. openocr/openrec/preprocess/ar_label_encode.py +36 -0
  213. openocr/openrec/preprocess/auto_augment.py +1012 -0
  214. openocr/openrec/preprocess/cam_label_encode.py +141 -0
  215. openocr/openrec/preprocess/ce_label_encode.py +116 -0
  216. openocr/openrec/preprocess/char_label_encode.py +36 -0
  217. openocr/openrec/preprocess/cppd_label_encode.py +173 -0
  218. openocr/openrec/preprocess/ctc_label_encode.py +124 -0
  219. openocr/openrec/preprocess/ep_label_encode.py +38 -0
  220. openocr/openrec/preprocess/igtr_label_encode.py +360 -0
  221. openocr/openrec/preprocess/mgp_label_encode.py +95 -0
  222. openocr/openrec/preprocess/parseq_aug.py +150 -0
  223. openocr/openrec/preprocess/rec_aug.py +211 -0
  224. openocr/openrec/preprocess/resize.py +534 -0
  225. openocr/openrec/preprocess/smtr_label_encode.py +125 -0
  226. openocr/openrec/preprocess/srn_label_encode.py +37 -0
  227. openocr/openrec/preprocess/visionlan_label_encode.py +67 -0
  228. openocr/tools/create_lmdb_dataset.py +118 -0
  229. openocr/tools/data/__init__.py +94 -0
  230. openocr/tools/data/collate_fn.py +100 -0
  231. openocr/tools/data/lmdb_dataset.py +142 -0
  232. openocr/tools/data/lmdb_dataset_test.py +166 -0
  233. openocr/tools/data/multi_scale_sampler.py +177 -0
  234. openocr/tools/data/ratio_dataset.py +217 -0
  235. openocr/tools/data/ratio_dataset_test.py +273 -0
  236. openocr/tools/data/ratio_dataset_tvresize.py +213 -0
  237. openocr/tools/data/ratio_dataset_tvresize_test.py +276 -0
  238. openocr/tools/data/ratio_sampler.py +190 -0
  239. openocr/tools/data/simple_dataset.py +263 -0
  240. openocr/tools/data/strlmdb_dataset.py +143 -0
  241. openocr/tools/engine/__init__.py +5 -0
  242. openocr/tools/engine/config.py +158 -0
  243. openocr/tools/engine/trainer.py +621 -0
  244. openocr/tools/eval_rec.py +41 -0
  245. openocr/tools/eval_rec_all_ch.py +184 -0
  246. openocr/tools/eval_rec_all_en.py +206 -0
  247. openocr/tools/eval_rec_all_long.py +119 -0
  248. openocr/tools/eval_rec_all_long_simple.py +122 -0
  249. openocr/tools/export_rec.py +118 -0
  250. openocr/tools/infer/onnx_engine.py +65 -0
  251. openocr/tools/infer/predict_rec.py +140 -0
  252. openocr/tools/infer/utility.py +234 -0
  253. openocr/tools/infer_det.py +449 -0
  254. openocr/tools/infer_e2e.py +462 -0
  255. openocr/tools/infer_e2e_parallel.py +184 -0
  256. openocr/tools/infer_rec.py +371 -0
  257. openocr/tools/train_rec.py +37 -0
  258. openocr/tools/utility.py +45 -0
  259. openocr/tools/utils/EN_symbol_dict.txt +94 -0
  260. openocr/tools/utils/__init__.py +0 -0
  261. openocr/tools/utils/ckpt.py +87 -0
  262. openocr/tools/utils/dict/ar_dict.txt +117 -0
  263. openocr/tools/utils/dict/arabic_dict.txt +161 -0
  264. openocr/tools/utils/dict/be_dict.txt +145 -0
  265. openocr/tools/utils/dict/bg_dict.txt +140 -0
  266. openocr/tools/utils/dict/chinese_cht_dict.txt +8421 -0
  267. openocr/tools/utils/dict/cyrillic_dict.txt +163 -0
  268. openocr/tools/utils/dict/devanagari_dict.txt +167 -0
  269. openocr/tools/utils/dict/en_dict.txt +63 -0
  270. openocr/tools/utils/dict/fa_dict.txt +136 -0
  271. openocr/tools/utils/dict/french_dict.txt +136 -0
  272. openocr/tools/utils/dict/german_dict.txt +143 -0
  273. openocr/tools/utils/dict/hi_dict.txt +162 -0
  274. openocr/tools/utils/dict/it_dict.txt +118 -0
  275. openocr/tools/utils/dict/japan_dict.txt +4399 -0
  276. openocr/tools/utils/dict/ka_dict.txt +153 -0
  277. openocr/tools/utils/dict/kie_dict/xfund_class_list.txt +4 -0
  278. openocr/tools/utils/dict/korean_dict.txt +3688 -0
  279. openocr/tools/utils/dict/latex_symbol_dict.txt +111 -0
  280. openocr/tools/utils/dict/latin_dict.txt +185 -0
  281. openocr/tools/utils/dict/layout_dict/layout_cdla_dict.txt +10 -0
  282. openocr/tools/utils/dict/layout_dict/layout_publaynet_dict.txt +5 -0
  283. openocr/tools/utils/dict/layout_dict/layout_table_dict.txt +1 -0
  284. openocr/tools/utils/dict/mr_dict.txt +153 -0
  285. openocr/tools/utils/dict/ne_dict.txt +153 -0
  286. openocr/tools/utils/dict/oc_dict.txt +96 -0
  287. openocr/tools/utils/dict/pu_dict.txt +130 -0
  288. openocr/tools/utils/dict/rs_dict.txt +91 -0
  289. openocr/tools/utils/dict/rsc_dict.txt +134 -0
  290. openocr/tools/utils/dict/ru_dict.txt +125 -0
  291. openocr/tools/utils/dict/spin_dict.txt +68 -0
  292. openocr/tools/utils/dict/ta_dict.txt +128 -0
  293. openocr/tools/utils/dict/table_dict.txt +277 -0
  294. openocr/tools/utils/dict/table_master_structure_dict.txt +39 -0
  295. openocr/tools/utils/dict/table_structure_dict.txt +28 -0
  296. openocr/tools/utils/dict/table_structure_dict_ch.txt +48 -0
  297. openocr/tools/utils/dict/te_dict.txt +151 -0
  298. openocr/tools/utils/dict/ug_dict.txt +114 -0
  299. openocr/tools/utils/dict/uk_dict.txt +142 -0
  300. openocr/tools/utils/dict/ur_dict.txt +137 -0
  301. openocr/tools/utils/dict/xi_dict.txt +110 -0
  302. openocr/tools/utils/dict90.txt +90 -0
  303. openocr/tools/utils/e2e_metric/Deteval.py +802 -0
  304. openocr/tools/utils/e2e_metric/polygon_fast.py +70 -0
  305. openocr/tools/utils/e2e_utils/extract_batchsize.py +86 -0
  306. openocr/tools/utils/e2e_utils/extract_textpoint_fast.py +479 -0
  307. openocr/tools/utils/e2e_utils/extract_textpoint_slow.py +582 -0
  308. openocr/tools/utils/e2e_utils/pgnet_pp_utils.py +159 -0
  309. openocr/tools/utils/e2e_utils/visual.py +152 -0
  310. openocr/tools/utils/en_dict.txt +95 -0
  311. openocr/tools/utils/gen_label.py +68 -0
  312. openocr/tools/utils/ic15_dict.txt +36 -0
  313. openocr/tools/utils/logging.py +56 -0
  314. openocr/tools/utils/poly_nms.py +132 -0
  315. openocr/tools/utils/ppocr_keys_v1.txt +6623 -0
  316. openocr/tools/utils/stats.py +58 -0
  317. openocr/tools/utils/utility.py +165 -0
  318. openocr/tools/utils/visual.py +117 -0
  319. openocr_python-0.0.2.dist-info/LICENCE +201 -0
  320. openocr_python-0.0.2.dist-info/METADATA +98 -0
  321. openocr_python-0.0.2.dist-info/RECORD +323 -0
  322. openocr_python-0.0.2.dist-info/WHEEL +5 -0
  323. openocr_python-0.0.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,283 @@
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+
5
+ from .nrtr_decoder import Embeddings, TransformerBlock
6
+
7
+
8
+ class PVAM(nn.Module):
9
+
10
+ def __init__(self,
11
+ in_channels,
12
+ char_num,
13
+ max_text_length,
14
+ num_heads,
15
+ hidden_dims,
16
+ dropout_rate=0):
17
+ super(PVAM, self).__init__()
18
+ self.char_num = char_num
19
+ self.max_length = max_text_length
20
+ self.num_heads = num_heads
21
+ self.hidden_dims = hidden_dims
22
+ self.dropout_rate = dropout_rate
23
+ #TODO
24
+ self.emb = nn.Embedding(num_embeddings=256,
25
+ embedding_dim=hidden_dims,
26
+ sparse=False)
27
+ self.drop_out = nn.Dropout(dropout_rate)
28
+ self.feat_emb = nn.Linear(in_channels, in_channels)
29
+ self.token_emb = nn.Embedding(max_text_length, in_channels)
30
+ self.score = nn.Linear(in_channels, 1, bias=False)
31
+
32
+ def feat_pos_mix(self, conv_features, encoder_word_pos, dropout_rate):
33
+ #b h*w c
34
+ pos_emb = self.emb(encoder_word_pos)
35
+ # pos_emb = pos_emb.detach()
36
+ enc_input = conv_features + pos_emb
37
+
38
+ if dropout_rate:
39
+ enc_input = self.drop_out(enc_input)
40
+
41
+ return enc_input
42
+
43
+ def forward(self, inputs):
44
+ b, c, h, w = inputs.shape
45
+ conv_features = inputs.view(-1, c, h * w)
46
+ conv_features = conv_features.permute(0, 2, 1).contiguous()
47
+ # b h*w c
48
+
49
+ # transformer encoder
50
+ b, t, c = conv_features.shape
51
+
52
+ encoder_feat_pos = torch.arange(t, dtype=torch.long).to(inputs.device)
53
+
54
+ enc_inputs = self.feat_pos_mix(conv_features, encoder_feat_pos,
55
+ self.dropout_rate)
56
+
57
+ inputs = self.feat_emb(enc_inputs) # feat emb
58
+
59
+ inputs = inputs.unsqueeze(1).expand(-1, self.max_length, -1, -1)
60
+
61
+ # b maxlen h*w c
62
+
63
+ tokens_pos = torch.arange(self.max_length,
64
+ dtype=torch.long).to(inputs.device)
65
+ tokens_pos = tokens_pos.unsqueeze(0).expand(b, -1)
66
+
67
+ tokens_pos_emd = self.token_emb(tokens_pos)
68
+ tokens_pos_emd = tokens_pos_emd.unsqueeze(2).expand(-1, -1, t, -1)
69
+ # b maxlen h*w c
70
+
71
+ attention_weight = torch.tanh(tokens_pos_emd + inputs)
72
+
73
+ attention_weight = torch.squeeze(self.score(attention_weight),
74
+ -1) #b,25,256
75
+
76
+ attention_weight = F.softmax(attention_weight, dim=-1) #b,25,256
77
+
78
+ pvam_features = torch.matmul(attention_weight, enc_inputs)
79
+
80
+ return pvam_features
81
+
82
+
83
+ class GSRM(nn.Module):
84
+
85
+ def __init__(self,
86
+ in_channel,
87
+ char_num,
88
+ max_len,
89
+ num_heads,
90
+ hidden_dims,
91
+ num_layers,
92
+ dropout_rate=0,
93
+ attention_dropout=0.1):
94
+ super(GSRM, self).__init__()
95
+ self.char_num = char_num
96
+ self.max_len = max_len
97
+ self.num_heads = num_heads
98
+
99
+ self.cls_op = nn.Linear(in_channel, self.char_num)
100
+ self.cls_final = nn.Linear(in_channel, self.char_num)
101
+
102
+ self.word_emb = Embeddings(d_model=hidden_dims, vocab=char_num)
103
+ self.pos_emb = nn.Embedding(char_num, hidden_dims)
104
+ self.dropout_rate = dropout_rate
105
+ self.emb_drop_out = nn.Dropout(dropout_rate)
106
+
107
+ self.forward_self_attn = nn.ModuleList([
108
+ TransformerBlock(
109
+ d_model=hidden_dims,
110
+ nhead=num_heads,
111
+ attention_dropout_rate=attention_dropout,
112
+ residual_dropout_rate=0.1,
113
+ dim_feedforward=hidden_dims,
114
+ with_self_attn=True,
115
+ with_cross_attn=False,
116
+ ) for i in range(num_layers)
117
+ ])
118
+
119
+ self.backward_self_attn = nn.ModuleList([
120
+ TransformerBlock(
121
+ d_model=hidden_dims,
122
+ nhead=num_heads,
123
+ attention_dropout_rate=attention_dropout,
124
+ residual_dropout_rate=0.1,
125
+ dim_feedforward=hidden_dims,
126
+ with_self_attn=True,
127
+ with_cross_attn=False,
128
+ ) for i in range(num_layers)
129
+ ])
130
+
131
+ def _pos_emb(self, word_seq, pos, dropoutrate):
132
+ """
133
+ word_Seq: bsz len
134
+ pos: bsz len
135
+ """
136
+ word_emb_seq = self.word_emb(word_seq)
137
+ pos_emb_seq = self.pos_emb(pos)
138
+ # pos_emb_seq = pos_emb_seq.detach()
139
+
140
+ input_mix = word_emb_seq + pos_emb_seq
141
+ if dropoutrate > 0:
142
+ input_mix = self.emb_drop_out(input_mix)
143
+
144
+ return input_mix
145
+
146
+ def forward(self, inputs):
147
+
148
+ bos_idx = self.char_num - 2
149
+ eos_idx = self.char_num - 1
150
+ b, t, c = inputs.size() #b,25,512
151
+ inputs = inputs.view(-1, c)
152
+ cls_res = self.cls_op(inputs) #b,25,n_class
153
+
154
+ word_pred_PVAM = F.softmax(cls_res, dim=-1).argmax(-1)
155
+ word_pred_PVAM = word_pred_PVAM.view(-1, t, 1)
156
+ #b 25 1
157
+ word1 = F.pad(word_pred_PVAM, [0, 0, 1, 0], 'constant', value=bos_idx)
158
+ word_forward = word1[:, :-1, :].squeeze(-1)
159
+
160
+ word_backward = word_pred_PVAM.squeeze(-1)
161
+
162
+ #mask
163
+ attn_mask_forward = torch.triu(
164
+ torch.full((self.max_len, self.max_len),
165
+ dtype=torch.float32,
166
+ fill_value=-torch.inf),
167
+ diagonal=1,
168
+ ).to(inputs.device)
169
+ attn_mask_forward = attn_mask_forward.unsqueeze(0).expand(
170
+ self.num_heads, -1, -1)
171
+ attn_mask_backward = torch.tril(
172
+ torch.full((self.max_len, self.max_len),
173
+ dtype=torch.float32,
174
+ fill_value=-torch.inf),
175
+ diagonal=-1,
176
+ ).to(inputs.device)
177
+ attn_mask_backward = attn_mask_backward.unsqueeze(0).expand(
178
+ self.num_heads, -1, -1)
179
+
180
+ #B,25
181
+
182
+ pos = torch.arange(self.max_len, dtype=torch.long).to(inputs.device)
183
+ pos = pos.unsqueeze(0).expand(b, -1) #b,25
184
+
185
+ word_front_mix = self._pos_emb(word_forward, pos, self.dropout_rate)
186
+ word_backward_mix = self._pos_emb(word_backward, pos,
187
+ self.dropout_rate)
188
+ # b 25 emb_dim
189
+
190
+ for attn_layer in self.forward_self_attn:
191
+ word_front_mix = attn_layer(word_front_mix,
192
+ self_mask=attn_mask_forward)
193
+
194
+ for attn_layer in self.backward_self_attn:
195
+ word_backward_mix = attn_layer(word_backward_mix,
196
+ self_mask=attn_mask_backward)
197
+
198
+ #b,25,emb_dim
199
+ eos_emd = self.word_emb(torch.full(
200
+ (1, ), eos_idx).to(inputs.device)).expand(b, 1, -1)
201
+ word_backward_mix = torch.cat((word_backward_mix, eos_emd), dim=1)
202
+ word_backward_mix = word_backward_mix[:, 1:, ]
203
+
204
+ gsrm_features = word_front_mix + word_backward_mix
205
+
206
+ gsrm_out = self.cls_final(gsrm_features)
207
+ # torch.matmul(gsrm_features,
208
+ # self.word_emb.embedding.weight.permute(1, 0))
209
+
210
+ b, t, c = gsrm_out.size()
211
+ #b,25,n_class
212
+ gsrm_out = gsrm_out.view(-1, c).contiguous()
213
+
214
+ return gsrm_features, cls_res, gsrm_out
215
+
216
+
217
+ class VSFD(nn.Module):
218
+
219
+ def __init__(self, in_channels, out_channels):
220
+ super(VSFD, self).__init__()
221
+ self.char_num = out_channels
222
+ self.fc0 = nn.Linear(in_channels * 2, in_channels)
223
+ self.fc1 = nn.Linear(in_channels, self.char_num)
224
+
225
+ def forward(self, pvam_feature, gsrm_feature):
226
+ _, t, c1 = pvam_feature.size()
227
+ _, t, c2 = gsrm_feature.size()
228
+ combine_featurs = torch.cat([pvam_feature, gsrm_feature], dim=-1)
229
+ combine_featurs = combine_featurs.view(-1, c1 + c2).contiguous()
230
+ atten = self.fc0(combine_featurs)
231
+ atten = torch.sigmoid(atten)
232
+ atten = atten.view(-1, t, c1)
233
+ combine_featurs = atten * pvam_feature + (1 - atten) * gsrm_feature
234
+ combine_featurs = combine_featurs.view(-1, c1).contiguous()
235
+ out = self.fc1(combine_featurs)
236
+ return out
237
+
238
+
239
+ class SRNDecoder(nn.Module):
240
+
241
+ def __init__(self,
242
+ in_channels,
243
+ out_channels,
244
+ hidden_dims,
245
+ num_decoder_layers=4,
246
+ max_text_length=25,
247
+ num_heads=8,
248
+ **kwargs):
249
+ super(SRNDecoder, self).__init__()
250
+
251
+ self.max_text_length = max_text_length
252
+ self.num_heads = num_heads
253
+
254
+ self.pvam = PVAM(in_channels=in_channels,
255
+ char_num=out_channels,
256
+ max_text_length=max_text_length,
257
+ num_heads=num_heads,
258
+ hidden_dims=hidden_dims,
259
+ dropout_rate=0.1)
260
+
261
+ self.gsrm = GSRM(in_channel=in_channels,
262
+ char_num=out_channels,
263
+ max_len=max_text_length,
264
+ num_heads=num_heads,
265
+ num_layers=num_decoder_layers,
266
+ hidden_dims=hidden_dims)
267
+
268
+ self.vsfd = VSFD(in_channels=in_channels, out_channels=out_channels)
269
+
270
+ def forward(self, feat, data=None):
271
+ # feat [B,512,8,32]
272
+
273
+ pvam_feature = self.pvam(feat)
274
+
275
+ gsrm_features, pvam_preds, gsrm_preds = self.gsrm(pvam_feature)
276
+
277
+ vsfd_preds = self.vsfd(pvam_feature, gsrm_features)
278
+
279
+ if not self.training:
280
+ preds = F.softmax(vsfd_preds, dim=-1)
281
+ return preds
282
+
283
+ return [pvam_preds, gsrm_preds, vsfd_preds]
@@ -0,0 +1,321 @@
1
+ import torch
2
+ import torch.nn as nn
3
+
4
+ from openrec.modeling.decoders.nrtr_decoder import PositionalEncoding, TransformerBlock
5
+
6
+
7
+ class Transformer_Encoder(nn.Module):
8
+
9
+ def __init__(
10
+ self,
11
+ n_layers=3,
12
+ n_head=8,
13
+ d_model=512,
14
+ d_inner=2048,
15
+ dropout=0.1,
16
+ n_position=256,
17
+ ):
18
+
19
+ super(Transformer_Encoder, self).__init__()
20
+ self.pe = PositionalEncoding(dropout=dropout,
21
+ dim=d_model,
22
+ max_len=n_position)
23
+ self.layer_stack = nn.ModuleList([
24
+ TransformerBlock(d_model, n_head, d_inner) for _ in range(n_layers)
25
+ ])
26
+ self.layer_norm = nn.LayerNorm(d_model, eps=1e-6)
27
+
28
+ def forward(self, enc_output, src_mask):
29
+ enc_output = self.pe(enc_output) # position embeding
30
+ for enc_layer in self.layer_stack:
31
+ enc_output = enc_layer(enc_output, self_mask=src_mask)
32
+ enc_output = self.layer_norm(enc_output)
33
+ return enc_output
34
+
35
+
36
+ class PP_layer(nn.Module):
37
+
38
+ def __init__(self, n_dim=512, N_max_character=25, n_position=256):
39
+ super(PP_layer, self).__init__()
40
+ self.character_len = N_max_character
41
+ self.f0_embedding = nn.Embedding(N_max_character, n_dim)
42
+ self.w0 = nn.Linear(N_max_character, n_position)
43
+ self.wv = nn.Linear(n_dim, n_dim)
44
+ self.we = nn.Linear(n_dim, N_max_character)
45
+ self.active = nn.Tanh()
46
+ self.softmax = nn.Softmax(dim=2)
47
+
48
+ def forward(self, enc_output):
49
+ reading_order = torch.arange(self.character_len,
50
+ dtype=torch.long,
51
+ device=enc_output.device)
52
+ reading_order = reading_order.unsqueeze(0).expand(
53
+ enc_output.shape[0], -1) # (S,) -> (B, S)
54
+ reading_order = self.f0_embedding(reading_order) # b,25,512
55
+ # calculate attention
56
+
57
+ t = self.w0(reading_order.transpose(1, 2)) # b,512,256
58
+ t = self.active(t.transpose(1, 2) + self.wv(enc_output)) # b,256,512
59
+ t = self.we(t) # b,256,25
60
+ t = self.softmax(t.transpose(1, 2)) # b,25,256
61
+ g_output = torch.bmm(t, enc_output) # b,25,512
62
+ return g_output
63
+
64
+
65
+ class Prediction(nn.Module):
66
+
67
+ def __init__(
68
+ self,
69
+ n_dim=512,
70
+ n_class=37,
71
+ N_max_character=25,
72
+ n_position=256,
73
+ ):
74
+ super(Prediction, self).__init__()
75
+ self.pp = PP_layer(n_dim=n_dim,
76
+ N_max_character=N_max_character,
77
+ n_position=n_position)
78
+ self.pp_share = PP_layer(n_dim=n_dim,
79
+ N_max_character=N_max_character,
80
+ n_position=n_position)
81
+ self.w_vrm = nn.Linear(n_dim, n_class) # output layer
82
+ self.w_share = nn.Linear(n_dim, n_class) # output layer
83
+ self.nclass = n_class
84
+
85
+ def forward(self, cnn_feature, f_res, f_sub, is_Train=False, use_mlm=True):
86
+ if is_Train:
87
+ if not use_mlm:
88
+ g_output = self.pp(cnn_feature) # b,25,512
89
+ g_output = self.w_vrm(g_output)
90
+ f_res = 0
91
+ f_sub = 0
92
+ return g_output, f_res, f_sub
93
+ g_output = self.pp(cnn_feature) # b,25,512
94
+ f_res = self.pp_share(f_res)
95
+ f_sub = self.pp_share(f_sub)
96
+ g_output = self.w_vrm(g_output)
97
+ f_res = self.w_share(f_res)
98
+ f_sub = self.w_share(f_sub)
99
+ return g_output, f_res, f_sub
100
+ else:
101
+ g_output = self.pp(cnn_feature) # b,25,512
102
+ g_output = self.w_vrm(g_output)
103
+ return g_output
104
+
105
+
106
+ class MLM(nn.Module):
107
+ """Architecture of MLM."""
108
+
109
+ def __init__(
110
+ self,
111
+ n_dim=512,
112
+ n_position=256,
113
+ n_head=8,
114
+ dim_feedforward=2048,
115
+ max_text_length=25,
116
+ ):
117
+ super(MLM, self).__init__()
118
+ self.MLM_SequenceModeling_mask = Transformer_Encoder(
119
+ n_layers=2,
120
+ n_head=n_head,
121
+ d_model=n_dim,
122
+ d_inner=dim_feedforward,
123
+ n_position=n_position,
124
+ )
125
+ self.MLM_SequenceModeling_WCL = Transformer_Encoder(
126
+ n_layers=1,
127
+ n_head=n_head,
128
+ d_model=n_dim,
129
+ d_inner=dim_feedforward,
130
+ n_position=n_position,
131
+ )
132
+ self.pos_embedding = nn.Embedding(max_text_length, n_dim)
133
+ self.w0_linear = nn.Linear(1, n_position)
134
+ self.wv = nn.Linear(n_dim, n_dim)
135
+ self.active = nn.Tanh()
136
+ self.we = nn.Linear(n_dim, 1)
137
+ self.sigmoid = nn.Sigmoid()
138
+
139
+ def forward(self, input, label_pos):
140
+ # transformer unit for generating mask_c
141
+ feature_v_seq = self.MLM_SequenceModeling_mask(input, src_mask=None)
142
+ # position embedding layer
143
+ pos_emb = self.pos_embedding(label_pos.long())
144
+ pos_emb = self.w0_linear(torch.unsqueeze(pos_emb,
145
+ dim=2)).transpose(1, 2)
146
+ # fusion position embedding with features V & generate mask_c
147
+ att_map_sub = self.active(pos_emb + self.wv(feature_v_seq))
148
+ att_map_sub = self.we(att_map_sub) # b,256,1
149
+ att_map_sub = self.sigmoid(att_map_sub.transpose(1, 2)) # b,1,256
150
+ # WCL
151
+ # generate inputs for WCL
152
+ f_res = input * (1 - att_map_sub.transpose(1, 2)
153
+ ) # second path with remaining string
154
+ f_sub = input * (att_map_sub.transpose(1, 2)
155
+ ) # first path with occluded character
156
+ # transformer units in WCL
157
+ f_res = self.MLM_SequenceModeling_WCL(f_res, src_mask=None)
158
+ f_sub = self.MLM_SequenceModeling_WCL(f_sub, src_mask=None)
159
+ return f_res, f_sub, att_map_sub
160
+
161
+
162
+ class MLM_VRM(nn.Module):
163
+
164
+ def __init__(
165
+ self,
166
+ n_layers=3,
167
+ n_position=256,
168
+ n_dim=512,
169
+ n_head=8,
170
+ dim_feedforward=2048,
171
+ max_text_length=25,
172
+ nclass=37,
173
+ ):
174
+ super(MLM_VRM, self).__init__()
175
+ self.MLM = MLM(
176
+ n_dim=n_dim,
177
+ n_position=n_position,
178
+ n_head=n_head,
179
+ dim_feedforward=dim_feedforward,
180
+ max_text_length=max_text_length,
181
+ )
182
+ self.SequenceModeling = Transformer_Encoder(
183
+ n_layers=n_layers,
184
+ n_head=n_head,
185
+ d_model=n_dim,
186
+ d_inner=dim_feedforward,
187
+ n_position=n_position,
188
+ )
189
+ self.Prediction = Prediction(
190
+ n_dim=n_dim,
191
+ n_position=n_position,
192
+ N_max_character=max_text_length + 1,
193
+ n_class=nclass,
194
+ ) # N_max_character = 1 eos + 25 characters
195
+ self.nclass = nclass
196
+ self.max_text_length = max_text_length
197
+
198
+ def forward(self, input, label_pos, training_step, is_Train=False):
199
+ nT = self.max_text_length
200
+
201
+ b, c, h, w = input.shape
202
+ input = input.reshape(b, c, -1)
203
+ input = input.transpose(1, 2)
204
+
205
+ if is_Train:
206
+ if training_step == 'LF_1':
207
+ f_res = 0
208
+ f_sub = 0
209
+ input = self.SequenceModeling(input, src_mask=None)
210
+ text_pre, text_rem, text_mas = self.Prediction(input,
211
+ f_res,
212
+ f_sub,
213
+ is_Train=True,
214
+ use_mlm=False)
215
+ return text_pre, text_pre, text_pre
216
+ elif training_step == 'LF_2':
217
+ # MLM
218
+ f_res, f_sub, mask_c = self.MLM(input, label_pos)
219
+ input = self.SequenceModeling(input, src_mask=None)
220
+ text_pre, text_rem, text_mas = self.Prediction(input,
221
+ f_res,
222
+ f_sub,
223
+ is_Train=True)
224
+ return text_pre, text_rem, text_mas
225
+ elif training_step == 'LA':
226
+ # MLM
227
+ f_res, f_sub, mask_c = self.MLM(input, label_pos)
228
+ # use the mask_c (1 for occluded character and 0 for remaining characters) to occlude input
229
+ # ratio controls the occluded number in a batch
230
+ ratio = 2
231
+ character_mask = torch.zeros_like(mask_c)
232
+ character_mask[0:b // ratio, :, :] = mask_c[0:b // ratio, :, :]
233
+ input = input * (1 - character_mask.transpose(1, 2))
234
+ # VRM
235
+ # transformer unit for VRM
236
+ input = self.SequenceModeling(input, src_mask=None)
237
+ # prediction layer for MLM and VSR
238
+ text_pre, text_rem, text_mas = self.Prediction(input,
239
+ f_res,
240
+ f_sub,
241
+ is_Train=True)
242
+ return text_pre, text_rem, text_mas
243
+ else: # VRM is only used in the testing stage
244
+ f_res = 0
245
+ f_sub = 0
246
+ contextual_feature = self.SequenceModeling(input, src_mask=None)
247
+ C = self.Prediction(contextual_feature,
248
+ f_res,
249
+ f_sub,
250
+ is_Train=False,
251
+ use_mlm=False)
252
+ C = C.transpose(1, 0) # (25, b, 38))
253
+ out_res = torch.zeros(nT, b, self.nclass).type_as(input.data)
254
+
255
+ out_length = torch.zeros(b).type_as(input.data)
256
+ now_step = 0
257
+ while 0 in out_length and now_step < nT:
258
+ tmp_result = C[now_step, :, :]
259
+ out_res[now_step] = tmp_result
260
+ tmp_result = tmp_result.topk(1)[1].squeeze(dim=1)
261
+ for j in range(b):
262
+ if out_length[j] == 0 and tmp_result[j] == 0:
263
+ out_length[j] = now_step + 1
264
+ now_step += 1
265
+ for j in range(0, b):
266
+ if int(out_length[j]) == 0:
267
+ out_length[j] = nT
268
+ start = 0
269
+ output = torch.zeros(int(out_length.sum()),
270
+ self.nclass).type_as(input.data)
271
+ for i in range(0, b):
272
+ cur_length = int(out_length[i])
273
+ output[start:start + cur_length] = out_res[0:cur_length, i, :]
274
+ start += cur_length
275
+
276
+ return output, out_length
277
+
278
+
279
+ class VisionLANDecoder(nn.Module):
280
+
281
+ def __init__(
282
+ self,
283
+ in_channels,
284
+ out_channels,
285
+ n_head=None,
286
+ training_step='LA',
287
+ n_layers=3,
288
+ n_position=256,
289
+ max_text_length=25,
290
+ ):
291
+ super(VisionLANDecoder, self).__init__()
292
+ self.training_step = training_step
293
+ n_dim = in_channels
294
+ dim_feedforward = n_dim * 4
295
+ n_head = n_head if n_head is not None else n_dim // 32
296
+
297
+ self.MLM_VRM = MLM_VRM(
298
+ n_layers=n_layers,
299
+ n_position=n_position,
300
+ n_dim=n_dim,
301
+ n_head=n_head,
302
+ dim_feedforward=dim_feedforward,
303
+ max_text_length=max_text_length,
304
+ nclass=out_channels + 1,
305
+ )
306
+
307
+ def forward(self, x, data=None):
308
+ # MLM + VRM
309
+ if self.training:
310
+ label_pos = data[-2]
311
+ text_pre, text_rem, text_mas = self.MLM_VRM(x,
312
+ label_pos,
313
+ self.training_step,
314
+ is_Train=True)
315
+ return text_pre, text_rem, text_mas
316
+ else:
317
+ output, out_length = self.MLM_VRM(x,
318
+ None,
319
+ self.training_step,
320
+ is_Train=False)
321
+ return output, out_length
@@ -0,0 +1,39 @@
1
+ __all__ = ['build_encoder']
2
+
3
+
4
+ def build_encoder(config):
5
+ # from .rec_mobilenet_v3 import MobileNetV3
6
+ from .focalsvtr import FocalSVTR
7
+ from .rec_hgnet import PPHGNet_small
8
+ from .rec_lcnetv3 import PPLCNetV3
9
+ from .rec_mv1_enhance import MobileNetV1Enhance
10
+ from .rec_nrtr_mtb import MTB
11
+ from .rec_resnet_31 import ResNet31
12
+ from .rec_resnet_45 import ResNet45
13
+ from .rec_resnet_fpn import ResNet_FPN
14
+ from .rec_resnet_vd import ResNet
15
+ from .resnet31_rnn import ResNet_ASTER
16
+ from .svtrnet import SVTRNet
17
+ from .svtrnet2dpos import SVTRNet2DPos
18
+ from .svtrv2 import SVTRv2
19
+ from .svtrv2_lnconv import SVTRv2LNConv
20
+ from .svtrv2_lnconv_two33 import SVTRv2LNConvTwo33
21
+ from .vit import ViT
22
+ from .cam_encoder import CAMEncoder
23
+ from .convnextv2 import ConvNeXtV2
24
+ from .autostr_encoder import AutoSTREncoder
25
+ from .nrtr_encoder import NRTREncoder
26
+ from .repvit import RepSVTREncoder
27
+ support_dict = [
28
+ 'MobileNetV1Enhance', 'ResNet31', 'MobileNetV3', 'PPLCNetV3',
29
+ 'PPHGNet_small', 'ResNet', 'MTB', 'SVTRNet', 'ResNet45', 'ViT',
30
+ 'SVTRNet2DPos', 'SVTRv2', 'FocalSVTR', 'ResNet_FPN', 'ResNet_ASTER',
31
+ 'SVTRv2LNConv', 'SVTRv2LNConvTwo33', 'CAMEncoder', 'ConvNeXtV2',
32
+ 'AutoSTREncoder', 'NRTREncoder', 'RepSVTREncoder'
33
+ ]
34
+
35
+ module_name = config.pop('name')
36
+ assert module_name in support_dict, Exception(
37
+ 'when encoder of rec model only support {}'.format(support_dict))
38
+ module_class = eval(module_name)(**config)
39
+ return module_class