| 30 | return (w * (wbce + wiou)).mean() |
| 31 | |
| 32 | def dice_coefficient(predicted, labels): |
| 33 | if predicted.device != labels.device: |
| 34 | labels = labels.to(predicted.device) |
| 35 | smooth = 1e-6 |
| 36 | predicted_flat = predicted.contiguous().view(-1) |
| 37 | labels_flat = labels.contiguous().view(-1) |
| 38 | intersection = (predicted_flat * labels_flat).sum() |
| 39 | total = predicted_flat.sum() + labels_flat.sum() |
| 40 | return (2. * intersection + smooth) / (total + smooth) |
| 41 | |
| 42 | def iou(predicted, labels): |
| 43 | if predicted.device != labels.device: |