我已经使用 onnx 格式在 tensorrt 中实现了我的 Pix2Pix GAN 模型。但我不知道如何对 tensorRT 模型进行推理,因为 (3, 512, 512 ) 图像中模型的输入和输出也是 (3, 512, 512) 图像。样本没有清楚地显示如何从 tensorRT 引擎输入和输出图像。
这是我的代码:
def get_image():
img = _load_and_resize("/home/abdullah/Desktop/Profile.png")
img = _shuffle_and_normalize(img)
return img
def _load_and_resize(input_image_path):
image_raw = Image.open(input_image_path)
# convention (width, height) in PIL:
image_resized = image_raw.resize(
(512, 512), resample=Image.BICUBIC)
image_resized = np.array(image_resized, dtype=np.float32, order='C')
return image_resized
def _shuffle_and_normalize(image):
image /= 255.0
# HWC to CHW format:
image = np.transpose(image, [2, 0, 1])
# CHW to NCHW format
image = np.expand_dims(image, axis=0)
# Convert the image to row-major order, also known as "C order":
image = np.array(image, dtype=np.float32, order='C')
return image
def _reshape_output(output):
return np.reshape(output, (512, 512, 3))
def main():
"""Create inference for generator for eye."""
onnx_file_path = 'generator.onnx'
engine_file_path = "generator.trt"
output_shapes = [(1, 3,512, 512)]
image = get_image()
print('IMage shape is ', image.shape)
# Do inference with TensorRT
with get_engine(onnx_file_path, engine_file_path) as engine, engine.create_execution_context() as context:
inputs, outputs, bindings, stream = common.allocate_buffers(engine)
print('Running inference on image {}...'.format(1))
inputs[0].host = image
print(inputs)
trt_outputs = common.do_inference(context, bindings=bindings, inputs=inputs, outputs=outputs, stream=stream)
# Before doing post-processing, we need to reshape the outputs as the common.do_inference will give us flat arrays.
trt_outputs = [output.reshape(shape) for output, shape in zip(trt_outputs, output_shapes)]
image = _reshape_output(trt_outputs)
image = np.array(image, dtype = 'float32')
plt.imshow(image)
plt.show()
print('Output shape is ', trt_outputs.shape)
但是输出并不像预期的那样。请告诉我,将图像输入到 tensorRT 引擎的最佳方式是什么,以及如何从中获得正确的输出。谢谢