Introduction to TensorFlow for Artificial Intelligence, Machine Learning, and Deep Learning week4

han811·2020년 11월 8일
0
post-thumbnail

tf.keras.preprocessing.image를 활용하여 이미지 데이터를 generator로 불러오는 과정에 대한 내용이다.

먼저 zipfile모듈에 대한 간단한 언급을 하고 넘어가자

import os
import zipfile

local_zip = '/tmp/horse-or-human.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp/horse-or-human')
zip_ref.close()

위의 과정을 통해 zip파일의 경로만 안다면 해당 객체를 생성해주고 압출을 특정경로에 푸는 것까지 가능하다.

from tensorflow.keras.preprocessing.image import ImageDataGenerator

# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale=1/255)

# Flow training images in batches of 128 using train_datagen generator
train_generator = train_datagen.flow_from_directory(
        '/tmp/horse-or-human/',  # This is the source directory for training images
        target_size=(300, 300),  # All images will be resized to 150x150
        batch_size=128,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

위와 같이 tf.keras.preprocessing.image의 ImageDataGenerator는 먼저 rescale과 같이 자동으로 normalize 해주는 기능이 있다.
이후 해당 객체의 flow_from_directory함수를 통해 경로를 넘겨주고 target_size는 해당 이미지를 특정 사이즈로 알아서 crop해주는 것 같다.
batch_size는 특정 batch단위로 데이터를 내뱉어준다.
class_mode는 기본적으로 폴더명으로 class를 구분하는데 여기서 binary를 설정해 줌으로써 이진분류인 것을 알려준다.
이때 generator에 넘겨준 경로내에는 각자의 class 명으로 된 폴더안에 데이터들이 있어야한다.

history = model.fit(
      train_generator,
      steps_per_epoch=8,  
      epochs=15,
      verbose=1)

이후 학습시 fit을 해도되고 fit_generator를 해도된다.
주의할점은 steps_per_epoch을 통해 generator에서 나오는 batch를 각 epoch마다 몇 step을 학습할것인지 정해주어야 한다.
여기서는 128*8 만큼의 이미지를 학습하며 gradient가 update되는 것은 8번 일어나게 된다.

from tensorflow.keras.preprocessing.image import ImageDataGenerator

# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale=1/255)
validation_datagen = ImageDataGenerator(rescale=1/255)

# Flow training images in batches of 128 using train_datagen generator
train_generator = train_datagen.flow_from_directory(
        '/tmp/horse-or-human/',  # This is the source directory for training images
        target_size=(300, 300),  # All images will be resized to 300x300
        batch_size=128,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

# Flow training images in batches of 128 using train_datagen generator
validation_generator = validation_datagen.flow_from_directory(
        '/tmp/validation-horse-or-human/',  # This is the source directory for training images
        target_size=(300, 300),  # All images will be resized to 300x300
        batch_size=32,
        # Since we use binary_crossentropy loss, we need binary labels
        class_mode='binary')

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

validation같은 경우에는 똑같이 생성해주고 validation_data와 validation_steps를 정해주면 된다.
validation_steps는 steps_per_epoch과 같은 역할을 한다.

https://github.com/han811/tensorflow

profile
han811

0개의 댓글