1. 수기로 작성된 숫자(필기체) 영상 60000개 와 시험셑 10000 개를 머신러닝 학습시켜 인식률을 97% 이상 올린
사례인 CNN(Convolution Neural Network) 기반 작성소스를 정밀하게 분석한 소스를 제출하시오.
(특히 각 layer의 역할을 잘 설명하시오)
[1] 데이터는 인공지능 범용 데이터 셑인 keras.datasets.mnist를 활용합니다.
[2] 8단계로 분석합니다.
1단계 ~ 사용할 패키지 임포트와 버젼 확인 단계
# 사용할 패키지 import
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import sys
import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
import numpy as np
np.random.seed(7)
# 파이썬, 텐스플로우, 케라스 버전확인
print('Python version : ', sys.version)
print('TensorFlow version : ', tf.__version__)
print('Keras version : ', keras.__version__)
![](https://t1.daumcdn.net/cfile/cafe/99C4C7395DCCCD4923)
2단계 ~ 훈련과 시험셑 분리하고 one-hot encoding 단계
# 이미지 크기
img_rows = 28
img_cols = 28
# 훈련셋과 검증셋을 나눈다
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# 데이터 셋 전처리
input_shape = (img_rows, img_cols, 1) # 튜플
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
print('x_train shape:', x_train.shape) # 훈련셋, 가로, 세로 1
print(x_train.shape[0], 'train samples') # 훈련셋
print(x_test.shape[0], 'test samples') # 검증셋
batch_size = 128
num_classes = 10
epochs = 12
# 원핫 인코딩
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
![](https://t1.daumcdn.net/cfile/cafe/990E21375DCCCD6226)
3단계 ~ 모델 생성과 그 결과 요약 단계
모델 생성
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5), strides=(1, 1), padding='same',
activation='relu',
input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(64, (2, 2), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
# 파라미터 층 구조 파악
model.summary()
![](https://t1.daumcdn.net/cfile/cafe/991A823F5DCCCD7422)
4단계 ~ 모델의 구성을 주피트 노트에 인라인 시켜 확인하는 단계
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
%matplotlib inline
# SVG로 모델을 시각화 하기
SVG(model_to_dot(model, show_shapes=True).create(prog='dot', format='svg'))
![](https://t1.daumcdn.net/cfile/cafe/994166485DCCCF0120)
5단계 ~ 훈련시켜(epoch) 모델을 만드는 단계
#모델 컴파일
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit()함수를 사용하여 모델 훈련시키기
hist = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
![](https://t1.daumcdn.net/cfile/cafe/994D4F365DCCCD9D1F)
6단계 ~ 손실률과 정확도 출력단계
score = model.evaluate(x_test, y_test, verbose=0)
# 손실률
print('Test loss:', score[0])
# 정확도
print('Test accuracy:', score[1])
![](https://t1.daumcdn.net/cfile/cafe/99E033335DCCCDAC19)
7단계 ~ 입력 이미지 정보를 화면에 출력해보기
from matplotlib.pyplot import *
import matplotlib.pyplot as plt
n = 0
plt.imshow(x_test[n].reshape(28, 28), cmap='Greys', interpolation='nearest')
# 이미지 출력
plt.show()
print('The Answer is ', model.predict_classes(x_test[n].reshape((1, 28, 28, 1))))
![](https://t1.daumcdn.net/cfile/cafe/99B588395DCCCDBB1D)
8단계 ~ 인식 실패한 정보를 화면에 출력해보기
import random
predicted_result = model.predict(x_test)
predicted_labels = np.argmax(predicted_result, axis=1)
test_labels = np.argmax(y_test, axis=1)
wrong_result = []
for n in range(0, len(test_labels)):
# 예측값과 다르면 실패
if predicted_labels[n] != test_labels[n]:
wrong_result.append(n)
samples = random.choices(population=wrong_result, k=16)
count = 0
nrows = ncols = 4
plt.figure(figsize=(12,8))
for n in samples:
count += 1
plt.subplot(nrows, ncols, count)
plt.imshow(x_test[n].reshape(28, 28), cmap='Greys', interpolation='nearest')
tmp = "Label:" + str(test_labels[n]) + ", Prediction:" + str(predicted_labels[n])
plt.title(tmp)
plt.tight_layout()
plt.show()
![](https://t1.daumcdn.net/cfile/cafe/99BC02335DCCCDCD1A)