question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Log polar transformation in torchvision.transforms

See original GitHub issue

🚀 The feature

Log polar transformation for images, which maps an image from the 2D Cartesian coordinate system to the log-polar system. This transformation each pixel [x, y] to the (ρ, θ) coordinate system, where ρ is the log of the distance from the center and θ is the angle.

Motivation, pitch

I’ve been researching ways of making one of my CNNs rotation invariant and happened to find this paper. I looked all over the docs for a similar transformation but couldn’t find any, so I was wondering if this transformation could be included in the torchvision.transforms module in order to make it easier to do this type of preprocessing on images.

Alternatives

No response

Additional context

I already have a minimal implementation for my personal use that is based on existing transformations in the package. If you think this feature will be useful, I could polish it up and open a pull request to merge it.

cc @vfdev-5 @datumbox

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
vfdev-5commented, Sep 17, 2021

@aftoul meanwhile you can use the following code as a custom transformation for you use-case. This implementation uses OpenCV to warp the image:

Code
import numpy as np
import cv2
from PIL import Image


def to_log_polar(img, max_radius=32.0):    
    assert isinstance(img, Image.Image)
    
    dsize = img.size
    center = [s // 2 for s in img.size]
    flags = cv2.WARP_POLAR_LOG
    out = cv2.warpPolar(
        np.asarray(img), dsize=dsize, center=center, maxRadius=max_radius, flags=flags
    )
    return Image.fromarray(out)


def from_log_polar(polar_img, max_radius=32.0):
    assert isinstance(img, Image.Image)

    dsize = polar_img.size
    center = [s // 2 for s in polar_img.size]
    flags = cv2.WARP_POLAR_LOG | cv2.WARP_INVERSE_MAP
    out = cv2.warpPolar(
        np.asarray(polar_img), dsize=dsize, center=center, maxRadius=max_radius, flags=flags
    )    
    return Image.fromarray(out)


class ToLogPolar:

    def __call__(self, image):
        return to_log_polar(image)

    
class FromLogPolar:
    
    def __call__(self, image):
        return from_log_polar(image)
Usage
from torchvision.datasets import MNIST
import matplotlib.pyplot as plt

dataset = MNIST("..", download=False)


plt.figure(figsize=(20, 7))
for i in range(4):
    img, target = dataset[i]
    plt.subplot(1, 4, i + 1)
    plt.imshow(img)
    plt.title(f"Target: {target}")  
    

t = ToLogPolar()

plt.figure(figsize=(20, 7))
for i in range(4):
    img, target = dataset[i]
    plt.subplot(1, 4, i + 1)
    plt.imshow(t(img))
    
t_inv = FromLogPolar()

plt.figure(figsize=(20, 7))
for i in range(4):
    img, target = dataset[i]
    plt.subplot(1, 4, i + 1)
    plt.imshow(t_inv(t(img)))
Output

Original: image

Log-Polar: image

Reconstruction: image

HTH

1reaction
vfdev-5commented, Sep 17, 2021

Maybe, it is about providing Log-Polar and its inverse transformations. An image from the paper on MNIST: image Image from the paper…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Log-polar transform
The log-polar transform is performed by remapping points from the 2D Cartesian coordinate system (x,y) to the 2D log-polar coordinate system ρ=log(√(x−xc) ...
Read more >
torch_geometric.transforms - PyTorch Geometric
Saves the polar coordinates of linked nodes in its edge attributes (functional name: polar ). ... This transform can be used with any...
Read more >
torch.polar — PyTorch 1.13 documentation
Constructs a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with absolute value abs and angle angle .
Read more >
PyTorch – torchvision.transforms – GaussianBlur()
The torchvision.transforms module provides many important transformations that can be used to perform different types of manipulations on ...
Read more >
Complete Guide to the DataLoader Class in PyTorch
... show how to load built-in and custom datasets in PyTorch, plus how to transform and rescale the data. ... What's in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found