Open vuthehuy1997 opened 2 days ago
The issue you are experiencing, where the inference results using the exported model are inconsistent or incorrect compared to the results from training or direct inference with the best_accuracy
checkpoint, is a common problem when exporting models for static inference. Below are potential causes and solutions based on the provided details and similar issues encountered in PaddleOCR:
Inconsistent Preprocessing Between Training and Inference
predict_structure.py
. If the exported inference model uses preprocessing steps different from those used during training, this could lead to mismatched results.Postprocessing Differences
infer_table.py
and predict_structure.py
. This can cause discrepancies in the output.Character Dictionary Mismatch
Export Command Configuration
character_dict_path
, max_text_length
, or merge_no_span_structure
.Dynamic vs. Static Graph Differences
best_accuracy
) uses a dynamic computational graph, while the exported model uses a static graph. Some components (e.g., batch normalization or variable shapes) may behave differently in static graphs compared to dynamic ones.Error in the Exported Model
export_model.py
implementation or the configuration passed to it, causing the exported model to incorrectly represent the trained model.SLANet_finetune.yml
with those in predict_structure.py
. Key preprocessing components to check:
DecodeImage
NormalizeImage
(mean, stddev)ResizeTableImage
PaddingTableImage
PostProcess
block in your configuration file and ensure that parameters like merge_no_span_structure
are consistent with how predict_structure.py
decodes the output.predict_structure.py
to align it with infer_table.py
's decoding logic if needed.table_structure_dict_ch.txt
) is used. Ensure that the same dictionary is referenced during training, inference, and export.SLANet_finetune.yml
are passed during export. For example:
python3 tools/export_model.py -c configs/table/SLANet_finetune.yml \
-o Global.pretrained_model=../translate-pdf-scan/src/weights/table_rec/SLANet_ch/best_accuracy \
Global.save_inference_dir=../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_finetune \
Global.character_dict_path=ppocr/utils/dict/table_structure_dict_ch.txt \
Global.max_text_length=500 \
PostProcess.merge_no_span_structure=True
tools/infer_table.py
directly on the exported model:
python3 tools/infer_table.py -c configs/table/SLANet_finetune.yml \
-o Global.pretrained_model=../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_finetune \
Global.infer_img=ppstructure/docs/table/table.jpg
predict_structure.py
.paddle_infer
API to check whether the exported model is working as expected:
paddle_infer --model_dir=../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_finetune \
--image_dir=ppstructure/docs/table/table.jpg
Global.infer_mode=True
option to force the model into inference mode during training:
Global:
infer_mode: True
ch_ppstructure_mobile_v2.0_SLANet_train
) without fine-tuning to ensure the export process works correctly:
python3 tools/export_model.py -c configs/table/SLANet.yml \
-o Global.pretrained_model=./weights/whl/table/ch_ppstructure_mobile_v2.0_SLANet_train/best_accuracy \
Global.save_inference_dir=./output/pretrained_inference
tools/infer_table.py
.infer_mode=True
during training to debug differences in dynamic vs. static graph behavior.If none of these solutions resolve the issue, you might need to open a detailed issue on the PaddleOCR GitHub repository with a focus on the export process. Be sure to include:
infer_table.py
and predict_structure.py
.Response generated by ๐ค feifei-bot | chatgpt-4o-latest
@vuthehuy1997 you may need try paddleocr 2.8.1.
The issue you are experiencing, where the inference results using the exported model are inconsistent or incorrect compared to the results from training or direct inference with the
best_accuracy
checkpoint, is a common problem when exporting models for static inference. Below are potential causes and solutions based on the provided details and similar issues encountered in PaddleOCR:Potential Causes
1. **Inconsistent Preprocessing Between Training and Inference** * The preprocessing steps (e.g., image normalization, resizing, padding) during training or inference might differ from those applied in `predict_structure.py`. If the exported inference model uses preprocessing steps different from those used during training, this could lead to mismatched results. 2. **Postprocessing Differences** * The way the predictions are decoded (e.g., parsing table structure) might vary between `infer_table.py` and `predict_structure.py`. This can cause discrepancies in the output. 3. **Character Dictionary Mismatch** * During inference, the wrong character dictionary might be used, leading to incorrect decoding of the table structure. 4. **Export Command Configuration** * The export process might not fully replicate the configuration used during training, such as missing parameters like `character_dict_path`, `max_text_length`, or `merge_no_span_structure`. 5. **Dynamic vs. Static Graph Differences** * In PaddleOCR, the training model (`best_accuracy`) uses a dynamic computational graph, while the exported model uses a static graph. Some components (e.g., batch normalization or variable shapes) may behave differently in static graphs compared to dynamic ones. 6. **Error in the Exported Model** * There might be a bug in the `export_model.py` implementation or the configuration passed to it, causing the exported model to incorrectly represent the trained model.
Steps to Debug and Resolve
1. Verify Preprocessing Consistency
* Compare the preprocessing steps in `SLANet_finetune.yml` with those in `predict_structure.py`. Key preprocessing components to check: * `DecodeImage` * `NormalizeImage` (mean, stddev) * `ResizeTableImage` * `PaddingTableImage` * Ensure that the same transformations are applied in both training and inference.
2. Ensure Postprocessing Consistency
* Check the `PostProcess` block in your configuration file and ensure that parameters like `merge_no_span_structure` are consistent with how `predict_structure.py` decodes the output. * Modify `predict_structure.py` to align it with `infer_table.py`'s decoding logic if needed.
3. Confirm Character Dictionary
* Verify that the correct character dictionary (`table_structure_dict_ch.txt`) is used. Ensure that the same dictionary is referenced during training, inference, and export.
4. Pass All Necessary Parameters During Export
* Ensure that all critical parameters from `SLANet_finetune.yml` are passed during export. For example: ```shell python3 tools/export_model.py -c configs/table/SLANet_finetune.yml \ -o Global.pretrained_model=../translate-pdf-scan/src/weights/table_rec/SLANet_ch/best_accuracy \ Global.save_inference_dir=../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_finetune \ Global.character_dict_path=ppocr/utils/dict/table_structure_dict_ch.txt \ Global.max_text_length=500 \ PostProcess.merge_no_span_structure=True ```
5. Run Direct Inference on Exported Model
* Test if the exported model is functional by using `tools/infer_table.py` directly on the exported model: ```shell python3 tools/infer_table.py -c configs/table/SLANet_finetune.yml \ -o Global.pretrained_model=../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_finetune \ Global.infer_img=ppstructure/docs/table/table.jpg ``` * If the result is correct, the issue lies specifically in `predict_structure.py`.
6. Validate the Exported Model
* Use the `paddle_infer` API to check whether the exported model is working as expected: ```shell paddle_infer --model_dir=../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_finetune \ --image_dir=ppstructure/docs/table/table.jpg ``` * If the results are incorrect, the issue is likely during the export process.
7. Check for Known Issues
* Similar problems have been reported in PaddleOCR's GitHub repository. For example: * [Issue #14064](https://github.com/PaddlePaddle/PaddleOCR/issues/14064) * [Issue #14088](https://github.com/PaddlePaddle/PaddleOCR/discussions/14088) * These issues often involve differences in preprocessing or bugs in the export script.
8. Debug Static Graph Behavior
* Some differences might arise due to the transition from a dynamic to a static graph. Try retraining with the `Global.infer_mode=True` option to force the model into inference mode during training: ```yaml Global: infer_mode: True ```
9. Test with a Pretrained Model
* As a sanity check, export the pretrained model (`ch_ppstructure_mobile_v2.0_SLANet_train`) without fine-tuning to ensure the export process works correctly: ```shell python3 tools/export_model.py -c configs/table/SLANet.yml \ -o Global.pretrained_model=./weights/whl/table/ch_ppstructure_mobile_v2.0_SLANet_train/best_accuracy \ Global.save_inference_dir=./output/pretrained_inference ```
Recommended Workflow
1. **Preprocessing/Postprocessing Check**: Ensure preprocessing and postprocessing are consistent across training, inference, and export. 2. **Parameter Alignment**: Pass all required parameters during both training and export. 3. **Export Validation**: Test the exported model directly using `tools/infer_table.py`. 4. **Static Graph Debugging**: Use `infer_mode=True` during training to debug differences in dynamic vs. static graph behavior.
If none of these solutions resolve the issue, you might need to open a detailed issue on the PaddleOCR GitHub repository with a focus on the export process. Be sure to include:
* The exact commands you ran. * Configuration files used. * Input/output examples from both `infer_table.py` and `predict_structure.py`.
Response generated by ๐ค feifei-bot | chatgpt-4o-latest
About Steps to Debug and Resolve
I use config from github is configs/table/SLANet_ch.yml -> change data and add pretrained according to finetune instructions -> configs/table/SLANet_finetune.yml
python3 tools/infer_table.py -c configs/table/SLANet_finetune.yml \ -o Global.pretrained_model=../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_finetune \ Global.infer_img=ppstructure/docs/table/table.jpg
Get results AssertionError: The ../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_finetune_infer/.pdparams does not exists!
tools/infer_table.py is used to run the infer model which does not support running mdel train
Use the paddle_infer API to check whether the exported model is working as expected:
paddle_infer --model_dir=../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_finetune \ --image_dir=ppstructure/docs/table/table.jpg
I do not use
I can not get problem from it
Some differences might arise due to the transition from a dynamic to a static graph. Try retraining with the Global.infer_mode=True option to force the model into inference mode during training:
Global: infer_mode: True -> Tested but the result is still not correct
python3 tools/infer_table.py -c configs/table/SLANet_ch.yml -o \ Global.pretrained_model=../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_train/best_accuracy Global.infer_img=ppstructure/docs/table/table.jpg
cd ppstructure/ python table/predict_structure.py \ --table_model_dir=../../translate-pdf-scan/src/weights/table_rec/ch_ppstructure_mobile_v2.0_SLANet_train_infer \ --table_char_dict_path=../ppocr/utils/dict/table_structure_dict_ch.txt \ --image_dir=docs/table/table.jpg \ --output=../output/
-> The result is still wrong
paddleocr 2.8.1 : I also tried to install it but it didn't work..
I noticed that the predicted output of the exported model is very small, and many,
the model train predicts the box to shape (8,10) then the exported model outputs to (50, 60),
The result returns 50 instead of 8 coordinates xyxyxyxy
๐ Search before asking
๐ Bug (้ฎ้ขๆ่ฟฐ)
When I finished training the SLANet recognition table model, I exported it to a format for inference, but the results were wrong. i only edit SLANet.yml to SLANet_finetune.yml like tutorial on github document
Model training command (finetune from model ch_ppstructure_mobile_v2.0_SLANet_train)
export command:
If i run the model after training with the command:
Then it gives correct results
If running model was export:
Then the result will be wrong
Does anyone know what the error is when exporting?
๐โโ๏ธ Environment (่ฟ่ก็ฏๅข)
๐ฐ Minimal Reproducible Example (ๆๅฐๅฏๅค็ฐ้ฎ้ข็Demo)