Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

AI study/๋”ฅ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ

[keras] CNN ๋ชจ๋ธ - ImageDataGenerator ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

๊ฐ์ž ๐Ÿฅ” 2021. 3. 8. 17:39
๋ฐ˜์‘ํ˜•

์‹œ์ž‘ํ•˜๋ฉฐ

TF ๊ณต๋ถ€๊ฐ€ ์ƒ๊ฐ๋ณด๋‹ค ๋„ˆ๋ฌด ์˜ค๋ž˜๊ฑธ๋ ค!! ์บ๊ธ€, ๊นƒํ—ˆ๋ธŒ ๋“ฑ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉฐ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ–ˆ์—ˆ๋˜ ๋‚ด๊ฐ€ ๊ธฐ๋ณธ๊ธฐ๊ฐ€ ๋งŽ์ด ๋ถ€์กฑํ–ˆ์Œ์„ ๋Š๊ผˆ๋‹ค... ํ•˜๋‚˜ํ•˜๋‚˜ ์ดํ•ดํ•˜๊ณ  ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ๋„˜์–ด๊ฐ€๋ ค๋‹ˆ๊นŒ ๋๋„ ์—†๋‹ค. 
์–ด์จŒ๋“ , CNN๋ชจ๋ธ์„ ๋งŒ๋“ค๊ณ , ImageGenerator์„ ํ™œ์šฉํ•œ ์˜ˆ์ œ๋ฅผ ๊นŒ๋จน์ง€ ์•Š๊ธฐ ์œ„ํ•ด ํฌ์ŠคํŒ… ํ•ด๋ณผ๊นŒ ํ•œ๋‹ค. ์‹œ์ž‘!

 

1. import library

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์— ํ•„์š”ํ•œ library๋ฅผ import

import tensorflow as tf
import os
from os import path, getcwd, chdir
from tensorflow.keras.optimizers imort RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator

 

2. callbacks ํด๋ž˜์Šค ์ž‘์„ฑ

์ผ์ •ํ•œ accuracy ๋‹ฌ์„ฑ ํ›„ ๋ชจ๋ธ training์„ ๋ฉˆ์ถฐ์ฃผ๋Š” ๊ธฐ๋Šฅ์ธ callbacks๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— callbacks ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค.

class MyCallBack(tf.keras.callbacks.Callback):
	def on_epoch_end(self, epoch, logs={}):
    	if(logs.get('accuracy') >= 0.95):
        	print("\n====Reached 95% accuracy, stop training====")
            self.stop_training = True
            
callbacks = MyCallBack()

 

3. ๋ชจ๋ธ ์ƒ์„ฑ

3๊ฐœ์˜ conv2d, maxpooling layer๋ฅผ ๊ฐ€์ง„ ๋ชจ๋ธ์„ ๊ตฌ์„ฑํ•  ๊ฒƒ์ด๊ณ , ์ตœ์ข… ๋ถ„๋ฅ˜๋Š” 0๊ณผ 1์‚ฌ์ด์˜ ํ™•๋ฅ ๊ฐ’์œผ๋กœ ์ถœ๋ ฅํ•  ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋งˆ์ง€๋ง‰ Dense layer์˜ node์ˆ˜๋Š” 1๊ฐœ์ด๊ณ , sigmoid ํ™œ์„ฑํ™”ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ–ˆ๋‹ค.

model = tf.keras.Sequential([
          tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape = (300, 300, 3)),
          tf.keras.layers.MaxPooling2D(2,2),
          tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
          tf.keras.layers.MaxPooling2D(2,2),
          tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
          tf.keras.layers.MaxPooling2D(2,2),
          tf.kers.Flatten(),
          tf.keras.layers.Dense(512, activation='relu'),
          tf.keras.layers.Dense(64, activation='relu'),
          tf.keras.layers.Dense(1, activation'sigmoid')	
	])

model.summary()

 

3. ๋ชจ๋ธ ์ปดํŒŒ์ผ (์ตœ์ ํ™”, ์†์‹คํ•จ์ˆ˜ ์„ ํƒ)

model.compile(loss='binary_crossentrophy',
		optimizer=RMSprop(lr=0.001),
                metrics=['accuracy'])
  • RMSprop ๋ž€?
    • Adagrad ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ž๋™์œผ๋กœ ํ•™์Šต๋ฅ (learning rate)๋ฅผ ์„ค์ •ํ•˜๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ ํ•™์Šต๋ฅ ์ด ๊ธ‰๊ฒฉํ•˜๊ฒŒ ๊ฐ์†Œํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.
    • ์™œ ์ด ์˜ตํ‹ฐ๋งˆ์ด์ €๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”์ง€๋Š” ์ฐจ์ฐจ ์•Œ์•„๊ฐ€๋„๋กํ•˜๊ณ , ์ผ๋‹จ ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ๋Š” ์˜ˆ์ œ์— ์žˆ๋Š”๋Œ€๋กœ ๊ณต๋ถ€ํ–ˆ๋‹ค. ใ…‹ใ…Ž


4. ImageDataGenerator์„ ํ™œ์šฉํ•˜์—ฌ data ํ˜•์„ฑ

train_datagen = ImageDataGenerator(rescale = 1./255)

train_generator = train_datagen.flow_from_directory(
	'/desktop/mycomputer/', #train data๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๊ฒฝ๋กœ
        target_size = (300, 300),
        batch_size = 10,
        class_mode = 'binary'
    )
  • flow_from_directory ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋ฉด์„œ, ํŒŒ์ผ๋ช…์ด ๋ผ๋ฒจ์ด ๋˜๋„๋ก ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ๋„์™€์ค€๋‹ค.
    • ์ด๋ ‡๊ฒŒ train ํด๋” ๊ฒฝ๋กœ ์•ˆ์— dogs์™€ cats ๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊ทธ ์•ˆ์— ์žˆ๋Š” ์‚ฌ์ง„๋“ค์˜ label์€ dogs์™€ cats๊ฐ€ ๋œ๋‹ค.

 

5. model fitting (fit_generatorํ•จ์ˆ˜)

model.fit_generator(
	train_generator,
        steps_per_epoch=10, #ํ•œ ๋ฒˆ์˜ epoch๋ฅผ ๋Œ ๋•Œ, ๋ฐ์ดํ„ฐ๋ฅผ ๋ช‡ ๋ฒˆ ๋ณผ ๊ฒƒ์ธ๊ฐ€
        epochs=15,
        verbose=1,
        callbacks=[callbacks]
        # validation_data = val_data
        # validation_steps = 2000
        )
  • validation data๊ฐ€ ์žˆ์œผ๋ฉด ๋งจ ์•„๋ž˜ val_data์— ์ฑ„์›Œ์ฃผ๋ฉด ๋œ๋‹ค.
  • validation_steps : ํ•œ ๋ฒˆ์˜ epoch๊ฐ€ ๋Œ๊ณ  ๋‚œ ํ›„, val_data๋กœ accuracy๋ฅผ ์ธก์ •ํ•  ๋•Œ, val_data๋ฅผ ๋ช‡ ๋ฒˆ ๋ณผ ๊ฒƒ์ด๋ƒ
  • steps_per_epoch ์˜ ์ˆ˜ ๊ฒฐ์ • : train_data ์ˆ˜ / train_generator์— ์ž…๋ ฅํ•œ batch_size ์ˆ˜ 

 

6. test set ์˜ˆ์ธก

predict = model.predict_generator(test_data, steps=5)
print(test_data.class_indices)
print(predict)
  • test_data ๋Š” ์•ž์„œ train_datagen, train_generator ๋งŒ๋“  ๋ฐฉ์‹๊ณผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.
  • ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•๋ฅ ๊ฐ’์œผ๋กœ ๋‚˜์˜จ๋‹ค. 

์ฐธ๊ณ ๋ฌธํ—Œ3months.tistory.com/199

 

Keras - CNN ImageDataGenerator ํ™œ์šฉํ•˜๊ธฐ

Keras - CNN ImageDataGenerator ํ™œ์šฉํ•˜๊ธฐ keras์—์„œ๋Š” ์ด๋ฏธ์ง€๋ฐ์ดํ„ฐ ํ•™์Šต์„ ์‰ฝ๊ฒŒํ•˜๋„๋ก ํ•˜๊ธฐ์œ„ํ•ด ๋‹ค์–‘ํ•œ ํŒจํ‚ค์ง€๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ๊ทธ ์ค‘ ํ•˜๋‚˜๊ฐ€ ImageDataGenerator ํด๋ž˜์Šค์ด๋‹ค. ImageDataGenerator ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๊ฐ..

3months.tistory.com

 

๋งˆ์น˜๋ฉฐ

ImageDataGenerator์„ ์‚ฌ์šฉํ•  ์ค„ ์•Œ์•„์•ผ TF ์‹œํ—˜์‘์‹œ๊ฐ€๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•ธ๋“œ๋ถ์— ๋‚˜์™€์žˆ๋‹ค. ์ •ํ™•ํ•œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์ตํžˆ๊ณ  ์‹œํ—˜์— ์‘์‹œํ•ด์„œ ๊ผญ ํ•ฉ๊ฒฉํ•˜๊ฒ ๋‹ค.

๋ฐ˜์‘ํ˜•