In [1]:
# Copyright (C) 2017 Zhixian MA <zxma_sjtu@qq.com>

# Do FIRST AGN feature learning by our code agn-ae

In [2]:
import numpy as np
import matplotlib
matplotlib.use('Agg') # Change matplotlib backend, in case we have no X server running..
import matplotlib.pyplot as plt
%matplotlib inline
from IPython.display import Image as IPImage
from PIL import Image

import sys   
sys.setrecursionlimit(1000000)

In [3]:
from ConvAE import ConvAE
import utils


Using gpu device 0: GeForce GT 730 (CNMeM is disabled, cuDNN 5105)

In [4]:
from nolearn.lasagne.visualize import plot_conv_weights
from nolearn.lasagne import PrintLayerInfo

In [5]:
# load data
import pickle

fname = './sample_raw_50.pkl'
fp = open(fname, 'rb')
datadict = pickle.load(fp)
X = datadict['data']
fp.close()
"""
folder = '../img_cmap_a_box_200x200/'
ftype = 'jpeg'
savepath = 'sample_raw_50.pkl'
crop_box = (150,150)
res_box = (50,50)
X = utils.gen_sample(folder=folder, ftype=ftype,
                    savepath=savepath, crop_box=crop_box,
                    res_box=res_box)
"""


Out[5]:
"\nfolder = '../img_cmap_a_box_200x200/'\nftype = 'jpeg'\nsavepath = 'sample_raw_50.pkl'\ncrop_box = (150,150)\nres_box = (50,50)\nX = utils.gen_sample(folder=folder, ftype=ftype,\n                    savepath=savepath, crop_box=crop_box,\n                    res_box=res_box)\n"

In [6]:
X.shape


Out[6]:
(18285, 2500)

In [7]:
X = X.astype('float32')
np.isnan(X).sum()
X[np.isnan(X)] = 0
# X_max = X.max(), print(X_max)
# X_min = X.min(), print(X_min)
# X_norm = (X - X_min) / (X_max - X_min)

In [8]:
X_norm = (X - X.min()) / (X.max() - X.min())
X_norm.max()


Out[8]:
1.0

In [9]:
X_norm = X_norm[0:15000,:]
X_norm.shape


Out[9]:
(15000, 2500)

In [10]:
# define the net
# idx = np.random.permutation(X.shape[0])
# X = X_train[idx[0:1000],:]
rs = 50
X_in = X_norm.reshape(-1,1,rs,rs)
X_out = X_norm
kernel_size = [3, 5, 3]
kernel_num = [32, 32, 16]
pool_flag = [True, True, True]
fc_nodes = [128]
encode_nodes = 16
net = ConvAE(X_in=X_in, X_out=X_out, kernel_size=kernel_size, pool_flag=pool_flag,
             kernel_num=kernel_num, fc_nodes=fc_nodes, encode_nodes = encode_nodes)

In [11]:
I = X_in[30,0,:,:].reshape(rs,rs)
# plt.imshow(I)
I.max()
I.min()


Out[11]:
0.018415686

In [12]:
plt.imshow(I)


Out[12]:
<matplotlib.image.AxesImage at 0x7f4ea9da45f8>

In [13]:
# generate layers
net.gen_layers()
net.layers


Out[13]:
[(lasagne.layers.input.InputLayer, {'shape': (None, 1, 50, 50)}),
 (lasagne.layers.conv.Conv2DLayer,
  {'filter_size': 3, 'num_filters': 32, 'pad': 'valid'}),
 (lasagne.layers.pool.MaxPool2DLayer, {'pool_size': 2}),
 (lasagne.layers.conv.Conv2DLayer,
  {'filter_size': 5, 'num_filters': 32, 'pad': 'valid'}),
 (lasagne.layers.pool.MaxPool2DLayer, {'pool_size': 2}),
 (lasagne.layers.conv.Conv2DLayer,
  {'filter_size': 3, 'num_filters': 16, 'pad': 'valid'}),
 (lasagne.layers.pool.MaxPool2DLayer, {'pool_size': 2}),
 (lasagne.layers.shape.ReshapeLayer, {'shape': ([0], -1)}),
 (lasagne.layers.dense.DenseLayer, {'num_units': 128}),
 (lasagne.layers.dense.DenseLayer, {'name': 'encode', 'num_units': 16}),
 (lasagne.layers.dense.DenseLayer, {'num_units': 128}),
 (lasagne.layers.dense.DenseLayer, {'num_units': 256}),
 (lasagne.layers.shape.ReshapeLayer, {'shape': ([0], 16, 4, 4)}),
 (lasagne.layers.pool.Upscale2DLayer, {'scale_factor': 2}),
 (lasagne.layers.conv.Conv2DLayer,
  {'filter_size': 3, 'num_filters': 16, 'pad': 'full'}),
 (lasagne.layers.pool.Upscale2DLayer, {'scale_factor': 2}),
 (lasagne.layers.conv.Conv2DLayer,
  {'filter_size': 5, 'num_filters': 32, 'pad': 'full'}),
 (lasagne.layers.pool.Upscale2DLayer, {'scale_factor': 2}),
 (lasagne.layers.conv.Conv2DLayer,
  {'filter_size': 3, 'num_filters': 1, 'pad': 'full'}),
 (lasagne.layers.shape.ReshapeLayer, {'shape': ([0], -1)})]

In [14]:
# Build the network and initilization
net.cae_build(learning_rate=0.01, max_epochs=50, batch_size=100, momentum=0.95, verbose=2)

In [15]:
# Train the network
net.cae_train()


Training the network...
# Neural Network with 116177 learnable parameters

## Layer information

name         size        total    cap.Y    cap.X    cov.Y    cov.X
-----------  --------  -------  -------  -------  -------  -------
input0       1x50x50      2500   100.00   100.00   100.00   100.00
conv2d1      32x48x48    73728   100.00   100.00     6.00     6.00
maxpool2d2   32x24x24    18432   100.00   100.00     6.00     6.00
conv2d3      32x20x20    12800    90.91    90.91    22.00    22.00
maxpool2d4   32x10x10     3200    90.91    90.91    22.00    22.00
conv2d5      16x8x8       1024    63.16    63.16    38.00    38.00
maxpool2d6   16x4x4        256    63.16    63.16    38.00    38.00
reshape7     256           256   100.00   100.00   100.00   100.00
dense8       128           128   100.00   100.00   100.00   100.00
encode       16             16   100.00   100.00   100.00   100.00
dense10      128           128   100.00   100.00   100.00   100.00
dense11      256           256   100.00   100.00   100.00   100.00
reshape12    16x4x4        256   100.00   100.00   100.00   100.00
upscale2d13  16x8x8       1024   100.00   100.00   100.00   100.00
conv2d14     16x10x10     1600   100.00   100.00   100.00   100.00
upscale2d15  16x20x20     6400   100.00   100.00   100.00   100.00
conv2d16     32x24x24    18432   100.00   100.00   100.00   100.00
upscale2d17  32x48x48    73728   100.00   100.00   100.00   100.00
conv2d18     1x50x50      2500   100.00   100.00   100.00   100.00
reshape19    2500         2500   100.00   100.00   100.00   100.00

Explanation
    X, Y:    image dimensions
    cap.:    learning capacity
    cov.:    coverage of image
    magenta: capacity too low (<1/6)
    cyan:    image coverage too high (>100%)
    red:     capacity too low and coverage too high


  epoch    trn loss    val loss    trn/val  dur
-------  ----------  ----------  ---------  ------
      1     0.03725     0.03334    1.11732  49.62s
      2     0.02761     0.01838    1.50264  48.68s
      3     0.01434     0.01170    1.22530  48.66s
      4     0.01049     0.00947    1.10827  48.72s
      5     0.00881     0.00818    1.07724  48.69s
      6     0.00787     0.00751    1.04809  48.71s
      7     0.00738     0.00713    1.03469  48.68s
      8     0.00706     0.00686    1.02909  48.68s
      9     0.00683     0.00665    1.02665  48.71s
     10     0.00664     0.00648    1.02463  48.68s
     11     0.00649     0.00634    1.02320  48.72s
     12     0.00636     0.00622    1.02229  48.68s
     13     0.00626     0.00613    1.02106  48.71s
     14     0.00616     0.00604    1.02089  48.71s
     15     0.00608     0.00596    1.02046  48.69s
     16     0.00601     0.00589    1.01993  48.72s
     17     0.00595     0.00583    1.01997  49.97s
     18     0.00589     0.00577    1.01962  48.97s
     19     0.00583     0.00572    1.01953  49.10s
     20     0.00579     0.00568    1.01946  48.63s
     21     0.00574     0.00563    1.01921  48.63s
     22     0.00570     0.00559    1.01920  48.64s
     23     0.00566     0.00556    1.01907  48.63s
     24     0.00563     0.00552    1.01874  48.63s
     25     0.00560     0.00549    1.01869  48.63s
     26     0.00557     0.00546    1.01883  48.64s
     27     0.00554     0.00544    1.01896  48.63s
     28     0.00551     0.00541    1.01855  48.62s
     29     0.00549     0.00539    1.01845  48.63s
     30     0.00547     0.00537    1.01845  48.63s
     31     0.00545     0.00535    1.01853  49.76s
     32     0.00543     0.00533    1.01847  48.79s
     33     0.00541     0.00531    1.01845  48.78s
     34     0.00539     0.00529    1.01864  48.78s
     35     0.00538     0.00528    1.01853  48.79s
     36     0.00536     0.00526    1.01856  48.78s
     37     0.00535     0.00525    1.01863  48.79s
     38     0.00533     0.00524    1.01847  48.79s
     39     0.00532     0.00522    1.01846  48.78s
     40     0.00531     0.00521    1.01840  48.79s
     41     0.00530     0.00520    1.01852  48.79s
     42     0.00529     0.00519    1.01865  48.98s
     43     0.00528     0.00518    1.01858  49.19s
     44     0.00527     0.00517    1.01870  48.69s
     45     0.00526     0.00516    1.01859  48.72s
     46     0.00525     0.00515    1.01867  48.72s
     47     0.00524     0.00515    1.01869  48.69s
     48     0.00523     0.00514    1.01875  48.74s
     49     0.00523     0.00513    1.01887  48.67s
     50     0.00522     0.00512    1.01867  48.71s
Training done.

In [16]:
# save result
net.cae_save('net_15000.pkl')

In [17]:
# Plot the loss curve
net.cae_eval()



In [30]:
# from imp import reload
# reload(utils)
# Test the network
imgs = X_in.reshape(-1,rs,rs)
img_small = imgs[35,:,:]

# encode
img_en = utils.get_encode(net.cae, img_small)
# decode
img_de = utils.get_decode(net.cae, img_en)

# Compare
# img_pre = np.rint(img_de.reshape(50,50) * 256).astype(int)
# img_pre = np.clip(img_pre, a_min = 0, a_max = 255)
# img_pre = img_pre.astype('uint8')
img_pre = img_de.reshape(rs,rs)
plt.imshow(img_pre)

# img_pre = utils.get_predict(net.cae, img_small)


Out[30]:
<matplotlib.image.AxesImage at 0x7f4e5d399668>

In [31]:
def get_picture_array(X, rescale=4):
    array = X.reshape(rs,rs)*255
    array = np.clip(array, a_min = 0, a_max = 255)
    return  array.repeat(rescale, axis = 0).repeat(rescale, axis = 1).astype(np.uint8())

def compare_images(img, img_pre):
    original_image = Image.fromarray(get_picture_array(img))
    new_size = (original_image.size[0] * 2, original_image.size[1])
    new_im = Image.new('L', new_size)
    new_im.paste(original_image, (0,0))
    rec_image = Image.fromarray(get_picture_array(img_pre))
    new_im.paste(rec_image, (original_image.size[0],0))
    new_im.save('test.png', format="PNG")
    return IPImage('test.png')

compare_images(img_small, img_pre)


Out[31]:

In [32]:
plt.imshow(img_small)


Out[32]:
<matplotlib.image.AxesImage at 0x7f4e5d372470>

In [33]:
plt.imshow(img_pre)


Out[33]:
<matplotlib.image.AxesImage at 0x7f4e5d2cf390>

In [43]:
plot_conv_weights(net.cae.layers_[1], figsize=(4,4))


Out[43]:
<module 'matplotlib.pyplot' from '/home/mzx/work/deeplearning/convolutional_autoencoder/env/lib/python3.4/site-packages/matplotlib/pyplot.py'>

In [38]:
utils.get_concate(net.cae, layer_idx=3, savefolder='C2')
IPImage('C2/map_con.png')


Concating the maps
montage -mode concatenate -tile 4x C2/*.png C2/map_con.png
Out[38]:

In [39]:
img_conv = utils.get_conv(net.cae, layer_idx=1, img=img_small, savefolder='conv1')
IPImage('conv1/conv_con.png')


Concating the convolved maps
montage -mode concatenate -tile 4x conv1/*.png conv1/conv_con.png
Out[39]:

In [40]:
img_conv = utils.get_conv(net.cae, layer_idx=3, img=img_conv[0,:,:], savefolder='conv2')
IPImage('conv2/conv_con.png')


Concating the convolved maps
montage -mode concatenate -tile 4x conv2/*.png conv2/conv_con.png
Out[40]:

In [41]:
img_conv = utils.get_conv(net.cae, layer_idx=5, img=img_conv[0,:,:], savefolder='conv3')
IPImage('conv3/conv_con.png')


Concating the convolved maps
montage -mode concatenate -tile 4x conv3/*.png conv3/conv_con.png
Out[41]: