Neural Network with TensorFlow@2017.11.30

Neural Networkによる手書き数字認識@2017.11.28をアルゴリズムをTensorFlowに変えて挑戦してみる。
ーーーーーーーーーーーーーーーーーーー
## 1.ライブラリの読み込み ##

# TensorFlowライブラリ
import tensorflow as tf
# tflearnライブラリ
import tflearn

# mnistデータセットを扱うためのライブラリ
import tflearn.datasets.mnist as mnist

# MNIST画像を表示するためのライブラリ
from matplotlib import pyplot as plt
from matplotlib import cm
import numpy as np
ーーーーーーーーーーーーーーーーーーー
## 2.データの読み込みと前処理 ##
# MNISTデータを./data/mnistへダウンロードし、解凍して各変数へ格納
trainX, trainY, testX, testY = mnist.load_data(‘./data/mnist/’, one_hot=True)
ーーーーーーーーーーーーーーーーーーー
## データの確認
# 学習用の画像ピクセルデータと正解データのサイズを確認
print(len(trainX),len(trainY))

# テスト用の画像ピクセルデータと正解データのサイズを確認
print(len(testX),len(testY))

# 学習用の画像ピクセルデータを確認
print(trainX)

# 学習用の正解データを確認
print(trainY)
ーーーーーーーーーーーーーーーーーーー
## 3.ニューラルネットワークの作成 ##

## 初期化
tf.reset_default_graph()

## 入力層の作成
net = tflearn.input_data(shape=[None, 784])

## 中間層の作成
net = tflearn.fully_connected(net, 128, activation=’relu’)
net = tflearn.dropout(net, 0.5)

## 出力層の作成
net = tflearn.fully_connected(net, 10, activation=’softmax’)
net = tflearn.regression(net, optimizer=’sgd’, learning_rate=0.5, loss=’categorical_crossentropy’)
ーーーーーーーーーーーーーーーーーーー
## 4.モデルの作成(学習) ##
# 学習の実行
model = tflearn.DNN(net)
model.fit(trainX, trainY, n_epoch=20, batch_size=100, validation_set=0.1, show_metric=True)
ーーーーーーーーーーーーーーーーーーー
## 5.モデルの適用(予測) ##
pred = np.array(model.predict(testX)).argmax(axis=1)
print(pred)

label = testY.argmax(axis=1)
print(label)

accuracy = np.mean(pred == label, axis=0)
print(accuracy)
ーーーーーーーーーーーーーーーーーーー
[7 2 1 …, 4 5 6]
[7 2 1 …, 4 5 6]
0.9719
ーーーーーーーーーーーーーーーーーーー
TFLearnの学習用では、97.2%という識字率!

では、このTensorFlow+TFLearnのアルゴリズムでのテスト画像配列testX、testYに代わり、
自作ニューラルネットで前回使用した画像の配列arrX、arrYを当てはめてみる。

ーーーーーーーーーーーーーーーーーーー
import numpy
# scipy.special for the sigmoid function expit()
import scipy.special
# library for plotting arrays
import matplotlib.pyplot
# ensure the plots are inside this notebook, not an external window
%matplotlib inline
# helper to load data from PNG image files
import scipy.misc
# glob helps select multiple files using patterns
import glob

# our own image test data set
our_own_dataset = []
arrX = np.empty((0,784), float)
arrY = np.empty((0), int)

# load the png image data as test data set
for image_file_name in glob.glob(‘my_own_images/2828_my_own_R_?.png’):

# use the filename to set the correct label
label = int(image_file_name[-5:-4])

# load image data from png files into an array
print (“loading … “, image_file_name)
img_array = scipy.misc.imread(image_file_name, flatten=True)

# reshape from 28×28 to list of 784 values, invert values
img_data = 255.0 – img_array.reshape(784)

# then scale data to range from 0.01 to 1.0
img_data = (img_data / 255.0 * 0.99) + 0.01
print(numpy.min(img_data))
print(numpy.max(img_data))

# append label and image data to test data set

arrX = np.append(arrX, np.array([img_data]), axis=0)
arrY = np.append(arrY, np.array([label]), axis=0)

record = numpy.append(label,img_data)
our_own_dataset.append(record)

pass
ーーーーーーーーーーーーーーーーーーー
## 5.モデルの適用(予測) ##
pred = np.array(model.predict(arrX)).argmax(axis=1)
print(pred)

##label = testY.argmax(axis=1)
print(arrY)

accuracy = np.mean(pred == arrY, axis=0)
print(accuracy)
ーーーーーーーーーーーーーーーーーーー
[0 1 2 3 4 5 6 7 8 8]
[0 7 2 3 4 5 6 3 8 9]
0.7
ーーーーーーーーーーーーーーーーーーー
もともとの画像で行うと正答率70%。自作ニューラルネットより上昇!

そこで、姑息手段だが、1と7と9をちょっと綺麗なフォント画像に変更してみる。

ーーーーーーーーーーーーーーーーーーー
[0 1 2 3 4 5 6 7 8 8]
[0 1 2 3 4 5 6 7 8 9]
0.9
ーーーーーーーーーーーーーーーーーーー
で正答率90%と上昇した。8と9は確かに似ているが。。。。