MNIST란 어떤 데이터셋일까?
- Dataset: 숫자 손글씨 데이터셋 MNIST
- 데이터 제공자: Prof.Yann Lecun
- Datset: yann.lecun.com/exdb/mnist/
"The MNIST database of handwritten digits, available from this page, has a training set of 60,000 examples, and a test set of 10,000 examples. It is a subset of a larger set available from NIST. The digits have been size-normalized and centered in a fixed-size image."
공식 사이트를 참고하면 6만장의 training set과 1만장의 test set을 가지고 있음을 알 수 있습니다.
"The original black and white (bilevel) images from NIST were size normalized to fit in a 20x20 pixel box while preserving their aspect ratio. The resulting images contain grey levels as a result of the anti-aliasing technique used by the normalization algorithm. the images were centered in a 28x28 image by computing the center of mass of the pixels, and translating the image so as to position this point at the center of the 28x28 field."
NIST의 subset인 데이터셋에 해당하는 것이 MNIST이며 20x20으로 정규화된 Grayscale 이미지를 28x28 이미지 로 변환한다?
"The MNIST database was constructed from NIST's Special Database 3 and Special Database 1 which contain binary images of handwritten digits. NIST originally designated SD-3 as their training set and SD-1 as their test set. However, SD-3 is much cleaner and easier to recognize than SD-1. The reason for this can be found on the fact that SD-3 was collected among Census Bureau employees, while SD-1 was collected among high-school students. Drawing sensible conclusions from learning experiments requires that the result be independent of the choice of training set and test among the complete set of samples. Therefore it was necessary to build a new database by mixing NIST's datasets."
MNIST는 NIST의 3가지 종류의 Database에서 가져온 것이다. SD-n으로 각 Database에 따라 손글씨를 작성한 데이터구축참여자의 업종(고등학생, 직장인)이 다르다.
"The MNIST training set is composed of 30,000 patterns from SD-3 and 30,000 patterns from SD-1. Our test set was composed of 5,000 patterns from SD-3 and 5,000 patterns from SD-1. The 60,000 pattern training set contained examples from approximately 250 writers. We made sure that the sets of writers of the training set and test set were disjoint."
train set은 약 250명의 손글씨를 반영하고 있다. Training set과 test set은 중복되지 않는다.
MNIST를 불러오자
- 데이터셋 불러오기: mnist.load_data()
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
print(tf.__version__) # Tensorflow의 버전을 출력
mnist = keras.datasets.mnist
# MNIST 데이터를 로드. 다운로드하지 않았다면 다운로드까지 자동으로 진행됩니다.
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(len(x_train)) # x_train 배열의 크기를 출력
2.2.0
60000
mnist라는 변수에 keras에 내장된 mnist 데이터셋을 불러온다. 그리고 해당 데이터셋은 이미 training set과 test set이 나눠져서 제공되기 때문에 각각 train, test로 나눠 받을 수 있다. 이때, x_train, y_train은 무슨 차이를 가지고 있는지 아래 코드를 통해 확인할 수 있다.
- 불러온 데이터셋 읽어보기
plt.imshow(x_train[1],cmap=plt.cm.binary) #[1]은 x_train의 2번째 이미지
plt.show()
print(y_train[1]) #0 출력
더보기
0
# index에 0에서 59999 사이 숫자를 지정해 보세요.
index=3000
plt.imshow(x_train[index],cmap=plt.cm.binary)
plt.show()
print( (index+1), '번째 이미지의 숫자는 바로 ', y_train[index], '입니다.')
MNIST는 흑백 영상이기 때문에 cmap을 binary type으로 지정하고, x_train과 y_train을 출력해보면 x_train은 train의 영상 데이터를 의미하고 y_train은 해당 영상데이터의 label을 의미하는 것을 알 수 있다. x_train에는 9라는 이미지가 있는거고, y_train은 해당 이미지가 무엇인지 나타나있다고 생각하면 된다.
3. 사용 라이브러리
- Numpy
- Matplotlib: 파이썬에서 제공하는 시각화(Visualization) 패키지인 Matplotlib은 차트(chart), 플롯(plot) 등 다양한 형태로 데이터를 시각화할 수 있는 강력한 기능을 제공합니다. (공식 문서: matplotlib.org/gallery.html)
참고 블로그 모음
데이터셋 이야기: https://tykimos.github.io/2017/03/25/Dataset_and_Fit_Talk/