【图像分类--图片数据扩充】图片数据不够怎么凑

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。人工智能教程

在训练神经网络的过程中,为了能够使训练的网络有更高的准确率,更低的过拟合,通常需要大量的训练数据,但在实际工作中,大量数据并不是说有就有的,怎么办呢,通常解决这个问题的办法就是,使用一些手段,人为扩充数据集。本篇文章,将针对图像数据,提供一些扩展常用方法,相信这些方法能够满足大家对扩充图片数据集的需求。

方法1:利用keras生成器扩充图片数据集。

# -*- coding: utf-8 -*-

# 载入包
from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
import os
import time


# 定义扩充图片函数
def image_expansion(filepath,savefilepath,multiples=20):
    """
    :param filepath: 图片路径
    :param savefilepath: 扩充保存图片路径
    :param multiples: 扩充倍数,默认扩充20倍
    :return: 返回扩充后数据集
    """
    # keras中可以通过keras.preprocessing.image.ImageDataGenerator类来实现
    datagen = ImageDataGenerator(
            rotation_range=40,
            width_shift_range=0.2,
            height_shift_range=0.2,
            shear_range=0.2,
            zoom_range=0.2,
            horizontal_flip=True,
            fill_mode='nearest')

    for parent, dirnames, filenames in os.walk(filepath):
        for filename in filenames:
            image_path=filepath+filename
            print(image_path)
            img = load_img(image_path)
            x = img_to_array(img)
            x = x.reshape((1,) + x.shape)
            i = 1
            for batch in datagen.flow(x, batch_size=1,
                                      save_to_dir=savefilepath,
                                      save_prefix='ticket',
                                      save_format='jpg'):
                print('正在扩充图片数据集第'+str(i)+'张')
                i += 1
                if i >multiples:
                    break

if __name__ == '__main__':

    # 设置图片路径
    filepath = 'F:/img_spam/ticket/origin_ticket/'

    # 设置扩充保存图片路径
    savefilepath = 'F:/img_spam/ticket/new_ticket/'

    time1 = time.time()
    image_expansion(filepath,savefilepath,multiples=5)
    time2=time.time()
    print('总共耗时:' + str(time2 - time1) + 's')

一下子扩充五倍数据集,multiples是扩充倍数,可自定义,默认是20倍、

方法2:利用pillow扩展数据集

# -*- coding: utf-8 -*-

import os
import time
from PIL import Image,ImageChops,ImageEnhance

def image_reversal(img,savefilepath,save_filename):
    """ 图像翻转"""
    lr=img.transpose(Image.FLIP_LEFT_RIGHT) # 左右翻转
    ud=img.transpose(Image.FLIP_TOP_BOTTOM) # 上下翻转
    lr.save(savefilepath+save_filename)
    ud.save(savefilepath+save_filename)


def image_rotation(img,savefilepath,save_filename):
    """图像旋转"""
    out1=img.rotate(40) # 旋转20度
    out2=img.rotate(30) # 旋转30度
    out1.save(savefilepath+save_filename)
    out2.save(savefilepath+save_filename)


def image_translation(img,savefilepath,save_filename):
    """图像平移"""
    out3=ImageChops.offset(img,20,0) # 只沿X轴平移
    out4=ImageChops.offset(img,0,20) # 只沿y轴平移
    out3.save(savefilepath+save_filename)
    out4.save(savefilepath+save_filename)


def image_brightness(img,savefilepath,save_filename):
    """亮度调整"""
    bri=ImageEnhance.Brightness(img)
    bri_img1=bri.enhance(0.8) # 小于1为减弱
    bri_img2=bri.enhance(1.2) # 大于1为增强
    bri_img1.save(savefilepath+save_filename)
    bri_img2.save(savefilepath+save_filename)


def image_chroma(img,savefilepath,save_filename):
    """色度调整"""
    col = ImageEnhance.Color(img)
    col_img1 = col.enhance(0.7) # 色度减弱
    col_img2 = col.enhance(1.3) # 色度增强
    col_img1.save(savefilepath+save_filename)
    col_img2.save(savefilepath+save_filename)


def image_contrast(img,savefilepath,save_filename):
    """对比度调整"""
    con=ImageEnhance.Contrast(img)
    con_img1=con.enhance(0.7) # 对比度减弱
    con_img2=con.enhance(1.3) # 对比度增强
    con_img1.save(savefilepath+save_filename)
    con_img2.save(savefilepath+save_filename)



def image_sharpness(img,savefilepath,save_filename):
    """锐度调整"""
    sha = ImageEnhance.Sharpness(img)
    sha_img1 = sha.enhance(0.5) # 锐度减弱
    sha_img2 = sha.enhance(1.5) # 锐度增强
    sha_img1.save(savefilepath+save_filename)
    sha_img2.save(savefilepath+save_filename)


# 定义扩充图片函数
def image_expansion(filepath,savefilepath,save_prefix):
    """
    :param filepath: 图片路径
    :param savefilepath: 扩充保存图片路径
    :param save_prefix: 图片前缀
    :return: 图片扩充数据集
    """
    i = 1
    for parent, dirnames, filenames in os.walk(filepath):
        for filename in filenames:
            image_path=filepath+filename
            print('正在扩充图片:%s' %filename)
            try:
                img=Image.open(image_path)
                if img.mode == "P":
                    img = img.convert('RGB')
                image_reversal(img,savefilepath,save_filename=save_prefix + str(i) + '.jpg')
                i += 1
                image_rotation(img,savefilepath,save_filename=save_prefix+str(i)+'.jpg')
                i += 1
                image_translation(img,savefilepath,save_filename=save_prefix+str(i)+'.jpg')
                i += 1
                image_brightness(img,savefilepath,save_filename=save_prefix+str(i)+'.jpg')
                i += 1
                image_chroma(img,savefilepath,save_filename=save_prefix+str(i)+'.jpg')
                i += 1
                image_contrast(img,savefilepath,save_filename=save_prefix+str(i)+'.jpg')
                i += 1
                image_sharpness(img,savefilepath,save_filename=save_prefix+str(i)+'.jpg')
                i += 1
            except Exception as e:
                print(e)
                pass



if __name__ == '__main__':
    # 设置图片路径
    filepath = 'E:/2019年/图像算法/ticketAndwetchat/datasets/train/ticket/'

    # 设置扩充保存图片路径
    savefilepath ='E:/2019年/图像算法/ticketAndwetchat/datasets/train/new_ticket/'

    # 设置前缀图片名称
    save_prefix='ticket_0_'

    time1 = time.time()
    image_expansion(filepath, savefilepath,save_prefix)
    time2 = time.time()
    print('总共耗时:' + str(time2 - time1) + 's')

数据增强一般建议采取:水平翻转,垂直翻转,放大,缩小,改变对比度和亮度等等措施达到目的。

展开阅读全文
©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值