ImageDataGenerator

ImageDataGenerator ์ด์šฉํ•˜๋ฉด ์ž๋™์œผ๋กœ label ๋ถ€์—ฌํ•ด์ค€๋‹ค.

 

from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1./255)
trian_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=(300, 300),
    batch_size=128,
    class_mode='binary'
)
  • train_dir
    • ์—ฌ๊ธฐ์„œ train_dir๋Š” ์ด๋ฏธ์ง€๊ฐ€ ํฌํ•จ๋œ ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ํฌํ•จํ•œ ์ƒ์œ„ ๋””๋ ‰ํ† ๋ฆฌ์—ฌ์•ผ ํ•œ๋‹ค.
      (์œ„์˜ ์ด๋ฏธ์ง€์—์„œ trian์— ๋Œ€์‘ํ•˜๋Š” ์œ„์น˜)
    • train_dir์˜ ํ•˜์œ„ ๋””๋ ‰ํ† ๋ฆฌ ์ด๋ฆ„์€ label์˜ ์ด๋ฆ„์ด์—ฌ์•ผ ํ•œ๋‹ค.

  • target_size = (300, 300)
    • ์—ฌ๊ธฐ์„œ channel size ์ง€์ •ํ•ด์ค„ ํ•„์š” X
    • ์‹ ๊ฒฝ๋ง์„ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ ๋™์ผํ•œ ํฌ๊ธฐ์—ฌ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ด์ฆˆ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    • ์ด๋ฏธ์ง€๊ฐ€ ๋กœ๋“œ๋  ๋•Œ ์‚ฌ์ด์ฆˆ๋ฅผ ์กฐ์ ˆํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์ „์ฒ˜๋ฆฌ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • batch_size
    • ๋ฐฐ์น˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ง€์ •ํ•ด์ค€๋‹ค.
  • class_mode
    • "binary": 1D numpy array of binary labels,
    • "categorical": 2D numpy array of one-hot encoded labels. Supports multi-label output.
    • "input": images identical to input images (mainly used to work with autoencoders),
    • "multi_output": list with the values of the different columns,
    • "raw": numpy array of values in y_col column(s),
    • "sparse": 1D numpy array of integer labels, - None, no targets are returned (the generator will only yield batches of image data, which is useful to use in model.predict()).
  • ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ train_generator์™€ validation_generator์˜ target_size๊ฐ€ ๋™์ผํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

ConvNet ์ •์˜

model = tf.keras.models.Sequential([
    # 1st CONV+POOL
    tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(300, 300, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    # 2nd CONV+POOL
    tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2, 2),
    # 3rd CONV+POOL
    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2, 2),
    # Flatten
    tf.flatten(),
    # 1st DENSE
    tf.keras.layers.Dense(512, activation='relu'),
    # 2nd DENSE
    tf.keras.layers.Dense(1, activation='sigmoid') # binary classification
])

 

  • input_shape
    • channel ์‚ฌ์ด์ฆˆ๋„ ๊ธฐ์žฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 
    • (300, 300, 3) : row, col, channel ์ˆœ์„œ
  • ๋งˆ์ง€๋ง‰ layer์˜ output node
    • binary classification ๋ฌธ์ œ์˜€๊ธฐ ๋•Œ๋ฌธ์— unit์˜ ์ˆ˜๋ฅผ 1์œผ๋กœ ์„ค์ •ํ•˜๊ณ 
    • activation function ์—ญ์‹œ sigmoid๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.
    • ๋งŒ์•ฝ์— multi-class classification์ด๋ผ๋ฉด class์˜ ๊ฐœ์ˆ˜๋กœ units์˜ ์ˆ˜๋ฅผ ์„ค์ •ํ•ด์ฃผ๊ณ ,
      activation function์œผ๋กœ๋Š” 'softmax'๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

from tensorflow.keras.optimizers import RMSProp

model.compile(loss='binary_crossentropy',
              optimizer=RMSProp(lr=0.001),
              metrics=['acc'])
  • binary classification์ด๊ธฐ ๋•Œ๋ฌธ์— loss๋Š” 'binary_crossentropy' ์‚ฌ์šฉํ•˜์˜€๋‹ค.
  • multi-class classification ๋ฌธ์ œ๋ผ๋ฉด 'categorical_crossentropy'  ํ˜น์€ 'sparse_categorical_crossentropy' loss๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ํ•œ๋‹ค.
    • y๊ฐ€ one-hot encoding์ด ๋˜์–ด์žˆ๋Š” ์ƒํƒœ๋ผ๋ฉด categorical_crossentropy๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ,
    • y๊ฐ€ one-hot encoding์ด ๋˜์–ด์žˆ์ง€ ์•Š์€ ์ƒํƒœ๋ผ๋ฉด sparse_categorical_crossentropy๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

history = model.fit_generator(
    train_generator,
    steps_per_epoch=8,
    epochs=15,
    validation_data=validation_generator,
    validation_steps=8,
    verbose=2
)

 

  • model.fit ๋Œ€์‹  model.fit_generator ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.
  • training directory์— 1,024์žฅ์˜ ์ด๋ฏธ์ง€๊ฐ€ ์กด์žฌํ•˜๊ณ , batch_size๋Š” 128๋กœ ์ง€์ •ํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—
    steps_per_epoch๋Š” 1024/128 = 8์œผ๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.
  • validation directory์— 256์žฅ์˜ ์ด๋ฏธ์ง€๊ฐ€ ์กด์žฌํ•˜๊ณ , batch_size๋Š” 32๊ฐœ๋กœ ์ง€์ •ํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—
    validation_steps๋Š” 256/32=8์œผ๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.
  • verbose๋Š” tranining๋™์•ˆ ์–ผ๋งˆ๋‚˜ ์ž์ฃผ ์ถœ๋ ฅํ•  ๊ฒƒ์ธ์ง€๋ฅผ ์„ค์ •ํ•˜๋Š” parameter์ด๋‹ค.

 


Compressing images

 

  • ๋ณธ ๊ฐ•์˜์˜ ๋‚ด์šฉ์—์„œ๋Š” 300*300*3 ํฌ๊ธฐ์˜ horse, human ์ด๋ฏธ์ง€๋กœ ํ•™์Šต์„ ์ˆ˜ํ–‰ํ–ˆ๋‹ค.
  • ํ•˜์ง€๋งŒ, 150*150*3์˜ ํฌ๊ธฐ๋กœ ์ˆ˜์ •๋œ ์ด๋ฏธ์ง€๋กœ ํ•™์Šตํ•œ๋‹ค๋ฉด ์–ด๋–ค ์ผ์ด ์ผ์–ด๋‚ ๊นŒ?
  • (image generator์—์„œ ํฌ๊ธฐ์™€ ๋ชจ๋ธ์˜ input_size๋ฅผ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค)
  • ํฌ๊ธฐ๊ฐ€ ์ž‘์•„์ง€๋‹ˆ ํ•™์Šตํ•  ๋•Œ์˜ ๊ณ„์‚ฐ ๋น„์šฉ์ด ์ค„์–ด๋“ ๋‹ค. ํ•˜์ง€๋งŒ ์ •ํ™•๋„ ๋˜ํ•œ ๋‚ฎ์•„์กŒ๋‹ค.
    (์ž‘์€ ์‚ฌ์ด์ฆˆ์˜ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด layer์˜ ๊ฐœ์ˆ˜๋ฅผ ์ค„์˜€๊ธฐ ๋•Œ๋ฌธ์—)

 

github.com/lmoroney/dlaicourse

 

lmoroney/dlaicourse

Notebooks for learning deep learning. Contribute to lmoroney/dlaicourse development by creating an account on GitHub.

github.com

 

+ Recent posts