gmalivenko / onnx2keras

Convert ONNX model graph to Keras model format.
MIT License
195 stars 115 forks source link

Input layer raise Current node is not in weights / model inputs / layers. #70

Open M-Tonin opened 4 years ago

M-Tonin commented 4 years ago

Hey, doing the basic step, to LSTM model. ``

Returns a compiled model identical to the previous one

onnx_model = onnx.load("stock_price.onnx") k_model_onnx = onnx_to_keras(onnx_model, 'lstm_input', name_policy="short") k_model_onnx.graph = tf.compat.v1.get_default_graph() weights_onnx = k_model_onnx.get_weights()

``


`` INFO:onnx2keras:Converter is called. DEBUG:onnx2keras:List input shapes: DEBUG:onnx2keras:None DEBUG:onnx2keras:List inputs: DEBUG:onnx2keras:Input 0 -> lstm_input. DEBUG:onnx2keras:List outputs: DEBUG:onnx2keras:Output 0 -> dense. DEBUG:onnx2keras:Gathering weights to dictionary. DEBUG:onnx2keras:Found weight dense/kernel:0 with shape (256, 1). DEBUG:onnx2keras:Found weight dense/bias:0 with shape (1,). DEBUG:onnx2keras:Found weight W with shape (1, 1024, 256). DEBUG:onnx2keras:Found weight R with shape (1, 1024, 256). DEBUG:onnx2keras:Found weight B with shape (1, 2048). DEBUG:onnx2keras:Found weight shape_tensor with shape (2,). DEBUG:onnx2keras:Found weight W1 with shape (1, 1024, 256). DEBUG:onnx2keras:Found weight R1 with shape (1, 1024, 256). DEBUG:onnx2keras:Found weight B1 with shape (1, 2048). DEBUG:onnx2keras:Found weight dropout/Identity:0_seq_len_tensor_start with shape (1,). DEBUG:onnx2keras:Found weight dropout/Identity:0_seq_len_tensor_end with shape (1,). DEBUG:onnx2keras:Found weight dropout/Identity:0_seq_len_tensor_axes with shape (1,). DEBUG:onnx2keras:Found weight dropout/Identity:0_output_seq_shape_a with shape (1,). DEBUG:onnx2keras:Found weight dropout/Identity:0_output_seq_shape_b with shape (1,). DEBUG:onnx2keras:Found weight W2 with shape (1, 1024, 70). DEBUG:onnx2keras:Found weight R2 with shape (1, 1024, 256). DEBUG:onnx2keras:Found weight B2 with shape (1, 2048). DEBUG:onnx2keras:Found weight lstm_input:01_seq_len_tensor_start with shape (1,). DEBUG:onnx2keras:Found weight lstm_input:01_seq_len_tensor_end with shape (1,). DEBUG:onnx2keras:Found weight lstm_input:01_seq_len_tensor_axes with shape (1,). DEBUG:onnx2keras:Found weight lstm_input:01_output_seq_shape_a with shape (1,). DEBUG:onnx2keras:Found weight lstm_input:01_output_seq_shape_b with shape (1,). DEBUG:onnx2keras:###### DEBUG:onnx2keras:... DEBUG:onnx2keras:Converting ONNX operation DEBUG:onnx2keras:type: Identity DEBUG:onnx2keras:node_name: lstm_input:01 DEBUG:onnx2keras:node_params: {'change_ordering': False, 'name_policy': 'short'} DEBUG:onnx2keras:... DEBUG:onnx2keras:Check if all inputs are available: DEBUG:onnx2keras:Check input 0 (name lstm_input). DEBUG:onnx2keras:The input not found in layers / model inputs.

AttributeError Traceback (most recent call last)

in () 1 # Returns a compiled model identical to the previous one 2 onnx_model = onnx.load(FILE_ONNX_PATH+ FILENAME_ONNX+".onnx") ----> 3 k_model_onnx = onnx_to_keras(onnx_model, 'lstm_input', name_policy="short") 4 k_model_onnx.graph = tf.compat.v1.get_default_graph() 5 weights_onnx = k_model_onnx.get_weights() /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) 168 logger.debug('Found in weights, add as a numpy constant.') 169 layers[node_input] = weights[node_input] --> 170 else: 171 logger.debug(node_input) AttributeError: Current node is not in weights / model inputs / layers. `` It does not make sense because the first input is 'lstm_input'
M-Tonin commented 4 years ago

The error was made by : k_model_onnx = onnx_to_keras(onnx_model, 'lstm_input', name_policy="short") I forget to put "[lstm_input]" The real error is:

INFO:onnx2keras:Converter is called. DEBUG:onnx2keras:List input shapes: DEBUG:onnx2keras:None DEBUG:onnx2keras:List inputs: DEBUG:onnx2keras:Input 0 -> lstm_input. DEBUG:onnx2keras:List outputs: DEBUG:onnx2keras:Output 0 -> dense. DEBUG:onnx2keras:Gathering weights to dictionary. DEBUG:onnx2keras:Found weight dense/kernel:0 with shape (256, 1). DEBUG:onnx2keras:Found weight dense/bias:0 with shape (1,). DEBUG:onnx2keras:Found weight W with shape (1, 1024, 256). DEBUG:onnx2keras:Found weight R with shape (1, 1024, 256). DEBUG:onnx2keras:Found weight B with shape (1, 2048). DEBUG:onnx2keras:Found weight shape_tensor with shape (2,). DEBUG:onnx2keras:Found weight W1 with shape (1, 1024, 256). DEBUG:onnx2keras:Found weight R1 with shape (1, 1024, 256). DEBUG:onnx2keras:Found weight B1 with shape (1, 2048). DEBUG:onnx2keras:Found weight dropout/Identity:0_seq_len_tensor_start with shape (1,). DEBUG:onnx2keras:Found weight dropout/Identity:0_seq_len_tensor_end with shape (1,). DEBUG:onnx2keras:Found weight dropout/Identity:0_seq_len_tensor_axes with shape (1,). DEBUG:onnx2keras:Found weight dropout/Identity:0_output_seq_shape_a with shape (1,). DEBUG:onnx2keras:Found weight dropout/Identity:0_output_seq_shape_b with shape (1,). DEBUG:onnx2keras:Found weight W2 with shape (1, 1024, 70). DEBUG:onnx2keras:Found weight R2 with shape (1, 1024, 256). DEBUG:onnx2keras:Found weight B2 with shape (1, 2048). DEBUG:onnx2keras:Found weight lstm_input:01_seq_len_tensor_start with shape (1,). DEBUG:onnx2keras:Found weight lstm_input:01_seq_len_tensor_end with shape (1,). DEBUG:onnx2keras:Found weight lstm_input:01_seq_len_tensor_axes with shape (1,). DEBUG:onnx2keras:Found weight lstm_input:01_output_seq_shape_a with shape (1,). DEBUG:onnx2keras:Found weight lstm_input:01_output_seq_shape_b with shape (1,). DEBUG:onnx2keras:Found input lstm_input with shape [0, 70] DEBUG:onnx2keras:###### DEBUG:onnx2keras:... DEBUG:onnx2keras:Converting ONNX operation DEBUG:onnx2keras:type: Identity DEBUG:onnx2keras:node_name: lstm_input:01 DEBUG:onnx2keras:node_params: {'change_ordering': False, 'name_policy': 'short'} DEBUG:onnx2keras:... DEBUG:onnx2keras:Check if all inputs are available: DEBUG:onnx2keras:Check input 0 (name lstm_input). DEBUG:onnx2keras:... found all, continue DEBUG:onnx2keras:###### DEBUG:onnx2keras:... DEBUG:onnx2keras:Converting ONNX operation DEBUG:onnx2keras:type: Shape DEBUG:onnx2keras:node_name: lstm_input:01_input_shape_tensor0 DEBUG:onnx2keras:node_params: {'change_ordering': False, 'name_policy': 'short'} DEBUG:onnx2keras:... DEBUG:onnx2keras:Check if all inputs are available: DEBUG:onnx2keras:Check input 0 (name lstm_input:01). DEBUG:onnx2keras:... found all, continue DEBUG:onnx2keras:shape:Actual shape: DEBUG:onnx2keras:shape:[None 0 70] DEBUG:onnx2keras:###### DEBUG:onnx2keras:... DEBUG:onnx2keras:Converting ONNX operation DEBUG:onnx2keras:type: Slice DEBUG:onnx2keras:node_name: lstm_input:01_seq_len_tensor0 DEBUG:onnx2keras:node_params: {'change_ordering': False, 'name_policy': 'short'} DEBUG:onnx2keras:... DEBUG:onnx2keras:Check if all inputs are available: DEBUG:onnx2keras:Check input 0 (name lstm_input:01_input_shape_tensor0). DEBUG:onnx2keras:Check input 1 (name lstm_input:01_seq_len_tensor_start). DEBUG:onnx2keras:The input not found in layers / model inputs. DEBUG:onnx2keras:Found in weights, add as a numpy constant. DEBUG:onnx2keras:Check input 2 (name lstm_input:01_seq_len_tensor_end). DEBUG:onnx2keras:The input not found in layers / model inputs. DEBUG:onnx2keras:Found in weights, add as a numpy constant. DEBUG:onnx2keras:Check input 3 (name lstm_input:01_seq_len_tensor_axes). DEBUG:onnx2keras:The input not found in layers / model inputs. DEBUG:onnx2keras:Found in weights, add as a numpy constant. DEBUG:onnx2keras:... found all, continue DEBUG:onnx2keras:slice:Slice numpy constants

AttributeError Traceback (most recent call last)

in () 1 # Returns a compiled model identical to the previous one 2 onnx_model = onnx.load(FILE_ONNX_PATH+ FILENAME_ONNX+".onnx") ----> 3 k_model_onnx = onnx_to_keras(onnx_model, ['lstm_input'], name_policy="short") 4 k_model_onnx.graph = tf.compat.v1.get_default_graph() 5 weights_onnx = k_model_onnx.get_weights() 1 frames /usr/local/lib/python3.6/dist-packages/onnx2keras/reshape_layers.py in convert_slice(node, params, layers, lambda_func, node_name, keras_name) 292 starts = params["starts"][0] 293 else: --> 294 raise AttributeError('Not implemented') 295 296 if axes == 0: AttributeError: Not implemented
vipulbjj commented 4 years ago

Getting the same error. Were you able to solve it?

M-Tonin commented 4 years ago

Not yet, I used onnxruntime to this model

gmalivenko commented 4 years ago

Hello @M-Tonin @vipulbjj .

Unfortunately, recurrent layers are not supported now. ONNX produces a bunch of low-level operations, and something doesn't match somewhere in the middle. I don't want to write ad-hoc for LSTM / GRU / etc, because in the end it will not work optimal using TF / Keras anyway (TF/Keras have high-level layers for RNNs).

I know it's necessary features, but it can't be solved without new ONNX / any-other-good-way-to-export-graph (if you use PyTorch, for example).

cyrusParvereshi commented 3 years ago

Hi @nerox8664 is there some way we can use an older version of Keras/Onnx so that this will work? If not do you know how we can resolve this? It's still an important issue that needs to be resolved, especially for users that need to convert a MATLAB model into Python TensorFlow/Keras. Also, in your last sentence you mention that: "it can't be solved without new ONNX / any-other-good-way-to-export-graph (if you use PyTorch, for example". Does this mean that if I used pytorch2keras or some kind of PyTorch function then it would still work? Thank you!