gmalivenko / onnx2keras

Convert ONNX model graph to Keras model format.
MIT License
193 stars 114 forks source link

KeyError: 'ConstantOfShape' #47

Open gitathrun opened 4 years ago

gitathrun commented 4 years ago

Hi, I am working on the conversion of onnx to keras, until I got this error message:

...
DEBUG:onnx2keras:Check input 0 (name 73).
DEBUG:onnx2keras:... found all, continue
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-9-c9733b56662e> in <module>()
      2 
      3 # Call the converter (input - is the main model input name, can be different for your model)
----> 4 k_model = onnx_to_keras(onnx_model, input_name_list)

/usr/local/lib/python3.6/dist-packages/onnx2keras/converter.py in onnx_to_keras(onnx_model, input_names, input_shapes, name_policy, verbose, change_ordering)
    170             logger.debug('... found all, continue')
    171 
--> 172         AVAILABLE_CONVERTERS[node_type](
    173             node,
    174             node_params,

KeyError: 'ConstantOfShape'

There is no any other indications, is there any ideas about how to solve this problem?

onnx version 1.6 onnx2keras version 0.0.18

Shiro-LK commented 4 years ago

@gitathrun Hi, I have exactly the same issue, do you solve your issue ?

LucasKirsten commented 3 years ago

Any solutions?

fahim78611 commented 3 years ago

i am also facing similar issue: "constantOfShape", any resolutions?

Vozf commented 3 years ago

I fixed it with creating converter for constanOfShape ConstantOfShape creates a constant with value stored in node of shape passed as input so np.full is basically what it does ref https://github.com/onnx/onnx/blob/master/docs/Operators.md#Constant https://github.com/onnx/onnx/blob/master/docs/Operators.md#ConstantOfShape

Got other errors too, so not sure if it is working properly

    from onnx2keras import onnx_to_keras
    from onnx2keras.layers import AVAILABLE_CONVERTERS
    import numpy as np

    def convert_constant_of_shape(node, params, layers, lambda_func, node_name, keras_name):
        """
        Convert Constant layer
        :param node: current operation node
        :param params: operation attributes
        :param layers: available keras layers
        :param lambda_func: function for keras Lambda layer
        :param node_name: internal converter name
        :param keras_name: resulting layer name
        :return: None
        """
        layers[node_name] = np.full(layers[node.input[0]], params["value"])

    AVAILABLE_CONVERTERS["ConstantOfShape"] = convert_constant_of_shape

    onnx_model = onnx.load_model(onnx_path)
    onnx.checker.check_model(onnx_model)

    k_model = onnx_to_keras(onnx_model, ["input_1"])
Vozf commented 3 years ago

You can also try exporting to onnx opset_version=9. In my case it avoided creation of constantOfShape

balisujohn commented 1 year ago

I'm still working on this so I don't know if this will generally work but I had a shape of the form [none, 3] and the np.full method broke down with this. Here is a modified version that seems to work when there is a none in the shape:

 from keras import initializers 
 ...
 def convert_constant_of_shape(node, params, layers, lambda_func, node_name, keras_name):
            """            
            Convert Constant layer
            :param node_name: current operation node
            :param params: operation attributes
            :param layers: available keras layers
            :param lambda_func: function for keras Lambda layer
            :param node_name: internal converter name
            :param keras_name: resulting layer name
            :return: None
            """
            import numpy as np
            layers[node_name] = initializers.Constant(value = params["value"][0])(layers[node.input[0]])

            #layers[node_name] = np.full(layers[node.input[0]], params["value"][0])

        AVAILABLE_CONVERTERS["ConstantOfShape"] = convert_constant_of_shape

I put these modifications in converter.py in onnx2keras. (this actually isn't working yet, I was mistaken. I went a different direction that involved more targeted exports of submodules, eliminating the need for me to figure this out.)