[PYTHON] Inflating learning data [Image Date Generator]

Inflating learning data [Image Date Generator] from keras.preprocessing.image import load_img,img_to_array,ImageDataGenerator, array_to_img # import matplotlib.pyplot as plt from keras_preprocessing.image import list_pictures import os from PIL import Image

---- Classifying class ---

classes = ["dog", "cat", "bird"]

---- Set image size ----

img_width, img_height = 1600, 1200

---- Directory definition ----

DATA_DIR = [""] * len(classes)
SAVE_DIR = [""] * len(classes)
for i in range(len(classes)):
    DATA_DIR[i] = 'input/' + classes[i]

SAVE_DIR [i] = os.path.join ('output /', classes [i]) # Directory where the generated image is saved if not os.path.exists(SAVE_DIR[i]): os.makedirs(SAVE_DIR[i])

#Load image (PIL format image) # img = load_img(IMAGE_FILE)

pasting

# plt.imshow(img)

Display

# plt.show()

#Rotation: -15 ~ 15 #Translation up and down: -0.8 to 1.2% movement #Translation from side to side: -0.8 to 1.2% movement #Shear: Shear at -5 degrees to 5 degrees

Scaling: Scaling by 0.8 ~ 1.2%

#Brightness change: Add value to pixel value in the range of -5.0 to 5.0

Add a value to each pixel value: Change the value in the range of 0.3 to 1.0

datagen = ImageDataGenerator(
    rotation_range=15,
    height_shift_range=0.2,
    width_shift_range=0.2,
    shear_range=5,
    zoom_range=0.2,
   channel_shift_range=5,
   brightness_range=[0.3, 1.0]
)

for i in range(len(classes)):
    for picture in list_pictures(DATA_DIR[i]):
        img = img_to_array(load_img(picture, target_size=(img_height, img_width)))

Convert to an array of # numpy x = img_to_array(img)

Convert to a 4-dimensional array

        # x = np.expand_dims(x, axis=0)
        x = x.reshape((1,) + x.shape)

        # print(x.shape)

        g = datagen.flow(x, batch_size=1, save_to_dir=SAVE_DIR[i], save_prefix='out', save_format='jpg')
        for j in range(3):
            batches = g.next()

(1, vertical size, horizontal size, number of channels)

            # print(batches.shape)

#To display as an image, change from 4D to 3D data, and change from an array to an image. gen_img = array_to_img(batches[0])

            # plt.subplot(8, 8, i + 1)
            # plt.imshow(gen_img)
            # plt.axis('off')

Recommended Posts

Inflating learning data [Image Date Generator]
Inflated learning image
Creating learning data for face image dataset sorting (# 1)
Data set for machine learning
Deep learning image recognition 1 theory
Python data analysis learning notes
Image data type conversion [Python]