TypeError: 'Lambda' object does not support item assignment
stashvala opened this issue · 6 comments
Hi, I tried to load a simple two layered ONNX model exported with pytorch:
k_model = onnx2keras.onnx_to_keras("simple_cnn.txt", ["input.1"])
but, i got the following output and error:
2021-04-19 17:53:04.793268: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/user/torch/install/lib::/usr/local/cuda:/usr/local/cuda-10.0/lib64
2021-04-19 17:53:04.793300: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
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:List outputs:
DEBUG:onnx2keras:Output 0 -> 9.
DEBUG:onnx2keras:Gathering weights to dictionary.
DEBUG:onnx2keras:Found weight conv.weight with shape (8, 5, 3).
DEBUG:onnx2keras:Found weight conv.bias with shape (8,).
DEBUG:onnx2keras:Found weight fc.weight with shape (2, 80).
DEBUG:onnx2keras:Found weight fc.bias with shape (2,).
DEBUG:onnx2keras:Found input input.1 with shape [5, 10]
DEBUG:onnx2keras:######
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Converting ONNX operation
DEBUG:onnx2keras:type: Conv
DEBUG:onnx2keras:node_name: 5
DEBUG:onnx2keras:node_params: {'dilations': [1], 'group': 1, 'kernel_shape': [3], 'pads': [1, 1], 'strides': [1], 'change_ordering': False, 'name_policy': None}
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Check if all inputs are available:
DEBUG:onnx2keras:Check input 0 (name input.1).
DEBUG:onnx2keras:Check input 1 (name conv.weight).
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 conv.bias).
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:conv:Conv with bias
3 5 8 True
Traceback (most recent call last):
File "onnx_2_keras.py", line 19, in <module>
k_model = onnx2keras.onnx_to_keras(onnx_model, input_all)
File "/home/user/.local/lib/python3.6/site-packages/onnx2keras/converter.py", line 181, in onnx_to_keras
keras_names
File "/home/user/.local/lib/python3.6/site-packages/onnx2keras/convolution_layers.py", line 197, in convert_conv
lambda_layer[keras_name] = target_layer
TypeError: 'Lambda' object does not support item assignment
I have tensorflow 2.4.1, keras 2.4.3 and no CUDA.
Edit: The problem occurs because of the use of Conv1D
At line 197 on file onnx2keras/convolution_layers.py, you change lambda_layer[keras_name] = target_layer
to lambda_func[keras_name] = target_layer
.
@KietHoang2212 It seems to work, why not writing a PR ?
@KietHoang2212, actually I think it does not solve it, it simply removes the layer.
A simple model.summary() shows no parameters
Model: "functional_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_0 (InputLayer) [(None, 10, 32)] 0
_________________________________________________________________
output_0 (Lambda) (None, 16, 32) 0
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
@londumas , actually it replaces Conv1d in PyTorch by Lambda layer with fixed weights in Keras, so it has no prams. But the result is still the same. If their results are different, you can replace the code from line 179 in the file onnx2keras/convolution_layers.py with the following code:
`
1D conv
W = W.transpose(2, 1, 0)
width, channels, n_filters = W.shape
print(width, channels, n_filters, has_bias)
# if has_bias:
# weights = [W, bias]
# else:
# weights = [W]
weights = [W]
def target_layer(x, w=weights, stride=strides[0]):
import tensorflow as tf
w = tf.convert_to_tensor(w[0])
x = tf.transpose(x, [0, 2, 1])
#Note: padding='VALID' if padding==0, else: padding='SAME
x = tf.nn.conv1d(x, w, stride=stride, padding='VALID', data_format='NWC')
if has_bias:
x = tf.nn.bias_add(x, bias, data_format='N...C')
return tf.transpose(x, [0, 2, 1])
lambda_layer = keras.layers.Lambda(target_layer, name=keras_name)
lambda_func[keras_name] = target_layer
layers[node_name] = lambda_layer(input_0)
`
@KietHoang2212, indeed the code you gave there works. Why not writing a PR?
hi, I'm having this same issue, also with a Conv1D layer originally from a PyTorch model. @KietHoang2212 if this fix works for you, any chance you could write a PR? thank you :)