Hey, I’ve been trying to build an engine with a Resnet50 built in Keras. Here is an snippet of the code:
# It creates an Onnx file from a Keras model
def fromKeras2Onnx(inputfile, outfile):
if inputfile == None:
print('No Keras Model was inserted, using already pretrained')
#model = VGG16(include_top=True, weights='imagenet')
model = ResNet50(include_top=True, weights='imagenet')
#model = fromKerasScratch2Onnx(outfile)
else :
model = load_model(inputfile)
onnx_model = onnxmltools.convert_keras(model,target_opset=7)
onnxmltools.utils.save_model(onnx_model, outfile)
# It creates a TensorRt engine from an ONNX model
def fromOnnx2TensorRtEngine(onnx_file_path, engine_file_path):
b = False
with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.OnnxParser(network, TRT_LOGGER) as parser:
builder.max_workspace_size = 1 << 30 #1GB
builder.max_batch_size = 1
builder.fp16_mode = True
if not os.path.exists(onnx_file_path):
print('ONNX file {} not found!'.format(onnx_file_path))
exit(0)
print('Loading ONNX file from path {}'.format(onnx_file_path))
with open(onnx_file_path,'rb') as model:
print('Beginning ONNX file parsing')
b = parser.parse(model.read()) # if the parsing was completed b is true
if b:
print('Completed parsing of ONNX file')
print('Building an engine from file {}; this may take a while'.format(onnx_file_path))
engine = builder.build_cuda_engine(network)
del parser
if(engine):
print('Completed creating Engine')
with open(engine_file_path,"wb") as f:
f.write(engine.serialize())
print('Engine saved')
else:
print('Error building engine')
exit(1)
else:
print('Number of errors: {}'.format(parser.num_errors))
error = parser.get_error(0) # if it gets mnore than one error this have to be changed
del parser
desc = error.desc()
line = error.line()
code = error.code()
print('Description of the error: {}'.format(desc))
print('Line where the error occurred: {}'.format(line))
print('Error code: {}'.format(code))
print("Model was not parsed successfully")
exit(0)
When I run my code I get:
Number of errors: 1
Description of the error: Assertion failed: onnx_padding[0] == 0 && onnx_padding[1] == 0 && onnx_padding[4] == 0 && onnx_padding[5] == 0
Line where the error occurred: 1366
Error code: UNSUPPORTED_NODE
Model was not parsed successfully
Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x7f5cb4f83b00>>
However if instead of using the ONNX version from the Keras one I use the ONNX file from here:
wget https://s3.amazonaws.com/onnx-model-zoo/resnet/resnet50v2/resnet50v2.tar.gz
everything works fine. Why this happens? Why do I have unsupported versions if theoretically is the same model? How can I make the Keras version work?