본문 바로가기
Visual Intelligence/Image Deep Learning

[시각 지능] Image Data Augmentation + Transfer Learning

by goatlab 2022. 8. 20.
728x90
반응형
SMALL

Image Data Augmentation

 

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.applications import MobileNet, Xception
from tensorflow.keras.layers import Flatten, Dense, Dropout, GlobalAveragePooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator

!wget https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip
import os
import shutil

if os.path.exists('/content/cats_and_dogs_filtered/'):    # 작업 디렉토리 cats_and_dogs_filtered
    shutil.rmtree('/content/cats_and_dogs_filtered/')
    print('/content/cats_and_dogs_filtered/  is removed.')
# 압축파일 풀기
import zipfile

with zipfile.ZipFile('/content/cats_and_dogs_filtered.zip', 'r') as target_file:
    target_file.extractall('/content/cats_and_dogs_filtered/')
train_data_gen = ImageDataGenerator(rescale=1./255, 
                                    rotation_range=10, width_shift_range=0.1, 
                                    height_shift_range=0.1, shear_range=0.1, 
                                    zoom_range=0.1, horizontal_flip=True,
                                    validation_split=0.2)

validation_data_gen = ImageDataGenerator(rescale=1./255, validation_split=0.2)

test_data_gen = ImageDataGenerator(rescale=1./255)

train_dir = '/content/cats_and_dogs_filtered/cats_and_dogs_filtered/train'

test_dir = '/content/cats_and_dogs_filtered/cats_and_dogs_filtered/validation'

IMG_WIDTH = 224
IMG_HEIGHT = 224

 

binary

 

train_generator = train_data_gen.flow_from_directory(train_dir, batch_size=32, 
                                                color_mode='rgb', class_mode='binary',
                                                target_size=(IMG_WIDTH,IMG_HEIGHT),
                                                subset='training')

validation_generator = validation_data_gen.flow_from_directory(train_dir, batch_size=32, 
                                                color_mode='rgb', class_mode='binary',
                                                target_size=(IMG_WIDTH,IMG_HEIGHT),
                                                subset='validation')

test_generator = test_data_gen.flow_from_directory(test_dir, batch_size=32, 
                                              color_mode='rgb', class_mode='binary',
                                              target_size=(IMG_WIDTH,IMG_HEIGHT))

 

categorical

 

train_generator = train_data_gen.flow_from_directory(train_dir, batch_size=32, 
                                                color_mode='rgb', class_mode='categorical',
                                                target_size=(IMG_WIDTH,IMG_HEIGHT),
                                                subset='training')

validation_generator = validation_data_gen.flow_from_directory(train_dir, batch_size=32, 
                                                color_mode='rgb', class_mode='categorical',
                                                target_size=(IMG_WIDTH,IMG_HEIGHT),
                                                subset='validation')

test_generator = test_data_gen.flow_from_directory(test_dir, batch_size=32, 
                                              color_mode='rgb', class_mode='categorical',
                                              target_size=(IMG_WIDTH,IMG_HEIGHT))

 

sparse

 

train_generator = train_data_gen.flow_from_directory(train_dir, batch_size=32, 
                                                color_mode='rgb', class_mode='sparse',
                                                target_size=(IMG_WIDTH,IMG_HEIGHT),
                                                subset='training')

validation_generator = validation_data_gen.flow_from_directory(train_dir, batch_size=32, 
                                                color_mode='rgb', class_mode='sparse',
                                                target_size=(IMG_WIDTH,IMG_HEIGHT),
                                                subset='validation')

test_generator = test_data_gen.flow_from_directory(test_dir, batch_size=32, 
                                              color_mode='rgb', class_mode='sparse',
                                              target_size=(IMG_WIDTH,IMG_HEIGHT))
Found 1600 images belonging to 2 classes.
Found 400 images belonging to 2 classes.
Found 1000 images belonging to 2 classes.
# 정답 확인
print(train_generator.class_indices.items())
print(validation_generator.class_indices.items())
print(test_generator.class_indices.items())
print(len(train_generator.classes))
print(len(validation_generator.classes))
print(len(test_generator.classes))
print(train_generator.num_classes)
print(validation_generator.num_classes)
print(test_generator.num_classes)
{'cats': 0, 'dogs': 1}
{'cats': 0, 'dogs': 1}
{'cats': 0, 'dogs': 1}
1600
400
1000
2
2
2

 

Transfer Learning

 

base_model = Xception(weights='imagenet', include_top=False, input_shape=(IMG_WIDTH,IMG_HEIGHT,3))

model = Sequential()

model.add(base_model)

model.add(GlobalAveragePooling2D())

model.add(Dense(16, activation='relu'))
model.add(Dropout(0.25))

model.add(Dense(1, activation='softmax'))

model.summary()
from datetime import datetime
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping

# 모델 컴파일
model.compile(loss='sparse_categorical_crossentropy', 
              optimizer=tf.keras.optimizers.Adam(2e-5), metrics=['accuracy'])

save_file_name = './cats_and_dogs_filtered_Xception_Colab.h5'

checkpoint = ModelCheckpoint(save_file_name,       # file명 지정
                             monitor='val_loss',   # val_loss 값이 개선되었을 때 호출
                             verbose=1,            # 로그 출력
                             save_best_only=True,  # 가장 best 값만 저장
                             mode='auto'           # auto : best값 찾음
                            )

earlystopping = EarlyStopping(monitor='val_loss',  # 모니터 기준 설정 (val loss) 
                              patience=5,          # 5 Epoch동안 개선되지 않는다면 종료
                             )

start_time = datetime.now()

hist = model.fit(train_generator, epochs=20, 
                 validation_data=validation_generator)

end_time = datetime.now()

print('elapsed time = ', end_time-start_time)
import matplotlib.pyplot as plt

plt.title('accuracy trend')
plt.grid()
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.plot(hist.history['accuracy'], label='train')
plt.plot(hist.history['val_accuracy'], label='validation')
plt.legend(loc='best')
plt.show()

plt.title('loss trend')
plt.grid()
plt.xlabel('epochs')
plt.ylabel('loss')
plt.plot(hist.history['loss'], label='train')
plt.plot(hist.history['val_loss'], label='validation')
plt.legend(loc='best')
plt.show()

 

테스트 이미지 확인

 

test_dir.zip
0.03MB

import cv2
import glob

with zipfile.ZipFile('/content/test_dir.zip', 'r') as target_file:
    target_file.extractall('/content/test_dir/')

test_img_list = []

test_img_name_list = glob.glob('/content/test_dir/*')

for i in range(len(test_img_name_list)):
    src_img = cv2.imread(test_img_name_list[i], cv2.IMREAD_COLOR)
    src_img = cv2.resize(src_img, dsize=(IMG_WIDTH, IMG_HEIGHT))

    dst_img = cv2.cvtColor(src_img, cv2.COLOR_BGR2RGB)

    dst_img = dst_img / 255.0

    test_img_list.append(dst_img)

print(test_img_name_list)
print(len(test_img_list))
['/content/test_dir/dog1.jpg', '/content/test_dir/dog2.jpg', '/content/test_dir/dog3.jpg', '/content/test_dir/cat1.jpg', '/content/test_dir/cat3.jpg', '/content/test_dir/cat2.jpg']
6
plt.figure(figsize=(6,4))

for i in range(len(test_img_list)):
    plt.subplot(2, 3, i+1)
    plt.axis('off')
    plt.imshow(test_img_list[i])

plt.show()

for i in range(len(test_img_list)):
    print(test_img_list[i].shape)
(224, 224, 3)
(224, 224, 3)
(224, 224, 3)
(224, 224, 3)
(224, 224, 3)
(224, 224, 3)
# predict 수행하기 위해 batch 차원 추가
import numpy as np

print(np.array(test_img_list).shape)
(6, 224, 224, 3)
# predict 실행
class_name = [ 'cat', 'dog' ]  # ImageDataGenerator 에서 cat=0, dog=1

pred = model.predict(np.array(test_img_list))

print(pred.shape)
print(pred)
(6, 1)
[[0.9999931 ]
 [0.9996532 ]
 [0.9444051 ]
 [0.00306303]
 [0.02342984]
 [0.01182175]]
class_name = [ 'cat', 'dog' ]  # ImageDataGenerator에서 cat=0, dog=1

plt.figure(figsize=(6,6))

for i in range(len(pred)):
    plt.subplot(2, 3, i+1)
    prediction = str(class_name[np.argmax(pred[i])])
    probility = '{0:0.2f}'.format(100*max(pred[i]))
    title_str = prediction + ' , ' + probility + '%'
    plt.axis('off')
    plt.title(title_str)
    plt.imshow(test_img_list[i])

plt.show()

728x90
반응형
LIST