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
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]:
In [6]:
X.shape
Out[6]:
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]:
In [9]:
X_norm = X_norm[0:15000,:]
X_norm.shape
Out[9]:
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]:
In [12]:
plt.imshow(I)
Out[12]:
In [13]:
# generate layers
net.gen_layers()
net.layers
Out[13]:
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()
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]:
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]:
In [33]:
plt.imshow(img_pre)
Out[33]:
In [43]:
plot_conv_weights(net.cae.layers_[1], figsize=(4,4))
Out[43]:
In [38]:
utils.get_concate(net.cae, layer_idx=3, savefolder='C2')
IPImage('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')
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')
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')
Out[41]: