日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

【Tool】Augmentor和imgaug——python图像数据增强库

發(fā)布時(shí)間:2024/6/30 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Tool】Augmentor和imgaug——python图像数据增强库 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Augmentor和imgaug——python圖像數(shù)據(jù)增強(qiáng)庫(kù)

Tags: ComputerVision Python


介紹兩個(gè)圖像增強(qiáng)庫(kù):Augmentor和imgaug,Augmentor使用比較簡(jiǎn)單,只有一些簡(jiǎn)單的操作。 imgaug實(shí)現(xiàn)的功能更多,可以對(duì)keypoint, bounding box同步處理,比如你現(xiàn)在由一些標(biāo)記好的數(shù)據(jù),只有同時(shí)對(duì)原始圖片和標(biāo)記信息同步處理,才能有更多的標(biāo)記數(shù)據(jù)進(jìn)行訓(xùn)練。我在segmentation和detection任務(wù)經(jīng)常使用imgaug這個(gè)庫(kù)。

Augmentor

http://augmentor.readthedocs.io/en/master/index.html
Augmentor 是管道化的圖像增強(qiáng)庫(kù),每一個(gè)增強(qiáng)操作都是逐步疊加在圖像上。此外對(duì)于輸入的圖像,可以選擇按照一定的概率進(jìn)行增強(qiáng),比如只隨機(jī)對(duì)一半圖像進(jìn)行旋轉(zhuǎn)。

rotate(probability=0.5, max_left_rotation=5, max_right_rotation=10)

可以實(shí)現(xiàn)的操作有, rotate, crop, perspective skew(視角傾斜), elastic distortions(彈性變換), sheering(坐標(biāo)軸傾斜), mirroring(鏡像)

可以使用Augumentor.Pipeline()創(chuàng)建一個(gè)實(shí)例,調(diào)用各種方法向pipeline添加方法, status()可以顯示當(dāng)前pipeline的狀態(tài),在status中每個(gè)操作都有一個(gè)對(duì)應(yīng)的index, remove_operation(index)移除一個(gè)操作, 最后調(diào)用sample(nums)得到nums個(gè)augument后的圖像。

import Augmentor p = Augmentor.Pipeline("/path/to/images/") p.status() p.remove_operation(0)

rotate

  • rotate() 旋轉(zhuǎn),非90度旋轉(zhuǎn)會(huì)帶來(lái)padding或者裁剪
  • rotate90()
  • rotate180()
  • rotate270()
  • rotate_random_90() 隨機(jī)旋轉(zhuǎn),90, 180, 270

resize

crop

  • crop_centre()
  • crop_by_size()
  • crop_random()

sheer

+ sheer()

mirroring

  • flip_left_right()
  • flip_top_bottom()
  • flip_random()

elastic distortion

  • random_distortion()
    Before

    After

    彈性變換是在計(jì)算機(jī)視覺(jué)任務(wù)中經(jīng)常使用的一種變換,比較有名的Segmentation Model U-Net就使用了elastic deformation來(lái)對(duì)自己的數(shù)據(jù)做Augmentation.最后取得了較好的效果.

imgaug

http://imgaug.readthedocs.io/en/latest/index.html

安裝

  依賴(lài)

  • numpy
  • scipy
  • scikit-image (pip install -U + scikit-image)
  • six (pip install -U six)
  • OpenCV (i.e. cv2 must be available in python). The library is mainly tested in OpenCV 2, but seems to also work in OpenCV 3.
pip install git+https://github.com/aleju/imgaug

或者

pip install imgaug

前者安裝github最新版本,后者安裝pypi版本。 

basic

Keypoint

Bounding Boxes

這個(gè)部分做object detection的人應(yīng)該經(jīng)常用到。

imgaug支持:

  • 將bounding boxes作為對(duì)象表示
  • 增強(qiáng)boundiing boxes
  • 在圖像上畫(huà)bounding boxes
  • boxing boxes移動(dòng), 映射, 計(jì)算IoU

Before

After

由于VOC_PASCAL是在分割和檢測(cè)領(lǐng)域常見(jiàn)的數(shù)據(jù)集,這里給出一個(gè)使用VOC_PASCAL標(biāo)記格式進(jìn)行數(shù)據(jù)增強(qiáng)的例子。
標(biāo)記格式:

<?xml version="1.0" ?> <annotation> <folder>Pictures</folder> <filename>bndbox.jpg</filename> <path>/home/redtea/Pictures/bndbox.jpg</path> <source><database>Unknown</database> </source> <size><width>1200</width><height>1200</height><depth>3</depth> </size><segmented>0</segmented><object><name>cat</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>49</xmin><ymin>647</ymin><xmax>599</xmax><ymax>1125</ymax></bndbox> </object><object><name>dog</name><pose>Unspecified</pose><truncated>0</truncated><difficult>0</difficult><bndbox><xmin>678</xmin><ymin>547</ymin><xmax>1159</xmax><ymax>1159</ymax></bndbox> </object> </annotation> import xml.etree.ElementTree as ET import pickle import os from os import getcwd import numpy as np from PIL import Image import cv2import imgaug as ia from imgaug import augmenters as iaaia.seed(1)def read_xml_annotation(root,image_id):in_file = open(os.path.join(root,image_id))tree = ET.parse(in_file)root = tree.getroot()bndbox = root.find('object').find('bndbox')xmin = int(bndbox.find('xmin').text)xmax = int(bndbox.find('xmax').text)ymin = int(bndbox.find('ymin').text)ymax = int(bndbox.find('ymax').text)return (xmin, ymin, xmax, ymax)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_ymax = new_target[3]in_file = open(os.path.join(root, str(image_id)+'.xml')) #這里root分別由兩個(gè)意思tree = ET.parse(in_file)xmlroot = tree.getroot() object = xmlroot.find('object')bndbox = object.find('bndbox')xmin = bndbox.find('xmin')xmin.text = str(new_xmin)ymin = bndbox.find('ymin')ymin.text = str(new_ymin)xmax = bndbox.find('xmax')xmax.text = str(new_xmax)ymax = bndbox.find('ymax')ymax.text = str(new_ymax)tree.write(os.path.join(root,str(image_id)+"_aug"+'.xml'))if __name__ == "__main__":cmd = os.getcwd()image_id = "bndbox"img = Image.open(os.path.join(cmd, str(image_id)+'.jpg'))img = np.array(img)bndbox = read_xml_annotation(cmd, str(image_id)+'.xml')bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=bndbox[0], y1=bndbox[1], x2=bndbox[2], y2=bndbox[3])], shape=img.shape)seq = iaa.Sequential([iaa.Flipud(0.5), # vertically flip 20% of all imagesiaa.Multiply((1.2, 1.5)), # change brightness, doesn't affect BBsiaa.Affine(translate_px={"x": 10, "y": 10},scale=(0.8, 0.95),rotate=(-10,10)) # translate by 40/60px on x/y axis, and scale to 50-70%, affects BBs])seq_det = seq.to_deterministic() # 保持坐標(biāo)和圖像同步改變,而不是隨機(jī)image_aug = seq_det.augment_images([img])[0]bbs_aug = seq_det.augment_bounding_boxes([bbs])[0]before = bbs.bounding_boxes[0]after = bbs_aug.bounding_boxes[0]print("BB : (%.4f, %.4f, %.4f, %.4f) -> (%.4f, %.4f, %.4f, %.4f)" % (before.x1, before.y1, before.x2, before.y2,after.x1, after.y1, after.x2, after.y2))image_before = bbs.draw_on_image(img, thickness=2)image_after = bbs_aug.draw_on_image(image_aug, thickness=2)Image.fromarray(image_before).save("before.jpg")Image.fromarray(image_after).save('after.jpg')new_bndbox = []new_bndbox.append(int(bbs_aug.bounding_boxes[0].x1))new_bndbox.append(int(bbs_aug.bounding_boxes[0].y1))new_bndbox.append(int(bbs_aug.bounding_boxes[0].x2))new_bndbox.append(int(bbs_aug.bounding_boxes[0].y2))# 修改xml tree 并保存change_xml_annotation(cmd, image_id, new_bndbox)

這個(gè)包好像不能畫(huà)出傾斜的bounding box, 我的read xml程序只能讀取第一個(gè)bounding box,懶得修改了。

總之我覺(jué)得如果你Augmentor不能滿(mǎn)足你就可以使用imgaug,但是有一點(diǎn)需要注意!imgaug中一些變換會(huì)給邊緣區(qū)域帶來(lái)黑色填充塊,如果這些黑色填充塊對(duì)你的模型有影響的話(huà),就需要特殊處理!!

轉(zhuǎn)載于:https://www.cnblogs.com/vincentcheng/p/9186540.html

總結(jié)

以上是生活随笔為你收集整理的【Tool】Augmentor和imgaug——python图像数据增强库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。