MCPcopy Create free account
hub / github.com/ImprintLab/Medical-SAM2 / DiceCoeff

Class DiceCoeff

func_3d/utils.py:228–252  ·  view source on GitHub ↗

Dice coeff for individual examples

Source from the content-addressed store, hash-verified

226 return s / (i + 1)
227
228class DiceCoeff(Function):
229 """Dice coeff for individual examples"""
230
231 def forward(self, input, target):
232 self.save_for_backward(input, target)
233 eps = 0.0001
234 self.inter = torch.dot(input.view(-1), target.view(-1))
235 self.union = torch.sum(input) + torch.sum(target) + eps
236
237 t = (2 * self.inter.float() + eps) / self.union.float()
238 return t
239
240 # This function has only a single output, so it gets only one gradient
241 def backward(self, grad_output):
242
243 input, target = self.saved_variables
244 grad_input = grad_target = None
245
246 if self.needs_input_grad[0]:
247 grad_input = grad_output * 2 * (target * self.union - self.inter) \
248 / (self.union * self.union)
249 if self.needs_input_grad[1]:
250 grad_target = None
251
252 return grad_input, grad_target

Callers 1

dice_coeffFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected