forked from PaddlePaddle/PaddleOCR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* v4rec code * v4rec add nrtrloss * Add V4rec backbone file * Add V4Rec config file. * Fix V4rec reparameters when export_model * convert lvnetv3 * fix codestyle * fix infer_rec v4rec
- Loading branch information
Showing
15 changed files
with
787 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
Global: | ||
debug: false | ||
use_gpu: true | ||
epoch_num: 200 | ||
log_smooth_window: 20 | ||
print_batch_step: 10 | ||
save_model_dir: ./output/rec_ppocr_v4 | ||
save_epoch_step: 10 | ||
eval_batch_step: [0, 2000] | ||
cal_metric_during_train: true | ||
pretrained_model: | ||
checkpoints: | ||
save_inference_dir: | ||
use_visualdl: false | ||
infer_img: doc/imgs_words/ch/word_1.jpg | ||
character_dict_path: ppocr/utils/ppocr_keys_v1.txt | ||
max_text_length: &max_text_length 25 | ||
infer_mode: false | ||
use_space_char: true | ||
distributed: true | ||
save_res_path: ./output/rec/predicts_ppocrv3.txt | ||
|
||
|
||
Optimizer: | ||
name: Adam | ||
beta1: 0.9 | ||
beta2: 0.999 | ||
lr: | ||
name: Cosine | ||
learning_rate: 0.001 | ||
warmup_epoch: 5 | ||
regularizer: | ||
name: L2 | ||
factor: 3.0e-05 | ||
|
||
|
||
Architecture: | ||
model_type: rec | ||
algorithm: SVTR_LCNet | ||
Transform: | ||
Backbone: | ||
name: LCNetv3 | ||
Head: | ||
name: MultiHead | ||
head_list: | ||
- CTCHead: | ||
Neck: | ||
name: svtr | ||
dims: 120 | ||
depth: 2 | ||
hidden_dims: 120 | ||
kernel_size: [1, 3] | ||
use_guide: True | ||
Head: | ||
fc_decay: 0.00001 | ||
- NRTRHead: | ||
nrtr_dim: 384 | ||
max_text_length: *max_text_length | ||
|
||
Loss: | ||
name: MultiLoss | ||
loss_config_list: | ||
- CTCLoss: | ||
- NRTRLoss: | ||
|
||
PostProcess: | ||
name: CTCLabelDecode | ||
|
||
Metric: | ||
name: RecMetric | ||
main_indicator: acc | ||
|
||
Train: | ||
dataset: | ||
name: SimpleDataSet | ||
data_dir: ./train_data/ | ||
ext_op_transform_idx: 1 | ||
label_file_list: | ||
- ./train_data/train_list.txt | ||
transforms: | ||
- DecodeImage: | ||
img_mode: BGR | ||
channel_first: false | ||
- RecConAug: | ||
prob: 0.5 | ||
ext_data_num: 2 | ||
image_shape: [48, 320, 3] | ||
max_text_length: *max_text_length | ||
- RecAug: | ||
- MultiLabelEncode: | ||
gtc_encode: NRTRLabelEncode | ||
- RecResizeImg: | ||
image_shape: [3, 48, 320] | ||
- KeepKeys: | ||
keep_keys: | ||
- image | ||
- label_ctc | ||
- label_gtc | ||
- length | ||
- valid_ratio | ||
loader: | ||
shuffle: true | ||
batch_size_per_card: 128 | ||
drop_last: true | ||
num_workers: 4 | ||
Eval: | ||
dataset: | ||
name: SimpleDataSet | ||
data_dir: ./train_data | ||
label_file_list: | ||
- ./train_data/val_list.txt | ||
transforms: | ||
- DecodeImage: | ||
img_mode: BGR | ||
channel_first: false | ||
- MultiLabelEncode: | ||
gtc_encode: NRTRLabelEncode | ||
- RecResizeImg: | ||
image_shape: [3, 48, 320] | ||
- KeepKeys: | ||
keep_keys: | ||
- image | ||
- label_ctc | ||
- label_gtc | ||
- length | ||
- valid_ratio | ||
loader: | ||
shuffle: false | ||
drop_last: false | ||
batch_size_per_card: 128 | ||
num_workers: 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import paddle | ||
from paddle import nn | ||
import paddle.nn.functional as F | ||
|
||
|
||
class NRTRLoss(nn.Layer): | ||
def __init__(self, smoothing=True, ignore_index=0, **kwargs): | ||
super(NRTRLoss, self).__init__() | ||
if ignore_index >= 0 and not smoothing: | ||
self.loss_func = nn.CrossEntropyLoss( | ||
reduction='mean', ignore_index=ignore_index) | ||
self.smoothing = smoothing | ||
|
||
def forward(self, pred, batch): | ||
max_len = batch[2].max() | ||
tgt = batch[1][:, 1:2 + max_len] | ||
pred = pred.reshape([-1, pred.shape[2]]) | ||
tgt = tgt.reshape([-1]) | ||
if self.smoothing: | ||
eps = 0.1 | ||
n_class = pred.shape[1] | ||
one_hot = F.one_hot(tgt, pred.shape[1]) | ||
one_hot = one_hot * (1 - eps) + (1 - one_hot) * eps / (n_class - 1) | ||
log_prb = F.log_softmax(pred, axis=1) | ||
non_pad_mask = paddle.not_equal( | ||
tgt, paddle.zeros( | ||
tgt.shape, dtype=tgt.dtype)) | ||
loss = -(one_hot * log_prb).sum(axis=1) | ||
loss = loss.masked_select(non_pad_mask).mean() | ||
else: | ||
loss = self.loss_func(pred, tgt) | ||
return {'loss': loss} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.