1. 程式人生 > >深度學習之批量圖片資料增強

深度學習之批量圖片資料增強

在之前的文章中,分別對資料增強的方法以及庫函式進行了介紹,本文將結合實際應用進行批量圖片的資料增強。

背景:專案採集的是灰度圖,原資料只有不到20張圖片,因此,選擇資料增強的方法,通過不同變換方法的組合,實現資料增加的百張以上,這樣才可以放入深度學習模型進行訓練(利用遷移學習)。

話不多說,直接上程式碼,在程式碼中解釋用到的變換操作。

#!usr/bin/python
# -*- coding: utf-8 -*-
import cv2
from imgaug import augmenters as iaa
import os

# Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
# e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
sometimes = lambda aug: iaa.Sometimes(0.5, aug)

# 定義一組變換方法.
seq = iaa.Sequential([

    # 選擇0到5種方法做變換
    iaa.SomeOf((0, 5),
        [
                iaa.Fliplr(0.5), # 對50%的圖片進行水平映象翻轉
                iaa.Flipud(0.5), # 對50%的圖片進行垂直映象翻轉	
																
                # Convert some images into their superpixel representation,
                # sample between 20 and 200 superpixels per image, but do
                # not replace all superpixels with their average, only
                # some of them (p_replace).
                sometimes(
                    iaa.Superpixels(
                        p_replace=(0, 1.0),
                        n_segments=(20, 200)
                    )
                ),

                # Blur each image with varying strength using
                # gaussian blur (sigma between 0 and 3.0),
                # average/uniform blur (kernel size between 2x2 and 7x7)
                # median blur (kernel size between 3x3 and 11x11).
                iaa.OneOf([
                    iaa.GaussianBlur((0, 3.0)),
                    iaa.AverageBlur(k=(2, 7)),
                    iaa.MedianBlur(k=(3, 11)),
                ]),

                # Sharpen each image, overlay the result with the original
                # image using an alpha between 0 (no sharpening) and 1
                # (full sharpening effect).
                iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)),

                # Same as sharpen, but for an embossing effect.
                iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)),


                # Add gaussian noise to some images.
                # In 50% of these cases, the noise is randomly sampled per
                # channel and pixel.
                # In the other 50% of all cases it is sampled once per
                # pixel (i.e. brightness change).
                iaa.AdditiveGaussianNoise(
                    loc=0, scale=(0.0, 0.05*255)
                ),

                # Invert each image's chanell with 5% probability.
                # This sets each pixel value v to 255-v.
                iaa.Invert(0.05, per_channel=True), # invert color channels

                # Add a value of -10 to 10 to each pixel.
                iaa.Add((-10, 10), per_channel=0.5),

                # Add random values between -40 and 40 to images, with each value being sampled per pixel:
                iaa.AddElementwise((-40, 40)),

                # Change brightness of images (50-150% of original value).
                iaa.Multiply((0.5, 1.5)),

                # Multiply each pixel with a random value between 0.5 and 1.5.
                iaa.MultiplyElementwise((0.5, 1.5)),

                # Improve or worsen the contrast of images.
                iaa.ContrastNormalization((0.5, 2.0)),


        ],
        # do all of the above augmentations in random order
        random_order=True
    )

],random_order=True) #apply augmenters in random order

# 圖片檔案相關路徑
path = 'yingdaqi0/'
savedpath = 'yingdaqi_aug/'

imglist=[]
filelist = os.listdir(path)

# 遍歷要增強的資料夾,把所有的圖片儲存在imglist中
for item in filelist:
	img = cv2.imread(path + item)
	#print('item is ',item)
	#print('img is ',img)
	#images = load_batch(batch_idx)
	imglist.append(img)
	#print('imglist is ' ,imglist)
print('all the picture have been appent to imglist')

#對資料夾中的圖片進行增強操作,迴圈100次
for count in range(100):
	images_aug = seq.augment_images(imglist)
	for index in range(len(images_aug)):
		filename = str(count) + str(index) +'.jpg'
		#儲存圖片
		cv2.imwrite(savedpath + filename,images_aug[index])
		print('image of count%s index%s has been writen'%(count,index))	

通過以上程式碼的操作,可將目標資料夾中的原始圖片進行隨機變化,選擇變化方法組中的0到5種操作,當然,也可以在方法組中新增其它需要的操作,由於我的原圖是灰度圖,沒有涉及到顏色空間的變化,如果是彩色圖,可以增加相對應的變化,這樣更全面一些。

經過100次迴圈,相當於對每張圖片進行100次隨機變化,每次變化可能涉及到多種方法,這樣操作完之後,原始資料就擴大了100倍,並且沒有重複的資料在裡邊,達到資料增強的效果。