This library contains 2 pytorch layers for performing the diferentiable 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 derivative way.
A polygon of size
with
A mask or distance map of size
with
The polygon must have values between 0 and 1. It is therefore important to apply a sigmoid function before the layer.*.
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()