keras 이미지 데이터 강화 | keras data augmentation

3439 단어 kezunlin.me
본문 은 개인 블로그 에서 발간 되었다https://kezunlin.me/post/8db507ff/최신 내용을 읽으신 것을 환영합니다!
keras data augmentation

Guide


code

# import the necessary packages
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import load_img
import numpy as np
import argparse

from keras_util import *

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
    help="path to the input image")
ap.add_argument("-o", "--output", required=True,
    help="path to output directory to store augmentation examples")
ap.add_argument("-p", "--prefix", type=str, default="image",
    help="output filename prefix")
args = vars(ap.parse_args())

# load the input image, convert it to a NumPy array, and then
# reshape it to have an extra dimension
print("[INFO] loading example image...")
target_size = None 
#target_size=(224,224)
image = load_img(args["image"], target_size=target_size)
image = img_to_array(image)
image = np.expand_dims(image, axis=0) # 1,h,w,c

# construct the image generator for data augmentation then
# initialize the total number of images generated thus far

# preprocessing_function: The function will run after the image is resized and augmented.
# The function should take one argument:
#    one image (Numpy tensor with rank 3),
#     and should output a Numpy tensor with the same shape.


# for 1 image --->(424,640,3)--->aug---(424,640,3)--->preprocess_input--->(424,640,3)
# for 1 image --->resize--->(224,224,3)--->aug---(224,224,3)--->preprocess_input--->(224,224,3)
aug = ImageDataGenerator(preprocessing_function=resnet.preprocess_input,
                         rotation_range=30, 
                         width_shift_range=0.1,
                         height_shift_range=0.1, 
                         shear_range=0.2, 
                         zoom_range=0.2,
                         horizontal_flip=True,
                         fill_mode="nearest")
total = 0

# construct the actual Python generator
print("[INFO] generating images...")
imageGen = aug.flow(image, 
                    batch_size=1, 
                    save_to_dir=args["output"],
                    save_prefix=args["prefix"], 
                    save_format="png")

next_image = next(imageGen)
print(next_image.shape)
print(next_image[0, :5,:5,0])

# loop over examples from our image data augmentation generator
for image in imageGen:
    # increment our counter
    total  = 1

    # if we have reached 10 examples, break from the loop
    if total == 10:
        break

output
target_size = None:
1 image --->(424,640,3)--->aug--->(424,640,3)--->preprocess_input--->(424,640,3)
target_size = (224,224):
1 image --->resize--->(224,224,3)--->aug--->(224,224,3)--->preprocess_input--->(224,224,3)

Reference


History

  • 20190910: created.

  • Copyright

  • Post author: kezunlin
  • Post link: https://kezunlin.me/post/8db507ff/
  • Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
  • 좋은 웹페이지 즐겨찾기