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.

TypeError: 'Lambda' object does not support item assignment

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
KietHoang2212commented, Jul 17, 2021

@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)

`

1reaction
KietHoang2212commented, Jun 18, 2021

At line 197 on file onnx2keras/convolution_layers.py, you change lambda_layer[keras_name] = target_layer to lambda_func[keras_name] = target_layer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lambda error "TypeError: 'str' object does not support item ...
Based on the posted form of the event , you should use ast , not json import ast item = ast.literal_eval(event['body']).
Read more >
TypeError: 'type' object does not support item assignment
The Python "TypeError: 'type' object does not support item assignment" occurs when we assign a data type to a variable and use square...
Read more >
'str' Object Does Not Support Item Assignment - Python Pool
When you alter characters using the assignment operator, you will receive TypeError:'str' Object Does Not Support Item Assignment.
Read more >
'str' object does not support item assignment Traceback - Higgs
Lambda error "TypeError: 'str' object does not support item assignment Traceback". Detection score: 1. Current body; Last body; Edit summary.
Read more >
Python 'str' object does not support item assignment solution
Let's start by taking a look at our error: Typeerror: 'str' object does not support item assignment. This error message tells us that...
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