| 14 | from medpy.metric.binary import hd95 |
| 15 | |
| 16 | def dice_coefficient(predicted, labels): |
| 17 | if predicted.device != labels.device: |
| 18 | labels = labels.to(predicted.device) |
| 19 | smooth = 1e-6 |
| 20 | predicted_flat = predicted.contiguous().view(-1) |
| 21 | labels_flat = labels.contiguous().view(-1) |
| 22 | intersection = (predicted_flat * labels_flat).sum() |
| 23 | total = predicted_flat.sum() + labels_flat.sum() |
| 24 | return (2. * intersection + smooth) / (total + smooth) |
| 25 | |
| 26 | def iou(predicted, labels): |
| 27 | if predicted.device != labels.device: |