Data augmentation method summary, dataaugmentation

Source: Internet
Author: User

Data augmentation method summary, dataaugmentation
Summary of several methods of data augmentation

In deep learning, data augmentation is a good choice, sometimes when there are not enough training sets, or a certain type of data is small, or to prevent over-fitting and make the model more robust.

Common Methods

Color Jittering: Color Data enhancement: image brightness, saturation, and contrast changes (the Color jitter is not properly understood here );

PCA Jittering: calculate the mean and standard deviation based on three RGB color channels, calculate the covariance matrix in the entire training set, and perform feature decomposition to obtain the feature vectors and feature values for PCA Jittering;

Random Scale: Scale transformation;

Random Crop: uses the Random image difference method to Crop and Scale the image, including the Scale Jittering method (used by VGG and ResNet models) or Scale and aspect ratio enhancement transformation;

Horizontal/Vertical Flip: Horizontal/Vertical Flip;

Shift: Translation transformation;

Rotation/Reflection: Rotation/affine transformation;

Noise: Gaussian Noise and fuzzy processing;

Label shuffle: The Data of unbalanced categories is extended. For more information, see the report of hikvision ILSVRC2016. In addition, a Supervised Data Augmentation method is proposed in this paper. If you are interested, you can perform a hands-on experiment.

Implementation of some methods
#-*-Coding: UTF-8-*-"Data enhancement 1. flip transform flip 2. randomly trim random crop 3. color jitter color jittering 4. shift 5. scale 6. contrast conversion contrast 7. noise disturbance noise 8. rotation transform/reflection transform Rotation/reflection "from PIL import Image, ImageEnhance, ImageOps, ImageFileimport numpy as npimport randomimport threading, OS, timeimport logginglogger = logging. getLogger (_ name _) ImageFile. LOAD_TRUNCATED_IMAGES = Trueclass DataAugmenta Tion: "eight methods of Data enhancement" def _ init _ (self): pass @ staticmethod def openImage (image): return Image. open (image, mode = "r") @ staticmethod def randomRotation (image, mode = Image. BICUBIC): "random angle of the image (0 ~ 360 degrees) rotation: param mode adjacent interpolation, bilinear interpolation, bilinear cubic B-spline interpolation (default): param image PIL image: return: the rotated image "random_angle = np. random. randint (1,360) return image. rotate (random_angle, mode) @ staticmethod def randomcdrop (image): "allows you to cut images randomly. Considering the image size range (), you can use a value greater than (36*36) run: param image: PIL image: return: Cut image "" image_width = image. size [0] image_height = image. size [1] crop_win_size = np. random. randint (40, 68) random_region = (image_width-crop_win_size)> 1, (image_height-crop_win_size)> 1, (image_width + crop_win_size)> 1, (image_height + crop_win_size)> 1) return image. crop (random_region) @ staticmethod def randomColor (image): "color jitter on images: param image: PIL image: return: image "" random_factor = np with color differences. random. randint (0, 31)/10. # random factor color_image = ImageEnhance. color (im Age ). enhance (random_factor) # adjust the image saturation random_factor = np. random. randint (10, 21)/10. # random factor brightness_image = ImageEnhance. brightness (color_image ). enhance (random_factor) # adjust the image brightness random_factor = np. random. randint (10, 21)/10. # random because 1 sub-contrast_image = ImageEnhance. contrast (brightness_image ). enhance (random_factor) # adjust the image contrast random_factor = np. random. randint (0, 31)/10. # random factor return ImageEnhan Ce. sharpness (contrast_image ). enhance (random_factor) # adjust image sharpness @ staticmethod def randomGaussian (image, mean = 0.2, sigma = 0.3): "perform Gaussian noise processing on the image: param image: return: "def gaussianNoisy (im, mean = 0.2, sigma = 0.3):" "perform Gaussian noise processing on the image: param im: single channel image: param mean: offset: param sigma: Standard Deviation: return: "" for _ I in range (len (im): im [_ I] + = random. gauss (mean, sigma) return im # convert the image into an array img = np. asarray (image) img. Flags. writeable = True # change the array to read/write mode width, height = img. shape [: 2] img_r = gaussianNoisy (img [:,:, 0]. flatten (), mean, sigma) img_g = gaussianNoisy (img [:,:, 1]. flatten (), mean, sigma) img_ B = gaussianNoisy (img [:,:, 2]. flatten (), mean, sigma) img [:,:, 0] = img_r.reshape ([width, height]) img [:,:, 1] = img_g.reshape ([width, height]) img [:,:, 2] = img_ B .reshape ([width, height]) return Image. fromar Ray (np. uint8 (img) @ staticmethod def saveImage (image, path): image. save (path) def makeDir (path): try: if not OS. path. exists (path): if not OS. path. isfile (path): # OS. mkdir (path) OS. makedirs (path) return 0 else: return 1 parse t Exception, e: print str (e) return-2def imageOps (func_name, image, des_path, file_name, times = 5 ): funcMap = {"randomRotation": DataAugmentation. randomRotation, "randomcdrop": D AtaAugmentation. randomcdrop, "randomColor": DataAugmentation. randomColor, "randomGaussian": DataAugmentation. randomGaussian} if funcMap. get (func_name) is None: logger. error ("% s is not exist", func_name) return-1 for _ I in range (0, times, 1): new_image = funcMap [func_name] (image) DataAugmentation. saveImage (new_image, OS. path. join (des_path, func_name + str (_ I) + file_name) opsList = {"randomRotatio N "," randomcdrop "," randomColor "," randomGaussian "} def threadOPS (path, new_path):" "multi-thread transaction processing: param src_path: resource file: param des_path: destination File: return: "" if OS. path. isdir (path): img_names = OS. listdir (path) else: img_names = [path] for img_name in img_names: print img_name tmp_img_name = OS. path. join (path, img_name) if OS. path. isdir (tmp_img_name): if makeDir (OS. path. join (new_path, img_name ))! =-1: threadOPS (tmp_img_name, OS. path. join (new_path, img_name) else: print 'create new dir failure 'Return-1 # OS. removedirs (tmp_img_name) elif tmp_img_name.split ('. ') [1]! = "DS_Store": # Read the file and perform operations image = DataAugmentation. openImage (tmp_img_name) threadImage = [0] * 5 _ index = 0 for ops_name in opsList: threadImage [_ index] = threading. thread (target = imageOps, args = (ops_name, image, new_path, img_name,) threadImage [_ index]. start () _ index + = 1 time. sleep (0.2) if _ name _ = '_ main _': threadOPS ("/home/pic-image/train/12306 train ",

Related Article

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.