HarisIqbal88 / PlotNeuralNet

Latex code for making neural networks diagrams
MIT License
21.93k stars 2.86k forks source link

Plot automatically from model summary (TensorFlow / Keras) #63

Open universvm opened 5 years ago

universvm commented 5 years ago

Hey there!

Great work on the project, I think this is one of the best out there.

I've been thinking about possibly automating the plotting process. Keras / Tensorflow allow to transform a model object to a dictionary. So for instance, the code:

import tensorflow as tf

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(512, activation=tf.nn.relu),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

print(model.get_config())

returns:

{'name': 'sequential', 'layers': [{'class_name': 'Flatten', 'config': {'name': 'flatten', 'trainable': True, 'batch_input_shape': (None, 28, 28), 'dtype': 'float32', 'data_format': 'channels_last'}}, {'class_name': 'Dense', 'config': {'name': 'dense', 'trainable': True, 'dtype': 'float32', 'units': 512, 'activation': 'relu', 'use_bias': True, 'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'scale': 1.0, 'mode': 'fan_avg', 'distribution': 'uniform', 'seed': None, 'dtype': 'float32'}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {'dtype': 'float32'}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}}, {'class_name': 'Dropout', 'config': {'name': 'dropout', 'trainable': True, 'dtype': 'float32', 'rate': 0.2, 'noise_shape': None, 'seed': None}}, {'class_name': 'Dense', 'config': {'name': 'dense_1', 'trainable': True, 'dtype': 'float32', 'units': 10, 'activation': 'softmax', 'use_bias': True, 'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'scale': 1.0, 'mode': 'fan_avg', 'distribution': 'uniform', 'seed': None, 'dtype': 'float32'}}, 'bias_initializer': {'class_name': 'Zeros', 'config': {'dtype': 'float32'}}, 'kernel_regularizer': None, 'bias_regularizer': None, 'activity_regularizer': None, 'kernel_constraint': None, 'bias_constraint': None}}]}

Documentation here: https://keras.io/models/about-keras-models/

I think this is fairly parsable and could avoid having to manually write the layers. I'm quite busy these months but I might be able to do it over Christmas unless anyone else takes the lead.

sarim-zafar commented 4 years ago

Any updates on this? Are you planning on supporting 1d convolutions as well?

Aspie96 commented 4 years ago

Maybe it could be integrated with Netron? See: https://github.com/lutzroeder/netron

chrismaliszewski commented 4 years ago

@universvm, I am having the same idea as you do but after a small consideration, it may be a hard task to do because of a few reasons.

  1. Everything depends on how complicated a model is. For sure I can imagine Sequential models to be done this way automatically. But something more complex may be difficult.
  2. Besides, get_config() provides not enough information for the task, i.e. it lacks layers' input sizes. The only possible way would be doing iteration over model.layers.
  3. But the main problem I can imagine would be properly displaying the data so it doesn't overlap.
  4. Finally, I think the final tool would not allow users to adjust the drawing to their preferences as much as it can be done at the moment, i.e. widths, offsets etc..

If I happen to use the project more, I will consider creating such a piece of code on my own. At the moment I have other things to do but I can help in creating the code in my spare time if someone needs help.

Cheers.

sa501428 commented 3 years ago

Recently came across this tool while exploring different model visualizations. I really like the look of the examples, but it would be amazing to just plug in a keras model directly, similar to Net2Vis, VisualKeras, etc. Any chance there's a plan to revisit this issue?