๐Ÿ‘š Fashion-MNIST ๐Ÿ‘–

parkeuยท2022๋…„ 9์›” 29์ผ
0

ABC๋ถ€ํŠธ์บ ํ”„

๋ชฉ๋ก ๋ณด๊ธฐ
36/55

Fashion-MNIST ๋ฐ์ดํ„ฐ์…‹

  • 60000๊ฐœ ํ•™์Šต ๋ฐ์ดํ„ฐ, 10000๊ฐœ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ
  • Tshirts&top, Trouser, Pullover, Dress, Coat, Sandal, Shirt, Sneaker, Bag, Ankle boot
  • ๋น„๊ต๋ฅผ ์œ„ํ•œ ๋‘๊ฐ€์ง€ ๋ชจ๋ธ ๊ตฌ์„ฑ
    -> (28,28) => 784 ๋Œ€์‹  Flatten()์ธต ์‚ฌ์šฉ

๋ฐ์ดํ„ฐ ์ค€๋น„ํ•˜๊ธฐ

from keras.datasets.fashion_mnist import load_data

(X_train, y_train), (X_test, y_test) = load_data()
print(X_train.shape, X_test.shape)

๋ฐ์ดํ„ฐ ๊ทธ๋ ค๋ณด๊ธฐ

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(777)

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker','Bag','Ankle boot']
sample_size = 9
random_idx = np.random.randint(60000, size=sample_size)

plt.figure(figsize=(5,5))
for i, idx in enumerate(random_idx):
  plt.subplot(3,3,i+1)
  plt.xticks([])
  plt.yticks([])
  plt.imshow(X_train[idx], cmap='gray')
  plt.xlabel(class_names[y_train[idx]])
plt.show()

์ „์ฒ˜๋ฆฌ ๋ฐ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์…‹ ๋งŒ๋“ค๊ธฐ

1) ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ 0 ~ 255 -> 0 ~ 1 ์‚ฌ์ด๋กœ ์Šค์ผ€์ผ๋ง(MinMax ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ)

X_train = X_train / 255
X_test = X_test / 255

2) ์‹ค์ œ ์ •๋‹ต์„ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š”(๋‹ค์ค‘๋ถ„๋ฅ˜) -> ์ˆ˜์น˜ํ˜•์„ ๋ฒ”์ฃผํ˜•์œผ๋กœ ๋ณ€๊ฒฝ

from tensorflow.keras.utils import to_categorical
# ์‹ค์ œ ์ •๋‹ต ๋น„๊ต๋ฅผ ์œ„ํ•ด 0 ~ 9 ์ •๋‹ต์ง€๋ฅผ ๋”ฐ๋กœ ์ €์žฅ
real_y_test = y_test

# ๋ ˆ์ด๋ธ” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฒ”์ฃผํ˜•์œผ๋กœ ๋ณ€๊ฒฝ
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

3) ํ›ˆ๋ จ / ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ 70:30 ๋น„์œจ๋กœ ๋ถ„๋ฆฌ

# 3) ํ›ˆ๋ จ / ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ 70:30 ๋น„์œจ๋กœ ๋ถ„๋ฆฌ
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.3, random_state=777)
X_train.shape

  • ์†์‹ค๊ฐ’์ด ๋†’์•„์ง€๋ ค๊ณ  ํ•จ -> ์ •๋‹ต์—์„œ ๋ฉ€์–ด์ง
  • ๋”ฅ๋Ÿฌ๋‹์—์„œ ๊นŠ๊ฒŒ ์ธต์„ ๋งŒ๋“œ๋Š”๊ฒŒ ๋ฌด์กฐ๊ฑด ์ข‹์€๊ฑด ์•„๋‹˜
  • ์ผ๋ฐ˜ํ™”๋œ ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์ค‘์š”
  • ๋ฐ์ดํ„ฐ์— ๋งž๋Š” ๋ชจ๋ธ ๋งŒ๋“ค์–ด์•ผ

์ฒซ๋ฒˆ์งธ ๋ชจ๋ธ ๊ตฌ์„ฑ

from keras.models import Sequential
from keras.layers import Dense, Flatten

first_model = Sequential()
first_model.add(Flatten(input_shape=(28,28))) # Flatten(28,28) -> (28*28) -> 1์ฐจ์› 784๋กœ ๋ณ€ํ™˜
first_model.add(Dense(64, activation='relu'))
first_model.add(Dense(32, activation='relu'))
first_model.add(Dense(10, activation='softmax'))

์ฒซ๋ฒˆ์งธ ๋ชจ๋ธ ์„ค์ •

first_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])

์ฒซ๋ฒˆ์งธ ๋ชจ๋ธ ํ•™์Šต

first_history = first_model.fit(X_train, y_train, epochs=30, batch_size=128, validation_data=(X_val, y_val))

๋‘๋ฒˆ์งธ ๋ชจ๋ธ

from keras.models import Sequential
from keras.layers import Dense, Flatten

second_model = Sequential()
second_model.add(Flatten(input_shape=(28,28))) # Flatten(28,28) -> (28*28) -> 1์ฐจ์› 784๋กœ ๋ณ€ํ™˜
second_model.add(Dense(128, activation='relu')) # ์ธต ํ•˜๋‚˜ ์ถ”๊ฐ€
second_model.add(Dense(64, activation='relu'))
second_model.add(Dense(32, activation='relu'))
second_model.add(Dense(10, activation='softmax'))

second_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])

second_history = second_model.fit(X_train, y_train, epochs=30, batch_size=128, validation_data=(X_val, y_val))

ํ•™์Šต ๊ฒฐ๊ณผ ๊ทธ๋ฆฌ๊ธฐ

import numpy as np
import matplotlib.pyplot as plt

def draw_loss_acc(history_1, history_2, epochs):
    his_dict_1 = history_1.history
    his_dict_2 = history_2.history
    keys = list(his_dict_1.keys())
    
    epochs = range(1, epochs)
    fig = plt.figure(figsize = (10, 10))
    ax = fig.add_subplot(1, 1, 1)
    # axis ์„ ๊ณผ ax์˜ ์ถ• ๋ ˆ์ด๋ธ”์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
    ax.spines['top'].set_color('none')
    ax.spines['bottom'].set_color('none')
    ax.spines['left'].set_color('none')
    ax.spines['right'].set_color('none')
    ax.tick_params(labelcolor='w', top=False, bottom=False, left=False, right=False)
    
    for i in range(len(his_dict_1)):
        temp_ax = fig.add_subplot(2, 2, i + 1)
        temp = keys[i%2]
        val_temp = keys[(i + 2)%2 + 2]
        temp_history = his_dict_1 if i < 2 else his_dict_2
        temp_ax.plot(epochs, temp_history[temp][1:], color = 'blue', label = 'train_' + temp)
        temp_ax.plot(epochs, temp_history[val_temp][1:], color = 'orange', label = val_temp)
        if(i == 1 or i == 3):
            start, end = temp_ax.get_ylim()
            temp_ax.yaxis.set_ticks(np.arange(np.round(start, 2), end, 0.01))
        temp_ax.legend()
    ax.set_ylabel('loss', size = 20)
    ax.set_xlabel('Epochs', size = 20)
    plt.tight_layout()
    plt.show()
    
draw_loss_acc(first_history, second_history, 30)

๋ชจ๋ธ ํ‰๊ฐ€

first_model.evaluate(X_test, y_test)
second_model.evaluate(X_test, y_test)

ํ•™์Šต ํšŸ์ˆ˜ 10์œผ๋กœ ์กฐ์ •


๋ชจ๋ธ ์˜ˆ์ธกํ•˜์—ฌ ๊ทธ๋ ค๋ณด๊ธฐ

import numpy as np

results = first_model.predict(X_test)
arg_results = np.argmax(results, axis = -1)

random_idx = np.random.randint(10000)

plt.figure(figsize=(5,5))

plt.imshow(X_test[random_idx], cmap='gray')
plt.title('Predicted value of the image : ' + class_names[arg_results[random_idx]] + ', Real value of the image :' + class_names[real_y_test[random_idx]])
plt.show()
  • ํ‹€๋ฆฐ๊ฒฝ์šฐ
  • ๋งž์€๊ฒฝ์šฐ

๋ชจ๋ธ ํ‰๊ฐ€ - ํ˜ผ๋™ํ–‰๋ ฌ

from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
results = first_model.predict(X_test)

plt.figure(figsize=(7,7))

cm = confusion_matrix(np.argmax(y_test, axis=-1), np.argmax(results, axis=-1)) # ์‹ค์ œ ์ •๋‹ต๊ณผ ๋น„๊ต
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('predicted label')
plt.ylabel('true label')
plt.show()


๊ฒฐ๊ณผ ํ•ด์„

  • ๋ชจ๋ธ์„ ๊นŠ๊ฒŒ ๊ตฌ์„ฑ -> ๋†’์€ ์„ฑ๋Šฅ, But ๊ณผ๋Œ€์ ํ•ฉ(ํŒŒ๋ผ๋ฏธํ„ฐ ์ˆ˜ ์ฆ๊ฐ€)
  • ๋ชจ๋ธ์˜ ๊นŠ์ด๋Š” ๋ฐ์ดํ„ฐ์— ์ ํ•ฉํ•˜๊ฒŒ ๊ฒฐ์ •ํ•ด์•ผ ํ•จ
  • ์œ ๋ช…ํ•œ ๋ฐ์ดํ„ฐ์…‹์ด๋‚˜ ์œ ์‚ฌ ๋ถ„์•ผ์—์„œ ๋†’์€ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค€ ๋ชจ๋ธ ๊ตฌ์กฐ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๊ตฌ์„ฑํ•ด๋ณด๊ณ  ์‹คํ—˜ ์ง„ํ–‰
profile
๋ฐฐ๊ณ ํŒŒ์šฉ.

0๊ฐœ์˜ ๋Œ“๊ธ€