๐ŸงฉTensorflow Certification ์ทจ๋“ํ•˜๊ธฐ - part 3. ์‹ค์ „ (Fashion MNIST)

vincaยท2023๋…„ 1์›” 2์ผ
0

๐ŸŒ• AI/DL -Tenserflow Certification

๋ชฉ๋ก ๋ณด๊ธฐ
3/11

Fashion MNIST

  • Fully Connected Layer (Dense)๋ฅผ ํ™œ์šฉํ•œ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ (Image Classification)๋ฌธ์ œ

Create a classifier for the Fashion MNIST dataset
Note that the test will expect it to classify 10 classes and that
the input shape should be the native size of the Fashion MNIST dataset which is 28x28 monochrome.
Do not resize the data. Your input layer should accept
(28,28) as the input shape only.
If you amend this, the tests will fail.

Fashion MNIST ๋ฐ์ดํ„ฐ ์…‹์— ๋Œ€ํ•œ ๋ถ„๋ฅ˜๊ธฐ ์ƒ์„ฑ ํ…Œ์ŠคํŠธ๋Š” 10 ๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฅ˜ ํ•  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•˜๊ณ  ์ž…๋ ฅ ๋ชจ์–‘์€ Fashion MNIST ๋ฐ์ดํ„ฐ ์„ธํŠธ์˜ ๊ธฐ๋ณธ ํฌ๊ธฐ ์—ฌ์•ผํ•ฉ๋‹ˆ๋‹ค.28x28 ๋‹จ์ƒ‰.
๋ฐ์ดํ„ฐ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค. input_shape๋Š” (28,28)์„ ์ž…๋ ฅ ๋ชจ์–‘์œผ๋กœ ๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Solution

1. import ํ•˜๊ธฐ

ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ import ํ•ฉ๋‹ˆ๋‹ค.

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

from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import ModelCheckpoint

2. Load dataset

tf.keras.datasets ์—๋Š” ๋‹ค์–‘ํ•œ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•ด์ค๋‹ˆ๋‹ค.

fashion_mnist = tf.keras.datasets.fashion_mnist
(x_train, y_train), (x_valid, y_valid) = fashion_mnist.load_data()
x_train.shape, x_valid.shape
y_train.shape, y_valid.shape

3. ์ด๋ฏธ์ง€ ์ •๊ทœํ™” (Normalization)

  • ๋ชจ๋“  ์ด๋ฏธ์ง€ ํ”ฝ์…€(pixel)๊ฐ’๋“ค์„ 0~1 ์‚ฌ์ด์˜ ๊ฐ’์œผ๋กœ ์ •๊ทœํ™” ํ•ด ์ค๋‹ˆ๋‹ค.
  • x_train, x_valid ์— ๋Œ€ํ•ด์„œ๋งŒ ์ •๊ทœํ™”ํ•ฉ๋‹ˆ๋‹ค.

์ •๊ทœํ™”(Normalization) ์ „์˜ ์ตœ์†Œ๊ฐ’(min), ์ตœ๋Œ€๊ฐ’(max)์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

x_train.min(), x_train.max()

์ •๊ทœํ™”(Normalization) ํ•ฉ๋‹ˆ๋‹ค.

x_train = x_train / 255.0
x_valid = x_valid / 255.0

์ •๊ทœํ™” ํ›„ ์ตœ์†Œ๊ฐ’/์ตœ๋Œ€๊ฐ’ ํ™•์ธ

x_train.min(), x_train.max()

์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ Visualization

# ์‹œ๊ฐํ™”
fig, axes = plt.subplots(2, 5)
fig.set_size_inches(10, 5)

for i in range(10):
    axes[i//5, i%5].imshow(x_train[i], cmap='gray')
    axes[i//5, i%5].set_title(str(y_train[i]), fontsize=15)
    plt.setp( axes[i//5, i%5].get_xticklabels(), visible=False)
    plt.setp( axes[i//5, i%5].get_yticklabels(), visible=False)
    axes[i//5, i%5].axis('off')

plt.tight_layout()
plt.show()

  • 0: ํ‹ฐ์…”์ธ /ํƒ‘
  • 1: ๋ฐ”์ง€
  • 2: ํ’€์˜ค๋ฒ„(์Šค์›จํ„ฐ์˜ ์ผ์ข…)
  • 3: ๋“œ๋ ˆ์Šค
  • 4: ์ฝ”ํŠธ
  • 5: ์ƒŒ๋“ค
  • 6: ์…”์ธ 
  • 7: ์Šค๋‹ˆ์ปค์ฆˆ
  • 8: ๊ฐ€๋ฐฉ
  • 9: ์•ตํด ๋ถ€์ธ 

ํ™œ์„ฑํ•จ์ˆ˜ (relu, sigmoid, softmax)

from IPython.display import Image
import numpy as np
import matplotlib.pyplot as plt

4. ๋ชจ๋ธ ์ •์˜ (Sequential)

model = Sequential([
    # Flatten์œผ๋กœ shape ํŽผ์น˜๊ธฐ
    Flatten(input_shape=(28, 28)),
    # Dense Layer
    Dense(1024, activation='relu'),
    Dense(512, activation='relu'),
    Dense(256, activation='relu'),
    Dense(128, activation='relu'),
    Dense(64, activation='relu'),
    # Classification์„ ์œ„ํ•œ Softmax 
    Dense(10, activation='softmax'),
])

5. ์ปดํŒŒ์ผ (compile)

  1. optimizer๋Š” ๊ฐ€์žฅ ์ตœ์ ํ™”๊ฐ€ ์ž˜๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ธ 'adam'์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  2. loss์„ค์ •
  • ์ถœ๋ ฅ์ธต activation์ด sigmoid ์ธ ๊ฒฝ์šฐ: binary_crossentropy
  • ์ถœ๋ ฅ์ธต activation์ด softmax ์ธ ๊ฒฝ์šฐ:
    • ์›ํ•ซ์ธ์ฝ”๋”ฉ(O): categorical_crossentropy
    • ์›ํ•ซ์ธ์ฝ”๋”ฉ(X): sparse_categorical_crossentropy)
  1. metrics๋ฅผ 'acc' ํ˜น์€ 'accuracy'๋กœ ์ง€์ •ํ•˜๋ฉด, ํ•™์Šต์‹œ ์ •ํ™•๋„๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])

6. ModelCheckpoint: ์ฒดํฌํฌ์ธํŠธ ์ƒ์„ฑ

val_loss ๊ธฐ์ค€์œผ๋กœ epoch ๋งˆ๋‹ค ์ตœ์ ์˜ ๋ชจ๋ธ์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•˜์—ฌ, ModelCheckpoint๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

  • checkpoint_path๋Š” ๋ชจ๋ธ์ด ์ €์žฅ๋  ํŒŒ์ผ ๋ช…์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • ModelCheckpoint์„ ์„ ์–ธํ•˜๊ณ , ์ ์ ˆํ•œ ์˜ต์…˜ ๊ฐ’์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
checkpoint_path = "my_checkpoint.ckpt"
checkpoint = ModelCheckpoint(filepath=checkpoint_path, 
                             save_weights_only=True, 
                             save_best_only=True, 
                             monitor='val_loss', 
                             verbose=1)

7. ํ•™์Šต (fit)

  1. validation_data๋ฅผ ๋ฐ˜๋“œ์‹œ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  2. epochs์„ ์ ์ ˆํ•˜๊ฒŒ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  3. callbacks์— ๋ฐ”๋กœ ์œ„์—์„œ ๋งŒ๋“  checkpoint๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
history = model.fit(x_train, y_train,
                    validation_data=(x_valid, y_valid),
                    epochs=20,
                    callbacks=[checkpoint],
                   )

8. ํ•™์Šต ์™„๋ฃŒ ํ›„ Load Weights (ModelCheckpoint)

ํ•™์Šต์ด ์™„๋ฃŒ๋œ ํ›„์—๋Š” ๋ฐ˜๋“œ์‹œ load_weights๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด, ์—ด์‹ฌํžˆ ModelCheckpoint๋ฅผ ๋งŒ๋“  ์˜๋ฏธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

# checkpoint ๋ฅผ ์ €์žฅํ•œ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
model.load_weights(checkpoint_path)

๊ฒ€์ฆ์ด ํ•˜๊ณ ์‹ถ๋‹ค๋ฉด..

model.evaluate(x_valid, y_valid)
profile
๋ถ‰์€ ๋ฐฐ ์˜ค์ƒ‰ ๋”ฑ๋‹ค๊ตฌ๋ฆฌ ๊ฐœ๋ฐœ์ž ๐ŸฆƒCloud & DevOps

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