BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Computer vision32 of 35 in topic

814. Mix two images with CutMix and adjust the labels

MediumComputer vision Pro

Why it matters CutMix is a standard augmentation in image classifier training recipes such as timm's.

CutMix pastes a rectangle from one training image onto another and mixes the labels by the share of pixels that came from each image.

Write cutmix(x1, x2, y1, y2, box). x1 and x2 are (H, W) arrays, y1 and y2 are one-hot label vectors, and box is (r0, c0, r1, c1) with rows r0..r1-1 and columns c0..c1-1. Clip the box to the image. Copy x2's pixels inside the box onto a copy of x1. lam = 1 - area / (H * W) using the clipped area. Return (x_mix, y_mix) with y_mix = lam * y1 + (1 - lam) * y2. Do not modify the inputs.

Inputs can be Python lists or NumPy arrays, so convert them with np.asarray first.

Functiondef cutmix(x1, x2, y1, y2, box)

Example 1

Input: cutmix([[0, 0], [0, 0]], [[1, 1], [1, 1]], [1, 0], [0, 1], (0, 0, 1, 2))

Output: ([[1.0, 1.0], [0.0, 0.0]], [0.5, 0.5])

Example 2

Input: x1 = [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]], x2 = [[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]], y1 = [1, 0, 0], y2 = [0, 0, 1], box = (1, 1, 3, 4)

Output: ([[0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 1.0, 1.0], [0.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]], [0.625, 0.0, 0.375])

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Clip r0, r1 to [0, H] and c0, c1 to [0, W] before anything else.

Hint 2

Use the clipped area for lam, not the requested box size.

Hint 3

Copy x1 first so the caller's array is not changed.

Common mistakes
  • Computing lam from the unclipped box.
  • Writing into x1 in place.
Understand the idea

The label weight follows the pixels: if a quarter of the image came from x2, the label is 75% y1 and 25% y2. Clipping matters because random boxes often hang off the edge.

Worked example

a 2x2 image with the top row replaced: half the pixels change, so the label is [0.5, 0.5].

lam = 1 - clipped area / (H W)
Topics
Computer vision
Similar questions
  • Max pooling forward and backwardMedium
  • Detect edges with Sobel filters and a magnitude thresholdMedium
  • Unfold image patches with im2colMedium
  • Resize an image with bilinear interpolationMedium
All Computer vision problems
Report
814 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def cutmix(x1, x2, y1, y2, box):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS