0
0
Computer Visionml~10 mins

Cutout and CutMix in Computer Vision - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to apply Cutout augmentation by masking a square region in the image tensor.

Computer Vision
def cutout(image, size):
    h, w = image.shape[1], image.shape[2]
    y = torch.randint(0, h, (1,)).item()
    x = torch.randint(0, w, (1,)).item()
    y1 = max(0, y - size // 2)
    y2 = min(h, y + size // 2)
    x1 = max(0, x - size // 2)
    x2 = min(w, x + size // 2)
    image[:, y1:y2, x1:x2] = [1]
    return image
Drag options to blanks, or click blank then click option'
Aimage.mean()
B1
C0
Dtorch.rand(1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using random values instead of zero for masking.
Setting the region to one which brightens the area.
2fill in blank
medium

Complete the code to mix two images using CutMix by combining patches and labels with a lambda factor.

Computer Vision
def cutmix(image1, image2, label1, label2, alpha=1.0):
    lam = np.random.beta(alpha, alpha)
    h, w = image1.shape[1], image1.shape[2]
    cut_rat = np.sqrt(1. - lam)
    cut_w = int(w * cut_rat)
    cut_h = int(h * cut_rat)
    cx = np.random.randint(w)
    cy = np.random.randint(h)
    x1 = np.clip(cx - cut_w // 2, 0, w)
    y1 = np.clip(cy - cut_h // 2, 0, h)
    x2 = np.clip(cx + cut_w // 2, 0, w)
    y2 = np.clip(cy + cut_h // 2, 0, h)
    image1[:, y1:y2, x1:x2] = image2[:, y1:y2, x1:x2]
    lam = 1 - ((x2 - x1) * (y2 - y1) / (w * h))
    mixed_label = lam * label1 + [1] * label2
    return image1, mixed_label
Drag options to blanks, or click blank then click option'
A1 - lam
Blam
Clabel1
D0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Using lam instead of 1 - lam for label2.
Using fixed values like 0.5 instead of calculated proportions.
3fill in blank
hard

Fix the error in the Cutout function where the masked region is incorrectly assigned.

Computer Vision
def cutout_fixed(image, size):
    h, w = image.shape[1], image.shape[2]
    y = torch.randint(0, h, (1,)).item()
    x = torch.randint(0, w, (1,)).item()
    y1 = max(0, y - size // 2)
    y2 = min(h, y + size // 2)
    x1 = max(0, x - size // 2)
    x2 = min(w, x + size // 2)
    image[:, y1:y2, x1:x2] = [1]
    return image
Drag options to blanks, or click blank then click option'
Atorch.ones_like(image[:, y1:y2, x1:x2])
Btorch.zeros_like(image[:, y1:y2, x1:x2])
Cimage[:, y1:y2, x1:x2]
Dtorch.rand_like(image[:, y1:y2, x1:x2])
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning scalar zero causes broadcasting errors.
Using ones or random values changes the intended mask effect.
4fill in blank
hard

Fill both blanks to create a CutMix bounding box and calculate the lambda ratio correctly.

Computer Vision
def rand_bbox(size, lam):
    W = size[2]
    H = size[1]
    cut_rat = np.sqrt(1. - lam)
    cut_w = int(W * [1])
    cut_h = int(H * [2])
    cx = np.random.randint(W)
    cy = np.random.randint(H)
    bbx1 = np.clip(cx - cut_w // 2, 0, W)
    bby1 = np.clip(cy - cut_h // 2, 0, H)
    bbx2 = np.clip(cx + cut_w // 2, 0, W)
    bby2 = np.clip(cy + cut_h // 2, 0, H)
    return bbx1, bby1, bbx2, bby2
Drag options to blanks, or click blank then click option'
Acut_rat
Blam
C1 - lam
Dnp.sqrt(lam)
Attempts:
3 left
💡 Hint
Common Mistakes
Using lam or 1 - lam directly instead of sqrt(1 - lam).
Using different factors for width and height.
5fill in blank
hard

Fill all three blanks to implement CutMix that returns mixed images and adjusted labels.

Computer Vision
def cutmix_data(x, y, alpha=1.0):
    if alpha > 0:
        lam = np.random.beta(alpha, alpha)
    else:
        lam = 1
    batch_size = x.size()[0]
    index = torch.randperm(batch_size)
    bbx1, bby1, bbx2, bby2 = rand_bbox(x.size(), lam)
    x[:, :, bby1:bby2, bbx1:bbx2] = x[index, :, bby1:bby2, bbx1:bbx2]
    lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (x.size()[-1] * x.size()[-2]))
    y_a, y_b = y, y[index]
    return x, (y_a, y_b, [1]), lam, [2], [3]
Drag options to blanks, or click blank then click option'
Alam
By_a
Cy_b
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Returning labels in wrong order.
Returning index instead of labels.
Not returning lam for label weighting.