How to export a model from Tensorflow/Keras to ONNX?

191 views Asked by At

I'm trying to export a tensorflow model to ONNX using tf2onnx but I'm getting an error. I use Google Colab to train the model.

Model summary:

Model: "sequential_6"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 rescaling_6 (Rescaling)     (None, 256, 256, 1)       0         
                                                                 
 conv2d_18 (Conv2D)          (None, 256, 256, 16)      160       
                                                                 
 max_pooling2d_18 (MaxPooli  (None, 128, 128, 16)      0         
 ng2D)                                                           
                                                                 
 conv2d_19 (Conv2D)          (None, 128, 128, 32)      4640      
                                                                 
 max_pooling2d_19 (MaxPooli  (None, 64, 64, 32)        0         
 ng2D)                                                           
                                                                 
 conv2d_20 (Conv2D)          (None, 64, 64, 64)        18496     
                                                                 
 max_pooling2d_20 (MaxPooli  (None, 32, 32, 64)        0         
 ng2D)                                                           
                                                                 
 flatten_6 (Flatten)         (None, 65536)             0         
                                                                 
 dense_12 (Dense)            (None, 128)               8388736   
                                                                 
 dense_13 (Dense)            (None, 904)               116616    
                                                                 
=================================================================
Total params: 8528648 (32.53 MB)
Trainable params: 8528648 (32.53 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

Here's the code I use to export:

onnx_model, _ = tf2onnx.convert.from_keras(model, output_path="model.onnx")

Here's the error I get:

WARNING:tf2onnx.tf_loader:Could not search for non-variable resources. Concrete function internal representation may have changed.
ERROR:tf2onnx.tf_utils:pass1 convert failed for name: "sequential_6/conv2d_18/Conv2D"
op: "Conv2D"
input: "sequential_6/rescaling_6/add"
input: "sequential_6/conv2d_18/Conv2D/ReadVariableOp"
...
, ex=Could not infer attribute `explicit_paddings` type from empty iterator
1

There are 1 answers

0
Nicholas Kinnaird On

This issue I believe is a bug in some tf2onnx/onnx versions, see here: https://github.com/onnx/tensorflow-onnx/issues/2262

According to that thread it should be fixed in the most recent release, and does appear to work with versions:

onnx                         1.15.0
tf2onnx                      1.16.0

I also had it working in the past with versions:

onnx                         1.14.1
tf2onnx                      1.14.0

There may also be dependencies on the TensorFlow version used to train the model, but I'm not sure. Using the most recent TensorFlow, onnx, and tf2onnx works for me.

I'm not sure about the initial warning: WARNING:tf2onnx.tf_loader:Could not search for non-variable resources. Concrete function internal representation may have changed., which is what I was searching up when I found this thread. My models appear to be working correctly with it regardless.