Skip to content

Commit

Permalink
add d2t train support of svtr (PaddlePaddle#9479)
Browse files Browse the repository at this point in the history
* add d2s train for slanet and v3

* fix bug

* udpate tipc to_static

* update db

* remove_print

* update benchmark_train.sh

* update maybe_download_params

* add d2t train support of svtr
  • Loading branch information
WenmuZhou authored Mar 20, 2023
1 parent 6001b1a commit 20cb993
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions configs/rec/rec_svtrnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Global:
infer_mode: False
use_space_char: False
save_res_path: ./output/rec/predicts_svtr_tiny.txt
d2s_train_image_shape: [3, 64, 256]


Optimizer:
Expand Down
1 change: 1 addition & 0 deletions configs/rec/rec_svtrnet_ch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Global:
infer_mode: false
use_space_char: true
save_res_path: ./output/rec/predicts_svtr_tiny_ch_all.txt
d2s_train_image_shape: [3, 32, 320]
Optimizer:
name: AdamW
beta1: 0.9
Expand Down
11 changes: 10 additions & 1 deletion ppocr/modeling/architectures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def apply_to_static(model, config, logger):
return model
assert "d2s_train_image_shape" in config[
"Global"], "d2s_train_image_shape must be assigned for static training mode..."
supported_list = ["DB", "SVTR_LCNet", "TableMaster", "LayoutXLM", "SLANet"]
supported_list = [
"DB", "SVTR_LCNet", "TableMaster", "LayoutXLM", "SLANet", "SVTR"
]
if config["Architecture"]["algorithm"] in ["Distillation"]:
algo = list(config["Architecture"]["Models"].values())[0]["algorithm"]
else:
Expand Down Expand Up @@ -104,6 +106,13 @@ def apply_to_static(model, config, logger):
InputSpec(
[None, 6], dtype='float64'),
])
elif algo == "SVTR":
specs.append([
InputSpec(
[None, config["Global"]["max_text_length"]], dtype='int64'),
InputSpec(
[None], dtype='int64')
])
model = to_static(model, input_spec=specs)
logger.info("Successfully to apply @to_static with specs: {}".format(specs))
return model
6 changes: 4 additions & 2 deletions ppocr/modeling/transforms/tps_spatial_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def forward(self, input, source_control_points):

padding_matrix = paddle.expand(
self.padding_matrix, shape=[batch_size, 3, 2])
Y = paddle.concat([source_control_points, padding_matrix], 1)
Y = paddle.concat([
source_control_points.astype(padding_matrix.dtype), padding_matrix
], 1)
mapping_matrix = paddle.matmul(self.inverse_kernel, Y)
source_coordinate = paddle.matmul(self.target_coordinate_repr,
mapping_matrix)
Expand All @@ -153,4 +155,4 @@ def forward(self, input, source_control_points):
# the input to grid_sample is normalized [-1, 1], but what we get is [0, 1]
grid = 2.0 * grid - 1.0
output_maps = grid_sample(input, grid, canvas=None)
return output_maps, source_coordinate
return output_maps, source_coordinate
1 change: 1 addition & 0 deletions test_tipc/configs/rec_svtrnet/rec_svtrnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Global:
infer_mode: False
use_space_char: False
save_res_path: ./output/rec/predicts_svtr_tiny.txt
d2s_train_image_shape: [3, 64, 256]


Optimizer:
Expand Down
8 changes: 8 additions & 0 deletions test_tipc/configs/rec_svtrnet/train_infer_python.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ inference:tools/infer/predict_rec.py --rec_char_dict_path=./ppocr/utils/ic15_dic
null:null
===========================infer_benchmark_params==========================
random_infer_input:[{float32,[3,64,256]}]
===========================train_benchmark_params==========================
batch_size:512
fp_items:fp32|fp16
epoch:2
--profiler_options:batch_range=[10,20];state=GPU;tracer_option=Default;profile_path=model.profile
flags:FLAGS_eager_delete_tensor_gb=0.0;FLAGS_fraction_of_gpu_memory_to_use=0.98;FLAGS_conv_workspace_size_limit=4096
===========================to_static_train_benchmark_params===========================
to_static_train:Global.to_static=true
13 changes: 13 additions & 0 deletions test_tipc/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ if [ ${MODE} = "benchmark_train" ];then
cd ./train_data/ && tar xf StructureLabel_val_500.tar
cd ../
fi
if [ ${model_name} == "rec_svtrnet" ]; then
wget -nc -P ./train_data/ https://paddleocr.bj.bcebos.com/dataset/ic15_data_benckmark.tar --no-check-certificate
cd ./train_data/ && tar xf ic15_data_benckmark.tar
ln -s ./ic15_data_benckmark ./ic15_data
cd ic15_data
mv rec_gt_train4w.txt rec_gt_train.txt

for i in `seq 10`;do cp rec_gt_train.txt dup$i.txt;done
cat dup* > rec_gt_train.txt && rm -rf dup*

cd ../
cd ../
fi
fi

if [ ${MODE} = "lite_train_lite_infer" ];then
Expand Down

0 comments on commit 20cb993

Please sign in to comment.