Microscope circular mask aug

From wenmin-wu/ds-skills · View interactive version

Microscope circular mask aug is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Mask the corners of a dermoscopy image with a random-radius black circle to mimic the dark vignette of a dermatoscope field of view

How to use this skill

Skill file

Overview

About half the dermoscopy images in ISIC datasets have the characteristic dark circular vignette of a dermatoscope; the other half are cropped rectangles. Models pick up the vignette as a shortcut feature correlated with source site, which breaks cross-site generalization. The fix is a symmetric augmentation: apply a random-radius circular mask to train images that don't have one, so the model learns to ignore the vignette. Cheap, interpretable, and directly closes a train/test distribution gap.

Quick Start

import cv2
import numpy as np
import random

class MicroscopeMask:
    def __init__(self, p=0.5):
        self.p = p

    def __call__(self, img):
        if random.random() > self.p:
            return img
        h, w = img.shape[:2]
        circle = (np.ones(img.shape) * 255).astype(np.uint8)
        radius = random.randint(h // 2 - 3, h // 2 + 15)
        circle = cv2.circle(circle, (w // 2, h // 2), radius, (0, 0, 0), -1)
        mask = circle - 255                      # 0 inside circle, -255 outside
        return np.multiply(img, mask)            # zeros outside the circle

Workflow

  1. Place the aug in the train transform pipeline before normalization
  2. Randomize the radius within a narrow band around img_size / 2 — keeps the lesion visible while varying the vignette
  3. Apply with p=0.5 since roughly half the images already have a vignette
  4. Validate by plotting a grid of augmented samples before kicking off training
  5. Track val AUC across sites — this aug specifically lifts underrepresented sites

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.