Bird
Raised Fist0

The Overlapping Boxes Mystery

CODINGintermediateInterview freq: high
Choose your preparation mode6 modes available
🐛
The Overlapping Boxes Mystery
The issue started after the last model update two days ago.

An object detection model deployed in a real-time video surveillance system, processing 500 frames per second.

Symptom
The system is detecting multiple overlapping bounding boxes for the same object, leading to false positives.
📡
Signals
metric
Precision of the object detection model
Precision dropped from 0.85 to 0.60 after the update
log
Logs from the Non-Max Suppression (NMS) function
NMS applied to 1000 bounding boxes, but only reduced to 950
distribution
Distribution of IoU scores for detected boxes
IoU scores are unusually high, clustering around 0.9
code
Review of the IoU calculation code
IoU calculation seems to be returning values greater than 1
🔬
Hypotheses
The IoU calculation is incorrect, leading to improper NMS behavior.
IoU scores are unusually high, clustering around 0.9IoU calculation seems to be returning values greater than 1
Verify: Check the IoU calculation logic for errors in the formula.
def check_iou(bbox1, bbox2):
    # Calculate intersection
    inter_x1 = max(bbox1[0], bbox2[0])
    inter_y1 = max(bbox1[1], bbox2[1])
    inter_x2 = min(bbox1[2], bbox2[2])
    inter_y2 = min(bbox1[3], bbox2[3])
    inter_area = max(0, inter_x2 - inter_x1) * max(0, inter_y2 - inter_y1)
    # Calculate union
    bbox1_area = (bbox1[2] - bbox1[0]) * (bbox1[3] - bbox1[1])
    bbox2_area = (bbox2[2] - bbox2[0]) * (bbox2[3] - bbox2[1])
    union_area = bbox1_area + bbox2_area - inter_area
    # Calculate IoU
    iou = inter_area / union_area
    return max(0, min(1, iou))
The NMS threshold is set too high, allowing too many boxes to pass.
NMS applied to 1000 bounding boxes, but only reduced to 950
Verify: Check the NMS threshold parameter in the configuration.
def check_nms_threshold(threshold):
    return threshold > 0.5
🎯
Root Cause
The IoU calculation was incorrect due to a bug in the formula, causing values greater than 1.

A recent code refactor introduced a mistake in the IoU calculation logic.

🔧
Fix Strategy
ImmediateRollback to the previous stable model version.
Short-termCorrect the IoU calculation logic in the code.
Long-termImplement unit tests for IoU and NMS functions to catch such errors early.
🐍
Python Fix
Python
def correct_iou(bbox1, bbox2):
    inter_x1 = max(bbox1[0], bbox2[0])
    inter_y1 = max(bbox1[1], bbox2[1])
    inter_x2 = min(bbox1[2], bbox2[2])
    inter_y2 = min(bbox1[3], bbox2[3])
    inter_area = max(0, inter_x2 - inter_x1) * max(0, inter_y2 - inter_y1)
    bbox1_area = (bbox1[2] - bbox1[0]) * (bbox1[3] - bbox1[1])
    bbox2_area = (bbox2[2] - bbox2[0]) * (bbox2[3] - bbox2[1])
    union_area = bbox1_area + bbox2_area - inter_area
    iou = inter_area / union_area
    return max(0, min(1, iou))
✗
Common Wrong Diagnoses
▼ Show
The model weights were corrupted during deployment.
Why candidates think this: The precision drop suggests a model issue.
Why wrong: The model itself was not changed; the issue was in post-processing.
The NMS threshold was incorrectly set.
Why candidates think this: High number of boxes post-NMS suggests threshold issues.
Why wrong: The threshold was correct; the problem was with IoU values.
📊
Monitoring to Add
  • Monitor IoU score distributions for anomalies.
  • Set alerts for sudden drops in precision metrics.
  • Log the number of boxes before and after NMS for each frame.
📋
Process Change

Introduce code reviews specifically for mathematical logic changes.

🏗️
Architectural Change

Add a validation step in the CI/CD pipeline to test IoU and NMS outputs against known benchmarks.

💡
Key Insight
Always validate mathematical logic in ML code, especially after refactoring, to prevent subtle bugs.

Practice

(1/5)
1. What is the time complexity of calculating DCG for a list of n relevance scores?
easy
A. O(n)
B. O(n log n)
C. O(n^2)
D. O(log n)

Solution

  1. Step 1: Analyze the DCG Calculation

    DCG involves iterating over the list once and performing constant-time operations for each element.
  2. Step 2: Determine Complexity

    Since each element is processed once, the complexity is O(n).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    DCG iterates once over n elements. [OK]
Hint: DCG iterates once over n elements. [OK]
Common Mistakes:
2. What does the following Python code snippet output when implementing sinusoidal positional encoding?
import numpy as np

def positional_encoding(position, d_model):
    angle_rads = np.arange(position)[:, np.newaxis] / np.power(10000, (2 * (np.arange(d_model)[np.newaxis, :] // 2)) / np.float32(d_model))
    angle_rads[:, 0::2] = np.sin(angle_rads[:, 0::2])
    angle_rads[:, 1::2] = np.cos(angle_rads[:, 1::2])
    return angle_rads

pe = positional_encoding(4, 4)
print(pe)
easy
A. A 4x4 matrix with alternating sine and cosine values
B. A 4x4 matrix with random values
C. A 4x4 identity matrix
D. A 4x4 matrix with all zeros

Solution

  1. Step 1: Understand the code logic

    The code generates a matrix where each row corresponds to a position and each column to a dimension, filled with sinusoidal values.
  2. Step 2: Eliminate wrong options

    A 4x4 matrix with all zeros is incorrect because the matrix is not filled with zeros. A 4x4 identity matrix is incorrect because the matrix is not an identity matrix. A 4x4 matrix with random values is incorrect because the values are not random but deterministic sinusoidal values.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sinusoidal encoding alternates sine/cosine. [OK]
Hint: Sinusoidal encoding alternates sine/cosine. [OK]
Common Mistakes:
3. You implemented linear regression using gradient descent, but the model is not converging. What is the most likely cause?
medium
A. The learning rate is too low.
B. The learning rate is too high.
C. The dataset is too small.
D. The model has too many features.

Solution

  1. Step 1: Identify symptoms of non-convergence

    Non-convergence often results from oscillations due to a high learning rate.
  2. Step 2: Eliminate other options

    Low learning rate causes slow convergence, not non-convergence. Dataset size and feature count affect overfitting, not convergence.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    High learning rate causes oscillations. [OK]
Hint: High learning rate causes oscillations. [OK]
Common Mistakes:
4. In a text classification task using Multinomial Naive Bayes, which preprocessing step is crucial for improving model performance?
medium
A. Normalizing feature values
B. Scaling features to [0, 1]
C. Removing stop words
D. Applying PCA for dimensionality reduction

Solution

  1. Step 1: Understand text preprocessing needs

    In text classification, irrelevant words can add noise.
  2. Step 2: Evaluate preprocessing steps

    Removing stop words (Removing stop words) reduces noise and improves performance. Options A and C are less relevant as MultinomialNB doesn't require normalized or scaled features. Applying PCA for dimensionality reduction is unnecessary as PCA isn't typically used for text data.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Remove stop words to reduce noise in text data. [OK]
Hint: Remove stop words to reduce noise in text data. [OK]
Common Mistakes:
5. How would you adapt the ROC curve implementation to handle multi-class classification?
medium
A. Use one-vs-rest strategy to compute ROC for each class.
B. Use the average of ROC curves for each class.
C. Use the sum of ROC curves for each class.
D. Use the product of ROC curves for each class.

Solution

  1. Step 1: Understand multi-class ROC adaptation

    One-vs-rest strategy is commonly used to compute ROC for each class separately.
  2. Step 2: Eliminate wrong options

    Use the average of ROC curves for each class. is incorrect as averaging doesn't apply directly, Options C and D are mathematically incorrect.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Use one-vs-rest for multi-class ROC [OK]
Hint: Use one-vs-rest for multi-class ROC [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard