반응형
Fashion Mnist 데이터셋 확인
1
2
3
4
5
6
7
|
from tensorflow.keras.datasets import fashion_mnist
# 전체 6만개 데이터 중, 5만개는 학습 데이터용, 1만개는 테스트 데이터용으로 분리
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
# image size는 28x28의 grayscale 2차원 데이터
print("train dataset shape:", train_images.shape, train_labels.shape)
print("test dataset shape:", test_images.shape, test_labels.shape)
|
cs |
- 훈련 데이터 60,000개
- 테스트 데이터 10,000 개
- 데이터는 28x28 사이즈의 grayscale 2차원 데이터
Dense Layer 기반, Functional API로 모델 생성
1. 전처리 및 인코딩 함수 생성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import numpy as np
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
# 전처리 및 인코딩 함수 생성
# Step 1
def get_preprocessed_data(images, labels):
images = np.array(images/255.0, dtype=np.float32)
labels = np.array(labels, dtype=np.float32)
return images, labels
# Step 2
def get_preprocessed_ohe(images, labels):
images, labels = get_preprocessed_data(images, labels)
# One_Hot_Encoding 적용
oh_labels = to_categorical(labels)
return images, oh_labels
# Stem 3
def get_train_valid_test_set(train_images, train_labels, test_images, test_labels, valid_size=0.15, random_state=2021):
train_images, train_oh_labels = get_preprocessed_ohe(train_images, train_labels)
test_images, test_oh_labels = get_preprocessed_ohe(test_images, test_labels)
# 학습 데이터를 검증 데이터 세트로 다시 분리
tr_images, val_images, tr_oh_labels, val_oh_labels = train_test_split(train_images, train_oh_labels, test_size=valid_size, random_state=random_state)
return (tr_images, tr_oh_labels), (val_images, val_oh_labels), (test_images, test_oh_labels )
|
cs |
1). load한 데이터가 grayscale 2ckdnjs 데이터로 0 ~ 255 범위의 픽셀값을 0~1 사이의 값으로 변환함수 생성
2). 예측하고자 하는 결과값이 9개의 class 이므로 1)에서 변환한 label 값에 One_Hot_Encoding 적용함수 생성
3). 학습, 검증, 테스트 데이터셋 전처리, One_Hot_Encoding 적용 후 반환함수 생성
2. 데이터 재로딩 및 학습, 검증, 테스트 데이터셋 전처리, One_Hot_Encoding 적용 후 반환
1
2
3
4
5
6
7
8
9
10
11
12
|
from tensorflow.keras.datasets import fashion_mnist
# Fashion MNIST 데이터 재 로딩
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
print(train_images.shape, train_labels.shape, test_images.shape, test_labels.shape)
# 전처리 적용하여 학습/검증/데이터셋 생성
(tr_images, tr_oh_labels), (val_images, val_oh_labels), (test_images, test_oh_labels) = \
get_train_valid_test_set(train_images, train_labels, test_images, test_labels, valid_size=0.15, random_state=2021)
print(tr_images.shape, tr_oh_labels.shape, val_images.shape, val_oh_labels.shape, test_images.shape, test_labels.shape)
|
cs |
3. Model 생성 및 Optimizer, Loss, Metric 설정 및 적용
1
2
3
4
5
6
7
|
from tensorflow.keras.optimizers import Adam
# Model 생성 및 optimizer, loss, metric 적용
model = create_model()
model.summary()
model.compile(optimizer=Adam(0.001), loss='categorical_crossentropy', metrics=['accuracy'])
|
cs |
- 예측값이 multiclass 이기 때문에 loss는 'categorical_crossentopy 적용
4. 학습수행
1
2
3
|
# 학습 수행.
history = model.fit(x=tr_images, y=tr_oh_labels, batch_size=128, epochs=20, validation_data=(val_images, val_oh_labels))
|
cs |
4. 결과 시각화
1
2
3
4
5
6
7
8
9
|
import matplotlib.pyplot as plt
%matplotlib inline
def show_history(history):
plt.plot(history.history['accuracy'], label='train')
plt.plot(history.history['val_accuracy'], label='valid')
plt.legend()
show_history(history)
|
cs |
5. 모델 성능 검증
1
2
|
model.evaluate(test_images, test_oh_labels, batch_size=256, verbose=1)
|
cs |
반응형
'AI' 카테고리의 다른 글
[논문리뷰]Word2Vec (0) | 2022.09.05 |
---|---|
[ML]회귀(Regression) (0) | 2022.05.20 |
객체지향 프로그래밍-Object Oriented Programming(OOP) (0) | 2022.04.27 |
[논문리뷰]StarGAN (0) | 2022.01.18 |
Data Augmentation (0) | 2022.01.13 |
댓글