Comments (10)
from scipy.ndimage import distance_transform_edt as distance
from skimage import segmentation as skimage_seg
def compute_sdf(img_gt, out_shape):
"""
compute the signed distance map of binary mask
input: segmentation, shape = (batch_size, x, y, z)
output: the Signed Distance Map (SDM)
sdf(x) = 0; x in segmentation boundary
-inf|x-y|; x in segmentation
+inf|x-y|; x out of segmentation
"""
img_gt = img_gt.astype(np.uint8)
gt_sdf = np.zeros(out_shape)
for b in range(out_shape[0]): # batch size
for c in range(1, out_shape[1]):
posmask = img_gt[b]
negmask = 1-posmask
posdis = distance(posmask)
negdis = distance(negmask)
boundary = skimage_seg.find_boundaries(posmask, mode='inner').astype(np.uint8)
sdf = negdis - posdis
sdf[boundary==1] = 0
gt_sdf[b][c] = sdf
return gt_sdf
def boundary_loss(outputs_soft, gt_sdf):
"""
compute boundary loss for binary segmentation
input: outputs_soft: softmax results, shape=(b,2,x,y)
gt_sdf: sdf of ground truth (can be original or normalized sdf); shape=(b,2,x,y)
output: boundary_loss; sclar
"""
pc = outputs_soft[:,1,...]
dc = gt_sdf[:,1,...]
multipled = torch.einsum('bxy, bxy->bxy', pc, dc)
bd_loss = multipled.mean()
return bd_loss
with torch.no_grad():
gt_sdf_npy = compute_sdf(label_batch.cpu().numpy(), outputs_soft.shape)
gt_sdf = torch.from_numpy(gt_sdf_npy).float().cuda(outputs_soft.device.index)
loss_boundary = boundary_loss(outputs_soft, gt_sdf)
loss = alpha*(loss_dice) + (1 - alpha) * loss_boundary # alpha = 1.0
alpha -= 0.01
if alpha <= 0.01:
alpha = 0.01
from seglossodyssey.
@JunMa11 yeah and also make it multi-class ๐
from seglossodyssey.
Hi @MohamedAliRashad ,
We have a demo reimplementation at
and
Hope it could help you.
Best,
Jun
from seglossodyssey.
Thanks @JunMa11, but i still notice that the input and output are in this shape (b,2,x,y,z)
. Is there a way to make the DistLoss take the same inputs like the CrossEntropyLoss
from pytorch.
network_output=(Batch, Num_classes, H, W)
target_shape=(Batch, H, W)
from seglossodyssey.
Hi @MohamedAliRashad ,
You may need to use one-hot encode to labels or manually specify one dimension like this
from seglossodyssey.
@JunMa11 How this will enable me to use it with the Boundary Loss ?
And while we are on the subject, what compound loss would you suggest for an imbalanced, hight IoU requirement ?
from seglossodyssey.
Hi @MohamedAliRashad ,
To use boundary loss, please refer to this out-of-the-box code.
what compound loss would you suggest for an imbalanced
Based on my experience, using BD loss+ Dice loss is better than only Dice loss.
However, it should be noted that this is still an unsolved problem.
from seglossodyssey.
@JunMa11 I have some questions if you can help me.
- what is the precomputed distance map is used for ?
- Does the implementations of
DC_and_BD_loss
support normal images like it supports volumetric ones ? - if i want to combine the two losses is it better to just sum them or should i add weights ? and if i should add weights, how to determine them ?
- What is the
apply_nonlin
parameter in DICE ?
from seglossodyssey.
Hi @MohamedAliRashad ,
Sorry for my late reply.
- boundary loss relies on the distance map.
- No.
- According to the original paper, in the beginning, larger weights should be given to dice loss, and then gradually increase the weights of BD loss.
apply_nonlin
means apply softmax to the network logits output.
Again, please refer to
if you want to use boundary loss for your own tasks.
Best,
Jun
from seglossodyssey.
@JunMa11 I am sorry for the disturbance, I am just trying to figure out how to adjust the DC_and_BD_loss
function for a 2D input.
from seglossodyssey.
Related Issues (20)
- can this be used in 2D seg ? HOT 2
- What loss is worth trying for multi classification unbalanced data sets? HOT 1
- hi, how could we use boundary loss for nnunet? HOT 1
- Please add a license to this repository HOT 2
- about gradient HOT 5
- About distance map HOT 1
- Why some loss functions in README figure1 have same color? HOT 1
- How long will it take to train with DICEHD on multiorgan dataset by nnunet? HOT 2
- Polyloss HOT 1
- the time training for one epoch with boundary-loss is too long, are there some answers๏ผ HOT 2
- multi focal loss alpha HOT 1
- multi focal loss alpha HOT 1
- About Sensitivity Specificity loss function implementation HOT 1
- Hello, can you provide the ASOCA dataset? HOT 1
- why is there no resolution input in the distance_map computing(scipy.ndimage.distance_transform_edt)? HOT 1
- AttributeError: 'IoULoss' object has no attribute 'backward'
- Violin plot HOT 1
- is nnUNetTrainerV2.py available? HOT 1
- Request to include FCM loss.
- Remove additional entry of seglossbias?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from seglossodyssey.