본문 바로가기
수업(국비지원)/Python

[Python] Fashion-MNIST 데이터를 이용하여 예측 평가하기

by byeolsub 2023. 4. 27.

 📌

##################################################
# Fashion-MNIST 데이터셋 다운로드
from tensorflow.keras.datasets.fashion_mnist import load_data
(x_train, y_train), (x_test, y_test) = load_data()
print(x_train.shape,x_test.shape) #(60000, 28, 28) (10000, 28, 28)
class_names=['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
         'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
y_train[:10] # [9, 0, 0, 3, 0, 2, 7, 2, 5, 5]
x_train[0]
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize = (5, 5))
for i in range(9):
    plt.subplot(3, 3, i+1)
    plt.xticks([])
    plt.yticks([])
    plt.imshow(x_train[i], cmap = 'gray')
    plt.xlabel(class_names[y_train[i]])
plt.show()
# 이미지 데이터 정규화
x_train = x_train/255 # minmax 정규화 (x-min)/(max-min)
x_test = x_test/255

# 레이블을 onehot인코딩하기
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

# 검증데이터 분리. (훈련:검증)=(7:3)
x_train,x_val,y_train,y_val = \\
  train_test_split(x_train,y_train,test_size=0.3,random_state=777)

# 모델 구성
from tensorflow.keras.layers import Dense, Flatten
model1 = Sequential()

# Flatten :입력층
#          입력값을 1차원배열로 변경
#          28행28열 데이터를 28*28=784개의 1차원배열로 변경 입력
model1.add(Flatten(input_shape = (28, 28)))
model1.add(Dense(64, activation = 'relu'))
model1.add(Dense(32, activation = 'relu'))
model1.add(Dense(10, activation = 'softmax'))
model1.summary()
model1.compile(optimizer='adam', \\
       loss='categorical_crossentropy',metrics=['acc'])
history1 = model1.fit(x_train,y_train, epochs=30,
        batch_size=128, validation_data=(x_val,y_val))  
history1.history["loss"][29] # 0.19616620242595673
history1.history["acc"][29]  # 0.9278571605682373
history1.history["val_loss"][29] # 0.34093964099884033
history1.history["val_acc"][29] # 0.8887777924537659

# 테스트데이터의 손실함수값, 정확도 출력하기
# [0.38258302211761475, 0.8741999864578247]
model1.evaluate(x_test,y_test)

# 예측하기
results = model1.predict(x_test)
np.argmax(results[:10],axis=-1)
np.argmax(y_test[:10],axis=-1)
# 평가하기
# 혼동행렬 출력하기
cm=confusion_matrix(np.argmax(y_test,axis=-1),\\
                    np.argmax(results,axis=-1))
cm    

# 혼동행렬을 heatmap으로 출력하기
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize = (7, 7))
sns.heatmap(cm, annot = True, fmt = 'd',cmap = 'Blues')
plt.xlabel('predicted label', fontsize = 15)
plt.ylabel('true label', fontsize = 15)
plt.xticks(range(10),class_names,rotation=45)
plt.yticks(range(10),class_names,rotation=0)
plt.show()