Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions questions/_template/description.md
Original file line number Diff line number Diff line change
@@ -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
15 changes: 12 additions & 3 deletions questions/_template/example.json
Original file line number Diff line number Diff line change
@@ -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
"
}
107 changes: 62 additions & 45 deletions questions/_template/learn.md
Original file line number Diff line number Diff line change
@@ -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)


--------
11 changes: 8 additions & 3 deletions questions/_template/meta.json
Original file line number Diff line number Diff line change
@@ -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": ""
}
2 changes: 0 additions & 2 deletions questions/_template/pytorch/solution.py

This file was deleted.

2 changes: 0 additions & 2 deletions questions/_template/pytorch/starter_code.py

This file was deleted.

6 changes: 0 additions & 6 deletions questions/_template/pytorch/tests.json

This file was deleted.

20 changes: 17 additions & 3 deletions questions/_template/solution.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 10 additions & 2 deletions questions/_template/starter_code.py
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 20 additions & 2 deletions questions/_template/tests.json
Original file line number Diff line number Diff line change
@@ -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"
}


]
2 changes: 0 additions & 2 deletions questions/_template/tinygrad/solution.py

This file was deleted.

2 changes: 0 additions & 2 deletions questions/_template/tinygrad/starter_code.py

This file was deleted.

6 changes: 0 additions & 6 deletions questions/_template/tinygrad/tests.json

This file was deleted.