1
- import hub
1
+ import hub # type: ignore
2
+ from hub .core .dataset import Dataset # type: ignore
3
+ from PIL import Image , ImageFilter , ImageOps , ImageEnhance # type: ignore
2
4
import random
3
- from PIL import Image , ImageFilter , ImageOps , ImageEnhance
5
+ from typing import Tuple
4
6
5
7
6
8
@hub .compute
7
- def cvt_horizontal_flip (sample_input , sample_output , probability = 0.5 ):
8
-
9
+ def cvt_horizontal_flip (
10
+ sample_input : Dataset , sample_output : Dataset , probability : float = 0.5
11
+ ) -> Dataset :
12
+ """Converts the sample_input dataset to horizontal flips in sample_output dataset.
13
+
14
+ Args:
15
+ sample_input: input dataset passed to generate output dataset.
16
+ sample_output: output dataset which will contain transforms of input dataset.
17
+ probability: probability to randomly apply transformation. Defaults to 0.5.
18
+
19
+ Returns:
20
+ sample_output dataset with transformed images.
21
+ """
9
22
img = Image .fromarray (sample_input .images .numpy ())
10
23
if random .uniform (0 , 1 ) < probability :
11
24
img = img .transpose (Image .FLIP_LEFT_RIGHT )
@@ -15,8 +28,19 @@ def cvt_horizontal_flip(sample_input, sample_output, probability=0.5):
15
28
16
29
17
30
@hub .compute
18
- def cvt_vertical_flip (sample_input , sample_output , probability = 0.5 ):
19
-
31
+ def cvt_vertical_flip (
32
+ sample_input : Dataset , sample_output : Dataset , probability : float = 0.5
33
+ ) -> Dataset :
34
+ """Converts the sample_input dataset to vertical flips in sample_output dataset.
35
+
36
+ Args:
37
+ sample_input: input dataset passed to generate output dataset.
38
+ sample_output: output dataset which will contain transforms of input dataset.
39
+ probability: probability to randomly apply transformation. Defaults to 0.5.
40
+
41
+ Returns:
42
+ sample_output dataset with transformed images.
43
+ """
20
44
img = Image .fromarray (sample_input .images .numpy ())
21
45
if random .uniform (0 , 1 ) < probability :
22
46
img = img .transpose (Image .FLIP_TOP_BOTTOM )
@@ -26,8 +50,23 @@ def cvt_vertical_flip(sample_input, sample_output, probability=0.5):
26
50
27
51
28
52
@hub .compute
29
- def cvt_blur (sample_input , sample_output , blur_value = 0 , probability = 0.5 ):
30
-
53
+ def cvt_blur (
54
+ sample_input : Dataset ,
55
+ sample_output : Dataset ,
56
+ blur_value : float = 0 ,
57
+ probability : float = 0.5 ,
58
+ ) -> Dataset :
59
+ """Converts the sample_input dataset to blurs in sample_output dataset.
60
+
61
+ Args:
62
+ sample_input: input dataset passed to generate output dataset.
63
+ sample_output: output dataset which will contain transforms of input dataset.
64
+ blur_value: value to determine the extent of blur. Defaults to 0.
65
+ probability: probability to randomly apply transformation. Defaults to 0.5.
66
+
67
+ Returns:
68
+ sample_output dataset with transformed images.
69
+ """
31
70
img = Image .fromarray (sample_input .images .numpy ())
32
71
if random .uniform (0 , 1 ) < probability :
33
72
img = img .filter (ImageFilter .BoxBlur (blur_value ))
@@ -37,8 +76,19 @@ def cvt_blur(sample_input, sample_output, blur_value=0, probability=0.5):
37
76
38
77
39
78
@hub .compute
40
- def cvt_gray (sample_input , sample_output , probability = 0.5 ):
41
-
79
+ def cvt_gray (
80
+ sample_input : Dataset , sample_output : Dataset , probability : float = 0.5
81
+ ) -> Dataset :
82
+ """Converts the sample_input dataset to grayscale in sample_output dataset.
83
+
84
+ Args:
85
+ sample_input: input dataset passed to generate output dataset.
86
+ sample_output: output dataset which will contain transforms of input dataset.
87
+ probability: probability to randomly apply transformation. Defaults to 0.5.
88
+
89
+ Returns:
90
+ sample_output dataset with transformed images.
91
+ """
42
92
img = Image .fromarray (sample_input .images .numpy ())
43
93
if random .uniform (0 , 1 ) < probability :
44
94
img = ImageOps .grayscale (img )
@@ -48,8 +98,19 @@ def cvt_gray(sample_input, sample_output, probability=0.5):
48
98
49
99
50
100
@hub .compute
51
- def cvt_inverse (sample_input , sample_output , probability = 0.5 ):
52
-
101
+ def cvt_inverse (
102
+ sample_input : Dataset , sample_output : Dataset , probability : float = 0.5
103
+ ) -> Dataset :
104
+ """Converts the sample_input dataset to inverts in sample_output dataset.
105
+
106
+ Args:
107
+ sample_input: input dataset passed to generate output dataset.
108
+ sample_output: output dataset which will contain transforms of input dataset.
109
+ probability: probability to randomly apply transformation. Defaults to 0.5.
110
+
111
+ Returns:
112
+ sample_output dataset with transformed images.
113
+ """
53
114
img = Image .fromarray (sample_input .images .numpy ())
54
115
if random .uniform (0 , 1 ) < probability :
55
116
img = ImageOps .invert (img )
@@ -59,8 +120,23 @@ def cvt_inverse(sample_input, sample_output, probability=0.5):
59
120
60
121
61
122
@hub .compute
62
- def cvt_contrast (sample_input , sample_output , contrast_value = 1 , probability = 0.5 ):
63
-
123
+ def cvt_contrast (
124
+ sample_input : Dataset ,
125
+ sample_output : Dataset ,
126
+ contrast_value : float = 1 ,
127
+ probability : float = 0.5 ,
128
+ ) -> Dataset :
129
+ """Converts the sample_input dataset to contrasts in sample_output dataset.
130
+
131
+ Args:
132
+ sample_input: input dataset passed to generate output dataset.
133
+ sample_output: output dataset which will contain transforms of input dataset.
134
+ contrast_value: value to determine the extent of contrast. Defaults to 1.
135
+ probability: probability to randomly apply transformation. Defaults to 0.5.
136
+
137
+ Returns:
138
+ sample_output dataset with transformed images.
139
+ """
64
140
img = Image .fromarray (sample_input .images .numpy ())
65
141
if random .uniform (0 , 1 ) < probability :
66
142
img = ImageEnhance .Contrast (img ).enhance (contrast_value )
@@ -70,10 +146,25 @@ def cvt_contrast(sample_input, sample_output, contrast_value=1, probability=0.5)
70
146
71
147
72
148
@hub .compute
73
- def cvt_crop (sample_input , sample_output , crop_locations = - 1 , probability = 0.5 ):
74
-
149
+ def cvt_crop (
150
+ sample_input : Dataset ,
151
+ sample_output : Dataset ,
152
+ crop_locations : Tuple [int , int , int , int ] = None ,
153
+ probability : float = 0.5 ,
154
+ ) -> Dataset :
155
+ """Converts the sample_input dataset to crops in sample_output dataset.
156
+
157
+ Args:
158
+ sample_input: input dataset passed to generate output dataset.
159
+ sample_output: output dataset which will contain transforms of input dataset.
160
+ crop_locations: tuple (start_x,start_y,end_x,end_y) to determine region for crop. Defaults to -1 which causes a centre crop.
161
+ probability: probability to randomly apply transformation. Defaults to 0.5.
162
+
163
+ Returns:
164
+ sample_output dataset with transformed images.
165
+ """
75
166
img = Image .fromarray (sample_input .images .numpy ())
76
- if crop_locations == - 1 :
167
+ if crop_locations is None :
77
168
crop_locations = (
78
169
img .size [0 ] * 0.25 ,
79
170
img .size [1 ] * 0.25 ,
@@ -88,10 +179,25 @@ def cvt_crop(sample_input, sample_output, crop_locations=-1, probability=0.5):
88
179
89
180
90
181
@hub .compute
91
- def cvt_resize (sample_input , sample_output , resize_size = - 1 , probability = 0.5 ):
92
-
182
+ def cvt_resize (
183
+ sample_input : Dataset ,
184
+ sample_output : Dataset ,
185
+ resize_size : Tuple [int , int ] = None ,
186
+ probability : float = 0.5 ,
187
+ ) -> Dataset :
188
+ """Converts the sample_input dataset to resizes in sample_output dataset.
189
+
190
+ Args:
191
+ sample_input: input dataset passed to generate output dataset.
192
+ sample_output: output dataset which will contain transforms of input dataset.
193
+ resize_size: tuple (width,height) to determine dimensions for resize. Defaults to -1 which prevents resizing.
194
+ probability: probability to randomly apply transformation. Defaults to 0.5.
195
+
196
+ Returns:
197
+ sample_output dataset with transformed images.
198
+ """
93
199
img = Image .fromarray (sample_input .images .numpy ())
94
- if resize_size == - 1 :
200
+ if resize_size is None :
95
201
resize_size = (img .size [0 ], img .size [1 ])
96
202
if random .uniform (0 , 1 ) < probability :
97
203
img = img .resize (resize_size )
@@ -101,8 +207,23 @@ def cvt_resize(sample_input, sample_output, resize_size=-1, probability=0.5):
101
207
102
208
103
209
@hub .compute
104
- def cvt_rotate (sample_input , sample_output , rotate_angle = 0 , probability = 0.5 ):
105
-
210
+ def cvt_rotate (
211
+ sample_input : Dataset ,
212
+ sample_output : Dataset ,
213
+ rotate_angle : float = 0 ,
214
+ probability : float = 0.5 ,
215
+ ) -> Dataset :
216
+ """Converts the sample_input dataset to rotations in sample_output dataset.
217
+
218
+ Args:
219
+ sample_input: input dataset passed to generate output dataset.
220
+ sample_output: output dataset which will contain transforms of input dataset.
221
+ rotate_angle: value to determine extent of angular rotation. Defaults to 0 which prevents rotation.
222
+ probability: probability to randomly apply transformation. Defaults to 0.5.
223
+
224
+ Returns:
225
+ sample_output dataset with transformed images.
226
+ """
106
227
img = Image .fromarray (sample_input .images .numpy ())
107
228
if random .uniform (0 , 1 ) < probability :
108
229
img = img .rotate (rotate_angle )
@@ -112,8 +233,30 @@ def cvt_rotate(sample_input, sample_output, rotate_angle=0, probability=0.5):
112
233
113
234
114
235
@hub .compute
115
- def cvt_transpose (sample_input , sample_output , transpose_value = 0 , probability = 0.5 ):
116
-
236
+ def cvt_transpose (
237
+ sample_input : Dataset ,
238
+ sample_output : Dataset ,
239
+ transpose_value : int = 0 ,
240
+ probability : float = 0.5 ,
241
+ ) -> Dataset :
242
+ """Converts the sample_input dataset to transpose in sample_output dataset.
243
+
244
+ Args:
245
+ sample_input: input dataset passed to generate output dataset.
246
+ sample_output: output dataset which will contain transforms of input dataset.
247
+ transpose_value: value to determine type of transpose.
248
+ {
249
+ 0: Transpose top,
250
+ 90: Transpose right,
251
+ 180: Transpose bottom,
252
+ 270: Transpose left
253
+ }
254
+ Defaults to 0 which prevents transpose.
255
+ probability: probability to randomly apply transformation. Defaults to 0.5.
256
+
257
+ Returns:
258
+ sample_output dataset with transformed images.
259
+ """
117
260
values = {0 : 0 , 90 : Image .ROTATE_90 , 180 : Image .ROTATE_180 , 270 : Image .ROTATE_270 }
118
261
img = Image .fromarray (sample_input .images .numpy ())
119
262
if random .uniform (0 , 1 ) < probability and transpose_value in values :
@@ -125,13 +268,24 @@ def cvt_transpose(sample_input, sample_output, transpose_value=0, probability=0.
125
268
126
269
@hub .compute
127
270
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
-
271
+ sample_input : Dataset ,
272
+ sample_output : Dataset ,
273
+ pad_size : Tuple [int , int , int , int ] = (0 , 0 , 0 , 0 ),
274
+ pad_color : Tuple [int , int , int ] = (0 , 0 , 0 ),
275
+ probability : float = 0.5 ,
276
+ ) -> Dataset :
277
+ """Converts the sample_input dataset to padded in sample_output dataset.
278
+
279
+ Args:
280
+ sample_input: input dataset passed to generate output dataset.
281
+ sample_output: output dataset which will contain transforms of input dataset.
282
+ pad_size: tuple (pad_left,pad_top,pad_right,pad_bottom) to determine dimensions of padding. Defaults to (0,0,0,0) which prevents padding.
283
+ pad_color: tuple (r,g,b) in rgb format to set the color of padding. Defaults to black (0,0,0).
284
+ probability: probability to randomly apply transformation. Defaults to 0.5.
285
+
286
+ Returns:
287
+ sample_output dataset with transformed images.
288
+ """
135
289
img = Image .fromarray (sample_input .images .numpy ())
136
290
if random .uniform (0 , 1 ) < probability :
137
291
new_img = Image .new (
@@ -140,7 +294,7 @@ def cvt_padding(
140
294
pad_size [0 ] + img .size [0 ] + pad_size [2 ],
141
295
pad_size [1 ] + img .size [1 ] + pad_size [3 ],
142
296
),
143
- bg_color ,
297
+ pad_color ,
144
298
)
145
299
new_img .paste (img , (pad_size [0 ], pad_size [1 ]))
146
300
sample_output .images .append (new_img )
@@ -161,4 +315,4 @@ def cvt_padding(
161
315
cvt_resize (resize_size = (100 , 80 ), probability = 0.6 ),
162
316
]
163
317
)
164
- pipeline .eval (ds_input , ds_output , num_workers = 2 )
318
+ pipeline .eval (ds_input , ds_output , num_workers = 2 )
0 commit comments