Bird
Raised Fist0

2D Convolution Filter Debugging

CODINGadvancedInterview freq: very_high
Choose your preparation mode6 modes available
🐛
The Blurry Image Mystery
The issue started immediately after the deployment of the new convolution filter implementation.

Image classification model using a custom 2D convolution filter for preprocessing, deployed in a real-time image recognition system.

Symptom
Model accuracy dropped from 92% to 65% after the latest deployment.
📡
Signals
metric
Model accuracy on the test set
Dropped from 92% to 65% after deployment
log
Error logs from the convolution filter function
No errors reported
distribution
Output distribution of the convolution filter
All output values are significantly lower than expected
code
Review of the convolution filter implementation
Found a discrepancy in the filter application logic
latency
Processing time for the convolution filter
Increased by 30% compared to previous implementation
🔬
Hypotheses
The convolution filter is incorrectly normalizing the output values.
Output distribution of the convolution filter
Verify: Check the normalization step in the convolution filter implementation.
import numpy as np

# Simulate a convolution output
output = np.array([[0.1, 0.2], [0.3, 0.4]])
# Check normalization
normalized_output = output / np.max(output)
print(normalized_output)
The convolution filter is applying the kernel incorrectly, leading to incorrect feature extraction.
Code review of the convolution filter implementation
Verify: Review the kernel application logic to ensure correct sliding window operation.
def apply_convolution(image, kernel):
    # Check kernel application logic
    kernel_height, kernel_width = kernel.shape
    image_height, image_width = image.shape
    output = np.zeros((image_height - kernel_height + 1, image_width - kernel_width + 1))
    for i in range(output.shape[0]):
        for j in range(output.shape[1]):
            output[i, j] = np.sum(image[i:i+kernel_height, j:j+kernel_width] * kernel)
    return output

# Test with a sample image and kernel
image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
kernel = np.array([[1, 0], [0, -1]])
print(apply_convolution(image, kernel))
🎯
Root Cause
The convolution filter was incorrectly normalizing the output, leading to a loss of feature information.

A recent code change introduced an incorrect normalization step that scaled down the output values excessively.

🔧
Fix Strategy
ImmediateRollback to the previous stable version of the convolution filter.
Short-termCorrect the normalization logic in the convolution filter implementation.
Long-termImplement unit tests for the convolution filter to validate output ranges and correctness.
🐍
Python Fix
Python
def apply_convolution(image, kernel):
    kernel_height, kernel_width = kernel.shape
    image_height, image_width = image.shape
    output = np.zeros((image_height - kernel_height + 1, image_width - kernel_width + 1))
    for i in range(output.shape[0]):
        for j in range(output.shape[1]):
            output[i, j] = np.sum(image[i:i+kernel_height, j:j+kernel_width] * kernel)
    # Correct normalization
    return output / np.max(output)
✗
Common Wrong Diagnoses
▼ Show
The model architecture is flawed.
Why candidates think this: A significant drop in accuracy often points to architectural issues.
Why wrong: The architecture was unchanged; the issue arose from a preprocessing step.
Data pipeline issues causing corrupted inputs.
Why candidates think this: Corrupted inputs can lead to poor model performance.
Why wrong: Data inputs were verified to be correct; the issue was isolated to the convolution filter.
📊
Monitoring to Add
  • Monitor output value distribution of the convolution filter.
  • Set up alerts for significant drops in model accuracy post-deployment.
  • Track processing time changes for key preprocessing steps.
📋
Process Change

Introduce a code review checklist that includes validation of mathematical operations in ML components.

🏗️
Architectural Change

Adopt a testing framework that includes automated validation of preprocessing steps against known benchmarks.

💡
Key Insight
Normalization errors in preprocessing can silently degrade model performance, highlighting the need for thorough validation.

Practice

(1/5)
1. What does the following Python code snippet output when implementing the forward pass of batch normalization?
import numpy as np

X = np.array([[1, 2], [3, 4], [5, 6]])
mean = np.mean(X, axis=0)
var = np.var(X, axis=0)
X_norm = (X - mean) / np.sqrt(var + 1e-8)
print(X_norm)
easy
A. A matrix with zero mean but not unit variance for each feature.
B. A matrix with unit variance but not zero mean for each feature.
C. A matrix with zero mean and unit variance for each feature.
D. A matrix with neither zero mean nor unit variance for each feature.

Solution

  1. Step 1: Understand the code

    The code calculates the mean and variance for each feature, normalizes the data, and prints it.
  2. Step 2: Check the normalization

    Batch normalization ensures each feature has zero mean and unit variance.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Batch norm: zero mean, unit variance [OK]
Hint: Batch norm: zero mean, unit variance [OK]
Common Mistakes:
2. In a pipeline that includes flipping, cropping, and normalizing images, where should normalization be applied for best results?
medium
A. Before flipping and cropping
B. After flipping but before cropping
C. After cropping but before flipping
D. After both flipping and cropping

Solution

  1. Step 1: Understand the impact of operations

    Normalization should be the last step to ensure consistent scaling.
  2. Step 2: Evaluate each option

    Applying normalization last ensures it accounts for all transformations.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Normalize last for consistent scaling. [OK]
Hint: Normalize last for consistent scaling. [OK]
Common Mistakes:
3. In which scenario is gradient checking most useful?
medium
A. When implementing a new custom layer in a neural network.
B. When training a model with a large dataset.
C. When optimizing hyperparameters.
D. When deploying a model to production.

Solution

  1. Step 1: Identify scenarios where gradient checking is applicable

    Gradient checking is crucial when implementing new components to ensure correctness.
  2. Step 2: Eliminate unrelated scenarios

    Options A, C, and D are not directly related to verifying gradient correctness.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Use gradient check for new components. [OK]
Hint: Use gradient check for new components. [OK]
Common Mistakes:
4. 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:
5. In a Random Forest implementation, how would you apply feature sampling to handle high-dimensional data effectively?
medium
A. Use all features for each tree to ensure accuracy
B. Use a fixed subset of features for all trees
C. Randomly select a small subset of features for each tree
D. Select features based on their correlation with the target

Solution

  1. Step 1: Understand Feature Sampling

    Feature sampling involves selecting a random subset of features for each tree to reduce dimensionality.
  2. Step 2: Evaluate Options

    Using all features (A) or a fixed subset (C) doesn't reduce dimensionality; correlation-based selection (D) isn't random.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Random subsets reduce dimensionality [OK]
Hint: Random subsets reduce dimensionality [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