# Mean Intersection over Union (mIoU) for image segmentation

## Introduction

Image segmentation is the main and fundamental task in computer vision. The image is partitioned into different segments and parts. This image segmentation is widely used in every computer vision task like object detection, medical imaging, and other image editing-related tasks. Evaluating all the models of image segmentation is necessary to see the working of the models. The accuracy is ultimately evaluated to find the image segmentation model. For this purpose, the mean intersection over union is used for the image segmentation.

## What is MIOU?

Miou(Mean Intersection over union) is the primary metric used for the evaluation of the accuracy results of the image segmentation tasks. It will measure the model and how well the segmentation masks relate to the ground masks. mIoU will estimate the level of cross-over and overlapping between the original and predicted segmentations.

Miou describes the overlapping between two boxes. If the overlapping region is bigger, then the value of Miou is also will be greater. The process of miou in image segmentation will be like where we train a model to yield a container that fits impeccably around an object.

## How is MIOU calculated?

The process of calculating is done in different steps, and they are followed as

1. IoU Calculation

The first step is to calculate the intersection over the union. In the segmentation task, every class or category, like background and foreground, will be calculated as the intersection over the union of the respective class. There are two different kinds of masks, like ground truth masks and predicted masks. IoU is the ratio of the area between these two masks respectively.

Area of Union: it is defined as the total number of pixels that are labeled as positive in any of the predicted masks or the other ground truth mask.

Area of Intersection: this is defined as the pixel count in which the positive is labeled respectively in both the ground truth mask and predicted mask.

The formula for the calculation of IoU is

2. The second step is the calculation of the specific classes' IoU value. Every class IoU is calculated separately. For example, if we take an example of binary segmentation tasks like foreground and background, there have been two different classes. There will be multiple classes in much more difficult and complex tasks.

3. mIoU calculation:

Now, in the third step, the main mean value is calculated. It is the main step, after all, and the average of all the class's respective IoU values and scores are calculated into the mean. The formula for the calculation of mIoU is as follows:

here, the n is the total number of classes.

The mean intersection over the union score should be greater in order to have the best performance of the image segmentation model. The main objective behind calculating this mIoU is that the predicted masks will combine very close to the ground truth masks in every class. It is very valuable because the comprehensive single metric will give a complete idea about the quality in multiple classes of image segmentation tasks.

### Python Implementation using NumPy

Code

Output:

```IoU for Class 1 (Foreground): 0.50
IoU for Class 0 (Background): 0.50
Mean IoU (mIoU): 0.50
```