数据增强-albumentations与imgaug使用方法

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

目标检测过程中常遇见目标种类或者数量不足的情况,在此情况下,我们常常使用数据增强的策略去扩充数据,以获取更高的检测精度。
如下介绍两个数据增强的常用库

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:
  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI小花猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值