๐ŸงฉTensorflow Certification ์ทจ๋“ํ•˜๊ธฐ - part 6. ์‹ค์ „ (Cats vs Dogs ๋ถ„๋ฅ˜)

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

๐ŸŒ• AI/DL -Tenserflow Certification

๋ชฉ๋ก ๋ณด๊ธฐ
6/11
post-thumbnail

Cats vs Dogs ๋ถ„๋ฅ˜

  • Convolution Neural network ํ™œ์šฉํ•œ ๋ถ„๋ฅ˜ ๋ชจ๋ธ (Classification)
  • tensorflow-datasets ๋ฅผ ํ™œ์šฉํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ

Computer Vision with CNNs
For this exercise you will build a cats v dogs classifier
using the Cats v Dogs dataset from TFDS.
Be sure to use the final layer as shown
(Dense, 2 neurons, softmax activation)
The testing infrastructre will resize all images to 224x224
with 3 bytes of color depth. Make sure your input layer trains
images to that specification, or the tests will fail.
Make sure your output layer is exactly as specified here, or the
tests will fail.

์ด ์—ฐ์Šต์—์„œ๋Š” cats v dogs ๋ถ„๋ฅ˜๊ธฐ๋ฅผ ๋งŒ๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. TFDS์˜ Cats v Dogs ๋ฐ์ดํ„ฐ ์„ธํŠธ ์‚ฌ์šฉ.
๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ตœ์ข… ๋ ˆ์ด์–ด๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค
(Dense, ๋‰ด๋Ÿฐ 2 ๊ฐœ, activation='softmax')
ํ…Œ์ŠคํŠธ ์ธํ”„๋ผ๋Š” ๋ชจ๋“  ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ 224x224๋กœ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค(์ปฌ๋Ÿฌ์‚ฌ์ง„). ์ž…๋ ฅ ๋ ˆ์ด์–ด๋ฅผ ํ™•์ธํ•˜์‹ญ์‹œ์˜ค


Solution

์ˆœ์„œ ์š”์•ฝ

  1. import: ํ•„์š”ํ•œ ๋ชจ๋“ˆ import
  2. ์ „์ฒ˜๋ฆฌ: ํ•™์Šต์— ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  3. ๋ชจ๋ธ๋ง(model): ๋ชจ๋ธ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  4. ์ปดํŒŒ์ผ(compile): ๋ชจ๋ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  5. ํ•™์Šต (fit): ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ต๋‹ˆ๋‹ค.

1. import ํ•˜๊ธฐ

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

import tensorflow_datasets as tfds
import tensorflow as tf

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

2.1 ์ „์ฒ˜๋ฆฌ (Load dataset)

tensorflow-datasets๋ฅผ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.

์‹œํ—˜์—์„œ ์ฃผ์–ด์ง€๋Š” ๋ฐ์ดํ„ฐ์…‹ ๋กœ๋“œ ํ˜•ํƒœ

  • ์˜ˆ์ „ ๋ฐฉ์‹์ด๋ฏ€๋กœ ์•„๋ž˜์ฒ˜๋Ÿฌ ์ฃผ์–ด์ง€๋Š” ์ฝ”๋“œ๋ฅผ ๊ณผ๊ฐํžˆ ์‚ญ์ œ ํ›„, ์•„๋ž˜ ์ œ๊ณต๋˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
# ๋ฐ์ดํ„ฐ์…‹ ๋‹ค์šด๋กœ๋“œ ์˜ค๋ฅ˜ (๋งํฌ ๊นจ์ง ํ•ด๊ฒฐ)
setattr(tfds.image_classification.cats_vs_dogs, '_URL',"https://download.microsoft.com/download/3/E/1/3E1C3F21-ECDB-4869-8368-6DEBA77B919F/kagglecatsanddogs_5340.zip")
dataset_name = 'cats_vs_dogs'

# ์ฒ˜์Œ 80%์˜ ๋ฐ์ดํ„ฐ๋งŒ ์‚ฌ์šฉ
train_dataset = tfds.load(name=dataset_name, split='train[:80%]')

# ์ตœ๊ทผ 20%์˜ ๋ฐ์ดํ„ฐ๋งŒ ์‚ฌ์šฉ
valid_dataset = tfds.load(name=dataset_name, split='train[80%:]')

2.2 ์ „์ฒ˜๋ฆฌ (Define Folder)

์‹œํ—˜์—์„œ ์š”๊ตฌํ•˜๋Š” ์ „์ฒ˜๋ฆฌ ์š”๊ตฌ ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ์ด๋ฏธ์ง€ ์ •๊ทœํ™” (Normalization)
  2. ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ ๋งž์ถ”๊ธฐ: (224 X 224)
  3. image(x), label(y)๋ฅผ ๋ถ„ํ• 
def preprocess(data):
    # x, y ๋ฐ์ดํ„ฐ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
    x = data['image']
    y = data['label']
    # image ์ •๊ทœํ™”(Normalization)
    x = x / 255
    # ์‚ฌ์ด์ฆˆ๋ฅผ (224, 224)๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    x = tf.image.resize(x, size=(224, 224))
    # x, y  ๋ฐ์ดํ„ฐ๋ฅผ return ํ•ฉ๋‹ˆ๋‹ค.
    return x, y

๋งŒ๋“  ์ „์ฒ˜๋ฆฌ ํ•จ์ˆ˜(preprocessing)๋ฅผ dataset์— mappingํ•˜๊ณ , batch_size๋„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

batch_size=32
train_data = train_dataset.map(preprocess).batch(batch_size)
valid_data = valid_dataset.map(preprocess).batch(batch_size)

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

์ด์ œ Modeling์„ ํ•  ์ฐจ๋ก€์ž…๋‹ˆ๋‹ค.

Sequential ๋ชจ๋ธ ์•ˆ์—์„œ ์ธต์„ ๊นŠ๊ฒŒ ์Œ“์•„ ์˜ฌ๋ ค ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

  1. input_shape๋Š” (height, width, color_channel)์ž…๋‹ˆ๋‹ค. cats vs dogs ๋ฌธ์ œ์—์„œ๋Š” (224, 224, 3) ์ด ๋ฉ๋‹ˆ๋‹ค.
  2. ๊นŠ์€ ์ถœ๋ ฅ์ธต๊ณผ ๋” ๋งŽ์€ Layer๋ฅผ ์Œ“์Šต๋‹ˆ๋‹ค.
  3. Dense Layer์— activation='relu'๋ฅผ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.
  4. ๋ถ„๋ฅ˜(Classification)์˜ ๋งˆ์ง€๋ง‰ ์ธต์˜ ์ถœ๋ ฅ ์ˆซ์ž๋Š” ๋ถ„๋ฅ˜ํ•˜๊ณ ์ž ํ•˜๋Š” ํด๋ž˜์Šค ๊ฐฏ์ˆ˜์™€ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.
model = Sequential([
    Conv2D(64, (3, 3), input_shape=(224, 224, 3), activation='relu'),
    MaxPooling2D(2, 2),
    Conv2D(64, (3, 3), activation='relu'),
    MaxPooling2D(2, 2),
    Conv2D(128, (3, 3), activation='relu'),
    MaxPooling2D(2, 2),
    Conv2D(128, (3, 3), activation='relu'),
    MaxPooling2D(2, 2),
    Conv2D(256, (3, 3), activation='relu'),
    MaxPooling2D(2, 2),
    Flatten(),
    Dropout(0.5),
    Dense(512, activation='relu'),
    Dense(128, activation='relu'),
    Dense(2, activation='softmax'),
])
])

๋ชจ๋ธ ๊ฒฐ๊ณผ ์š”์•ฝ

model.summary()

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

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

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)

5. ํ•™์Šต (fit)

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

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

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

# checkpoint ๋ฅผ ์ €์žฅํ•œ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.
model.load_weights(checkpoint_path)
profile
๋ถ‰์€ ๋ฐฐ ์˜ค์ƒ‰ ๋”ฑ๋‹ค๊ตฌ๋ฆฌ ๊ฐœ๋ฐœ์ž ๐ŸฆƒCloud & DevOps

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