用于放大图像的TensorRT不会产生预期的结果

时间:2018-05-24 15:14:25

标签: python numpy tensorflow pycuda tensorrt

几个星期后,我正在与TensorRT(TensorRT 4 for python)战斗。我通过了很多问题来让TensorRT运行。 NVIDIA的示例代码对我很有用: TensorRT MNIST example

现在,我在tensorflow(一个非常简单的)中创建了我自己的网络来升级图像,让我们说(在HWC中)320x240x3到640x480x3。通常的方法是创建一个冻结图并运行一个基于Tensorflow的推理器给出我预期的结果,但没有使用TensorRT。

我有一种奇怪的感觉,我将图像输入GPU内存时出错了(这可能是关于pycuda和/或TensorRT的问题)。

最糟糕的情况是TensorRT会通过优化过程破坏我的网络。

我希望有人能够挽救我的生命。 这是我的Tensorflow模型(我只是包装了函数):

net = conv2d(input,
             64,
             k_size=3,
             activation=tf.nn.relu,
             name='conv1')

net = deconv2d(net,
               3,
               k_size=5,
               activation=tf.tanh,
               stride=self.params.resize_factor,
               scale=self.params.resize_factor,
               name='deconv')

这是我的推理者的重要片段:

import tensorrt as trt
import uff
from tensorrt.parsers import uffparser
import pycuda.driver as cuda
import numpy as np
...

def _init_infer(self, uff_model):

   g_logger = trt.infer.ConsoleLogger(trt.infer.LogSeverity.ERROR)

   parser = uffparser.create_uff_parser()
   parser.register_input(self.input_node, (self.channels, self.height, self.width), 0)
   parser.register_output(self.output_node)

   self.engine = trt.utils.uff_to_trt_engine(g_logger, uff_model, parser, self.max_batch_size,
                                              self.max_workspace_size)

   parser.destroy()

   self.runtime = trt.infer.create_infer_runtime(g_logger)
   self.context = self.engine.create_execution_context()

   self.output = np.empty(self.output_size, dtype=self.dtype)

   # create CUDA stream
   self.stream = cuda.Stream()

   # allocate device memory
   self.d_input = cuda.mem_alloc(self.channels * self.max_batch_size * self.width *
                                  self.height * self.output.dtype.itemsize)
   self.d_output = cuda.mem_alloc(self.output_size * self.output.dtype.itemsize)

   self.bindings = [int(self.d_input), int(self.d_output)]

def infer(self, input_batch, batch_size=1):

    # transfer input data to device
    cuda.memcpy_htod_async(self.d_input, input_batch, self.stream)
    # execute model
    self.context.enqueue(batch_size, self.bindings, self.stream.handle, None)
    # transfer predictions back
    cuda.memcpy_dtoh_async(self.output, self.d_output, self.stream)
    # synchronize threads
    self.stream.synchronize()

    return self.output

可执行代码段:

...
# create trt inferencer
trt_inferencer = TensorRTInferencer(params=params)

img = [misc.imread('./test_images/lion.png')]
img[0] = normalize(img[0])
img = img[0]

# inferencing method
result = trt_inferencer.infer(img)
result = inormalize(result, dtype=np.uint8)

result = result.reshape(1, params.height * 2, params.width * 2, 3)
...

通过比较奇怪的结果:( upscaled lion TensorRT, Tensorflow, Original

1 个答案:

答案 0 :(得分:1)

我终于明白了。问题是输入图像和输出的维度和顺序错误。对于遇到同样问题的每个人来说,这是采用的可执行代码段,取决于我的初始化:

...
# create trt inferencer
trt_inferencer = TensorRTInferencer(params=params)

img = [misc.imread('./test_images/lion.png')]
img[0] = normalize(img[0])
img = img[0]

img = np.transpose(img, (2, 0, 1))
img = img.ravel()

# inferencing method
result = trt_inferencer.infer(img)
result = inormalize(result, dtype=np.uint8)

result = np.reshape(result, newshape=[3, params.height * 2, params.width * 2])
result = np.transpose(result, (1, 2, 0))
...