imgaug数据增强
imgaug数据增强
最近在做⽬标检测的项⽬,由于⼿⾥的数据⽆法很好的满⾜深度学习的要求,所以需要⽤到数据增强的⼿段来扩充⾃⼰的数据集,这⾥我采⽤基于python的imgaug包,具体的调⽤⽅法⽹上都很详细,这⾥不再细说,接下来主要讲两点。
1.环境配置
⼀般直接安装imgaug会失败,因为安装之前需要安装其它⼏个依赖的包才⾏,如下:
six numpy scipy Pillow matplotlib scikit-image opencv-python imageio Shapely
其中⼤部分包在安装anaconda时都已经下载好了,可以⽤pip list查看缺什么包,⼀般缺
scikit-image opencv-python Shapely这三个,其中opencv-python Shapely这两个直接下载很慢,最好离线下载下来,在本地安装(新⼿对于怎么在本地可以百度)。
www.lfd.uci.edu/~gohlke/pythonlibs/#shapely
这是下载⽹站。
如果还是很慢,可以评论区留下邮箱,有空给你们发。
2.数据增强实⽤程序
我⾃⼰原始数据⼤概2000多张,分为六类,分到每⼀类就不太够了,所以需要进⾏数据扩充。
⾸先将⾃⼰的2000张图像进⾏标注,我是按照VOC格式标注的,会⽣成相应的xml⽂件,然后下⾯的代码就会⾃动扩充数据集,⽽且连xml⽂件也会⾃动⽣成,完全不⽤⾃⼰再标注了,⽐较省事。
ElementTree as ET
import pickle
import os
from os import getcwd
import numpy as np
from PIL import Image
import shutil
import matplotlib.pyplot as plt
import imgaug as ia
from imgaug import augmenters as iaa
ia.seed(1)
def read_xml_annotation(root, image_id):
in_file =open(os.path.join(root, image_id), encoding='UTF-8')
tree = ET.parse(in_file)
root = t()
bndboxlist =[]
for object in root.findall('object'):# 到root节点下的所有country节点
bndbox =object.find('bndbox')# ⼦节点下节点rank的值
xmin =int(bndbox.find('xmin').text)
xmax =int(bndbox.find('xmax').text)
ymin =int(bndbox.find('ymin').text)
ymax =int(bndbox.find('ymax').text)
# print(xmin,ymin,xmax,ymax)
bndboxlist.append([xmin, ymin, xmax, ymax])
# print(bndboxlist)
# ndbox = root.find('object').find('bndbox')
return bndboxlist
# (506.0000, 330.0000, 528.0000, 348.0000) -> (520.4747, 381.5080, 540.5596, 398.6603)
def change_xml_annotation(root, image_id, new_target):
new_xmin = new_target[0]
new_ymin = new_target[1]
new_xmax = new_target[2]
new_xmax = new_target[2]
new_ymax = new_target[3]
in_file =open(os.path.join(root,str(image_id)+'.xml'), encoding='UTF-8')# 这⾥root分别由两个意思    tree = ET.parse(in_file)
xmlroot = t()
object= xmlroot.find('object')
bndbox =object.find('bndbox')
xmin = bndbox.find('xmin')
< =str(new_xmin)
ymin = bndbox.find('ymin')
< =str(new_ymin)
xmax = bndbox.find('xmax')
< =str(new_xmax)
ymax = bndbox.find('ymax')
< =str(new_ymax)
tree.write(os.path.join(root,str("%06d"%(str(id)+'.xml'))))
def change_xml_list_annotation(root, image_id, new_target, saveroot,id):
in_file =open(os.path.join(root,str(image_id)+'.xml'), encoding='UTF-8')# 这⾥root分别由两个意思    tree = ET.parse(in_file)
elem = tree.find('filename')
< =(str("%06d"%int(id))+'.jpg')
xmlroot = t()
index =0
for object in xmlroot.findall('object'):# 到root节点下的所有country节点
bndbox =object.find('bndbox')# ⼦节点下节点rank的值
# xmin = int(bndbox.find('xmin').text)
# xmax = int(bndbox.find('xmax').text)
# ymin = int(bndbox.find('ymin').text)
# ymax = int(bndbox.find('ymax').text)
new_xmin = new_target[index][0]
new_ymin = new_target[index][1]
new_xmax = new_target[index][2]
new_ymax = new_target[index][3]
xmin = bndbox.find('xmin')
< =str(new_xmin)
ymin = bndbox.find('ymin')
< =str(new_ymin)
xmax = bndbox.find('xmax')
< =str(new_xmax)
ymax = bndbox.find('ymax')
< =str(new_ymax)
index +=1
print("index=", index)
tree.write(os.path.join(saveroot,str("%06d"%int(id))+'.xml'))
def mkdir(path):
# 去除⾸位空格
path = path.strip()
# 去除尾部 \ 符号
path = path.rstrip("\\")
# 判断路径是否存在
# 存在    True
# 不存在  False
isExists = ists(path)
# 判断结果
# 判断结果
if not isExists:
# 如果不存在则创建⽬录
# 创建⽬录操作函数
os.makedirs(path)
print(path +' 创建成功')
return True
else:
# 如果⽬录存在则不创建,并提⽰⽬录已存在
print(path +' ⽬录已存在')
return False
if __name__ =="__main__":
IMG_DIR ="C:\shujuzengqiqng\JPEGImages"
XML_DIR ="C:\shujuzengqiqng\Annotations"
AUG_XML_DIR ="C:\shujuzengqiqng\AUG_XML"# 存储增强后的XML⽂件夹路径
try:
<(AUG_XML_DIR)
except FileNotFoundError as e:
a =1
mkdir(AUG_XML_DIR)
AUG_IMG_DIR ="C:\shujuzengqiqng\AUG_IMG"# 存储增强后的影像⽂件夹路径
try:
<(AUG_IMG_DIR)
error parse newexcept FileNotFoundError as e:
a =1
mkdir(AUG_IMG_DIR)
AUGLOOP =3# 每张影像增强的数量
boxes_img_aug_list =[]
new_bndbox =[]
new_bndbox_list =[]
# 影像增强
seq = iaa.Sequential([
iaa.Fliplr(0.5),# 镜像,50%的照⽚做镜像处理
iaa.Flipud(0.5),
iaa.ContrastNormalization((0.75,1.5), per_channel=True),####0.75-1.5随机数值为alpha,对图像进⾏对⽐度增强,该alpha应⽤于每个通道        iaa.AdditiveGaussianNoise(loc=0, scale=(0.0,0.1*255), per_channel=0.5),
#### loc 噪声均值,scale噪声⽅差,50%的概率,对图⽚进⾏添加⽩噪声并应⽤于每个通道
iaa.Multiply((0.8,1.2), per_channel=0.2),####20%的图⽚像素值乘以0.8-1.2中间的数值,⽤以增加图⽚明亮度或改变颜⾊
])
for root, sub_folders, files in os.walk(XML_DIR):
for name in files:
bndbox = read_xml_annotation(XML_DIR, name)
print(os.path.join(IMG_DIR, name[:-4]+'.jpg'))
for epoch in range(AUGLOOP):
seq_det = _deterministic()# 保持坐标和图像同步改变,⽽不是随机
# 读取图⽚
img = Image.open(os.path.join(IMG_DIR, name[:-4]+'.jpg'))
# sp = img.size
img = np.asarray(img)
img = np.asarray(img)
# bndbox 坐标增强
for i in range(len(bndbox)):
bbs = ia.BoundingBoxesOnImage([
ia.BoundingBox(x1=bndbox[i][0], y1=bndbox[i][1], x2=bndbox[i][2], y2=bndbox[i][3]),
], shape=img.shape)
bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]
boxes_img_aug_list.append(bbs_aug)
# new_bndbox_list:[[x1,y1,x2,y2],...[],[]]
n_x1 =int(max(1,min(img.shape[1], bbs_aug.bounding_boxes[0].x1)))
n_y1 =int(max(1,min(img.shape[0], bbs_aug.bounding_boxes[0].y1)))
n_x2 =int(max(1,min(img.shape[1], bbs_aug.bounding_boxes[0].x2)))
n_y2 =int(max(1,min(img.shape[0], bbs_aug.bounding_boxes[0].y2)))
if n_x1 ==1and n_x1 == n_x2:
n_x2 +=1
if n_y1 ==1and n_y2 == n_y1:
n_y2 +=1
if n_x1 >= n_x2 or n_y1 >= n_y2:
print('error', name)
new_bndbox_list.append([n_x1, n_y1, n_x2, n_y2])
# 存储变化后的图⽚
image_aug = seq_det.augment_images([img])[0]
path = os.path.join(AUG_IMG_DIR,str("%06d"%(len(files)+int(name[:-4])+ epoch *250))+'.jpg')
image_auged = bbs.draw_on_image(image_aug, thickness=0)>>>>>>
>#                    Image.fromarray(image_auged).save(path)
# 存储变化后的XML
change_xml_list_annotation(XML_DIR, name[:-4], new_bndbox_list, AUG_XML_DIR,
len(files)+int(name[:-4])+ epoch *250)
print(str("%06d"%(len(files)+int(name[:-4])+ epoch *250))+'.jpg')
new_bndbox_list =[]
代码讲解很详细,只需要安装好imgaug包,替换⾃⼰的绝对路径就⾏。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论