图像数据准备对神经网络与卷积神经网络模型训练有重要影响,当样本空间不够或者样本数量不足的时候会严重影响训练或者导致训练出来的模型泛化程度不够,识别率与准确率不高!本文将会带你学会如何对已有的图像数据进行数据增强,获取样本的多样性与数据的多样性从而为训练模型打下良好基础。在不改变图像类别的情况下,增加数据量,能提高模型的泛化能力。
       不同的视角,不同的大小,物体的形变问题,物体的遮挡问题,光照条件,背景复杂的问题,每一类中有多种形态的问题。 
而数据增广的思路也就是解决这个问题。数据增广如何增广就要从实际的问题出发,比如医学的图片基本上拍摄的时候视角是固定的,所以就不需要不同视角的增广。木纹检测中视角是不固定的,就需要不同的视角,不同的大小的增广,还需要应不同的光照条件对数据进行增广。
        自然图像的数据增广方式包括很多,如常用的水平翻转,一定程度的位移或者裁剪和颜色抖动。此外还可以尝试多种操作的组合, 例如同时做旋转和随机尺度变换,此外还可以把每个patch中所有像素在HSV颜色空间中的饱和度和明度提升0.25-4次幂方,乘以0.7-1.4之间的一个因子,再加一个-0.1-0.1之间的值。同样你可以在色调通道(H)对每张图片或patch的所有像素增加一个-0.1-0.1之间的值。

其他处理

  • shuffle,打乱数据进行训练是必须的,防止相邻样本有较强相关性。
  • 图像标准化,计算数据集的std与mean,而不是直接使用imagenet的std与mean
  • 增大图像的输入尺寸可获得客观的提升,使用了480*480的输入尺寸
  • 选择合适的迁移学习方式,本例进行全局finetune比只训练最后1层或几层好很多
  • 可以先用Adam快速收敛,后面阶段用SGD慢慢调
  • 模型融合,举办方在复赛限制最多只能用两个模型是明智的,初赛都有队伍用接近10个模型进行融合,如此刷分就没意义了
  • 对测试集图片进行增强,比如镜像,旋转,再预测并取平均。可以得到更鲁棒的结果。这里没有用到tencrop,因为样本有些特征在顶部或者底部,tencrop会将特征截走,导致成绩降低。

        随机获取9张28x28的大小的数据图像,然后进行处理,处理之后通过opencv来显示结果。加载mnisnt数据集,获取随机9张图像,显示的代码如下:

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
import cv2 as cv
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
batch_xs, batch_ys = mnist.train.next_batch(9)


def show_images(images_data, win_name):
    plot_image = np.zeros(shape=[96, 96], dtype=np.float32)
    for i in range(0, 9):
        col = i % 3
        row = i // 3
        plot_image[row*28:row*28+28, col*28:col*28+28] = images_data[i].reshape(28, 28)

    # show the plot
    cv.imshow(win_name, cv.resize(plot_image, (256, 256)))


batch_xs = batch_xs.reshape(batch_xs.shape[0], 1, 28, 28)
show_images(batch_xs, "batches")
sess = tf.Session()
print(batch_xs.shape)

图像分类的数据增强的比例有哪些 图像数据增广_数据集

图像标准化

深度学习训练-详解图像数据标准化与归一化

def standardization():
    results = np.copy(batch_xs)
    for i in range(9):
        image = sess.run(tf.image.per_image_standardization(batch_xs[i].reshape(28, 28, -1)))
        results[i, :, :, :] = image.reshape(-1, 28,28)
    show_images(results, "standardization")

翻转、旋转

图像几何变换通常包括图像的平移、翻转、旋转等操作,利用图像几何操作实现图像数据增强。

def random_flip():
    copy = np.copy(batch_xs)
    copy = np.squeeze(copy, axis=1)
    copy = np.expand_dims(copy, axis=3)
    flip_results = sess.run(tf.image.flip_left_right(copy))
    flip_results = np.squeeze(flip_results, axis=3)
    flip_results = np.expand_dims(flip_results, axis=1)
    print(flip_results.shape)
    show_images(flip_results, "flip_left_right")

旋转操作代码如下

def random_rotate():
    results = np.copy(batch_xs)
    for i in range(9):
        image = sess.run(tf.image.rot90(batch_xs[i].reshape(28, 28, -1), i%4+1))
        results[i, :, :, :] = image.reshape(-1, 28,28)
    show_images(results, "random_rotate")

随机亮度

随机亮度通过调整图像像素值改变图像亮度,这种方式对图像进行数据增强的代码如下:

def random_brightness():
    results = np.copy(batch_xs)
    for i in range(9):
        image = sess.run(tf.image.random_brightness(batch_xs[i].reshape(28, 28), 0.9))
        results[i, :, :, :] = image.reshape(-1, 28,28)
    show_images(results,"random_brightness")

随机对比度

随机对比度,通过调整图像对比度来对图像进行数据增强,代码实现如下:

def random_contrast():
    results = np.copy(batch_xs)
    for i in range(9):
        image = sess.run(tf.image.random_contrast(batch_xs[i].reshape(28, 28, -1), 0.85, 1.5))
        results[i, :, :, :] = image.reshape(-1, 28,28)
    show_images(results, "random_contrast")