Computer >> 컴퓨터 >  >> 프로그래밍 >> Python

TensorFlow로 Fashion MNIST 데이터셋 다운로드하고 탐색하는 방법

TensorFlow는 구글(Google)에서 제공하는 머신러닝 프레임워크입니다. 오픈소스 기반으로 Python과 함께 사용되어 알고리즘 구현, 딥러닝 애플리케이션 개발 등 다양한 용도로 활용되며, 연구 목적과 실제 프로덕션 환경 모두에서 널리 쓰이고 있습니다.

TensorFlow 설치

'tensorflow' 패키지는 Windows 환경에서 아래 명령어 한 줄로 간단히 설치할 수 있습니다.

pip install tensorflow

Fashion MNIST 데이터셋이란?

'Fashion MNIST' 데이터셋은 다양한 종류의 의류 이미지를 담고 있는 데이터셋입니다. 10개의 서로 다른 카테고리에 속하는 7만 장 이상의 회색조(grayscale) 이미지로 구성되어 있으며, 각 이미지의 해상도는 28 x 28 픽셀로 낮은 편입니다.

이 글에서는 Google Colaboratory(Colab)를 사용하여 코드를 실행합니다. Google Colab은 브라우저에서 바로 Python 코드를 실행할 수 있게 해주며, 별도의 환경 설정이 필요 없고 GPU(그래픽 처리 장치)에 무료로 접근할 수 있다는 큰 장점이 있습니다. Colaboratory는 Jupyter Notebook을 기반으로 만들어진 도구입니다.

예제 코드

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

print("The tensorflow version used is ")
print(tf.__version__)
print("The dataset is being loaded")
fashion_mnist = tf.keras.datasets.fashion_mnist
print("The dataset is being classified into training and testing data ")
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

print("The dimensions of training data ")             
print(train_images.shape)

print("The number of rows in the training data")
print(len(train_labels))

print("The column names of dataset")
print(train_labels)
print("The dimensions of test data ")             
print(test_images.shape)
print("The number of rows in the test data")
print(len(test_labels))

코드 출처 - https://www.tensorflow.org/tutorials/keras/classification

실행 결과

The tensorflow version used is
2.4.0
The dataset is being loaded
The dataset is being classified into training and testing data
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
The dimensions of training data
(60000, 28, 28)
The number of rows in the training data
60000
The column names of dataset
[9 0 0 ... 3 0 5]
The dimensions of test data
(10000, 28, 28)
The number of rows in the test data
10000

코드 설명

  • 먼저 필요한 패키지들을 임포트합니다.

  • 현재 사용 중인 TensorFlow의 버전을 확인합니다.

  • Fashion MNIST 데이터셋을 로드하며, 이 데이터셋은 TensorFlow에서 직접 접근하여 사용할 수 있습니다.

  • 다음으로 전체 데이터를 학습용(train) 데이터셋과 테스트용(test) 데이터셋으로 분할합니다.

  • 데이터셋에는 총 70,000개의 행이 있으며, 그중 6만 개의 이미지는 모델 학습에 사용되고 나머지 1만 개는 모델이 이미지를 각 레이블별로 얼마나 잘 분류하는지 성능을 평가하는 데 사용됩니다.

  • 이것은 대표적인 분류(classification) 문제로, 데이터셋의 모든 이미지마다 고유한 레이블이 지정됩니다.

  • 이미지들은 의류 사진이며, 각 이미지에 해당하는 카테고리 레이블이 할당되어 있습니다.

  • 마지막으로 학습 및 테스트 데이터셋의 형태(shape), 행(row) 수, 데이터셋의 레이블 정보 등이 콘솔에 출력됩니다.