2

I am currently trying to convert the onnx model using onnx2keras. However, I am facing the error below:

AttributeError                            Traceback (most recent call last)
<ipython-input-10-8e8800d4ffef> in <module>()
      6 
      7 onnx_model = onnx.load('onnx_model.onnx')
----> 8 k_model = onnx_to_keras(onnx_model,['input_ids'])
      9 
     10 tf.keras.models.save_model(k_model,'kerasModel.h5',overwrite=True,include_optimizer=True)

/usr/local/lib/python3.7/dist-packages/onnx2keras/converter.py in onnx_to_keras(onnx_model, input_names, input_shapes, name_policy, verbose, change_ordering)
    168                     layers[node_input] = weights[node_input]
    169                 else:
--> 170                     raise AttributeError('Current node is not in weights / model inputs / layers.')
    171         else:
    172             logger.debug('... found all, continue')

AttributeError: Current node is not in weights / model inputs / layers.

Here is my code:

import onnx2keras
from onnx2keras import onnx_to_keras
import keras
import onnx
import tensorflow as tf

onnx_model = onnx.load('onnx_model.onnx')
k_model = onnx_to_keras(onnx_model,['input_ids'])

tf.keras.models.save_model(k_model,'kerasModel.h5',overwrite=True,include_optimizer=True)

I have also found the nodes from onnx model using below code:

input_all = [node.name for node in onnx_model.graph.input]
print(input_all)

This is the output of 'input_all' = ['input_ids', 'attention_mask', 'token_type_ids']

Can someone provide a solution for my problem. Thank you

FND FYP
  • 51
  • 4

0 Answers0