利用Augmentor将原图和标签同时数据增强-语义分割

import Augmentor
import glob
import os
import random
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

train_path = '/home/csj/task/task1/ori_images'
groud_truth_path = '/home/csj/task/task1/ori_masks'
img_type = 'jpg'
train_tmp_path = '/home/csj/task/task1/new_train33'
mask_tmp_path = '/home/csj/task/task1/new_label33'

def start(train_path,groud_truth_path):
    train_img = glob.glob(train_path+'/*.'+img_type)
    masks = glob.glob(groud_truth_path+'/*.'+img_type)

    if len(train_img) != len(masks):
        print ("trains can't match masks")
        return 0
    for i in range(len(train_img)):
        train_img_tmp_path = train_tmp_path
        if not os.path.lexists(train_img_tmp_path):
            os.mkdir(train_img_tmp_path)
        img = load_img(train_img[i])
        x_t = img_to_array(img)
        img_tmp = array_to_img(x_t)
        img_tmp.save(train_img_tmp_path+'/'+str(i)+'.'+img_type)

        mask_img_tmp_path =mask_tmp_path
        if not os.path.lexists(mask_img_tmp_path):
            os.mkdir(mask_img_tmp_path)
        mask = load_img(masks[i])
        x_l = img_to_array(mask)
        mask_tmp = array_to_img(x_l)
        mask_tmp.save(mask_img_tmp_path+'/'+str(i)+'.'+img_type)
        print ("%s folder has been created!"%str(i))
    return i+1


def doAugment(num):
    sum = 0
    for i in range(num):
        p = Augmentor.Pipeline(train_tmp_path)
        p.ground_truth(mask_tmp_path)
        p.rotate(probability=0.5, max_left_rotation=5, max_right_rotation=5)#旋转
        p.flip_left_right(probability=0.5)#按概率左右翻转
        p.zoom_random(probability=0.6, percentage_area=0.99)#随即将一定比例面积的图形放大至全图
        p.flip_top_bottom(probability=0.6)#按概率随即上下翻转
        #p.random_distortion(probability=0.8,grid_width=10,grid_height=10, magnitude=20)#小块变形
        count = random.randint(40, 60)
        print("\nNo.%s data is being augmented and %s data will be created"%(i,count))
        sum = sum + count
        p.sample(count)
        print("Done")
    print("%s pairs of data has been created totally"%sum)


a = start(train_path, groud_truth_path)
doAugment(a)

  • 0
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
语义分割图像增强算法是为了丰富图像训练集,提取图像特征和泛化模型而设计的。常见的图像增强方式包括旋转图像、剪切图像、改变图像色差、扭曲图像特征、改变图像尺寸大小和增强图像噪音等。然而,对于语义分割任务,需要同时对原始图像和掩码图进行增强,因此现有的深度学习框架自带的图像增强工具并不能直接使用。但是可以使用Augmentor工具库来实现该功能。通过定义图像的旋转、翻转、缩放等操作,并指定执行的概率和参数,可以在训练集中生成更多的增强样本。例如,可以使用以下Python代码对图像进行数据增强: ``` import Augmentor p = Augmentor.Pipeline("test1") p.ground_truth("test2") p.rotate(probability=0.8, max_left_rotation=10, max_right_rotation=10) p.flip_left_right(probability=0.5) p.zoom_random(probability=0.3, percentage_area=0.85) p.sample(20) ``` 这段代码使用Augmentor库导入数据增强工具并设置图像存储路径和掩码文件存储路径。然后通过调用旋转、翻转和缩放等函数来增强图像,可以根据需要设置执行的概率和参数。最后,调用sample函数生成扩充后的数据样本。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [结合图像语义分割的增强现实型平视显示系统设计与研究](https://download.csdn.net/download/weixin_38595473/14937134)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [语义分割图像增强新方法](https://blog.csdn.net/niuxuerui11/article/details/114866772)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值