UOMOP

Data Augmentation(공간 기반) 본문

Ai/DL

Data Augmentation(공간 기반)

Happy PinGu 2022. 2. 10. 16:13

1. 각종 모듈 및 함수 호출

import numpy as np
import pandas as pd
import os
import cv2
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
%matplotlib inline


def show_image(image) :
    plt.figure(figsize = (8,8))
    plt.imshow(image)
    plt.axis("off")
    
    
def show_aug_image_batch(image, data_generator, how_many) : 
    
    image_4 = np.expand_dims(image, axis=0)
    
    data_generator.fit(image_4)
    change = data_generator.flow(image_4)
    
    fig, axs = plt.subplots(figsize = (22, 8), nrows=1, ncols=how_many)
    
    for i in range(how_many) :
        
        image_4 = next(change)
        image_3 = np.squeeze(image_4)
        image_3 = image_3.astype("int")
            
        axs[i].imshow(image_3)
        axs[i].axis("off")

2. url로 데이터 불러오기

!wget https://www.sciencenews.org/wp-content/uploads/2020/03/033120_HT_covid-cat_feat-1028x579.jpg

3. RGB순으로 변경 후, image 확인해보기

cat_3dim_BGR = cv2.imread("033120_HT_covid-cat_feat-1028x579.jpg")

cat_3dim = cv2.cvtColor(cat_3dim_BGR, cv2.COLOR_BGR2RGB)

plt.imshow(cat_3dim)
plt.title("original")
plt.axis("off")

horizontal_flip

data_gen = ImageDataGenerator(horizontal_flip = True)

show_aug_image_batch(cat_3dim, data_gen, 5)

vertical_flip

data_gen = ImageDataGenerator(vertical_flip = True)

show_aug_image_batch(cat_3dim, data_gen, 5)

horizontal_flip + vertical_flip

data_gen = ImageDataGenerator(horizontal_flip=True, vertical_flip=True)

show_aug_image_batch(cat_3dim, data_gen, 5)

rotation_range

data_gen = ImageDataGenerator(rotation_range=45)
# -45<=(rotation_range)<=45

show_aug_image_batch(cat_3dim, data_gen, 5)
data_gen = ImageDataGenerator(rotation_range=-38, fill_mode="constant", cval=0)
# -45<=(rotation_range)<=45

show_aug_image_batch(cat_3dim, data_gen, 5)

(width / height)_shift_range

data_gen = ImageDataGenerator(width_shift_range=0.4, fill_mode="nearest")

show_aug_image_batch(cat_3dim, data_gen, 5)
data_gen = ImageDataGenerator(width_shift_range=0.4, fill_mode="reflect")

show_aug_image_batch(cat_3dim, data_gen, 5)
data_gen = ImageDataGenerator(width_shift_range=0.4, fill_mode="wrap")

show_aug_image_batch(cat_3dim, data_gen, 5)
data_gen = ImageDataGenerator(height_shift_range=0.4, fill_mode="constant", cval=180)

show_aug_image_batch(cat_3dim, data_gen, 5)

zoom_range

data_gen = ImageDataGenerator(zoom_range=[0.5, 0.9])

show_aug_image_batch(cat_3dim, data_gen, 5)
data_gen = ImageDataGenerator(zoom_range=[1.1, 1.5], fill_mode="constant", cval=0)

show_aug_image_batch(cat_3dim, data_gen, 5)

shear_range

data_gen = ImageDataGenerator(shear_range=45)
# -45<=(shear_range)<=45

show_aug_image_batch(cat_3dim, data_gen, 5)

 

 

 

 

 

Comments