| 24 | return (2. * intersection + smooth) / (total + smooth) |
| 25 | |
| 26 | def iou(predicted, labels): |
| 27 | if predicted.device != labels.device: |
| 28 | labels = labels.to(predicted.device) |
| 29 | smooth = 1e-6 |
| 30 | predicted_flat = predicted.contiguous().view(-1) |
| 31 | labels_flat = labels.contiguous().view(-1) |
| 32 | intersection = (predicted_flat * labels_flat).sum() |
| 33 | union = predicted_flat.sum() + labels_flat.sum() - intersection |
| 34 | return (intersection + smooth) / (union + smooth) |
| 35 | |
| 36 | def get_binary_metrics(pred, gt): |
| 37 | tp = (pred * gt).sum().item() |