paulgavrikov/visualkeras

[BUG] Conv1D does not work

Closed this issue · 7 comments

Describe the bug
This package does not appear to support CNNs that work on 1D images. The layered_view function does not work with 1D images (times series), which use Conv1D, MaxPooling1D, etc. layers.
This happens because the layered.py file throws an error at line 100, which in turn happens because it does not understand that, e.g., a Conv1D layer would have len(layer.output_shape)==3, which would result in the wrong code being executed. Line 100 has a statement such as max(z) which is faulty because z is a scalar and cannot be iterated over in the max() function.

Minimal Code To Reproduce

import visualkeras
import tensorflow as tf

test_model = tf.keras.models.Sequential()
test_model.add(tf.keras.layers.Conv1D(32, 3, input_shape=(28, 1)))
test_model.summary()
visualkeras.layered_view(test_model, legend=True)

Expected behavior
A picture should be shown just like any other CNN, but perhaps with only x instead of y, or just simply a normal cube as in a dense layer.

Screenshots

Model: "sequential_9"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv1d_11 (Conv1D)          (None, 26, 32)            128       
                                                                 
=================================================================
Total params: 128
Trainable params: 128
Non-trainable params: 0
_________________________________________________________________
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[28], [line 4](vscode-notebook-cell:?execution_count=28&line=4)
      [2](vscode-notebook-cell:?execution_count=28&line=2) test_model.add(tf.keras.layers.Conv1D(32, 3, input_shape=(28, 1)))
      [3](vscode-notebook-cell:?execution_count=28&line=3) test_model.summary()
----> [4](vscode-notebook-cell:?execution_count=28&line=4) visualkeras.layered_view(test_model, legend=True)

File [d:\miniconda3\envs\dl\lib\site-packages\visualkeras\layered.py:100](file:///D:/miniconda3/envs/dl/lib/site-packages/visualkeras/layered.py:100), in layered_view(model, to_file, min_z, min_xy, max_z, max_xy, scale_z, scale_xy, type_ignore, index_ignore, color_map, one_dim_orientation, background_fill, draw_volume, padding, spacing, draw_funnel, shade_step, legend, font, font_color)
     [98](file:///D:/miniconda3/envs/dl/lib/site-packages/visualkeras/layered.py:98)     x = min(max(shape[1] * scale_xy, x), max_xy)
     [99](file:///D:/miniconda3/envs/dl/lib/site-packages/visualkeras/layered.py:99)     y = min(max(shape[2] * scale_xy, y), max_xy)
--> [100](file:///D:/miniconda3/envs/dl/lib/site-packages/visualkeras/layered.py:100)     z = min(max(z), max_z)
    [101](file:///D:/miniconda3/envs/dl/lib/site-packages/visualkeras/layered.py:101) elif len(shape) == 2:
    [102](file:///D:/miniconda3/envs/dl/lib/site-packages/visualkeras/layered.py:102)     if one_dim_orientation == 'x':

TypeError: 'int' object is not iterable

Environment (please complete the following information):

  • OS: Windows 10 Home
  • Python 3.9.18
  • Module Version 0.0.2

Yes I am also having the same issue.

image
  1. First remove currently installed visualkeras library:
    pip uninstall visualkeras
    or
    !pip uninstall visualkeras for Jupyter Notebook and Colab,

  2. Again install visualkeras library using below command:
    pip install git+https://github.com/paulgavrikov/visualkeras
    or
    !pip install git+https://github.com/paulgavrikov/visualkeras for Jupyter Notebook and Colab.

I hope it can solve your problem.

I've the same issue, looks like the Conv-1D layer gives rise to it.

Screenshot from 2024-03-15 12-36-33

Screenshot from 2024-03-15 12-43-44

@amitsingha tried your trick, but it doesn't work for me.

Screenshot from 2024-03-15 12-38-46

  1. First remove currently installed visualkeras library:
    pip uninstall visualkeras
    or
    !pip uninstall visualkeras for Jupyter Notebook and Colab,
  2. Again install visualkeras library using below command:
    pip install git+https://github.com/paulgavrikov/visualkeras
    or
    !pip install git+https://github.com/paulgavrikov/visualkeras for Jupyter Notebook and Colab.

I hope it can solve your problem.

Tried your suggestion still have the same error

1.pip uninstall visualkeras
2.pip install git+https://github.com/paulgavrikov/visualkeras
and it works!

1.pip uninstall visualkeras 2.pip install git+https://github.com/paulgavrikov/visualkeras and it works!

It does work, however, it seems to visualize the number of filters on the y-axis. I.e., it appears as if the layer size (first dimension) would expand during convolution instead of shrinking.

Stale issue message