[OFE-01] 必要なモジュールをインポートします。
In [1]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import cPickle as pickle
[OFE-02] データファイル「ORENIST.data」から画像とラベルのデータを読み込みます。
In [2]:
with open('ORENIST.data', 'rb') as file:
images, labels = pickle.load(file)
[OFE-03] 画像データのサンプルを表示します。
In [3]:
fig = plt.figure(figsize=(10,5))
for i in range(40):
subplot = fig.add_subplot(4, 10, i+1)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.set_title('%d' % np.argmax(labels[i]))
subplot.imshow(images[i].reshape(28,28), vmin=0, vmax=1,
cmap=plt.cm.gray_r, interpolation='nearest')
[OFE-04] フィルターの情報を格納した多次元リストを作る関数を用意します。
In [4]:
def edge_filter():
filter0 = np.array(
[[ 2, 1, 0,-1,-2],
[ 3, 2, 0,-2,-3],
[ 4, 3, 0,-3,-4],
[ 3, 2, 0,-2,-3],
[ 2, 1, 0,-1,-2]]) / 23.0
filter1 = np.array(
[[ 2, 3, 4, 3, 2],
[ 1, 2, 3, 2, 1],
[ 0, 0, 0, 0, 0],
[-1,-2,-3,-2,-1],
[-2,-3,-4,-3,-2]]) / 23.0
filter_array = np.zeros([5,5,1,2])
filter_array[:,:,0,0] = filter0
filter_array[:,:,0,1] = filter1
return tf.constant(filter_array, dtype=tf.float32)
[OFE-05] 画像データにフィルターを適用する計算式を用意します。
In [5]:
x = tf.placeholder(tf.float32, [None, 784])
x_image = tf.reshape(x, [-1,28,28,1])
W_conv = edge_filter()
h_conv = tf.abs(tf.nn.conv2d(x_image, W_conv,
strides=[1,1,1,1], padding='SAME'))
h_conv_cutoff = tf.nn.relu(h_conv-0.2)
h_pool =tf.nn.max_pool(h_conv_cutoff, ksize=[1,2,2,1],
strides=[1,2,2,1], padding='SAME')
[OFE-06] セッションを用意して、Variable を初期化します。
In [6]:
sess = tf.Session()
sess.run(tf.initialize_all_variables())
[OFE-07] 最初の9個分の画像データに対して、畳み込みフィルターを適用した結果を計算します。
In [7]:
filter_vals, conv_vals = sess.run([W_conv, h_conv_cutoff],
feed_dict={x:images[:9]})
[OFE-08] 得られた結果を画像として表示します。
ここでは、オリジナルの画像と2種類のフィルターを適用したそれぞれの結果を表示しています。
左端は、適用したフィルターを画像化したものです。
In [8]:
fig = plt.figure(figsize=(10,3))
for i in range(2):
subplot = fig.add_subplot(3, 10, 10*(i+1)+1)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.imshow(filter_vals[:,:,0,i],
cmap=plt.cm.gray_r, interpolation='nearest')
v_max = np.max(conv_vals)
for i in range(9):
subplot = fig.add_subplot(3, 10, i+2)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.set_title('%d' % np.argmax(labels[i]))
subplot.imshow(images[i].reshape((28,28)), vmin=0, vmax=1,
cmap=plt.cm.gray_r, interpolation='nearest')
subplot = fig.add_subplot(3, 10, 10+i+2)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.imshow(conv_vals[i,:,:,0], vmin=0, vmax=v_max,
cmap=plt.cm.gray_r, interpolation='nearest')
subplot = fig.add_subplot(3, 10, 20+i+2)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.imshow(conv_vals[i,:,:,1], vmin=0, vmax=v_max,
cmap=plt.cm.gray_r, interpolation='nearest')
[OFE-09] フィルターに加えて、プーリング層を適用した結果を取得します。
In [9]:
pool_vals = sess.run(h_pool, feed_dict={x:images[:9]})
[OFE-10] 得られた結果を画像として表示します。
In [10]:
fig = plt.figure(figsize=(10,3))
for i in range(2):
subplot = fig.add_subplot(3, 10, 10*(i+1)+1)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.imshow(filter_vals[:,:,0,i],
cmap=plt.cm.gray_r, interpolation='nearest')
v_max = np.max(pool_vals)
for i in range(9):
subplot = fig.add_subplot(3, 10, i+2)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.set_title('%d' % np.argmax(labels[i]))
subplot.imshow(images[i].reshape((28,28)), vmin=0, vmax=1,
cmap=plt.cm.gray_r, interpolation='nearest')
subplot = fig.add_subplot(3, 10, 10+i+2)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.imshow(pool_vals[i,:,:,0], vmin=0, vmax=v_max,
cmap=plt.cm.gray_r, interpolation='nearest')
subplot = fig.add_subplot(3, 10, 20+i+2)
subplot.set_xticks([])
subplot.set_yticks([])
subplot.imshow(pool_vals[i,:,:,1], vmin=0, vmax=v_max,
cmap=plt.cm.gray_r, interpolation='nearest')