FZJ-INM1-BDA / celldetection

Scalable Instance Segmentation using PyTorch & PyTorch Lightning.
https://docs.celldetection.org
Apache License 2.0
125 stars 21 forks source link

Converting Countours to Labels #12

Closed pakiessling closed 1 year ago

pakiessling commented 1 year ago

Hi, I must be overlooking something obvious. How do I save calculated contours as a label image?

Keeping with your Coin example:

# Run model
with torch.no_grad():
    x = cd.to_tensor(img, transpose=True, device=device, dtype=torch.float32)
    x = x / 255  # ensure 0..1 range
    x = x[None]  # add batch dimension: Tensor[3, h, w] -> Tensor[1, 3, h, w]
    y = model(x)

Bothlabel = cd.data.contours2labels(y['contours'],size=img.shape[:2]) andlabel = cd.data.contours2labels(np.array(y['contours']),size=img.shape[:2]) give me error messages.

pakiessling commented 1 year ago

Ok,

label = cd.data.contours2labels(y["contours"][0].detach().numpy(),size=img.shape[:2])

does the trick

pakiessling commented 1 year ago

I noticed that label = cd.data.contours2labels(y["contours"][0].to("cpu").detach().numpy(),size=img.shape[:2]) returns multiple channels. What excatly does "Channels are used to model overlap" mean? I saved label = label[:,:,0] , but it actually looked different (much worse) then the plotted contour. What is the best way to create a single 1 ... n label image for comparison to ground truth?

ericup commented 1 year ago

Hi, for the contours2labels function it is important to note that it expects numpy arrays, rather than Tensors, and works on single batch items. For specifics, you may also refer to the documentation.

By default, the label images come with channels, as contours may assign pixels to multiple objects. Since such multi-assignments cannot be easily encoded in a channel-free label image, channels are used. I updated the documentation to make this clearer. There are many ways to resolve unwanted overlaps, i.e. channels. I just pushed cd.data.resolve_label_channels, which currently offers the solution used in the CellSeg challenge.

For your case you could do the following:

contours, = cd.asnumpy(y['contours'])
labels = cd.data.contours2labels(contours, img.shape[:2])
labels = cd.data.resolve_label_channels(labels)  # remove channels

Note, that you need to install the latest version of celldetection from git to use resolve_label_channels.

pakiessling commented 1 year ago

Thanks for pushing such a quick update!