From 67f2ee2c82ed9e525e2d767eb1a8dcf1042faecd Mon Sep 17 00:00:00 2001 From: dsaha21 Date: Sat, 1 Nov 2025 13:23:09 +0530 Subject: [PATCH] add: All required files for iou-score --- questions/_template/description.md | 4 +- questions/_template/example.json | 15 ++- questions/_template/learn.md | 107 +++++++++++-------- questions/_template/meta.json | 11 +- questions/_template/pytorch/solution.py | 2 - questions/_template/pytorch/starter_code.py | 2 - questions/_template/pytorch/tests.json | 6 -- questions/_template/solution.py | 20 +++- questions/_template/starter_code.py | 12 ++- questions/_template/tests.json | 22 +++- questions/_template/tinygrad/solution.py | 2 - questions/_template/tinygrad/starter_code.py | 2 - questions/_template/tinygrad/tests.json | 6 -- 13 files changed, 131 insertions(+), 80 deletions(-) delete mode 100644 questions/_template/pytorch/solution.py delete mode 100644 questions/_template/pytorch/starter_code.py delete mode 100644 questions/_template/pytorch/tests.json delete mode 100644 questions/_template/tinygrad/solution.py delete mode 100644 questions/_template/tinygrad/starter_code.py delete mode 100644 questions/_template/tinygrad/tests.json diff --git a/questions/_template/description.md b/questions/_template/description.md index 684dbcd6..617fbf4c 100644 --- a/questions/_template/description.md +++ b/questions/_template/description.md @@ -1,3 +1,3 @@ -## Problem +Write a Python function to implement the Intersection over Union Score. The function should calculate the score of area of overlap of 2 bounding boxes divided by the area of union of both box. -Write a concise problem description here. +The output : iou score of datatype float \ No newline at end of file diff --git a/questions/_template/example.json b/questions/_template/example.json index 4e7fdd99..e2a96964 100644 --- a/questions/_template/example.json +++ b/questions/_template/example.json @@ -1,5 +1,14 @@ { - "input": "...", - "output": "...", - "reasoning": "Explain why the output follows from the input." + "input": { + "bbox_A": [100, 100, 200, 200], + "bbox_B": [150, 150, 250, 250] + }, + "output": { + "iou_score": 0.146 + }, + "reasoning": "The output shows the Intersection over Union (IoU) score between bbox_A and bbox_B. Here's how it's calculated: bbox_A is [100,100,200,200] representing [x1,y1,x2,y2] bbox_B is [150,150,250,250] representing [x1,y1,x2,y2] + The intersection area is 50x50=2500 (overlapping region).The union area is the sum of both areas minus the intersection: + - Area_A = 100x100 = 10000 - Area_B = 100x100 = 10000 + - Union = 10000 + 10000 - 2500 = 17500 IoU = intersection/union = 2500/17500 ≈ 0.14286 + " } diff --git a/questions/_template/learn.md b/questions/_template/learn.md index 31c0cec5..386020e3 100644 --- a/questions/_template/learn.md +++ b/questions/_template/learn.md @@ -1,47 +1,64 @@ ## Solution Explanation -Add intuition, math, and step-by-step reasoning here. - -### Writing Mathematical Expressions with LaTeX - -This editor supports LaTeX for rendering mathematical equations and expressions. Here's how you can use it: - -1. **Inline Math**: - - Wrap your expression with single `$` symbols. - - Example: `$E = mc^2$` → Renders as: ( $E = mc^2$ ) - -2. **Block Math**: - - Wrap your expression with double `$$` symbols. - - Example: - ``` - $$ - \int_a^b f(x) \, dx - $$ - ``` - Renders as: - $$ - \int_a^b f(x) \, dx - $$ - -3. **Math Functions**: - - Use standard LaTeX functions like `\frac`, `\sqrt`, `\sum`, etc. - - Examples: - - `$\frac{a}{b}$` → ( $\frac{a}{b}$ ) - - `$\sqrt{x}$` → ( $\sqrt{x}$ ) - -4. **Greek Letters and Symbols**: - - Use commands like `\alpha`, `\beta`, etc., for Greek letters. - - Example: `$\alpha + \beta = \gamma$` → ( $\alpha + \beta = \gamma$ ) - -5. **Subscripts and Superscripts**: - - Use `_{}` for subscripts and `^{}` for superscripts. - - Examples: - - `$x_i$` → ( $x_i$ ) - - `$x^2$` → ( $x^2$ ) - -6. **Combined Examples**: - - `$\sum_{i=1}^n i^2 = \frac{n(n+1)(2n+1)}{6}$` - Renders as: - $\sum_{i=1}^n i^2 = \frac{n(n+1)(2n+1)}{6}$ - -Feel free to write your own mathematical expressions, and they will be rendered beautifully in the preview! +Intuition +---- + +The Intersection over Union (IoU) score is a metric used to evaluate how well a predicted region (for example, a bounding box or segmentation mask) matches the ground truth region. + +The intuition is simple: + +It measures how much the predicted area overlaps with the true area, +And compares that overlap to the total area covered by both regions combined. +If the prediction perfectly matches the ground truth, IoU = 1 (perfect overlap). +If the prediction does not overlap at all, IoU = 0. + +Thus, IoU quantifies spatial similarity between the prediction and the ground truth. + +Mathematical definition: +IoU = Area of Overlap/Area of Union + + +## Step-by-step reasoning: + +1. Identify regions: Determine the predicted area B and ground truth area A. + + `INPUTS bboxA and bboxB` + + `bbox = [x1, y1, x2, y2]` + + (x1,y1 -> top left coordinates || x2,y2 -> bottom right coordinates ) + + +2. Compute intersection: Calculate the overlapping area `|A ∩ B∣` + +3. Compute union: Calculate the combined area ∣ 𝐴 ∪ 𝐵 ∣ = ∣ 𝐴 ∣ + ∣ 𝐵 ∣ − ∣ 𝐴 ∩ 𝐵 ∣ +` ∣ A∪B ∣ = ∣A∣ + ∣B∣ − ∣ A∩B∣ ` + +4. Compute IoU: Divide the intersection area by the union area. + + +Example: +If +∣A∣=100, ∣B∣=80, and ∣A∩B∣=60, +then: + +`∣A∪B∣ = 100 + 80 − 60 = 120` + +`IoU = 60/120 = 0.5` + +→ The prediction overlaps 50% with the ground truth. + + +## THE MATH + +The IoU score is calculated as $IoU = \frac{|A \cap B|}{|A \cup B|}$, +where $A$ is the ground truth and $B$ is the predicted region. + +where: +A = Ground truth region +B = Predicted region +∣A∩B∣ = Intersection area (common area between A and B) +∣A∪B∣ = Union area (total area covered by A and B) + + +-------- \ No newline at end of file diff --git a/questions/_template/meta.json b/questions/_template/meta.json index 9db2e26a..c5a3aab5 100644 --- a/questions/_template/meta.json +++ b/questions/_template/meta.json @@ -1,12 +1,17 @@ { "id": "XXX", - "title": "TITLE GOES HERE", + "title": "IOU-score calculation", "difficulty": "medium", - "category": "Machine Learning", + "category": "Computer Vision", "video": "", "likes": "0", "dislikes": "0", - "contributor": [], + "contributor": [ + { + "profile_link": "https://github.com/dsaha21", + "name": "Dripto Saha" + } + ], "tinygrad_difficulty": "", "pytorch_difficulty": "" } diff --git a/questions/_template/pytorch/solution.py b/questions/_template/pytorch/solution.py deleted file mode 100644 index 9b74bcbd..00000000 --- a/questions/_template/pytorch/solution.py +++ /dev/null @@ -1,2 +0,0 @@ -def your_function(...): - ... diff --git a/questions/_template/pytorch/starter_code.py b/questions/_template/pytorch/starter_code.py deleted file mode 100644 index d3e5beb5..00000000 --- a/questions/_template/pytorch/starter_code.py +++ /dev/null @@ -1,2 +0,0 @@ -def your_function(...): - pass diff --git a/questions/_template/pytorch/tests.json b/questions/_template/pytorch/tests.json deleted file mode 100644 index e4e4b180..00000000 --- a/questions/_template/pytorch/tests.json +++ /dev/null @@ -1,6 +0,0 @@ -[ - { - "test": "print(your_function(...))", - "expected_output": "..." - } -] diff --git a/questions/_template/solution.py b/questions/_template/solution.py index b1ff1c5b..d621c0f7 100644 --- a/questions/_template/solution.py +++ b/questions/_template/solution.py @@ -1,3 +1,17 @@ -def your_function(...): - # reference implementation - ... +import numpy as np +def iou_score(bboxA, bboxB): + xA = np.maximum(bboxA[0], bboxB[0]) + yA = np.maximum(bboxA[1], bboxB[1]) + xB = np.minimum(bboxA[2], bboxB[2]) + yB = np.minimum(bboxA[3], bboxB[3]) + + interWidth = np.maximum(0, xB - xA + 1) + interHeight = np.maximum(0, yB - yA + 1) + interArea = interWidth * interHeight + + boxAArea = (bboxA[2] - bboxA[0] + 1) * (bboxA[3] - bboxA[1] + 1) + boxBArea = (bboxB[2] - bboxB[0] + 1) * (bboxB[3] - bboxB[1] + 1) + + #unionArea = boxAArea + boxBArea - interArea + iou = interArea / float(boxAArea + boxBArea - interArea) + return round(iou, 3) \ No newline at end of file diff --git a/questions/_template/starter_code.py b/questions/_template/starter_code.py index 564b3118..19df9234 100644 --- a/questions/_template/starter_code.py +++ b/questions/_template/starter_code.py @@ -1,4 +1,12 @@ # Implement your function below. -def your_function(...): - pass +def iou_score(bboxA, bboxB): + #xA = np.maximum(bboxA[0], bboxB[0]) + + #interWidth = np.maximum(0, xB - xA + 1) + #interArea = interWidth * interHeight + + #find bbox areas of A and B + + iou = interArea / float(boxAArea + boxBArea - interArea) + return round(iou, 3) diff --git a/questions/_template/tests.json b/questions/_template/tests.json index e4e4b180..2c57ce85 100644 --- a/questions/_template/tests.json +++ b/questions/_template/tests.json @@ -1,6 +1,24 @@ [ { - "test": "print(your_function(...))", - "expected_output": "..." + "test": "print(iou_score([0, 0, 100, 100],[0, 0, 100, 100]))", + "expected_output": "1.0" + }, + { + "test": "print(iou_score([0, 0, 50, 50],[60, 60, 100, 100]))", + "expected_output": "0.0" + }, + { + "test": "print(iou_score([100, 100, 200, 200],[150, 150, 250, 250]))", + "expected_output": "0.146" + }, + { + "test": "print(iou_score([10, 20, 100, 100],[90, 90, 200, 201]))", + "expected_output": "0.006" + }, + { + "test": "print(iou_score([100, 100.2, 200.7, 201.5],[150.5, 150, 250.8, 250.9]))", + "expected_output": "0.149" } + + ] diff --git a/questions/_template/tinygrad/solution.py b/questions/_template/tinygrad/solution.py deleted file mode 100644 index 9b74bcbd..00000000 --- a/questions/_template/tinygrad/solution.py +++ /dev/null @@ -1,2 +0,0 @@ -def your_function(...): - ... diff --git a/questions/_template/tinygrad/starter_code.py b/questions/_template/tinygrad/starter_code.py deleted file mode 100644 index d3e5beb5..00000000 --- a/questions/_template/tinygrad/starter_code.py +++ /dev/null @@ -1,2 +0,0 @@ -def your_function(...): - pass diff --git a/questions/_template/tinygrad/tests.json b/questions/_template/tinygrad/tests.json deleted file mode 100644 index e4e4b180..00000000 --- a/questions/_template/tinygrad/tests.json +++ /dev/null @@ -1,6 +0,0 @@ -[ - { - "test": "print(your_function(...))", - "expected_output": "..." - } -]