Skip to content

Commit c9ac24a

Browse files
committed
Updated docstrings and formatting
1 parent 2bbb98e commit c9ac24a

File tree

1 file changed

+187
-33
lines changed

1 file changed

+187
-33
lines changed

transforming/augment.py

+187-33
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
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
24
import random
3-
from PIL import Image, ImageFilter, ImageOps, ImageEnhance
5+
from typing import Tuple
46

57

68
@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+
"""
922
img = Image.fromarray(sample_input.images.numpy())
1023
if random.uniform(0, 1) < probability:
1124
img = img.transpose(Image.FLIP_LEFT_RIGHT)
@@ -15,8 +28,19 @@ def cvt_horizontal_flip(sample_input, sample_output, probability=0.5):
1528

1629

1730
@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+
"""
2044
img = Image.fromarray(sample_input.images.numpy())
2145
if random.uniform(0, 1) < probability:
2246
img = img.transpose(Image.FLIP_TOP_BOTTOM)
@@ -26,8 +50,23 @@ def cvt_vertical_flip(sample_input, sample_output, probability=0.5):
2650

2751

2852
@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+
"""
3170
img = Image.fromarray(sample_input.images.numpy())
3271
if random.uniform(0, 1) < probability:
3372
img = img.filter(ImageFilter.BoxBlur(blur_value))
@@ -37,8 +76,19 @@ def cvt_blur(sample_input, sample_output, blur_value=0, probability=0.5):
3776

3877

3978
@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+
"""
4292
img = Image.fromarray(sample_input.images.numpy())
4393
if random.uniform(0, 1) < probability:
4494
img = ImageOps.grayscale(img)
@@ -48,8 +98,19 @@ def cvt_gray(sample_input, sample_output, probability=0.5):
4898

4999

50100
@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+
"""
53114
img = Image.fromarray(sample_input.images.numpy())
54115
if random.uniform(0, 1) < probability:
55116
img = ImageOps.invert(img)
@@ -59,8 +120,23 @@ def cvt_inverse(sample_input, sample_output, probability=0.5):
59120

60121

61122
@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+
"""
64140
img = Image.fromarray(sample_input.images.numpy())
65141
if random.uniform(0, 1) < probability:
66142
img = ImageEnhance.Contrast(img).enhance(contrast_value)
@@ -70,10 +146,25 @@ def cvt_contrast(sample_input, sample_output, contrast_value=1, probability=0.5)
70146

71147

72148
@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+
"""
75166
img = Image.fromarray(sample_input.images.numpy())
76-
if crop_locations == -1:
167+
if crop_locations is None:
77168
crop_locations = (
78169
img.size[0] * 0.25,
79170
img.size[1] * 0.25,
@@ -88,10 +179,25 @@ def cvt_crop(sample_input, sample_output, crop_locations=-1, probability=0.5):
88179

89180

90181
@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+
"""
93199
img = Image.fromarray(sample_input.images.numpy())
94-
if resize_size == -1:
200+
if resize_size is None:
95201
resize_size = (img.size[0], img.size[1])
96202
if random.uniform(0, 1) < probability:
97203
img = img.resize(resize_size)
@@ -101,8 +207,23 @@ def cvt_resize(sample_input, sample_output, resize_size=-1, probability=0.5):
101207

102208

103209
@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+
"""
106227
img = Image.fromarray(sample_input.images.numpy())
107228
if random.uniform(0, 1) < probability:
108229
img = img.rotate(rotate_angle)
@@ -112,8 +233,30 @@ def cvt_rotate(sample_input, sample_output, rotate_angle=0, probability=0.5):
112233

113234

114235
@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+
"""
117260
values = {0: 0, 90: Image.ROTATE_90, 180: Image.ROTATE_180, 270: Image.ROTATE_270}
118261
img = Image.fromarray(sample_input.images.numpy())
119262
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.
125268

126269
@hub.compute
127270
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+
"""
135289
img = Image.fromarray(sample_input.images.numpy())
136290
if random.uniform(0, 1) < probability:
137291
new_img = Image.new(
@@ -140,7 +294,7 @@ def cvt_padding(
140294
pad_size[0] + img.size[0] + pad_size[2],
141295
pad_size[1] + img.size[1] + pad_size[3],
142296
),
143-
bg_color,
297+
pad_color,
144298
)
145299
new_img.paste(img, (pad_size[0], pad_size[1]))
146300
sample_output.images.append(new_img)
@@ -161,4 +315,4 @@ def cvt_padding(
161315
cvt_resize(resize_size=(100, 80), probability=0.6),
162316
]
163317
)
164-
pipeline.eval(ds_input, ds_output, num_workers=2)
318+
pipeline.eval(ds_input, ds_output, num_workers=2)

0 commit comments

Comments
 (0)