question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError: 'ConstantOfShape'

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
Vozfcommented, Mar 19, 2021

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"])
1reaction
Vozfcommented, Mar 19, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trouble Converting LSTM Pytorch Model to ONNX
I am trying to export my LSTM Anomally-Detection Pytorch model to ONNX, but I'm experiencing errors. Please take a look at my code...
Read more >
Model Optimizer issue with a custom ONNX model
Solved: Hi, I am having issue while running Model Optimizer on a custom ONNX model file exported from Matlab. This model takes a...
Read more >
PyTorch: torch/onnx/symbolic_opset11.py - Fossies
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting...
Read more >
crnn模型onnx转rknn报错- Toybrick - Powered by Discuz!
Load model failed! 查询文档得知KeyError似乎是因为有op不支持,但是后面提示是空的,不知道是哪个op报错了, ...
Read more >
Yolov5 evaluate 發生問題— Kneron Developer Forums
[piano][warning][helper_node.cc:18][EmptyNode] Warning: creating an EmptyNode instance for op_type: ConstantOfShape ... KeyError: 'images'.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found