Star-Clouds/CenterFace

Onnx model is rasing error for different input shapes

ViswanathaReddyGajjala opened this issue · 1 comments

The model is working fine for the 32x32 input size. The model is not working for other input shapes(multiples of 32).

import onnxruntime as rt
import numpy

sess = rt.InferenceSession("/content/CenterFace/models/onnx/centerface.onnx")
input_name = sess.get_inputs()[0].name
print("input name", input_name)
input_shape = sess.get_inputs()[0].shape
print("input shape", input_shape)

output_name = sess.get_outputs()[0].name
print("output name", output_name)
output_shape = sess.get_outputs()[0].shape
print("output shape", output_shape)

outputs =  sess.get_outputs()

for i, output in enumerate(outputs):
    print(i, output.name, output.shape)

import numpy.random
x = numpy.random.random((10, 3, 32, 32))
x = x.astype(numpy.float32)
output_names = ['537', '538', '539', '540']
res = sess.run([], {input_name: x})
print([r.shape for r in res])

# for 320x320
x = numpy.random.random((10, 3, 320, 320))
x = x.astype(numpy.float32)
output_names = ['537', '538', '539', '540']
res = sess.run([], {input_name: x})
print([r.shape for r in res])
InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Got invalid dimensions for input: input.1 for the following indices
 index: 2 Got: 320 Expected: 32
 index: 3 Got: 320 Expected: 32
 Please fix either the inputs or the model.


This code reproduces the error. TIA.

import onnx
import math

input_size =(1088,1920)
model = onnx.load_model("centerface.onnx")
d = model.graph.input[0].type.tensor_type.shape.dim
print(d)
rate = (int(math.ceil(input_size[0]/d[2].dim_value)),int(math.ceil(input_size[1]/d[3].dim_value)))
print("rare",rate)
d[0].dim_value = `1`
d[2].dim_value *= rate[0]
d[3].dim_value *= rate[1]
for output in model.graph.output:
    d = output.type.tensor_type.shape.dim
    print(d)
    d[0].dim_value = 1
    d[2].dim_value  *= rate[0]
    d[3].dim_value  *= rate[1]

onnx.save_model(model,"centerface_1088_1920.onnx" )