Skip to content
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

How to approximate pytorch's gaussian blur using PIL? #6744

Closed
vedantroy opened this issue Nov 16, 2022 · 3 comments
Closed

How to approximate pytorch's gaussian blur using PIL? #6744

vedantroy opened this issue Nov 16, 2022 · 3 comments

Comments

@vedantroy
Copy link

vedantroy commented Nov 16, 2022

Pytorch has the method:

    dtype = image.dtype if torch.is_floating_point(image) else torch.float32
    kernel = _get_gaussian_kernel2d(kernel_size, sigma, dtype=dtype, device=image.device)
    kernel = kernel.expand(image.shape[-3], 1, kernel.shape[0], kernel.shape[1])

    image, need_cast, need_squeeze, out_dtype = _FT._cast_squeeze_in(image, [kernel.dtype])

    # padding = (left, right, top, bottom)
    padding = [kernel_size[0] // 2, kernel_size[0] // 2, kernel_size[1] // 2, kernel_size[1] // 2]
    output = torch_pad(image, padding, mode="reflect")
    output = conv2d(output, kernel, groups=output.shape[-3])

which does a gaussian blur of a tensor using the following helper methods:

def _get_gaussian_kernel1d(kernel_size: int, sigma: float, dtype: torch.dtype, device: torch.device) -> torch.Tensor:
    lim = (kernel_size - 1) / (2 * math.sqrt(2) * sigma)
    x = torch.linspace(-lim, lim, steps=kernel_size, dtype=dtype, device=device)
    kernel1d = torch.softmax(-x.pow_(2), dim=0)
    return kernel1d


def _get_gaussian_kernel2d(
    kernel_size: List[int], sigma: List[float], dtype: torch.dtype, device: torch.device
) -> torch.Tensor:
    kernel1d_x = _get_gaussian_kernel1d(kernel_size[0], sigma[0], dtype, device)
    kernel1d_y = _get_gaussian_kernel1d(kernel_size[1], sigma[1], dtype, device)
    kernel2d = kernel1d_y.unsqueeze(-1) * kernel1d_x
    return kernel2d

Direct link to code: https://github.com/pytorch/vision/blob/ad128b753c7e8cc0c600dfddac22ff48fc73c9d9/torchvision/prototype/transforms/functional/_misc.py#L85.

PIL's gaussian blur method takes in a single parameter, radius. I was wondering how to approximate Pytorch's gaussian blur using PIL?

@radarhere
Copy link
Member

There's actually an open issue about this on pytorch - pytorch/vision#5194

@radarhere radarhere changed the title [Question] How to approximate pytorch's gaussian blur using PIL? How to approximate pytorch's gaussian blur using PIL? Nov 16, 2022
@radarhere
Copy link
Member

radarhere commented Nov 26, 2022

pytorch's gaussian_blur_image_tensor takes a sigma argument.

ImagingGaussianBlur(Imaging imOut, Imaging imIn, float radius, int passes) {
float sigma2, L, l, a;
sigma2 = radius * radius / passes;
// from https://www.mia.uni-saarland.de/Publications/gwosdek-ssvm11.pdf

Looking at https://www.mia.uni-saarland.de/Publications/gwosdek-ssvm11.pdf and the above code, sigma squared is equal to the radius squared divided by the number of passes (the number of passes is 3 by default).

Could you specify what you mean by 'approximate'?

@github-actions
Copy link

github-actions bot commented Dec 6, 2022

Closing this issue as no feedback has been received.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants