pyxllib 0.3.96__py3-none-any.whl → 0.3.200__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 (358) hide show
  1. pyxllib/__init__.py +21 -21
  2. pyxllib/algo/__init__.py +8 -8
  3. pyxllib/algo/disjoint.py +54 -54
  4. pyxllib/algo/geo.py +541 -529
  5. pyxllib/algo/intervals.py +964 -964
  6. pyxllib/algo/matcher.py +389 -311
  7. pyxllib/algo/newbie.py +166 -166
  8. pyxllib/algo/pupil.py +629 -461
  9. pyxllib/algo/shapelylib.py +67 -67
  10. pyxllib/algo/specialist.py +241 -240
  11. pyxllib/algo/stat.py +494 -458
  12. pyxllib/algo/treelib.py +149 -149
  13. pyxllib/algo/unitlib.py +66 -66
  14. {pyxlpr → pyxllib/autogui}/__init__.py +5 -5
  15. pyxllib/autogui/activewin.py +246 -0
  16. pyxllib/autogui/all.py +9 -0
  17. pyxllib/{ext/autogui → autogui}/autogui.py +852 -823
  18. pyxllib/autogui/uiautolib.py +362 -0
  19. pyxllib/{ext/autogui → autogui}/virtualkey.py +102 -102
  20. pyxllib/autogui/wechat.py +827 -0
  21. pyxllib/autogui/wechat_msg.py +421 -0
  22. pyxllib/autogui/wxautolib.py +84 -0
  23. pyxllib/cv/__init__.py +5 -5
  24. pyxllib/cv/expert.py +267 -267
  25. pyxllib/cv/imfile.py +159 -159
  26. pyxllib/cv/imhash.py +39 -39
  27. pyxllib/cv/pupil.py +9 -9
  28. pyxllib/cv/rgbfmt.py +1525 -1525
  29. pyxllib/cv/slidercaptcha.py +137 -0
  30. pyxllib/cv/trackbartools.py +251 -251
  31. pyxllib/cv/xlcvlib.py +1040 -1040
  32. pyxllib/cv/xlpillib.py +423 -423
  33. pyxllib/data/echarts.py +240 -129
  34. pyxllib/data/jsonlib.py +89 -0
  35. pyxllib/data/oss.py +72 -72
  36. pyxllib/data/pglib.py +1127 -643
  37. pyxllib/data/sqlite.py +568 -341
  38. pyxllib/data/sqllib.py +297 -297
  39. pyxllib/ext/JLineViewer.py +505 -492
  40. pyxllib/ext/__init__.py +6 -6
  41. pyxllib/ext/demolib.py +246 -246
  42. pyxllib/ext/drissionlib.py +277 -0
  43. pyxllib/ext/kq5034lib.py +12 -1606
  44. pyxllib/ext/old.py +663 -663
  45. pyxllib/ext/qt.py +449 -449
  46. pyxllib/ext/robustprocfile.py +497 -0
  47. pyxllib/ext/seleniumlib.py +76 -76
  48. pyxllib/ext/tk.py +173 -173
  49. pyxllib/ext/unixlib.py +827 -826
  50. pyxllib/ext/utools.py +351 -338
  51. pyxllib/ext/webhook.py +124 -101
  52. pyxllib/ext/win32lib.py +40 -40
  53. pyxllib/ext/wjxlib.py +88 -0
  54. pyxllib/ext/wpsapi.py +124 -0
  55. pyxllib/ext/xlwork.py +9 -0
  56. pyxllib/ext/yuquelib.py +1105 -173
  57. pyxllib/file/__init__.py +17 -17
  58. pyxllib/file/docxlib.py +761 -761
  59. pyxllib/file/gitlib.py +309 -309
  60. pyxllib/file/libreoffice.py +165 -0
  61. pyxllib/file/movielib.py +148 -139
  62. pyxllib/file/newbie.py +10 -10
  63. pyxllib/file/onenotelib.py +1469 -1469
  64. pyxllib/file/packlib/__init__.py +330 -293
  65. pyxllib/file/packlib/zipfile.py +2441 -2441
  66. pyxllib/file/pdflib.py +426 -426
  67. pyxllib/file/pupil.py +185 -185
  68. pyxllib/file/specialist/__init__.py +685 -685
  69. pyxllib/file/specialist/dirlib.py +799 -799
  70. pyxllib/file/specialist/download.py +193 -186
  71. pyxllib/file/specialist/filelib.py +2829 -2618
  72. pyxllib/file/xlsxlib.py +3131 -2976
  73. pyxllib/file/xlsyncfile.py +341 -0
  74. pyxllib/prog/__init__.py +5 -5
  75. pyxllib/prog/cachetools.py +64 -0
  76. pyxllib/prog/deprecatedlib.py +233 -233
  77. pyxllib/prog/filelock.py +42 -0
  78. pyxllib/prog/ipyexec.py +253 -253
  79. pyxllib/prog/multiprogs.py +940 -0
  80. pyxllib/prog/newbie.py +451 -444
  81. pyxllib/prog/pupil.py +1197 -1128
  82. pyxllib/prog/sitepackages.py +33 -33
  83. pyxllib/prog/specialist/__init__.py +391 -217
  84. pyxllib/prog/specialist/bc.py +203 -200
  85. pyxllib/prog/specialist/browser.py +497 -488
  86. pyxllib/prog/specialist/common.py +347 -347
  87. pyxllib/prog/specialist/datetime.py +199 -131
  88. pyxllib/prog/specialist/tictoc.py +240 -241
  89. pyxllib/prog/specialist/xllog.py +180 -180
  90. pyxllib/prog/xlosenv.py +108 -101
  91. pyxllib/stdlib/__init__.py +17 -17
  92. pyxllib/stdlib/tablepyxl/__init__.py +10 -10
  93. pyxllib/stdlib/tablepyxl/style.py +303 -303
  94. pyxllib/stdlib/tablepyxl/tablepyxl.py +130 -130
  95. pyxllib/text/__init__.py +8 -8
  96. pyxllib/text/ahocorasick.py +39 -39
  97. pyxllib/text/airscript.js +744 -0
  98. pyxllib/text/charclasslib.py +121 -109
  99. pyxllib/text/jiebalib.py +267 -264
  100. pyxllib/text/jinjalib.py +32 -0
  101. pyxllib/text/jsa_ai_prompt.md +271 -0
  102. pyxllib/text/jscode.py +922 -767
  103. pyxllib/text/latex/__init__.py +158 -158
  104. pyxllib/text/levenshtein.py +303 -303
  105. pyxllib/text/nestenv.py +1215 -1215
  106. pyxllib/text/newbie.py +300 -288
  107. pyxllib/text/pupil/__init__.py +8 -8
  108. pyxllib/text/pupil/common.py +1121 -1095
  109. pyxllib/text/pupil/xlalign.py +326 -326
  110. pyxllib/text/pycode.py +47 -47
  111. pyxllib/text/specialist/__init__.py +8 -8
  112. pyxllib/text/specialist/common.py +112 -112
  113. pyxllib/text/specialist/ptag.py +186 -186
  114. pyxllib/text/spellchecker.py +172 -172
  115. pyxllib/text/templates/echart_base.html +11 -0
  116. pyxllib/text/templates/highlight_code.html +17 -0
  117. pyxllib/text/templates/latex_editor.html +103 -0
  118. pyxllib/text/vbacode.py +17 -17
  119. pyxllib/text/xmllib.py +747 -685
  120. pyxllib/xl.py +42 -38
  121. pyxllib/xlcv.py +17 -17
  122. pyxllib-0.3.200.dist-info/METADATA +48 -0
  123. pyxllib-0.3.200.dist-info/RECORD +126 -0
  124. {pyxllib-0.3.96.dist-info → pyxllib-0.3.200.dist-info}/WHEEL +1 -2
  125. {pyxllib-0.3.96.dist-info → pyxllib-0.3.200.dist-info/licenses}/LICENSE +190 -190
  126. pyxllib/ext/autogui/__init__.py +0 -8
  127. pyxllib-0.3.96.dist-info/METADATA +0 -51
  128. pyxllib-0.3.96.dist-info/RECORD +0 -333
  129. pyxllib-0.3.96.dist-info/top_level.txt +0 -2
  130. pyxlpr/ai/__init__.py +0 -5
  131. pyxlpr/ai/clientlib.py +0 -1281
  132. pyxlpr/ai/specialist.py +0 -286
  133. pyxlpr/ai/torch_app.py +0 -172
  134. pyxlpr/ai/xlpaddle.py +0 -655
  135. pyxlpr/ai/xltorch.py +0 -705
  136. pyxlpr/data/__init__.py +0 -11
  137. pyxlpr/data/coco.py +0 -1325
  138. pyxlpr/data/datacls.py +0 -365
  139. pyxlpr/data/datasets.py +0 -200
  140. pyxlpr/data/gptlib.py +0 -1291
  141. pyxlpr/data/icdar/__init__.py +0 -96
  142. pyxlpr/data/icdar/deteval.py +0 -377
  143. pyxlpr/data/icdar/icdar2013.py +0 -341
  144. pyxlpr/data/icdar/iou.py +0 -340
  145. pyxlpr/data/icdar/rrc_evaluation_funcs_1_1.py +0 -463
  146. pyxlpr/data/imtextline.py +0 -473
  147. pyxlpr/data/labelme.py +0 -866
  148. pyxlpr/data/removeline.py +0 -179
  149. pyxlpr/data/specialist.py +0 -57
  150. pyxlpr/eval/__init__.py +0 -85
  151. pyxlpr/paddleocr.py +0 -776
  152. pyxlpr/ppocr/__init__.py +0 -15
  153. pyxlpr/ppocr/configs/rec/multi_language/generate_multi_language_configs.py +0 -226
  154. pyxlpr/ppocr/data/__init__.py +0 -135
  155. pyxlpr/ppocr/data/imaug/ColorJitter.py +0 -26
  156. pyxlpr/ppocr/data/imaug/__init__.py +0 -67
  157. pyxlpr/ppocr/data/imaug/copy_paste.py +0 -170
  158. pyxlpr/ppocr/data/imaug/east_process.py +0 -437
  159. pyxlpr/ppocr/data/imaug/gen_table_mask.py +0 -244
  160. pyxlpr/ppocr/data/imaug/iaa_augment.py +0 -114
  161. pyxlpr/ppocr/data/imaug/label_ops.py +0 -789
  162. pyxlpr/ppocr/data/imaug/make_border_map.py +0 -184
  163. pyxlpr/ppocr/data/imaug/make_pse_gt.py +0 -106
  164. pyxlpr/ppocr/data/imaug/make_shrink_map.py +0 -126
  165. pyxlpr/ppocr/data/imaug/operators.py +0 -433
  166. pyxlpr/ppocr/data/imaug/pg_process.py +0 -906
  167. pyxlpr/ppocr/data/imaug/randaugment.py +0 -143
  168. pyxlpr/ppocr/data/imaug/random_crop_data.py +0 -239
  169. pyxlpr/ppocr/data/imaug/rec_img_aug.py +0 -533
  170. pyxlpr/ppocr/data/imaug/sast_process.py +0 -777
  171. pyxlpr/ppocr/data/imaug/text_image_aug/__init__.py +0 -17
  172. pyxlpr/ppocr/data/imaug/text_image_aug/augment.py +0 -120
  173. pyxlpr/ppocr/data/imaug/text_image_aug/warp_mls.py +0 -168
  174. pyxlpr/ppocr/data/lmdb_dataset.py +0 -115
  175. pyxlpr/ppocr/data/pgnet_dataset.py +0 -104
  176. pyxlpr/ppocr/data/pubtab_dataset.py +0 -107
  177. pyxlpr/ppocr/data/simple_dataset.py +0 -372
  178. pyxlpr/ppocr/losses/__init__.py +0 -61
  179. pyxlpr/ppocr/losses/ace_loss.py +0 -52
  180. pyxlpr/ppocr/losses/basic_loss.py +0 -135
  181. pyxlpr/ppocr/losses/center_loss.py +0 -88
  182. pyxlpr/ppocr/losses/cls_loss.py +0 -30
  183. pyxlpr/ppocr/losses/combined_loss.py +0 -67
  184. pyxlpr/ppocr/losses/det_basic_loss.py +0 -208
  185. pyxlpr/ppocr/losses/det_db_loss.py +0 -80
  186. pyxlpr/ppocr/losses/det_east_loss.py +0 -63
  187. pyxlpr/ppocr/losses/det_pse_loss.py +0 -149
  188. pyxlpr/ppocr/losses/det_sast_loss.py +0 -121
  189. pyxlpr/ppocr/losses/distillation_loss.py +0 -272
  190. pyxlpr/ppocr/losses/e2e_pg_loss.py +0 -140
  191. pyxlpr/ppocr/losses/kie_sdmgr_loss.py +0 -113
  192. pyxlpr/ppocr/losses/rec_aster_loss.py +0 -99
  193. pyxlpr/ppocr/losses/rec_att_loss.py +0 -39
  194. pyxlpr/ppocr/losses/rec_ctc_loss.py +0 -44
  195. pyxlpr/ppocr/losses/rec_enhanced_ctc_loss.py +0 -70
  196. pyxlpr/ppocr/losses/rec_nrtr_loss.py +0 -30
  197. pyxlpr/ppocr/losses/rec_sar_loss.py +0 -28
  198. pyxlpr/ppocr/losses/rec_srn_loss.py +0 -47
  199. pyxlpr/ppocr/losses/table_att_loss.py +0 -109
  200. pyxlpr/ppocr/metrics/__init__.py +0 -44
  201. pyxlpr/ppocr/metrics/cls_metric.py +0 -45
  202. pyxlpr/ppocr/metrics/det_metric.py +0 -82
  203. pyxlpr/ppocr/metrics/distillation_metric.py +0 -73
  204. pyxlpr/ppocr/metrics/e2e_metric.py +0 -86
  205. pyxlpr/ppocr/metrics/eval_det_iou.py +0 -274
  206. pyxlpr/ppocr/metrics/kie_metric.py +0 -70
  207. pyxlpr/ppocr/metrics/rec_metric.py +0 -75
  208. pyxlpr/ppocr/metrics/table_metric.py +0 -50
  209. pyxlpr/ppocr/modeling/architectures/__init__.py +0 -32
  210. pyxlpr/ppocr/modeling/architectures/base_model.py +0 -88
  211. pyxlpr/ppocr/modeling/architectures/distillation_model.py +0 -60
  212. pyxlpr/ppocr/modeling/backbones/__init__.py +0 -54
  213. pyxlpr/ppocr/modeling/backbones/det_mobilenet_v3.py +0 -268
  214. pyxlpr/ppocr/modeling/backbones/det_resnet_vd.py +0 -246
  215. pyxlpr/ppocr/modeling/backbones/det_resnet_vd_sast.py +0 -285
  216. pyxlpr/ppocr/modeling/backbones/e2e_resnet_vd_pg.py +0 -265
  217. pyxlpr/ppocr/modeling/backbones/kie_unet_sdmgr.py +0 -186
  218. pyxlpr/ppocr/modeling/backbones/rec_mobilenet_v3.py +0 -138
  219. pyxlpr/ppocr/modeling/backbones/rec_mv1_enhance.py +0 -258
  220. pyxlpr/ppocr/modeling/backbones/rec_nrtr_mtb.py +0 -48
  221. pyxlpr/ppocr/modeling/backbones/rec_resnet_31.py +0 -210
  222. pyxlpr/ppocr/modeling/backbones/rec_resnet_aster.py +0 -143
  223. pyxlpr/ppocr/modeling/backbones/rec_resnet_fpn.py +0 -307
  224. pyxlpr/ppocr/modeling/backbones/rec_resnet_vd.py +0 -286
  225. pyxlpr/ppocr/modeling/heads/__init__.py +0 -54
  226. pyxlpr/ppocr/modeling/heads/cls_head.py +0 -52
  227. pyxlpr/ppocr/modeling/heads/det_db_head.py +0 -118
  228. pyxlpr/ppocr/modeling/heads/det_east_head.py +0 -121
  229. pyxlpr/ppocr/modeling/heads/det_pse_head.py +0 -37
  230. pyxlpr/ppocr/modeling/heads/det_sast_head.py +0 -128
  231. pyxlpr/ppocr/modeling/heads/e2e_pg_head.py +0 -253
  232. pyxlpr/ppocr/modeling/heads/kie_sdmgr_head.py +0 -206
  233. pyxlpr/ppocr/modeling/heads/multiheadAttention.py +0 -163
  234. pyxlpr/ppocr/modeling/heads/rec_aster_head.py +0 -393
  235. pyxlpr/ppocr/modeling/heads/rec_att_head.py +0 -202
  236. pyxlpr/ppocr/modeling/heads/rec_ctc_head.py +0 -88
  237. pyxlpr/ppocr/modeling/heads/rec_nrtr_head.py +0 -826
  238. pyxlpr/ppocr/modeling/heads/rec_sar_head.py +0 -402
  239. pyxlpr/ppocr/modeling/heads/rec_srn_head.py +0 -280
  240. pyxlpr/ppocr/modeling/heads/self_attention.py +0 -406
  241. pyxlpr/ppocr/modeling/heads/table_att_head.py +0 -246
  242. pyxlpr/ppocr/modeling/necks/__init__.py +0 -32
  243. pyxlpr/ppocr/modeling/necks/db_fpn.py +0 -111
  244. pyxlpr/ppocr/modeling/necks/east_fpn.py +0 -188
  245. pyxlpr/ppocr/modeling/necks/fpn.py +0 -138
  246. pyxlpr/ppocr/modeling/necks/pg_fpn.py +0 -314
  247. pyxlpr/ppocr/modeling/necks/rnn.py +0 -92
  248. pyxlpr/ppocr/modeling/necks/sast_fpn.py +0 -284
  249. pyxlpr/ppocr/modeling/necks/table_fpn.py +0 -110
  250. pyxlpr/ppocr/modeling/transforms/__init__.py +0 -28
  251. pyxlpr/ppocr/modeling/transforms/stn.py +0 -135
  252. pyxlpr/ppocr/modeling/transforms/tps.py +0 -308
  253. pyxlpr/ppocr/modeling/transforms/tps_spatial_transformer.py +0 -156
  254. pyxlpr/ppocr/optimizer/__init__.py +0 -61
  255. pyxlpr/ppocr/optimizer/learning_rate.py +0 -228
  256. pyxlpr/ppocr/optimizer/lr_scheduler.py +0 -49
  257. pyxlpr/ppocr/optimizer/optimizer.py +0 -160
  258. pyxlpr/ppocr/optimizer/regularizer.py +0 -52
  259. pyxlpr/ppocr/postprocess/__init__.py +0 -55
  260. pyxlpr/ppocr/postprocess/cls_postprocess.py +0 -33
  261. pyxlpr/ppocr/postprocess/db_postprocess.py +0 -234
  262. pyxlpr/ppocr/postprocess/east_postprocess.py +0 -143
  263. pyxlpr/ppocr/postprocess/locality_aware_nms.py +0 -200
  264. pyxlpr/ppocr/postprocess/pg_postprocess.py +0 -52
  265. pyxlpr/ppocr/postprocess/pse_postprocess/__init__.py +0 -15
  266. pyxlpr/ppocr/postprocess/pse_postprocess/pse/__init__.py +0 -29
  267. pyxlpr/ppocr/postprocess/pse_postprocess/pse/setup.py +0 -14
  268. pyxlpr/ppocr/postprocess/pse_postprocess/pse_postprocess.py +0 -118
  269. pyxlpr/ppocr/postprocess/rec_postprocess.py +0 -654
  270. pyxlpr/ppocr/postprocess/sast_postprocess.py +0 -355
  271. pyxlpr/ppocr/tools/__init__.py +0 -14
  272. pyxlpr/ppocr/tools/eval.py +0 -83
  273. pyxlpr/ppocr/tools/export_center.py +0 -77
  274. pyxlpr/ppocr/tools/export_model.py +0 -129
  275. pyxlpr/ppocr/tools/infer/predict_cls.py +0 -151
  276. pyxlpr/ppocr/tools/infer/predict_det.py +0 -300
  277. pyxlpr/ppocr/tools/infer/predict_e2e.py +0 -169
  278. pyxlpr/ppocr/tools/infer/predict_rec.py +0 -414
  279. pyxlpr/ppocr/tools/infer/predict_system.py +0 -204
  280. pyxlpr/ppocr/tools/infer/utility.py +0 -629
  281. pyxlpr/ppocr/tools/infer_cls.py +0 -83
  282. pyxlpr/ppocr/tools/infer_det.py +0 -134
  283. pyxlpr/ppocr/tools/infer_e2e.py +0 -122
  284. pyxlpr/ppocr/tools/infer_kie.py +0 -153
  285. pyxlpr/ppocr/tools/infer_rec.py +0 -146
  286. pyxlpr/ppocr/tools/infer_table.py +0 -107
  287. pyxlpr/ppocr/tools/program.py +0 -596
  288. pyxlpr/ppocr/tools/test_hubserving.py +0 -117
  289. pyxlpr/ppocr/tools/train.py +0 -163
  290. pyxlpr/ppocr/tools/xlprog.py +0 -748
  291. pyxlpr/ppocr/utils/EN_symbol_dict.txt +0 -94
  292. pyxlpr/ppocr/utils/__init__.py +0 -24
  293. pyxlpr/ppocr/utils/dict/ar_dict.txt +0 -117
  294. pyxlpr/ppocr/utils/dict/arabic_dict.txt +0 -162
  295. pyxlpr/ppocr/utils/dict/be_dict.txt +0 -145
  296. pyxlpr/ppocr/utils/dict/bg_dict.txt +0 -140
  297. pyxlpr/ppocr/utils/dict/chinese_cht_dict.txt +0 -8421
  298. pyxlpr/ppocr/utils/dict/cyrillic_dict.txt +0 -163
  299. pyxlpr/ppocr/utils/dict/devanagari_dict.txt +0 -167
  300. pyxlpr/ppocr/utils/dict/en_dict.txt +0 -63
  301. pyxlpr/ppocr/utils/dict/fa_dict.txt +0 -136
  302. pyxlpr/ppocr/utils/dict/french_dict.txt +0 -136
  303. pyxlpr/ppocr/utils/dict/german_dict.txt +0 -143
  304. pyxlpr/ppocr/utils/dict/hi_dict.txt +0 -162
  305. pyxlpr/ppocr/utils/dict/it_dict.txt +0 -118
  306. pyxlpr/ppocr/utils/dict/japan_dict.txt +0 -4399
  307. pyxlpr/ppocr/utils/dict/ka_dict.txt +0 -153
  308. pyxlpr/ppocr/utils/dict/korean_dict.txt +0 -3688
  309. pyxlpr/ppocr/utils/dict/latin_dict.txt +0 -185
  310. pyxlpr/ppocr/utils/dict/mr_dict.txt +0 -153
  311. pyxlpr/ppocr/utils/dict/ne_dict.txt +0 -153
  312. pyxlpr/ppocr/utils/dict/oc_dict.txt +0 -96
  313. pyxlpr/ppocr/utils/dict/pu_dict.txt +0 -130
  314. pyxlpr/ppocr/utils/dict/rs_dict.txt +0 -91
  315. pyxlpr/ppocr/utils/dict/rsc_dict.txt +0 -134
  316. pyxlpr/ppocr/utils/dict/ru_dict.txt +0 -125
  317. pyxlpr/ppocr/utils/dict/ta_dict.txt +0 -128
  318. pyxlpr/ppocr/utils/dict/table_dict.txt +0 -277
  319. pyxlpr/ppocr/utils/dict/table_structure_dict.txt +0 -2759
  320. pyxlpr/ppocr/utils/dict/te_dict.txt +0 -151
  321. pyxlpr/ppocr/utils/dict/ug_dict.txt +0 -114
  322. pyxlpr/ppocr/utils/dict/uk_dict.txt +0 -142
  323. pyxlpr/ppocr/utils/dict/ur_dict.txt +0 -137
  324. pyxlpr/ppocr/utils/dict/xi_dict.txt +0 -110
  325. pyxlpr/ppocr/utils/dict90.txt +0 -90
  326. pyxlpr/ppocr/utils/e2e_metric/Deteval.py +0 -574
  327. pyxlpr/ppocr/utils/e2e_metric/polygon_fast.py +0 -83
  328. pyxlpr/ppocr/utils/e2e_utils/extract_batchsize.py +0 -87
  329. pyxlpr/ppocr/utils/e2e_utils/extract_textpoint_fast.py +0 -457
  330. pyxlpr/ppocr/utils/e2e_utils/extract_textpoint_slow.py +0 -592
  331. pyxlpr/ppocr/utils/e2e_utils/pgnet_pp_utils.py +0 -162
  332. pyxlpr/ppocr/utils/e2e_utils/visual.py +0 -162
  333. pyxlpr/ppocr/utils/en_dict.txt +0 -95
  334. pyxlpr/ppocr/utils/gen_label.py +0 -81
  335. pyxlpr/ppocr/utils/ic15_dict.txt +0 -36
  336. pyxlpr/ppocr/utils/iou.py +0 -54
  337. pyxlpr/ppocr/utils/logging.py +0 -69
  338. pyxlpr/ppocr/utils/network.py +0 -84
  339. pyxlpr/ppocr/utils/ppocr_keys_v1.txt +0 -6623
  340. pyxlpr/ppocr/utils/profiler.py +0 -110
  341. pyxlpr/ppocr/utils/save_load.py +0 -150
  342. pyxlpr/ppocr/utils/stats.py +0 -72
  343. pyxlpr/ppocr/utils/utility.py +0 -80
  344. pyxlpr/ppstructure/__init__.py +0 -13
  345. pyxlpr/ppstructure/predict_system.py +0 -187
  346. pyxlpr/ppstructure/table/__init__.py +0 -13
  347. pyxlpr/ppstructure/table/eval_table.py +0 -72
  348. pyxlpr/ppstructure/table/matcher.py +0 -192
  349. pyxlpr/ppstructure/table/predict_structure.py +0 -136
  350. pyxlpr/ppstructure/table/predict_table.py +0 -221
  351. pyxlpr/ppstructure/table/table_metric/__init__.py +0 -16
  352. pyxlpr/ppstructure/table/table_metric/parallel.py +0 -51
  353. pyxlpr/ppstructure/table/table_metric/table_metric.py +0 -247
  354. pyxlpr/ppstructure/table/tablepyxl/__init__.py +0 -13
  355. pyxlpr/ppstructure/table/tablepyxl/style.py +0 -283
  356. pyxlpr/ppstructure/table/tablepyxl/tablepyxl.py +0 -118
  357. pyxlpr/ppstructure/utility.py +0 -71
  358. pyxlpr/xlai.py +0 -10
@@ -1,826 +0,0 @@
1
- # copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- import math
16
- import paddle
17
- import copy
18
- from paddle import nn
19
- import paddle.nn.functional as F
20
- from paddle.nn import LayerList
21
- from paddle.nn.initializer import XavierNormal as xavier_uniform_
22
- from paddle.nn import Dropout, Linear, LayerNorm, Conv2D
23
- import numpy as np
24
- from pyxlpr.ppocr.modeling.heads.multiheadAttention import MultiheadAttention
25
- from paddle.nn.initializer import Constant as constant_
26
- from paddle.nn.initializer import XavierNormal as xavier_normal_
27
-
28
- zeros_ = constant_(value=0.)
29
- ones_ = constant_(value=1.)
30
-
31
-
32
- class Transformer(nn.Layer):
33
- """A transformer model. User is able to modify the attributes as needed. The architechture
34
- is based on the paper "Attention Is All You Need". Ashish Vaswani, Noam Shazeer,
35
- Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez, Lukasz Kaiser, and
36
- Illia Polosukhin. 2017. Attention is all you need. In Advances in Neural Information
37
- Processing Systems, pages 6000-6010.
38
-
39
- Args:
40
- d_model: the number of expected features in the encoder/decoder inputs (default=512).
41
- nhead: the number of heads in the multiheadattention models (default=8).
42
- num_encoder_layers: the number of sub-encoder-layers in the encoder (default=6).
43
- num_decoder_layers: the number of sub-decoder-layers in the decoder (default=6).
44
- dim_feedforward: the dimension of the feedforward network model (default=2048).
45
- dropout: the dropout value (default=0.1).
46
- custom_encoder: custom encoder (default=None).
47
- custom_decoder: custom decoder (default=None).
48
-
49
- """
50
-
51
- def __init__(self,
52
- d_model=512,
53
- nhead=8,
54
- num_encoder_layers=6,
55
- beam_size=0,
56
- num_decoder_layers=6,
57
- dim_feedforward=1024,
58
- attention_dropout_rate=0.0,
59
- residual_dropout_rate=0.1,
60
- custom_encoder=None,
61
- custom_decoder=None,
62
- in_channels=0,
63
- out_channels=0,
64
- scale_embedding=True):
65
- super(Transformer, self).__init__()
66
- self.out_channels = out_channels + 1
67
- self.embedding = Embeddings(
68
- d_model=d_model,
69
- vocab=self.out_channels,
70
- padding_idx=0,
71
- scale_embedding=scale_embedding)
72
- self.positional_encoding = PositionalEncoding(
73
- dropout=residual_dropout_rate,
74
- dim=d_model, )
75
- if custom_encoder is not None:
76
- self.encoder = custom_encoder
77
- else:
78
- if num_encoder_layers > 0:
79
- encoder_layer = TransformerEncoderLayer(
80
- d_model, nhead, dim_feedforward, attention_dropout_rate,
81
- residual_dropout_rate)
82
- self.encoder = TransformerEncoder(encoder_layer,
83
- num_encoder_layers)
84
- else:
85
- self.encoder = None
86
-
87
- if custom_decoder is not None:
88
- self.decoder = custom_decoder
89
- else:
90
- decoder_layer = TransformerDecoderLayer(
91
- d_model, nhead, dim_feedforward, attention_dropout_rate,
92
- residual_dropout_rate)
93
- self.decoder = TransformerDecoder(decoder_layer, num_decoder_layers)
94
-
95
- self._reset_parameters()
96
- self.beam_size = beam_size
97
- self.d_model = d_model
98
- self.nhead = nhead
99
- self.tgt_word_prj = nn.Linear(
100
- d_model, self.out_channels, bias_attr=False)
101
- w0 = np.random.normal(0.0, d_model**-0.5,
102
- (d_model, self.out_channels)).astype(np.float32)
103
- self.tgt_word_prj.weight.set_value(w0)
104
- self.apply(self._init_weights)
105
-
106
- def _init_weights(self, m):
107
-
108
- if isinstance(m, nn.Conv2D):
109
- xavier_normal_(m.weight)
110
- if m.bias is not None:
111
- zeros_(m.bias)
112
-
113
- def forward_train(self, src, tgt):
114
- tgt = tgt[:, :-1]
115
-
116
- tgt_key_padding_mask = self.generate_padding_mask(tgt)
117
- tgt = self.embedding(tgt).transpose([1, 0, 2])
118
- tgt = self.positional_encoding(tgt)
119
- tgt_mask = self.generate_square_subsequent_mask(tgt.shape[0])
120
-
121
- if self.encoder is not None:
122
- src = self.positional_encoding(src.transpose([1, 0, 2]))
123
- memory = self.encoder(src)
124
- else:
125
- memory = src.squeeze(2).transpose([2, 0, 1])
126
- output = self.decoder(
127
- tgt,
128
- memory,
129
- tgt_mask=tgt_mask,
130
- memory_mask=None,
131
- tgt_key_padding_mask=tgt_key_padding_mask,
132
- memory_key_padding_mask=None)
133
- output = output.transpose([1, 0, 2])
134
- logit = self.tgt_word_prj(output)
135
- return logit
136
-
137
- def forward(self, src, targets=None):
138
- """Take in and process masked source/target sequences.
139
- Args:
140
- src: the sequence to the encoder (required).
141
- tgt: the sequence to the decoder (required).
142
- Shape:
143
- - src: :math:`(S, N, E)`.
144
- - tgt: :math:`(T, N, E)`.
145
- Examples:
146
- >>> output = transformer_model(src, tgt)
147
- """
148
-
149
- if self.training:
150
- max_len = targets[1].max()
151
- tgt = targets[0][:, :2 + max_len]
152
- return self.forward_train(src, tgt)
153
- else:
154
- if self.beam_size > 0:
155
- return self.forward_beam(src)
156
- else:
157
- return self.forward_test(src)
158
-
159
- def forward_test(self, src):
160
- bs = paddle.shape(src)[0]
161
- if self.encoder is not None:
162
- src = self.positional_encoding(paddle.transpose(src, [1, 0, 2]))
163
- memory = self.encoder(src)
164
- else:
165
- memory = paddle.transpose(paddle.squeeze(src, 2), [2, 0, 1])
166
- dec_seq = paddle.full((bs, 1), 2, dtype=paddle.int64)
167
- dec_prob = paddle.full((bs, 1), 1., dtype=paddle.float32)
168
- for len_dec_seq in range(1, 25):
169
- dec_seq_embed = paddle.transpose(self.embedding(dec_seq), [1, 0, 2])
170
- dec_seq_embed = self.positional_encoding(dec_seq_embed)
171
- tgt_mask = self.generate_square_subsequent_mask(
172
- paddle.shape(dec_seq_embed)[0])
173
- output = self.decoder(
174
- dec_seq_embed,
175
- memory,
176
- tgt_mask=tgt_mask,
177
- memory_mask=None,
178
- tgt_key_padding_mask=None,
179
- memory_key_padding_mask=None)
180
- dec_output = paddle.transpose(output, [1, 0, 2])
181
- dec_output = dec_output[:, -1, :]
182
- word_prob = F.softmax(self.tgt_word_prj(dec_output), axis=1)
183
- preds_idx = paddle.argmax(word_prob, axis=1)
184
- if paddle.equal_all(
185
- preds_idx,
186
- paddle.full(
187
- paddle.shape(preds_idx), 3, dtype='int64')):
188
- break
189
- preds_prob = paddle.max(word_prob, axis=1)
190
- dec_seq = paddle.concat(
191
- [dec_seq, paddle.reshape(preds_idx, [-1, 1])], axis=1)
192
- dec_prob = paddle.concat(
193
- [dec_prob, paddle.reshape(preds_prob, [-1, 1])], axis=1)
194
- return [dec_seq, dec_prob]
195
-
196
- def forward_beam(self, images):
197
- ''' Translation work in one batch '''
198
-
199
- def get_inst_idx_to_tensor_position_map(inst_idx_list):
200
- ''' Indicate the position of an instance in a tensor. '''
201
- return {
202
- inst_idx: tensor_position
203
- for tensor_position, inst_idx in enumerate(inst_idx_list)
204
- }
205
-
206
- def collect_active_part(beamed_tensor, curr_active_inst_idx,
207
- n_prev_active_inst, n_bm):
208
- ''' Collect tensor parts associated to active instances. '''
209
-
210
- beamed_tensor_shape = paddle.shape(beamed_tensor)
211
- n_curr_active_inst = len(curr_active_inst_idx)
212
- new_shape = (n_curr_active_inst * n_bm, beamed_tensor_shape[1],
213
- beamed_tensor_shape[2])
214
-
215
- beamed_tensor = beamed_tensor.reshape([n_prev_active_inst, -1])
216
- beamed_tensor = beamed_tensor.index_select(
217
- curr_active_inst_idx, axis=0)
218
- beamed_tensor = beamed_tensor.reshape(new_shape)
219
-
220
- return beamed_tensor
221
-
222
- def collate_active_info(src_enc, inst_idx_to_position_map,
223
- active_inst_idx_list):
224
- # Sentences which are still active are collected,
225
- # so the decoder will not run on completed sentences.
226
-
227
- n_prev_active_inst = len(inst_idx_to_position_map)
228
- active_inst_idx = [
229
- inst_idx_to_position_map[k] for k in active_inst_idx_list
230
- ]
231
- active_inst_idx = paddle.to_tensor(active_inst_idx, dtype='int64')
232
- active_src_enc = collect_active_part(
233
- src_enc.transpose([1, 0, 2]), active_inst_idx,
234
- n_prev_active_inst, n_bm).transpose([1, 0, 2])
235
- active_inst_idx_to_position_map = get_inst_idx_to_tensor_position_map(
236
- active_inst_idx_list)
237
- return active_src_enc, active_inst_idx_to_position_map
238
-
239
- def beam_decode_step(inst_dec_beams, len_dec_seq, enc_output,
240
- inst_idx_to_position_map, n_bm,
241
- memory_key_padding_mask):
242
- ''' Decode and update beam status, and then return active beam idx '''
243
-
244
- def prepare_beam_dec_seq(inst_dec_beams, len_dec_seq):
245
- dec_partial_seq = [
246
- b.get_current_state() for b in inst_dec_beams if not b.done
247
- ]
248
- dec_partial_seq = paddle.stack(dec_partial_seq)
249
- dec_partial_seq = dec_partial_seq.reshape([-1, len_dec_seq])
250
- return dec_partial_seq
251
-
252
- def predict_word(dec_seq, enc_output, n_active_inst, n_bm,
253
- memory_key_padding_mask):
254
- dec_seq = paddle.transpose(self.embedding(dec_seq), [1, 0, 2])
255
- dec_seq = self.positional_encoding(dec_seq)
256
- tgt_mask = self.generate_square_subsequent_mask(
257
- paddle.shape(dec_seq)[0])
258
- dec_output = self.decoder(
259
- dec_seq,
260
- enc_output,
261
- tgt_mask=tgt_mask,
262
- tgt_key_padding_mask=None,
263
- memory_key_padding_mask=memory_key_padding_mask, )
264
- dec_output = paddle.transpose(dec_output, [1, 0, 2])
265
- dec_output = dec_output[:,
266
- -1, :] # Pick the last step: (bh * bm) * d_h
267
- word_prob = F.softmax(self.tgt_word_prj(dec_output), axis=1)
268
- word_prob = paddle.reshape(word_prob, [n_active_inst, n_bm, -1])
269
- return word_prob
270
-
271
- def collect_active_inst_idx_list(inst_beams, word_prob,
272
- inst_idx_to_position_map):
273
- active_inst_idx_list = []
274
- for inst_idx, inst_position in inst_idx_to_position_map.items():
275
- is_inst_complete = inst_beams[inst_idx].advance(word_prob[
276
- inst_position])
277
- if not is_inst_complete:
278
- active_inst_idx_list += [inst_idx]
279
-
280
- return active_inst_idx_list
281
-
282
- n_active_inst = len(inst_idx_to_position_map)
283
- dec_seq = prepare_beam_dec_seq(inst_dec_beams, len_dec_seq)
284
- word_prob = predict_word(dec_seq, enc_output, n_active_inst, n_bm,
285
- None)
286
- # Update the beam with predicted word prob information and collect incomplete instances
287
- active_inst_idx_list = collect_active_inst_idx_list(
288
- inst_dec_beams, word_prob, inst_idx_to_position_map)
289
- return active_inst_idx_list
290
-
291
- def collect_hypothesis_and_scores(inst_dec_beams, n_best):
292
- all_hyp, all_scores = [], []
293
- for inst_idx in range(len(inst_dec_beams)):
294
- scores, tail_idxs = inst_dec_beams[inst_idx].sort_scores()
295
- all_scores += [scores[:n_best]]
296
- hyps = [
297
- inst_dec_beams[inst_idx].get_hypothesis(i)
298
- for i in tail_idxs[:n_best]
299
- ]
300
- all_hyp += [hyps]
301
- return all_hyp, all_scores
302
-
303
- with paddle.no_grad():
304
- #-- Encode
305
- if self.encoder is not None:
306
- src = self.positional_encoding(images.transpose([1, 0, 2]))
307
- src_enc = self.encoder(src)
308
- else:
309
- src_enc = images.squeeze(2).transpose([0, 2, 1])
310
-
311
- n_bm = self.beam_size
312
- src_shape = paddle.shape(src_enc)
313
- inst_dec_beams = [Beam(n_bm) for _ in range(1)]
314
- active_inst_idx_list = list(range(1))
315
- # Repeat data for beam search
316
- src_enc = paddle.tile(src_enc, [1, n_bm, 1])
317
- inst_idx_to_position_map = get_inst_idx_to_tensor_position_map(
318
- active_inst_idx_list)
319
- # Decode
320
- for len_dec_seq in range(1, 25):
321
- src_enc_copy = src_enc.clone()
322
- active_inst_idx_list = beam_decode_step(
323
- inst_dec_beams, len_dec_seq, src_enc_copy,
324
- inst_idx_to_position_map, n_bm, None)
325
- if not active_inst_idx_list:
326
- break # all instances have finished their path to <EOS>
327
- src_enc, inst_idx_to_position_map = collate_active_info(
328
- src_enc_copy, inst_idx_to_position_map,
329
- active_inst_idx_list)
330
- batch_hyp, batch_scores = collect_hypothesis_and_scores(inst_dec_beams,
331
- 1)
332
- result_hyp = []
333
- hyp_scores = []
334
- for bs_hyp, score in zip(batch_hyp, batch_scores):
335
- l = len(bs_hyp[0])
336
- bs_hyp_pad = bs_hyp[0] + [3] * (25 - l)
337
- result_hyp.append(bs_hyp_pad)
338
- score = float(score) / l
339
- hyp_score = [score for _ in range(25)]
340
- hyp_scores.append(hyp_score)
341
- return [
342
- paddle.to_tensor(
343
- np.array(result_hyp), dtype=paddle.int64),
344
- paddle.to_tensor(hyp_scores)
345
- ]
346
-
347
- def generate_square_subsequent_mask(self, sz):
348
- """Generate a square mask for the sequence. The masked positions are filled with float('-inf').
349
- Unmasked positions are filled with float(0.0).
350
- """
351
- mask = paddle.zeros([sz, sz], dtype='float32')
352
- mask_inf = paddle.triu(
353
- paddle.full(
354
- shape=[sz, sz], dtype='float32', fill_value='-inf'),
355
- diagonal=1)
356
- mask = mask + mask_inf
357
- return mask
358
-
359
- def generate_padding_mask(self, x):
360
- padding_mask = paddle.equal(x, paddle.to_tensor(0, dtype=x.dtype))
361
- return padding_mask
362
-
363
- def _reset_parameters(self):
364
- """Initiate parameters in the transformer model."""
365
-
366
- for p in self.parameters():
367
- if p.dim() > 1:
368
- xavier_uniform_(p)
369
-
370
-
371
- class TransformerEncoder(nn.Layer):
372
- """TransformerEncoder is a stack of N encoder layers
373
- Args:
374
- encoder_layer: an instance of the TransformerEncoderLayer() class (required).
375
- num_layers: the number of sub-encoder-layers in the encoder (required).
376
- norm: the layer normalization component (optional).
377
- """
378
-
379
- def __init__(self, encoder_layer, num_layers):
380
- super(TransformerEncoder, self).__init__()
381
- self.layers = _get_clones(encoder_layer, num_layers)
382
- self.num_layers = num_layers
383
-
384
- def forward(self, src):
385
- """Pass the input through the endocder layers in turn.
386
- Args:
387
- src: the sequnce to the encoder (required).
388
- mask: the mask for the src sequence (optional).
389
- src_key_padding_mask: the mask for the src keys per batch (optional).
390
- """
391
- output = src
392
-
393
- for i in range(self.num_layers):
394
- output = self.layers[i](output,
395
- src_mask=None,
396
- src_key_padding_mask=None)
397
-
398
- return output
399
-
400
-
401
- class TransformerDecoder(nn.Layer):
402
- """TransformerDecoder is a stack of N decoder layers
403
-
404
- Args:
405
- decoder_layer: an instance of the TransformerDecoderLayer() class (required).
406
- num_layers: the number of sub-decoder-layers in the decoder (required).
407
- norm: the layer normalization component (optional).
408
-
409
- """
410
-
411
- def __init__(self, decoder_layer, num_layers):
412
- super(TransformerDecoder, self).__init__()
413
- self.layers = _get_clones(decoder_layer, num_layers)
414
- self.num_layers = num_layers
415
-
416
- def forward(self,
417
- tgt,
418
- memory,
419
- tgt_mask=None,
420
- memory_mask=None,
421
- tgt_key_padding_mask=None,
422
- memory_key_padding_mask=None):
423
- """Pass the inputs (and mask) through the decoder layer in turn.
424
-
425
- Args:
426
- tgt: the sequence to the decoder (required).
427
- memory: the sequnce from the last layer of the encoder (required).
428
- tgt_mask: the mask for the tgt sequence (optional).
429
- memory_mask: the mask for the memory sequence (optional).
430
- tgt_key_padding_mask: the mask for the tgt keys per batch (optional).
431
- memory_key_padding_mask: the mask for the memory keys per batch (optional).
432
- """
433
- output = tgt
434
- for i in range(self.num_layers):
435
- output = self.layers[i](
436
- output,
437
- memory,
438
- tgt_mask=tgt_mask,
439
- memory_mask=memory_mask,
440
- tgt_key_padding_mask=tgt_key_padding_mask,
441
- memory_key_padding_mask=memory_key_padding_mask)
442
-
443
- return output
444
-
445
-
446
- class TransformerEncoderLayer(nn.Layer):
447
- """TransformerEncoderLayer is made up of self-attn and feedforward network.
448
- This standard encoder layer is based on the paper "Attention Is All You Need".
449
- Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez,
450
- Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in
451
- Neural Information Processing Systems, pages 6000-6010. Users may modify or implement
452
- in a different way during application.
453
-
454
- Args:
455
- d_model: the number of expected features in the input (required).
456
- nhead: the number of heads in the multiheadattention models (required).
457
- dim_feedforward: the dimension of the feedforward network model (default=2048).
458
- dropout: the dropout value (default=0.1).
459
-
460
- """
461
-
462
- def __init__(self,
463
- d_model,
464
- nhead,
465
- dim_feedforward=2048,
466
- attention_dropout_rate=0.0,
467
- residual_dropout_rate=0.1):
468
- super(TransformerEncoderLayer, self).__init__()
469
- self.self_attn = MultiheadAttention(
470
- d_model, nhead, dropout=attention_dropout_rate)
471
-
472
- self.conv1 = Conv2D(
473
- in_channels=d_model,
474
- out_channels=dim_feedforward,
475
- kernel_size=(1, 1))
476
- self.conv2 = Conv2D(
477
- in_channels=dim_feedforward,
478
- out_channels=d_model,
479
- kernel_size=(1, 1))
480
-
481
- self.norm1 = LayerNorm(d_model)
482
- self.norm2 = LayerNorm(d_model)
483
- self.dropout1 = Dropout(residual_dropout_rate)
484
- self.dropout2 = Dropout(residual_dropout_rate)
485
-
486
- def forward(self, src, src_mask=None, src_key_padding_mask=None):
487
- """Pass the input through the endocder layer.
488
- Args:
489
- src: the sequnce to the encoder layer (required).
490
- src_mask: the mask for the src sequence (optional).
491
- src_key_padding_mask: the mask for the src keys per batch (optional).
492
- """
493
- src2 = self.self_attn(
494
- src,
495
- src,
496
- src,
497
- attn_mask=src_mask,
498
- key_padding_mask=src_key_padding_mask)
499
- src = src + self.dropout1(src2)
500
- src = self.norm1(src)
501
-
502
- src = paddle.transpose(src, [1, 2, 0])
503
- src = paddle.unsqueeze(src, 2)
504
- src2 = self.conv2(F.relu(self.conv1(src)))
505
- src2 = paddle.squeeze(src2, 2)
506
- src2 = paddle.transpose(src2, [2, 0, 1])
507
- src = paddle.squeeze(src, 2)
508
- src = paddle.transpose(src, [2, 0, 1])
509
-
510
- src = src + self.dropout2(src2)
511
- src = self.norm2(src)
512
- return src
513
-
514
-
515
- class TransformerDecoderLayer(nn.Layer):
516
- """TransformerDecoderLayer is made up of self-attn, multi-head-attn and feedforward network.
517
- This standard decoder layer is based on the paper "Attention Is All You Need".
518
- Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N Gomez,
519
- Lukasz Kaiser, and Illia Polosukhin. 2017. Attention is all you need. In Advances in
520
- Neural Information Processing Systems, pages 6000-6010. Users may modify or implement
521
- in a different way during application.
522
-
523
- Args:
524
- d_model: the number of expected features in the input (required).
525
- nhead: the number of heads in the multiheadattention models (required).
526
- dim_feedforward: the dimension of the feedforward network model (default=2048).
527
- dropout: the dropout value (default=0.1).
528
-
529
- """
530
-
531
- def __init__(self,
532
- d_model,
533
- nhead,
534
- dim_feedforward=2048,
535
- attention_dropout_rate=0.0,
536
- residual_dropout_rate=0.1):
537
- super(TransformerDecoderLayer, self).__init__()
538
- self.self_attn = MultiheadAttention(
539
- d_model, nhead, dropout=attention_dropout_rate)
540
- self.multihead_attn = MultiheadAttention(
541
- d_model, nhead, dropout=attention_dropout_rate)
542
-
543
- self.conv1 = Conv2D(
544
- in_channels=d_model,
545
- out_channels=dim_feedforward,
546
- kernel_size=(1, 1))
547
- self.conv2 = Conv2D(
548
- in_channels=dim_feedforward,
549
- out_channels=d_model,
550
- kernel_size=(1, 1))
551
-
552
- self.norm1 = LayerNorm(d_model)
553
- self.norm2 = LayerNorm(d_model)
554
- self.norm3 = LayerNorm(d_model)
555
- self.dropout1 = Dropout(residual_dropout_rate)
556
- self.dropout2 = Dropout(residual_dropout_rate)
557
- self.dropout3 = Dropout(residual_dropout_rate)
558
-
559
- def forward(self,
560
- tgt,
561
- memory,
562
- tgt_mask=None,
563
- memory_mask=None,
564
- tgt_key_padding_mask=None,
565
- memory_key_padding_mask=None):
566
- """Pass the inputs (and mask) through the decoder layer.
567
-
568
- Args:
569
- tgt: the sequence to the decoder layer (required).
570
- memory: the sequnce from the last layer of the encoder (required).
571
- tgt_mask: the mask for the tgt sequence (optional).
572
- memory_mask: the mask for the memory sequence (optional).
573
- tgt_key_padding_mask: the mask for the tgt keys per batch (optional).
574
- memory_key_padding_mask: the mask for the memory keys per batch (optional).
575
-
576
- """
577
- tgt2 = self.self_attn(
578
- tgt,
579
- tgt,
580
- tgt,
581
- attn_mask=tgt_mask,
582
- key_padding_mask=tgt_key_padding_mask)
583
- tgt = tgt + self.dropout1(tgt2)
584
- tgt = self.norm1(tgt)
585
- tgt2 = self.multihead_attn(
586
- tgt,
587
- memory,
588
- memory,
589
- attn_mask=memory_mask,
590
- key_padding_mask=memory_key_padding_mask)
591
- tgt = tgt + self.dropout2(tgt2)
592
- tgt = self.norm2(tgt)
593
-
594
- # default
595
- tgt = paddle.transpose(tgt, [1, 2, 0])
596
- tgt = paddle.unsqueeze(tgt, 2)
597
- tgt2 = self.conv2(F.relu(self.conv1(tgt)))
598
- tgt2 = paddle.squeeze(tgt2, 2)
599
- tgt2 = paddle.transpose(tgt2, [2, 0, 1])
600
- tgt = paddle.squeeze(tgt, 2)
601
- tgt = paddle.transpose(tgt, [2, 0, 1])
602
-
603
- tgt = tgt + self.dropout3(tgt2)
604
- tgt = self.norm3(tgt)
605
- return tgt
606
-
607
-
608
- def _get_clones(module, N):
609
- return LayerList([copy.deepcopy(module) for i in range(N)])
610
-
611
-
612
- class PositionalEncoding(nn.Layer):
613
- """Inject some information about the relative or absolute position of the tokens
614
- in the sequence. The positional encodings have the same dimension as
615
- the embeddings, so that the two can be summed. Here, we use sine and cosine
616
- functions of different frequencies.
617
- .. math::
618
- \text{PosEncoder}(pos, 2i) = sin(pos/10000^(2i/d_model))
619
- \text{PosEncoder}(pos, 2i+1) = cos(pos/10000^(2i/d_model))
620
- \text{where pos is the word position and i is the embed idx)
621
- Args:
622
- d_model: the embed dim (required).
623
- dropout: the dropout value (default=0.1).
624
- max_len: the max. length of the incoming sequence (default=5000).
625
- Examples:
626
- >>> pos_encoder = PositionalEncoding(d_model)
627
- """
628
-
629
- def __init__(self, dropout, dim, max_len=5000):
630
- super(PositionalEncoding, self).__init__()
631
- self.dropout = nn.Dropout(p=dropout)
632
-
633
- pe = paddle.zeros([max_len, dim])
634
- position = paddle.arange(0, max_len, dtype=paddle.float32).unsqueeze(1)
635
- div_term = paddle.exp(
636
- paddle.arange(0, dim, 2).astype('float32') *
637
- (-math.log(10000.0) / dim))
638
- pe[:, 0::2] = paddle.sin(position * div_term)
639
- pe[:, 1::2] = paddle.cos(position * div_term)
640
- pe = paddle.unsqueeze(pe, 0)
641
- pe = paddle.transpose(pe, [1, 0, 2])
642
- self.register_buffer('pe', pe)
643
-
644
- def forward(self, x):
645
- """Inputs of forward function
646
- Args:
647
- x: the sequence fed to the positional encoder model (required).
648
- Shape:
649
- x: [sequence length, batch size, embed dim]
650
- output: [sequence length, batch size, embed dim]
651
- Examples:
652
- >>> output = pos_encoder(x)
653
- """
654
- x = x + self.pe[:paddle.shape(x)[0], :]
655
- return self.dropout(x)
656
-
657
-
658
- class PositionalEncoding_2d(nn.Layer):
659
- """Inject some information about the relative or absolute position of the tokens
660
- in the sequence. The positional encodings have the same dimension as
661
- the embeddings, so that the two can be summed. Here, we use sine and cosine
662
- functions of different frequencies.
663
- .. math::
664
- \text{PosEncoder}(pos, 2i) = sin(pos/10000^(2i/d_model))
665
- \text{PosEncoder}(pos, 2i+1) = cos(pos/10000^(2i/d_model))
666
- \text{where pos is the word position and i is the embed idx)
667
- Args:
668
- d_model: the embed dim (required).
669
- dropout: the dropout value (default=0.1).
670
- max_len: the max. length of the incoming sequence (default=5000).
671
- Examples:
672
- >>> pos_encoder = PositionalEncoding(d_model)
673
- """
674
-
675
- def __init__(self, dropout, dim, max_len=5000):
676
- super(PositionalEncoding_2d, self).__init__()
677
- self.dropout = nn.Dropout(p=dropout)
678
-
679
- pe = paddle.zeros([max_len, dim])
680
- position = paddle.arange(0, max_len, dtype=paddle.float32).unsqueeze(1)
681
- div_term = paddle.exp(
682
- paddle.arange(0, dim, 2).astype('float32') *
683
- (-math.log(10000.0) / dim))
684
- pe[:, 0::2] = paddle.sin(position * div_term)
685
- pe[:, 1::2] = paddle.cos(position * div_term)
686
- pe = paddle.transpose(paddle.unsqueeze(pe, 0), [1, 0, 2])
687
- self.register_buffer('pe', pe)
688
-
689
- self.avg_pool_1 = nn.AdaptiveAvgPool2D((1, 1))
690
- self.linear1 = nn.Linear(dim, dim)
691
- self.linear1.weight.data.fill_(1.)
692
- self.avg_pool_2 = nn.AdaptiveAvgPool2D((1, 1))
693
- self.linear2 = nn.Linear(dim, dim)
694
- self.linear2.weight.data.fill_(1.)
695
-
696
- def forward(self, x):
697
- """Inputs of forward function
698
- Args:
699
- x: the sequence fed to the positional encoder model (required).
700
- Shape:
701
- x: [sequence length, batch size, embed dim]
702
- output: [sequence length, batch size, embed dim]
703
- Examples:
704
- >>> output = pos_encoder(x)
705
- """
706
- w_pe = self.pe[:paddle.shape(x)[-1], :]
707
- w1 = self.linear1(self.avg_pool_1(x).squeeze()).unsqueeze(0)
708
- w_pe = w_pe * w1
709
- w_pe = paddle.transpose(w_pe, [1, 2, 0])
710
- w_pe = paddle.unsqueeze(w_pe, 2)
711
-
712
- h_pe = self.pe[:paddle.shape(x).shape[-2], :]
713
- w2 = self.linear2(self.avg_pool_2(x).squeeze()).unsqueeze(0)
714
- h_pe = h_pe * w2
715
- h_pe = paddle.transpose(h_pe, [1, 2, 0])
716
- h_pe = paddle.unsqueeze(h_pe, 3)
717
-
718
- x = x + w_pe + h_pe
719
- x = paddle.transpose(
720
- paddle.reshape(x,
721
- [x.shape[0], x.shape[1], x.shape[2] * x.shape[3]]),
722
- [2, 0, 1])
723
-
724
- return self.dropout(x)
725
-
726
-
727
- class Embeddings(nn.Layer):
728
- def __init__(self, d_model, vocab, padding_idx, scale_embedding):
729
- super(Embeddings, self).__init__()
730
- self.embedding = nn.Embedding(vocab, d_model, padding_idx=padding_idx)
731
- w0 = np.random.normal(0.0, d_model**-0.5,
732
- (vocab, d_model)).astype(np.float32)
733
- self.embedding.weight.set_value(w0)
734
- self.d_model = d_model
735
- self.scale_embedding = scale_embedding
736
-
737
- def forward(self, x):
738
- if self.scale_embedding:
739
- x = self.embedding(x)
740
- return x * math.sqrt(self.d_model)
741
- return self.embedding(x)
742
-
743
-
744
- class Beam():
745
- ''' Beam search '''
746
-
747
- def __init__(self, size, device=False):
748
-
749
- self.size = size
750
- self._done = False
751
- # The score for each translation on the beam.
752
- self.scores = paddle.zeros((size, ), dtype=paddle.float32)
753
- self.all_scores = []
754
- # The backpointers at each time-step.
755
- self.prev_ks = []
756
- # The outputs at each time-step.
757
- self.next_ys = [paddle.full((size, ), 0, dtype=paddle.int64)]
758
- self.next_ys[0][0] = 2
759
-
760
- def get_current_state(self):
761
- "Get the outputs for the current timestep."
762
- return self.get_tentative_hypothesis()
763
-
764
- def get_current_origin(self):
765
- "Get the backpointers for the current timestep."
766
- return self.prev_ks[-1]
767
-
768
- @property
769
- def done(self):
770
- return self._done
771
-
772
- def advance(self, word_prob):
773
- "Update beam status and check if finished or not."
774
- num_words = word_prob.shape[1]
775
-
776
- # Sum the previous scores.
777
- if len(self.prev_ks) > 0:
778
- beam_lk = word_prob + self.scores.unsqueeze(1).expand_as(word_prob)
779
- else:
780
- beam_lk = word_prob[0]
781
-
782
- flat_beam_lk = beam_lk.reshape([-1])
783
- best_scores, best_scores_id = flat_beam_lk.topk(self.size, 0, True,
784
- True) # 1st sort
785
- self.all_scores.append(self.scores)
786
- self.scores = best_scores
787
- # bestScoresId is flattened as a (beam x word) array,
788
- # so we need to calculate which word and beam each score came from
789
- prev_k = best_scores_id // num_words
790
- self.prev_ks.append(prev_k)
791
- self.next_ys.append(best_scores_id - prev_k * num_words)
792
- # End condition is when top-of-beam is EOS.
793
- if self.next_ys[-1][0] == 3:
794
- self._done = True
795
- self.all_scores.append(self.scores)
796
-
797
- return self._done
798
-
799
- def sort_scores(self):
800
- "Sort the scores."
801
- return self.scores, paddle.to_tensor(
802
- [i for i in range(int(self.scores.shape[0]))], dtype='int32')
803
-
804
- def get_the_best_score_and_idx(self):
805
- "Get the score of the best in the beam."
806
- scores, ids = self.sort_scores()
807
- return scores[1], ids[1]
808
-
809
- def get_tentative_hypothesis(self):
810
- "Get the decoded sequence for the current timestep."
811
- if len(self.next_ys) == 1:
812
- dec_seq = self.next_ys[0].unsqueeze(1)
813
- else:
814
- _, keys = self.sort_scores()
815
- hyps = [self.get_hypothesis(k) for k in keys]
816
- hyps = [[2] + h for h in hyps]
817
- dec_seq = paddle.to_tensor(hyps, dtype='int64')
818
- return dec_seq
819
-
820
- def get_hypothesis(self, k):
821
- """ Walk back to construct the full hypothesis. """
822
- hyp = []
823
- for j in range(len(self.prev_ks) - 1, -1, -1):
824
- hyp.append(self.next_ys[j + 1][k])
825
- k = self.prev_ks[j][k]
826
- return list(map(lambda x: x.item(), hyp[::-1]))