Learning Data Augmentation Based on keras, augmentationkeras

Source: Internet
Author: User
Tags keras

Learning Data Augmentation Based on keras, augmentationkeras

In deep learning, when the data size is not large enough, the following 4 methods are often used:

1. Manually increase the size of the training set. A batch of "new" Data is created from existing Data by means of translation, flip, and Noise addition. That is, Data Augmentation.
2. regularization. A small amount of data may lead to over-fitting of the model, making the training error small and the test error extremely large. by adding a regular expression after the Loss Function, the generation of overfitting can be restrained. the disadvantage is that a hyper-parameter that needs to be manually adjusted is introduced.
3. Dropout. This is also a normalization method, but unlike the above, it is achieved by randomly setting the output of some neurons to zero.
4. Unsupervised Pre-training. Use Auto-Encoder or RBM convolution to perform Unsupervised Pre-training layer by layer, and add the classification layer to implement supervised Fine-Tuning.

Next we will discuss Data Augmentation:

Under different task backgrounds, we can increase the input data volume by performing geometric transformation of the image using one or more of the following combined data enhancement transformations. the specific methods here are from the content of digital image processing, the introduction of related knowledge points, all available on the Internet.

Rotation | reflection conversion (Rotation/reflection): Randomly rotates the image to a certain angle and changes the orientation of the image content;

Flip transform (flip): flip an image along the horizontal or vertical direction;

Zoom: scales up or down an image according to a certain proportion;

Shift: The image is translated in certain ways on the image plane;
You can specify the translation range and the moving step size randomly or manually, and pan along the horizontal or vertical direction to change the position of the image content;

Scale: scales up or down an image based on a specified scale factor. Or, the image is filtered to construct a scale space based on the SIFT feature extraction method. changes the size or blur of the image content;

Contrast: In the HSV color space of the image, the saturation S and V brightness are changed to keep the color H unchanged. perform exponential operations on the S and V components of each pixel (the index factor is between 0.25 and 4) to increase the illumination variation;

Noise: random disturbance is performed on each pixel RGB Of the image. The common noise modes are prepaster noise and Gaussian noise;

Color Transformation (color): PCA is performed in the RGB color space of the training set pixel values to obtain three main direction vectors, three feature values, p1, p2, p3, λ 1, λ 2, λ 3. add the following changes to each pixel Ixy = [IRxy, IGxy, IBxy] T of each image:

[P1, p2, p3] [α 1 λ 1, α 2 λ 2, α 3 λ 3] T

Where: α I is a random variable that satisfies the mean of 0 and the variance of 0.1.

Code Implementation

As an implementation part, the following describes how to use the existing open-source code library Keras as a practice in the python environment:

1 #-*-coding: UTF-8-*-2 _ author _ = 'admin' 3 4 # import packages 5 from keras. preprocessing. image import ImageDataGenerator, kernel, img_to_array, load_img 6 7 kernel en = image1_enerator (8 rotation_range = 0.2, 9 width_shift_range = 0.2, 10 kernel = 0.2, 11 shear_range = 0.2, 12 zoom_range = 0.2, 13 horizontal_flip = True, 14 fill_mode = 'nearest ') 15 16 img = load_img ('C: \ Users \ Administrator \ Desktop \ dataA \ lena.jpg') # this is a PIL image, please replace to your own file path17 x = img_to_array (img) # this is a Numpy array with shape (3,150,150) 18 x = x. reshape (1,) + x. shape) # this is a Numpy array with shape (1, 3,150,150) 19 20 #. flow () command below generates batches of randomly transformed images21 # and saves the results to the 'preview/'directory22 23 I = 024 for batch in en. flow (x, 25 batch_size = 1,26 save_to_dir = 'C: \ Users \ Administrator \ Desktop \ dataA \ pre', # Save the generated image path 27 save_prefix = 'lena ', 28 save_format = 'jpg '): 29 I + = 130 if I> 20: # This 20 indicates how much data to expand 31 break # otherwise the generator wowould loop indefinitely

Main functions:ImageDataGenerator implements most of the image geometric transformation methods mentioned above.

Rotation_range: rotation range, random rotation (0-180;

Width_shift and height_shift: Shifts randomly along the horizontal or vertical direction, with the variation range as the percentage of the image's length, width, and small part;

Rescale: scales up or down an image based on a specified scale factor. The value ranges from 0 to 1, usually 1/255;

Shear_range: horizontal or vertical projection transformation,

Zoom_range: scales the image size proportionally;

Horizontal_flip: horizontal flip image;

Fill_mode: Fill pixels, after rotation or translation.

Shows the effect:

1 #-*-coding: UTF-8-*-2 _ author _ = 'admin' 3 4 # import packages 5 from keras. preprocessing. image import ImageDataGenerator, kernel, img_to_array, load_img 6 7 kernel en = image1_enerator (8 rotation_range = 0.2, 9 width_shift_range = 0.2, 10 kernel = 0.2, 11 shear_range = 0.2, 12 zoom_range = 0.2, 13 horizontal_flip = True, 14 fill_mode = 'nearest ') 15 16 for k in range (33): 17 numstr = "{0: d }". format (k); 18 filename = 'C: \ Users \ Administrator \ Desktop \ bad \ '{numstr}'.jpg '; 19 ufilename = unicode (filename, "utf8 ") 20 img = load_img (ufilename) # this is a PIL image, please replace to your own file path21 x = img_to_array (img) # this is a Numpy array with shape (3,150,150) 22 x = x. reshape (1,) + x. shape) # this is a Numpy array with shape (1, 3,150,150) 23 24 #. flow () command below generates batches of randomly transformed images25 # and saves the results to the 'preview/'directory26 27 I = 028 29 for batch in en. flow (x, 30 batch_size = 1,31 save_to_dir = 'C: \ Users \ Administrator \ Desktop \ dataA \\', # the generated image storage path 32 save_prefix = numstr, 33 save_format = 'jpg '): 34 I + = 135 if I> 20:36 break # otherwise the generator wowould loop indefinitely37 end
1 #-*-coding: UTF-8-*-2 _ author _ = 'admin' 3 4 # import packages 5 from keras. preprocessing. image import image1_enerator, array_to_img, img_to_array, load_img 6 7 en = image1_enerator (8 rotation_range = 10, 9 width_shift_range = 0.2, 10 rows = 0.2, 11 rescale = 1. /0.2, 12 shear_range = 0.2, 13 zoom_range =, 14 horizontal_flip = True, 15 fill_mode = 'nearest ') 16 import os17 18 import sys19 reload (sys) 20 sys. setdefaultencoding ('utf8') 21 22 ufilename = unicode ("C: \ Users \ Administrator \ Desktop \ test", "utf8") 23 24 for filename in OS. listdir (ufilename): # The listdir parameter is the folder path 25 print (filename) # at this time, filename is the name of the file in the folder 26 pathname = 'C: \ Users \ Administrator \ Desktop \ test \ '+ filename; 27 # ufilename = unicode (pathname, "utf8") 28 img = load_img (pathname) # this is a PIL image, please replace to your own file path29 x = img_to_array (img) # this is a Numpy array with shape (3,150,150) 30 x = x. reshape (1,) + x. shape) # this is a Numpy array with shape (1, 3,150,150) 31 #. flow () command below generates batches of randomly transformed images32 # and saves the results to the 'preview/'directory33 I = 034 for batch in en. flow (x, 35 batch_size = 1, 36 save_to_dir = 'C: \ Users \ Administrator \ Desktop \ result \\', # Save path of the generated image 37 save_prefix = filename, 38 save_format = 'jpg '): 39 I + = 140 if I> 100:41 break # otherwise the generator wowould loop indefinitely42 43 44 # worker en = image1_enerator (45 # rotation_range = 0.2, 46 # width_shift_range = 0.2, 47 # height_shift_range = 0.2, 48 # rescale = 1. /0.1 # shear_range = 0.4, 50 # zoom_range =, 51 # horizontal_flip = True, 52 # fill_mode = 'nearest ') 53 #54 # ufilename = unicode ("C: \ Users \ Administrator \ Desktop \ training "," utf8 ") 55 # for filename in OS. listdir (ufilename): # The listdir parameter is the folder path 56 # print (filename) # at this time, filename is the name of the file in the folder 57 # pathname = 'C: \ Users \ Administrator \ Desktop \ training \ '+ filename; 58 # ufilename = unicode (pathname, "utf8") 59 # img = load_img (pathname) # this is a PIL image, please replace to your own file path60 # x = img_to_array (img) # this is a Numpy array with shape (3,150,150) 61 # x = x. reshape (1,) + x. shape) # this is a Numpy array with shape (1, 3,150,150) 62 #63 #. flow () command below generates batches of randomly transformed images64 # and saves the results to the 'preview/'directory65 #66 # I = 067 #68 # for batch in en. flow (x, 69 # batch_size = 1,70 # save_to_dir = 'C: \ Users \ Administrator \ Desktop \ result \\', # The generated image storage path 71 # save_prefix = filename, 72 # save_format = 'jpg '): 73 # I + = 174 # if I> 100: 75 # break # otherwise the generator wowould loop indefinitely

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.