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] Text in legend is cut off #75

Closed mlin26 closed 3 months ago

mlin26 commented 3 months ago

Hi,

I am trying to run the code. But it seems that the text in the image cannot display correctly. There is some overlap. Can you tell me how to display correctly? Thanks.

I am using:

model = Sequential([
    Conv2D(8, (3, 3), activation='relu', input_shape=(64, 64, 3)),
    MaxPooling2D((2, 2)),
    Conv2D(16, (3, 3), activation='relu'),
    MaxPooling2D((2, 2)),
    Flatten(),
    Dense(10, activation='softmax')  # 20 classes for A1, A2, ..., E4
])
visualkeras.layered_view(model, legend=True).show() # selected font

image

Soontosh commented 3 months ago

Hello, thank you for using visualkeras! I'll make a fix in 2-3 hours and let you know.

Soontosh commented 3 months ago

Seems that the last two letters of text are always cut off, no matter the font or font size. I've applied a fix for this in PR #76. I'll apply a cleaner fix once I figure out the root cause. Try installing from source using the command below:

pip install git+https://github.com/paulgavrikov/visualkeras

and run your code again. Let me know if this works for you.

mlin26 commented 3 months ago

Seems that the last two letters of text are always cut off, no matter the font or font size. I've applied a fix for this in PR #76. I'll apply a cleaner fix once I figure out the root cause. Try installing from source using the command below:

pip install git+https://github.com/paulgavrikov/visualkeras

and run your code again. Let me know if this works for you.

Thanks for your quick reply. Please see the image after using your new code. It seems that in this case it is not totally fixed. The last letter is cut off. Thanks for your help! image

Soontosh commented 3 months ago

Hey, I'm still unable to figure out the root cause of this issue. I'll open up another issue in the Pillow repository, but in the meantime, I'll add an argument which lets you specify an offset for the width of the text, which should prevent the text from being cut off. I'll let you know when I get around to this.

samuelthomaz7 commented 3 months ago

image Same here, even after installing the way you suggested @Soontosh Thanks for the help :)

Soontosh commented 3 months ago

@samuelthomaz7 @mlin26 Just pushed a temporary fix to this bug. You can use the legend_text_spacing_offset argument in layered_view to add an offset that increases the width of the space allocated for the text. This stops your legend text from being cut off, if you use the right value (default is 15).

@mlin26 For your use case I found a value of 10 to work well. Example usage:

visualkeras.layered_view(model, legend=True, legend_text_spacing_offset=10).show()

Could you try reinstalling from source and see how your code works now:

pip install git+https://github.com/paulgavrikov/visualkeras

I will keep this issue open for now and update you when I find a full fix (which addresses the root problem).

samuelthomaz7 commented 3 months ago

Thank you so much, it worked :) @Soontosh

mlin26 commented 3 months ago

@Soontosh Thanks for your update. I am not sure why I got this error for the new argument after pip install and using the code you provided:

layered_view() got an unexpected keyword argument 'legend_text_spacing_offset'

Soontosh commented 3 months ago

@Soontosh Thanks for your update. I am not sure why I got this error for the new argument after pip install and using the code you provided:

layered_view() got an unexpected keyword argument 'legend_text_spacing_offset'

Could you run the following command and try again?

pip install git+https://github.com/paulgavrikov/visualkeras
Soontosh commented 3 months ago

Pushed to latest Pypi version (0.1.2)

Soontosh commented 3 months ago

Found root problem and applied a fix which will be a part of version 1.0.0 - this will be an incompatible API change (in regards to version 0.1.2). Closing this issue!