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()