From cc6c606f147e95bfec9208ea89bdb3428cc0eb4e Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Fri, 12 Jun 2020 13:49:24 +0800 Subject: [PATCH 1/7] discard export PYTHONPATH manually --- README.md | 5 ----- README_en.md | 5 ----- tools/eval.py | 5 +++++ tools/export_model.py | 6 +++--- tools/infer/predict_det.py | 7 ++++++- tools/infer/predict_rec.py | 5 +++++ tools/infer/predict_system.py | 7 ++++--- tools/infer_det.py | 9 +++++---- tools/infer_rec.py | 14 ++++++-------- tools/train.py | 6 +++--- 10 files changed, 37 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 5f6bca2de4..07479a9bf5 100644 --- a/README.md +++ b/README.md @@ -69,11 +69,6 @@ cd .. ```bash -# 设置PYTHONPATH环境变量 -export PYTHONPATH=. -# windows下设置环境变量 -SET PYTHONPATH=. - # 预测image_dir指定的单张图像 python3 tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_det_mv3_db/" --rec_model_dir="./inference/ch_rec_mv3_crnn/" diff --git a/README_en.md b/README_en.md index 8005e09e7e..c3420302a0 100644 --- a/README_en.md +++ b/README_en.md @@ -69,11 +69,6 @@ The following code implements text detection and recognition inference tandemly. ```bash -# Set PYTHONPATH environment variable -export PYTHONPATH=. -# Setting environment variable in Windows -SET PYTHONPATH=. - # Prediction on a single image by specifying image path to image_dir python3 tools/infer/predict_system.py --image_dir="./doc/imgs/11.jpg" --det_model_dir="./inference/ch_det_mv3_db/" --rec_model_dir="./inference/ch_rec_mv3_crnn/" diff --git a/tools/eval.py b/tools/eval.py index d1762e9294..5517049db5 100755 --- a/tools/eval.py +++ b/tools/eval.py @@ -17,6 +17,11 @@ from __future__ import division from __future__ import print_function import os +import os +import sys +__dir__ = os.path.dirname(__file__) +sys.path.append(__dir__) +sys.path.append(os.path.join(__dir__, '..')) def set_paddle_flags(**kwargs): diff --git a/tools/export_model.py b/tools/export_model.py index 555ba7b0e4..4415eda840 100644 --- a/tools/export_model.py +++ b/tools/export_model.py @@ -18,9 +18,9 @@ from __future__ import print_function import os import sys -import time -import multiprocessing -import numpy as np +__dir__ = os.path.dirname(__file__) +sys.path.append(__dir__) +sys.path.append(os.path.join(__dir__, '..')) def set_paddle_flags(**kwargs): diff --git a/tools/infer/predict_det.py b/tools/infer/predict_det.py index 35d8a09daf..93a21c512d 100755 --- a/tools/infer/predict_det.py +++ b/tools/infer/predict_det.py @@ -11,8 +11,13 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import os +import sys +__dir__ = os.path.dirname(__file__) +sys.path.append(__dir__) +sys.path.append(os.path.join(__dir__, '../..')) -import utility +import tools.infer.utility as utility from ppocr.utils.utility import initial_logger logger = initial_logger() from ppocr.utils.utility import get_image_file_list diff --git a/tools/infer/predict_rec.py b/tools/infer/predict_rec.py index d47b81afd0..249d100605 100755 --- a/tools/infer/predict_rec.py +++ b/tools/infer/predict_rec.py @@ -11,6 +11,11 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +import os +import sys +__dir__ = os.path.dirname(__file__) +sys.path.append(__dir__) +sys.path.append(os.path.join(__dir__, '../..')) import utility from ppocr.utils.utility import initial_logger diff --git a/tools/infer/predict_system.py b/tools/infer/predict_system.py index 1019279a24..032d094cf4 100755 --- a/tools/infer/predict_system.py +++ b/tools/infer/predict_system.py @@ -16,12 +16,13 @@ import sys __dir__ = os.path.dirname(__file__) sys.path.append(__dir__) sys.path.append(os.path.join(__dir__, '../..')) -import utility + +import tools.infer.utility as utility from ppocr.utils.utility import initial_logger logger = initial_logger() import cv2 -import predict_det -import predict_rec +import tools.infer.predict_det as predict_det +import tools.infer.predict_rec as predict_rec import copy import numpy as np import math diff --git a/tools/infer_det.py b/tools/infer_det.py index 800067655a..f7dbd8eb1e 100755 --- a/tools/infer_det.py +++ b/tools/infer_det.py @@ -16,14 +16,15 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import os -import sys -import time import numpy as np from copy import deepcopy import json -# from paddle.fluid.contrib.model_stat import summary +import os +import sys +__dir__ = os.path.dirname(__file__) +sys.path.append(__dir__) +sys.path.append(os.path.join(__dir__, '..')) def set_paddle_flags(**kwargs): diff --git a/tools/infer_rec.py b/tools/infer_rec.py index ec64a38bba..b1ddc8dda1 100755 --- a/tools/infer_rec.py +++ b/tools/infer_rec.py @@ -16,10 +16,12 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import os -import time -import multiprocessing import numpy as np +import os +import sys +__dir__ = os.path.dirname(__file__) +sys.path.append(__dir__) +sys.path.append(os.path.join(__dir__, '..')) def set_paddle_flags(**kwargs): @@ -35,10 +37,7 @@ set_paddle_flags( FLAGS_eager_delete_tensor_gb=0, # enable GC to save memory ) -from paddle import fluid - -# from ppocr.utils.utility import load_config, merge_config -import program +import tools.program as program from paddle import fluid from ppocr.utils.utility import initial_logger logger = initial_logger() @@ -47,7 +46,6 @@ from ppocr.utils.save_load import init_model from ppocr.utils.character import CharacterOps from ppocr.utils.utility import create_module from ppocr.utils.utility import get_image_file_list -logger = initial_logger() def main(): diff --git a/tools/train.py b/tools/train.py index 4f69d40f5f..287ed2059e 100755 --- a/tools/train.py +++ b/tools/train.py @@ -18,9 +18,9 @@ from __future__ import print_function import os import sys -import time -import multiprocessing -import numpy as np +__dir__ = os.path.dirname(__file__) +sys.path.append(__dir__) +sys.path.append(os.path.join(__dir__, '..')) def set_paddle_flags(**kwargs): From 1636c8c156af7b607f2e9a7ac6b058ef1990da9e Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Mon, 15 Jun 2020 10:00:36 +0800 Subject: [PATCH 2/7] discard export PYTHONPATH manually --- tools/eval.py | 1 - tools/infer/predict_rec.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tools/eval.py b/tools/eval.py index 5517049db5..2db6273a7b 100755 --- a/tools/eval.py +++ b/tools/eval.py @@ -16,7 +16,6 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import os import os import sys __dir__ = os.path.dirname(__file__) diff --git a/tools/infer/predict_rec.py b/tools/infer/predict_rec.py index 249d100605..b96a9fab08 100755 --- a/tools/infer/predict_rec.py +++ b/tools/infer/predict_rec.py @@ -17,7 +17,7 @@ __dir__ = os.path.dirname(__file__) sys.path.append(__dir__) sys.path.append(os.path.join(__dir__, '../..')) -import utility +import tools.infer.utility as utility from ppocr.utils.utility import initial_logger logger = initial_logger() from ppocr.utils.utility import get_image_file_list From ff566d3699501108d7f10f5a8f91ae76e0ae8be3 Mon Sep 17 00:00:00 2001 From: dyning Date: Tue, 16 Jun 2020 12:55:27 +0800 Subject: [PATCH 3/7] Update FAQ.md --- doc/doc_ch/FAQ.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/doc_ch/FAQ.md b/doc/doc_ch/FAQ.md index 2bae57abdd..a533592c0c 100644 --- a/doc/doc_ch/FAQ.md +++ b/doc/doc_ch/FAQ.md @@ -46,3 +46,6 @@ PaddleOCR已完成Windows和Mac系统适配,运行时注意两点:1、在[ 报错信息:Input(X) dims[3] and Input(Grid) dims[2] should be equal, but received X dimension[3](320) != Grid dimension[2](100) 原因:TPS模块暂时无法支持变长的输入,请设置 --rec_image_shape='3,32,100' --rec_char_type='en' 固定输入shape + +11. **自定义字典训练的模型,识别结果出现字典里没出现的字** +预测时没有设置采用的自定义字典路径。设置方法是在预测时,通过增加输入参数rec_char_dict_path来设置。 From d52120505bf3cd5ab3b04ab98ec1bfae65b11256 Mon Sep 17 00:00:00 2001 From: dyning Date: Tue, 16 Jun 2020 12:55:46 +0800 Subject: [PATCH 4/7] Update FAQ.md --- doc/doc_ch/FAQ.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/doc_ch/FAQ.md b/doc/doc_ch/FAQ.md index a533592c0c..eddc1d7709 100644 --- a/doc/doc_ch/FAQ.md +++ b/doc/doc_ch/FAQ.md @@ -48,4 +48,5 @@ PaddleOCR已完成Windows和Mac系统适配,运行时注意两点:1、在[ 原因:TPS模块暂时无法支持变长的输入,请设置 --rec_image_shape='3,32,100' --rec_char_type='en' 固定输入shape 11. **自定义字典训练的模型,识别结果出现字典里没出现的字** + 预测时没有设置采用的自定义字典路径。设置方法是在预测时,通过增加输入参数rec_char_dict_path来设置。 From f4094b9700c4028afbb1d88110361156e8c41643 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Tue, 16 Jun 2020 14:55:52 +0800 Subject: [PATCH 5/7] fix issue #206 --- ppocr/modeling/architectures/det_model.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ppocr/modeling/architectures/det_model.py b/ppocr/modeling/architectures/det_model.py index 5016546ac0..516413e992 100755 --- a/ppocr/modeling/architectures/det_model.py +++ b/ppocr/modeling/architectures/det_model.py @@ -32,6 +32,7 @@ class DetModel(object): params (dict): the super parameters for detection module. """ global_params = params['Global'] + self.global_params = global_params self.algorithm = global_params['algorithm'] backbone_params = deepcopy(params["Backbone"]) @@ -64,11 +65,23 @@ class DetModel(object): if mode == "train": if self.algorithm == "EAST": score = fluid.layers.data( - name='score', shape=[1, 128, 128], dtype='float32') + name='score', + shape=[ + 1, int(image_shape[1] // 4), int(image_shape[2] // 4) + ], + dtype='float32') geo = fluid.layers.data( - name='geo', shape=[9, 128, 128], dtype='float32') + name='geo', + shape=[ + 9, int(image_shape[1] // 4), int(image_shape[2] // 4) + ], + dtype='float32') mask = fluid.layers.data( - name='mask', shape=[1, 128, 128], dtype='float32') + name='mask', + shape=[ + 1, int(image_shape[1] // 4), int(image_shape[2] // 4) + ], + dtype='float32') feed_list = [image, score, geo, mask] labels = {'score': score, 'geo': geo, 'mask': mask} elif self.algorithm == "DB": From a5f1efdfc9b09b0df0c053d44a2b310b0abb1cc4 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Tue, 16 Jun 2020 15:00:50 +0800 Subject: [PATCH 6/7] delete not used code --- ppocr/modeling/architectures/det_model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ppocr/modeling/architectures/det_model.py b/ppocr/modeling/architectures/det_model.py index 516413e992..d86454be5b 100755 --- a/ppocr/modeling/architectures/det_model.py +++ b/ppocr/modeling/architectures/det_model.py @@ -32,7 +32,6 @@ class DetModel(object): params (dict): the super parameters for detection module. """ global_params = params['Global'] - self.global_params = global_params self.algorithm = global_params['algorithm'] backbone_params = deepcopy(params["Backbone"]) From e33e38d1d0d4e49594c26ce86ceadc9e8ed52a19 Mon Sep 17 00:00:00 2001 From: LDOUBLEV Date: Tue, 16 Jun 2020 15:22:19 +0800 Subject: [PATCH 7/7] add error message when image shape are not divisible by 4 --- ppocr/modeling/architectures/det_model.py | 25 +++++++++-------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/ppocr/modeling/architectures/det_model.py b/ppocr/modeling/architectures/det_model.py index d86454be5b..7ea0949bc8 100755 --- a/ppocr/modeling/architectures/det_model.py +++ b/ppocr/modeling/architectures/det_model.py @@ -59,28 +59,23 @@ class DetModel(object): return: (image, corresponding label, dataloader) """ image_shape = deepcopy(self.image_shape) + if image_shape[1] % 4 != 0 or image_shape[2] % 4 != 0: + raise Exception("The size of the image must be divisible by 4, " + "received image shape is {}, please reset the " + "Global.image_shape in the yml file".format( + image_shape)) + image = fluid.layers.data( name='image', shape=image_shape, dtype='float32') if mode == "train": if self.algorithm == "EAST": + h, w = int(image_shape[1] // 4), int(image_shape[2] // 4) score = fluid.layers.data( - name='score', - shape=[ - 1, int(image_shape[1] // 4), int(image_shape[2] // 4) - ], - dtype='float32') + name='score', shape=[1, h, w], dtype='float32') geo = fluid.layers.data( - name='geo', - shape=[ - 9, int(image_shape[1] // 4), int(image_shape[2] // 4) - ], - dtype='float32') + name='geo', shape=[9, h, w], dtype='float32') mask = fluid.layers.data( - name='mask', - shape=[ - 1, int(image_shape[1] // 4), int(image_shape[2] // 4) - ], - dtype='float32') + name='mask', shape=[1, h, w], dtype='float32') feed_list = [image, score, geo, mask] labels = {'score': score, 'geo': geo, 'mask': mask} elif self.algorithm == "DB":