预测库infer的结果和正常infer结果不同
Created by: mozpp
我根据 https://www.paddlepaddle.org.cn/documentation/docs/zh/advanced_usage/deploy/inference/python_infer_cn.html 进行YOLOv3的服务部署。 model是用https://github.com/PaddlePaddle/models/blob/release/1.5/PaddleCV/yolov3/infer.py 保存的 结果不同!!!
# -*- coding: utf-8 -*-
import argparse
import numpy as np
from paddle.fluid.core import PaddleBuf
from paddle.fluid.core import PaddleDType
from paddle.fluid.core import PaddleTensor
from paddle.fluid.core import AnalysisConfig
from paddle.fluid.core import create_paddle_predictor
import cv2
def main():
args = parse_args()
# Set config
config = AnalysisConfig(args.model_dir)
# config.disable_gpu()
# Create PaddlePredictor
predictor = create_paddle_predictor(config)
# Set inputs
inputs = fake_input(args.batch_size)
# img = cv2.imread('test.jpg')
# img = img_reader1(img, 608)
# inputs={'image':img}
# Infer
outputs = predictor.run(inputs)
# parse outputs
output = outputs[0]
print(output.name)
output_data = output.data.float_data()
print(len(output_data))
b=np.array(output_data)
b=b.reshape([-1,6])
print(b)
# bboxes = np.array(outputs[0])
# print(bboxes)
# assert len(output_data) == 512 * args.batch_size
# for i in range(args.batch_size):
# print(np.argmax(output_data[i * 512:(i + 1) * 512]))
def fake_input(batch_size):
image = PaddleTensor()
image.name = "image"
image.dtype = PaddleDType.FLOAT32
# image.data = PaddleBuf(
# np.random.randn(*image.shape).flatten().astype("float32").tolist())
img = cv2.imread('test.jpg')
h, w, _ = img.shape
input_size=608
image.shape = [batch_size, 3, input_size, input_size]
# pixel mean values
pixel_means = [0.485, 0.456, 0.406]
# pixel std values
pixel_stds = [0.229, 0.224, 0.225]
img=img_reader1(img,input_size,pixel_means,pixel_stds)
img=img.reshape(image.shape)
image.data=PaddleBuf(img.flatten().astype("float32").tolist())
im_shape= PaddleTensor()
im_shape.name="im_shape"
im_shape.dtype=PaddleDType.INT32
im_shape.shape = [batch_size,2]
im_shape.data=PaddleBuf(
np.array([h,w]).flatten().astype("int32").tolist())
return [image,im_shape]
def img_reader1(im, size, mean, std):
h, w, _ = im.shape
im_scale_x = size / float(w)
im_scale_y = size / float(h)
out_img = cv2.resize(im, None, None,
fx=im_scale_x, fy=im_scale_y,
interpolation=cv2.INTER_CUBIC)
mean = np.array(mean).reshape((1, 1, -1))
std = np.array(std).reshape((1, 1, -1))
out_img = (out_img / 255.0 - mean) / std
out_img = out_img.transpose((2, 0, 1))
return out_img
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--model_dir", type=str, help="model dir")
parser.add_argument("--prog_file", type=str, help="program filename")
parser.add_argument("--params_file", type=str, help="parameter filename")
parser.add_argument("--batch_size", type=int, default=1, help="batch size")
return parser.parse_args()
if __name__ == "__main__":
main()