python使用Augmentor对图像分割数据集中原图和标签mask同时进行变换

p y t h o n 使 用 A u g m e n t o r 对 图 像 分 割 数 据 集 中 原 图 和 标 签 m a s k 同 时 进 行 变 换 python使用Augmentor对图像分割数据集中原图和标签mask同时进行变换 python使Augmentormask

import Augmentor

'''
步骤 1.创建管道Pipeline实例
'''
# 设置原图路径
p = Augmentor.Pipeline("defects")
# 设置标签mask路径
p.ground_truth("rgb_masks")

'''
步骤 2:向管道添加数据增强(执行概率+参数范围)操作
'''
# 设置处理概率
process_probability = 1

# 1、旋转
# 1.1 不带裁剪的旋转
p.rotate_without_crop(probability=process_probability, max_left_rotation=60, max_right_rotation=60)
# 1.2 带裁剪的旋转
p.rotate(probability=process_probability, max_left_rotation=25, max_right_rotation=25)
# 1.3 旋转90°
p.rotate_random_90(probability=process_probability)

# 2、缩放
p.zoom(probability=process_probability, min_factor=0.5, max_factor=2)

# 3、裁剪
# 3.1 按大小裁剪
# p.crop_by_size(probability=1, width=1000, height=1000)
# 3.2 从区域中心裁剪
p.crop_centre(probability=1,percentage_area=0.8)

# 3.3 随机裁剪
p.crop_random(probability=1, percentage_area=0.8, randomise_percentage_area=True)

# 4、翻转
# 4.1 水平翻转
p.flip_left_right(probability=process_probability)

# 4.2 上下翻转
p.flip_top_bottom(probability=process_probability)

# 4.3 随机翻转
p.flip_random(probability=process_probability)

# 5、亮度增强/减弱
p.random_brightness(probability=process_probability, min_factor=0.7, max_factor=1.2)

# 6、颜色增强/减弱
p.random_color(probability=process_probability, min_factor=0.0, max_factor=1.5)

# 7、对比度增强/减弱
p.random_contrast(probability=process_probability, min_factor=0.7, max_factor=1.2)

# 8、错切形变
p.shear(probability=process_probability,max_shear_left=15,max_shear_right=15)

# 9、透视形变
# 9.1 垂直方向透视形变
p.skew_tilt(probability=process_probability, magnitude=1)

# 9.2 斜四角方向透视形变
p.skew_corner(probability=process_probability, magnitude=0.5)

# 10、弹性扭曲
p.random_distortion(probability=process_probability, grid_height=5, grid_width=16, magnitude=8)

# 11、随机区域擦除
p.random_erasing(probability=process_probability, rectangle_area=0.5)  


'''
步骤 3:生成数据增强后的图像和标签mask
'''
# 设置生成个数
p.sample(100)
# 对每个图像做一次数据增强操作
p.process()

python使用Augmentor对图像分割数据集中原图和标签mask同时进行变换_第1张图片

一 使用步骤

步骤 1.创建管道Pipeline实例

import Augmentor
# 设置原图路径
p = Augmentor.Pipeline("./defects")
# 设置标签mask路径
p.ground_truth("./rgb_masks")

步骤 2:向管道添加数据增强(执行概率+参数范围)操作

'''
步骤 2:向管道添加数据增强(执行概率+参数范围)操作
'''
# 设置处理概率
process_probability = 1

# 1、旋转
# 1.1 不带裁剪的旋转
p.rotate_without_crop(probability=process_probability, max_left_rotation=60, max_right_rotation=60)
# 1.2 带裁剪的旋转
p.rotate(probability=process_probability, max_left_rotation=25, max_right_rotation=25)
# 1.3 旋转90°
p.rotate_random_90(probability=process_probability)

# 2、缩放
p.zoom(probability=process_probability, min_factor=0.5, max_factor=2)

# 3、裁剪
# 3.1 按大小裁剪
# p.crop_by_size(probability=1, width=1000, height=1000)
# 3.2 从区域中心裁剪
p.crop_centre(probability=1,percentage_area=0.8)

# 3.3 随机裁剪
p.crop_random(probability=1, percentage_area=0.8, randomise_percentage_area=True)

# 4、翻转
# 4.1 水平翻转
p.flip_left_right(probability=process_probability)

# 4.2 上下翻转
p.flip_top_bottom(probability=process_probability)

# 4.3 随机翻转
p.flip_random(probability=process_probability)

# 5、亮度增强/减弱
p.random_brightness(probability=process_probability, min_factor=0.7, max_factor=1.2)

# 6、颜色增强/减弱
p.random_color(probability=process_probability, min_factor=0.0, max_factor=1.5)

# 7、对比度增强/减弱
p.random_contrast(probability=process_probability, min_factor=0.7, max_factor=1.2)

# 8、错切形变
p.shear(probability=process_probability,max_shear_left=15,max_shear_right=15)

# 9、透视形变
# 9.1 垂直方向透视形变
p.skew_tilt(probability=process_probability, magnitude=1)

# 9.2 斜四角方向透视形变
p.skew_corner(probability=process_probability, magnitude=0.5)

# 10、弹性扭曲
p.random_distortion(probability=process_probability, grid_height=5, grid_width=16, magnitude=8)

# 11、随机区域擦除
p.random_erasing(probability=process_probability, rectangle_area=0.5)

步骤 3:生成数据增强后的图像和标签mask

# 设置生成个数
p.sample(5)
# 对每个图像做一次数据增强操作
p.process()  

新生成的增强图像和标签mask会保存到图像数据集的目录下名为 output 的目录中

python使用Augmentor对图像分割数据集中原图和标签mask同时进行变换_第2张图片


二 Augmentor数据增强详解

1、旋转

2、缩放

3、裁剪

4、翻转

5、亮度增强/减弱

6、颜色增强/减弱

7、对比度增强/减弱

8、错切形变

9、透视形变

10、弹性扭曲

11、随机区域擦除

示例

import Augmentor

'''
步骤 1.创建管道Pipeline实例
'''
# 设置原图路径
p = Augmentor.Pipeline("defects")
# 设置标签mask路径
p.ground_truth("rgb_masks")
# 设置生成增强数据样本数量
sample_num = 10
'''
设置标志位
'''
# 1、旋转
is_rotate = True

# 2、缩放
is_zoom = True

# 3、裁剪
is_crop = False

# 4、翻转
is_flip = False

# 5、亮度增强/减弱
is_brightness = True

# 6、颜色增强/减弱
is_color = True

# 7、对比度增强/减弱
is_contrast = False

# 8、错切形变
is_sheer = False

# 9、透视形变
is_skew = False

# 10、弹性扭曲
is_distortion = False

# 11、随机区域擦除
is_erasing = False

'''
设置参数范围
'''

# 1、旋转
rotate_max_left_rotation = 60
rotate_max_right_rotation = 60
# 2、缩放
zoom_min_factor = 0.5
zoom_max_factor = 2
# 3、裁剪
crop_percentage_area=0.8
# 4、翻转

# 5、亮度增强/减弱
brightness_min_factor=0.7  
brightness_max_factor=1.2
# 6、颜色增强/减弱
color_min_factor=0.0    
color_max_factor=1.5
# 7、对比度增强/减弱
contrast_min_factor=0.7  
contrast_max_factor=1.2
# 8、错切形变
sheer_max_shear_left=15  
sheer_max_shear_right=15
# 9、透视形变
skew_tilt_magnitude = 1  
skew_corner_magnitude = 0.5
# 10、弹性扭曲
distortion_grid_height=5 
distortion_grid_width=16  
distortion_magnitude=8
# 11、随机区域擦除
erasing_rectangle_area = 0.5


'''
步骤 2:向管道添加数据增强(执行概率+参数范围)操作
'''
# 设置处理概率
process_probability = 1
# 1、旋转
if is_rotate:
    # 1.1 不带裁剪的旋转
    p.rotate_without_crop(probability=process_probability, max_left_rotation=rotate_max_left_rotation, max_right_rotation=rotate_max_right_rotation)
    # # 1.2 带裁剪的旋转
    # p.rotate(probability=process_probability, max_left_rotation=25, max_right_rotation=25)
    # # 1.3 旋转90°
    # p.rotate_random_90(probability=process_probability)

# 2、缩放   zoom_min_factor=0.5   zoom_max_factor=2
if is_zoom:
    p.zoom(probability=process_probability, min_factor=zoom_min_factor, max_factor=zoom_max_factor)

# 3、裁剪   crop_percentage_area=0.8
if is_crop:
    # 3.1 按大小裁剪
    # p.crop_by_size(probability=1, width=1000, height=1000)
    # 3.2 从区域中心裁剪
    # p.crop_centre(probability=1,percentage_area=0.8)
    # 3.3 随机裁剪
    p.crop_random(probability=process_probability, percentage_area=crop_percentage_area, randomise_percentage_area=True)

# 4、翻转
if is_flip:
    # # 4.1 水平翻转
    # p.flip_left_right(probability=process_probability)
    #
    # # 4.2 上下翻转
    # p.flip_top_bottom(probability=process_probability)
    # 4.3 随机翻转
    p.flip_random(probability=process_probability)

# 5、亮度增强/减弱   brightness_min_factor=0.7  brightness_max_factor=1.2
if is_brightness:
    p.random_brightness(probability=process_probability, min_factor=brightness_min_factor, max_factor=brightness_max_factor)

# 6、颜色增强/减弱  color_min_factor=0.0    color_max_factor=1.5
if is_color:
    p.random_color(probability=process_probability, min_factor=color_min_factor, max_factor=color_max_factor)

# 7、对比度增强/减弱  contrast_min_factor=0.7  contrast_max_factor=1.2
if is_contrast:
    p.random_contrast(probability=process_probability, min_factor=contrast_min_factor, max_factor=contrast_max_factor)

# 8、错切形变  sheer_max_shear_left=15  sheer_max_shear_right=15
if is_sheer:
    p.shear(probability=process_probability,max_shear_left=sheer_max_shear_left,max_shear_right=sheer_max_shear_right)

# 9、透视形变  skew_tilt_magnitude = 1  skew_corner_magnitude = 0.5
if is_skew:
    # 9.1 垂直方向透视形变
    p.skew_tilt(probability=process_probability, magnitude=skew_tilt_magnitude)
    # 9.2 斜四角方向透视形变
    p.skew_corner(probability=process_probability, magnitude=skew_corner_magnitude)

# 10、弹性扭曲   distortion_grid_height=5 distortion_grid_width=16  distortion_magnitude=8
if is_distortion:
    p.random_distortion(probability=process_probability, grid_height=distortion_grid_height, grid_width=distortion_grid_width, magnitude=distortion_magnitude)

# 11、随机区域擦除  erasing_rectangle_area = 0.5
if is_erasing:
    p.random_erasing(probability=process_probability, rectangle_area=erasing_rectangle_area)


'''
步骤 3:生成数据增强后的图像和标签mask
'''
# 设置生成个数
p.sample(sample_num)
# 对每个图像做一次数据增强操作
p.process()

你可能感兴趣的:(深度学习,计算机视觉)