tf.pad is not supported by UFFParser.

Hi, I’m trying to implement a Yolo V3 by Tensorflow and tensorRT 3.0 GA on TX2, however, when I try to convert the .pb to .plan with UFFParser in python API, this is what happened:

Warning: No conversion function registered for layer: Pad yet.
Converting as custom op Pad convolutional_9/Pad
name: "convolutional_9/Pad"
op: "Pad"
input: "shortcut_2/Add"
input: "convolutional_9/Const"
attr {
  key: "T"
  value {
    type: DT_FLOAT
  }
}
attr {
  key: "Tpaddings"
  value {
    type: DT_INT32
  }
}

And of course, the conversion failed with below exception:

UFFParser: Validator error: convolutional_9/Pad: Unsupported operation _Pad
Failed to parse UFF

This is what I found in tensorRT’s doc, seems that padding should be supported in my case.

“Pad is supported if followed by one of these TensorFlow layers: Conv2D,
DepthwiseConv2dNative, MaxPool, and AvgPool”
From: https://docs.nvidia.com/deeplearning/sdk/tensorrt-developer-guide/index.html#support_op

This is what I found in Nvidia’s official demo code, which is almost the same as I adopt in my case.
From: https://docs.nvidia.com/deeplearning/sdk/tensorrt-api/topics/topics/workflows/tf_to_tensorrt.html#Training-a-Model-in-Tensorflow

def Conv2d(x, W, b, strides=1):
    # Conv2D wrapper, with bias and relu activation
    filter_size = W.get_shape().as_list()
    pad_size = filter_size[0]//2
    pad_mat = np.array([[0,0],[pad_size,pad_size],[pad_size,pad_size],[0,0]])
    x = tf.pad(x, pad_mat)
    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='VALID')
    x = tf.nn.bias_add(x, b)
    return tf.nn.relu(x)

Is it because padding is only supported in the latest TensorRT 4.0 or only supported in C++ API?

Thank you.

UPDATE:

I set up a very simple inference model follow the demo, and the problem came out again:

import tensorflow as tf
import uff
import numpy as np

input_img = tf.placeholder(shape=(None,None,None,3),  dtype=tf.float32, name='input')

def WeightsVariable(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.1, name='weights'))

def BiasVariable(shape):
    return tf.Variable(tf.constant(0.1, shape=shape, name='biases'))

def Conv2d(x, W, b, strides=1):
    # Conv2D wrapper, with bias and relu activation
    filter_size = W.get_shape().as_list()
    pad_size = filter_size[0]//2
    pad_mat = np.array([[0,0],[<b><u><i>pad_size_0,pad_size_1</i></u></b>],[<b><i><u>pad_size_2,pad_size_3</u></i></b>],[0,0]])
    x = tf.pad(x, pad_mat)
    x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='VALID')
    x = tf.nn.bias_add(x, b)

shape=[3,3,3,16]
weights = WeightsVariable(shape)
biases = BiasVariable([shape[-1]])
x = tf.nn.relu(Conv2d(input_img, weights, biases), name='relu')
 
with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    constant_graph = tf.graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(), ['relu'])
    uff_model = uff.from_tensorflow(constant_graph, ['relu'])

This is the error:

Warning: No conversion function registered for layer: Pad yet.
Converting as custom op Pad Pad_1
name: "Pad_1"
op: "Pad"
input: "Placeholder"
input: "Pad_1/paddings"
attr {
  key: "T"
  value {
    type: DT_FLOAT
  }
}
attr {
  key: "Tpaddings"
  value {
    type: DT_INT32
  }
}

Seems that the padding has to be symmetric for each dimension, e.g., pad_size_0 == pad_size_1 and pad_size_2 == pad_size_3, then the problem is gone. However, this is not accordant with the official doc “The Padding layer implements spatial zero-padding of tensors. Padding can be different on each axis, asymmetric, and either positive (resulting in expansion of the tensor) or negative (resulting in trimming).https://docs.nvidia.com/deeplearning/sdk/tensorrt-developer-guide/index.html#layers. Seems that I have no choice but to rewrite my padding operation in low level Tensorflow operations. I’m not sure if this is also true for C++ API.

@Tony_Su

Were you able to solve the issue? I am also trying to use YOLOV3 (tiny-yolov3 specifically) with tensorRT and I have been unable to do so.

Please let me know if you have a working implementation, I have been stuck on this issue for quite a while.

Thanks

The link you posted is for documentation to TRT 4, you are using a different version. Look for its that version’s documentation.