| 40 | return (2. * intersection + smooth) / (total + smooth) |
| 41 | |
| 42 | def iou(predicted, labels): |
| 43 | if predicted.device != labels.device: |
| 44 | labels = labels.to(predicted.device) |
| 45 | smooth = 1e-6 |
| 46 | predicted_flat = predicted.contiguous().view(-1) |
| 47 | labels_flat = labels.contiguous().view(-1) |
| 48 | intersection = (predicted_flat * labels_flat).sum() |
| 49 | union = predicted_flat.sum() + labels_flat.sum() - intersection |
| 50 | return (intersection + smooth) / (union + smooth) |
| 51 | |
| 52 | def test(model, path, dataset, opt): |
| 53 | data_path = os.path.join(path, dataset) |