[PYTHON] Draw Deconvolution 2D learning progress on jupyter (Bokeh)

Introduction

When I was looking for something that could display images of deep learning learning progress in real time on jupyter, I found a library called Bokeh, so I tried it. I tried it.

content of study

I used Chainer to train a single layer of Deconvolution 2D (like a simple filter). For more information on Deconvolution, please refer to Introduced here. In the gif below, we are learning to change the point ⇒ spherical pattern.

test2.gif

With Bokeh, you can scale the image with the mouse wheel, which is convenient for checking learning.

Source

In the previous Chainer, it was necessary to convert numpy to Variable once, but now in Ver 1.17, it seems that it will automatically convert to Variable. For the display of Bokeh on jupyter hereなど参考にしています。

import chainer.links as L
import chainer.functions as F
from chainer import Variable, optimizers
import numpy as np
import math
import time

#Create one spherical pattern (Gauss)
def make_one_core():            
    max_xy=15    
    sig=5.0
    sig2=sig*sig
    c_xy=7
    core=np.zeros((max_xy, max_xy), dtype= np.float32)
    for px in range(0, max_xy):
        for py in range(0, max_xy):
            r2=(px-c_xy)*(px-c_xy)+(py-c_xy)*(py-c_xy)
            core[py][px]=math.exp(-r2/sig2)*1
    return core.reshape((1, 1, core.shape[0], core.shape[1]))

#Create point and spherical images
def get_image(N=1, img_w=128, img_h=128):
    
    #Randomly 0.Make a 1% point
    img_p = np.random.randint(0, 10000, size = N*img_w*img_h)
    img_p[img_p < 9990]=0
    img_p[img_p >= 9990]=255

    img_p = img_p.reshape((N,1,img_h, img_w)).astype(np.float32)
    
    decon_core = L.Deconvolution2D(1, 1, 15, stride=1, pad=7)
    #Apply a spherical pattern to W
    decon_core.W.data = make_one_core()
    
    #Convert from point to sphere
    img_core = decon_core(img_p)#OK without conversion to Variable
    
    return img_p, img_core.data    


#Initial drawing
from bokeh.plotting import figure
from bokeh.io import push_notebook, show, output_notebook
from bokeh.layouts import gridplot

output_notebook()

palette_256 = ['#%02x%02x%02x' %(i,i,i) for i in range(256)] #For black and white display in 256 steps

img_p, img_core = get_image()#Get point and spherical images

img_h = img_p.shape[2]
img_w = img_p.shape[3]

plt1 = figure(title = 'epoch = --', x_range=[0, img_w], y_range=[0, img_h])
rend1 = plt1.image(image=[img_p[0][0]],x=[0], y=[0], dw=[img_w], dh=[img_h], palette=palette_256)

plt2 = figure(title = 'loss  = 0', x_range=plt1.x_range, y_range=plt1.y_range)
rend2 = plt2.image(image=[img_core[0][0]],x=[0], y=[0], dw=[img_w], dh=[img_h], palette=palette_256)

plts = gridplot([[plt1,plt2]], plot_width=300, plot_height=300)
handle = show(plts, notebook_handle=True)

#Model optimizer settings
model =  L.Deconvolution2D(1, 1, 15, stride=1, pad=7)#1 layer Deconvolution
optimizer = optimizers.SGD(lr=0.001)#It diverges when it is large
optimizer.setup(model)
 
#Calculation
for epoch in range(0,31):    
    
    #Calculate and update loss through 1-layer Deconvolution
    model.cleargrads()
    img_y = model(img_p)
    loss = F.mean_squared_error(img_y, img_core)
    loss.backward()
    optimizer.update()
    
    #Set image / loss data
    rend1.data_source.data['image'] = [img_p[0][0]]
    rend2.data_source.data['image'] = [img_y.data[0][0]]
    plt1.title.text='epoch = '+str(epoch)
    plt2.title.text='loss  = '+str(loss.data)
    push_notebook(handle = handle)#Update display
    time.sleep(0.5)

Recommended Posts

Draw Deconvolution 2D learning progress on jupyter (Bokeh)
Golang on jupyter
Jupyter on AWS
How to view progress bar on Jupyter Notebook to see progress