[PYTHON] How to easily draw the structure of a neural network on Google Colaboratory using "convnet-drawer"

nn-draw.png

What is "convnet-drawer"?

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.

If you use Google Colaboratory, you don't need to build an environment and you can do Python machine learning for free.

How to draw the structure of a neural network

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.

Defining a convolutional neural network like Keras created a tool that saves architectural diagrams in PowerPoint

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.

Preparation

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

How to create a model from 0 and visualize it

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.

nn-draw.png

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)

matplotlib.png

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)

manga.png

This is cute.

How to load a model made with Keras (using keras-util)

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.

nn-draw.png

Summary

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

How to easily draw the structure of a neural network on Google Colaboratory using "convnet-drawer"
A command to easily check the speed of the network on the console
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
Basics of PyTorch (2) -How to make a neural network-
[NNabla] How to remove the middle tier of a pre-built network
How to draw a graph using Matplotlib
I tried how to improve the accuracy of my own Neural Network
How to copy and paste the contents of a sheet in Google Spreadsheet in JSON format (using Google Colab)
How to calculate the volatility of a brand
Visualize the inner layer of a neural network
[2015/11/19] How to register a service locally using the python SDK on naoqi os
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
How to access the contents of a Linux disk on a Mac (but read-only)
How to write a GUI using the maya command
The story of making a music generation neural network
How to analyze with Google Colaboratory using Kaggle API
Implementation of a convolutional neural network using only Numpy
Let's easily make a math gif using Google Colaboratory
I want to take a screenshot of the site on Docker using any font
[NNabla] How to add a new layer between the middle layers of a pre-built network
A memo on how to overcome the difficult problem of capturing FX with AI
[Implementation explanation] How to use the Japanese version of BERT in Google Colaboratory (PyTorch)
Run the Caffe model on Google Colaboratory to predict the age and gender of the world's supermodels
A memo on how to easily prepare a Linux exercise environment
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
[Deep learning] Investigating how to use each function of the convolutional neural network [DW day 3]
How to generate a query using the IN operator in Django
How to know the number of GPUs from python ~ Notes on using multiprocessing with pytorch ~
How to find the scaling factor of a biorthogonal wavelet
The story of creating a database using the Google Analytics API
How to connect the contents of a list into a string
How to use Google Colaboratory
How to determine the existence of a selenium element in Python
A note on how to check the connection to the license server port
How to know the internal structure of an object in Python
How to display formulas in latex when using sympy (> = 1.4) in Google Colaboratory
How easy is it to synthesize a drug on the market?
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
How to find the memory address of a Pandas dataframe value
How to output the output result of the Linux man command to a file
How to save only a part of a long video using OpenCV
How to get the vertex coordinates of a feature in ArcPy
How to use Jupyter on the front end of supercomputer ITO
[Reinforcement learning] How to draw OpenAI Gym on Google Corab (2020.6 version)
How to update the python version of Cloud Shell on GCP
Understand the number of input / output parameters of a convolutional neural network
Implementation of a two-layer neural network 2
Touch the object of the neural network
How to divide and process a data frame using the groupby function
How to find out the number of CPUs without using the sar command
I made a note of Google colaboratory which can use Spleeter easily.
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Semi-automatically generate a description of the package to be registered on PyPI
[NNabla] How to add a quantization layer to the middle layer of a trained model
How to put a line number at the beginning of a CSV file
How to draw a vertical line on a heatmap drawn with Python seaborn
How to fix the initial population with a genetic algorithm using DEAP
How to create a wrapper that preserves the signature of the function to wrap
Verification of how to periodically execute a script on a Linux server on Windows