-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add gaussian noise transform #6192 #6233
Changes from 31 commits
9be5e13
42952f6
db9756a
05a52af
c380281
431a7e0
5fc7c85
179908b
396abba
5d8b0f7
98e4e98
aa9b2e7
223074f
a26ed67
6d57443
1d1fbcd
ff80571
533e76f
b8d98d7
35ac3c9
42d49ae
74f92b1
54234f9
28fbd4b
6a95453
24804f6
b1bb81f
e08c9c1
7fc04aa
8a500fe
8b560fb
ac15585
3a85c34
5892695
e6b4e45
58d525d
021ecba
5956088
92d024f
bfde863
dbc3e1a
d18195b
a8d8137
2f7f558
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -748,8 +748,6 @@ def gaussian_blur(img: Tensor, kernel_size: List[int], sigma: List[float]) -> Te | |
kernel.dtype, | ||
], | ||
) | ||
|
||
# padding = (left, right, top, bottom) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep this comment |
||
padding = [kernel_size[0] // 2, kernel_size[0] // 2, kernel_size[1] // 2, kernel_size[1] // 2] | ||
img = torch_pad(img, padding, mode="reflect") | ||
img = conv2d(img, kernel, groups=img.shape[-3]) | ||
|
@@ -758,6 +756,29 @@ def gaussian_blur(img: Tensor, kernel_size: List[int], sigma: List[float]) -> Te | |
return img | ||
|
||
|
||
def gaussian_noise(img: Tensor, mean: float, sigma: float) -> Tensor: | ||
if not (isinstance(img, torch.Tensor)): | ||
raise TypeError(f"img should be Tensor. Got {type(img)}") | ||
|
||
_assert_image_tensor(img) | ||
dtype = img.dtype if torch.is_floating_point(img) else torch.float32 | ||
img, need_cast, need_squeeze, out_dtype = _cast_squeeze_in( | ||
img, | ||
[ | ||
dtype, | ||
], | ||
) | ||
parth-shastri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# add the gaussian noise with the given mean and sigma. | ||
normalize_img = img / 255.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't make this assumption that |
||
noise = sigma * torch.randn_like(img) + mean | ||
img = normalize_img + noise | ||
img = torch.clip(img, 0, 1) | ||
img = img * 255.0 | ||
|
||
img = _cast_squeeze_out(img, need_cast, need_squeeze, out_dtype) | ||
return img | ||
|
||
|
||
def invert(img: Tensor) -> Tensor: | ||
|
||
_assert_image_tensor(img) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something is unclear here about whether sigma is
Optional
or not. Docstring and typehint says optional, but you raise an error if sigma is None. Let's remove optional and assume that sigma is not NoneThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@parth-shastri check above comment and the code. Why do you check
sigma is None
if sigma is not intended to beOptional
.