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

Added legend #3

Closed alejandroloda closed 3 years ago

alejandroloda commented 3 years ago

Hi, I added a legend for the layered_view and some new colors. image

alejandroloda commented 3 years ago

Looks like Travis can't open the font, but at least on my machine works, check if on yours you can run it, just create a simple model and: visualkeras.layered_view(model, legend=True) I mean, maybe I made something to allow it on the past, but I'm pretty sure that with font = ImageFont.truetype("arial.ttf", font_size) that comes from PIL should it works everywhere.

paulgavrikov commented 3 years ago

A quick google search tells me that you have to provide the full path to the font under Linux. And I believe I had similar issues when I tried to integrate fonts into visualkeras. I'd really want this module to be platform independant. So, a simple solution would be to not provide the font parameter at all and let PIL use the default or to add another parameter to layered_view to pass an ImageFont. Would you be able to integrate those changes?

alejandroloda commented 3 years ago

This change should works In case of not valid path for the font, the PIL default font will be load, but this one for now cannot be size change image

paulgavrikov commented 3 years ago

I have reverted the merge, since this code breaks examples/generate_examples.py with legend=True:

Traceback (most recent call last):
  File "/home/paulgavrikov/Coding/git/visualkeras/examples/generate_examples.py", line 2, in <module>
    from examples import vgg16
  File "/home/paulgavrikov/Coding/git/visualkeras/examples/vgg16.py", line 75, in <module>
    visualkeras.layered_view(model, to_file='../figures/vgg16_color_map.png',
  File "/home/paulgavrikov/Coding/git/visualkeras/visualkeras/layered.py", line 208, in layered_view
    draw_text.text((box.x2 + 10, box.y1 - (font_size / 2)), layer, font=font, fill='black')
  File "/home/paulgavrikov/.local/lib/python3.8/site-packages/PIL/ImageDraw.py", line 289, in text
    if self._multiline_check(text):
  File "/home/paulgavrikov/.local/lib/python3.8/site-packages/PIL/ImageDraw.py", line 264, in _multiline_check
    return split_character in text
TypeError: argument of type 'type' is not iterable

Process finished with exit code 1
paulgavrikov commented 3 years ago

I also finally had some time to take a closer look. There are many hardcoded values that we have parameters for. Is your PR editable? If so I could push some changes later.

alejandroloda commented 3 years ago

Sorry I'm pretty new with PR, so I'm not sure if it is or isn't editable, but I see on your commit that you implemented my changes with the params that you mentioned

I made a try, and looks like its works, but I miss to have more colors by default, if you consider it, these are what I used ["#ffd166", "#ef476f", "#f95c3a", "#f456d4", "#06d6a0", "#35d049", "#118ab2", "#073b4c"]

But it's nice to have the legend working :)

alejandroloda commented 3 years ago

I see too that you could just equal cube_size to text_height, to avoid big cubes with the default font (that I estimated that is an 8 size) (lines 184-186)

Before: image

After: image

Also I see that due to use a set for layers_names, the order is loosen, so you could use a list instead, and on line 191 do:

seen = set()
layer_names = [x for x in layer_names if not (x in seen or seen.add(x))]

image

In this case don't forget to upgrade lines 51 (list for set) and 68 (append for add) to keep the compatibility

paulgavrikov commented 3 years ago

There should be a checkbox when creating the PR to set it editable, but I am no expert either. Your changes are pretty much in the master. I'll change cube_size to text_height and I'll fix the legend order (with a slightly different trick) - thanks for the heads up! The proposed colors however don't really look nice on the VGG16 test (under /examples) and honestly, I don't have the time to play around with them for too long. I believe I got the original colors from a generator that attempted to maximize the distinguishability but I can no longer find the website. Anyways, I agree we should expand the number of colors in the wheel.

alejandroloda commented 3 years ago

Pretty nice :) I will be waiting for the pip