基于Python的图像数据增强Data Augmentation解析
作者:SiyuanChen 发布时间:2022-12-21 11:50:31
1.1 简介
深层神经网络一般都需要大量的训练数据才能获得比较理想的结果。在数据量有限的情况下,可以通过数据增强(Data Augmentation)来增加训练样本的多样性, 提高模型鲁棒性,避免过拟合。
在计算机视觉中,典型的数据增强方法有翻转(Flip),旋转(Rotat ),缩放(Scale),随机裁剪或补零(Random Crop or Pad),色彩抖动(Color jittering),加噪声(Noise)
笔者在跟进视频及图像中的人体姿态检测和关键点追踪(Human Pose Estimatiion and Tracking in videos)的项目。因此本文的数据增强仅使用——翻转(Flip),旋转(Rotate ),缩放以及缩放(Scale)
2.1 裁剪(Crop)
image.shape--([3, width, height])一个视频序列中的一帧图片,裁剪前大小不统一
bbox.shape--([4,])人体检测框,用于裁剪
x.shape--([1,13]) 人体13个关键点的所有x坐标值
y.shape--([1,13])人体13个关键点的所有y坐标值
def crop(image, bbox, x, y, length):
x, y, bbox = x.astype(np.int), y.astype(np.int), bbox.astype(np.int)
x_min, y_min, x_max, y_max = bbox
w, h = x_max - x_min, y_max - y_min
# Crop image to bbox
image = image[y_min:y_min + h, x_min:x_min + w, :]
# Crop joints and bbox
x -= x_min
y -= y_min
bbox = np.array([0, 0, x_max - x_min, y_max - y_min])
# Scale to desired size
side_length = max(w, h)
f_xy = float(length) / float(side_length)
image, bbox, x, y = Transformer.scale(image, bbox, x, y, f_xy)
# Pad
new_w, new_h = image.shape[1], image.shape[0]
cropped = np.zeros((length, length, image.shape[2]))
dx = length - new_w
dy = length - new_h
x_min, y_min = int(dx / 2.), int(dy / 2.)
x_max, y_max = x_min + new_w, y_min + new_h
cropped[y_min:y_max, x_min:x_max, :] = image
x += x_min
y += y_min
x = np.clip(x, x_min, x_max)
y = np.clip(y, y_min, y_max)
bbox += np.array([x_min, y_min, x_min, y_min])
return cropped, bbox, x.astype(np.int), y.astype(np.int)
2.2 缩放(Scale)
image.shape--([3, 256, 256])一个视频序列中的一帧图片,裁剪后输入网络为256*256
bbox.shape--([4,])人体检测框,用于裁剪
x.shape--([1,13]) 人体13个关键点的所有x坐标值
y.shape--([1,13])人体13个关键点的所有y坐标值
f_xy--缩放倍数
def scale(image, bbox, x, y, f_xy):
(h, w, _) = image.shape
h, w = int(h * f_xy), int(w * f_xy)
image = resize(image, (h, w), preserve_range=True, anti_aliasing=True, mode='constant').astype(np.uint8)
x = x * f_xy
y = y * f_xy
bbox = bbox * f_xy
x = np.clip(x, 0, w)
y = np.clip(y, 0, h)
return image, bbox, x, y
2.3 翻转(fillip)
这里是将图片围绕对称轴进行左右翻转(因为人体是左右对称的,在关键点检测中有助于防止模型过拟合)
def flip(image, bbox, x, y):
image = np.fliplr(image).copy()
w = image.shape[1]
x_min, y_min, x_max, y_max = bbox
bbox = np.array([w - x_max, y_min, w - x_min, y_max])
x = w - x
x, y = Transformer.swap_joints(x, y)
return image, bbox, x, y
翻转前:
翻转后:
2.4 旋转(rotate)
angle--旋转角度
def rotate(image, bbox, x, y, angle):
# image - -(256, 256, 3)
# bbox - -(4,)
# x - -[126 129 124 117 107 99 128 107 108 105 137 155 122 99]
# y - -[209 176 136 123 178 225 65 47 46 24 44 64 49 54]
# angle - --8.165648811999333
# center of image [128,128]
o_x, o_y = (np.array(image.shape[:2][::-1]) - 1) / 2.
width,height = image.shape[0],image.shape[1]
x1 = x
y1 = height - y
o_x = o_x
o_y = height - o_y
image = rotate(image, angle, preserve_range=True).astype(np.uint8)
r_x, r_y = o_x, o_y
angle_rad = (np.pi * angle) /180.0
x = r_x + np.cos(angle_rad) * (x1 - o_x) - np.sin(angle_rad) * (y1 - o_y)
y = r_y + np.sin(angle_rad) * (x1 - o_x) + np.cos(angle_rad) * (y1 - o_y)
x = x
y = height - y
bbox[0] = r_x + np.cos(angle_rad) * (bbox[0] - o_x) + np.sin(angle_rad) * (bbox[1] - o_y)
bbox[1] = r_y + -np.sin(angle_rad) * (bbox[0] - o_x) + np.cos(angle_rad) * (bbox[1] - o_y)
bbox[2] = r_x + np.cos(angle_rad) * (bbox[2] - o_x) + np.sin(angle_rad) * (bbox[3] - o_y)
bbox[3] = r_y + -np.sin(angle_rad) * (bbox[2] - o_x) + np.cos(angle_rad) * (bbox[3] - o_y)
return image, bbox, x.astype(np.int), y.astype(np.int)
旋转前:
旋转后:
3 结果(output)
数据增强前的原图:
数据增强后:
来源:https://www.cnblogs.com/siyuan1998/p/10686616.html
猜你喜欢
- 由于最近需要做项目,需要进行分词等,查了资料之后,发现python NLTK很强大,于是就想试试看。在网上找了很多安装资料,都不太完整,下载
- 一、前言关于什么是Dapper(详细入口),在此不做赘述;本文仅对Dapper在.Net Core中的使用作扼要说明,所陈代码以示例讲解为主
- 类和对象类和函数一样都是Python中的对象。当一个类定义完成之后,Python将创建一个“类对象”并将其赋值给一个同名变量。类是type类
- 如题:在python的函数调用中需要记录时间,下面是记录毫秒时间的方法。import datetimeimport timet1 = dat
- 前几天,看到有人写了个superLink的东东,主要的做什么用呢?我们有时会给在大块元素加个window.location='htt
- 大家好,我是安果!目前公司使用 Jira 作为项目管理工具,在每一次迭代完成后的复盘会上,我们都需要针对本次迭代的&nb
- 一、前言随着三胎政策的开放,人们对于生娃的讨论也逐渐热烈了起来,经常能够在各大社交媒体当中看到相关的话题,而随着时间慢慢地流逝,中国的首批“
- 本文主要介绍了数据处理方面的内容,希望大家仔细阅读。一、数据分析得到了以下列字符串开头的文本数据,我们需要进行处理二、回滚我们需要对http
- join 方法用于连接字符串数组 s = ['a', 'b', 'c', 'd
- 一、概述KNN(K-最近邻)算法是相对比较简单的机器学习算法之一,它主要用于对事物进行分类。用比较官方的话来说就是:给定一个训练数据集,对新
- 已知有一个XML文件(bookstore.xml)如下:<?xml version="1.0" encoding=
- 从文本文件中调出记录出现丢失换行?解决方法:<%Dim fsDim tsset fs=Server.Cr
- 如果使用Python做大型海量数据批量任务时,并且backend用mongodb做数据储存时,常常面临大量读写数据库的情况。尤其是大量更新任
- 原理:建立一个任务队列,然多个线程都从这个任务队列中取出任务然后执行,当然任务队列要加锁,详细请看代码import threadingimp
- 简单的说,组织体系指的就是组织信息的方式。看了一堆拗口的定义后,我根据自己的理解画了下面这个图:从上图看出,同样的图形元素由于采用了不同的组
- 在使用Keras搭建验证码识别模型时,需要大量的验证码图片。在这里,使用captcha模块生成验证码图片,验证码图片名称为验证码上显示的字符
- 本文研究的主要是Python subprocess模块的相关内容,具体如下。在学习这个模块前,我们先用Python的help()函数查看一下
- matplotlib中的字体文件被封装在font_manager这个子模块中,fontManager.ttflist这个列表涵盖了所有Mat
- 我们已经知道Application和Session对象的OnStart、OnEnd事件的脚本,都必须是在 Global.asa&n
- jQuery的选择器是CSS 1-3,XPath的结合物。jQuery提取这二种查询语言最好的部分,融合后创造出了最终的jQuery表达式查