p
y
t
h
o
n
使
用
A
u
g
m
e
n
t
o
r
对
图
像
分
割
数
据
集
中
原
图
和
标
签
m
a
s
k
同
时
进
行
变
换
python使用Augmentor对图像分割数据集中原图和标签mask同时进行变换
p y t h o n 使 用 A u g m e n t o r 对 图 像 分 割 数 据 集 中 原 图 和 标 签 m a s k 同 时 进 行 变 换
import Augmentor
'''
步骤 1.创建管道Pipeline实例
'''
p = Augmentor. Pipeline( "defects" )
p. ground_truth( "rgb_masks" )
'''
步骤 2:向管道添加数据增强(执行概率+参数范围)操作
'''
process_probability = 1
p. rotate_without_crop( probability= process_probability, max_left_rotation= 60 , max_right_rotation= 60 )
p. rotate( probability= process_probability, max_left_rotation= 25 , max_right_rotation= 25 )
p. rotate_random_90( probability= process_probability)
p. zoom( probability= process_probability, min_factor= 0.5 , max_factor= 2 )
p. crop_centre( probability= 1 , percentage_area= 0.8 )
p. crop_random( probability= 1 , percentage_area= 0.8 , randomise_percentage_area= True )
p. flip_left_right( probability= process_probability)
p. flip_top_bottom( probability= process_probability)
p. flip_random( probability= process_probability)
p. random_brightness( probability= process_probability, min_factor= 0.7 , max_factor= 1.2 )
p. random_color( probability= process_probability, min_factor= 0.0 , max_factor= 1.5 )
p. random_contrast( probability= process_probability, min_factor= 0.7 , max_factor= 1.2 )
p. shear( probability= process_probability, max_shear_left= 15 , max_shear_right= 15 )
p. skew_tilt( probability= process_probability, magnitude= 1 )
p. skew_corner( probability= process_probability, magnitude= 0.5 )
p. random_distortion( probability= process_probability, grid_height= 5 , grid_width= 16 , magnitude= 8 )
p. random_erasing( probability= process_probability, rectangle_area= 0.5 )
'''
步骤 3:生成数据增强后的图像和标签mask
'''
p. sample( 100 )
p. process( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
一 使用步骤
步骤 1.创建管道Pipeline实例
import Augmentor
p = Augmentor. Pipeline( "./defects" )
p. ground_truth( "./rgb_masks" )
步骤 2:向管道添加数据增强(执行概率+参数范围)操作
'''
步骤 2:向管道添加数据增强(执行概率+参数范围)操作
'''
process_probability = 1
p. rotate_without_crop( probability= process_probability, max_left_rotation= 60 , max_right_rotation= 60 )
p. rotate( probability= process_probability, max_left_rotation= 25 , max_right_rotation= 25 )
p. rotate_random_90( probability= process_probability)
p. zoom( probability= process_probability, min_factor= 0.5 , max_factor= 2 )
p. crop_centre( probability= 1 , percentage_area= 0.8 )
p. crop_random( probability= 1 , percentage_area= 0.8 , randomise_percentage_area= True )
p. flip_left_right( probability= process_probability)
p. flip_top_bottom( probability= process_probability)
p. flip_random( probability= process_probability)
p. random_brightness( probability= process_probability, min_factor= 0.7 , max_factor= 1.2 )
p. random_color( probability= process_probability, min_factor= 0.0 , max_factor= 1.5 )
p. random_contrast( probability= process_probability, min_factor= 0.7 , max_factor= 1.2 )
p. shear( probability= process_probability, max_shear_left= 15 , max_shear_right= 15 )
p. skew_tilt( probability= process_probability, magnitude= 1 )
p. skew_corner( probability= process_probability, magnitude= 0.5 )
p. random_distortion( probability= process_probability, grid_height= 5 , grid_width= 16 , magnitude= 8 )
p. random_erasing( probability= process_probability, rectangle_area= 0.5 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
步骤 3:生成数据增强后的图像和标签mask
p. sample( 5 )
p. process( )
新生成的增强图像和标签mask会保存到图像数据集 的目录下名为 output 的目录中
二 Augmentor数据增强详解
1、旋转
2、缩放
3、裁剪
4、翻转
5、亮度增强/减弱
6、颜色增强/减弱
7、对比度增强/减弱
8、错切形变
9、透视形变
10、弹性扭曲
11、随机区域擦除
示例
import Augmentor
'''
步骤 1.创建管道Pipeline实例
'''
p = Augmentor. Pipeline( "defects" )
p. ground_truth( "rgb_masks" )
sample_num = 10
'''
设置标志位
'''
is_rotate = True
is_zoom = True
is_crop = False
is_flip = False
is_brightness = True
is_color = True
is_contrast = False
is_sheer = False
is_skew = False
is_distortion = False
is_erasing = False
'''
设置参数范围
'''
rotate_max_left_rotation = 60
rotate_max_right_rotation = 60
zoom_min_factor = 0.5
zoom_max_factor = 2
crop_percentage_area= 0.8
brightness_min_factor= 0.7
brightness_max_factor= 1.2
color_min_factor= 0.0
color_max_factor= 1.5
contrast_min_factor= 0.7
contrast_max_factor= 1.2
sheer_max_shear_left= 15
sheer_max_shear_right= 15
skew_tilt_magnitude = 1
skew_corner_magnitude = 0.5
distortion_grid_height= 5
distortion_grid_width= 16
distortion_magnitude= 8
erasing_rectangle_area = 0.5
'''
步骤 2:向管道添加数据增强(执行概率+参数范围)操作
'''
process_probability = 1
if is_rotate:
p. rotate_without_crop( probability= process_probability, max_left_rotation= rotate_max_left_rotation, max_right_rotation= rotate_max_right_rotation)
if is_zoom:
p. zoom( probability= process_probability, min_factor= zoom_min_factor, max_factor= zoom_max_factor)
if is_crop:
p. crop_random( probability= process_probability, percentage_area= crop_percentage_area, randomise_percentage_area= True )
if is_flip:
p. flip_random( probability= process_probability)
if is_brightness:
p. random_brightness( probability= process_probability, min_factor= brightness_min_factor, max_factor= brightness_max_factor)
if is_color:
p. random_color( probability= process_probability, min_factor= color_min_factor, max_factor= color_max_factor)
if is_contrast:
p. random_contrast( probability= process_probability, min_factor= contrast_min_factor, max_factor= contrast_max_factor)
if is_sheer:
p. shear( probability= process_probability, max_shear_left= sheer_max_shear_left, max_shear_right= sheer_max_shear_right)
if is_skew:
p. skew_tilt( probability= process_probability, magnitude= skew_tilt_magnitude)
p. skew_corner( probability= process_probability, magnitude= skew_corner_magnitude)
if is_distortion:
p. random_distortion( probability= process_probability, grid_height= distortion_grid_height, grid_width= distortion_grid_width, magnitude= distortion_magnitude)
if is_erasing:
p. random_erasing( probability= process_probability, rectangle_area= erasing_rectangle_area)
'''
步骤 3:生成数据增强后的图像和标签mask
'''
p. sample( sample_num)
p. process( )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160