如何在tensorRT pytorch GAN模型上进行推断

时间:2019-01-12 07:05:45

标签: deep-learning nvidia pytorch generative-adversarial-network tensorrt

我已经使用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引擎的最佳方法是什么,以及如何从中获取适当的输出。 谢谢

0 个答案:

没有答案