Skip to main content

Differentiable contour to mask and contour to distance map implementation with PyTorch

Project description

torch_contour

Example of torch contour on a circle when varying the number of nodes

Example of torch contour on a circle when varying the number of nodes

Pytorch Layers

This library contains 2 pytorch non trainable layers for performing the differentiable operations of :

  1. contour to mask
  2. contour to distance map.

It can therefore be used to transform a polygon into a binary mask or distance map in a completely differentiable way. In particular, it can be used to transform the detection task into a segmentation task. The two layers have no learnable weight, so all it does is apply a function in a derivative way.

Input (Float):

A polygon of size $B \times 2 \times n$ with
with $n$ the number of nodes and $B$ the batch size

Output (Float):

A mask or distance map of size $B \times H \times H$.
with $H$ the Heigh of the distance map or mask and $B$ the batch size

Important:

The polygon must have values between 0 and 1.

Example:

from torch_contour.torch_contour import Contour_to_distance_map, Contour_to_mask
import torch
import matplotlib.pyplot as plt

x = torch.tensor([[0.1,0.1],
                 [0.1,0.9],
                 [0.9,0.9],
                 [0.9,0.1]])[None]

Dmap = Contour_to_distance_map(200)
Mask = Contour_to_mask(200)

plt.imshow(Dmap(x).cpu().detach().numpy()[0,0])
plt.show()
plt.imshow(Mask(x).cpu().detach().numpy()[0,0])
plt.show()

Pytorch functions

This library also contains batch torch operations for performing:

  1. The area of a batch of polygons
  2. The perimeter of a batch of polygons
  3. The haussdorf distance between 2 sets of polygons
from torch_contour.torch_contour import area, perimeter, haussdorf_distance
import torch

polygons1 = torch.tensor([
   [[0, 0], [1, 0], [1, 1], [0, 1]],  # Square
   [[0, 0], [2, 0], [2, 1], [0, 1]]   # Rectangle
], dtype=torch.float32).permute(0, 2, 1)  # Permute to shape (B, 2, N)
polygons2 = torch.tensor([
   [[0, 0], [1, 0], [1, 1], [0, 1]],  # Another Square
   [[0, 0], [2, 0], [2, 2], [0, 2]]   # Another Rectangle
], dtype=torch.float32).permute(0, 2, 1)  # Permute to shape (B, 2, N)


area_ = area(polygons1)
perimeter_ = perimeter(polygons2)
hausdorff_dists = hausdorff_distance(polygons1, polygons2)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_contour-1.0.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

torch_contour-1.0.2-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file torch_contour-1.0.2.tar.gz.

File metadata

  • Download URL: torch_contour-1.0.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.10

File hashes

Hashes for torch_contour-1.0.2.tar.gz
Algorithm Hash digest
SHA256 b1bcc33ea4b3e2ed0692f4a644ff963b76b023d270b9ca763521a2e29072f533
MD5 d15e3750c5122758301f028fe6a7637b
BLAKE2b-256 4243b0921502c7e606f43215c488f108c520a70ebd058c781c6d27583e44f690

See more details on using hashes here.

File details

Details for the file torch_contour-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_contour-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 18a07db533ecae5c9ddd9a84069ac3f5b7615dfe6beb481e010fe0dea60075b0
MD5 9239b8428c1932f883b0f656f2e417da
BLAKE2b-256 d4c6149799141b9bc25c736c56df2f0d968ce1b3e6185e65f61c5793ed799c4d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page