简体   繁体   English

我在 torch 中训练了一个模型,然后将其转换为 caffe,然后再转换为 tf. 现在如何将其转换为onnx?

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

I trained a Resnet model in torch.我在 Torch 中训练了一个 Resnet 模型。 Then, I converted it to caffe and to tflite.然后,我将其转换为 caffe 和 tflite。 now I want to convert it to onnx.现在我想将其转换为 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,因为模型的当前格式是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.谢谢。

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")

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:尝试再次复制/下载文件,然后尝试 tf 转换:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM