目标检测过程中常遇见目标种类或者数量不足的情况,在此情况下,我们常常使用数据增强的策略去扩充数据,以获取更高的检测精度。
如下介绍两个数据增强的常用库
1、imgaug库
(1)安装
直接安装pypi最新的版本
pip install imgaug
如果遇见Shapely导致的错误,则进行如下操作:
pip install six numpy scipy Pillow matplotlib scikit-image opencv-python imageio
pip install --no-dependencies imgaug
当然你也可以直接使用github仓库的release版本进行安装,如下:
pip install six numpy scipy Pillow matplotlib scikit-image opencv-python imageio
pip install --no-dependencies imgaug
当然,要想使用imgaug里面的augmenters库,需要额外安装imagecorruptions包:
pip install imagecorruptions
(2)使用方法
以目标检测自动变化生成对应的增强图片/bbox/xml为例说明
注意:针对crop或者平移或者空间变换等操作,引起bbox超出原图的,需要使用如下操作,对结果进行变换:
bbs_aug_clip = bbs_aug.remove_out_of_image().clip_out_of_image()
import imageio
import imgaug as ia
import imgaug.augmenters as iaa
from imgaug.augmentables.bbs import BoundingBox, BoundingBoxesOnImage
import os
from xml.dom.minidom import Document
import xml.etree.ElementTree as ET
from tqdm import tqdm
ia.seed(42)
def generate_xml(save_path, img_info):
doc = Document()
DOCUMENT = doc.createElement('annotation')
floder = doc.createElement('folder')
floder_text = doc.createTextNode(img_info["folder"])
floder.appendChild(floder_text)
DOCUMENT.appendChild(floder)
doc.appendChild(DOCUMENT)
filename = doc.createElement('filename')
filename_text = doc.createTextNode(img_info["filename"]) # filename:
filename.appendChild(filename_text)
DOCUMENT.appendChild(filename)
doc.appendChild(DOCUMENT)
path = doc.createElement('path')
path_text = doc.createTextNode(img_info["img_path"]) # path:
path.appendChild(path_text)
DOCUMENT.appendChild(path)
doc.appendChild(DOCUMENT)
source = doc.createElement('source')
database = doc.createElement('database')
database_text = doc.createTextNode('Unknow')
database.appendChild(database_text)
source.appendChild(database)
DOCUMENT.appendChild(source)
doc.appendChild(DOCUMENT)
size = doc.createElement('size')
width = doc.createElement('width')
width_text = doc.createTextNode(str(img_info["img_shape"][1]))
width.appendChild(width_text)
size.appendChild(width)
height = doc.createElement('height')
height_text = doc.createTextNode(str(img_info["img_shape"][0]))
height.appendChild(height_text)
size.appendChild(height)
depth = doc.createElement('depth')
depth_text = doc.createTextNode(str(img_info["img_shape"][2]))
depth.appendChild(depth_text)
size.appendChild(depth)
DOCUMENT.appendChild(size)
segmented = doc.createElement('segmented')
segmented_text = doc.createTextNode('0')
segmented.appendChild(segmented_text)
DOCUMENT.appendChild(segmented)
doc.appendChild(DOCUMENT)
objectes = img_info["object"]
if len(objectes) == 0:
return
for obj in objectes:

本文介绍了在目标检测中遇到样本不足时如何利用数据增强提高检测精度。重点讲解了两个常用的数据增强库:imgaug和albumentations。imgaug的安装和使用包括对bbox的处理,albumentations则提供了直接对图像和带有bbox样本的增强方法。文章还详细解释了如何处理bbox格式和参数设置,以确保变换后的bbox有效。
最低0.47元/天 解锁文章
848

被折叠的 条评论
为什么被折叠?



