1
+ import hub
2
+ import random
3
+ from PIL import Image , ImageFilter , ImageOps , ImageEnhance
4
+
5
+
6
+ @hub .compute
7
+ def cvt_horizontal_flip (sample_input , sample_output , probability = 0.5 ):
8
+
9
+ img = Image .fromarray (sample_input .images .numpy ())
10
+ if random .uniform (0 , 1 ) < probability :
11
+ img = img .transpose (Image .FLIP_LEFT_RIGHT )
12
+ sample_output .images .append (img )
13
+ sample_output .labels .append (sample_input .labels .numpy ())
14
+ return sample_output
15
+
16
+
17
+ @hub .compute
18
+ def cvt_vertical_flip (sample_input , sample_output , probability = 0.5 ):
19
+
20
+ img = Image .fromarray (sample_input .images .numpy ())
21
+ if random .uniform (0 , 1 ) < probability :
22
+ img = img .transpose (Image .FLIP_TOP_BOTTOM )
23
+ sample_output .images .append (img )
24
+ sample_output .labels .append (sample_input .labels .numpy ())
25
+ return sample_output
26
+
27
+
28
+ @hub .compute
29
+ def cvt_blur (sample_input , sample_output , blur_value = 0 , probability = 0.5 ):
30
+
31
+ img = Image .fromarray (sample_input .images .numpy ())
32
+ if random .uniform (0 , 1 ) < probability :
33
+ img = img .filter (ImageFilter .BoxBlur (blur_value ))
34
+ sample_output .images .append (img )
35
+ sample_output .labels .append (sample_input .labels .numpy ())
36
+ return sample_output
37
+
38
+
39
+ @hub .compute
40
+ def cvt_gray (sample_input , sample_output , probability = 0.5 ):
41
+
42
+ img = Image .fromarray (sample_input .images .numpy ())
43
+ if random .uniform (0 , 1 ) < probability :
44
+ img = ImageOps .grayscale (img )
45
+ sample_output .images .append (img )
46
+ sample_output .labels .append (sample_input .labels .numpy ())
47
+ return sample_output
48
+
49
+
50
+ @hub .compute
51
+ def cvt_inverse (sample_input , sample_output , probability = 0.5 ):
52
+
53
+ img = Image .fromarray (sample_input .images .numpy ())
54
+ if random .uniform (0 , 1 ) < probability :
55
+ img = ImageOps .invert (img )
56
+ sample_output .images .append (img )
57
+ sample_output .labels .append (sample_input .labels .numpy ())
58
+ return sample_output
59
+
60
+
61
+ @hub .compute
62
+ def cvt_contrast (sample_input , sample_output , contrast_value = 1 , probability = 0.5 ):
63
+
64
+ img = Image .fromarray (sample_input .images .numpy ())
65
+ if random .uniform (0 , 1 ) < probability :
66
+ img = ImageEnhance .Contrast (img ).enhance (contrast_value )
67
+ sample_output .images .append (img )
68
+ sample_output .labels .append (sample_input .labels .numpy ())
69
+ return sample_output
70
+
71
+
72
+ @hub .compute
73
+ def cvt_crop (sample_input , sample_output , crop_locations = - 1 , probability = 0.5 ):
74
+
75
+ img = Image .fromarray (sample_input .images .numpy ())
76
+ if crop_locations == - 1 :
77
+ crop_locations = (
78
+ img .size [0 ] * 0.25 ,
79
+ img .size [1 ] * 0.25 ,
80
+ img .size [0 ] * 0.75 ,
81
+ img .size [1 ] * 0.75 ,
82
+ )
83
+ if random .uniform (0 , 1 ) < probability :
84
+ img = img .crop (crop_locations )
85
+ sample_output .images .append (img )
86
+ sample_output .labels .append (sample_input .labels .numpy ())
87
+ return sample_output
88
+
89
+
90
+ @hub .compute
91
+ def cvt_resize (sample_input , sample_output , resize_size = - 1 , probability = 0.5 ):
92
+
93
+ img = Image .fromarray (sample_input .images .numpy ())
94
+ if resize_size == - 1 :
95
+ resize_size = (img .size [0 ], img .size [1 ])
96
+ if random .uniform (0 , 1 ) < probability :
97
+ img = img .resize (resize_size )
98
+ sample_output .images .append (img )
99
+ sample_output .labels .append (sample_input .labels .numpy ())
100
+ return sample_output
101
+
102
+
103
+ @hub .compute
104
+ def cvt_rotate (sample_input , sample_output , rotate_angle = 0 , probability = 0.5 ):
105
+
106
+ img = Image .fromarray (sample_input .images .numpy ())
107
+ if random .uniform (0 , 1 ) < probability :
108
+ img = img .rotate (rotate_angle )
109
+ sample_output .images .append (img )
110
+ sample_output .labels .append (sample_input .labels .numpy ())
111
+ return sample_output
112
+
113
+
114
+ @hub .compute
115
+ def cvt_transpose (sample_input , sample_output , transpose_value = 0 , probability = 0.5 ):
116
+
117
+ values = {0 : 0 , 90 : Image .ROTATE_90 , 180 : Image .ROTATE_180 , 270 : Image .ROTATE_270 }
118
+ img = Image .fromarray (sample_input .images .numpy ())
119
+ if random .uniform (0 , 1 ) < probability and transpose_value in values :
120
+ img = img .transpose (values [transpose_value ])
121
+ sample_output .images .append (img )
122
+ sample_output .labels .append (sample_input .labels .numpy ())
123
+ return sample_output
124
+
125
+
126
+ @hub .compute
127
+ def cvt_padding (
128
+ sample_input ,
129
+ sample_output ,
130
+ pad_size = (0 , 0 , 0 , 0 ),
131
+ bg_color = (0 , 0 , 0 ),
132
+ probability = 0.5 ,
133
+ ):
134
+
135
+ img = Image .fromarray (sample_input .images .numpy ())
136
+ if random .uniform (0 , 1 ) < probability :
137
+ new_img = Image .new (
138
+ img .mode ,
139
+ (
140
+ pad_size [0 ] + img .size [0 ] + pad_size [2 ],
141
+ pad_size [1 ] + img .size [1 ] + pad_size [3 ],
142
+ ),
143
+ bg_color ,
144
+ )
145
+ new_img .paste (img , (pad_size [0 ], pad_size [1 ]))
146
+ sample_output .images .append (new_img )
147
+ sample_output .labels .append (sample_input .labels .numpy ())
148
+ return sample_output
149
+
150
+
151
+ if __name__ == "__main__" :
152
+
153
+ ds_input = hub .load ("/input/path" )
154
+ ds_output = hub .like ("/output/path" , ds_input )
155
+ pipeline = hub .compose (
156
+ [
157
+ cvt_horizontal_flip (probability = 0.4 ),
158
+ cvt_crop (crop_locations = - 1 , probability = 0.8 ),
159
+ cvt_gray (probability = 0.7 ),
160
+ cvt_padding (pad_size = (10 , 10 , 10 , 10 ), bg_color = (0 , 0 , 0 ), probability = 0.5 ),
161
+ cvt_resize (resize_size = (100 , 80 ), probability = 0.6 ),
162
+ ]
163
+ )
164
+ pipeline .eval (ds_input , ds_output , num_workers = 2 )
0 commit comments