Onnx model conversion

Which Khadas SBC do you use?

VIM3 PRO

Which system do you use? Android, Ubuntu, OOWOW or others?

Ubuntu-20.04-gnome

Which version of system do you use? Khadas official images, self built images, or others?

(Khadas official images) vim3-ubuntu-20.04-gnome-linux-4.9-fenix-1.1.2-220930-emmc.img

Please describe your issue below:

To use NPU, keras(MobileNetV3)->onnx->NPU(KSNN) conversion was carried out.

The conversion was successful, but the output is strange when actually using the model.

The ONNX model before conversion is used normally.

I don’t know what is the cause.

can you help me?

Post a console log of your issue below:

The terminal command used for conversion:

./convert --model-name o1 --platform onnx --model q10_221018.onnx --input-size-list '244,244,3' --inputs input_1  --mean-values '0 0 0 0.00392156' --quantized-dtype asymmetric_affine --source-files ./data/dataset2/data.txt --kboard VIM3 --print-level 1

ONNX code (python) before conversion:

onnx_model = onnxruntime.InferenceSession("q10_221018.onnx")

frame=cv2.imread("./test/1_1.jpg")

image1 = cv2.resize(frame, (224, 224))
image1 = image1[..., ::-1]

x = np.expand_dims(image1, axis=0).astype(np.float32)

x = x if isinstance(x, list) else [x]
feed = dict([(input.name, x[n]) for n, input in enumerate(onnx_model.get_inputs())])

yhat_ = onnx_model.run(None, feed)

print(yhat_)


--------------output:

	[array([[0.04316498, 0.91322935, 0.04360576]], dtype=float32)]

KSNN code (python) after conversion:

mobilenet_v3 = KSNN('VIM3')
mobilenet_v3.nn_init(library="./models/libnn_o1.so", model="./models/o1.nb", level=0)

frame=cv2.imread("./test/1_1.jpg")

image1 = cv2.resize(frame, (224, 224))
image1 = image1[..., ::-1]  

x = np.expand_dims(image1, axis=0).astype(np.float32)



yhat_ = mobilenet_v3.nn_inference(x, platform = 'ONNX', reorder='0 1 2', output_format=output_format.OUT_FORMAT_FLOAT32)


print(yhat_)


--------------output:

	[array([0.7128906 , 0.06658936, 0.22058105], dtype=float32)]

1 Like

@muffin Maybe you can try with this, we have a mobilnet demo with caffe.

Why you use those parameters?

Thank you for answer. We’ll follow that example later.

It seems that it was because the conversion did not proceed when I tried to convert without “–mean-values”.

The default value suggested in the example was (128 128 128 0.0078125), but conversion using this value also did not work properly.

example referenced

$ ./convert --model-name inception
–platform tensorflow
–model /home/yan/yan/Yan/models-zoo/tensorflow/inception/inception_v3_2016_08_28_frozen.pb
–input-size-list ‘299,299,3’
–inputs input
–outputs InceptionV3/Predictions/Reshape_1
–mean-values ‘128 128 128 0.0078125’
–quantized-dtype asymmetric_affine
–source-files ./data/dataset/dataset0.txt
–kboard VIM3 --print-level 0