# Precision, recall and mIOU for binary segmentation

**URL:** <https://discuss.pytorch.org/t/precision-recall-and-miou-for-binary-segmentation/65884>\
**Category:** vision\
**Created:** [January 6, 2020, 8:06am UTC](https://discuss.pytorch.org/t/precision-recall-and-miou-for-binary-segmentation/65884 "2020-01-06T08:06:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_sp](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/e19adc/32.png) [@Jack\_sp](https://discuss.pytorch.org/u/Jack_sp)\
**Post date:** [January 6, 2020, 8:06am UTC](https://discuss.pytorch.org/t/precision-recall-and-miou-for-binary-segmentation/65884/1 "2020-01-06T08:06:29Z")

</div>

I am doing a binary segmentation of brain tumors from CT scan images. Following is the code I am using for precision and recall

```auto
def get_precision(PR,GT,threshold=0.5):
    PR = PR > threshold
    GT = GT == torch.max(GT)
    TP = ((PR==1)&(GT==1))
    FP = ((PR==1)&(GT==0))
    Precision = float(torch.sum(TP))/(float(torch.sum(TP)+torch.sum(FP)) + 1e-6)
    return Precision

```

```auto
def get_recall(PR,GT,threshold=0.5):
    PR = PR > threshold
    GT = GT == torch.max(GT)
    TP = ((PR==1)&(GT==1))
    FN = ((PR==0)&(GT==0))
    Recall = float(torch.sum(TP))/(float(torch.sum(TP)+torch.sum(FN)) + 1e-6)
    return Recall

```

PR is the predicted probabilities of the model from sigmoid layer and GT is the ground truth images divided by 255.Is this the right implementation of the metrics for binary segmentation as I am getting both values in high 90’s. Also can I follow same approach for calculating mIOU.
