keras-onnx icon indicating copy to clipboard operation
keras-onnx copied to clipboard

Convert Sequential model throws "Unsupported dimension type:...

Open gurkan8941 opened this issue 4 years ago • 3 comments

Hi,

I have a binary classification model with this architecture: Built with tf.keras using tensorflow version 2.3.0

def create_model_architecture(input_dimension, layers=[96], act='relu', dr=[0.10], batch_size_value=None):
    model = Sequential()
    model.add(Input(shape=(input_dimension,), batch_size=batch_size_value))
    model.add(Dense(layers[0], activation=act))
    model.add(Dropout(dr[0]))
    # create output layer
    model.add(Dense(1, activation='sigmoid'))
    return model

Converting to ONNX (using latest version 1.7.0):

 keras_model_single_batch = create_model_architecture(113, batch_size_value=1)
 keras_model_single_batch.set_weights(trained_weights)
 onnx_model = keras2onnx.convert_keras(keras_model_single_batch)

throws "Unsupported dimension type:" error in data_types.py in method to_onnx_type. But my input shape is (1,113) in this case for the keras model so did not understand how that can be unsupported dimensions.

I updated the onnx conversion package code like below (had to set d = dim.value) and then conversion was successful:

   def to_onnx_type(self):
        onnx_type = onnx_proto.TypeProto()
        onnx_type.tensor_type.elem_type = self._get_element_onnx_type()
        for dim in self.shape:
            d = dim.value
            s = onnx_type.tensor_type.shape.dim.add()
            if d is None:
                pass
            elif isinstance(d, numbers.Integral):
                s.dim_value = d
            elif isinstance(d, str):
                s.dim_param = d
            else:
                raise ValueError('Unsupported dimension type: %s, see %s' % (
                    type(d), "https://github.com/onnx/onnx/blob/master/docs/IR.md#" +
                    "input--output-data-types"))
        if getattr(onnx_type, 'denotation', None) is not None:
            if self.denotation:
                onnx_type.denotation = self.denotation
            if self.channel_denotations:
                for d, denotation in zip(onnx_type.tensor_type.shape.dim,
                                         self.channel_denotations):
                    if denotation:
                        d.denotation = denotation
        return onnx_type

Can anyone help explain this. Would rather not modify the package locally. Thanks for a great package!

ENV:

  • virtualenv python 3.8
  • onnx 1.8.0
  • keras2onnx 1.7.0
  • tensorflow 2.3.0

gurkan8941 avatar Dec 04 '20 13:12 gurkan8941

Same problem using on Windows: tensorflow==2.3.1 tensorflow-gpu==2.3.1 keras2onnx==1.7.0

joverwey avatar Mar 27 '21 15:03 joverwey

I added a PR to fix this issue. Not sure if it is correct but it worked for me.

joverwey avatar Mar 27 '21 18:03 joverwey

This issue is a showstopper for me - would love to see this fixed!

dnola avatar Jun 09 '21 17:06 dnola