欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)數(shù)據(jù)增強(qiáng)及預(yù)處理示例詳解

 更新時(shí)間:2022年05月06日 17:19:09   作者:Bubbliiiing  
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)數(shù)據(jù)增強(qiáng)及預(yù)處理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

學(xué)習(xí)前言

進(jìn)行訓(xùn)練的話,如果直接用原圖進(jìn)行訓(xùn)練,也是可以的(就如我們最喜歡Mnist手寫體),但是大部分圖片長(zhǎng)和寬不一樣,直接resize的話容易出問(wèn)題。

除去resize的問(wèn)題外,有些時(shí)候數(shù)據(jù)不足該怎么辦呢,當(dāng)然要用到數(shù)據(jù)增強(qiáng)啦。

這篇文章就是記錄我最近收集的一些數(shù)據(jù)預(yù)處理的方式

處理長(zhǎng)寬不同的圖片

對(duì)于很多分類、目標(biāo)檢測(cè)算法,輸入的圖片長(zhǎng)寬是一樣的,如224,224、416,416等。

直接resize的話,圖片就會(huì)失真。

但是我們可以采用如下的代碼,使其用padding的方式不失真。

from PIL import Image
def letterbox_image(image, size):
    # 對(duì)圖片進(jìn)行resize,使圖片不失真。在空缺的地方進(jìn)行padding
    iw, ih = image.size
    w, h = size
    scale = min(w/iw, h/ih)
    nw = int(iw*scale)
    nh = int(ih*scale)
    image = image.resize((nw,nh), Image.BICUBIC)
    new_image = Image.new('RGB', size, (128,128,128))
    new_image.paste(image, ((w-nw)//2, (h-nh)//2))
    return new_image
img = Image.open("2007_000039.jpg")
new_image = letterbox_image(img,[416,416])
new_image.show()

得到圖片為:

數(shù)據(jù)增強(qiáng)

1、在數(shù)據(jù)集內(nèi)進(jìn)行數(shù)據(jù)增強(qiáng)

這個(gè)的意思就是可以直接增加圖片的方式進(jìn)行數(shù)據(jù)增強(qiáng)。其主要用到的函數(shù)是:

ImageDataGenerator(featurewise_center=False,  
                    samplewise_center=False, 
                    featurewise_std_normalization=False, 
                    samplewise_std_normalization=False, 
                    zca_whitening=False, 
                    zca_epsilon=1e-06, 
                    rotation_range=0, 
                    width_shift_range=0.0, 
                    height_shift_range=0.0, 
                    brightness_range=None, 
                    shear_range=0.0, 
                    zoom_range=0.0, 
                    channel_shift_range=0.0, 
                    fill_mode='nearest', 
                    cval=0.0, 
                    horizontal_flip=False, 
                    vertical_flip=False, 
                    rescale=None, 
                    preprocessing_function=None, 
                    data_format=None, 
                    validation_split=0.0, 
                    dtype=None)

對(duì)于我而言,常用的方法如下:

datagen = ImageDataGenerator(
        rotation_range=10,
        width_shift_range=0.1,
        height_shift_range=0.1,
        shear_range=0.2,
        zoom_range=0.1,
        horizontal_flip=False,
        brightness_range=[0.1, 2],
        fill_mode='nearest')

其中,參數(shù)的意義為:

1、rotation_range:旋轉(zhuǎn)范圍

2、width_shift_range:水平平移范圍

3、height_shift_range:垂直平移范圍

4、shear_range:float, 透視變換的范圍

5、zoom_range:縮放范圍

6、horizontal_flip:水平反轉(zhuǎn)

7、brightness_range:圖像隨機(jī)亮度增強(qiáng),給定一個(gè)含兩個(gè)float值的list,亮度值取自上下限值間

8、fill_mode:‘constant’,‘nearest’,‘reflect’或‘wrap’之一,當(dāng)進(jìn)行變換時(shí)超出邊界的點(diǎn)將根據(jù)本參數(shù)給定的方法進(jìn)行處理。

實(shí)際使用時(shí)可以利用如下函數(shù)生成圖像:

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import os 
datagen = ImageDataGenerator(
        rotation_range=10,
        width_shift_range=0.1,
        height_shift_range=0.1,
        shear_range=0.2,
        zoom_range=0.1,
        horizontal_flip=False,
        brightness_range=[0.1, 2],
        fill_mode='nearest')
trains = os.listdir("./train/")
for index,train in enumerate(trains):
    img = load_img("./train/" + train)
    x = img_to_array(img)
    x = x.reshape((1,) + x.shape)
    i = 0
    for batch in datagen.flow(x, batch_size=1,
                            save_to_dir='./train_out', save_prefix=str(index), save_format='jpg'):
        i += 1
        if i > 20:
            break  

生成效果為:

2、在讀取圖片的時(shí)候數(shù)據(jù)增強(qiáng)

ImageDataGenerator是一個(gè)非常nice的增強(qiáng)方式,不過(guò)如果不想生成太多的圖片,然后想要直接在讀圖的時(shí)候處理,也是可以的。

我們用到PIL中的ImageEnhance庫(kù)。

1、亮度增強(qiáng)ImageEnhance.Brightness(image)

2、色度增強(qiáng)ImageEnhance.Color(image)

3、對(duì)比度增強(qiáng)ImageEnhance.Contrast(image)

4、銳度增強(qiáng)ImageEnhance.Sharpness(image)

在如下的函數(shù)中,可以通過(guò)改變Ehance函數(shù)中的參數(shù)實(shí)現(xiàn)不同的增強(qiáng)方式。

import os
import numpy as np
from PIL import Image
from PIL import ImageEnhance
def Enhance_Brightness(image):
    # 變亮,增強(qiáng)因子為0.0將產(chǎn)生黑色圖像,為1.0將保持原始圖像。
    # 亮度增強(qiáng)
    enh_bri = ImageEnhance.Brightness(image)
    brightness = np.random.uniform(0.6,1.6)
    image_brightened = enh_bri.enhance(brightness)
    return image_brightened
def Enhance_Color(image):
    # 色度,增強(qiáng)因子為1.0是原始圖像
    # 色度增強(qiáng)
    enh_col = ImageEnhance.Color(image)
    color = np.random.uniform(0.4,2.6)
    image_colored = enh_col.enhance(color)
    return image_colored
def Enhance_contrasted(image):
    # 對(duì)比度,增強(qiáng)因子為1.0是原始圖片
    # 對(duì)比度增強(qiáng)
    enh_con = ImageEnhance.Contrast(image)
    contrast = np.random.uniform(0.6,1.6)
    image_contrasted = enh_con.enhance(contrast)
    return image_contrasted
def Enhance_sharped(image):
    # 銳度,增強(qiáng)因子為1.0是原始圖片
    # 銳度增強(qiáng)
    enh_sha = ImageEnhance.Sharpness(image)
    sharpness = np.random.uniform(0.4,4)
    image_sharped = enh_sha.enhance(sharpness)
    return image_sharped
def Add_pepper_salt(image):
    # 增加椒鹽噪聲
    img = np.array(image)
    rows,cols,_=img.shape
    random_int = np.random.randint(500,1000)
    for _ in range(random_int):
        x=np.random.randint(0,rows)
        y=np.random.randint(0,cols)
        if np.random.randint(0,2):
            img[x,y,:]=255
        else:
            img[x,y,:]=0
    img = Image.fromarray(img)
    return img
def Enhance(image_path, change_bri=1, change_color=1, change_contras=1, change_sha=1, add_noise=1):
    #讀取圖片
    image = Image.open(image_path)
    if change_bri==1:
        image = Enhance_Brightness(image)
    if change_color==1:
        image = Enhance_Color(image)
    if change_contras==1:
        image = Enhance_contrasted(image)
    if change_sha==1:
        image = Enhance_sharped(image)
    if add_noise==1:
        image = Add_pepper_salt(image)
    image.save("0.jpg")
Enhance("2007_000039.jpg")

原圖:

效果如下:

3、目標(biāo)檢測(cè)中的數(shù)據(jù)增強(qiáng)

在目標(biāo)檢測(cè)中如果要增強(qiáng)數(shù)據(jù),并不是直接增強(qiáng)圖片就好了,還要考慮到圖片扭曲后框的位置。

也就是框的位置要跟著圖片的位置進(jìn)行改變。

原圖:

增強(qiáng)后:

from PIL import Image, ImageDraw
import numpy as np
from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
def rand(a=0, b=1):
    return np.random.rand()*(b-a) + a
def get_random_data(annotation_line, input_shape, random=True, max_boxes=20, jitter=.3, hue=.1, sat=1.5, val=1.5, proc_img=True):
    '''random preprocessing for real-time data augmentation'''
    line = annotation_line.split()
    image = Image.open(line[0])
    iw, ih = image.size
    h, w = input_shape
    box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])
    # resize image
    new_ar = w/h * rand(1-jitter,1+jitter)/rand(1-jitter,1+jitter)
    scale = rand(.7, 1.3)
    if new_ar < 1:
        nh = int(scale*h)
        nw = int(nh*new_ar)
    else:
        nw = int(scale*w)
        nh = int(nw/new_ar)
    image = image.resize((nw,nh), Image.BICUBIC)
    # place image
    dx = int(rand(0, w-nw))
    dy = int(rand(0, h-nh))
    new_image = Image.new('RGB', (w,h), (128,128,128))
    new_image.paste(image, (dx, dy))
    image = new_image
    # flip image or not
    flip = rand()<.5
    if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)
    # distort image
    hue = rand(-hue, hue)
    sat = rand(1, sat) if rand()<.5 else 1/rand(1, sat)
    val = rand(1, val) if rand()<.5 else 1/rand(1, val)
    x = rgb_to_hsv(np.array(image)/255.)
    x[..., 0] += hue
    x[..., 0][x[..., 0]>1] -= 1
    x[..., 0][x[..., 0]<0] += 1
    x[..., 1] *= sat
    x[..., 2] *= val
    x[x>1] = 1
    x[x<0] = 0
    image_data = hsv_to_rgb(x) # numpy array, 0 to 1
    # correct boxes
    box_data = np.zeros((max_boxes,5))
    if len(box)>0:
        np.random.shuffle(box)
        box[:, [0,2]] = box[:, [0,2]]*nw/iw + dx
        box[:, [1,3]] = box[:, [1,3]]*nh/ih + dy
        if flip: box[:, [0,2]] = w - box[:, [2,0]]
        box[:, 0:2][box[:, 0:2]<0] = 0
        box[:, 2][box[:, 2]>w] = w
        box[:, 3][box[:, 3]>h] = h
        box_w = box[:, 2] - box[:, 0]
        box_h = box[:, 3] - box[:, 1]
        box = box[np.logical_and(box_w>1, box_h>1)] # discard invalid box
        if len(box)>max_boxes: box = box[:max_boxes]
        box_data[:len(box)] = box
    return image_data, box_data
if __name__ == "__main__":
    line = r"F:\Collection\yolo_Collection\keras-yolo3-master\VOCdevkit/VOC2007/JPEGImages/00001.jpg 738,279,815,414,0"
    image_data, box_data = get_random_data(line,[416,416])
    left, top, right, bottom  = box_data[0][0:4]
    img = Image.fromarray((image_data*255).astype(np.uint8))
    draw = ImageDraw.Draw(img)
    draw.rectangle([left, top, right, bottom])
    img.show()

以上就是python神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)數(shù)據(jù)增強(qiáng)及預(yù)處理示例詳解的詳細(xì)內(nèi)容,更多關(guān)于python神經(jīng)網(wǎng)絡(luò)數(shù)據(jù)增強(qiáng)預(yù)處理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論