pyxllib 0.3.96__py3-none-any.whl → 0.3.197__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.
- pyxllib/algo/geo.py +12 -0
- pyxllib/algo/intervals.py +1 -1
- pyxllib/algo/matcher.py +78 -0
- pyxllib/algo/pupil.py +187 -19
- pyxllib/algo/specialist.py +2 -1
- pyxllib/algo/stat.py +38 -2
- {pyxlpr → pyxllib/autogui}/__init__.py +1 -1
- pyxllib/autogui/activewin.py +246 -0
- pyxllib/autogui/all.py +9 -0
- pyxllib/{ext/autogui → autogui}/autogui.py +40 -11
- pyxllib/autogui/uiautolib.py +362 -0
- pyxllib/autogui/wechat.py +827 -0
- pyxllib/autogui/wechat_msg.py +421 -0
- pyxllib/autogui/wxautolib.py +84 -0
- pyxllib/cv/slidercaptcha.py +137 -0
- pyxllib/data/echarts.py +123 -12
- pyxllib/data/jsonlib.py +89 -0
- pyxllib/data/pglib.py +514 -30
- pyxllib/data/sqlite.py +231 -4
- pyxllib/ext/JLineViewer.py +14 -1
- pyxllib/ext/drissionlib.py +277 -0
- pyxllib/ext/kq5034lib.py +0 -1594
- pyxllib/ext/robustprocfile.py +497 -0
- pyxllib/ext/unixlib.py +6 -5
- pyxllib/ext/utools.py +108 -95
- pyxllib/ext/webhook.py +32 -14
- pyxllib/ext/wjxlib.py +88 -0
- pyxllib/ext/wpsapi.py +124 -0
- pyxllib/ext/xlwork.py +9 -0
- pyxllib/ext/yuquelib.py +1003 -71
- pyxllib/file/docxlib.py +1 -1
- pyxllib/file/libreoffice.py +165 -0
- pyxllib/file/movielib.py +9 -0
- pyxllib/file/packlib/__init__.py +112 -75
- pyxllib/file/pdflib.py +1 -1
- pyxllib/file/pupil.py +1 -1
- pyxllib/file/specialist/dirlib.py +1 -1
- pyxllib/file/specialist/download.py +10 -3
- pyxllib/file/specialist/filelib.py +266 -55
- pyxllib/file/xlsxlib.py +205 -50
- pyxllib/file/xlsyncfile.py +341 -0
- pyxllib/prog/cachetools.py +64 -0
- pyxllib/prog/filelock.py +42 -0
- pyxllib/prog/multiprogs.py +940 -0
- pyxllib/prog/newbie.py +9 -2
- pyxllib/prog/pupil.py +129 -60
- pyxllib/prog/specialist/__init__.py +176 -2
- pyxllib/prog/specialist/bc.py +5 -2
- pyxllib/prog/specialist/browser.py +11 -2
- pyxllib/prog/specialist/datetime.py +68 -0
- pyxllib/prog/specialist/tictoc.py +12 -13
- pyxllib/prog/specialist/xllog.py +5 -5
- pyxllib/prog/xlosenv.py +7 -0
- pyxllib/text/airscript.js +744 -0
- pyxllib/text/charclasslib.py +17 -5
- pyxllib/text/jiebalib.py +6 -3
- pyxllib/text/jinjalib.py +32 -0
- pyxllib/text/jsa_ai_prompt.md +271 -0
- pyxllib/text/jscode.py +159 -4
- pyxllib/text/nestenv.py +1 -1
- pyxllib/text/newbie.py +12 -0
- pyxllib/text/pupil/common.py +26 -0
- pyxllib/text/specialist/ptag.py +2 -2
- pyxllib/text/templates/echart_base.html +11 -0
- pyxllib/text/templates/highlight_code.html +17 -0
- pyxllib/text/templates/latex_editor.html +103 -0
- pyxllib/text/xmllib.py +76 -14
- pyxllib/xl.py +2 -1
- pyxllib-0.3.197.dist-info/METADATA +48 -0
- pyxllib-0.3.197.dist-info/RECORD +126 -0
- {pyxllib-0.3.96.dist-info → pyxllib-0.3.197.dist-info}/WHEEL +1 -2
- pyxllib/ext/autogui/__init__.py +0 -8
- pyxllib-0.3.96.dist-info/METADATA +0 -51
- pyxllib-0.3.96.dist-info/RECORD +0 -333
- pyxllib-0.3.96.dist-info/top_level.txt +0 -2
- pyxlpr/ai/__init__.py +0 -5
- pyxlpr/ai/clientlib.py +0 -1281
- pyxlpr/ai/specialist.py +0 -286
- pyxlpr/ai/torch_app.py +0 -172
- pyxlpr/ai/xlpaddle.py +0 -655
- pyxlpr/ai/xltorch.py +0 -705
- pyxlpr/data/__init__.py +0 -11
- pyxlpr/data/coco.py +0 -1325
- pyxlpr/data/datacls.py +0 -365
- pyxlpr/data/datasets.py +0 -200
- pyxlpr/data/gptlib.py +0 -1291
- pyxlpr/data/icdar/__init__.py +0 -96
- pyxlpr/data/icdar/deteval.py +0 -377
- pyxlpr/data/icdar/icdar2013.py +0 -341
- pyxlpr/data/icdar/iou.py +0 -340
- pyxlpr/data/icdar/rrc_evaluation_funcs_1_1.py +0 -463
- pyxlpr/data/imtextline.py +0 -473
- pyxlpr/data/labelme.py +0 -866
- pyxlpr/data/removeline.py +0 -179
- pyxlpr/data/specialist.py +0 -57
- pyxlpr/eval/__init__.py +0 -85
- pyxlpr/paddleocr.py +0 -776
- pyxlpr/ppocr/__init__.py +0 -15
- pyxlpr/ppocr/configs/rec/multi_language/generate_multi_language_configs.py +0 -226
- pyxlpr/ppocr/data/__init__.py +0 -135
- pyxlpr/ppocr/data/imaug/ColorJitter.py +0 -26
- pyxlpr/ppocr/data/imaug/__init__.py +0 -67
- pyxlpr/ppocr/data/imaug/copy_paste.py +0 -170
- pyxlpr/ppocr/data/imaug/east_process.py +0 -437
- pyxlpr/ppocr/data/imaug/gen_table_mask.py +0 -244
- pyxlpr/ppocr/data/imaug/iaa_augment.py +0 -114
- pyxlpr/ppocr/data/imaug/label_ops.py +0 -789
- pyxlpr/ppocr/data/imaug/make_border_map.py +0 -184
- pyxlpr/ppocr/data/imaug/make_pse_gt.py +0 -106
- pyxlpr/ppocr/data/imaug/make_shrink_map.py +0 -126
- pyxlpr/ppocr/data/imaug/operators.py +0 -433
- pyxlpr/ppocr/data/imaug/pg_process.py +0 -906
- pyxlpr/ppocr/data/imaug/randaugment.py +0 -143
- pyxlpr/ppocr/data/imaug/random_crop_data.py +0 -239
- pyxlpr/ppocr/data/imaug/rec_img_aug.py +0 -533
- pyxlpr/ppocr/data/imaug/sast_process.py +0 -777
- pyxlpr/ppocr/data/imaug/text_image_aug/__init__.py +0 -17
- pyxlpr/ppocr/data/imaug/text_image_aug/augment.py +0 -120
- pyxlpr/ppocr/data/imaug/text_image_aug/warp_mls.py +0 -168
- pyxlpr/ppocr/data/lmdb_dataset.py +0 -115
- pyxlpr/ppocr/data/pgnet_dataset.py +0 -104
- pyxlpr/ppocr/data/pubtab_dataset.py +0 -107
- pyxlpr/ppocr/data/simple_dataset.py +0 -372
- pyxlpr/ppocr/losses/__init__.py +0 -61
- pyxlpr/ppocr/losses/ace_loss.py +0 -52
- pyxlpr/ppocr/losses/basic_loss.py +0 -135
- pyxlpr/ppocr/losses/center_loss.py +0 -88
- pyxlpr/ppocr/losses/cls_loss.py +0 -30
- pyxlpr/ppocr/losses/combined_loss.py +0 -67
- pyxlpr/ppocr/losses/det_basic_loss.py +0 -208
- pyxlpr/ppocr/losses/det_db_loss.py +0 -80
- pyxlpr/ppocr/losses/det_east_loss.py +0 -63
- pyxlpr/ppocr/losses/det_pse_loss.py +0 -149
- pyxlpr/ppocr/losses/det_sast_loss.py +0 -121
- pyxlpr/ppocr/losses/distillation_loss.py +0 -272
- pyxlpr/ppocr/losses/e2e_pg_loss.py +0 -140
- pyxlpr/ppocr/losses/kie_sdmgr_loss.py +0 -113
- pyxlpr/ppocr/losses/rec_aster_loss.py +0 -99
- pyxlpr/ppocr/losses/rec_att_loss.py +0 -39
- pyxlpr/ppocr/losses/rec_ctc_loss.py +0 -44
- pyxlpr/ppocr/losses/rec_enhanced_ctc_loss.py +0 -70
- pyxlpr/ppocr/losses/rec_nrtr_loss.py +0 -30
- pyxlpr/ppocr/losses/rec_sar_loss.py +0 -28
- pyxlpr/ppocr/losses/rec_srn_loss.py +0 -47
- pyxlpr/ppocr/losses/table_att_loss.py +0 -109
- pyxlpr/ppocr/metrics/__init__.py +0 -44
- pyxlpr/ppocr/metrics/cls_metric.py +0 -45
- pyxlpr/ppocr/metrics/det_metric.py +0 -82
- pyxlpr/ppocr/metrics/distillation_metric.py +0 -73
- pyxlpr/ppocr/metrics/e2e_metric.py +0 -86
- pyxlpr/ppocr/metrics/eval_det_iou.py +0 -274
- pyxlpr/ppocr/metrics/kie_metric.py +0 -70
- pyxlpr/ppocr/metrics/rec_metric.py +0 -75
- pyxlpr/ppocr/metrics/table_metric.py +0 -50
- pyxlpr/ppocr/modeling/architectures/__init__.py +0 -32
- pyxlpr/ppocr/modeling/architectures/base_model.py +0 -88
- pyxlpr/ppocr/modeling/architectures/distillation_model.py +0 -60
- pyxlpr/ppocr/modeling/backbones/__init__.py +0 -54
- pyxlpr/ppocr/modeling/backbones/det_mobilenet_v3.py +0 -268
- pyxlpr/ppocr/modeling/backbones/det_resnet_vd.py +0 -246
- pyxlpr/ppocr/modeling/backbones/det_resnet_vd_sast.py +0 -285
- pyxlpr/ppocr/modeling/backbones/e2e_resnet_vd_pg.py +0 -265
- pyxlpr/ppocr/modeling/backbones/kie_unet_sdmgr.py +0 -186
- pyxlpr/ppocr/modeling/backbones/rec_mobilenet_v3.py +0 -138
- pyxlpr/ppocr/modeling/backbones/rec_mv1_enhance.py +0 -258
- pyxlpr/ppocr/modeling/backbones/rec_nrtr_mtb.py +0 -48
- pyxlpr/ppocr/modeling/backbones/rec_resnet_31.py +0 -210
- pyxlpr/ppocr/modeling/backbones/rec_resnet_aster.py +0 -143
- pyxlpr/ppocr/modeling/backbones/rec_resnet_fpn.py +0 -307
- pyxlpr/ppocr/modeling/backbones/rec_resnet_vd.py +0 -286
- pyxlpr/ppocr/modeling/heads/__init__.py +0 -54
- pyxlpr/ppocr/modeling/heads/cls_head.py +0 -52
- pyxlpr/ppocr/modeling/heads/det_db_head.py +0 -118
- pyxlpr/ppocr/modeling/heads/det_east_head.py +0 -121
- pyxlpr/ppocr/modeling/heads/det_pse_head.py +0 -37
- pyxlpr/ppocr/modeling/heads/det_sast_head.py +0 -128
- pyxlpr/ppocr/modeling/heads/e2e_pg_head.py +0 -253
- pyxlpr/ppocr/modeling/heads/kie_sdmgr_head.py +0 -206
- pyxlpr/ppocr/modeling/heads/multiheadAttention.py +0 -163
- pyxlpr/ppocr/modeling/heads/rec_aster_head.py +0 -393
- pyxlpr/ppocr/modeling/heads/rec_att_head.py +0 -202
- pyxlpr/ppocr/modeling/heads/rec_ctc_head.py +0 -88
- pyxlpr/ppocr/modeling/heads/rec_nrtr_head.py +0 -826
- pyxlpr/ppocr/modeling/heads/rec_sar_head.py +0 -402
- pyxlpr/ppocr/modeling/heads/rec_srn_head.py +0 -280
- pyxlpr/ppocr/modeling/heads/self_attention.py +0 -406
- pyxlpr/ppocr/modeling/heads/table_att_head.py +0 -246
- pyxlpr/ppocr/modeling/necks/__init__.py +0 -32
- pyxlpr/ppocr/modeling/necks/db_fpn.py +0 -111
- pyxlpr/ppocr/modeling/necks/east_fpn.py +0 -188
- pyxlpr/ppocr/modeling/necks/fpn.py +0 -138
- pyxlpr/ppocr/modeling/necks/pg_fpn.py +0 -314
- pyxlpr/ppocr/modeling/necks/rnn.py +0 -92
- pyxlpr/ppocr/modeling/necks/sast_fpn.py +0 -284
- pyxlpr/ppocr/modeling/necks/table_fpn.py +0 -110
- pyxlpr/ppocr/modeling/transforms/__init__.py +0 -28
- pyxlpr/ppocr/modeling/transforms/stn.py +0 -135
- pyxlpr/ppocr/modeling/transforms/tps.py +0 -308
- pyxlpr/ppocr/modeling/transforms/tps_spatial_transformer.py +0 -156
- pyxlpr/ppocr/optimizer/__init__.py +0 -61
- pyxlpr/ppocr/optimizer/learning_rate.py +0 -228
- pyxlpr/ppocr/optimizer/lr_scheduler.py +0 -49
- pyxlpr/ppocr/optimizer/optimizer.py +0 -160
- pyxlpr/ppocr/optimizer/regularizer.py +0 -52
- pyxlpr/ppocr/postprocess/__init__.py +0 -55
- pyxlpr/ppocr/postprocess/cls_postprocess.py +0 -33
- pyxlpr/ppocr/postprocess/db_postprocess.py +0 -234
- pyxlpr/ppocr/postprocess/east_postprocess.py +0 -143
- pyxlpr/ppocr/postprocess/locality_aware_nms.py +0 -200
- pyxlpr/ppocr/postprocess/pg_postprocess.py +0 -52
- pyxlpr/ppocr/postprocess/pse_postprocess/__init__.py +0 -15
- pyxlpr/ppocr/postprocess/pse_postprocess/pse/__init__.py +0 -29
- pyxlpr/ppocr/postprocess/pse_postprocess/pse/setup.py +0 -14
- pyxlpr/ppocr/postprocess/pse_postprocess/pse_postprocess.py +0 -118
- pyxlpr/ppocr/postprocess/rec_postprocess.py +0 -654
- pyxlpr/ppocr/postprocess/sast_postprocess.py +0 -355
- pyxlpr/ppocr/tools/__init__.py +0 -14
- pyxlpr/ppocr/tools/eval.py +0 -83
- pyxlpr/ppocr/tools/export_center.py +0 -77
- pyxlpr/ppocr/tools/export_model.py +0 -129
- pyxlpr/ppocr/tools/infer/predict_cls.py +0 -151
- pyxlpr/ppocr/tools/infer/predict_det.py +0 -300
- pyxlpr/ppocr/tools/infer/predict_e2e.py +0 -169
- pyxlpr/ppocr/tools/infer/predict_rec.py +0 -414
- pyxlpr/ppocr/tools/infer/predict_system.py +0 -204
- pyxlpr/ppocr/tools/infer/utility.py +0 -629
- pyxlpr/ppocr/tools/infer_cls.py +0 -83
- pyxlpr/ppocr/tools/infer_det.py +0 -134
- pyxlpr/ppocr/tools/infer_e2e.py +0 -122
- pyxlpr/ppocr/tools/infer_kie.py +0 -153
- pyxlpr/ppocr/tools/infer_rec.py +0 -146
- pyxlpr/ppocr/tools/infer_table.py +0 -107
- pyxlpr/ppocr/tools/program.py +0 -596
- pyxlpr/ppocr/tools/test_hubserving.py +0 -117
- pyxlpr/ppocr/tools/train.py +0 -163
- pyxlpr/ppocr/tools/xlprog.py +0 -748
- pyxlpr/ppocr/utils/EN_symbol_dict.txt +0 -94
- pyxlpr/ppocr/utils/__init__.py +0 -24
- pyxlpr/ppocr/utils/dict/ar_dict.txt +0 -117
- pyxlpr/ppocr/utils/dict/arabic_dict.txt +0 -162
- pyxlpr/ppocr/utils/dict/be_dict.txt +0 -145
- pyxlpr/ppocr/utils/dict/bg_dict.txt +0 -140
- pyxlpr/ppocr/utils/dict/chinese_cht_dict.txt +0 -8421
- pyxlpr/ppocr/utils/dict/cyrillic_dict.txt +0 -163
- pyxlpr/ppocr/utils/dict/devanagari_dict.txt +0 -167
- pyxlpr/ppocr/utils/dict/en_dict.txt +0 -63
- pyxlpr/ppocr/utils/dict/fa_dict.txt +0 -136
- pyxlpr/ppocr/utils/dict/french_dict.txt +0 -136
- pyxlpr/ppocr/utils/dict/german_dict.txt +0 -143
- pyxlpr/ppocr/utils/dict/hi_dict.txt +0 -162
- pyxlpr/ppocr/utils/dict/it_dict.txt +0 -118
- pyxlpr/ppocr/utils/dict/japan_dict.txt +0 -4399
- pyxlpr/ppocr/utils/dict/ka_dict.txt +0 -153
- pyxlpr/ppocr/utils/dict/korean_dict.txt +0 -3688
- pyxlpr/ppocr/utils/dict/latin_dict.txt +0 -185
- pyxlpr/ppocr/utils/dict/mr_dict.txt +0 -153
- pyxlpr/ppocr/utils/dict/ne_dict.txt +0 -153
- pyxlpr/ppocr/utils/dict/oc_dict.txt +0 -96
- pyxlpr/ppocr/utils/dict/pu_dict.txt +0 -130
- pyxlpr/ppocr/utils/dict/rs_dict.txt +0 -91
- pyxlpr/ppocr/utils/dict/rsc_dict.txt +0 -134
- pyxlpr/ppocr/utils/dict/ru_dict.txt +0 -125
- pyxlpr/ppocr/utils/dict/ta_dict.txt +0 -128
- pyxlpr/ppocr/utils/dict/table_dict.txt +0 -277
- pyxlpr/ppocr/utils/dict/table_structure_dict.txt +0 -2759
- pyxlpr/ppocr/utils/dict/te_dict.txt +0 -151
- pyxlpr/ppocr/utils/dict/ug_dict.txt +0 -114
- pyxlpr/ppocr/utils/dict/uk_dict.txt +0 -142
- pyxlpr/ppocr/utils/dict/ur_dict.txt +0 -137
- pyxlpr/ppocr/utils/dict/xi_dict.txt +0 -110
- pyxlpr/ppocr/utils/dict90.txt +0 -90
- pyxlpr/ppocr/utils/e2e_metric/Deteval.py +0 -574
- pyxlpr/ppocr/utils/e2e_metric/polygon_fast.py +0 -83
- pyxlpr/ppocr/utils/e2e_utils/extract_batchsize.py +0 -87
- pyxlpr/ppocr/utils/e2e_utils/extract_textpoint_fast.py +0 -457
- pyxlpr/ppocr/utils/e2e_utils/extract_textpoint_slow.py +0 -592
- pyxlpr/ppocr/utils/e2e_utils/pgnet_pp_utils.py +0 -162
- pyxlpr/ppocr/utils/e2e_utils/visual.py +0 -162
- pyxlpr/ppocr/utils/en_dict.txt +0 -95
- pyxlpr/ppocr/utils/gen_label.py +0 -81
- pyxlpr/ppocr/utils/ic15_dict.txt +0 -36
- pyxlpr/ppocr/utils/iou.py +0 -54
- pyxlpr/ppocr/utils/logging.py +0 -69
- pyxlpr/ppocr/utils/network.py +0 -84
- pyxlpr/ppocr/utils/ppocr_keys_v1.txt +0 -6623
- pyxlpr/ppocr/utils/profiler.py +0 -110
- pyxlpr/ppocr/utils/save_load.py +0 -150
- pyxlpr/ppocr/utils/stats.py +0 -72
- pyxlpr/ppocr/utils/utility.py +0 -80
- pyxlpr/ppstructure/__init__.py +0 -13
- pyxlpr/ppstructure/predict_system.py +0 -187
- pyxlpr/ppstructure/table/__init__.py +0 -13
- pyxlpr/ppstructure/table/eval_table.py +0 -72
- pyxlpr/ppstructure/table/matcher.py +0 -192
- pyxlpr/ppstructure/table/predict_structure.py +0 -136
- pyxlpr/ppstructure/table/predict_table.py +0 -221
- pyxlpr/ppstructure/table/table_metric/__init__.py +0 -16
- pyxlpr/ppstructure/table/table_metric/parallel.py +0 -51
- pyxlpr/ppstructure/table/table_metric/table_metric.py +0 -247
- pyxlpr/ppstructure/table/tablepyxl/__init__.py +0 -13
- pyxlpr/ppstructure/table/tablepyxl/style.py +0 -283
- pyxlpr/ppstructure/table/tablepyxl/tablepyxl.py +0 -118
- pyxlpr/ppstructure/utility.py +0 -71
- pyxlpr/xlai.py +0 -10
- /pyxllib/{ext/autogui → autogui}/virtualkey.py +0 -0
- {pyxllib-0.3.96.dist-info → pyxllib-0.3.197.dist-info/licenses}/LICENSE +0 -0
@@ -1,406 +0,0 @@
|
|
1
|
-
# copyright (c) 2020 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
|
-
from __future__ import absolute_import
|
16
|
-
from __future__ import division
|
17
|
-
from __future__ import print_function
|
18
|
-
|
19
|
-
import math
|
20
|
-
|
21
|
-
import paddle
|
22
|
-
from paddle import ParamAttr, nn
|
23
|
-
from paddle import nn, ParamAttr
|
24
|
-
from paddle.nn import functional as F
|
25
|
-
import paddle.fluid as fluid
|
26
|
-
import numpy as np
|
27
|
-
gradient_clip = 10
|
28
|
-
|
29
|
-
|
30
|
-
class WrapEncoderForFeature(nn.Layer):
|
31
|
-
def __init__(self,
|
32
|
-
src_vocab_size,
|
33
|
-
max_length,
|
34
|
-
n_layer,
|
35
|
-
n_head,
|
36
|
-
d_key,
|
37
|
-
d_value,
|
38
|
-
d_model,
|
39
|
-
d_inner_hid,
|
40
|
-
prepostprocess_dropout,
|
41
|
-
attention_dropout,
|
42
|
-
relu_dropout,
|
43
|
-
preprocess_cmd,
|
44
|
-
postprocess_cmd,
|
45
|
-
weight_sharing,
|
46
|
-
bos_idx=0):
|
47
|
-
super(WrapEncoderForFeature, self).__init__()
|
48
|
-
|
49
|
-
self.prepare_encoder = PrepareEncoder(
|
50
|
-
src_vocab_size,
|
51
|
-
d_model,
|
52
|
-
max_length,
|
53
|
-
prepostprocess_dropout,
|
54
|
-
bos_idx=bos_idx,
|
55
|
-
word_emb_param_name="src_word_emb_table")
|
56
|
-
self.encoder = Encoder(n_layer, n_head, d_key, d_value, d_model,
|
57
|
-
d_inner_hid, prepostprocess_dropout,
|
58
|
-
attention_dropout, relu_dropout, preprocess_cmd,
|
59
|
-
postprocess_cmd)
|
60
|
-
|
61
|
-
def forward(self, enc_inputs):
|
62
|
-
conv_features, src_pos, src_slf_attn_bias = enc_inputs
|
63
|
-
enc_input = self.prepare_encoder(conv_features, src_pos)
|
64
|
-
enc_output = self.encoder(enc_input, src_slf_attn_bias)
|
65
|
-
return enc_output
|
66
|
-
|
67
|
-
|
68
|
-
class WrapEncoder(nn.Layer):
|
69
|
-
"""
|
70
|
-
embedder + encoder
|
71
|
-
"""
|
72
|
-
|
73
|
-
def __init__(self,
|
74
|
-
src_vocab_size,
|
75
|
-
max_length,
|
76
|
-
n_layer,
|
77
|
-
n_head,
|
78
|
-
d_key,
|
79
|
-
d_value,
|
80
|
-
d_model,
|
81
|
-
d_inner_hid,
|
82
|
-
prepostprocess_dropout,
|
83
|
-
attention_dropout,
|
84
|
-
relu_dropout,
|
85
|
-
preprocess_cmd,
|
86
|
-
postprocess_cmd,
|
87
|
-
weight_sharing,
|
88
|
-
bos_idx=0):
|
89
|
-
super(WrapEncoder, self).__init__()
|
90
|
-
|
91
|
-
self.prepare_decoder = PrepareDecoder(
|
92
|
-
src_vocab_size,
|
93
|
-
d_model,
|
94
|
-
max_length,
|
95
|
-
prepostprocess_dropout,
|
96
|
-
bos_idx=bos_idx)
|
97
|
-
self.encoder = Encoder(n_layer, n_head, d_key, d_value, d_model,
|
98
|
-
d_inner_hid, prepostprocess_dropout,
|
99
|
-
attention_dropout, relu_dropout, preprocess_cmd,
|
100
|
-
postprocess_cmd)
|
101
|
-
|
102
|
-
def forward(self, enc_inputs):
|
103
|
-
src_word, src_pos, src_slf_attn_bias = enc_inputs
|
104
|
-
enc_input = self.prepare_decoder(src_word, src_pos)
|
105
|
-
enc_output = self.encoder(enc_input, src_slf_attn_bias)
|
106
|
-
return enc_output
|
107
|
-
|
108
|
-
|
109
|
-
class Encoder(nn.Layer):
|
110
|
-
"""
|
111
|
-
encoder
|
112
|
-
"""
|
113
|
-
|
114
|
-
def __init__(self,
|
115
|
-
n_layer,
|
116
|
-
n_head,
|
117
|
-
d_key,
|
118
|
-
d_value,
|
119
|
-
d_model,
|
120
|
-
d_inner_hid,
|
121
|
-
prepostprocess_dropout,
|
122
|
-
attention_dropout,
|
123
|
-
relu_dropout,
|
124
|
-
preprocess_cmd="n",
|
125
|
-
postprocess_cmd="da"):
|
126
|
-
|
127
|
-
super(Encoder, self).__init__()
|
128
|
-
|
129
|
-
self.encoder_layers = list()
|
130
|
-
for i in range(n_layer):
|
131
|
-
self.encoder_layers.append(
|
132
|
-
self.add_sublayer(
|
133
|
-
"layer_%d" % i,
|
134
|
-
EncoderLayer(n_head, d_key, d_value, d_model, d_inner_hid,
|
135
|
-
prepostprocess_dropout, attention_dropout,
|
136
|
-
relu_dropout, preprocess_cmd,
|
137
|
-
postprocess_cmd)))
|
138
|
-
self.processer = PrePostProcessLayer(preprocess_cmd, d_model,
|
139
|
-
prepostprocess_dropout)
|
140
|
-
|
141
|
-
def forward(self, enc_input, attn_bias):
|
142
|
-
for encoder_layer in self.encoder_layers:
|
143
|
-
enc_output = encoder_layer(enc_input, attn_bias)
|
144
|
-
enc_input = enc_output
|
145
|
-
enc_output = self.processer(enc_output)
|
146
|
-
return enc_output
|
147
|
-
|
148
|
-
|
149
|
-
class EncoderLayer(nn.Layer):
|
150
|
-
"""
|
151
|
-
EncoderLayer
|
152
|
-
"""
|
153
|
-
|
154
|
-
def __init__(self,
|
155
|
-
n_head,
|
156
|
-
d_key,
|
157
|
-
d_value,
|
158
|
-
d_model,
|
159
|
-
d_inner_hid,
|
160
|
-
prepostprocess_dropout,
|
161
|
-
attention_dropout,
|
162
|
-
relu_dropout,
|
163
|
-
preprocess_cmd="n",
|
164
|
-
postprocess_cmd="da"):
|
165
|
-
|
166
|
-
super(EncoderLayer, self).__init__()
|
167
|
-
self.preprocesser1 = PrePostProcessLayer(preprocess_cmd, d_model,
|
168
|
-
prepostprocess_dropout)
|
169
|
-
self.self_attn = MultiHeadAttention(d_key, d_value, d_model, n_head,
|
170
|
-
attention_dropout)
|
171
|
-
self.postprocesser1 = PrePostProcessLayer(postprocess_cmd, d_model,
|
172
|
-
prepostprocess_dropout)
|
173
|
-
|
174
|
-
self.preprocesser2 = PrePostProcessLayer(preprocess_cmd, d_model,
|
175
|
-
prepostprocess_dropout)
|
176
|
-
self.ffn = FFN(d_inner_hid, d_model, relu_dropout)
|
177
|
-
self.postprocesser2 = PrePostProcessLayer(postprocess_cmd, d_model,
|
178
|
-
prepostprocess_dropout)
|
179
|
-
|
180
|
-
def forward(self, enc_input, attn_bias):
|
181
|
-
attn_output = self.self_attn(
|
182
|
-
self.preprocesser1(enc_input), None, None, attn_bias)
|
183
|
-
attn_output = self.postprocesser1(attn_output, enc_input)
|
184
|
-
ffn_output = self.ffn(self.preprocesser2(attn_output))
|
185
|
-
ffn_output = self.postprocesser2(ffn_output, attn_output)
|
186
|
-
return ffn_output
|
187
|
-
|
188
|
-
|
189
|
-
class MultiHeadAttention(nn.Layer):
|
190
|
-
"""
|
191
|
-
Multi-Head Attention
|
192
|
-
"""
|
193
|
-
|
194
|
-
def __init__(self, d_key, d_value, d_model, n_head=1, dropout_rate=0.):
|
195
|
-
super(MultiHeadAttention, self).__init__()
|
196
|
-
self.n_head = n_head
|
197
|
-
self.d_key = d_key
|
198
|
-
self.d_value = d_value
|
199
|
-
self.d_model = d_model
|
200
|
-
self.dropout_rate = dropout_rate
|
201
|
-
self.q_fc = paddle.nn.Linear(
|
202
|
-
in_features=d_model, out_features=d_key * n_head, bias_attr=False)
|
203
|
-
self.k_fc = paddle.nn.Linear(
|
204
|
-
in_features=d_model, out_features=d_key * n_head, bias_attr=False)
|
205
|
-
self.v_fc = paddle.nn.Linear(
|
206
|
-
in_features=d_model, out_features=d_value * n_head, bias_attr=False)
|
207
|
-
self.proj_fc = paddle.nn.Linear(
|
208
|
-
in_features=d_value * n_head, out_features=d_model, bias_attr=False)
|
209
|
-
|
210
|
-
def _prepare_qkv(self, queries, keys, values, cache=None):
|
211
|
-
if keys is None: # self-attention
|
212
|
-
keys, values = queries, queries
|
213
|
-
static_kv = False
|
214
|
-
else: # cross-attention
|
215
|
-
static_kv = True
|
216
|
-
|
217
|
-
q = self.q_fc(queries)
|
218
|
-
q = paddle.reshape(x=q, shape=[0, 0, self.n_head, self.d_key])
|
219
|
-
q = paddle.transpose(x=q, perm=[0, 2, 1, 3])
|
220
|
-
|
221
|
-
if cache is not None and static_kv and "static_k" in cache:
|
222
|
-
# for encoder-decoder attention in inference and has cached
|
223
|
-
k = cache["static_k"]
|
224
|
-
v = cache["static_v"]
|
225
|
-
else:
|
226
|
-
k = self.k_fc(keys)
|
227
|
-
v = self.v_fc(values)
|
228
|
-
k = paddle.reshape(x=k, shape=[0, 0, self.n_head, self.d_key])
|
229
|
-
k = paddle.transpose(x=k, perm=[0, 2, 1, 3])
|
230
|
-
v = paddle.reshape(x=v, shape=[0, 0, self.n_head, self.d_value])
|
231
|
-
v = paddle.transpose(x=v, perm=[0, 2, 1, 3])
|
232
|
-
|
233
|
-
if cache is not None:
|
234
|
-
if static_kv and not "static_k" in cache:
|
235
|
-
# for encoder-decoder attention in inference and has not cached
|
236
|
-
cache["static_k"], cache["static_v"] = k, v
|
237
|
-
elif not static_kv:
|
238
|
-
# for decoder self-attention in inference
|
239
|
-
cache_k, cache_v = cache["k"], cache["v"]
|
240
|
-
k = paddle.concat([cache_k, k], axis=2)
|
241
|
-
v = paddle.concat([cache_v, v], axis=2)
|
242
|
-
cache["k"], cache["v"] = k, v
|
243
|
-
|
244
|
-
return q, k, v
|
245
|
-
|
246
|
-
def forward(self, queries, keys, values, attn_bias, cache=None):
|
247
|
-
# compute q ,k ,v
|
248
|
-
keys = queries if keys is None else keys
|
249
|
-
values = keys if values is None else values
|
250
|
-
q, k, v = self._prepare_qkv(queries, keys, values, cache)
|
251
|
-
|
252
|
-
# scale dot product attention
|
253
|
-
product = paddle.matmul(x=q, y=k, transpose_y=True)
|
254
|
-
product = product * self.d_model**-0.5
|
255
|
-
if attn_bias is not None:
|
256
|
-
product += attn_bias
|
257
|
-
weights = F.softmax(product)
|
258
|
-
if self.dropout_rate:
|
259
|
-
weights = F.dropout(
|
260
|
-
weights, p=self.dropout_rate, mode="downscale_in_infer")
|
261
|
-
out = paddle.matmul(weights, v)
|
262
|
-
|
263
|
-
# combine heads
|
264
|
-
out = paddle.transpose(out, perm=[0, 2, 1, 3])
|
265
|
-
out = paddle.reshape(x=out, shape=[0, 0, out.shape[2] * out.shape[3]])
|
266
|
-
|
267
|
-
# project to output
|
268
|
-
out = self.proj_fc(out)
|
269
|
-
|
270
|
-
return out
|
271
|
-
|
272
|
-
|
273
|
-
class PrePostProcessLayer(nn.Layer):
|
274
|
-
"""
|
275
|
-
PrePostProcessLayer
|
276
|
-
"""
|
277
|
-
|
278
|
-
def __init__(self, process_cmd, d_model, dropout_rate):
|
279
|
-
super(PrePostProcessLayer, self).__init__()
|
280
|
-
self.process_cmd = process_cmd
|
281
|
-
self.functors = []
|
282
|
-
for cmd in self.process_cmd:
|
283
|
-
if cmd == "a": # add residual connection
|
284
|
-
self.functors.append(lambda x, y: x + y if y is not None else x)
|
285
|
-
elif cmd == "n": # add layer normalization
|
286
|
-
self.functors.append(
|
287
|
-
self.add_sublayer(
|
288
|
-
"layer_norm_%d" % len(self.sublayers()),
|
289
|
-
paddle.nn.LayerNorm(
|
290
|
-
normalized_shape=d_model,
|
291
|
-
weight_attr=fluid.ParamAttr(
|
292
|
-
initializer=fluid.initializer.Constant(1.)),
|
293
|
-
bias_attr=fluid.ParamAttr(
|
294
|
-
initializer=fluid.initializer.Constant(0.)))))
|
295
|
-
elif cmd == "d": # add dropout
|
296
|
-
self.functors.append(lambda x: F.dropout(
|
297
|
-
x, p=dropout_rate, mode="downscale_in_infer")
|
298
|
-
if dropout_rate else x)
|
299
|
-
|
300
|
-
def forward(self, x, residual=None):
|
301
|
-
for i, cmd in enumerate(self.process_cmd):
|
302
|
-
if cmd == "a":
|
303
|
-
x = self.functors[i](x, residual)
|
304
|
-
else:
|
305
|
-
x = self.functors[i](x)
|
306
|
-
return x
|
307
|
-
|
308
|
-
|
309
|
-
class PrepareEncoder(nn.Layer):
|
310
|
-
def __init__(self,
|
311
|
-
src_vocab_size,
|
312
|
-
src_emb_dim,
|
313
|
-
src_max_len,
|
314
|
-
dropout_rate=0,
|
315
|
-
bos_idx=0,
|
316
|
-
word_emb_param_name=None,
|
317
|
-
pos_enc_param_name=None):
|
318
|
-
super(PrepareEncoder, self).__init__()
|
319
|
-
self.src_emb_dim = src_emb_dim
|
320
|
-
self.src_max_len = src_max_len
|
321
|
-
self.emb = paddle.nn.Embedding(
|
322
|
-
num_embeddings=self.src_max_len, embedding_dim=self.src_emb_dim)
|
323
|
-
self.dropout_rate = dropout_rate
|
324
|
-
|
325
|
-
def forward(self, src_word, src_pos):
|
326
|
-
src_word_emb = src_word
|
327
|
-
src_word_emb = fluid.layers.cast(src_word_emb, 'float32')
|
328
|
-
src_word_emb = paddle.scale(x=src_word_emb, scale=self.src_emb_dim**0.5)
|
329
|
-
src_pos = paddle.squeeze(src_pos, axis=-1)
|
330
|
-
src_pos_enc = self.emb(src_pos)
|
331
|
-
src_pos_enc.stop_gradient = True
|
332
|
-
enc_input = src_word_emb + src_pos_enc
|
333
|
-
if self.dropout_rate:
|
334
|
-
out = F.dropout(
|
335
|
-
x=enc_input, p=self.dropout_rate, mode="downscale_in_infer")
|
336
|
-
else:
|
337
|
-
out = enc_input
|
338
|
-
return out
|
339
|
-
|
340
|
-
|
341
|
-
class PrepareDecoder(nn.Layer):
|
342
|
-
def __init__(self,
|
343
|
-
src_vocab_size,
|
344
|
-
src_emb_dim,
|
345
|
-
src_max_len,
|
346
|
-
dropout_rate=0,
|
347
|
-
bos_idx=0,
|
348
|
-
word_emb_param_name=None,
|
349
|
-
pos_enc_param_name=None):
|
350
|
-
super(PrepareDecoder, self).__init__()
|
351
|
-
self.src_emb_dim = src_emb_dim
|
352
|
-
"""
|
353
|
-
self.emb0 = Embedding(num_embeddings=src_vocab_size,
|
354
|
-
embedding_dim=src_emb_dim)
|
355
|
-
"""
|
356
|
-
self.emb0 = paddle.nn.Embedding(
|
357
|
-
num_embeddings=src_vocab_size,
|
358
|
-
embedding_dim=self.src_emb_dim,
|
359
|
-
padding_idx=bos_idx,
|
360
|
-
weight_attr=paddle.ParamAttr(
|
361
|
-
name=word_emb_param_name,
|
362
|
-
initializer=nn.initializer.Normal(0., src_emb_dim**-0.5)))
|
363
|
-
self.emb1 = paddle.nn.Embedding(
|
364
|
-
num_embeddings=src_max_len,
|
365
|
-
embedding_dim=self.src_emb_dim,
|
366
|
-
weight_attr=paddle.ParamAttr(name=pos_enc_param_name))
|
367
|
-
self.dropout_rate = dropout_rate
|
368
|
-
|
369
|
-
def forward(self, src_word, src_pos):
|
370
|
-
src_word = fluid.layers.cast(src_word, 'int64')
|
371
|
-
src_word = paddle.squeeze(src_word, axis=-1)
|
372
|
-
src_word_emb = self.emb0(src_word)
|
373
|
-
src_word_emb = paddle.scale(x=src_word_emb, scale=self.src_emb_dim**0.5)
|
374
|
-
src_pos = paddle.squeeze(src_pos, axis=-1)
|
375
|
-
src_pos_enc = self.emb1(src_pos)
|
376
|
-
src_pos_enc.stop_gradient = True
|
377
|
-
enc_input = src_word_emb + src_pos_enc
|
378
|
-
if self.dropout_rate:
|
379
|
-
out = F.dropout(
|
380
|
-
x=enc_input, p=self.dropout_rate, mode="downscale_in_infer")
|
381
|
-
else:
|
382
|
-
out = enc_input
|
383
|
-
return out
|
384
|
-
|
385
|
-
|
386
|
-
class FFN(nn.Layer):
|
387
|
-
"""
|
388
|
-
Feed-Forward Network
|
389
|
-
"""
|
390
|
-
|
391
|
-
def __init__(self, d_inner_hid, d_model, dropout_rate):
|
392
|
-
super(FFN, self).__init__()
|
393
|
-
self.dropout_rate = dropout_rate
|
394
|
-
self.fc1 = paddle.nn.Linear(
|
395
|
-
in_features=d_model, out_features=d_inner_hid)
|
396
|
-
self.fc2 = paddle.nn.Linear(
|
397
|
-
in_features=d_inner_hid, out_features=d_model)
|
398
|
-
|
399
|
-
def forward(self, x):
|
400
|
-
hidden = self.fc1(x)
|
401
|
-
hidden = F.relu(hidden)
|
402
|
-
if self.dropout_rate:
|
403
|
-
hidden = F.dropout(
|
404
|
-
hidden, p=self.dropout_rate, mode="downscale_in_infer")
|
405
|
-
out = self.fc2(hidden)
|
406
|
-
return out
|
@@ -1,246 +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
|
-
from __future__ import absolute_import
|
16
|
-
from __future__ import division
|
17
|
-
from __future__ import print_function
|
18
|
-
|
19
|
-
import paddle
|
20
|
-
import paddle.nn as nn
|
21
|
-
import paddle.nn.functional as F
|
22
|
-
import numpy as np
|
23
|
-
|
24
|
-
|
25
|
-
class TableAttentionHead(nn.Layer):
|
26
|
-
def __init__(self,
|
27
|
-
in_channels,
|
28
|
-
hidden_size,
|
29
|
-
loc_type,
|
30
|
-
in_max_len=488,
|
31
|
-
max_text_length=100,
|
32
|
-
max_elem_length=800,
|
33
|
-
max_cell_num=500,
|
34
|
-
**kwargs):
|
35
|
-
super(TableAttentionHead, self).__init__()
|
36
|
-
self.input_size = in_channels[-1]
|
37
|
-
self.hidden_size = hidden_size
|
38
|
-
self.elem_num = 30
|
39
|
-
self.max_text_length = max_text_length
|
40
|
-
self.max_elem_length = max_elem_length
|
41
|
-
self.max_cell_num = max_cell_num
|
42
|
-
|
43
|
-
self.structure_attention_cell = AttentionGRUCell(
|
44
|
-
self.input_size, hidden_size, self.elem_num, use_gru=False)
|
45
|
-
self.structure_generator = nn.Linear(hidden_size, self.elem_num)
|
46
|
-
self.loc_type = loc_type
|
47
|
-
self.in_max_len = in_max_len
|
48
|
-
|
49
|
-
if self.loc_type == 1:
|
50
|
-
self.loc_generator = nn.Linear(hidden_size, 4)
|
51
|
-
else:
|
52
|
-
if self.in_max_len == 640:
|
53
|
-
self.loc_fea_trans = nn.Linear(400, self.max_elem_length + 1)
|
54
|
-
elif self.in_max_len == 800:
|
55
|
-
self.loc_fea_trans = nn.Linear(625, self.max_elem_length + 1)
|
56
|
-
else:
|
57
|
-
self.loc_fea_trans = nn.Linear(256, self.max_elem_length + 1)
|
58
|
-
self.loc_generator = nn.Linear(self.input_size + hidden_size, 4)
|
59
|
-
|
60
|
-
def _char_to_onehot(self, input_char, onehot_dim):
|
61
|
-
input_ont_hot = F.one_hot(input_char, onehot_dim)
|
62
|
-
return input_ont_hot
|
63
|
-
|
64
|
-
def forward(self, inputs, targets=None):
|
65
|
-
# if and else branch are both needed when you want to assign a variable
|
66
|
-
# if you modify the var in just one branch, then the modification will not work.
|
67
|
-
fea = inputs[-1]
|
68
|
-
if len(fea.shape) == 3:
|
69
|
-
pass
|
70
|
-
else:
|
71
|
-
last_shape = int(np.prod(fea.shape[2:])) # gry added
|
72
|
-
fea = paddle.reshape(fea, [fea.shape[0], fea.shape[1], last_shape])
|
73
|
-
fea = fea.transpose([0, 2, 1]) # (NTC)(batch, width, channels)
|
74
|
-
batch_size = fea.shape[0]
|
75
|
-
|
76
|
-
hidden = paddle.zeros((batch_size, self.hidden_size))
|
77
|
-
output_hiddens = []
|
78
|
-
if self.training and targets is not None:
|
79
|
-
structure = targets[0]
|
80
|
-
for i in range(self.max_elem_length + 1):
|
81
|
-
elem_onehots = self._char_to_onehot(
|
82
|
-
structure[:, i], onehot_dim=self.elem_num)
|
83
|
-
(outputs, hidden), alpha = self.structure_attention_cell(
|
84
|
-
hidden, fea, elem_onehots)
|
85
|
-
output_hiddens.append(paddle.unsqueeze(outputs, axis=1))
|
86
|
-
output = paddle.concat(output_hiddens, axis=1)
|
87
|
-
structure_probs = self.structure_generator(output)
|
88
|
-
if self.loc_type == 1:
|
89
|
-
loc_preds = self.loc_generator(output)
|
90
|
-
loc_preds = F.sigmoid(loc_preds)
|
91
|
-
else:
|
92
|
-
loc_fea = fea.transpose([0, 2, 1])
|
93
|
-
loc_fea = self.loc_fea_trans(loc_fea)
|
94
|
-
loc_fea = loc_fea.transpose([0, 2, 1])
|
95
|
-
loc_concat = paddle.concat([output, loc_fea], axis=2)
|
96
|
-
loc_preds = self.loc_generator(loc_concat)
|
97
|
-
loc_preds = F.sigmoid(loc_preds)
|
98
|
-
else:
|
99
|
-
temp_elem = paddle.zeros(shape=[batch_size], dtype="int32")
|
100
|
-
structure_probs = None
|
101
|
-
loc_preds = None
|
102
|
-
elem_onehots = None
|
103
|
-
outputs = None
|
104
|
-
alpha = None
|
105
|
-
max_elem_length = paddle.to_tensor(self.max_elem_length)
|
106
|
-
i = 0
|
107
|
-
while i < max_elem_length + 1:
|
108
|
-
elem_onehots = self._char_to_onehot(
|
109
|
-
temp_elem, onehot_dim=self.elem_num)
|
110
|
-
(outputs, hidden), alpha = self.structure_attention_cell(
|
111
|
-
hidden, fea, elem_onehots)
|
112
|
-
output_hiddens.append(paddle.unsqueeze(outputs, axis=1))
|
113
|
-
structure_probs_step = self.structure_generator(outputs)
|
114
|
-
temp_elem = structure_probs_step.argmax(axis=1, dtype="int32")
|
115
|
-
i += 1
|
116
|
-
|
117
|
-
output = paddle.concat(output_hiddens, axis=1)
|
118
|
-
structure_probs = self.structure_generator(output)
|
119
|
-
structure_probs = F.softmax(structure_probs)
|
120
|
-
if self.loc_type == 1:
|
121
|
-
loc_preds = self.loc_generator(output)
|
122
|
-
loc_preds = F.sigmoid(loc_preds)
|
123
|
-
else:
|
124
|
-
loc_fea = fea.transpose([0, 2, 1])
|
125
|
-
loc_fea = self.loc_fea_trans(loc_fea)
|
126
|
-
loc_fea = loc_fea.transpose([0, 2, 1])
|
127
|
-
loc_concat = paddle.concat([output, loc_fea], axis=2)
|
128
|
-
loc_preds = self.loc_generator(loc_concat)
|
129
|
-
loc_preds = F.sigmoid(loc_preds)
|
130
|
-
return {'structure_probs': structure_probs, 'loc_preds': loc_preds}
|
131
|
-
|
132
|
-
|
133
|
-
class AttentionGRUCell(nn.Layer):
|
134
|
-
def __init__(self, input_size, hidden_size, num_embeddings, use_gru=False):
|
135
|
-
super(AttentionGRUCell, self).__init__()
|
136
|
-
self.i2h = nn.Linear(input_size, hidden_size, bias_attr=False)
|
137
|
-
self.h2h = nn.Linear(hidden_size, hidden_size)
|
138
|
-
self.score = nn.Linear(hidden_size, 1, bias_attr=False)
|
139
|
-
self.rnn = nn.GRUCell(
|
140
|
-
input_size=input_size + num_embeddings, hidden_size=hidden_size)
|
141
|
-
self.hidden_size = hidden_size
|
142
|
-
|
143
|
-
def forward(self, prev_hidden, batch_H, char_onehots):
|
144
|
-
batch_H_proj = self.i2h(batch_H)
|
145
|
-
prev_hidden_proj = paddle.unsqueeze(self.h2h(prev_hidden), axis=1)
|
146
|
-
res = paddle.add(batch_H_proj, prev_hidden_proj)
|
147
|
-
res = paddle.tanh(res)
|
148
|
-
e = self.score(res)
|
149
|
-
alpha = F.softmax(e, axis=1)
|
150
|
-
alpha = paddle.transpose(alpha, [0, 2, 1])
|
151
|
-
context = paddle.squeeze(paddle.mm(alpha, batch_H), axis=1)
|
152
|
-
concat_context = paddle.concat([context, char_onehots], 1)
|
153
|
-
cur_hidden = self.rnn(concat_context, prev_hidden)
|
154
|
-
return cur_hidden, alpha
|
155
|
-
|
156
|
-
|
157
|
-
class AttentionLSTM(nn.Layer):
|
158
|
-
def __init__(self, in_channels, out_channels, hidden_size, **kwargs):
|
159
|
-
super(AttentionLSTM, self).__init__()
|
160
|
-
self.input_size = in_channels
|
161
|
-
self.hidden_size = hidden_size
|
162
|
-
self.num_classes = out_channels
|
163
|
-
|
164
|
-
self.attention_cell = AttentionLSTMCell(
|
165
|
-
in_channels, hidden_size, out_channels, use_gru=False)
|
166
|
-
self.generator = nn.Linear(hidden_size, out_channels)
|
167
|
-
|
168
|
-
def _char_to_onehot(self, input_char, onehot_dim):
|
169
|
-
input_ont_hot = F.one_hot(input_char, onehot_dim)
|
170
|
-
return input_ont_hot
|
171
|
-
|
172
|
-
def forward(self, inputs, targets=None, batch_max_length=25):
|
173
|
-
batch_size = inputs.shape[0]
|
174
|
-
num_steps = batch_max_length
|
175
|
-
|
176
|
-
hidden = (paddle.zeros((batch_size, self.hidden_size)), paddle.zeros(
|
177
|
-
(batch_size, self.hidden_size)))
|
178
|
-
output_hiddens = []
|
179
|
-
|
180
|
-
if targets is not None:
|
181
|
-
for i in range(num_steps):
|
182
|
-
# one-hot vectors for a i-th char
|
183
|
-
char_onehots = self._char_to_onehot(
|
184
|
-
targets[:, i], onehot_dim=self.num_classes)
|
185
|
-
hidden, alpha = self.attention_cell(hidden, inputs,
|
186
|
-
char_onehots)
|
187
|
-
|
188
|
-
hidden = (hidden[1][0], hidden[1][1])
|
189
|
-
output_hiddens.append(paddle.unsqueeze(hidden[0], axis=1))
|
190
|
-
output = paddle.concat(output_hiddens, axis=1)
|
191
|
-
probs = self.generator(output)
|
192
|
-
|
193
|
-
else:
|
194
|
-
targets = paddle.zeros(shape=[batch_size], dtype="int32")
|
195
|
-
probs = None
|
196
|
-
|
197
|
-
for i in range(num_steps):
|
198
|
-
char_onehots = self._char_to_onehot(
|
199
|
-
targets, onehot_dim=self.num_classes)
|
200
|
-
hidden, alpha = self.attention_cell(hidden, inputs,
|
201
|
-
char_onehots)
|
202
|
-
probs_step = self.generator(hidden[0])
|
203
|
-
hidden = (hidden[1][0], hidden[1][1])
|
204
|
-
if probs is None:
|
205
|
-
probs = paddle.unsqueeze(probs_step, axis=1)
|
206
|
-
else:
|
207
|
-
probs = paddle.concat(
|
208
|
-
[probs, paddle.unsqueeze(
|
209
|
-
probs_step, axis=1)], axis=1)
|
210
|
-
|
211
|
-
next_input = probs_step.argmax(axis=1)
|
212
|
-
|
213
|
-
targets = next_input
|
214
|
-
|
215
|
-
return probs
|
216
|
-
|
217
|
-
|
218
|
-
class AttentionLSTMCell(nn.Layer):
|
219
|
-
def __init__(self, input_size, hidden_size, num_embeddings, use_gru=False):
|
220
|
-
super(AttentionLSTMCell, self).__init__()
|
221
|
-
self.i2h = nn.Linear(input_size, hidden_size, bias_attr=False)
|
222
|
-
self.h2h = nn.Linear(hidden_size, hidden_size)
|
223
|
-
self.score = nn.Linear(hidden_size, 1, bias_attr=False)
|
224
|
-
if not use_gru:
|
225
|
-
self.rnn = nn.LSTMCell(
|
226
|
-
input_size=input_size + num_embeddings, hidden_size=hidden_size)
|
227
|
-
else:
|
228
|
-
self.rnn = nn.GRUCell(
|
229
|
-
input_size=input_size + num_embeddings, hidden_size=hidden_size)
|
230
|
-
|
231
|
-
self.hidden_size = hidden_size
|
232
|
-
|
233
|
-
def forward(self, prev_hidden, batch_H, char_onehots):
|
234
|
-
batch_H_proj = self.i2h(batch_H)
|
235
|
-
prev_hidden_proj = paddle.unsqueeze(self.h2h(prev_hidden[0]), axis=1)
|
236
|
-
res = paddle.add(batch_H_proj, prev_hidden_proj)
|
237
|
-
res = paddle.tanh(res)
|
238
|
-
e = self.score(res)
|
239
|
-
|
240
|
-
alpha = F.softmax(e, axis=1)
|
241
|
-
alpha = paddle.transpose(alpha, [0, 2, 1])
|
242
|
-
context = paddle.squeeze(paddle.mm(alpha, batch_H), axis=1)
|
243
|
-
concat_context = paddle.concat([context, char_onehots], 1)
|
244
|
-
cur_hidden = self.rnn(concat_context, prev_hidden)
|
245
|
-
|
246
|
-
return cur_hidden, alpha
|
@@ -1,32 +0,0 @@
|
|
1
|
-
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
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
|
-
__all__ = ['build_neck']
|
16
|
-
|
17
|
-
|
18
|
-
def build_neck(config):
|
19
|
-
from .db_fpn import DBFPN
|
20
|
-
from .east_fpn import EASTFPN
|
21
|
-
from .sast_fpn import SASTFPN
|
22
|
-
from .rnn import SequenceEncoder
|
23
|
-
from .pg_fpn import PGFPN
|
24
|
-
from .table_fpn import TableFPN
|
25
|
-
from .fpn import FPN
|
26
|
-
support_dict = ['FPN','DBFPN', 'EASTFPN', 'SASTFPN', 'SequenceEncoder', 'PGFPN', 'TableFPN']
|
27
|
-
|
28
|
-
module_name = config.pop('name')
|
29
|
-
assert module_name in support_dict, Exception('neck only support {}'.format(
|
30
|
-
support_dict))
|
31
|
-
module_class = eval(module_name)(**config)
|
32
|
-
return module_class
|