แ„‚ ๐Ÿ˜„ [12 ์ผ์ฐจ] : FUNDAMENTAL

๋ฐฑ๊ฑดยท2022๋…„ 1์›” 21์ผ
0

์‚ฌ์ง„ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๊ธฐ

๋ฆฌ๋ทฐ


  • ๋จธ์‹ ๋Ÿฌ๋‹ ๋˜๋Š” ๋”ฅ๋Ÿฌ๋‹์„ ํ™œ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„๋ฅ˜(classification)ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ดค๋‹ค.
  • ํ…์„œํ”Œ๋กœ์šฐ(TensorFlow)๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ๋ฉฐ, ์ด๋ฅผ ํ™œ์šฉํ•ด์„œ ๋จธ์‹ ๋Ÿฌ๋‹/๋”ฅ๋Ÿฌ๋‹ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ดค๋‹ค.
  • ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜๋ฅผ ํ•˜๋Š” CNN ๊ณ„์—ด์˜ Backbone ๋ชจ๋ธ์„ ๋‹ค๋ค„๋ณด์ง€ ์•Š์•˜๋‹ค.
  • python์˜ ๊ธฐ๋ณธ์ ์ธ ๋ฌธ๋ฒ•์„ ์•Œ๋ฉฐ, ํ•จ์ˆ˜ ๋˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค

ํ•™์Šต๋ชฉํ‘œ


  • ์ด๋ฏธ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•  ๋•Œ ์ด๋ฏธ ์ž˜ ํ•™์Šต๋˜์–ด ์žˆ๋Š” ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์•„์ด๋””์–ด๋ฅผ ๋– ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋‹ค.
  • ์‚ฌ์ „ํ•™์Šต ๋ชจ๋ธ์ธ Backbone ๋ชจ๋ธ์˜ ์ข…๋ฅ˜์™€ ๊ฐœ๋…์„ ์•Œ๊ณ , Transfer Learning์˜ ๊ฐœ๋…์„ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • VGG, ResNet๊ณผ ๊ฐ™์€ ๊ธฐ๋ณธ์ ์ธ Backbone ๋ชจ๋ธ์„ ๋ถˆ๋Ÿฌ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Backbone ๋ชจ๋ธ์„ ์›ํ•˜๋Š” ๋ ˆ์ด์–ด(layer)๋งŒํผ ์ƒˆ๋กœ ํ•™์Šต์‹œ์ผœ์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Backbone ๋ชจ๋ธ์„ Transfer Learning ์‹œํ‚ด์œผ๋กœ์จ ์›ํ•˜๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋ถ„๋ฅ˜์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

๋ชฉ์ฐจ


  • ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ๋ฌธ์ œ
  • ๋ชจ๋ธ์ด ํ•™์Šตํ•˜๋ ค๋ฉด? ๊ณต๋ถ€ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ค˜์•ผ์ง€!
  • ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ๋์€ ์•„๋‹ˆ์•ผ, ์ด์˜๊ฒŒ ๋‹ค๋“ฌ๋Š” ์ž‘์—…์€ ํ•„์ˆ˜!
  • ๋ฐ์ดํ„ฐ๊ฐ€ ์ค€๋น„๋˜์—ˆ์œผ๋‹ˆ, ์ด์ œ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด๋ณด์ž
  • ๋ชจ๋ธ์•„ ๋ชจ๋ธ์•„, ๋ฐ์ดํ„ฐ๋ฅผ ๋จน๊ณ  ๋˜‘๋˜‘ํ•ด์ง€๋ ด!
  • ๋ชจ๋ธ์€ ์–ผ๋งˆ๋‚˜ ๋˜‘๋˜‘ํ•ด์กŒ์„๊นŒ? ํ™•์ธํ•ด ๋ณด์ž!

ํ”„๋กœ์ ํŠธ


  • ๋ฐ์ดํ„ฐ์…‹ ์„ ํƒํ•˜๊ธฐ
  • ๋ฐ์ดํ„ฐ์…‹์„ ๋ชจ๋ธ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ์ค€๋น„ํ•˜๊ธฐ
  • ๋ชจ๋ธ ์„ค๊ณ„ํ•˜๊ธฐ
  • ๋ชจ๋ธ ํ•™์Šต์‹œํ‚ค๊ธฐ
  • ๋ชจ๋ธ ์„ฑ๋Šฅ ํ‰๊ฐ€ํ•˜๊ธฐ

์ค€๋น„


$ pip install pillow $

  • ๋ชจ๋ธ ํ™œ์šฉํ•˜๊ธฐ

๋ฒ„์ „ ๋“ฑ์˜ ์ด์œ ๋กœ ๊ฒฝ๊ณ  ๋ฉ”์„ธ์ง€๊ฐ€ ์ถœ๋ ฅ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๊ผญ ํ•„์š”ํ•œ ๋ฉ”์„ธ์ง€

import warnings
warnings.filterwarnings("ignore")

print("์™„๋ฃŒ!")
์™„๋ฃŒ!

ํ…์„œํ”Œ๋กœ์šฐ ํ™œ์šฉ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜.

import tensorflow as tf
print(tf.__version__)   # 2.6.0 ์œผ๋กœ ์ง„ํ–‰
2.6.0

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

  • ํ…์„œํ”Œ๋กœ์šฐ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ์…‹์„ ์‚ฌ์šฉ tensorflow_datasets ํŒจํ‚ค์ง€
import tensorflow_datasets as tfds

tfds.__version__
'4.4.0'

tensorflow_datasets์— ๊ด€ํ•œ ์„ธ๋ถ€ ๋‚ด์šฉ์„ ํ™•์ธ

  • tensorflow_datasets์—์„œ ์ œ๊ณต๋˜๋Š” ๋ฐ์ดํ„ฐ์…‹ 8rkwl
    - Audio, Image, Object_detection, Structured,
    Summarization, Text, Translate, Video

tensorflow_datasets ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ tfds๋กœ ๊ฐ€์ ธ์™”์œผ๋‹ˆ, ๊ทธ ์ค‘ cats_vs_dogs ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉ

# ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
(raw_train, raw_validation, raw_test), metadata = tfds.load(
    'cats_vs_dogs',
    split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'],
    with_info=True,
    as_supervised=True,
)
Downloading and preparing dataset 786.68 MiB (download: 786.68 MiB, generated: Unknown size, total: 786.68 MiB) to /aiffel/tensorflow_datasets/cats_vs_dogs/4.0.0...



Dl Completed...: 0 url [00:00, ? url/s]



Dl Size...: 0 MiB [00:00, ? MiB/s]



Generating splits...:   0%|          | 0/1 [00:00<?, ? splits/s]



Generating train examples...:   0%|          | 0/23262 [00:00<?, ? examples/s]


WARNING:absl:1738 images were corrupted and were skipped



Shuffling cats_vs_dogs-train.tfrecord...:   0%|          | 0/23262 [00:00<?, ? examples/s]


Dataset cats_vs_dogs downloaded and prepared to /aiffel/tensorflow_datasets/cats_vs_dogs/4.0.0. Subsequent calls will reuse this data.

๋ฐ์ดํ„ฐ ์…‹ ํ™•์ธ

print(raw_train)
print(raw_validation)
print(raw_test)
<PrefetchDataset shapes: ((None, None, 3), ()), types: (tf.uint8, tf.int64)>
<PrefetchDataset shapes: ((None, None, 3), ()), types: (tf.uint8, tf.int64)>
<PrefetchDataset shapes: ((None, None, 3), ()), types: (tf.uint8, tf.int64)>
  • ๋ชจ๋“  ๋ฐ์ดํ„ฐ์…‹์€ (image, label)์˜ ํ˜•ํƒœ
  • ((None, None, 3), ())
    • (None, None, 3)์€ image์˜ shape
    • ()๋Š” ์ •๋‹ต ์นดํ…Œ๊ณ ๋ฆฌ์ธ label์˜ shape
  • ์ด๋ฏธ์ง€๋Š” (height, width, channel)๋กœ 3์ฐจ์› ๋ฐ์ดํ„ฐ
    • ์‚ฌ์ด์ฆˆ๊ฐ€ ๋‹ค๋ฅด๋ฉด None์œผ๋กœ ๋‚˜์˜ด

๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ

๋ฐ์ดํ„ฐ ํ™•์ธ์„ ์œ„ํ•ด matplotlib ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๊ฐ€์ ธ์˜ด

import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

print("์Š~")
์Š~

raw_train ์•ˆ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ธฐ ์œ„ํ•ด tf.data.Dataset์—์„œ ์ œ๊ณตํ•˜๋Š” take๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ

  • ์ด ํ•จ์ˆ˜๋Š” ์ธ์ˆ˜๋กœ ๋ฐ›์€ ๋งŒํผ์˜ ๊ฐœ์ˆ˜๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ์…‹ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฆฌํ„ดํ•œ๋Š” ํ•จ์ˆ˜.

  • ๊ฐ•์•„์ง€๋Š” label 1๋กœ, ๊ณ ์–‘์ด๋Š” label 0์œผ๋กœ ์„ค์ •

๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ฌ ๋•Œ์—๋Š” ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ๋ฅผ ํ†ต์ผ์‹œ์ผœ ์ฃผ๋Š” ์ž‘์—…

  • ํƒ€์ž…์บ์ŠคํŒ…(Type Casting)
    • ํ˜•๋ณ€ํ™˜์ด๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” ํƒ€์ž…๊ฐœ์ŠคํŒ…์€ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ํ˜•(ํƒ€์ž…)์„ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜ํ˜•์„ ์‹ค์ˆ˜ํ˜•์œผ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด float()๋ฅผ ์‚ฌ์šฉ
IMG_SIZE = 160 # ๋ฆฌ์‚ฌ์ด์ง•ํ•  ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ

def format_example(image, label):
    image = tf.cast(image, tf.float32)  # image=float(image)๊ฐ™์€ ํƒ€์ž…์บ์ŠคํŒ…์˜  ํ…์„œํ”Œ๋กœ์šฐ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค.
    image = (image/127.5) - 1 # ํ”ฝ์…€๊ฐ’์˜ scale ์ˆ˜์ •
    image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
    return image, label

print("์Š~")
์Š~

์ด๋ฏธ์ง€์˜ ์‚ฌ์ด์ฆˆ๋ฅผ 160x160 ํ”ฝ์…€๋กœ ํ†ต์ผ์‹œํ‚ฌ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๊ฐ ํ”ฝ์…€๊ฐ’์˜ scale์„ ์ˆ˜์ •ํ•ด์ฃผ๋Š” ์—ญํ• 

train, validataion, test ๋ฐ์ดํ„ฐ์…‹์œผ๋กœ ๋ณ€ํ™˜

  • format_example() ํ•จ์ˆ˜๋ฅผ raw_train, raw_validation, raw_test ์— map() ํ•จ์ˆ˜๋กœ ์ ์šฉ์‹œ์ผœ์„œ ์›ํ•˜๋Š” ๋ชจ์–‘์˜ train, validataion, test ๋ฐ์ดํ„ฐ์…‹์œผ๋กœ ๋ณ€ํ™˜

  • ๋ฆฌ์ŠคํŠธ ์›์†Œ ์ „์ฒด์— ๋™์ผํ•œ ๋ณ€ํ™˜ ํ•จ์ˆ˜๋ฅผ for ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์†์‰ฝ๊ฒŒ ์ ์šฉํ•˜๊ฒŒ ํ•ด์ฃผ๋Š” map ํ•จ์ˆ˜ ์‚ฌ์šฉ

    ๋ฆฌ์ŠคํŠธ์— map ํ•จ์ˆ˜ ์ ์šฉ

train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
test = raw_test.map(format_example)

print(train)
print(validation)
print(test)
<MapDataset shapes: ((160, 160, 3), ()), types: (tf.float32, tf.int64)>
<MapDataset shapes: ((160, 160, 3), ()), types: (tf.float32, tf.int64)>
<MapDataset shapes: ((160, 160, 3), ()), types: (tf.float32, tf.int64)>

IMG_SIZE๋ฅผ 160์œผ๋กœ ์ง€์ •ํ•ด ์คŒ์œผ๋กœ์จ, ๋ชจ๋“  ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ (160, 160, 3)

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

get_label_name = metadata.features['label'].int2str

for idx, (image, label) in enumerate(raw_train.take(10)):  # 10๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ ์˜ต๋‹ˆ๋‹ค.
    plt.subplot(2, 5, idx+1)
    plt.imshow(image)
    plt.title(f'label {label}: {get_label_name(label)}')
    plt.axis('off')

์œ„์—๊บผ๊ฐ€ ์›๋ž˜ ์ด๋ฏธ์ง€ ์•„๋ž˜๊บผ๊ฐ€ ๋ฐ”๋€์ด๋ฏธ์ง€
plt.figure(figsize=(10, 5))


get_label_name = metadata.features['label'].int2str

for idx, (image, label) in enumerate(train.take(10)):
    plt.subplot(2, 5, idx+1)
    image = (image + 1) / 2
    plt.imshow(image)
    plt.title(f'label {label}: {get_label_name(label)}')
    plt.axis('off')

๊ท ์ผํ•ด์ง€๊ธฐ๋Š” ํ–ˆ๋Š”๋ฐ ๊ทธ๋ƒฅ ์žก์•„์„œ ๋Š˜๋ฆฌ๊ณ  ์ค„์ธ๊ฑด๋ฐ????? ๊ดœ์ฐฎ๋‚˜?????

ํ…์„œํ”Œ๋กœ์šฐ๋ฅผ ํ™œ์šฉํ•œ ํ•™์Šต์‹œํ‚ฌ ๋ชจ๋ธ ๋งŒ๋“ค๊ธฐ

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

print("์Š~")
์Š~
model = Sequential([
    Conv2D(filters=16, kernel_size=3, padding='same', activation='relu', input_shape=(160, 160, 3)),
    MaxPooling2D(),
    Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'),
    MaxPooling2D(),
    Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'),
    MaxPooling2D(),
    Flatten(),
    Dense(units=512, activation='relu'),
    Dense(units=2, activation='softmax')
])

print("์Š~")
์Š~
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 160, 160, 16)      448       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 80, 80, 16)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 80, 80, 32)        4640      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 40, 40, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 40, 40, 64)        18496     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 20, 20, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 25600)             0         
_________________________________________________________________
dense (Dense)                (None, 512)               13107712  
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 1026      
=================================================================
Total params: 13,132,322
Trainable params: 13,132,322
Non-trainable params: 0
_________________________________________________________________
  • 4๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ๋ ˆ์ด์–ด
    • Conv2D, MaxPooling2D, Flatten, Dense
  • ๊ฐ ๋ ˆ์ด์–ด๋ฅผ ์ง€๋‚˜๋ฉด์„œ ์ค„์–ด๋“ ๋‹ค
  • flatten ๊ณ„์ธต์—์„œ 1์ฐจ์›์˜ shape๊ฐ€ ์ค„์–ด๋“ ๋‹ค.
  • CNN ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์˜ ํŠน์ง•

2022-01-12fu14-01.png

๋งจ ์™ผ์ชฝ์ฒ˜๋Ÿผ ์ด๋ฏธ์ง€ ํ•œ ์žฅ์ด ์ž…๋ ฅ๋˜๋ฉด ๊ทธ ์ด๋ฏธ์ง€๋Š” Convolutional(ํ•ฉ์„ฑ๊ณฑ) ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๊ทธ ํ˜•ํƒœ๊ฐ€ ์ ์  ๊ธธ์ญ‰ํ•ด์ง€๋‹ค๊ฐ€, Flatten ๋ ˆ์ด์–ด๋ฅผ ๋งŒ๋‚˜๋ฉด ์˜ค๋ฅธ์ชฝ์ฒ˜๋Ÿผ ํ•œ ์ค„๋กœ ํŽด์ง‘๋‹ˆ๋‹ค. 3์ฐจ์›์˜ ์ด๋ฏธ์ง€๋ฅผ 1์ฐจ์›์œผ๋กœ

Flatten์„ ์กฐ๊ธˆ ๋” ์ง๊ด€์ ์œผ๋กœ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฐ์—ด์„ ๋‹ค๋ฃจ๊ธฐ ์šฉ์ดํ•œ numpy๋ฅผ ํ™œ์šฉ

import numpy as np

image = np.array([[1, 2], [3, 4]])
print(image.shape)
image
(2, 2)





array([[1, 2],
       [3, 4]])

์œ„์™€ ๊ฐ™์ด (2,2) ํฌ๊ธฐ์˜ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ์„ ๋•Œ ์ด๊ฒƒ์„ Flatten์‹œํ‚ค๋ฉด.
.

image.flatten()
array([1, 2, 3, 4])

๋ชจ๋“  ์ˆซ์ž๋ฅผ ์ผ๋ ฌ๋กœ ํŽธ ์ƒํƒœ๋กœ

๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์€ (160, 160, 3) ํฌ๊ธฐ์˜ 3์ฐจ์› ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์—ฌ๋Ÿฌ ๋ ˆ์ด์–ด๋ฅผ ๊ฑฐ์น˜๋ฉฐ ํ˜•ํƒœ๋ฅผ ๋ฐ”๊พธ๋‹ค๊ฐ€ ์ตœ์ข…์ ์œผ๋กœ๋Š” ๋ช‡ ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅํ•ด๋‚ด๋Š” ํ•จ์ˆ˜

๋ชจ๋ธ compile ์™„๋ฃŒ ํ›„ ํ•™์Šต์‹œํ‚ค๊ธฐ

๋ชจ๋ธ ํ•™์Šต

  • ๋ชจ๋ธ์ด ํ•™์Šตํ•˜๋Š” ๋ฐ์— ํ•„์š”ํ•œ "ํ•™์Šต๋ฅ "(learning_rate)์ด๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ 0.0001๋กœ ์„ค์ •ํ•ด์ฃผ๊ณ , ๋ชจ๋ธ์„ compile ํ•˜์—ฌ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜
learning_rate = 0.0001
model.compile(optimizer=tf.keras.optimizers.RMSprop(lr=learning_rate),
              loss=tf.keras.losses.sparse_categorical_crossentropy,
              metrics=['accuracy'])

print("์Š~")
์Š~

compile์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ์„ธ ๊ฐ€์ง€๊ฐ€

  • optimizer
    • ํ•™์Šต์„ ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์‹œํ‚ฌ ๊ฒƒ์ธ์ง€ ๊ฒฐ์ •
    • ์–ด๋–ป๊ฒŒ ์ตœ์ ํ™”์‹œํ‚ฌ ๊ฒƒ์ธ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ ํ™” ํ•จ์ˆ˜
  • loss
    • ๋ชจ๋ธ์ด ํ•™์Šตํ•ด๋‚˜๊ฐ€์•ผ ํ•˜๋Š” ๋ฐฉํ–ฅ์„ ๊ฒฐ์ •
    • ์ž…๋ ฅ๋ฐ›์€ ์ด๋ฏธ์ง€๊ฐ€ ๊ณ ์–‘์ด์ธ์ง€ ๊ฐ•์•„์ง€์ธ์ง€์— ๋Œ€ํ•œ ํ™•๋ฅ ๋ถ„ํฌ
    • ์ด๋ฏธ์ง€๊ฐ€ ๊ณ ์–‘์ด(label=0)์ผ ๊ฒฝ์šฐ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ์ด [1.0, 0.0]์— ๊ฐ€๊น๋„๋ก, ๊ฐ•์•„์ง€(label=1)์ผ ๊ฒฝ์šฐ [0.0, 1.0]์— ๊ฐ€๊นŒ์›Œ์ง€๋„๋ก ํ•˜๋Š” ๋ฐฉํ–ฅ์„ ์ œ์‹œ
  • metrics
    • ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•˜๋Š” ์ฒ™๋„
    • ๋ถ„๋ฅ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ, ์„ฑ๋Šฅ์„ ํ‰๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ์ง€ํ‘œ๋Š” ์ •ํ™•๋„(accuracy), ์ •๋ฐ€๋„(precision), ์žฌํ˜„์œจ(recall)

๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ฌ ๋ฐ์ดํ„ฐ ์ค€๋น„

  • ํ‚ฌ ๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜์ธ BATCH_SIZE์™€ ํ•™์Šต ๋ฐ์ดํ„ฐ๋ฅผ ์ ์ ˆํžˆ ์„ž์–ด์ค„
    SHUFFLE_BUFFER_SIZE๋ฅผ ์„ค์ •
BATCH_SIZE = 32
SHUFFLE_BUFFER_SIZE = 1000
print("์Š~")
์Š~
  • BATCH_SIZE์— ๋”ฐ๋ผ 32๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋žœ๋ค์œผ๋กœ ๋ฟŒ๋ ค์ค„ train_batches, validation_batches, test_batches๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค. train_batches๋Š” ๋ชจ๋ธ์ด ๋Š์ž„์—†์ด ํ•™์Šต๋  ์ˆ˜ ์žˆ๋„๋ก ์ „์ฒด ๋ฐ์ดํ„ฐ์—์„œ 32๊ฐœ๋ฅผ ๋žœ๋ค์œผ๋กœ ๋ฝ‘์•„ ๊ณ„์† ์ œ๊ณต
train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
validation_batches = validation.batch(BATCH_SIZE)
test_batches = test.batch(BATCH_SIZE)
print("์Š~")
์Š~

train_batches์—์„œ ํ•˜๋‚˜์˜ batch๋งŒ ๊บผ๋‚ด ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธ

for image_batch, label_batch in train_batches.take(1):
    pass

image_batch.shape, label_batch.shape
(TensorShape([32, 160, 160, 3]), TensorShape([32]))
# image_batch์˜ shape๋Š” [32, 160, 160, 3]์„, label_batch์˜ shape๋Š” [32]

image_batch์˜ shape๋Š” (160, 160, 3)์˜ shape์ธ 32๊ฐœ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. ์ฆ‰, ๋ฐ์ดํ„ฐ ํ•˜๋‚˜์˜ ํฌ๊ธฐ๋Š” (160, 160, 3)์ด๊ณ , ๊ทธ ๊ฐœ์ˆ˜๊ฐ€ 32๊ฐœ

label์€ ๊ฐ•์•„์ง€์ด๋ฉด 1, ๊ณ ์–‘์ด์ด๋ฉด 0์œผ๋กœ ์ •๋‹ต label์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ํ•œ batch์— ๋ฐ์ดํ„ฐ๊ฐ€ 32๊ฐœ๋ผ๋ฉด label์€ 0 ๋˜๋Š” 1์˜ 32๊ฐœ์˜ ์ˆซ์ž๋กœ๋งŒ ๊ตฌ์„ฑ

์ดˆ๊ธฐ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์€ ์–ด๋–ค์ง€ ํ™•์ธ

  • validation(๊ฒ€์ฆ)์„ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ์…‹์ธ validation_batches๋ฅผ ์ด์šฉํ•ด 20๋ฒˆ์˜ ์˜ˆ์ธก์„ ํ•ด ๋ณด๊ณ , ํ‰๊ท  loss์™€ ํ‰๊ท  accuracy๋ฅผ ํ™•์ธ
validation_steps = 20
loss0, accuracy0 = model.evaluate(validation_batches, steps=validation_steps)

print("initial loss: {:.2f}".format(loss0))
print("initial accuracy: {:.2f}".format(accuracy0))
11/20 [===============>..............] - ETA: 0s - loss: 0.6959 - accuracy: 0.4858

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9


20/20 [==============================] - 26s 31ms/step - loss: 0.6949 - accuracy: 0.5047
initial loss: 0.69
initial accuracy: 0.50


Corrupt JPEG data: 252 extraneous bytes before marker 0xd9

loss๋Š” ๋ง ๊ทธ๋Œ€๋กœ "์†์‹ค"์ด๋ผ๋Š” ๋œป์œผ๋กœ, ์–ผ๋งˆ๋‚˜ ๋ชจ๋ธ์ด ํ‹€๋ ธ๋Š”์ง€ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ loss๋Š” ๋‚ฎ์„์ˆ˜๋ก ์ข‹์€ ๊ฒƒ์ด์ฃ . ๋˜ํ•œ accuracy๋Š” ๋ช‡ ํผ์„ผํŠธ์˜ ์ •ํ™•๋„๋ฅผ ๋ณด์ด๋Š๋ƒ์— ๋Œ€ํ•œ ์ˆ˜์น˜์ž…๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ฐ•์•„์ง€์™€ ๊ณ ์–‘์ด๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋ ค๊ณ  ํ•˜๋Š”๋ฐ, ๋‘ ์žฅ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ฐ์–ด๋„ 50%๋Š” ๋‚˜์˜ฌ ํ…Œ๋‹ˆ ์ง€๊ธˆ ๋ชจ๋ธ์€ ์ „ํ˜€ ์˜๋ฏธ ์—†๋Š” ์˜ˆ์ธก์„ ํ•˜๋Š” ๊ฒƒ

10 epoch๋ฅผ ํ•™์Šต์‹œ์ผœ์„œ ์ •ํ™•๋„๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ•˜๋Š”์ง€ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋Š” ํ•™์Šต ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ์•ฝ 10~20๋ถ„ ๋‚ด์™ธ

EPOCHS = 10
history = model.fit(train_batches,
                    epochs=EPOCHS,
                    validation_data=validation_batches)
Epoch 1/10
171/582 [=======>......................] - ETA: 18s - loss: 0.6723 - accuracy: 0.5932

Corrupt JPEG data: 99 extraneous bytes before marker 0xd9


201/582 [=========>....................] - ETA: 17s - loss: 0.6652 - accuracy: 0.6028

Warning: unknown JFIF revision number 0.00


213/582 [=========>....................] - ETA: 16s - loss: 0.6634 - accuracy: 0.6037

Corrupt JPEG data: 396 extraneous bytes before marker 0xd9


285/582 [=============>................] - ETA: 13s - loss: 0.6488 - accuracy: 0.6197

Corrupt JPEG data: 65 extraneous bytes before marker 0xd9


493/582 [========================>.....] - ETA: 3s - loss: 0.6145 - accuracy: 0.6561

Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9


505/582 [=========================>....] - ETA: 3s - loss: 0.6120 - accuracy: 0.6583

Corrupt JPEG data: 128 extraneous bytes before marker 0xd9


516/582 [=========================>....] - ETA: 2s - loss: 0.6109 - accuracy: 0.6590

Corrupt JPEG data: 239 extraneous bytes before marker 0xd9


542/582 [==========================>...] - ETA: 1s - loss: 0.6073 - accuracy: 0.6632

Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9


550/582 [===========================>..] - ETA: 1s - loss: 0.6063 - accuracy: 0.6638

Corrupt JPEG data: 228 extraneous bytes before marker 0xd9


582/582 [==============================] - ETA: 0s - loss: 0.6021 - accuracy: 0.6680

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9


582/582 [==============================] - 32s 46ms/step - loss: 0.6021 - accuracy: 0.6680 - val_loss: 0.5294 - val_accuracy: 0.7506
Epoch 2/10
169/582 [=======>......................] - ETA: 17s - loss: 0.5214 - accuracy: 0.7378

Corrupt JPEG data: 99 extraneous bytes before marker 0xd9


201/582 [=========>....................] - ETA: 15s - loss: 0.5185 - accuracy: 0.7414

Warning: unknown JFIF revision number 0.00


213/582 [=========>....................] - ETA: 15s - loss: 0.5170 - accuracy: 0.7435

Corrupt JPEG data: 396 extraneous bytes before marker 0xd9


285/582 [=============>................] - ETA: 12s - loss: 0.5090 - accuracy: 0.7491

Corrupt JPEG data: 65 extraneous bytes before marker 0xd9


492/582 [========================>.....] - ETA: 3s - loss: 0.4906 - accuracy: 0.7606

Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9


506/582 [=========================>....] - ETA: 3s - loss: 0.4910 - accuracy: 0.7604

Corrupt JPEG data: 128 extraneous bytes before marker 0xd9


516/582 [=========================>....] - ETA: 2s - loss: 0.4899 - accuracy: 0.7612

Corrupt JPEG data: 239 extraneous bytes before marker 0xd9


544/582 [===========================>..] - ETA: 1s - loss: 0.4868 - accuracy: 0.7644

Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9


550/582 [===========================>..] - ETA: 1s - loss: 0.4872 - accuracy: 0.7641

Corrupt JPEG data: 228 extraneous bytes before marker 0xd9


580/582 [============================>.] - ETA: 0s - loss: 0.4848 - accuracy: 0.7657

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9


582/582 [==============================] - 27s 44ms/step - loss: 0.4848 - accuracy: 0.7658 - val_loss: 0.4952 - val_accuracy: 0.7584
Epoch 3/10
169/582 [=======>......................] - ETA: 17s - loss: 0.4497 - accuracy: 0.8014

Corrupt JPEG data: 99 extraneous bytes before marker 0xd9


201/582 [=========>....................] - ETA: 16s - loss: 0.4425 - accuracy: 0.8019

Warning: unknown JFIF revision number 0.00


213/582 [=========>....................] - ETA: 15s - loss: 0.4430 - accuracy: 0.8003

Corrupt JPEG data: 396 extraneous bytes before marker 0xd9


284/582 [=============>................] - ETA: 12s - loss: 0.4345 - accuracy: 0.8040

Corrupt JPEG data: 65 extraneous bytes before marker 0xd9


492/582 [========================>.....] - ETA: 3s - loss: 0.4231 - accuracy: 0.8095

Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9


506/582 [=========================>....] - ETA: 3s - loss: 0.4229 - accuracy: 0.8100

Corrupt JPEG data: 128 extraneous bytes before marker 0xd9


516/582 [=========================>....] - ETA: 2s - loss: 0.4223 - accuracy: 0.8102

Corrupt JPEG data: 239 extraneous bytes before marker 0xd9


543/582 [==========================>...] - ETA: 1s - loss: 0.4205 - accuracy: 0.8111

Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9


549/582 [===========================>..] - ETA: 1s - loss: 0.4204 - accuracy: 0.8112

Corrupt JPEG data: 228 extraneous bytes before marker 0xd9


581/582 [============================>.] - ETA: 0s - loss: 0.4183 - accuracy: 0.8117

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9


582/582 [==============================] - 27s 45ms/step - loss: 0.4182 - accuracy: 0.8118 - val_loss: 0.5108 - val_accuracy: 0.7554
Epoch 4/10
170/582 [=======>......................] - ETA: 18s - loss: 0.3974 - accuracy: 0.8208

Corrupt JPEG data: 99 extraneous bytes before marker 0xd9


202/582 [=========>....................] - ETA: 16s - loss: 0.3959 - accuracy: 0.8232

Warning: unknown JFIF revision number 0.00


212/582 [=========>....................] - ETA: 16s - loss: 0.3946 - accuracy: 0.8230

Corrupt JPEG data: 396 extraneous bytes before marker 0xd9


283/582 [=============>................] - ETA: 12s - loss: 0.3861 - accuracy: 0.8287

Corrupt JPEG data: 65 extraneous bytes before marker 0xd9


493/582 [========================>.....] - ETA: 3s - loss: 0.3710 - accuracy: 0.8370

Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9


505/582 [=========================>....] - ETA: 3s - loss: 0.3701 - accuracy: 0.8373

Corrupt JPEG data: 128 extraneous bytes before marker 0xd9


515/582 [=========================>....] - ETA: 2s - loss: 0.3690 - accuracy: 0.8377

Corrupt JPEG data: 239 extraneous bytes before marker 0xd9


543/582 [==========================>...] - ETA: 1s - loss: 0.3684 - accuracy: 0.8390

Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9


549/582 [===========================>..] - ETA: 1s - loss: 0.3690 - accuracy: 0.8386

Corrupt JPEG data: 228 extraneous bytes before marker 0xd9


581/582 [============================>.] - ETA: 0s - loss: 0.3667 - accuracy: 0.8396

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9


582/582 [==============================] - 27s 45ms/step - loss: 0.3667 - accuracy: 0.8395 - val_loss: 0.5042 - val_accuracy: 0.7562
Epoch 5/10
170/582 [=======>......................] - ETA: 17s - loss: 0.3392 - accuracy: 0.8524

Corrupt JPEG data: 99 extraneous bytes before marker 0xd9


202/582 [=========>....................] - ETA: 15s - loss: 0.3368 - accuracy: 0.8524

Warning: unknown JFIF revision number 0.00


212/582 [=========>....................] - ETA: 15s - loss: 0.3363 - accuracy: 0.8526

Corrupt JPEG data: 396 extraneous bytes before marker 0xd9


284/582 [=============>................] - ETA: 12s - loss: 0.3276 - accuracy: 0.8587

Corrupt JPEG data: 65 extraneous bytes before marker 0xd9


492/582 [========================>.....] - ETA: 3s - loss: 0.3172 - accuracy: 0.8657

Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9


506/582 [=========================>....] - ETA: 3s - loss: 0.3175 - accuracy: 0.8654

Corrupt JPEG data: 128 extraneous bytes before marker 0xd9


516/582 [=========================>....] - ETA: 2s - loss: 0.3165 - accuracy: 0.8658

Corrupt JPEG data: 239 extraneous bytes before marker 0xd9


544/582 [===========================>..] - ETA: 1s - loss: 0.3148 - accuracy: 0.8668

Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9


550/582 [===========================>..] - ETA: 1s - loss: 0.3144 - accuracy: 0.8672

Corrupt JPEG data: 228 extraneous bytes before marker 0xd9


580/582 [============================>.] - ETA: 0s - loss: 0.3125 - accuracy: 0.8677

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9


582/582 [==============================] - 27s 44ms/step - loss: 0.3123 - accuracy: 0.8678 - val_loss: 0.5062 - val_accuracy: 0.7743
Epoch 6/10
170/582 [=======>......................] - ETA: 17s - loss: 0.2838 - accuracy: 0.8801

Corrupt JPEG data: 99 extraneous bytes before marker 0xd9


202/582 [=========>....................] - ETA: 16s - loss: 0.2851 - accuracy: 0.8796

Warning: unknown JFIF revision number 0.00


212/582 [=========>....................] - ETA: 15s - loss: 0.2843 - accuracy: 0.8805

Corrupt JPEG data: 396 extraneous bytes before marker 0xd9


284/582 [=============>................] - ETA: 12s - loss: 0.2780 - accuracy: 0.8845

Corrupt JPEG data: 65 extraneous bytes before marker 0xd9


492/582 [========================>.....] - ETA: 3s - loss: 0.2680 - accuracy: 0.8888

Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9


506/582 [=========================>....] - ETA: 3s - loss: 0.2670 - accuracy: 0.8888

Corrupt JPEG data: 128 extraneous bytes before marker 0xd9


516/582 [=========================>....] - ETA: 2s - loss: 0.2663 - accuracy: 0.8895

Corrupt JPEG data: 239 extraneous bytes before marker 0xd9


544/582 [===========================>..] - ETA: 1s - loss: 0.2651 - accuracy: 0.8899

Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9


550/582 [===========================>..] - ETA: 1s - loss: 0.2643 - accuracy: 0.8905

Corrupt JPEG data: 228 extraneous bytes before marker 0xd9


580/582 [============================>.] - ETA: 0s - loss: 0.2630 - accuracy: 0.8904

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9


582/582 [==============================] - 27s 44ms/step - loss: 0.2633 - accuracy: 0.8903 - val_loss: 0.5072 - val_accuracy: 0.7829
Epoch 7/10
169/582 [=======>......................] - ETA: 17s - loss: 0.2425 - accuracy: 0.9042

Corrupt JPEG data: 99 extraneous bytes before marker 0xd9


201/582 [=========>....................] - ETA: 15s - loss: 0.2393 - accuracy: 0.9053

Warning: unknown JFIF revision number 0.00


213/582 [=========>....................] - ETA: 15s - loss: 0.2379 - accuracy: 0.9068

Corrupt JPEG data: 396 extraneous bytes before marker 0xd9


285/582 [=============>................] - ETA: 12s - loss: 0.2295 - accuracy: 0.9116

Corrupt JPEG data: 65 extraneous bytes before marker 0xd9


493/582 [========================>.....] - ETA: 3s - loss: 0.2236 - accuracy: 0.9123

Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9


505/582 [=========================>....] - ETA: 3s - loss: 0.2229 - accuracy: 0.9126

Corrupt JPEG data: 128 extraneous bytes before marker 0xd9


517/582 [=========================>....] - ETA: 2s - loss: 0.2222 - accuracy: 0.9131

Corrupt JPEG data: 239 extraneous bytes before marker 0xd9


543/582 [==========================>...] - ETA: 1s - loss: 0.2204 - accuracy: 0.9138

Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9


549/582 [===========================>..] - ETA: 1s - loss: 0.2201 - accuracy: 0.9139

Corrupt JPEG data: 228 extraneous bytes before marker 0xd9


581/582 [============================>.] - ETA: 0s - loss: 0.2192 - accuracy: 0.9142

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9


582/582 [==============================] - 26s 44ms/step - loss: 0.2191 - accuracy: 0.9142 - val_loss: 0.4732 - val_accuracy: 0.7966
Epoch 8/10
170/582 [=======>......................] - ETA: 17s - loss: 0.1912 - accuracy: 0.9316

Corrupt JPEG data: 99 extraneous bytes before marker 0xd9


202/582 [=========>....................] - ETA: 15s - loss: 0.1879 - accuracy: 0.9325

Warning: unknown JFIF revision number 0.00


212/582 [=========>....................] - ETA: 15s - loss: 0.1866 - accuracy: 0.9325

Corrupt JPEG data: 396 extraneous bytes before marker 0xd9


284/582 [=============>................] - ETA: 12s - loss: 0.1836 - accuracy: 0.9329

Corrupt JPEG data: 65 extraneous bytes before marker 0xd9


493/582 [========================>.....] - ETA: 3s - loss: 0.1747 - accuracy: 0.9358

Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9


505/582 [=========================>....] - ETA: 3s - loss: 0.1742 - accuracy: 0.9360

Corrupt JPEG data: 128 extraneous bytes before marker 0xd9


517/582 [=========================>....] - ETA: 2s - loss: 0.1738 - accuracy: 0.9360

Corrupt JPEG data: 239 extraneous bytes before marker 0xd9


543/582 [==========================>...] - ETA: 1s - loss: 0.1730 - accuracy: 0.9364

Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9


549/582 [===========================>..] - ETA: 1s - loss: 0.1728 - accuracy: 0.9365

Corrupt JPEG data: 228 extraneous bytes before marker 0xd9


581/582 [============================>.] - ETA: 0s - loss: 0.1714 - accuracy: 0.9368

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9


582/582 [==============================] - 27s 44ms/step - loss: 0.1714 - accuracy: 0.9368 - val_loss: 0.5210 - val_accuracy: 0.7966
Epoch 9/10
169/582 [=======>......................] - ETA: 17s - loss: 0.1495 - accuracy: 0.9482

Corrupt JPEG data: 99 extraneous bytes before marker 0xd9


202/582 [=========>....................] - ETA: 15s - loss: 0.1501 - accuracy: 0.9476

Warning: unknown JFIF revision number 0.00


212/582 [=========>....................] - ETA: 15s - loss: 0.1515 - accuracy: 0.9462

Corrupt JPEG data: 396 extraneous bytes before marker 0xd9


283/582 [=============>................] - ETA: 12s - loss: 0.1447 - accuracy: 0.9483

Corrupt JPEG data: 65 extraneous bytes before marker 0xd9


493/582 [========================>.....] - ETA: 3s - loss: 0.1369 - accuracy: 0.9519

Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9


505/582 [=========================>....] - ETA: 3s - loss: 0.1363 - accuracy: 0.9522

Corrupt JPEG data: 128 extraneous bytes before marker 0xd9


515/582 [=========================>....] - ETA: 2s - loss: 0.1356 - accuracy: 0.9523

Corrupt JPEG data: 239 extraneous bytes before marker 0xd9


543/582 [==========================>...] - ETA: 1s - loss: 0.1334 - accuracy: 0.9534

Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9


549/582 [===========================>..] - ETA: 1s - loss: 0.1332 - accuracy: 0.9535

Corrupt JPEG data: 228 extraneous bytes before marker 0xd9


581/582 [============================>.] - ETA: 0s - loss: 0.1325 - accuracy: 0.9539

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9


582/582 [==============================] - 27s 44ms/step - loss: 0.1325 - accuracy: 0.9538 - val_loss: 0.5836 - val_accuracy: 0.7928
Epoch 10/10
170/582 [=======>......................] - ETA: 17s - loss: 0.1139 - accuracy: 0.9601

Corrupt JPEG data: 99 extraneous bytes before marker 0xd9


202/582 [=========>....................] - ETA: 15s - loss: 0.1123 - accuracy: 0.9618

Warning: unknown JFIF revision number 0.00


212/582 [=========>....................] - ETA: 15s - loss: 0.1119 - accuracy: 0.9617

Corrupt JPEG data: 396 extraneous bytes before marker 0xd9


284/582 [=============>................] - ETA: 12s - loss: 0.1117 - accuracy: 0.9624

Corrupt JPEG data: 65 extraneous bytes before marker 0xd9


493/582 [========================>.....] - ETA: 3s - loss: 0.1040 - accuracy: 0.9649

Corrupt JPEG data: 2226 extraneous bytes before marker 0xd9


505/582 [=========================>....] - ETA: 3s - loss: 0.1033 - accuracy: 0.9653

Corrupt JPEG data: 128 extraneous bytes before marker 0xd9


517/582 [=========================>....] - ETA: 2s - loss: 0.1036 - accuracy: 0.9652

Corrupt JPEG data: 239 extraneous bytes before marker 0xd9


543/582 [==========================>...] - ETA: 1s - loss: 0.1018 - accuracy: 0.9660

Corrupt JPEG data: 1153 extraneous bytes before marker 0xd9


549/582 [===========================>..] - ETA: 1s - loss: 0.1015 - accuracy: 0.9660

Corrupt JPEG data: 228 extraneous bytes before marker 0xd9


581/582 [============================>.] - ETA: 0s - loss: 0.1013 - accuracy: 0.9663

Corrupt JPEG data: 162 extraneous bytes before marker 0xd9
Corrupt JPEG data: 252 extraneous bytes before marker 0xd9
Corrupt JPEG data: 214 extraneous bytes before marker 0xd9


582/582 [==============================] - 27s 44ms/step - loss: 0.1013 - accuracy: 0.9663 - val_loss: 0.6586 - val_accuracy: 0.7829

์˜ˆ์ธก ๊ฒฐ๊ณผํ™•์ธ

  • ๋ชจ๋‘ ํ•™์Šต์ด ๋˜์—ˆ๋‚˜์š”? ์ด 10 epoch๋ฅผ ํ•™์Šตํ•œ ํ›„, ์ •ํ™•๋„๊ฐ€ ์–ด๋Š ์ •๋„๊นŒ์ง€ ์˜ฌ๋ž๋‚˜

  • Accuracy

    • ์ฒซ ๋ฒˆ์งธ accuracy๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์ •ํ™•๋„์ž…๋‹ˆ๋‹ค. ํ•™์Šตํ•˜๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•๋„์ด์ฃ .
    • ๋‘ ๋ฒˆ์งธ val_accuracy๋Š” ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์ •ํ™•๋„์ž…๋‹ˆ๋‹ค. ํ•™์Šตํ•˜์ง€ ์•Š๊ณ  ์žˆ๋Š”, ์ฆ‰ ํ•ด๋‹น ํ•™์Šต ๋‹จ๊ณ„์—์„œ ๋ณด์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์ •ํ™•

๊ทธ๋ž˜ํ”„๋กœ ํ™•์ธ

  • 10 epoch๋ฅผ ๋ชจ๋‘ ํ•™์Šตํ•œ ํ›„์—๋Š” ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์ •ํ™•๋„๋Š” ์•ฝ 90% ๋‚จ์ง“, ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ accuracy๋Š” ์•ฝ 80% ์กฐ๊ธˆ ๋ชป ๋ฏธ์น˜๊ฒŒ ๋‚˜์™”์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ•™์Šต ๋‹จ๊ณ„์— ๋”ฐ๋ฅธ ์ •ํ™•๋„ ๋ณ€ํ™”๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ํ™•์ธ
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss=history.history['loss']
val_loss=history.history['val_loss']

epochs_range = range(EPOCHS)

plt.figure(figsize=(12, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend()
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend()
plt.title('Training and Validation Loss')
plt.show()

์ •ํ™•๋„(accuracy)์™€ ์†์‹ค๊ฐ’(loss)์— ๋Œ€ํ•œ ๋‘ ๊ฐ€์ง€ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ทธ๋ ค๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋‘ ๊ทธ๋ž˜ํ”„ ๋ชจ๋‘ ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์ˆ˜์น˜์™€ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์ˆ˜์น˜

2022-01-12fu14-02.png

์ด 10 epoch๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๋™์•ˆ training accuracy์™€ validation accuracy๋Š” ์–ด๋–ป๊ฒŒ ๋ณ€ํ™”ํ•˜๋‚˜์š”? ๊ฐ accuracy๊ฐ€ ๊ทธ๋ ‡๊ฒŒ ๋ณ€ํ•˜๋Š” ์ด์œ 

  • training accuracy๋Š” 10 epoch๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๋™์•ˆ ๊พธ์ค€ํžˆ ์ฆ๊ฐ€ํ•œ๋‹ค. ๋งˆ์ง€๋ง‰์—๋Š” ๊ฑฐ์˜ 90%๋ฅผ ๋„˜์–ด์„œ๋Š” ์ˆ˜์น˜๋ฅผ ๋ณด์ธ๋‹ค.

  • ๋ฐ˜๋ฉด, validation accuracy๋Š” ์ดˆ๋ฐ˜์— 75%~80% ์‚ฌ์ด๊นŒ์ง€๋Š” ์ฆ๊ฐ€ํ•˜์ง€๋งŒ ์ฆ๊ฐ€ํญ์ด training dataset์— ๋น„ํ•ด ๋งค์šฐ ์ ๊ณ , ์ฆ๊ฐ€ํ•˜๋Š” ์–‘์ƒ๋„ ๋ถˆ์•ˆ์ •ํ•˜๋‹ค.

  • training accuracy๋Š” ํ˜„์žฌ ํ•™์Šตํ•˜๋Š” ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์ •ํ™•๋„์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋ธ์˜ ๊ตฌ์กฐ๋‚˜ ๋ฐ์ดํ„ฐ์…‹ ๋“ฑ์— ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ํ•™์Šตํ•˜๋ฉด ํ• ์ˆ˜๋ก ๊พธ์ค€ํžˆ ๊ณ„์† ์˜ค๋ฅธ๋‹ค. ๋ฐ˜๋ฉด validation accuracy๋Š” ํ•™์Šตํ•˜์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์ •ํ™•๋„์ด๊ธฐ ๋•Œ๋ฌธ์— ์ผ์ •์ˆ˜์ค€๊นŒ์ง€ ์˜ค๋ฅธ ํ›„์—๋Š” ๊ณ„์† ์˜ค๋ฅผ์ง€ ์žฅ๋‹ดํ•  ์ˆ˜ ์—†๋‹ค.

  • training accuracy๋Š” ๊พธ์ค€ํžˆ ์˜ค๋ฅด์ง€๋งŒ validation accuracy๋Š” ์–ด๋–ค ํ•œ๊ณ„์„ ์„ ๋„˜์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ

  • loss ๊ทธ๋ž˜ํ”„์—์„œ training loss๋Š” ๊ณ„์† ์•ˆ์ •์ ์œผ๋กœ ์ค„์–ด๋“ค์ง€๋งŒ, validation loss๊ฐ’์€ ํŠน์ • ์ˆœ๊ฐ„ ์ดํ›„๋กœ ๋‹ค์‹œ ์ปค์ง€๋Š” ๋ชจ์Šต

  • ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ์นญํ•˜๋Š” ์šฉ์–ด๋Š”

๊ณผ์ ํ•ฉ(Overfitting, ์˜ค๋ฒ„ํ”ผํŒ…)

  • ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์ด ์ œ๋Œ€๋กœ ์˜ฌ๋ผ๊ฐ€๋ ค๋ฉด "ํ•™์Šตํ•˜์ง€ ์•Š์€" ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ๋„ ์„ฑ๋Šฅ์ด ์ข‹์•„์•ผ ํ•˜๋Š”๋ฐ, ํ›ˆ๋ จ ๋ฐ์ดํ„ฐ๋งŒ์œผ๋กœ ๊ณ„์† ํ•™์Šตํ•˜๋‹ค ๋ณด๋‹ˆ ๊ทธ ๋ฐ์ดํ„ฐ์—๋งŒ ๊ณผ๋„ํ•˜๊ฒŒ ์ ํ•ฉ(fitting) ๋˜์–ด์„œ ์ผ๋ฐ˜ํ™” ๋Šฅ๋ ฅ์ด ๋–จ์–ด์ง€๊ฒŒ ๋˜๋Š” ๊ฒƒ
  • ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ฌ ๋•Œ๋Š” ๊ณผ์ ํ•ฉ ๋ฌธ์ œ๋ฅผ ์˜ˆ๋ฏผํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ณ , ๊ณผ์ ํ•ฉ์ด ๋˜์ง€ ์•Š๋Š” ์ˆœ๊ฐ„์„ ์ž˜ ์žก์•„๋‚ด ์ผ๋ฐ˜ํ™”๊ฐ€ ์ž˜ ๋˜๋Š” ๋ชจ๋ธ๋กœ ํ•™์Šต์‹œํ‚ค๋Š” ๊ฒƒ์ด ์ค‘์š”
for image_batch, label_batch in test_batches.take(1):
    images = image_batch
    labels = label_batch
    predictions = model.predict(image_batch)
    pass

predictions
array([[9.9761641e-01, 2.3835611e-03],
       [5.9462464e-01, 4.0537530e-01],
       [1.3369282e-01, 8.6630714e-01],
       [9.9969792e-01, 3.0216199e-04],
       [5.5195206e-01, 4.4804800e-01],
       [8.3197273e-02, 9.1680270e-01],
       [2.3315031e-04, 9.9976689e-01],
       [8.1265157e-01, 1.8734843e-01],
       [1.0383534e-01, 8.9616460e-01],
       [5.6698918e-01, 4.3301082e-01],
       [5.5843964e-03, 9.9441564e-01],
       [9.9648523e-01, 3.5148002e-03],
       [9.9378610e-01, 6.2139085e-03],
       [8.3754794e-04, 9.9916244e-01],
       [9.9590296e-01, 4.0970314e-03],
       [9.9773228e-01, 2.2677223e-03],
       [2.1256688e-01, 7.8743309e-01],
       [4.8445108e-06, 9.9999511e-01],
       [5.8668250e-01, 4.1331753e-01],
       [9.9996424e-01, 3.5768771e-05],
       [3.8094068e-01, 6.1905932e-01],
       [6.3675117e-01, 3.6324883e-01],
       [9.9929571e-01, 7.0431735e-04],
       [9.3341851e-01, 6.6581517e-02],
       [2.9821005e-01, 7.0178992e-01],
       [1.7958991e-02, 9.8204100e-01],
       [9.9986172e-01, 1.3824736e-04],
       [1.5848826e-01, 8.4151173e-01],
       [9.6713167e-01, 3.2868307e-02],
       [9.4930458e-01, 5.0695356e-02],
       [9.7832263e-01, 2.1677295e-02],
       [5.2817100e-01, 4.7182900e-01]], dtype=float32)
  • predictions๊ฐ€ ์—„์ฒญ๋‚œ ์†Œ์ˆ˜์  ๊ฐ’๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ตฐ์š”. ์ด ๊ฐ’์€ ๋ชจ๋ธ์ด ํŒ๋‹จํ•œ [๊ณ ์–‘์ด์ผ ํ™•๋ฅ , ๊ฐ•์•„์ง€์˜ ํ™•๋ฅ ]์ธ๋ฐ, [1.0, 0.0]์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก label์ด 0์ธ ๊ณ ์–‘์ด๋กœ, [0.0, 1.0]์— ๊ฐ€๊นŒ์šธ์ˆ˜๋ก label์ด 1์ธ ๊ฐ•์•„์ง€๋กœ ์˜ˆ์ธกํ–ˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Œ

  • prediction ๊ฐ’๋“ค์„ ์‹ค์ œ ์ถ”๋ก ํ•œ ๋ผ๋ฒจ(๊ณ ์–‘์ด:0, ๊ฐ•์•„์ง€:1)๋กœ ๋ณ€ํ™˜ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋ชจ๋ธ์ด ๊ฐ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ•์•„์ง€๋ผ๊ณ  ํŒ๋‹จํ–ˆ๋Š”์ง€, ๊ณ ์–‘์ด๋กœ ํŒ๋‹จํ–ˆ๋Š”์ง€ ๋ณด๊ธฐ ์œ„ํ•ด

predictions = np.argmax(predictions, axis=1)
predictions
array([0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0,
       0, 0, 1, 1, 0, 1, 0, 0, 0, 0])
plt.figure(figsize=(20, 12))

for idx, (image, label, prediction) in enumerate(zip(images, labels, predictions)):
    plt.subplot(4, 8, idx+1)
    image = (image + 1) / 2
    plt.imshow(image)
    correct = label == prediction
    title = f'real: {label} / pred :{prediction}\n {correct}!'
    if not correct:
        plt.title(title, fontdict={'color': 'red'})
    else:
        plt.title(title, fontdict={'color': 'blue'})
    plt.axis('off')
count = 0   # ์ •๋‹ต์„ ๋งž์ถ˜ ๊ฐœ์ˆ˜
for image, label, prediction in zip(images, labels, predictions):
    correct = label == prediction
    if correct:
        count = count + 1
        
print(count / 32 * 100)
profile
๋งˆ์ผ€ํŒ…์„ ์œ„ํ•œ ์ธ๊ณต์ง€๋Šฅ ์„ค๊ณ„์™€ ์Šคํƒ€ํŠธ์—… Log

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