• keras对图像数据进行增强 | keras data augmentation


    本文首发于个人博客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

  • 相关阅读:
    「SPOJ10707」Count on a tree II
    UVA 11021 /概率
    power oj/2360/Change
    POJ1613 147/思维题
    Power oj2498/DP/递推
    HDU4815/计数DP
    444A/CF
    观光公交noip<贪心>
    2014 Shanghai Invitation Contest
    POJ1734/Floyd求最小环
  • 原文地址:https://www.cnblogs.com/kezunlin/p/11968906.html
Copyright © 2020-2023  润新知