Hi.
I wanna to use tensorrt6 to accelerate my training model using the keras-tensorflow framework.
I have trained the model which is mobilenet-segnet and saved as:
mobilenet_segnet.h5
Then I converted this mobilenet_segnet.h5 to mobilenet_segnet.uff using this command:
from keras.models import load_model
import tensorflow as tf
import keras
import keras.backend as K
from keras.backend.tensorflow_backend import set_session
import uff
from keras.utils.generic_utils import CustomObjectScope
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
config.log_device_placement = True
sess = tf.Session(config = config)
set_session(sess)
keras.backend.get_session().run(tf.initialize_all_variables())
K.set_learning_phase(0)
with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
model = load_model('./mobilenet_segnet.h5')
K.set_learning_phase(0)
output_name = model.output.op.name
input_name = model.input.op.name
frozen_graph = tf.graph_util.remove_training_nodes(tf.graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(),[output_name]))
uff_model = uff.from_tensorflow(frozen_graph, output_filename = './mobilenet_segnet.uff')
Then I can see a mobilenet_segnet.uff in my path and show some result as following:
NOTE: UFF has been tested with TensorFlow 1.14.0.
WARNING: The version of TensorFlow installed on this system is not guaranteed to work with UFF.
UFF Version 0.6.5
=== Automatically deduced input nodes ===
[name: "input_1"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "shape"
value {
shape {
dim {
size: -1
}
dim {
size: 224
}
dim {
size: 224
}
dim {
size: 3
}
}
}
}
]
=========================================
=== Automatically deduced output nodes ===
[name: "activation_1/truediv"
op: "RealDiv"
input: "activation_1/Exp"
input: "activation_1/Sum"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
]
==========================================
Using output node activation_1/truediv
Converting to UFF graph
Warning: keepdims is ignored by the UFF Parser and defaults to True
Warning: keepdims is ignored by the UFF Parser and defaults to True
Warning: No conversion function registered for layer: AddV2 yet.
Converting batch_normalization_4/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting batch_normalization_4/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: ResizeNearestNeighbor yet.
Converting up_sampling2d_3/ResizeNearestNeighbor as custom op: ResizeNearestNeighbor
Warning: No conversion function registered for layer: AddV2 yet.
Converting batch_normalization_3/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting batch_normalization_3/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: ResizeNearestNeighbor yet.
Converting up_sampling2d_2/ResizeNearestNeighbor as custom op: ResizeNearestNeighbor
Warning: No conversion function registered for layer: AddV2 yet.
Converting batch_normalization_2/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting batch_normalization_2/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: ResizeNearestNeighbor yet.
Converting up_sampling2d_1/ResizeNearestNeighbor as custom op: ResizeNearestNeighbor
Warning: No conversion function registered for layer: AddV2 yet.
Converting batch_normalization_1/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting batch_normalization_1/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_11_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_11_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_11_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_11_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_10_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_10_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_10_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_10_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_9_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_9_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_9_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_9_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_8_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_8_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_8_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_8_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_7_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_7_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_7_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_7_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_6_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_6_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_6_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_6_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_5_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_5_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_5_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_5_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_4_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_4_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_4_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_4_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_3_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_3_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_3_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_3_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_2_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_2_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_2_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_2_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_1_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_pw_1_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_1_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv_dw_1_bn/batchnorm/add as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv1_bn/batchnorm/add_1 as custom op: AddV2
Warning: No conversion function registered for layer: AddV2 yet.
Converting conv1_bn/batchnorm/add as custom op: AddV2
DEBUG [/usr/lib/python3.6/dist-packages/uff/converters/tensorflow/converter.py:96] Marking ['activation_1/truediv'] as outputs
No. nodes: 499
UFF Output written to ./mobilenet_segnet.uff
Then I try to continue to run this command in a new terminal:
import tensorrt as trt
model_file = './mobilenet_segnet.uff'
TRT_LOGGER = trt.Logger(trt.Logger.WARNING)
def build_engine(model_file):
with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.UffParser() as parser:
builder.max_workspace_size = 1 << 20
parser.register_input("input_1", (224, 224, 3))
parser.register_output("activation_1/truediv")
parser.parse(model_file, network)
return builder.build_cuda_engine(network)
with build_engine(model_file) as engine:
print('1')
And the terminal shows as following:
[TensorRT] ERROR: UffParser: Validator error: batch_normalization_4/batchnorm/add: Unsupported operation _AddV2
Is that mean I should use gs.create_plugin_node to build some new trt_nodes to replace original nodes such as the Relu6, DepthwiseConv2D, PointwiseCon2D? How to create a new node for this problem?
Then I try to create a new mobilenet_segnet.uff as following in a new terminal:
from keras.models import load_model
import tensorflow as tf
import keras
import keras.backend as K
from keras.backend.tensorflow_backend import set_session
import uff
from keras.utils.generic_utils import CustomObjectScope
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
config.log_device_placement = True
sess = tf.Session(config = config)
set_session(sess)
keras.backend.get_session().run(tf.initialize_all_variables())
K.set_learning_phase(0)
with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
model = load_model('./mobilenet_segnet.h5')
K.set_learning_phase(0)
output_name = model.output.op.name
input_name = model.input.op.name
frozen_graph = tf.graph_util.remove_training_nodes(tf.graph_util.convert_variables_to_constants(sess, sess.graph.as_graph_def(),[output_name]))
# ---------*new code*-------------
import graphsurgeon as gs
dynamic_graph = gs.DynamicGraph(frozen_graph)
add_nodes = dynamic_graph.find_nodes_by_op('AddV2')
for node in add_nodes:
node.op = "Add"
# ---------*new code*-------------
uff_model = uff.from_tensorflow(frozen_graph, output_filename = './mobilenet_segnet.uff')
Then the terminal shows as following:
NOTE: UFF has been tested with TensorFlow 1.14.0.
WARNING: The version of TensorFlow installed on this system is not guaranteed to work with UFF.
UFF Version 0.6.5
=== Automatically deduced input nodes ===
[name: "input_1"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_FLOAT
}
}
attr {
key: "shape"
value {
shape {
dim {
size: -1
}
dim {
size: 224
}
dim {
size: 224
}
dim {
size: 3
}
}
}
}
]
=========================================
=== Automatically deduced output nodes ===
[name: "activation_1/truediv"
op: "RealDiv"
input: "activation_1/Exp"
input: "activation_1/Sum"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
]
==========================================
Using output node activation_1/truediv
Converting to UFF graph
Warning: keepdims is ignored by the UFF Parser and defaults to True
Warning: keepdims is ignored by the UFF Parser and defaults to True
Warning: No conversion function registered for layer: ResizeNearestNeighbor yet.
Converting up_sampling2d_3/ResizeNearestNeighbor as custom op: ResizeNearestNeighbor
WARNING:tensorflow:From /usr/lib/python3.6/dist-packages/uff/converters/tensorflow/converter.py:179: The name tf.AttrValue is deprecated. Please use tf.compat.v1.AttrValue instead.
Warning: No conversion function registered for layer: ResizeNearestNeighbor yet.
Converting up_sampling2d_2/ResizeNearestNeighbor as custom op: ResizeNearestNeighbor
Warning: No conversion function registered for layer: ResizeNearestNeighbor yet.
Converting up_sampling2d_1/ResizeNearestNeighbor as custom op: ResizeNearestNeighbor
DEBUG [/usr/lib/python3.6/dist-packages/uff/converters/tensorflow/converter.py:96] Marking ['activation_1/truediv'] as outputs
No. nodes: 499
UFF Output written to ./mobilenet_segnet.uff
Then I try to do this again in a new terminal:
import tensorrt as trt
model_file = './mobilenet_segnet.uff'
TRT_LOGGER = trt.Logger(trt.Logger.WARNING)
def build_engine(model_file):
with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.UffParser() as parser:
builder.max_workspace_size = 1 << 20
parser.register_input("input_1", (224, 224, 3))
parser.register_output("activation_1/truediv")
parser.parse(model_file, network)
return builder.build_cuda_engine(network)
with build_engine(model_file) as engine:
print('1')
And shows as following:
[TensorRT] ERROR: UffParser: Validator error: up_sampling2d_1/ResizeNearestNeighbor: Unsupported operation _ResizeNearestNeighbor
How to solve it?
Many thanks.
My platform is Jetson Nano Jetpack4.2 keras 2.1.3 tensorflow 1.14 Tensorrt6.
Many thanks.