paulgavrikov / visualkeras

Visualkeras is a Python package to help visualize Keras (either standalone or included in TensorFlow) neural network architectures. It allows easy styling to fit most needs. This module supports layered style architecture generation which is great for CNNs (Convolutional Neural Networks), and a graph style architecture, which works great for most models including plain feed-forward networks.
MIT License
582 stars 67 forks source link

[BUG]AttributeError: 'Sequential' object has no attribute '_layers' #14

Closed JuggernautPanda closed 2 years ago

JuggernautPanda commented 3 years ago

import visualkeras model.build() visualkeras.layered_view(model, legend=True, to_file='output.png').show() # write and show visualkeras.graph_view(model,show_neurons=True, to_file='graph.png').show()

AttributeError Traceback (most recent call last)

in () 2 model.build() 3 visualkeras.layered_view(model, legend=True, to_file='output.png').show() # write and show ----> 4 visualkeras.graph_view(model,show_neurons=True, to_file='graph.png').show() 1 frames /usr/local/lib/python3.7/dist-packages/visualkeras/layer_utils.py in model_to_adj_matrix(model) 37 if not model.built: 38 model.build() ---> 39 layers = model._layers 40 41 adj_matrix = np.zeros((len(layers), len(layers))) AttributeError: 'Sequential' object has no attribute '_layers'
JuggernautPanda commented 3 years ago

I tried the fix by changing the _layers to layers.

I get a new issue:

/usr/local/lib/python3.7/dist-packages/visualkeras/layer_utils.py in model_to_adj_matrix(model) 55 src = id_to_num_mapping[inbound_layer_id] 56 tgt = id_to_num_mapping[layer_id] ---> 57 adj_matrix[src, tgt] += 1 58 59 return id_to_num_mapping, adj_matrix

IndexError: index 12 is out of bounds for axis 1 with size 12

github-actions[bot] commented 3 years ago

Stale issue message

paulgavrikov commented 3 years ago

What tensorflow version are you using? Sounds like an older one. _layers contains layers that tensorflow implicitly adds, so I'd assume that when you forgot to change it somewhere.

alessiogmonti commented 3 years ago

What tensorflow version are you using? Sounds like an older one. _layers contains layers that tensorflow implicitly adds, so I'd assume that when you forgot to change it somewhere.

Hey- I'm having the same 'Sequential' object has no attribute '_layers' issue. I'm using tensorflow 2.7. Any idea what it might be?

Changing _layers to layers returns a similar error:

/usr/local/lib/python3.7/dist-packages/visualkeras/layer_utils.py in model_to_adj_matrix(model)
     55             src = id_to_num_mapping[inbound_layer_id]
     56             tgt = id_to_num_mapping[layer_id]
---> 57             adj_matrix[src, tgt] += 1
     58 
     59     return id_to_num_mapping, adj_matrix

IndexError: index 4 is out of bounds for axis 1 with size 4

This is a great library- looks amazing, but it would be a shame if the graph view doesn't work- I'm not a very experienced programmer but if there is any way I can help please let me know.

paulgavrikov commented 3 years ago

@alessiogmonti I will take a look at this, but due to the upcoming CVPR deadline it will take a while. It looks like the _layers attribute has been abandoned and seems to have been replaced by _self_tracked_trackables in recent tensorflow versions. Could you try changing it? Also, does the bug only affect graph_view or also layered_view?

alessiogmonti commented 3 years ago

@paulgavrikov replacing _layers with _self_tracked_trackables works great- thank you! It runs just fine now. This bug only affects graph_view.

paulgavrikov commented 2 years ago

@alessiogmonti awesome. Would you like to try and create a PR that fixes this? You could check if the passed model has the _layers arg and fallback to the other solution if not. Ideally, we’d also integrate a test into Travis CI

alessiogmonti commented 2 years ago

@paulgavrikov Absolutely- I'll do this today.

jainakhil88 commented 2 years ago

@alessiogmonti awesome. Would you like to try and create a PR that fixes this? You could check if the passed model has the _layers arg and fallback to the other solution if not. Ideally, we’d also integrate a test into Travis CI

Any updates on this, is it fixed? I am getting below error image

alessiogmonti commented 2 years ago

Hey, for now you can use this. It will try to use _layers and fall back to _self_tracked_trackables when necessary: !pip install git+https://github.com/alessiogmonti/visualkeras

paulgavrikov commented 2 years ago

The PR has been merged into the master branch. @jainakhil88 Your issue is related to something else. It looks like you are not passing a valid Keras model to visualkeras. Are you sure MaskRCNN is not wrapping the actual model?

github-actions[bot] commented 2 years ago

Stale issue message