gmalivenko / onnx2keras

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

Can't gather from tf tensor #65

Open M-Tonin opened 4 years ago

M-Tonin commented 4 years ago

'''

Returns a compiled model identical to the previous one

onnx_model = onnx.load(FILE_ONNX_PATH+"wine_price.onnx") k_model_onnx = onnx_to_keras(onnx_model, ['input_3', 'input_2', 'input_1'], 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 -> input_1. DEBUG:onnx2keras:Input 1 -> input_2. DEBUG:onnx2keras:Input 2 -> input_3. DEBUG:onnx2keras:List outputs: DEBUG:onnx2keras:Output 0 -> dense_3. DEBUG:onnx2keras:Gathering weights to dictionary. DEBUG:onnx2keras:Found weight dense_3/kernel:0 with shape (2, 1). DEBUG:onnx2keras:Found weight dense_3/bias:0 with shape (1,). DEBUG:onnx2keras:Found weight dense_1/kernel:0 with shape (256, 1). DEBUG:onnx2keras:Found weight dense_1/bias:0 with shape (1,). DEBUG:onnx2keras:Found weight dense_2/kernel:0 with shape (1360, 1). DEBUG:onnx2keras:Found weight dense_2/bias:0 with shape (1,). DEBUG:onnx2keras:Found weight dense/kernel:0 with shape (12040, 256). DEBUG:onnx2keras:Found weight dense/bias:0 with shape (256,). DEBUG:onnx2keras:Found weight shape_tensor with shape (2,). DEBUG:onnx2keras:Found weight embedding/embeddings:0 with shape (12000, 8). DEBUG:onnx2keras:Found input input_3 with shape [170] DEBUG:onnx2keras:Found input input_2 with shape [40] DEBUG:onnx2keras:Found input input_1 with shape [12000] DEBUG:onnx2keras:###### DEBUG:onnx2keras:... DEBUG:onnx2keras:Converting ONNX operation DEBUG:onnx2keras:type: Reshape DEBUG:onnx2keras:node_name: embedding_reshaped 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 input_3). DEBUG:onnx2keras:Check input 1 (name shape_tensor). 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:reshape:The second argument is numpy array. DEBUG:onnx2keras:reshape:The first argument is Keras/tf layer. Apply keras.Reshape. DEBUG:onnx2keras:reshape:Target shape : DEBUG:onnx2keras:reshape:[-1] DEBUG:onnx2keras:reshape:The first argument is Keras/tf layer. Apply keras.Flatten. DEBUG:onnx2keras:###### DEBUG:onnx2keras:... DEBUG:onnx2keras:Converting ONNX operation DEBUG:onnx2keras:type: Cast DEBUG:onnx2keras:node_name: casted DEBUG:onnx2keras:node_params: {'to': 6, 'change_ordering': False, 'name_policy': 'short'} DEBUG:onnx2keras:... DEBUG:onnx2keras:Check if all inputs are available: DEBUG:onnx2keras:Check input 0 (name embedding_reshaped). DEBUG:onnx2keras:... found all, continue DEBUG:onnx2keras:###### DEBUG:onnx2keras:... DEBUG:onnx2keras:Converting ONNX operation DEBUG:onnx2keras:type: Gather DEBUG:onnx2keras:node_name: embedding/Identity:0 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 embedding/embeddings:0). DEBUG:onnx2keras:The input not found in layers / model inputs. DEBUG:onnx2keras:Found in weights, add as a numpy constant. DEBUG:onnx2keras:Check input 1 (name casted). DEBUG:onnx2keras:... found all, continue

AttributeError Traceback (most recent call last)

in () 1 onnx_model = onnx.load(FILE_ONNX_PATH+"wine_price.onnx") ----> 2 k_model_onnx = onnx_to_keras(onnx_model, ['input_3', 'input_2', 'input_1'], name_policy="short") 3 k_model_onnx.graph = tf.compat.v1.get_default_graph() 4 weights_onnx = k_model_onnx.get_weights() 1 frames /usr/local/lib/python3.6/dist-packages/onnx2keras/reshape_layers.py in convert_gather(node, params, layers, lambda_func, node_name, keras_name) 85 raise AttributeError('Can\'t gather by axis more than 3.') 86 else: ---> 87 raise AttributeError('Can\'t gather from tf tensor.') 88 89 AttributeError: Can't gather from tf tensor. ''' Some can help ?
nestyme commented 4 years ago

hi! Did you managed to deal with this issue?

akshayrajeev1503 commented 4 years ago

@nerox8664 same issue "Can't gather from tf tensor" while converting T5 onnx as well as pytorch model to keras. please help !!

TianpengBu commented 2 years ago

Hi I solved this problem by rewriting the def convert_gather. It is probably caused by receiving a tf.tensor rather than a numpy array as the input. To allow a tensor as input, you can do the following:

image

Yaosmart commented 6 months ago

@TianpengBu yours works, thx