We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
some updates repeated
2020.6.5 Support exporting attention model to inference_model
2020.6.5 Support separate prediction and recognition, output result score
2020.6.5 Support exporting attention model to inference_model
2020.6.5 Support separate prediction and recognition, output result score

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `tools/infer/predict_rec.py`
Content:
```
1 # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 import os
15 import sys
16 
17 __dir__ = os.path.dirname(os.path.abspath(__file__))
18 sys.path.append(__dir__)
19 sys.path.append(os.path.abspath(os.path.join(__dir__, '../..')))
20 
21 os.environ["FLAGS_allocator_strategy"] = 'auto_growth'
22 
23 import cv2
24 import numpy as np
25 import math
26 import time
27 import traceback
28 import paddle
29 
30 import tools.infer.utility as utility
31 from ppocr.postprocess import build_post_process
32 from ppocr.utils.logging import get_logger
33 from ppocr.utils.utility import get_image_file_list, check_and_read_gif
34 
35 logger = get_logger()
36 
37 
38 class TextRecognizer(object):
39     def __init__(self, args):
40         self.rec_image_shape = [int(v) for v in args.rec_image_shape.split(",")]
41         self.character_type = args.rec_char_type
42         self.rec_batch_num = args.rec_batch_num
43         self.rec_algorithm = args.rec_algorithm
44         postprocess_params = {
45             'name': 'CTCLabelDecode',
46             "character_type": args.rec_char_type,
47             "character_dict_path": args.rec_char_dict_path,
48             "use_space_char": args.use_space_char
49         }
50         if self.rec_algorithm == "SRN":
51             postprocess_params = {
52                 'name': 'SRNLabelDecode',
53                 "character_type": args.rec_char_type,
54                 "character_dict_path": args.rec_char_dict_path,
55                 "use_space_char": args.use_space_char
56             }
57         elif self.rec_algorithm == "RARE":
58             postprocess_params = {
59                 'name': 'AttnLabelDecode',
60                 "character_type": args.rec_char_type,
61                 "character_dict_path": args.rec_char_dict_path,
62                 "use_space_char": args.use_space_char
63             }
64         self.postprocess_op = build_post_process(postprocess_params)
65         self.predictor, self.input_tensor, self.output_tensors, self.config = \
66             utility.create_predictor(args, 'rec', logger)
67 
68     def resize_norm_img(self, img, max_wh_ratio):
69         imgC, imgH, imgW = self.rec_image_shape
70         assert imgC == img.shape[2]
71         if self.character_type == "ch":
72             imgW = int((32 * max_wh_ratio))
73         h, w = img.shape[:2]
74         ratio = w / float(h)
75         if math.ceil(imgH * ratio) > imgW:
76             resized_w = imgW
77         else:
78             resized_w = int(math.ceil(imgH * ratio))
79         resized_image = cv2.resize(img, (resized_w, imgH))
80         resized_image = resized_image.astype('float32')
81         resized_image = resized_image.transpose((2, 0, 1)) / 255
82         resized_image -= 0.5
83         resized_image /= 0.5
84         padding_im = np.zeros((imgC, imgH, imgW), dtype=np.float32)
85         padding_im[:, :, 0:resized_w] = resized_image
86         return padding_im
87 
88     def resize_norm_img_srn(self, img, image_shape):
89         imgC, imgH, imgW = image_shape
90 
91         img_black = np.zeros((imgH, imgW))
92         im_hei = img.shape[0]
93         im_wid = img.shape[1]
94 
95         if im_wid <= im_hei * 1:
96             img_new = cv2.resize(img, (imgH * 1, imgH))
97         elif im_wid <= im_hei * 2:
98             img_new = cv2.resize(img, (imgH * 2, imgH))
99         elif im_wid <= im_hei * 3:
100             img_new = cv2.resize(img, (imgH * 3, imgH))
101         else:
102             img_new = cv2.resize(img, (imgW, imgH))
103 
104         img_np = np.asarray(img_new)
105         img_np = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
106         img_black[:, 0:img_np.shape[1]] = img_np
107         img_black = img_black[:, :, np.newaxis]
108 
109         row, col, c = img_black.shape
110         c = 1
111 
112         return np.reshape(img_black, (c, row, col)).astype(np.float32)
113 
114     def srn_other_inputs(self, image_shape, num_heads, max_text_length):
115 
116         imgC, imgH, imgW = image_shape
117         feature_dim = int((imgH / 8) * (imgW / 8))
118 
119         encoder_word_pos = np.array(range(0, feature_dim)).reshape(
120             (feature_dim, 1)).astype('int64')
121         gsrm_word_pos = np.array(range(0, max_text_length)).reshape(
122             (max_text_length, 1)).astype('int64')
123 
124         gsrm_attn_bias_data = np.ones((1, max_text_length, max_text_length))
125         gsrm_slf_attn_bias1 = np.triu(gsrm_attn_bias_data, 1).reshape(
126             [-1, 1, max_text_length, max_text_length])
127         gsrm_slf_attn_bias1 = np.tile(
128             gsrm_slf_attn_bias1,
129             [1, num_heads, 1, 1]).astype('float32') * [-1e9]
130 
131         gsrm_slf_attn_bias2 = np.tril(gsrm_attn_bias_data, -1).reshape(
132             [-1, 1, max_text_length, max_text_length])
133         gsrm_slf_attn_bias2 = np.tile(
134             gsrm_slf_attn_bias2,
135             [1, num_heads, 1, 1]).astype('float32') * [-1e9]
136 
137         encoder_word_pos = encoder_word_pos[np.newaxis, :]
138         gsrm_word_pos = gsrm_word_pos[np.newaxis, :]
139 
140         return [
141             encoder_word_pos, gsrm_word_pos, gsrm_slf_attn_bias1,
142             gsrm_slf_attn_bias2
143         ]
144 
145     def process_image_srn(self, img, image_shape, num_heads, max_text_length):
146         norm_img = self.resize_norm_img_srn(img, image_shape)
147         norm_img = norm_img[np.newaxis, :]
148 
149         [encoder_word_pos, gsrm_word_pos, gsrm_slf_attn_bias1, gsrm_slf_attn_bias2] = \
150             self.srn_other_inputs(image_shape, num_heads, max_text_length)
151 
152         gsrm_slf_attn_bias1 = gsrm_slf_attn_bias1.astype(np.float32)
153         gsrm_slf_attn_bias2 = gsrm_slf_attn_bias2.astype(np.float32)
154         encoder_word_pos = encoder_word_pos.astype(np.int64)
155         gsrm_word_pos = gsrm_word_pos.astype(np.int64)
156 
157         return (norm_img, encoder_word_pos, gsrm_word_pos, gsrm_slf_attn_bias1,
158                 gsrm_slf_attn_bias2)
159 
160     def __call__(self, img_list):
161         img_num = len(img_list)
162         # Calculate the aspect ratio of all text bars
163         width_list = []
164         for img in img_list:
165             width_list.append(img.shape[1] / float(img.shape[0]))
166         # Sorting can speed up the recognition process
167         indices = np.argsort(np.array(width_list))
168         rec_res = [['', 0.0]] * img_num
169         batch_num = self.rec_batch_num
170         st = time.time()
171         for beg_img_no in range(0, img_num, batch_num):
172             end_img_no = min(img_num, beg_img_no + batch_num)
173             norm_img_batch = []
174             max_wh_ratio = 0
175             for ino in range(beg_img_no, end_img_no):
176                 h, w = img_list[indices[ino]].shape[0:2]
177                 wh_ratio = w * 1.0 / h
178                 max_wh_ratio = max(max_wh_ratio, wh_ratio)
179             for ino in range(beg_img_no, end_img_no):
180                 if self.rec_algorithm != "SRN":
181                     norm_img = self.resize_norm_img(img_list[indices[ino]],
182                                                     max_wh_ratio)
183                     norm_img = norm_img[np.newaxis, :]
184                     norm_img_batch.append(norm_img)
185                 else:
186                     norm_img = self.process_image_srn(
187                         img_list[indices[ino]], self.rec_image_shape, 8, 25)
188                     encoder_word_pos_list = []
189                     gsrm_word_pos_list = []
190                     gsrm_slf_attn_bias1_list = []
191                     gsrm_slf_attn_bias2_list = []
192                     encoder_word_pos_list.append(norm_img[1])
193                     gsrm_word_pos_list.append(norm_img[2])
194                     gsrm_slf_attn_bias1_list.append(norm_img[3])
195                     gsrm_slf_attn_bias2_list.append(norm_img[4])
196                     norm_img_batch.append(norm_img[0])
197             norm_img_batch = np.concatenate(norm_img_batch)
198             norm_img_batch = norm_img_batch.copy()
199 
200             if self.rec_algorithm == "SRN":
201                 encoder_word_pos_list = np.concatenate(encoder_word_pos_list)
202                 gsrm_word_pos_list = np.concatenate(gsrm_word_pos_list)
203                 gsrm_slf_attn_bias1_list = np.concatenate(
204                     gsrm_slf_attn_bias1_list)
205                 gsrm_slf_attn_bias2_list = np.concatenate(
206                     gsrm_slf_attn_bias2_list)
207 
208                 inputs = [
209                     norm_img_batch,
210                     encoder_word_pos_list,
211                     gsrm_word_pos_list,
212                     gsrm_slf_attn_bias1_list,
213                     gsrm_slf_attn_bias2_list,
214                 ]
215                 input_names = self.predictor.get_input_names()
216                 for i in range(len(input_names)):
217                     input_tensor = self.predictor.get_input_handle(input_names[
218                         i])
219                     input_tensor.copy_from_cpu(inputs[i])
220                 self.predictor.run()
221                 outputs = []
222                 for output_tensor in self.output_tensors:
223                     output = output_tensor.copy_to_cpu()
224                     outputs.append(output)
225                 preds = {"predict": outputs[2]}
226             else:
227                 self.input_tensor.copy_from_cpu(norm_img_batch)
228                 self.predictor.run()
229 
230                 outputs = []
231                 for output_tensor in self.output_tensors:
232                     output = output_tensor.copy_to_cpu()
233                     outputs.append(output)
234                 preds = outputs[0]
235             rec_result = self.postprocess_op(preds)
236             for rno in range(len(rec_result)):
237                 rec_res[indices[beg_img_no + rno]] = rec_result[rno]
238 
239         return rec_res, time.time() - st
240 
241 
242 def main(args):
243     image_file_list = get_image_file_list(args.image_dir)
244     text_recognizer = TextRecognizer(args)
245     valid_image_file_list = []
246     img_list = []
247 
248     # warmup 10 times
249     if args.warmup:
250         img = np.random.uniform(0, 255, [32, 320, 3]).astype(np.uint8)
251         for i in range(10):
252             res = text_recognizer([img])
253 
254     cpu_mem, gpu_mem, gpu_util = 0, 0, 0
255     count = 0
256 
257     for image_file in image_file_list:
258         img, flag = check_and_read_gif(image_file)
259         if not flag:
260             img = cv2.imread(image_file)
261         if img is None:
262             logger.info("error in loading image:{}".format(image_file))
263             continue
264         valid_image_file_list.append(image_file)
265         img_list.append(img)
266     try:
267         rec_res, _ = text_recognizer(img_list)
268 
269     except Exception as E:
270         logger.info(traceback.format_exc())
271         logger.info(E)
272         exit()
273     for ino in range(len(img_list)):
274         logger.info("Predicts of {}:{}".format(valid_image_file_list[ino],
275                                                rec_res[ino]))
276 
277 
278 if __name__ == "__main__":
279     main(utility.parse_args())
280
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/tools/infer/predict_rec.py b/tools/infer/predict_rec.py
--- a/tools/infer/predict_rec.py
+++ b/tools/infer/predict_rec.py
@@ -64,6 +64,24 @@
         self.postprocess_op = build_post_process(postprocess_params)
         self.predictor, self.input_tensor, self.output_tensors, self.config = \
             utility.create_predictor(args, 'rec', logger)
+        self.benchmark = args.benchmark
+        if args.benchmark:
+            import auto_log
+            pid = os.getpid()
+            self.autolog = auto_log.AutoLogger(
+                model_name="rec",
+                model_precision=args.precision,
+                batch_size=args.rec_batch_num,
+                data_shape="dynamic",
+                save_path=args.save_log_path,
+                inference_config=self.config,
+                pids=pid,
+                process_name=None,
+                gpu_ids=0 if args.use_gpu else None,
+                time_keys=[
+                    'preprocess_time', 'inference_time', 'postprocess_time'
+                ],
+                warmup=10)
 
     def resize_norm_img(self, img, max_wh_ratio):
         imgC, imgH, imgW = self.rec_image_shape
@@ -168,6 +186,8 @@
         rec_res = [['', 0.0]] * img_num
         batch_num = self.rec_batch_num
         st = time.time()
+        if self.benchmark:
+            self.autolog.times.start()
         for beg_img_no in range(0, img_num, batch_num):
             end_img_no = min(img_num, beg_img_no + batch_num)
             norm_img_batch = []
@@ -196,6 +216,8 @@
                     norm_img_batch.append(norm_img[0])
             norm_img_batch = np.concatenate(norm_img_batch)
             norm_img_batch = norm_img_batch.copy()
+            if self.benchmark:
+                self.autolog.times.stamp()
 
             if self.rec_algorithm == "SRN":
                 encoder_word_pos_list = np.concatenate(encoder_word_pos_list)
@@ -222,6 +244,8 @@
                 for output_tensor in self.output_tensors:
                     output = output_tensor.copy_to_cpu()
                     outputs.append(output)
+                if self.benchmark:
+                    self.autolog.times.stamp()
                 preds = {"predict": outputs[2]}
             else:
                 self.input_tensor.copy_from_cpu(norm_img_batch)
@@ -231,11 +255,14 @@
                 for output_tensor in self.output_tensors:
                     output = output_tensor.copy_to_cpu()
                     outputs.append(output)
+                if self.benchmark:
+                    self.autolog.times.stamp()
                 preds = outputs[0]
             rec_result = self.postprocess_op(preds)
             for rno in range(len(rec_result)):
                 rec_res[indices[beg_img_no + rno]] = rec_result[rno]
-
+            if self.benchmark:
+                self.autolog.times.end(stamp=True)
         return rec_res, time.time() - st
 
 
@@ -251,9 +278,6 @@
         for i in range(10):
             res = text_recognizer([img])
 
-    cpu_mem, gpu_mem, gpu_util = 0, 0, 0
-    count = 0
-
     for image_file in image_file_list:
         img, flag = check_and_read_gif(image_file)
         if not flag:
@@ -273,6 +297,8 @@
     for ino in range(len(img_list)):
         logger.info("Predicts of {}:{}".format(valid_image_file_list[ino],
                                                rec_res[ino]))
+    if args.benchmark:
+        text_recognizer.autolog.report()
 
 
 if __name__ == "__main__":

{"golden_diff": "diff --git a/tools/infer/predict_rec.py b/tools/infer/predict_rec.py\n--- a/tools/infer/predict_rec.py\n+++ b/tools/infer/predict_rec.py\n@@ -64,6 +64,24 @@\n         self.postprocess_op = build_post_process(postprocess_params)\n         self.predictor, self.input_tensor, self.output_tensors, self.config = \\\n             utility.create_predictor(args, 'rec', logger)\n+        self.benchmark = args.benchmark\n+        if args.benchmark:\n+            import auto_log\n+            pid = os.getpid()\n+            self.autolog = auto_log.AutoLogger(\n+                model_name=\"rec\",\n+                model_precision=args.precision,\n+                batch_size=args.rec_batch_num,\n+                data_shape=\"dynamic\",\n+                save_path=args.save_log_path,\n+                inference_config=self.config,\n+                pids=pid,\n+                process_name=None,\n+                gpu_ids=0 if args.use_gpu else None,\n+                time_keys=[\n+                    'preprocess_time', 'inference_time', 'postprocess_time'\n+                ],\n+                warmup=10)\n \n     def resize_norm_img(self, img, max_wh_ratio):\n         imgC, imgH, imgW = self.rec_image_shape\n@@ -168,6 +186,8 @@\n         rec_res = [['', 0.0]] * img_num\n         batch_num = self.rec_batch_num\n         st = time.time()\n+        if self.benchmark:\n+            self.autolog.times.start()\n         for beg_img_no in range(0, img_num, batch_num):\n             end_img_no = min(img_num, beg_img_no + batch_num)\n             norm_img_batch = []\n@@ -196,6 +216,8 @@\n                     norm_img_batch.append(norm_img[0])\n             norm_img_batch = np.concatenate(norm_img_batch)\n             norm_img_batch = norm_img_batch.copy()\n+            if self.benchmark:\n+                self.autolog.times.stamp()\n \n             if self.rec_algorithm == \"SRN\":\n                 encoder_word_pos_list = np.concatenate(encoder_word_pos_list)\n@@ -222,6 +244,8 @@\n                 for output_tensor in self.output_tensors:\n                     output = output_tensor.copy_to_cpu()\n                     outputs.append(output)\n+                if self.benchmark:\n+                    self.autolog.times.stamp()\n                 preds = {\"predict\": outputs[2]}\n             else:\n                 self.input_tensor.copy_from_cpu(norm_img_batch)\n@@ -231,11 +255,14 @@\n                 for output_tensor in self.output_tensors:\n                     output = output_tensor.copy_to_cpu()\n                     outputs.append(output)\n+                if self.benchmark:\n+                    self.autolog.times.stamp()\n                 preds = outputs[0]\n             rec_result = self.postprocess_op(preds)\n             for rno in range(len(rec_result)):\n                 rec_res[indices[beg_img_no + rno]] = rec_result[rno]\n-\n+            if self.benchmark:\n+                self.autolog.times.end(stamp=True)\n         return rec_res, time.time() - st\n \n \n@@ -251,9 +278,6 @@\n         for i in range(10):\n             res = text_recognizer([img])\n \n-    cpu_mem, gpu_mem, gpu_util = 0, 0, 0\n-    count = 0\n-\n     for image_file in image_file_list:\n         img, flag = check_and_read_gif(image_file)\n         if not flag:\n@@ -273,6 +297,8 @@\n     for ino in range(len(img_list)):\n         logger.info(\"Predicts of {}:{}\".format(valid_image_file_list[ino],\n                                                rec_res[ino]))\n+    if args.benchmark:\n+        text_recognizer.autolog.report()\n \n \n if __name__ == \"__main__\":\n", "issue": "some updates repeated\n2020.6.5 Support exporting attention model to inference_model\r\n2020.6.5 Support separate prediction and recognition, output result score\r\n2020.6.5 Support exporting attention model to inference_model\r\n2020.6.5 Support separate prediction and recognition, output result score\n", "before_files": [{"content": "# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nimport os\nimport sys\n\n__dir__ = os.path.dirname(os.path.abspath(__file__))\nsys.path.append(__dir__)\nsys.path.append(os.path.abspath(os.path.join(__dir__, '../..')))\n\nos.environ[\"FLAGS_allocator_strategy\"] = 'auto_growth'\n\nimport cv2\nimport numpy as np\nimport math\nimport time\nimport traceback\nimport paddle\n\nimport tools.infer.utility as utility\nfrom ppocr.postprocess import build_post_process\nfrom ppocr.utils.logging import get_logger\nfrom ppocr.utils.utility import get_image_file_list, check_and_read_gif\n\nlogger = get_logger()\n\n\nclass TextRecognizer(object):\n    def __init__(self, args):\n        self.rec_image_shape = [int(v) for v in args.rec_image_shape.split(\",\")]\n        self.character_type = args.rec_char_type\n        self.rec_batch_num = args.rec_batch_num\n        self.rec_algorithm = args.rec_algorithm\n        postprocess_params = {\n            'name': 'CTCLabelDecode',\n            \"character_type\": args.rec_char_type,\n            \"character_dict_path\": args.rec_char_dict_path,\n            \"use_space_char\": args.use_space_char\n        }\n        if self.rec_algorithm == \"SRN\":\n            postprocess_params = {\n                'name': 'SRNLabelDecode',\n                \"character_type\": args.rec_char_type,\n                \"character_dict_path\": args.rec_char_dict_path,\n                \"use_space_char\": args.use_space_char\n            }\n        elif self.rec_algorithm == \"RARE\":\n            postprocess_params = {\n                'name': 'AttnLabelDecode',\n                \"character_type\": args.rec_char_type,\n                \"character_dict_path\": args.rec_char_dict_path,\n                \"use_space_char\": args.use_space_char\n            }\n        self.postprocess_op = build_post_process(postprocess_params)\n        self.predictor, self.input_tensor, self.output_tensors, self.config = \\\n            utility.create_predictor(args, 'rec', logger)\n\n    def resize_norm_img(self, img, max_wh_ratio):\n        imgC, imgH, imgW = self.rec_image_shape\n        assert imgC == img.shape[2]\n        if self.character_type == \"ch\":\n            imgW = int((32 * max_wh_ratio))\n        h, w = img.shape[:2]\n        ratio = w / float(h)\n        if math.ceil(imgH * ratio) > imgW:\n            resized_w = imgW\n        else:\n            resized_w = int(math.ceil(imgH * ratio))\n        resized_image = cv2.resize(img, (resized_w, imgH))\n        resized_image = resized_image.astype('float32')\n        resized_image = resized_image.transpose((2, 0, 1)) / 255\n        resized_image -= 0.5\n        resized_image /= 0.5\n        padding_im = np.zeros((imgC, imgH, imgW), dtype=np.float32)\n        padding_im[:, :, 0:resized_w] = resized_image\n        return padding_im\n\n    def resize_norm_img_srn(self, img, image_shape):\n        imgC, imgH, imgW = image_shape\n\n        img_black = np.zeros((imgH, imgW))\n        im_hei = img.shape[0]\n        im_wid = img.shape[1]\n\n        if im_wid <= im_hei * 1:\n            img_new = cv2.resize(img, (imgH * 1, imgH))\n        elif im_wid <= im_hei * 2:\n            img_new = cv2.resize(img, (imgH * 2, imgH))\n        elif im_wid <= im_hei * 3:\n            img_new = cv2.resize(img, (imgH * 3, imgH))\n        else:\n            img_new = cv2.resize(img, (imgW, imgH))\n\n        img_np = np.asarray(img_new)\n        img_np = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)\n        img_black[:, 0:img_np.shape[1]] = img_np\n        img_black = img_black[:, :, np.newaxis]\n\n        row, col, c = img_black.shape\n        c = 1\n\n        return np.reshape(img_black, (c, row, col)).astype(np.float32)\n\n    def srn_other_inputs(self, image_shape, num_heads, max_text_length):\n\n        imgC, imgH, imgW = image_shape\n        feature_dim = int((imgH / 8) * (imgW / 8))\n\n        encoder_word_pos = np.array(range(0, feature_dim)).reshape(\n            (feature_dim, 1)).astype('int64')\n        gsrm_word_pos = np.array(range(0, max_text_length)).reshape(\n            (max_text_length, 1)).astype('int64')\n\n        gsrm_attn_bias_data = np.ones((1, max_text_length, max_text_length))\n        gsrm_slf_attn_bias1 = np.triu(gsrm_attn_bias_data, 1).reshape(\n            [-1, 1, max_text_length, max_text_length])\n        gsrm_slf_attn_bias1 = np.tile(\n            gsrm_slf_attn_bias1,\n            [1, num_heads, 1, 1]).astype('float32') * [-1e9]\n\n        gsrm_slf_attn_bias2 = np.tril(gsrm_attn_bias_data, -1).reshape(\n            [-1, 1, max_text_length, max_text_length])\n        gsrm_slf_attn_bias2 = np.tile(\n            gsrm_slf_attn_bias2,\n            [1, num_heads, 1, 1]).astype('float32') * [-1e9]\n\n        encoder_word_pos = encoder_word_pos[np.newaxis, :]\n        gsrm_word_pos = gsrm_word_pos[np.newaxis, :]\n\n        return [\n            encoder_word_pos, gsrm_word_pos, gsrm_slf_attn_bias1,\n            gsrm_slf_attn_bias2\n        ]\n\n    def process_image_srn(self, img, image_shape, num_heads, max_text_length):\n        norm_img = self.resize_norm_img_srn(img, image_shape)\n        norm_img = norm_img[np.newaxis, :]\n\n        [encoder_word_pos, gsrm_word_pos, gsrm_slf_attn_bias1, gsrm_slf_attn_bias2] = \\\n            self.srn_other_inputs(image_shape, num_heads, max_text_length)\n\n        gsrm_slf_attn_bias1 = gsrm_slf_attn_bias1.astype(np.float32)\n        gsrm_slf_attn_bias2 = gsrm_slf_attn_bias2.astype(np.float32)\n        encoder_word_pos = encoder_word_pos.astype(np.int64)\n        gsrm_word_pos = gsrm_word_pos.astype(np.int64)\n\n        return (norm_img, encoder_word_pos, gsrm_word_pos, gsrm_slf_attn_bias1,\n                gsrm_slf_attn_bias2)\n\n    def __call__(self, img_list):\n        img_num = len(img_list)\n        # Calculate the aspect ratio of all text bars\n        width_list = []\n        for img in img_list:\n            width_list.append(img.shape[1] / float(img.shape[0]))\n        # Sorting can speed up the recognition process\n        indices = np.argsort(np.array(width_list))\n        rec_res = [['', 0.0]] * img_num\n        batch_num = self.rec_batch_num\n        st = time.time()\n        for beg_img_no in range(0, img_num, batch_num):\n            end_img_no = min(img_num, beg_img_no + batch_num)\n            norm_img_batch = []\n            max_wh_ratio = 0\n            for ino in range(beg_img_no, end_img_no):\n                h, w = img_list[indices[ino]].shape[0:2]\n                wh_ratio = w * 1.0 / h\n                max_wh_ratio = max(max_wh_ratio, wh_ratio)\n            for ino in range(beg_img_no, end_img_no):\n                if self.rec_algorithm != \"SRN\":\n                    norm_img = self.resize_norm_img(img_list[indices[ino]],\n                                                    max_wh_ratio)\n                    norm_img = norm_img[np.newaxis, :]\n                    norm_img_batch.append(norm_img)\n                else:\n                    norm_img = self.process_image_srn(\n                        img_list[indices[ino]], self.rec_image_shape, 8, 25)\n                    encoder_word_pos_list = []\n                    gsrm_word_pos_list = []\n                    gsrm_slf_attn_bias1_list = []\n                    gsrm_slf_attn_bias2_list = []\n                    encoder_word_pos_list.append(norm_img[1])\n                    gsrm_word_pos_list.append(norm_img[2])\n                    gsrm_slf_attn_bias1_list.append(norm_img[3])\n                    gsrm_slf_attn_bias2_list.append(norm_img[4])\n                    norm_img_batch.append(norm_img[0])\n            norm_img_batch = np.concatenate(norm_img_batch)\n            norm_img_batch = norm_img_batch.copy()\n\n            if self.rec_algorithm == \"SRN\":\n                encoder_word_pos_list = np.concatenate(encoder_word_pos_list)\n                gsrm_word_pos_list = np.concatenate(gsrm_word_pos_list)\n                gsrm_slf_attn_bias1_list = np.concatenate(\n                    gsrm_slf_attn_bias1_list)\n                gsrm_slf_attn_bias2_list = np.concatenate(\n                    gsrm_slf_attn_bias2_list)\n\n                inputs = [\n                    norm_img_batch,\n                    encoder_word_pos_list,\n                    gsrm_word_pos_list,\n                    gsrm_slf_attn_bias1_list,\n                    gsrm_slf_attn_bias2_list,\n                ]\n                input_names = self.predictor.get_input_names()\n                for i in range(len(input_names)):\n                    input_tensor = self.predictor.get_input_handle(input_names[\n                        i])\n                    input_tensor.copy_from_cpu(inputs[i])\n                self.predictor.run()\n                outputs = []\n                for output_tensor in self.output_tensors:\n                    output = output_tensor.copy_to_cpu()\n                    outputs.append(output)\n                preds = {\"predict\": outputs[2]}\n            else:\n                self.input_tensor.copy_from_cpu(norm_img_batch)\n                self.predictor.run()\n\n                outputs = []\n                for output_tensor in self.output_tensors:\n                    output = output_tensor.copy_to_cpu()\n                    outputs.append(output)\n                preds = outputs[0]\n            rec_result = self.postprocess_op(preds)\n            for rno in range(len(rec_result)):\n                rec_res[indices[beg_img_no + rno]] = rec_result[rno]\n\n        return rec_res, time.time() - st\n\n\ndef main(args):\n    image_file_list = get_image_file_list(args.image_dir)\n    text_recognizer = TextRecognizer(args)\n    valid_image_file_list = []\n    img_list = []\n\n    # warmup 10 times\n    if args.warmup:\n        img = np.random.uniform(0, 255, [32, 320, 3]).astype(np.uint8)\n        for i in range(10):\n            res = text_recognizer([img])\n\n    cpu_mem, gpu_mem, gpu_util = 0, 0, 0\n    count = 0\n\n    for image_file in image_file_list:\n        img, flag = check_and_read_gif(image_file)\n        if not flag:\n            img = cv2.imread(image_file)\n        if img is None:\n            logger.info(\"error in loading image:{}\".format(image_file))\n            continue\n        valid_image_file_list.append(image_file)\n        img_list.append(img)\n    try:\n        rec_res, _ = text_recognizer(img_list)\n\n    except Exception as E:\n        logger.info(traceback.format_exc())\n        logger.info(E)\n        exit()\n    for ino in range(len(img_list)):\n        logger.info(\"Predicts of {}:{}\".format(valid_image_file_list[ino],\n                                               rec_res[ino]))\n\n\nif __name__ == \"__main__\":\n    main(utility.parse_args())\n", "path": "tools/infer/predict_rec.py"}], "after_files": [{"content": "# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nimport os\nimport sys\n\n__dir__ = os.path.dirname(os.path.abspath(__file__))\nsys.path.append(__dir__)\nsys.path.append(os.path.abspath(os.path.join(__dir__, '../..')))\n\nos.environ[\"FLAGS_allocator_strategy\"] = 'auto_growth'\n\nimport cv2\nimport numpy as np\nimport math\nimport time\nimport traceback\nimport paddle\n\nimport tools.infer.utility as utility\nfrom ppocr.postprocess import build_post_process\nfrom ppocr.utils.logging import get_logger\nfrom ppocr.utils.utility import get_image_file_list, check_and_read_gif\n\nlogger = get_logger()\n\n\nclass TextRecognizer(object):\n    def __init__(self, args):\n        self.rec_image_shape = [int(v) for v in args.rec_image_shape.split(\",\")]\n        self.character_type = args.rec_char_type\n        self.rec_batch_num = args.rec_batch_num\n        self.rec_algorithm = args.rec_algorithm\n        postprocess_params = {\n            'name': 'CTCLabelDecode',\n            \"character_type\": args.rec_char_type,\n            \"character_dict_path\": args.rec_char_dict_path,\n            \"use_space_char\": args.use_space_char\n        }\n        if self.rec_algorithm == \"SRN\":\n            postprocess_params = {\n                'name': 'SRNLabelDecode',\n                \"character_type\": args.rec_char_type,\n                \"character_dict_path\": args.rec_char_dict_path,\n                \"use_space_char\": args.use_space_char\n            }\n        elif self.rec_algorithm == \"RARE\":\n            postprocess_params = {\n                'name': 'AttnLabelDecode',\n                \"character_type\": args.rec_char_type,\n                \"character_dict_path\": args.rec_char_dict_path,\n                \"use_space_char\": args.use_space_char\n            }\n        self.postprocess_op = build_post_process(postprocess_params)\n        self.predictor, self.input_tensor, self.output_tensors, self.config = \\\n            utility.create_predictor(args, 'rec', logger)\n        self.benchmark = args.benchmark\n        if args.benchmark:\n            import auto_log\n            pid = os.getpid()\n            self.autolog = auto_log.AutoLogger(\n                model_name=\"rec\",\n                model_precision=args.precision,\n                batch_size=args.rec_batch_num,\n                data_shape=\"dynamic\",\n                save_path=args.save_log_path,\n                inference_config=self.config,\n                pids=pid,\n                process_name=None,\n                gpu_ids=0 if args.use_gpu else None,\n                time_keys=[\n                    'preprocess_time', 'inference_time', 'postprocess_time'\n                ],\n                warmup=10)\n\n    def resize_norm_img(self, img, max_wh_ratio):\n        imgC, imgH, imgW = self.rec_image_shape\n        assert imgC == img.shape[2]\n        if self.character_type == \"ch\":\n            imgW = int((32 * max_wh_ratio))\n        h, w = img.shape[:2]\n        ratio = w / float(h)\n        if math.ceil(imgH * ratio) > imgW:\n            resized_w = imgW\n        else:\n            resized_w = int(math.ceil(imgH * ratio))\n        resized_image = cv2.resize(img, (resized_w, imgH))\n        resized_image = resized_image.astype('float32')\n        resized_image = resized_image.transpose((2, 0, 1)) / 255\n        resized_image -= 0.5\n        resized_image /= 0.5\n        padding_im = np.zeros((imgC, imgH, imgW), dtype=np.float32)\n        padding_im[:, :, 0:resized_w] = resized_image\n        return padding_im\n\n    def resize_norm_img_srn(self, img, image_shape):\n        imgC, imgH, imgW = image_shape\n\n        img_black = np.zeros((imgH, imgW))\n        im_hei = img.shape[0]\n        im_wid = img.shape[1]\n\n        if im_wid <= im_hei * 1:\n            img_new = cv2.resize(img, (imgH * 1, imgH))\n        elif im_wid <= im_hei * 2:\n            img_new = cv2.resize(img, (imgH * 2, imgH))\n        elif im_wid <= im_hei * 3:\n            img_new = cv2.resize(img, (imgH * 3, imgH))\n        else:\n            img_new = cv2.resize(img, (imgW, imgH))\n\n        img_np = np.asarray(img_new)\n        img_np = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)\n        img_black[:, 0:img_np.shape[1]] = img_np\n        img_black = img_black[:, :, np.newaxis]\n\n        row, col, c = img_black.shape\n        c = 1\n\n        return np.reshape(img_black, (c, row, col)).astype(np.float32)\n\n    def srn_other_inputs(self, image_shape, num_heads, max_text_length):\n\n        imgC, imgH, imgW = image_shape\n        feature_dim = int((imgH / 8) * (imgW / 8))\n\n        encoder_word_pos = np.array(range(0, feature_dim)).reshape(\n            (feature_dim, 1)).astype('int64')\n        gsrm_word_pos = np.array(range(0, max_text_length)).reshape(\n            (max_text_length, 1)).astype('int64')\n\n        gsrm_attn_bias_data = np.ones((1, max_text_length, max_text_length))\n        gsrm_slf_attn_bias1 = np.triu(gsrm_attn_bias_data, 1).reshape(\n            [-1, 1, max_text_length, max_text_length])\n        gsrm_slf_attn_bias1 = np.tile(\n            gsrm_slf_attn_bias1,\n            [1, num_heads, 1, 1]).astype('float32') * [-1e9]\n\n        gsrm_slf_attn_bias2 = np.tril(gsrm_attn_bias_data, -1).reshape(\n            [-1, 1, max_text_length, max_text_length])\n        gsrm_slf_attn_bias2 = np.tile(\n            gsrm_slf_attn_bias2,\n            [1, num_heads, 1, 1]).astype('float32') * [-1e9]\n\n        encoder_word_pos = encoder_word_pos[np.newaxis, :]\n        gsrm_word_pos = gsrm_word_pos[np.newaxis, :]\n\n        return [\n            encoder_word_pos, gsrm_word_pos, gsrm_slf_attn_bias1,\n            gsrm_slf_attn_bias2\n        ]\n\n    def process_image_srn(self, img, image_shape, num_heads, max_text_length):\n        norm_img = self.resize_norm_img_srn(img, image_shape)\n        norm_img = norm_img[np.newaxis, :]\n\n        [encoder_word_pos, gsrm_word_pos, gsrm_slf_attn_bias1, gsrm_slf_attn_bias2] = \\\n            self.srn_other_inputs(image_shape, num_heads, max_text_length)\n\n        gsrm_slf_attn_bias1 = gsrm_slf_attn_bias1.astype(np.float32)\n        gsrm_slf_attn_bias2 = gsrm_slf_attn_bias2.astype(np.float32)\n        encoder_word_pos = encoder_word_pos.astype(np.int64)\n        gsrm_word_pos = gsrm_word_pos.astype(np.int64)\n\n        return (norm_img, encoder_word_pos, gsrm_word_pos, gsrm_slf_attn_bias1,\n                gsrm_slf_attn_bias2)\n\n    def __call__(self, img_list):\n        img_num = len(img_list)\n        # Calculate the aspect ratio of all text bars\n        width_list = []\n        for img in img_list:\n            width_list.append(img.shape[1] / float(img.shape[0]))\n        # Sorting can speed up the recognition process\n        indices = np.argsort(np.array(width_list))\n        rec_res = [['', 0.0]] * img_num\n        batch_num = self.rec_batch_num\n        st = time.time()\n        if self.benchmark:\n            self.autolog.times.start()\n        for beg_img_no in range(0, img_num, batch_num):\n            end_img_no = min(img_num, beg_img_no + batch_num)\n            norm_img_batch = []\n            max_wh_ratio = 0\n            for ino in range(beg_img_no, end_img_no):\n                h, w = img_list[indices[ino]].shape[0:2]\n                wh_ratio = w * 1.0 / h\n                max_wh_ratio = max(max_wh_ratio, wh_ratio)\n            for ino in range(beg_img_no, end_img_no):\n                if self.rec_algorithm != \"SRN\":\n                    norm_img = self.resize_norm_img(img_list[indices[ino]],\n                                                    max_wh_ratio)\n                    norm_img = norm_img[np.newaxis, :]\n                    norm_img_batch.append(norm_img)\n                else:\n                    norm_img = self.process_image_srn(\n                        img_list[indices[ino]], self.rec_image_shape, 8, 25)\n                    encoder_word_pos_list = []\n                    gsrm_word_pos_list = []\n                    gsrm_slf_attn_bias1_list = []\n                    gsrm_slf_attn_bias2_list = []\n                    encoder_word_pos_list.append(norm_img[1])\n                    gsrm_word_pos_list.append(norm_img[2])\n                    gsrm_slf_attn_bias1_list.append(norm_img[3])\n                    gsrm_slf_attn_bias2_list.append(norm_img[4])\n                    norm_img_batch.append(norm_img[0])\n            norm_img_batch = np.concatenate(norm_img_batch)\n            norm_img_batch = norm_img_batch.copy()\n            if self.benchmark:\n                self.autolog.times.stamp()\n\n            if self.rec_algorithm == \"SRN\":\n                encoder_word_pos_list = np.concatenate(encoder_word_pos_list)\n                gsrm_word_pos_list = np.concatenate(gsrm_word_pos_list)\n                gsrm_slf_attn_bias1_list = np.concatenate(\n                    gsrm_slf_attn_bias1_list)\n                gsrm_slf_attn_bias2_list = np.concatenate(\n                    gsrm_slf_attn_bias2_list)\n\n                inputs = [\n                    norm_img_batch,\n                    encoder_word_pos_list,\n                    gsrm_word_pos_list,\n                    gsrm_slf_attn_bias1_list,\n                    gsrm_slf_attn_bias2_list,\n                ]\n                input_names = self.predictor.get_input_names()\n                for i in range(len(input_names)):\n                    input_tensor = self.predictor.get_input_handle(input_names[\n                        i])\n                    input_tensor.copy_from_cpu(inputs[i])\n                self.predictor.run()\n                outputs = []\n                for output_tensor in self.output_tensors:\n                    output = output_tensor.copy_to_cpu()\n                    outputs.append(output)\n                if self.benchmark:\n                    self.autolog.times.stamp()\n                preds = {\"predict\": outputs[2]}\n            else:\n                self.input_tensor.copy_from_cpu(norm_img_batch)\n                self.predictor.run()\n\n                outputs = []\n                for output_tensor in self.output_tensors:\n                    output = output_tensor.copy_to_cpu()\n                    outputs.append(output)\n                if self.benchmark:\n                    self.autolog.times.stamp()\n                preds = outputs[0]\n            rec_result = self.postprocess_op(preds)\n            for rno in range(len(rec_result)):\n                rec_res[indices[beg_img_no + rno]] = rec_result[rno]\n            if self.benchmark:\n                self.autolog.times.end(stamp=True)\n        return rec_res, time.time() - st\n\n\ndef main(args):\n    image_file_list = get_image_file_list(args.image_dir)\n    text_recognizer = TextRecognizer(args)\n    valid_image_file_list = []\n    img_list = []\n\n    # warmup 10 times\n    if args.warmup:\n        img = np.random.uniform(0, 255, [32, 320, 3]).astype(np.uint8)\n        for i in range(10):\n            res = text_recognizer([img])\n\n    for image_file in image_file_list:\n        img, flag = check_and_read_gif(image_file)\n        if not flag:\n            img = cv2.imread(image_file)\n        if img is None:\n            logger.info(\"error in loading image:{}\".format(image_file))\n            continue\n        valid_image_file_list.append(image_file)\n        img_list.append(img)\n    try:\n        rec_res, _ = text_recognizer(img_list)\n\n    except Exception as E:\n        logger.info(traceback.format_exc())\n        logger.info(E)\n        exit()\n    for ino in range(len(img_list)):\n        logger.info(\"Predicts of {}:{}\".format(valid_image_file_list[ino],\n                                               rec_res[ino]))\n    if args.benchmark:\n        text_recognizer.autolog.report()\n\n\nif __name__ == \"__main__\":\n    main(utility.parse_args())\n", "path": "tools/infer/predict_rec.py"}]}