LoginSignup
2
2

More than 3 years have passed since last update.

手書き文字を認識する。

Posted at

こんにちはまさです。
Qiita初投稿ですよろしくお願いします。
画像認識の定番mnistを使っての画像認識をしてみました。

↓以下の記事を参考に手書き画像の分類をしました。
はじめてのニューラルネットワーク:分類問題の初歩
https://www.tensorflow.org/tutorials/keras/classification?hl=ja

まずTensorflow、numpy、matplotlibをインポートします。

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist

データセットにはクラス名が保存されていないため、クラス名を保存しておきます。

class_names = ['0', '1', '2', '3', '4', 
               '5', '6', '7', '8', '9']

画像手書き画像のmnistをロードします。

(train_images,train_labels),(test_images,test_labels)= mnist.load_data()

画像が正しくインポートされているかを確認します。

plt.figure()
plt.imshow(train_images[0],plt.cm.binary)
plt.colorbar()
plt.show()

画像が表示されることを確認します。
image.png

先頭の25個分の画像を5*5で表示させてみます。

plt.figure(figsize=(10,10))
for i in range(25):
  plt.subplot(5,5,i+1)
  plt.imshow(train_images[i],cmap=plt.cm.binary)
  plt.xlabel(class_names[train_labels[i]])
plt.show()

実際に表示された画像がこちら
image.png

データの前処理を行います。
ニューラルネットワークにデータを投入する前に、これらの値を0から1までの範囲にスケールするため、画素の値を255で割ります。

train_images = train_images / 255.0
test_images = test_images / 255.0

モデルの構築をします。今回のニューラルネットワークは2層で構築します。

model=keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128,activation='relu'),
    keras.layers.Dense(10,activation='softmax')
])

モデルをコンパイルします。

model.compile(optimizer='adam',
             loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

モデルを学習させます。

model.fit(train_images,train_labels,epochs=5)

モデルを評価します。

test_loss,test_acc=model.evaluate(test_images,test_labels,verbose=2)
print('¥nTest accuracy:',test_acc)

モデルを予測します。

predictions = model.predict(test_images)

10チャンネル全てグラフ化する。そのために定義します。

def plot_image(i, predictions_array, true_label, img):
    predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])

    plt.imshow(img, cmap=plt.cm.binary)

    predicted_label = np.argmax(predictions_array)
    if predicted_label == true_label:
        color = 'blue'
    else:
        color = 'red'

    plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                    100*np.max(predictions_array),
                                    class_names[true_label]),
                                    color=color)

def plot_value_array(i, predictions_array, true_label):
    predictions_array, true_label = predictions_array[i], true_label[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    thisplot = plt.bar(range(10), predictions_array, color="#777777")
    plt.ylim([0, 1]) 
    predicted_label = np.argmax(predictions_array)

    thisplot[predicted_label].set_color('red')
    thisplot[true_label].set_color('blue')

予測の中のいくつかの画像を、予測値とともに表示する。正しい予測は青で表示します。数字は予測したラベルのパーセント(100分率)を示します。

# X個のテスト画像、予測されたラベル、正解ラベルを表示します。
# 正しい予測は青で、間違った予測は赤で表示しています。
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_image(i, predictions, test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_value_array(i, predictions, test_labels)
plt.show()

予想された画像
image.png

全ての手書き数字画像を正しく予測することができました。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2