From 0dcb5a6789d1a69a8ec416c5478faece0cac5c72 Mon Sep 17 00:00:00 2001 From: shippingwang Date: Fri, 10 Apr 2020 09:19:21 +0000 Subject: [PATCH] fix --- docs/zh_cn/tutorials/getting_started.md | 13 +++++ tools/infer/{cpp_infer.py => predict.py} | 67 ++++++++++++++++-------- tools/infer/run.sh | 49 ----------------- tools/infer/utils.py | 1 - 4 files changed, 58 insertions(+), 72 deletions(-) rename tools/infer/{cpp_infer.py => predict.py} (55%) delete mode 100644 tools/infer/run.sh diff --git a/docs/zh_cn/tutorials/getting_started.md b/docs/zh_cn/tutorials/getting_started.md index fa1e9f1f..0993ed06 100644 --- a/docs/zh_cn/tutorials/getting_started.md +++ b/docs/zh_cn/tutorials/getting_started.md @@ -62,3 +62,16 @@ python eval.py \ -o pretrained_model=path_to_pretrained_models ``` 您可以更改configs/eval.yaml中的architecture字段和pretrained_model字段来配置评估模型,或是通过-o参数更新配置。 + +## 3 模型推理 + +PaddleClas通过预测引擎进行预测推理 + +```bash +python tools/predict.py \ + -m model文件路径 + -p params文件路径 + -i 图片路径 + --use_tensorrt True +``` +更多推理方式和实验请参考[分类预测框架](../extension/paddle_inference.md) diff --git a/tools/infer/cpp_infer.py b/tools/infer/predict.py similarity index 55% rename from tools/infer/cpp_infer.py rename to tools/infer/predict.py index 665dfa16..54838b45 100644 --- a/tools/infer/cpp_infer.py +++ b/tools/infer/predict.py @@ -12,14 +12,17 @@ # See the License for the specific language governing permissions and # limitations under the License. + import utils import argparse import numpy as np - +import logging +import time from paddle.fluid.core import PaddleTensor from paddle.fluid.core import AnalysisConfig from paddle.fluid.core import create_paddle_predictor - +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) def parse_args(): def str2bool(v): @@ -29,10 +32,14 @@ def parse_args(): parser.add_argument("-i", "--image_file", type=str) parser.add_argument("-m", "--model_file", type=str) parser.add_argument("-p", "--params_file", type=str) - parser.add_argument("-b", "--max_batch_size", type=int, default=1) + parser.add_argument("-b", "--batch_size", type=int, default=1) + parser.add_argument("--use_fp16", type=str2bool, default=False) parser.add_argument("--use_gpu", type=str2bool, default=True) parser.add_argument("--ir_optim", type=str2bool, default=True) parser.add_argument("--use_tensorrt", type=str2bool, default=False) + parser.add_argument("--gpu_mem", type=int, default=8000) + parser.add_argument("--enable_benchmark", type=str2bool, default=False) + parser.add_argument("--model_name", type=str) return parser.parse_args() @@ -40,15 +47,19 @@ def parse_args(): def create_predictor(args): config = AnalysisConfig(args.model_file, args.params_file) if args.use_gpu: - config.enable_use_gpu(1000, 0) + config.enable_use_gpu(args.gpu_mem, 0) else: config.disable_gpu() - - config.switch_ir_optim(args.ir_optim) # default true + config.disable_glog_info() + config.switch_ir_optim(args.ir_optim) # default true if args.use_tensorrt: config.enable_tensorrt_engine( - precision_mode=AnalysisConfig.Precision.Float32, - max_batch_size=args.max_batch_size) + precision_mode=AnalysisConfig.Precision.Half if args.use_fp16 else AnalysisConfig.Precision.Float32, + max_batch_size=args.batch_size) + + config.enable_memory_optim() + # use zero copy + config.switch_use_feed_fetch_ops(False) predictor = create_paddle_predictor(config) return predictor @@ -64,7 +75,7 @@ def create_operators(): resize_op = utils.ResizeImage(resize_short=256) crop_op = utils.CropImage(size=(size, size)) normalize_op = utils.NormalizeImage( - scale=img_scale, mean=img_mean, std=img_std) + scale=img_scale, mean=img_mean, std=img_std) totensor_op = utils.ToTensor() return [decode_op, resize_op, crop_op, normalize_op, totensor_op] @@ -78,25 +89,37 @@ def preprocess(fname, ops): return data -def postprocess(outputs, topk=5): - output = outputs[0] - prob = output.as_ndarray().flatten() - index = prob.argsort(axis=0)[-topk:][::-1].astype('int32') - return zip(index, prob[index]) - - def main(): args = parse_args() operators = create_operators() predictor = create_predictor(args) - data = preprocess(args.image_file, operators) - inputs = [PaddleTensor(data.copy())] - outputs = predictor.run(inputs) - probs = postprocess(outputs) + inputs = preprocess(args.image_file, operators) + inputs = np.expand_dims(inputs, axis=0).repeat(args.batch_size, axis=0).copy() - for idx, prob in probs: - print("class id: {:d}, probability: {:.4f}".format(idx, prob)) + input_names = predictor.get_input_names() + input_tensor = predictor.get_input_tensor(input_names[0]) + input_tensor.copy_from_cpu(inputs) + if not args.enable_benchmark: + predictor.zero_copy_run() + else: + for i in range(0,1010): + if i == 10: + start = time.time() + predictor.zero_copy_run() + + end = time.time() + fp_message = "FP16" if args.use_fp16 else "FP32" + logger.info("{0}\t{1}\tbatch size: {2}\ttime(ms): {3}".format(args.model_name, fp_message, args.batch_size, end-start)) + + output_names = predictor.get_output_names() + output_tensor = predictor.get_output_tensor(output_names[0]) + output = output_tensor.copy_to_cpu() + output = output.flatten() + cls = np.argmax(output) + score = output[cls] + logger.info("class: {0}".format(cls)) + logger.info("score: {0}".format(score)) if __name__ == "__main__": diff --git a/tools/infer/run.sh b/tools/infer/run.sh deleted file mode 100644 index abed02cd..00000000 --- a/tools/infer/run.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env bash - -python ./cpp_infer.py \ - -i=./test.jpeg \ - -m=./resnet50-vd/model \ - -p=./resnet50-vd/params \ - --use_gpu=1 - -python ./cpp_infer.py \ - -i=./test.jpeg \ - -m=./resnet50-vd/model \ - -p=./resnet50-vd/params \ - --use_gpu=0 - -python py_infer.py \ - -i=./test.jpeg \ - -d ./resnet50-vd/ \ - -m=model -p=params \ - --use_gpu=0 - -python py_infer.py \ - -i=./test.jpeg \ - -d ./resnet50-vd/ \ - -m=model -p=params \ - --use_gpu=1 - -python infer.py \ - -i=./test.jpeg \ - -m ResNet50_vd \ - -p ./resnet50-vd-persistable/ \ - --use_gpu=0 - -python infer.py \ - -i=./test.jpeg \ - -m ResNet50_vd \ - -p ./resnet50-vd-persistable/ \ - --use_gpu=1 - -python export_model.py \ - -m ResNet50_vd \ - -p ./resnet50-vd-persistable/ \ - -o ./test/ - -python py_infer.py \ - -i=./test.jpeg \ - -d ./test/ \ - -m=model \ - -p=params \ - --use_gpu=0 diff --git a/tools/infer/utils.py b/tools/infer/utils.py index 2e65c3f3..6c4a75e1 100644 --- a/tools/infer/utils.py +++ b/tools/infer/utils.py @@ -81,5 +81,4 @@ class ToTensor(object): def __call__(self, img): img = img.transpose((2, 0, 1)) - img = np.expand_dims(img, axis=0) return img -- GitLab