Click4Ai

141.

Easy

Cutout Augmentation

Implement Cutout augmentation, a regularization technique that randomly masks out a square region of the input image by replacing it with zeros. This forces the model to learn more robust features by not relying on any single spatial region.

Algorithm:

1. Select a random top-left corner (x, y) within valid bounds.

2. Define a square mask of dimensions mask_size x mask_size.

3. Set all pixel values inside the mask region to 0.

Example:

Input: image = [[1, 2, 3],

[4, 5, 6],

[7, 8, 9]], size = 2

Output (one possible result): [[1, 2, 3],

[4, 0, 0],

[7, 0, 0]]

The output shows a 2x2 region starting at a randomly chosen position replaced with zeros. The remaining pixels are left unchanged. Because the position is random, different runs may mask different regions of the image.

Constraints:

  • The input image is a 2D NumPy array of shape (H, W).
  • `size` is a positive integer such that `size <= min(H, W)`.
  • The masked region must be filled with zeros.
  • Use NumPy for all array operations.
  • Test Cases

    Test Case 1
    Input: [[1,2,3],[4,5,6],[7,8,9]]
    Expected: [[1,2,3],[4,0,0],[7,0,0]]
    Test Case 2
    Input: [[1,2,3],[4,5,6],[7,8,9]]
    Expected: [[1,2,3],[0,0,6],[0,0,9]]
    + 3 hidden test cases