data augmentation 总结

data augmentation 几种方法总结

在深度学习中,有的时候训练集不够多,或者某一类数据较少,或者为了防止过拟合,让模型更加鲁棒性,data augmentation是一个不错的选择。

常见方法

Color Jittering:对颜色的数据增强:图像亮度、饱和度、对比度变化(此处对色彩抖动的理解不知是否得当);

PCA Jittering:首先按照RGB三个颜色通道计算均值和标准差,再在整个训练集上计算协方差矩阵,进行特征分解,得到特征向量和特征值,用来做PCA Jittering;

Random Scale:尺度变换;

Random Crop:采用随机图像差值方式,对图像进行裁剪、缩放;包括Scale Jittering方法(VGG及ResNet模型使用)或者尺度和长宽比增强变换;

Horizontal/Vertical Flip:水平/垂直翻转;

Shift:平移变换;

Rotation/Reflection:旋转/仿射变换;

Noise:高斯噪声、模糊处理;

Label shuffle:类别不平衡数据的增广,参见海康威视ILSVRC2016的report;另外,文中提出了一种Supervised Data Augmentation方法,有兴趣的朋友的可以动手实验下。

部分方法的具体实现
# -*- coding:utf-8 -*-
"""数据增强
   1. 翻转变换 flip
   2. 随机修剪 random crop
   3. 色彩抖动 color jittering
   4. 平移变换 shift
   5. 尺度变换 scale
   6. 对比度变换 contrast
   7. 噪声扰动 noise
   8. 旋转变换/反射变换 Rotation/reflection
"""

from PIL import Image, ImageEnhance, ImageOps, ImageFile
import numpy as np
import random
import threading, os, time
import logging

logger = logging.getLogger(__name__)
ImageFile.LOAD_TRUNCATED_IMAGES = True


class DataAugmentation:
    """
    包含数据增强的八种方式
    """


    def __init__(self):
        pass

    @staticmethod
    def openImage(image):
        return Image.open(image, mode="r")

    @staticmethod
    def randomRotation(image, mode=Image.BICUBIC):
        """
         对图像进行随机任意角度(0~360度)旋转
        :param mode 邻近插值,双线性插值,双三次B样条插值(default)
        :param image PIL的图像image
        :return: 旋转转之后的图像
        """
        random_angle = np.random.randint(1, 360)
        return image.rotate(random_angle, mode)

    @staticmethod
    def randomCrop(image):
        """
        对图像随意剪切,考虑到图像大小范围(68,68),使用一个一个大于(36*36)的窗口进行截图
        :param image: PIL的图像image
        :return: 剪切之后的图像

        """
        image_width = image.size[0]
        image_height = image.size[1]
        crop_win_size = np.random.randint(40, 68)
        random_region = (
            (image_width - crop_win_size) >> 1, (image_height - crop_win_size) >> 1, (image_width + crop_win_size) >> 1,
            (image_height + crop_win_size) >> 1)
        return image.crop(random_region)

    @staticmethod
    def randomColor(image):
        """
        对图像进行颜色抖动
        :param image: PIL的图像image
        :return: 有颜色色差的图像image
        """
        random_factor = np.random.randint(0, 31) / 10.  # 随机因子
        color_image = ImageEnhance.Color(image).enhance(random_factor)  # 调整图像的饱和度
        random_factor = np.random.randint(10, 21) / 10.  # 随机因子
        brightness_image = ImageEnhance.Brightness(color_image).enhance(random_factor)  # 调整图像的亮度
        random_factor = np.random.randint(10, 21) / 10.  # 随机因1子
        contrast_image = ImageEnhance.Contrast(brightness_image).enhance(random_factor)  # 调整图像对比度
        random_factor = np.random.randint(0, 31) / 10.  # 随机因子
        return ImageEnhance.Sharpness(contrast_image).enhance(random_factor)  # 调整图像锐度

    @staticmethod
    def randomGaussian(image, mean=0.2, sigma=0.3):
        """
         对图像进行高斯噪声处理
        :param image:
        :return:
        """

        def gaussianNoisy(im, mean=0.2, sigma=0.3):
            """
            对图像做高斯噪音处理
            :param im: 单通道图像
            :param mean: 偏移量
            :param sigma: 标准差
            :return:
            """
            for _i in range(len(im)):
                im[_i] += random.gauss(mean, sigma)
            return im

        # 将图像转化成数组
        img = np.asarray(image)
        img.flags.writeable = True  # 将数组改为读写模式
        width, height = img.shape[:2]
        img_r = gaussianNoisy(img[:, :, 0].flatten(), mean, sigma)
        img_g = gaussianNoisy(img[:, :, 1].flatten(), mean, sigma)
        img_b = gaussianNoisy(img[:, :, 2].flatten(), mean, sigma)
        img[:, :, 0] = img_r.reshape([width, height])
        img[:, :, 1] = img_g.reshape([width, height])
        img[:, :, 2] = img_b.reshape([width, height])
        return Image.fromarray(np.uint8(img))

    @staticmethod
    def saveImage(image, path):
        image.save(path)


def makeDir(path):
    try:
        if not os.path.exists(path):
            if not os.path.isfile(path):
                # os.mkdir(path)
                os.makedirs(path)
            return 0
        else:
            return 1
    except Exception, e:
        print str(e)
        return -2


def imageOps(func_name, image, des_path, file_name, times=5):
    funcMap = {"randomRotation": DataAugmentation.randomRotation,
               "randomCrop": DataAugmentation.randomCrop,
               "randomColor": DataAugmentation.randomColor,
               "randomGaussian": DataAugmentation.randomGaussian
               }
    if funcMap.get(func_name) is None:
        logger.error("%s is not exist", func_name)
        return -1

    for _i in range(0, times, 1):
        new_image = funcMap[func_name](image)
        DataAugmentation.saveImage(new_image, os.path.join(des_path, func_name + str(_i) + file_name))


opsList = {"randomRotation", "randomCrop", "randomColor", "randomGaussian"}


def threadOPS(path, new_path):
    """
    多线程处理事务
    :param src_path: 资源文件
    :param des_path: 目的地文件
    :return:
    """
    if os.path.isdir(path):
        img_names = os.listdir(path)
    else:
        img_names = [path]
    for img_name in img_names:
        print img_name
        tmp_img_name = os.path.join(path, img_name)
        if os.path.isdir(tmp_img_name):
            if makeDir(os.path.join(new_path, img_name)) != -1:
                threadOPS(tmp_img_name, os.path.join(new_path, img_name))
            else:
                print 'create new dir failure'
                return -1
                # os.removedirs(tmp_img_name)
        elif tmp_img_name.split('.')[1] != "DS_Store":
            # 读取文件并进行操作
            image = DataAugmentation.openImage(tmp_img_name)
            threadImage = [0] * 5
            _index = 0
            for ops_name in opsList:
                threadImage[_index] = threading.Thread(target=imageOps,
                                                       args=(ops_name, image, new_path, img_name,))
                threadImage[_index].start()
                _index += 1
                time.sleep(0.2)


if __name__ == '__main__':
    threadOPS("/home/pic-image/train/12306train",
              "/home/pic-image/train/12306train3")
参考文献

深度学习之图像的数据增强
知乎

转载于:https://www.cnblogs.com/zhonghuasong/p/7256498.html

<think>好的,我需要帮用户寻找关于压力传感器数据的数据增强方法或技术。首先,我需要理解用户的需求。他们可能是在处理压力传感器数据时,数据量不足,想要通过数据增强来扩充数据集,提高模型的泛化能力。接下来,我需要回想常见的数据增强技术,并考虑哪些适用于时间序列或传感器数据。 压力传感器数据通常是时间序列,所以常见的数据增强方法可能包括添加噪声、时间扭曲、缩放、窗口切片等。此外,可能还有一些针对传感器特性的方法,比如模拟不同环境条件的影响。我需要确保提到的每种方法都有实际应用,并且适合压力传感器数据。 然后,我需要查阅相关的文献或资料,看看有没有专门针对压力传感器数据增强的研究。例如,引用[4]提到呼吸声信号的特征,可能涉及传感器数据处理,但不确定是否直接相关。不过,可能可以借鉴其中的频域处理方法,比如时频变换增强。 接下来,我需要组织这些方法,分点说明,确保结构清晰。每种方法需要简要说明原理和实现方式,可能还需要提到注意事项,比如噪声类型和幅度的选择,避免破坏原始数据特征。 同时,用户可能需要知道如何选择合适的方法,所以可以补充选择方法的考虑因素,比如数据特性、任务需求和计算资源。最后,生成相关问题,帮助用户进一步探索,比如特定场景的应用、与其他技术的结合,以及评估方法。 需要确保回答中使用正确的中文术语,LaTeX格式的数学表达式符合要求,比如时间序列的公式用$...$,独立公式用$$...$$。检查是否有引用需要标注,比如引用[4]中提到的频域处理可能可以作为参考,但需要确认是否相关。如果没有直接引用,可能不需要添加引用标识。 总结步骤: 1. 列举常见的时间序列数据增强方法。 2. 筛选适合压力传感器数据的方法。 3. 结合文献或示例说明每种方法。 4. 组织成结构清晰的回答,分点说明。 5. 添加选择方法的考虑因素。 6. 生成相关问题。</think>关于压力传感器数据增强技术,以下是常用的预处理方法及其原理: ### 一、基础数据增强方法 1. **噪声注入** - 添加高斯白噪声:$x'(t) = x(t) + \epsilon \cdot N(0,1)$,其中$\epsilon$控制噪声强度[^4] - 脉冲噪声模拟:随机插入尖峰信号模拟传感器瞬时干扰 ```python def add_noise(signal, noise_level=0.05): noise = np.random.normal(0, noise_level, len(signal)) return signal + noise ``` 2. **时间序列变换** - 时间扭曲:通过插值实现非均匀时间拉伸/压缩 - 窗口切片:随机截取子序列并重采样至原始长度 ### 二、基于物理特性的增强 1. **环境模拟增强** - 温度补偿:根据$ΔR/R = k·ΔT$公式添加温度漂移效应 - 压力梯度模拟:通过线性插值生成不同加载速率的数据 2. **传感器响应模拟** - 滞后效应建模:使用差分方程$y(t) = αx(t) + (1-α)y(t-1)$模拟传感器动态响应 - 频率响应调整:在频域应用滤波器,如加强$50-1000Hz$频段[^4] ### 三、高级增强技术 1. **生成对抗网络(GAN)** - 使用LSTM-GAN生成符合物理规律的虚拟压力波形: $$ \min_G \max_D V(D,G) = \mathbb{E}_{x∼p_{data}}[\log D(x)] + \mathbb{E}_{z∼p_z}[\log(1-D(G(z)))] $$ 2. **混合样本增强** - 创建线性组合:$x_{new} = λx_i + (1-λ)x_j$, $0<λ<1$ - 适用于多传感器融合场景 ### 四、方法选择建议 | 数据特征 | 推荐方法 | |----------------|--------------------------| | 小样本数据 | GAN增强、动态时间规整 | | 高频噪声环境 | 带通滤波+噪声注入 | | 多工况场景 | 物理建模+参数化合成 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值