MNIST 데이터
- 미국 국립표준기술원(NIST)이 고등학생과 인구조사국 직원 등이 쓴 손글씨를 이용해 만든 데이터
- 7만 개의 글자 이미지에 각각 0부터 9까지 이름표를 붙인 데이터셋
- keras API를 이용해 불러올 수 있음
이미지 인식
1. 데이터 준비
- X : 불러온 이미지 데이터 / y : 이미지에 0~9를 붙인 이름표
- X_train, y_train : 학습셋 / X_test, y_test : 테스트셋
from tensorflow.keras.datasets import mnist
# MNIST 데이터셋을 불러와 학습셋과 테스트셋으로 저장
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# 학습셋과 테스트셋이 각각 몇 개의 이미지로 되어 있는지 확인
print("학습셋 이미지 수 : %d 개" % (X_train.shape[0]))
print("테스트셋 이미지 수 : %d 개" % (X_test.shape[0]))
학습셋 이미지 수 : 60000 개
테스트셋 이미지 수 : 10000 개
2. 이미지 출력
- imshow()
: 이미지를 시각화하는 데 사용하는 함수
: matplotlib의 pyplot 모듈에 존재
- cmap : 색상 맵 지정하는 옵션
import matplotlib.pyplot as plt
# 첫 번째 이미지 확인
plt.imshow(X_train[0], cmap='Greys')
plt.show()
3. 이미지가 인식되는 원리
- 각 픽셀의 밝기 정도에 따라 0(흰색)부터 255의 숫자 중 하나로 채워져 긴 행렬로 이루어진 하나의 집합으로 변환
import sys
# 이미지가 인식되는 원리를 알아봄
for x in X_train[0]:
for i in x:
sys.stdout.write("%-3s" % i)
sys.stdout.write('\n')
데이터 전처리
1. 차원 변환
- 주어진 가로 28, 세로 28의 2차원 배열을 784개의 1차원 배열로 바꿔야 함
- 데이터의 첫 번째 요소로 실습
- reshape(총 샘플 수, 1차원 속성의 개수)
: 다차원 배열의 모양을 변경하는 함수
: 배열의 차원(dimension)을 재구성하거나 배열의 요소 수를 동일하게 유지하면서 배열의 모양을 변경
: numpy에 존재
# 차원 변환 과정 실습
X_train = X_train.reshape(X_train.shape[0], 784)
2. 데이터 정규화
- 데이터의 폭이 클 때 적절한 값으로 분산의 정도를 바꾸는 과정
- 현재 0~255 사이의 값으로 이루어진 값을 케라스가 쉽게 작동할 수 있도록 0~1 사이의 값으로 변경
- 각 값을 실수형으로 바꾼 후 255로 나눔
- astype(데이터 타입)
: 배열의 요소를 다른 데이터 타입으로 변환하는 함수
: numpy에 존재
X_train = X_train.astype('float64')
X_train = X_train / 255
X_test = X_test.reshape(X_test.shape[0], 784).astype('float64') / 255
# 클래스 값 확인
print("class : %d " % (y_train[0]))
class : 5
3. 원 - 핫 인코딩
- 0 또는 1로만 이루어진 벡터로 값 수정
- to_categorical(클래스, 클래스의 개수)
: 정수형 레이블을 원-핫 인코딩(one-hot encoding) 형태로 변환하는 함수
: keras의 utils 모듈에 존재
from tensorflow.keras.utils import to_categorical
# 바이너리화 과정 실습
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
print(y_train[0])
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
딥러닝 기본 프레임 - MNIST 데이터
1. 환경 및 데이터 준비
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
import numpy as np
import os
# MNIST 데이터를 불러옴
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# 차원 변환 후, 테스트셋과 학습셋으로 나눔
X_train = X_train.reshape(X_train.shape[0], 784).astype('float32') / 255
X_test = X_test.reshape(X_test.shape[0], 784).astype('float32') / 255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
2. 모델 구조 및 실행 환경 설정
# 모델 구조 설정
model = Sequential()
model.add(Dense(512, input_dim=784, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()
# 모델 실행 환경 설정
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
3. 모델 최적화
- 10번 이상 모델 성능이 향상되지 않으면 자동으로 학습 중단
MODEL_DIR = './data/model/'
if not os.path.exists(MODEL_DIR):
os.mkdir(MODEL_DIR)
modelpath="./data/model/MNIST_MLP.hdf5"
checkpointer = ModelCheckpoint(filepath=modelpath, monitor='val_loss', verbose=1, save_best_only=True)
early_stopping_callback = EarlyStopping(monitor='val_loss', patience=10)
4. 모델 실행
# 모델 실행
history = model.fit(X_train, y_train, validation_split=0.25, epochs=30, batch_size=200, verbose=0, callbacks=[early_stopping_callback,checkpointer])
# 테스트 정확도 출력
print("\n Test Accuracy: %.4f" % (model.evaluate(X_test, y_test)[1]))
5. 데이터 시각화
- 학습셋의 오차를 그래프로 표현
# 검증셋과 학습셋의 오차 저장
y_vloss = history.history['val_loss']
y_loss = history.history['loss']
# 그래프로 표현
x_len = np.arange(len(y_loss))
plt.plot(x_len, y_vloss, marker='.', c="red", label='Testset_loss')
plt.plot(x_len, y_loss, marker='.', c="blue", label='Trainset_loss')
# 그래프에 그리드를 주고 레이블을 표시
plt.legend(loc='upper right')
plt.grid()
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()
'모두의 딥러닝 개정 3판'의 내용과 https://github.com/taehojo/deeplearning의 코드 참고
'AI > 모두의 딥러닝' 카테고리의 다른 글
[모두의 딥러닝] 22. 텍스트 전처리 - 토큰화(tokenization), 빈도 수 세기 (0) | 2023.06.02 |
---|---|
[모두의 딥러닝] 21. 컨볼루션 신경망(CNN), 풀링(Pooling), 드롭아웃(drop out), 플래튼(flatten) - MNIST 데이터 (0) | 2023.06.02 |
[모두의 딥러닝] 19. 결측치 처리, 속성별 관련도 추출 - 부동산 가격 예측 데이터 (0) | 2023.05.29 |
[모두의 딥러닝] 18. 에포크(epochs), 과적합 그래프, 학습 자동 중단 - 와인 품질 예측 데이터 (0) | 2023.05.29 |
[모두의 딥러닝] 17. k겹 교차 검증(k-fold cross validation) - 초음파 광물 예측 데이터 (2) | 2023.05.26 |