Data Augmentation--数据增强解决你有限的数据集

参考来源–感兴趣请戳我戳我.
author是Bharath Raj
以下是翻译内容,有部分删减,感兴趣的可以读原文,该blog仅供学习。笔耕不易,互相交流。
image

can my “state-of-the-art” neural network perform well with the meagre amount of data I have?

Yes.我们的优化目的,是当参数沿着正确的方向调整时,模型的loss可以达到最低。

How do I get more data, if I don’t have “more data”?

因此,为了获得更多数据,我们只需要对现有数据集进行微小改动。轻微更改,例如翻转或翻译或轮换。无论如何,我们的神经网络会认为这些是不同的图像。
ball
image
卷积神经网络CNN,对放置在不同方向的对象,也能进行稳健的分类,即具有不变性的属性。更具体地,CNN对于平移,不同视角,尺度大小或光照等(或上述的组合)可以是不变的。
这基本上是数据增加的前提。在实际场景中,我们可能会在一组有限的条件下获取图像数据集。但是,我们的目标应用可能存在于各种条件下,例如不同的方向,位置,比例,亮度等。我们通过使用额外的合成对数据进行修改,并训练我们的神经网络来解释这些情况。

Can augmentation help even if I have lots of data?

是。它有助于增加数据集中的相关数据量。这与神经网络学习的方式有关。
Your neural network is only as good as the data you feed it.

Where do we augment data in our ML pipeline?

答案似乎很明显;我们在将数据提供给模型之前进行扩充吗?是的,但你有两个选择。一种选择是事先执行所有必要的转换,从根本上增加数据集的大小。另一种选择是在将小批量送到机器学习模型之前,在小批量上执行这些转换。
第一个选项称为离线扩充。对于相对较小的数据集,此方法是首选,因为您最终会将数据集的大小增加一个等于您执行的转换次数的因子(例如,通过翻转我的所有图像,我会增加数据集的大小系数为2)。
第二个选项称为在线增强,或即时增强。对于较大的数据集,此方法是首选,因为您无法承受大小的爆炸式增长。相反,您将对要提供给模型的迷你批次执行转换。一些机器学习框架支持在线增强,可以在GPU上加速。

Popular Augmentation Techniques

在本节中,我们将介绍一些常用但基本但功能强大的增强技术。在我们探索这些技术之前,为简单起见,让我们做一个假设。假设是,我们不需要考虑图像边界之外的东西。我们将使用以下技术,以便我们的假设是有效的。如果我们使用一种技术迫使我们猜出图像边界之外的东西,会发生什么?在这种情况下,我们需要插入一些信息。在我们介绍了增强类型之后,我们将详细讨论这个问题。对于这些技术中的每一种,我们还指定了数据集大小增加的因子(也称为数据增强因子)。

1、Flip

给你个图像自己体会!!!
flip

# NumPy.'img' = A single image.
flip_1 = np.fliplr(img)
# TensorFlow. 'x' = A placeholder for an image.
shape = [height, width, channels]
x = tf.placeholder(dtype = tf.float32, shape = shape)
flip_2 = tf.image.flip_up_down(x)
flip_3 = tf.image.flip_left_right(x)
flip_4 = tf.image.random_flip_up_down(x)
flip_5 = tf.image.ra
  • 64
    点赞
  • 440
    收藏
    觉得还不错? 一键收藏
  • 40
    评论
COCO数据集是一个非常广泛使用的计算机视觉数据集,它包含了各种各样的图像和标注,可以用于图像分类、目标检测、图像分割等任务。在使用COCO数据集时,数据增强是一个非常有用的技巧,可以增加数据的多样性,提高模型的泛化能力。 常见的数据增强方法包括:随机裁剪、缩放、旋转、翻转、色彩变换等等。 下面是一个使用Python和OpenCV库实现数据增强的示例代码: ```python import cv2 import numpy as np def random_crop(img, boxes, labels): """随机裁剪""" h, w, _ = img.shape if len(boxes) == 0: return img, boxes, labels max_box = np.max(boxes, axis=0) min_box = np.min(boxes, axis=0) max_l_trans = min_box[0] max_u_trans = min_box[1] max_r_trans = w - max_box[2] max_d_trans = h - max_box[3] crop_xmin = int(np.maximum(0, min_box[0] - np.random.uniform(0, max_l_trans))) crop_ymin = int(np.maximum(0, min_box[1] - np.random.uniform(0, max_u_trans))) crop_xmax = int(np.minimum(w, max_box[2] + np.random.uniform(0, max_r_trans))) crop_ymax = int(np.minimum(h, max_box[3] + np.random.uniform(0, max_d_trans))) img = img[crop_ymin : crop_ymax, crop_xmin : crop_xmax] boxes[:, [0, 2]] = boxes[:, [0, 2]] - crop_xmin boxes[:, [1, 3]] = boxes[:, [1, 3]] - crop_ymin boxes[:, [0, 2]] = np.clip(boxes[:, [0, 2]], 0, crop_xmax - crop_xmin) boxes[:, [1, 3]] = np.clip(boxes[:, [1, 3]], 0, crop_ymax - crop_ymin) labels = labels[np.where((boxes[:, 2] - boxes[:, 0]) > 0)] boxes = boxes[np.where((boxes[:, 2] - boxes[:, 0]) > 0)] return img, boxes, labels def random_flip(img, boxes): """随机翻转""" if np.random.uniform(0, 1) < 0.5: img = cv2.flip(img, 1) boxes[:, [0, 2]] = img.shape[1] - boxes[:, [2, 0]] return img, boxes def random_scale(img, boxes, labels): """随机缩放""" scale = np.random.uniform(0.8, 1.2) h, w, _ = img.shape img = cv2.resize(img, (int(w * scale), int(h * scale))) boxes[:, :4] *= scale return img, boxes, labels def random_rotate(img, boxes): """随机旋转""" angle = np.random.uniform(-10, 10) h, w, _ = img.shape cx, cy = w // 2, h // 2 rot_mat = cv2.getRotationMatrix2D((cx, cy), angle, 1.0) cos, sin = np.abs(rot_mat[0, 0]), np.abs(rot_mat[0, 1]) nW = int((h * sin) + (w * cos)) nH = int((h * cos) + (w * sin)) rot_mat[0, 2] += (nW / 2) - cx rot_mat[1, 2] += (nH / 2) - cy img = cv2.warpAffine(img, rot_mat, (nW, nH), flags=cv2.INTER_LINEAR) boxes[:, :2] = np.dot(boxes[:, :2], rot_mat.T) boxes[:, 2:4] = np.dot(boxes[:, 2:4], rot_mat.T) boxes[:, 0::2] = np.clip(boxes[:, 0::2], 0, nW) boxes[:, 1::2] = np.clip(boxes[:, 1::2], 0, nH) return img, boxes def random_distort(img): """随机色彩变换""" img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) img[..., 0] += np.random.randint(-10, 10) img[..., 1] += np.random.randint(-10, 10) img[..., 2] += np.random.randint(-10, 10) img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR) img = np.clip(img, 0, 255) return img def data_augmentation(img, boxes, labels): """数据增强""" img, boxes, labels = random_crop(img, boxes, labels) img, boxes = random_flip(img, boxes) img, boxes, labels = random_scale(img, boxes, labels) img, boxes = random_rotate(img, boxes) img = random_distort(img) return img, boxes, labels ``` 上面的代码实现了随机裁剪、随机翻转、随机缩放、随机旋转、随机色彩变换等增强方法。你可以根据自己的需求进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 40
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值