Skip to content

Commit

Permalink
feat/intersection-over-union (#15)
Browse files Browse the repository at this point in the history
* add notebook NiN

* added notebook NiN

* added notebook

* suggest

* add image IoU

* Add IoU on metrics

* Add IoU on metrics
  • Loading branch information
ramiro999 authored Jul 10, 2024
1 parent 4f4686b commit 4072b5e
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
Binary file added assets/feature-image-iou.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions automation/notebooks-table-data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ U-Net, architectures/unet.ipynb,,1505.04597
DenseNet, architectures/densenet.ipynb,,1608.06993v5
Mobilenet V1, architectures/mobilenetv1.ipynb,,1704.04861
Inception V1, architectures/inceptionv1.ipynb,,1409.4842
Intersection Over Union (IoU),metrics/intersection-over-union.ipynb,,1902.09630
193 changes: 193 additions & 0 deletions notebooks/metrics/intersection-over-union.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"source": [
"<image src=\"https://raw.githubusercontent.com/semilleroCV/deep-learning-notes/main/assets/banner-notebook.png\" width=100%>"
],
"metadata": {
"id": "M1wwX3eCyuPO"
}
},
{
"cell_type": "markdown",
"source": [
"# <font color='#4C5FDA'> **Intersection Over Union (IoU)** </font>"
],
"metadata": {
"id": "MaBAcPfI0RKH"
}
},
{
"cell_type": "markdown",
"source": [
"The paper **<font color=\"EB9A54\">\"The PASCAL Visual Object Classes (VOC) Challenge\"</font>** introduces Intersection over Union (IoU) as a critical metric for evaluating object detection models. IoU measures the overlap between predicted and ground truth bounding boxes, providing a robust standard for assessing model accuracy. Building on this foundation, the paper **<font color=\"EB9A54\">\"Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression\"</font>** extends the concept by introducing Generalized IoU (GIoU), which improves model training and performance by offering a more comprehensive evaluation of bounding box regression."
],
"metadata": {
"id": "iEV5ns6T0Ynt"
}
},
{
"cell_type": "markdown",
"source": [
"$$IOU(\\mathcal{A},\\mathcal{B}) = \\frac{\\left|\\mathcal{A} \\cap \\mathcal{B}\\right|}{\\left| \\mathcal{A} \\cup \\mathcal{B}\\right|}.$$"
],
"metadata": {
"id": "18LUav2hcNlG"
}
},
{
"cell_type": "markdown",
"source": [
"The mAP(mean average precision) metric in object detection is evaluated based on IoU (Intersection Over Union)."
],
"metadata": {
"id": "bDjg6i60bdep"
}
},
{
"cell_type": "markdown",
"source": [
"<image src=\"https://raw.githubusercontent.com/semilleroCV/deep-learning-notes/feat/intersection-over-union/assets/feature-image-iou.jpg\" width=100%>"
],
"metadata": {
"id": "Upy9-GAbcT-v"
}
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"id": "xYEl0lf0v2vl"
},
"outputs": [],
"source": [
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F"
]
},
{
"cell_type": "code",
"source": [
"print(torch.__version__)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6_uXqqwdedqU",
"outputId": "158eded7-83c3-4bd0-ab19-97b1c97428fb"
},
"execution_count": 23,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2.3.0+cu121\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"import torch\n",
"import torch.nn as nn\n",
"\n",
"class IoU(nn.Module):\n",
" def __init__(self):\n",
" super(IoU, self).__init__()\n",
"\n",
" def forward(self, box1, box2):\n",
" # Calculate intersection\n",
" x1 = torch.max(box1[0], box2[0])\n",
" y1 = torch.max(box1[1], box2[1])\n",
" x2 = torch.min(box1[2], box2[2])\n",
" y2 = torch.min(box1[3], box2[3])\n",
"\n",
" intersection = (x2 - x1).clamp(0) * (y2 - y1).clamp(0)\n",
"\n",
" # Calculate areas in the box\n",
" box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1])\n",
" box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])\n",
"\n",
" # Calculate union\n",
" union = box1_area + box2_area - intersection\n",
"\n",
" # Calculate IoU\n",
" iou = intersection / union\n",
" return iou\n"
],
"metadata": {
"id": "FWOemCQ8g0P4"
},
"execution_count": 33,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Example usage 1\n",
"box1 = torch.tensor([10, 20, 25, 35], dtype=torch.float)\n",
"box2 = torch.tensor([10, 20, 30, 40], dtype=torch.float)\n",
"\n",
"iou_calculator = IoU()\n",
"iou = iou_calculator(box1, box2)\n",
"print(f'IoU: {iou.item()}')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NCCqOcI3g9l-",
"outputId": "5846553d-50b4-45c9-8c0c-51910f92745c"
},
"execution_count": 34,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"IoU: 0.5625\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"---\n",
"# <font color='#4C5FDA'> **References** </font>\n",
"\n",
"The PASCAL Visual Object Classes (VOC) Challenge\n",
"\n",
"https://link.springer.com/article/10.1007/s11263-009-0275-4\n",
"\n",
"Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression\n",
"\n",
"https://arxiv.org/pdf/1902.09630\n",
"\n",
"---"
],
"metadata": {
"id": "g81tr2oajhtQ"
}
}
]
}

0 comments on commit 4072b5e

Please sign in to comment.