PaddleOCR/doc/doc_en/recognition_en.md

435 lines
17 KiB
Markdown
Raw Normal View History

2020-06-24 18:13:03 +08:00
## TEXT RECOGNITION
2020-06-08 10:16:26 +08:00
2021-02-03 11:58:24 +08:00
- [1 DATA PREPARATION](#DATA_PREPARATION)
- [1.1 Costom Dataset](#Costom_Dataset)
- [1.2 Dataset Download](#Dataset_download)
- [1.3 Dictionary](#Dictionary)
- [1.4 Add Space Category](#Add_space_category)
2020-10-13 17:49:16 +08:00
2021-02-03 11:58:24 +08:00
- [2 TRAINING](#TRAINING)
- [2.1 Data Augmentation](#Data_Augmentation)
2021-09-06 17:00:51 +08:00
- [2.2 General Training](#Training)
- [2.3 Multi-language Training](#Multi_language)
2020-10-13 17:49:16 +08:00
2021-02-03 11:58:24 +08:00
- [3 EVALUATION](#EVALUATION)
2020-10-13 17:49:16 +08:00
2021-02-03 11:58:24 +08:00
- [4 PREDICTION](#PREDICTION)
- [4.1 Training engine prediction](#Training_engine_prediction)
2020-10-13 17:49:16 +08:00
<a name="DATA_PREPARATION"></a>
2020-06-24 18:13:03 +08:00
### DATA PREPARATION
2020-06-08 10:16:26 +08:00
2021-02-03 11:58:24 +08:00
PaddleOCR supports two data formats:
2021-09-06 17:00:51 +08:00
- `LMDB` is used to train data sets stored in lmdb formatLMDBDataSet;
- `general data` is used to train data sets stored in text filesSimpleDataSet:
2020-06-08 10:16:26 +08:00
Please organize the dataset as follows:
The default storage path for training data is `PaddleOCR/train_data`, if you already have a dataset on your disk, just create a soft link to the dataset directory:
```
2021-02-03 11:58:24 +08:00
# linux and mac os
ln -sf <path/to/dataset> <path/to/paddle_ocr>/train_data/dataset
2021-02-03 11:58:24 +08:00
# windows
mklink /d <path/to/paddle_ocr>/train_data/dataset <path/to/dataset>
2020-06-08 10:16:26 +08:00
```
2020-10-13 17:49:16 +08:00
<a name="Costom_Dataset"></a>
2021-02-03 11:58:24 +08:00
#### 1.1 Costom dataset
2020-06-08 10:16:26 +08:00
If you want to use your own data for training, please refer to the following to organize your data.
- Training set
2021-02-03 11:58:24 +08:00
It is recommended to put the training images in the same folder, and use a txt file (rec_gt_train.txt) to store the image path and label. The contents of the txt file are as follows:
2020-06-08 10:16:26 +08:00
* Note: by default, the image path and image label are split with \t, if you use other methods to split, it will cause training error
```
" Image file name Image annotation "
2021-02-03 12:05:20 +08:00
train_data/rec/train/word_001.jpg 简单可依赖
train_data/rec/train/word_002.jpg 用科技让复杂的世界更简单
2021-02-03 11:58:24 +08:00
...
2020-06-08 10:16:26 +08:00
```
The final training set should have the following file structure:
```
|-train_data
2021-02-03 12:05:20 +08:00
|-rec
2021-02-03 11:58:24 +08:00
|- rec_gt_train.txt
|- train
|- word_001.png
|- word_002.jpg
|- word_003.jpg
| ...
2020-06-08 10:16:26 +08:00
```
- Test set
Similar to the training set, the test set also needs to be provided a folder containing all images (test) and a rec_gt_test.txt. The structure of the test set is as follows:
```
|-train_data
2021-02-03 12:05:20 +08:00
|-rec
2020-06-08 10:16:26 +08:00
|-ic15_data
|- rec_gt_test.txt
|- test
|- word_001.jpg
|- word_002.jpg
|- word_003.jpg
| ...
```
2021-02-03 11:58:24 +08:00
<a name="Dataset_download"></a>
#### 1.2 Dataset download
2021-09-06 17:00:51 +08:00
- ICDAR2015
2021-02-03 11:58:24 +08:00
2021-09-06 17:00:51 +08:00
If you do not have a dataset locally, you can download it on the official website [icdar2015](http://rrc.cvc.uab.es/?ch=4&com=downloads).
Also refer to [DTRB](https://github.com/clovaai/deep-text-recognition-benchmark#download-lmdb-dataset-for-traininig-and-evaluation-from-here) download the lmdb format dataset required for benchmark
2021-02-03 11:58:24 +08:00
PaddleOCR provides label files for training the icdar2015 dataset, which can be downloaded in the following ways:
```
# Training set label
wget -P ./train_data/ic15_data https://paddleocr.bj.bcebos.com/dataset/rec_gt_train.txt
# Test Set Label
wget -P ./train_data/ic15_data https://paddleocr.bj.bcebos.com/dataset/rec_gt_test.txt
```
2021-09-06 17:00:51 +08:00
PaddleOCR also provides a data format conversion script, which can convert ICDAR official website label to a data format
supported by PaddleOCR. The data conversion tool is in `ppocr/utils/gen_label.py`, here is the training set as an example:
```
# convert the official gt to rec_gt_label.txt
python gen_label.py --mode="rec" --input_path="{path/of/origin/label}" --output_label="rec_gt_label.txt"
```
The data format is as follows, (a) is the original picture, (b) is the Ground Truth text file corresponding to each picture:
![](../datasets/icdar_rec.png)
- Multilingual dataset
The multi-language model training method is the same as the Chinese model. The training data set is 100w synthetic data. A small amount of fonts and test data can be downloaded using the following two methods.
* [Baidu Netdisk](https://pan.baidu.com/s/1bS_u207Rm7YbY33wOECKDA) ,Extraction code:frgi.
* [Google drive](https://drive.google.com/file/d/18cSWX7wXSy4G0tbKJ0d9PuIaiwRLHpjA/view)
2020-10-13 17:49:16 +08:00
<a name="Dictionary"></a>
2021-02-03 11:58:24 +08:00
#### 1.3 Dictionary
2020-06-08 10:16:26 +08:00
Finally, a dictionary ({word_dict_name}.txt) needs to be provided so that when the model is trained, all the characters that appear can be mapped to the dictionary index.
Therefore, the dictionary needs to contain all the characters that you want to be recognized correctly. {word_dict_name}.txt needs to be written in the following format and saved in the `utf-8` encoding format:
```
l
d
a
d
r
n
```
In `word_dict.txt`, there is a single word in each line, which maps characters and numeric indexes together, e.g "and" will be mapped to [2 5 1]
2021-02-03 11:58:24 +08:00
PaddleOCR has built-in dictionaries, which can be used on demand.
2020-06-08 10:16:26 +08:00
`ppocr/utils/ppocr_keys_v1.txt` is a Chinese dictionary with 6623 characters.
2020-10-13 17:49:16 +08:00
`ppocr/utils/ic15_dict.txt` is an English dictionary with 63 characters
`ppocr/utils/dict/french_dict.txt` is a French dictionary with 118 characters
`ppocr/utils/dict/japan_dict.txt` is a Japanese dictionary with 4399 characters
2020-10-13 17:49:16 +08:00
2020-12-11 17:50:01 +08:00
`ppocr/utils/dict/korean_dict.txt` is a Korean dictionary with 3636 characters
2020-10-13 17:49:16 +08:00
2020-12-11 17:50:01 +08:00
`ppocr/utils/dict/german_dict.txt` is a German dictionary with 131 characters
2021-04-13 17:54:10 +08:00
`ppocr/utils/en_dict.txt` is a English dictionary with 96 characters
2020-10-13 17:49:16 +08:00
2020-10-13 17:49:16 +08:00
The current multi-language model is still in the demo stage and will continue to optimize the model and add languages. **You are very welcome to provide us with dictionaries and fonts in other languages**,
2021-02-01 22:03:32 +08:00
If you like, you can submit the dictionary file to [dict](../../ppocr/utils/dict) and we will thank you in the Repo.
2020-06-08 10:16:26 +08:00
To customize the dict file, please modify the `character_dict_path` field in `configs/rec/rec_icdar15_train.yml` and set `character_type` to `ch`.
2020-07-17 13:01:01 +08:00
- Custom dictionary
If you need to customize dic file, please add character_dict_path field in configs/rec/rec_icdar15_train.yml to point to your dictionary path. And set character_type to ch.
2020-10-13 17:49:16 +08:00
<a name="Add_space_category"></a>
2021-02-03 11:58:24 +08:00
#### 1.4 Add space category
2020-07-17 13:01:01 +08:00
2020-12-10 18:43:27 +08:00
If you want to support the recognition of the `space` category, please set the `use_space_char` field in the yml file to `True`.
2020-07-17 13:01:01 +08:00
**Note: use_space_char only takes effect when character_type=ch**
2020-10-13 17:49:16 +08:00
<a name="TRAINING"></a>
2021-02-03 11:58:24 +08:00
### 2 TRAINING
2020-06-08 10:16:26 +08:00
2021-09-06 17:00:51 +08:00
<a name="Data_Augmentation"></a>
#### 2.1 Data Augmentation
PaddleOCR provides a variety of data augmentation methods. All the augmentation methods are enabled by default.
The default perturbation methods are: cvtColor, blur, jitter, Gasuss noise, random crop, perspective, color reverse, TIA augmentation.
Each disturbance method is selected with a 40% probability during the training process. For specific code implementation, please refer to: [rec_img_aug.py](../../ppocr/data/imaug/rec_img_aug.py)
<a name="Training"></a>
#### 2.2 General Training
2020-06-08 10:16:26 +08:00
PaddleOCR provides training scripts, evaluation scripts, and prediction scripts. In this section, the CRNN recognition model will be used as an example:
First download the pretrain model, you can download the trained model to finetune on the icdar2015 data:
```
cd PaddleOCR/
# Download the pre-trained model of MobileNetV3
2020-12-12 14:13:27 +08:00
wget -P ./pretrain_models/ https://paddleocr.bj.bcebos.com/dygraph_v2.0/en/rec_mv3_none_bilstm_ctc_v2.0_train.tar
2020-06-08 10:16:26 +08:00
# Decompress model parameters
cd pretrain_models
2020-12-12 14:13:27 +08:00
tar -xf rec_mv3_none_bilstm_ctc_v2.0_train.tar && rm -rf rec_mv3_none_bilstm_ctc_v2.0_train.tar
2020-06-08 10:16:26 +08:00
```
Start training:
```
2021-09-06 17:00:51 +08:00
# GPU training Support single card and multi-card training
2020-12-15 19:56:42 +08:00
# Training icdar15 English data and The training log will be automatically saved as train.log under "{save_model_dir}"
2021-09-06 17:00:51 +08:00
#specify the single card training(Long training time, not recommended)
python3 tools/train.py -c configs/rec/rec_icdar15_train.yml
#specify the card number through --gpus
2020-12-09 20:45:56 +08:00
python3 -m paddle.distributed.launch --gpus '0,1,2,3' tools/train.py -c configs/rec/rec_icdar15_train.yml
2020-06-08 10:16:26 +08:00
```
2020-07-17 13:01:01 +08:00
2020-06-08 10:16:26 +08:00
PaddleOCR supports alternating training and evaluation. You can modify `eval_batch_step` in `configs/rec/rec_icdar15_train.yml` to set the evaluation frequency. By default, it is evaluated every 500 iter and the best acc model is saved under `output/rec_CRNN/best_accuracy` during the evaluation process.
If the evaluation set is large, the test will be time-consuming. It is recommended to reduce the number of evaluations, or evaluate after training.
* Tip: You can use the `-c` parameter to select multiple model configurations under the `configs/rec/` path for training. The recognition algorithms supported by PaddleOCR are:
| Configuration file | Algorithm | backbone | trans | seq | pred |
| :--------: | :-------: | :-------: | :-------: | :-----: | :-----: |
2020-12-10 18:43:27 +08:00
| [rec_chinese_lite_train_v2.0.yml](../../configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml) | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc |
| [rec_chinese_common_train_v2.0.yml](../../configs/rec/ch_ppocr_v2.0/rec_chinese_common_train_v2.0.yml) | CRNN | ResNet34_vd | None | BiLSTM | ctc |
2020-06-08 10:16:26 +08:00
| rec_chinese_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc |
2020-10-13 17:49:16 +08:00
| rec_chinese_common_train.yml | CRNN | ResNet34_vd | None | BiLSTM | ctc |
2020-06-08 10:16:26 +08:00
| rec_icdar15_train.yml | CRNN | Mobilenet_v3 large 0.5 | None | BiLSTM | ctc |
| rec_mv3_none_bilstm_ctc.yml | CRNN | Mobilenet_v3 large 0.5 | None | BiLSTM | ctc |
| rec_mv3_none_none_ctc.yml | Rosetta | Mobilenet_v3 large 0.5 | None | None | ctc |
| rec_r34_vd_none_bilstm_ctc.yml | CRNN | Resnet34_vd | None | BiLSTM | ctc |
| rec_r34_vd_none_none_ctc.yml | Rosetta | Resnet34_vd | None | None | ctc |
2021-02-01 14:27:56 +08:00
| rec_mv3_tps_bilstm_att.yml | CRNN | Mobilenet_v3 | TPS | BiLSTM | att |
| rec_r34_vd_tps_bilstm_att.yml | CRNN | Resnet34_vd | TPS | BiLSTM | att |
2021-01-29 15:08:58 +08:00
| rec_r50fpn_vd_none_srn.yml | SRN | Resnet50_fpn_vd | None | rnn | srn |
2021-08-11 17:50:51 +08:00
| rec_mtb_nrtr.yml | NRTR | nrtr_mtb | None | transformer encoder | transformer decoder |
2020-06-08 10:16:26 +08:00
2020-10-13 17:49:16 +08:00
For training Chinese data, it is recommended to use
2020-12-10 18:43:27 +08:00
[rec_chinese_lite_train_v2.0.yml](../../configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml). If you want to try the result of other algorithms on the Chinese data set, please refer to the following instructions to modify the configuration file:
2020-06-08 10:16:26 +08:00
co
2020-12-10 18:43:27 +08:00
Take `rec_chinese_lite_train_v2.0.yml` as an example:
2020-06-08 10:16:26 +08:00
```
Global:
...
2020-12-09 20:45:56 +08:00
# Add a custom dictionary, such as modify the dictionary, please point the path to the new dictionary
character_dict_path: ppocr/utils/ppocr_keys_v1.txt
2020-06-08 10:16:26 +08:00
# Modify character type
character_type: ch
...
2020-07-07 14:13:13 +08:00
# Whether to recognize spaces
2020-12-10 18:43:27 +08:00
use_space_char: True
2020-06-08 10:16:26 +08:00
2020-07-07 14:13:13 +08:00
Optimizer:
...
# Add learning rate decay strategy
2020-12-09 20:45:56 +08:00
lr:
name: Cosine
learning_rate: 0.001
...
...
Train:
dataset:
2021-06-08 15:44:37 +08:00
# Type of datasetwe support LMDBDataSet and SimpleDataSet
2020-12-09 20:45:56 +08:00
name: SimpleDataSet
# Path of dataset
data_dir: ./train_data/
# Path of train list
label_file_list: ["./train_data/train_list.txt"]
transforms:
...
- RecResizeImg:
# Modify image_shape to fit long text
image_shape: [3, 32, 320]
...
loader:
...
# Train batch_size for Single card
batch_size_per_card: 256
...
Eval:
dataset:
2021-06-08 15:44:37 +08:00
# Type of datasetwe support LMDBDataSet and SimpleDataSet
2020-12-09 20:45:56 +08:00
name: SimpleDataSet
# Path of dataset
data_dir: ./train_data
# Path of eval list
label_file_list: ["./train_data/val_list.txt"]
transforms:
...
- RecResizeImg:
# Modify image_shape to fit long text
image_shape: [3, 32, 320]
...
loader:
# Eval batch_size for Single card
batch_size_per_card: 256
...
2020-06-08 10:16:26 +08:00
```
**Note that the configuration file for prediction/evaluation must be consistent with the training.**
2020-10-13 17:49:16 +08:00
<a name="Multi_language"></a>
2021-09-06 17:00:51 +08:00
#### 2.3 Multi-language Training
2021-01-26 14:51:56 +08:00
Currently, the multi-language algorithms supported by PaddleOCR are:
2021-01-26 15:24:13 +08:00
| Configuration file | Algorithm name | backbone | trans | seq | pred | language | character_type |
2021-01-26 14:51:56 +08:00
| :--------: | :-------: | :-------: | :-------: | :-----: | :-----: | :-----: | :-----: |
| rec_chinese_cht_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc | chinese traditional | chinese_cht|
2021-01-26 15:53:49 +08:00
| rec_en_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc | English(Case sensitive) | EN |
2021-01-26 14:51:56 +08:00
| rec_french_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc | French | french |
| rec_ger_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc | German | german |
| rec_japan_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc | Japanese | japan |
| rec_korean_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc | Korean | korean |
2021-04-13 17:54:10 +08:00
| rec_latin_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc | Latin | latin |
| rec_arabic_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc | arabic | ar |
| rec_cyrillic_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc | cyrillic | cyrillic |
| rec_devanagari_lite_train.yml | CRNN | Mobilenet_v3 small 0.5 | None | BiLSTM | ctc | devanagari | devanagari |
2021-01-26 14:51:56 +08:00
2021-04-13 17:54:10 +08:00
For more supported languages, please refer to : [Multi-language model](https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.1/doc/doc_en/multi_languages_en.md#4-support-languages-and-abbreviations)
2020-10-13 17:49:16 +08:00
If you want to finetune on the basis of the existing model effect, please refer to the following instructions to modify the configuration file:
Take `rec_french_lite_train` as an example:
```
Global:
...
2020-12-09 20:45:56 +08:00
# Add a custom dictionary, such as modify the dictionary, please point the path to the new dictionary
2020-10-13 17:49:16 +08:00
character_dict_path: ./ppocr/utils/dict/french_dict.txt
...
2020-12-09 20:45:56 +08:00
# Whether to recognize spaces
2020-12-10 18:43:27 +08:00
use_space_char: True
2020-12-09 20:45:56 +08:00
2020-10-13 17:49:16 +08:00
...
2020-12-09 20:45:56 +08:00
Train:
dataset:
2021-06-08 15:44:37 +08:00
# Type of datasetwe support LMDBDataSet and SimpleDataSet
2020-12-09 20:45:56 +08:00
name: SimpleDataSet
# Path of dataset
data_dir: ./train_data/
# Path of train list
label_file_list: ["./train_data/french_train.txt"]
...
Eval:
dataset:
2021-06-08 15:44:37 +08:00
# Type of datasetwe support LMDBDataSet and SimpleDataSet
2020-12-09 20:45:56 +08:00
name: SimpleDataSet
# Path of dataset
data_dir: ./train_data
# Path of eval list
label_file_list: ["./train_data/french_val.txt"]
...
2020-10-13 17:49:16 +08:00
```
2020-06-08 10:16:26 +08:00
2020-10-13 17:49:16 +08:00
<a name="EVALUATION"></a>
2021-02-03 11:58:24 +08:00
### 3 EVALUATION
2020-06-08 10:16:26 +08:00
2020-12-18 23:00:16 +08:00
The evaluation dataset can be set by modifying the `Eval.dataset.label_file_list` field in the `configs/rec/rec_icdar15_train.yml` file.
2020-06-08 10:16:26 +08:00
```
# GPU evaluation, Global.checkpoints is the weight to be tested
2020-12-18 23:00:16 +08:00
python3 -m paddle.distributed.launch --gpus '0' tools/eval.py -c configs/rec/rec_icdar15_train.yml -o Global.checkpoints={path/to/weights}/best_accuracy
2020-06-08 10:16:26 +08:00
```
2020-10-13 17:49:16 +08:00
<a name="PREDICTION"></a>
2021-02-03 11:58:24 +08:00
### 4 PREDICTION
2020-06-08 10:16:26 +08:00
Using the model trained by paddleocr, you can quickly get prediction through the following script.
2021-09-06 17:00:51 +08:00
The default prediction picture is stored in `infer_img`, and the trained weight is specified via `-o Global.checkpoints`:
According to the `save_model_dir` and `save_epoch_step` fields set in the configuration file, the following parameters will be saved:
```
output/rec/
├── best_accuracy.pdopt
├── best_accuracy.pdparams
├── best_accuracy.states
├── config.yml
├── iter_epoch_3.pdopt
├── iter_epoch_3.pdparams
├── iter_epoch_3.states
├── latest.pdopt
├── latest.pdparams
├── latest.states
└── train.log
```
Among them, best_accuracy.* is the best model on the evaluation set; iter_epoch_x.* is the model saved at intervals of `save_epoch_step`; latest.* is the model of the last epoch.
2020-06-08 10:16:26 +08:00
```
# Predict English results
2020-12-18 23:00:16 +08:00
python3 tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.pretrained_model={path/to/weights}/best_accuracy Global.load_static_weights=false Global.infer_img=doc/imgs_words/en/word_1.jpg
2020-06-08 10:16:26 +08:00
```
2021-09-06 17:00:51 +08:00
2020-06-08 10:16:26 +08:00
Input image:
![](../imgs_words/en/word_1.png)
2020-06-08 10:16:26 +08:00
Get the prediction result of the input image:
```
infer_img: doc/imgs_words/en/word_1.png
2020-12-15 20:32:18 +08:00
result: ('joint', 0.9998967)
2020-06-08 10:16:26 +08:00
```
2020-12-10 18:43:27 +08:00
The configuration file used for prediction must be consistent with the training. For example, you completed the training of the Chinese model with `python3 tools/train.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml`, you can use the following command to predict the Chinese model:
2020-06-08 10:16:26 +08:00
```
# Predict Chinese results
2020-12-18 23:00:16 +08:00
python3 tools/infer_rec.py -c configs/rec/ch_ppocr_v2.0/rec_chinese_lite_train_v2.0.yml -o Global.pretrained_model={path/to/weights}/best_accuracy Global.load_static_weights=false Global.infer_img=doc/imgs_words/ch/word_1.jpg
2020-06-08 10:16:26 +08:00
```
Input image:
![](../imgs_words/ch/word_1.jpg)
2020-06-08 10:16:26 +08:00
Get the prediction result of the input image:
```
infer_img: doc/imgs_words/ch/word_1.jpg
2020-12-15 20:32:18 +08:00
result: ('韩国小馆', 0.997218)
2020-06-08 10:16:26 +08:00
```