Differentiable contour to mask and contour to distance map implementation with PyTorch
Project description
torch_contour
![](https://github.com/antoinehabis/torch_contour/blob/main/vary_nodes.jpg?raw=True)Pytorch Layers
This library contains 2 pytorch non trainable layers for performing the differentiable operations of :
- contour to mask
- 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 differentiable 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 shape $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:
- The area of a batch of polygons
- The perimeter of a batch of polygons
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file torch_contour-1.0.3.tar.gz
.
File metadata
- Download URL: torch_contour-1.0.3.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d6df8aabd4af59fec963d0a58a846f898d8fca8562a29c549a231c36bb156f9 |
|
MD5 | 84f653ba8a4555af563b6815b2cc2588 |
|
BLAKE2b-256 | 9e403d39dee7db3dc7a639ead0b1e4c3cdfe682eb70a8cb645ef5777a84453da |
File details
Details for the file torch_contour-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: torch_contour-1.0.3-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09acec75d327e2b6bb61826f39662d9c25849cae271aad038447215c3c21da47 |
|
MD5 | a60adbfc2b59975b13c0d6a7deab85a0 |
|
BLAKE2b-256 | 44acf14e85f4610ab5a19a87f179b8edda65061620c127553bf927109f8419d9 |