predict_rec.py 5.2 KB
Newer Older
littletomatodonkey's avatar
littletomatodonkey 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 cv2
import numpy as np

19 20 21 22 23
from paddleclas.deploy.utils import logger, config
from paddleclas.deploy.utils.predictor import Predictor
from paddleclas.deploy.utils.get_image_list import get_image_list
from paddleclas.deploy.python.preprocess import create_operators
from paddleclas.deploy.python.postprocess import build_postprocess
littletomatodonkey's avatar
littletomatodonkey 已提交
24 25 26 27 28 29 30 31 32


class RecPredictor(Predictor):
    def __init__(self, config):
        super().__init__(config["Global"],
                         config["Global"]["rec_inference_model_dir"])
        self.preprocess_ops = create_operators(config["RecPreProcess"][
            "transform_ops"])
        self.postprocess = build_postprocess(config["RecPostProcess"])
D
dongshuilong 已提交
33 34
        self.benchmark = config["Global"].get("benchmark", False)

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        if self.benchmark:
            import auto_log
            pid = os.getpid()
            self.auto_logger = auto_log.AutoLogger(
                model_name=config["Global"].get("model_name", "rec"),
                model_precision='fp16'
                if config["Global"]["use_fp16"] else 'fp32',
                batch_size=config["Global"].get("batch_size", 1),
                data_shape=[3, 224, 224],
                save_path=config["Global"].get("save_log_path",
                                               "./auto_log.log"),
                inference_config=self.config,
                pids=pid,
                process_name=None,
                gpu_ids=None,
                time_keys=[
                    'preprocess_time', 'inference_time', 'postprocess_time'
                ],
                warmup=2)
littletomatodonkey's avatar
littletomatodonkey 已提交
54

F
Felix 已提交
55
    def predict(self, images, feature_normalize=True):
56 57 58 59
        use_onnx = self.args.get("use_onnx", False)
        if not use_onnx:
            input_names = self.predictor.get_input_names()
            input_tensor = self.predictor.get_input_handle(input_names[0])
littletomatodonkey's avatar
littletomatodonkey 已提交
60

61 62 63 64 65
            output_names = self.predictor.get_output_names()
            output_tensor = self.predictor.get_output_handle(output_names[0])
        else:
            input_names = self.predictor.get_inputs()[0].name
            output_names = self.predictor.get_outputs()[0].name
littletomatodonkey's avatar
littletomatodonkey 已提交
66

D
dongshuilong 已提交
67 68
        if self.benchmark:
            self.auto_logger.times.start()
littletomatodonkey's avatar
littletomatodonkey 已提交
69 70 71 72 73 74
        if not isinstance(images, (list, )):
            images = [images]
        for idx in range(len(images)):
            for ops in self.preprocess_ops:
                images[idx] = ops(images[idx])
        image = np.array(images)
D
dongshuilong 已提交
75 76
        if self.benchmark:
            self.auto_logger.times.stamp()
littletomatodonkey's avatar
littletomatodonkey 已提交
77

78 79 80 81 82 83 84 85 86
        if not use_onnx:
            input_tensor.copy_from_cpu(image)
            self.predictor.run()
            batch_output = output_tensor.copy_to_cpu()
        else:
            batch_output = self.predictor.run(
                output_names=[output_names],
                input_feed={input_names: image})[0]

D
dongshuilong 已提交
87 88
        if self.benchmark:
            self.auto_logger.times.stamp()
89

F
Felix 已提交
90 91 92 93
        if feature_normalize:
            feas_norm = np.sqrt(
                np.sum(np.square(batch_output), axis=1, keepdims=True))
            batch_output = np.divide(batch_output, feas_norm)
94 95 96

        if self.postprocess is not None:
            batch_output = self.postprocess(batch_output)
D
dongshuilong 已提交
97 98 99

        if self.benchmark:
            self.auto_logger.times.end(stamp=True)
littletomatodonkey's avatar
littletomatodonkey 已提交
100 101 102 103 104 105 106
        return batch_output


def main(config):
    rec_predictor = RecPredictor(config)
    image_list = get_image_list(config["Global"]["infer_imgs"])

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    batch_imgs = []
    batch_names = []
    cnt = 0
    for idx, img_path in enumerate(image_list):
        img = cv2.imread(img_path)
        if img is None:
            logger.warning(
                "Image file failed to read and has been skipped. The path: {}".
                format(img_path))
        else:
            img = img[:, :, ::-1]
            batch_imgs.append(img)
            img_name = os.path.basename(img_path)
            batch_names.append(img_name)
            cnt += 1

D
dongshuilong 已提交
123 124 125
        if cnt % config["Global"]["batch_size"] == 0 or (idx + 1
                                                         ) == len(image_list):
            if len(batch_imgs) == 0:
126
                continue
D
dongshuilong 已提交
127

128 129 130 131 132 133
            batch_results = rec_predictor.predict(batch_imgs)
            for number, result_dict in enumerate(batch_results):
                filename = batch_names[number]
                print("{}:\t{}".format(filename, result_dict))
            batch_imgs = []
            batch_names = []
D
dongshuilong 已提交
134 135
    if rec_predictor.benchmark:
        rec_predictor.auto_logger.report()
136

littletomatodonkey's avatar
littletomatodonkey 已提交
137 138 139 140 141 142 143
    return


if __name__ == "__main__":
    args = config.parse_args()
    config = config.get_config(args.config, overrides=args.override, show=True)
    main(config)