I trained a model in torch and then convert it to caffe and after that to tf. How to convert it now to onnx?

310 views Asked by At

I trained a Resnet model in torch. Then, I converted it to caffe and to tflite. now I want to convert it to onnx. How can I do it? I try that command:

python3 -m tf2onnx.convert --tflite  resnet.lite --output resnet.lite.onnx --opset 13 --verbose

because the current format of the model is tflite,

and got that error:

return packer_type.unpack_from(memoryview_type(buf), head)[0]
struct.error: unpack_from requires a buffer of at least 11202612 bytes for unpacking 4 bytes at offset 11202608 (actual buffer size is 2408448)

Thanks.

2

There are 2 answers

8
Hiren Namera On

you can try something like this checkout link may be you need to freeze the model layers before starting conversion.

pip install onnxruntime
pip install git+https://github.com/onnx/tensorflow-onnx
python -m tf2onnx.convert --saved-model ./checkpoints/yolov4.tf --output model.onnx --opset 11 --verbose

you can try this one also link

pip install tf2onnx 
import tensorflow as tf
import tf2onnx
import onnx

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(4, activation="relu"))

input_signature = [tf.TensorSpec([3, 3], tf.float32, name='x')]
# Use from_function for tf functions
onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature, opset=13)
onnx.save(onnx_model, "dst/path/model.onnx")
0
Shirly On

You should try check your model file, maybe you have a wrong file, and that error is because of that. try copy / download again the files and then try the tf conversion:

python3 -m tf2onnx.convert --tflite  resnet.lite --output resnet.lite.onnx --opset 13 --verbose