ELEKTRONN / elektronn3

A PyTorch-based library for working with 3D and 2D convolutional neural networks, with focus on semantic segmentation of volumetric biomedical image data
MIT License
160 stars 27 forks source link

Sparse annotations for training #51

Open SebastienTs opened 1 year ago

SebastienTs commented 1 year ago

Is there currently a way to use sparse annotations for training, i.e. for pixel classification by ignoring all the pixels set to 0 in the target image during loss computation and only considering pixels set to a value >0? If that is not possible, what would be the minimal modification to the code to achieve this (at least for some, or ideally for all existing losses)?

mdraw commented 1 year ago

Hello @SebastienTs, yes this is possible with the regular torch.nn.CrossEntropyLoss by setting the ignore_index parameter to the label ID that you want to ignore. Since 0 is often reserved for a background class I usually use other ID values. It just has to be the ID that is used in the label files; no changes to the model outputs are needed.

The DiceLoss in elektronn3 doesn't support an ignore_index option but you can use the weight parameter to set the channel weight of the class that is to be ignored to 0.

SebastienTs commented 1 year ago

Great! Thanks so much Martin!

On Thu 17 Aug 2023, 21:09 Martin Drawitsch, @.***> wrote:

Hello @SebastienTs https://github.com/SebastienTs, yes this is possible with the regular torch.nn.CrossEntropyLoss by setting the ignore_index parameter to the label ID that you want to ignore. Since 0 is often reserved for a background class I usually use other ID values. It just has to be the ID that is used in the label files; no changes to the model outputs are needed.

The DiceLoss in elektronn3 doesn't support an ignore_idx option but you can use the weight parameter to set the channel weight of the class that is to be ignored to 0.

— Reply to this email directly, view it on GitHub https://github.com/ELEKTRONN/elektronn3/issues/51#issuecomment-1682818880, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTFCUG7D2DZ6B4KC66BBBDXVZT5HANCNFSM6AAAAAA3UPGTAE . You are receiving this because you were mentioned.Message ID: @.***>