[keras] ANN mnist 만들기
모든 경우를 다룰 수 없어 이번 예제에서는 keras model의 Sequential을 이용하여 ANN을 구성하겠습니다.
손글씨 분류 학습하기
데이터 로드
import keras
from keras import datasets
import numpy as np
import numpy as np
(x_train_raw, y_train_raw), (x_test_raw, y_test_raw) = datasets.mnist.load_data()
print("x_train_raw.shape = {0}".format(x_train_raw.shape))
print("x_test_raw.shape = {0}".format(x_test_raw.shape))
print("y_train_raw.shape = {0}".format(y_train_raw.shape))
print("y_test_raw.shape = {0}".format(y_test_raw.shape))
# 출력
# x_train_raw.shape = (60000, 28, 28)# x_test_raw.shape = (10000, 28, 28)
# y_train_raw.shape = (60000,)
# y_test_raw.shape = (10000,)
datasets.mnist.load_data()
keras에서 제공하는 datasets중 mnist라는 데이터 셋을 불러옵니다.x_train_raw, x_test_raw로 28x28 사이즈의 grayscale 이미지를 불러옵니다.
y_train_raw, y_test_raw로 0-9 사이의 수를 불러옵니다.
x, y train은 각 6만개(num_samples), x, y test는 각 만개(num_samples)를 불러옵니다.
Notes
x train 28x28의 grayscale 이미지가 7이라면
y train은 7이라는 수를 가지고 있습니다. (y는 이미지가 아닙니다.)
y train은 7이라는 수를 가지고 있습니다. (y는 이미지가 아닙니다.)
데이터 전처리
_ , rows, cols = x_train_raw.shape # 60000, 28, 28
x_train = x_train_raw.reshape(-1, rows*cols)
x_test = x_test_raw.reshape(-1, rows*cols)
x_train /= 255.
x_test /= 255.
print("x_train.shape = {0}".format(x_train.shape))
print("x_test.shape = {0}".format(x_test.shape))
print("type(x_train_raw) = {0}".format(type(x_train_raw[0][0][0])))
print("type(x_test_raw) = {0}".format(type(x_test_raw[0][0])))
# 출력
# x_train.shape = (60000, 784)# x_test.shape = (10000, 784)
# type(x_train_raw) = <class 'numpy.uint8'>
# type(x_test_raw) = <class 'numpy.ndarray'>
x_train = x_train_raw.reshape(-1, rows*cols)
현재 x_train_raw, x_test_raw는 현재 28x28의 사이즈를 가지는 이미지입니다.reshape 함수를 통해 2차원의 이미지를 1차원으로 줄여줍니다.
2차원(28*28)에서 1차원인 784로 shape이 변합니다.
Notes
reshape을 통해 x_train의 각 row는 하나의 이미지 데이터를 가지게 됩니다. (x_train[0] == 6만장 중 첫번째 이미지)
x_train /= 255.
이미지의 픽셀 값들은 0-255 범위를 가지기에 0.0 - 1.0의 범위를 가질 수 있도록 255로 나눠줍니다. (정규화)One-hot-Encoding
num_classes = 10
y_train = keras.utils.to_categorical(y_train_raw, num_classes=num_classes)
y_test = keras.utils.to_categorical(y_test_raw, num_classes=num_classes)
y_train = keras.utils.to_categorical(y_train_raw, num_classes=num_classes)
y_test = keras.utils.to_categorical(y_test_raw, num_classes=num_classes)
keras.utils.to_categorical()
단어를 벡터 표현의 방식으로 바꾸어 줍니다.표현하고 싶은 단어의 인덱스의 위치에 1을, 다른 단어의 인덱스의 위치에는 0을 부여합니다.
Notes
y_train의 첫번째 이미지 라벨이 5였다면 이를 [0, 0, 0, 0, 0, 1, 0, 0, 0, 0] index로 값을 표현합니다.
ANN 신경망 구성
model = keras.Sequential()
model.add(keras.layers.Dense(100, activation="relu", input_shape=(rows*cols,))) model.add(keras.layers.Dense(num_classes, activation="softmax"))
model.summary()
model.add(keras.layers.Dense(100, activation="relu", input_shape=(rows*cols,))) model.add(keras.layers.Dense(num_classes, activation="softmax"))
model.summary()
keras.layers.Dense(100, activation="relu", input_shape=(rows*cols,))
하나의 hidden layer(100개의 input을 가짐)만을 가지는 network를 구현하였으며input shape은 28*28 이미지를 1차원으로 압축한 784의 값을 그리고
activate은 relu(Rectified Linear Unit)로 설정했습니다.
model.summary()
신경망 모델 학습과정 설정
model.compile("adam", loss="categorical_crossentropy", metrics=["accuracy"])
compile(optimizer, loss, metrics)
# Configures the model for training.
model을 train하기 전에 loss와 optimizer를 정하는 과정입니다.optimizer = adam,
loss = categorical_crossentropy,
metrics = accuracy 로 설정했습니다.
신경망 모델 학습
model.fit(x_train, y_train, batch_size=128, epochs=10, verbose=2, validation_split=0.2)
fit(x, y, batch_size, epochs, verbose, validation_split)
# Trains the model for a given number of epochs (iterations on a dataset).
표현하고 싶은 단어의 인덱스의 위치에 1을, 다른 단어의 인덱스의 위치에는 0을 부여합니다.x_train, y_train 데이터를 학습시킵니다.
총 10번(epochs) 반복 학습을 하며
6만개의 데이터 중 20%(validation_split)는 검증 데이터로 활용합니다.
Notes
학습 이미지 수: 48,000, 검증 이미지 수: 12,000
신경망 모델 평가
loss, accuracy = model.evaluate(x_test, y_test, batch_size=128)
print("loss = {0}".format(loss))
print("accuracy = {0}".format(accuracy))
# loss = 0.08149969656001776
# accuracy = 0.9749
print("loss = {0}".format(loss))
print("accuracy = {0}".format(accuracy))
# loss = 0.08149969656001776
# accuracy = 0.9749
evaluate(x, y, batch_size)
# Returns the loss value & metrics values for the model in test mode.
x_train, y_train 데이터로 학습된 모델을 x_test, y_test 데이터를 사용해 성능을 측정합니다.loss = 0.0814
accuracy = 0.9749
신경망 모델 사용
y_predicted = model.predict_classes(x_test)
for i in range(5):
print("y_test[{0}] = {1}".format(i, y_test[i]))
print("y_predicted[{0}] = {1}".format(i, y_predicted[i]))
for i in range(5):
print("y_test[{0}] = {1}".format(i, y_test[i]))
print("y_predicted[{0}] = {1}".format(i, y_predicted[i]))
predict_classes(x)
# Generate class predictions for the input samples batch by batch
x_test만 입력 값을 넣어줍니다.y_test는 x_test에 대한 실제 라벨 값.
y_predicted는 신경망이 x_test에 대해 유추해 낸 값입니다.
Notes
신경망 모델이 입력 값 x만을 가지고 y를 제대로 유추해 내었습니다.
댓글
댓글 쓰기