๐Ÿ“’ [ TIL ] 2022.05.20_24์ผ์ฐจ # ์‚ฌ๋ฌผ์ธ์‹ ๋จธ์‹ ๋Ÿฌ๋‹(3)

๋ฌธ๋ช…์ฃผยท2022๋…„ 5์›” 20์ผ
0
post-thumbnail

[ 2022-05-20 (๊ธˆ) ์˜ค๋Š˜์˜ TIL ]

[ Today Project ]

  • ์‚ฌ๋ฌผ์ธ์‹ ๋จธ์‹ ๋Ÿฌ๋‹
    : ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ๋Š” ํŒ€ํ”„๋กœ์ ํŠธ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

[ Today Learn ]

  • ๋‘๋ฒˆ์งธ ์‚ฌ๋ฌผ์ธ์‹ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ
  • ์•„๋‚˜์ฝ˜๋‹คํ™œ์šฉํ•œ ์‚ฌ๋ฌผ์ธ์‹ ๊ธฐ๋Šฅ
  • ๊ฐ•์•„์ง€์™€ ๊ณ ์–‘์ด ์‚ฌ๋ฌผ์ธ์‹ํ•˜๊ธฐ

โœ๏ธ ๋‚ด๊ฐ€ ๋ฐฐ์šด๊ฒƒ, ์–ป์€๊ฒƒ

  • ๋ฐ์ดํ„ฐ ์ฆ๊ฐ•๊ธฐ๋ฒ• ํ™œ์šฉํ•˜๊ธฐ

โ“ ๋ฐ์ดํ„ฐ ์ฆ๊ฐ•๊ธฐ๋ฒ•์ด๋ž€ ?
๊ฐ–๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์…‹์„ ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ augmentํ•˜์—ฌ ์‹ค์งˆ์ ์ธ ํ•™์Šต ๋ฐ์ดํ„ฐ์…‹์˜ ๊ทœ๋ชจ๋ฅผ ํ‚ค์šธ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•

๐Ÿงฉ ์ ์šฉ ์˜ˆ์‹œ

train_datagen = ImageDataGenerator(
  rescale=1./255, # ์ผ๋ฐ˜ํ™”
  rotation_range=10, # ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ํšŒ์ „ (๋‹จ์œ„: ๋„, 0-180)
  zoom_range=0.1, # ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฏธ์ง€ ํ™•๋Œ€ (%)
  width_shift_range=0.1,  # ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜ํ‰์œผ๋กœ ์ด๋™ (%)
  height_shift_range=0.1,  # ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜์ง์œผ๋กœ ์ด๋™ (%)
  horizontal_flip=True # ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜ํ‰์œผ๋กœ ๋’ค์ง‘๊ธฐ
)
  • Cats & Dogs ๋ถ„๋ฅ˜ํ•˜๊ธฐ

์ด๋ฒˆ ํŒ€ ํ”„๋กœ์ ํŠธ๋Š” ๊ฐ•์•„์ง€์˜ ์ข…์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐ์ดํ„ฐ์…‹์„ ํ•™์Šต์‹œ์ผฐ๋Š”๋ฐ, ์ด๋ฒˆ์—๋Š” ๊ณ ์–‘์ด์™€ ๊ฐ•์•„์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ์…‹์„ ํ•™์Šต์‹œ์ผฐ๋‹ค.

๐Ÿงฉ ์ ์šฉ ์˜ˆ์‹œ

์ผ๋‹จ ์•„๋ž˜์˜ ํŒŒ์ผ์„ kaggle์—์„œ ๋‹ค์šด๋ฐ›์•˜๊ณ , ํ•„์ˆ˜์ ์œผ๋กœ ๋‹ค์šด๋ฐ›์•„์•ผ ํ•  ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ•™์Šต๋ฐ์ดํ„ฐ์˜๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ์ฆ๊ฐ•๊ธฐ๋ฒ•์„ ํ™œ์šฉํ•ด ์ •ํ™•๋„๋ฅผ ๋†’์ด๊ณ , ํ…Œ์ŠคํŠธ๋ฐ์ดํ„ฐ๋Š” ์ผ๋ฐ˜ํ™”์‹œ์ผฐ๋‹ค.

train_datagen = ImageDataGenerator(
  rescale=1./255, # ์ผ๋ฐ˜ํ™”
  rotation_range=10, # ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ํšŒ์ „ (๋‹จ์œ„: ๋„, 0-180)
  zoom_range=0.1, # ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฏธ์ง€ ํ™•๋Œ€ (%)
  width_shift_range=0.1,  # ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜ํ‰์œผ๋กœ ์ด๋™ (%)
  height_shift_range=0.1,  # ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜์ง์œผ๋กœ ์ด๋™ (%)
  horizontal_flip=True # ๋žœ๋คํ•˜๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์ˆ˜ํ‰์œผ๋กœ ๋’ค์ง‘๊ธฐ
)

test_datagen = ImageDataGenerator(
  rescale=1./255 # ์ผ๋ฐ˜ํ™”
)

train_gen = train_datagen.flow_from_directory(
  'test_set/test_set',
  target_size=(256, 256), # (height, width)
  batch_size=32,
  seed=2021,
  class_mode='binary',
  shuffle=True
)

test_gen = test_datagen.flow_from_directory(
  'training_set/training_set',
  target_size=(256, 256), # (height, width)
  batch_size=32,
  seed=2021,
  class_mode='binary',
  shuffle=False
)

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

from pprint import pprint
pprint(train_gen.class_indices)

๊ฐ€์ƒ๋ชจ๋ธ

model = Sequential([
    Conv2D(filters=32, kernel_size=3, padding='same', activation="relu", input_shape=(256, 256, 3)),
    MaxPooling2D(pool_size=(2, 2), strides=2),
    Conv2D(filters=64, kernel_size=3, padding='same', activation="relu"),
    MaxPooling2D(pool_size=(2, 2), strides=2),
    Conv2D(filters=128, kernel_size=3, padding='same', activation="relu"),
    MaxPooling2D(pool_size=(2, 2), strides=2),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(1, activation="sigmoid")
])

model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.001), metrics=['acc'])

ํ•™์Šต์‹œํ‚ค๊ธฐ

from tensorflow.keras.callbacks import ModelCheckpoint

history = model.fit(
    train_gen,
    validation_data=test_gen, # ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ํ•œ epoch์ด ๋๋‚ ๋•Œ๋งˆ๋‹ค ์ž๋™์œผ๋กœ ๊ฒ€์ฆ
    epochs=20, # epochs ๋ณต์ˆ˜ํ˜•์œผ๋กœ ์“ฐ๊ธฐ!
    callbacks=[
      ModelCheckpoint('model.h5', monitor='val_acc', verbose=1, save_best_only=True)
    ]
)

๐ŸŒฑ ๋Š๋‚€ ์ 

์˜ค๋Š˜์€ ๋กค์ฑ”ํ”ผ์–ธ์„ ๊ตฌ๋ถ„ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ํ•™์Šต์‹œ์ผœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํ”„๋ก ํŠธ์—”๋“œ๋ฅผ ์™„์„ฑํ–ˆ๊ณ , ๋‚˜๋Š” ๋กค๊ฒŒ์ž„์„ ํ•˜์ง€์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ํŒ€์›์„์ด ๊ฐ ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ง์ ‘ ์‚ฌ์ง„์„ ์ฐ์–ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์•„ ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•™์Šต์‹œ์ผฐ๋‹ค. ํ•œ ํ”„๋กœ์ ํŠธ ๊ธฐ๊ฐ„๋™์•ˆ ๋‘ ๊ฐœ์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•œ ์ผ์ด ์ฒ˜์Œ์ด๋ผ์„œ ๋†€๋ผ์› ๋‹ค.. ์›๋ž˜๋Š” ์ •ํ•ด์ง„ ํ”„๋กœ์ ํŠธ๊ธฐ๊ฐ„์ด ์งง๊ฒŒ๋Š๊ปด์งˆ์ •๋„์—ฌ์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์™„์„ฑํ•˜๋Š”๋ฐ์— ๋ฒ„๊ฑฐ์› ์—ˆ๋Š”๋ฐ! ์ด๋ฒˆ ํ”„๋กœ์ ํŠธ ๊ธฐ๊ฐ„๋™์•ˆ ํ•˜๋ฃจ์— ์ •ํ•ด๋†“์•˜๋˜ ๊ณ„ํš๋ณด๋‹ค ๋” ์ผ์ฐ ๋๋‚ผ ์ˆ˜ ์žˆ์–ด์„œ ์ƒ๋‹นํžˆ ์—ฌ์œ ๋กœ์› ๋‹ค. ๋‹ค์Œ ํ”„๋กœ์ ํŠธ๋Š” '๋„ทํ”Œ๋ฆญ์Šค'๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋กœ ์•Œ๊ณ ์žˆ๋Š”๋ฐ, ๋„ˆ๋ฌด ๊ธฐ๋Œ€๊ฐ€ ๋œ๋‹ค.

๐ŸŽ ์ž์„ธํ•œ ์ฝ”๋“œ๋Š” colab ์— ๊ฒŒ์‹œํ–ˆ์Šต๋‹ˆ๋‹ค

profile
ํ•˜๋ฃจ ํ•œ๊ฑธ์Œ์”ฉ ๊พธ์ค€ํžˆ ๋‚˜์•„๊ฐ€๋Š” ๊ฐœ๋ฐœ์ž๐Ÿ™†โ€โ™€๏ธ https://github.com/Moonmooj

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

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด