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

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

๐ŸŒ• AI/DL -Tenserflow Certification

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

Cats vs Dogs transfer learning

VGG16์„ ์ด์šฉํ•œ ์ „์ดํ•™์Šต์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • Convolution Neural Network (ํ•ฉ์„ฑ๊ณฑ ์‹ ๊ฒฝ๋ง)๋ฅผ ํ™œ์šฉํ•œ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ (Image Classification)

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
from tensorflow.keras.applications import VGG16

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๋Š” 224 X 224 ์ปฌ๋Ÿฌ์‚ฌ์ง„์ธ (224, 224, 3)์œผ๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  2. transfer learning ๊ธฐ๋ฒ•์„ ํ†ตํ•ด VGG16 ๋ชจ๋ธ์„ ํ™œ์šฉํ•œ ์ „์ดํ•™์Šต ๋ชจ๋ธ์„ ์™„์„ฑํ•ฉ๋‹ˆ๋‹ค.
  3. ์ถœ๋ ฅ์ธต์€ class ๊ฐฏ์ˆ˜ 2๊ฐœ์˜ ๋‰ด๋Ÿฐ์ด ์š”๊ตฌ๋ฉ๋‹ˆ๋‹ค.
transfer_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
transfer_model.trainable=False
model = Sequential([
    transfer_model,
    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. metrics๋ฅผ 'acc' ํ˜น์€ 'accuracy'๋กœ ์ง€์ •ํ•˜๋ฉด, ํ•™์Šต์‹œ ์ •ํ™•๋„๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ „์ฒ˜๋ฆฌ ๋‹จ๊ณ„์—์„œ one-hot encoding ์„ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ, categorical_crossentropy๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

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)

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๊ฐœ์˜ ๋Œ“๊ธ€