資料增強——基本方法
阿新 • • 發佈:2019-02-06
基本資料增強主要包含如下方式:
1.旋轉: 可通過在原圖上先放大影象,然後剪下影象得到。
2.平移:先放大影象,然後水平或垂直偏移位置剪下
3.縮放:縮放影象
4.隨機遮擋:對影象進行小區域遮擋
5.水平翻轉:以過影象中心的豎直軸為對稱軸,將左、右兩邊畫素交換
6.顏色色差(飽和度、亮度、對比度、 銳度等)
7.噪聲擾動: 對影象的每個畫素RGB進行隨機擾動, 常用的噪聲模式是椒鹽噪聲和高斯噪聲;
Tensorflow程式碼實現:
引數可根據需求進行相應調整。
# -*- coding: utf-8 -*-
"""
# 資料增強實現
"""
import tensorflow as tf
import cv2
import numpy as np
from scipy import misc
import random
def random_rotate_image(image):
interb = ['nearest','bilinear','cubic','bicubic']
angle = np.random.uniform(low=-10.0, high=10.0)
key = random.randint(0,3)
return misc.imrotate(image, angle, interb[key])
def random_occlusion (image):
b_ratio = 1./10 #遮擋比例
M1 = np.ones((320,250))
b_H = random.randint(10,320*(1-b_ratio)-10)
b_W = random.randint(10,250*(1-b_ratio)-10)
M1[b_H:int(b_H+320*b_ratio),b_W:int(b_W+250*b_ratio)] = 0
M1 = np.expand_dims(M1, 2)
image = image*M1
image = image.astype(np.uint8)
return image
def data_augumrntation(image):
image = tf.py_func(random_occlusion, [image], tf.uint8) #隨機遮擋
image = tf.py_func(random_rotate_image, [image], tf.uint8) #旋轉
ratio = [0.9,1.1] #縮放比例
new_H = random.randint(320*ratio[0], 320*ratio[1])
new_W = random.randint(250*ratio[0], 250*ratio[1])
print(new_H,new_W)
image.set_shape((320, 250,3))
image = tf.image.resize_images(image,[new_H, new_W])
image = tf.cast(image,tf.uint8)
image = tf.image.resize_image_with_crop_or_pad(image, 320, 250 )#縮放
image = tf.random_crop(image, [299, 235, 3]) #隨機裁剪
image = tf.image.random_flip_left_right(image)#映象
N_key = random.randint(0,10)
if N_key == 8:
image = tf.image.per_image_standardization(image)#標準化
image = tf.cast(image, tf.float32)
image = tf.minimum(255.0, tf.maximum(0.0,tf.image.random_brightness(image,25.0)))#光照
image = tf.minimum(255.0, tf.maximum(0.0,tf.image.random_contrast(image,0.8,1.2)))#對比度
noise = tf.random_normal((299, 235, 3), mean=0.0, stddev=1.0, dtype=tf.float32)
image = tf.minimum(255.0, tf.maximum(0.0,image+noise))#隨機噪聲
image = tf.subtract(image,127.5)
image = tf.multiply(image,0.0078125)
return image
if __name__ == '__main__':
pic = r"bb.jpg"
file_contents = tf.read_file(pic)
image = tf.image.decode_jpeg(file_contents, dct_method="INTEGER_ACCURATE")
R,G,B=tf.unstack(image, num=3, axis=2)
image=tf.stack([B,G,R], axis=2) #通道轉換
image = data_augumrntation(image)
#image = tf.cast(image,tf.uint8)
sess = tf.Session()
img = sess.run(image)
cv2.imshow('img',img)
cv2.waitKey()
原圖:
增強後圖像(影象做了歸一化操作):