It is software that visualizes the structure of the neural network created by @ yu4u in a nice way. I knew it existed for a long time, but I hadn't tried it yet, so I tried it. It felt good to run it on Google Colaboratory (Google Colab), so I created a notebook that I could easily try.
Please refer to the following blog article for the explanation about Google Colab.
From here, I will introduce how to draw a neural network using "convnet-drawer" on Google Colab.
The information I referred to is the code of @ yu4u's software and the following article.
The link of the created Google Colab Notebook is as follows. convnet_drawer_on_google_colab.ipynb
If you do the above, you'll understand it, but I'll explain it briefly. After that, it is assumed that it will be executed on Google Colab.
There are two main ways to draw a neural network: 2.
--How to create a model from 0 --How to load a model made with Keras
I will explain each of them.
Clone the "convnet-drawer". I'm using the one I forked, not the original. This is because when loading a Keras model, I've customized it a bit to skip the undrawable layers (except when using a Keras model that contains non-drawable layers, the original repository is OK).
!cd /content
!git clone https://github.com/karaage0703/convnet-drawer
%cd convnet-drawer
!git checkout -b custom_keras_util origin/custom_keras_util
First, create a model from 0 and visualize it.
Import the library.
from convnet_drawer import Model, Conv2D, MaxPooling2D, Flatten, Dense
Create a model. Create a model in the same way as Keras.
However, unsupported layers such as the Activation layer and Dropout layer will cause an error, so delete or comment them out. I have commented out this time.
The model is a small model of image recognition as used in MNIST.
model = Model(input_shape=(32, 32, 3))
model.add(Conv2D(32, (3, 3), padding='same'))
# model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
# model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
# model.add(Activation('relu'))
# model.add(Dropout(0.5))
model.add(Dense(10))
# model.add(Activation('softmax'))
Let's save the model in svg format and draw it.
model.save_fig("example.svg")
from IPython.display import *
display_svg(SVG('example.svg'))
Now you can draw nicely. It's the best.
If you want to draw with Matplotlib, execute the following.
import matplotlib.pyplot as plt
from convnet_drawer import Line, Text
def plot_model(model):
model.build()
fig1 = plt.figure(figsize=(5,5),dpi=100)
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.axis('off')
plt.xlim(model.x, model.x + model.width)
plt.ylim(model.y + model.height, model.y)
for feature_map in model.feature_maps + model.layers:
for obj in feature_map.objects:
if isinstance(obj, Line):
if obj.dasharray == 1:
linestyle = ":"
elif obj.dasharray == 2:
linestyle = "--"
else:
linestyle = "-"
plt.plot([obj.x1, obj.x2], [obj.y1, obj.y2], color=[c / 255 for c in obj.color], lw=obj.width,
linestyle=linestyle)
elif isinstance(obj, Text):
ax1.text(obj.x, obj.y, obj.body, horizontalalignment="center", verticalalignment="bottom",
size=2 * obj.size / 3, color=[c / 255 for c in obj.color])
The drawing is as follows.
plot_model(model)
It was displayed. Let's adjust the size etc. by yourself.
If you want to make it look like a manga, do the following.
with plt.xkcd():
plot_model(model)
This is cute.
In the comment of Qiita article, @ wakame1367 found that "convnet-drawer" has a PR that can read Keras model, so this is also I tried.
Import the software that loads Keras (keras_util) and Keras.
import keras_util
from tensorflow.python.keras.layers.convolutional import Conv2D, MaxPooling2D
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers.core import Dense, Dropout, Activation, Flatten
Let's create a model with Keras.
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', input_shape=(32, 32, 3)))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation('softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
Convert the model with convnet-drawer and draw it.
net = keras_util.convert_drawer_model(model)
net.save_fig("sample.svg")
from IPython.display import *
display_svg(SVG('sample.svg'))
I was able to draw with the Keras model as well. The unsupported activation layer and Dropout layer will be skipped without permission.
I briefly introduced how to run "convnet-drawer" on Google Colab. I'm glad that you can visualize the small network you created in a nice way.
However, it is difficult to visualize with the latest huge neural network, and I think that you can not understand well when you visualize it. I think it's best to use it for drawing points.
Recommended Posts