Spaces:
Running
Running
File size: 3,690 Bytes
e99a83c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | """
augmentations.py
Simple camera-style augmentations for color fundus photography (CFP)
classification.
Expected input:
RGB NumPy image, shape (H, W, 3)
Dependencies:
pip install albumentations opencv-python
"""
import albumentations as A
from albumentations.pytorch import ToTensorV2
IMAGENET_MEAN = (0.485, 0.456, 0.406)
IMAGENET_STD = (0.229, 0.224, 0.225)
def get_train_transforms(
image_size=1024,
mean=IMAGENET_MEAN,
std=IMAGENET_STD,
):
"""
Training transforms.
"""
return A.Compose([
A.Resize(image_size, image_size),
A.HorizontalFlip(p=0.5),
A.ShiftScaleRotate(
shift_limit=0.02,
scale_limit=0.05,
rotate_limit=7,
border_mode=0,
value=0,
p=0.3,
),
A.RandomBrightnessContrast(
brightness_limit=0.15,
contrast_limit=0.15,
p=0.5,
),
A.RandomGamma(
gamma_limit=(85, 115),
p=0.3,
),
A.HueSaturationValue(
hue_shift_limit=3,
sat_shift_limit=10,
val_shift_limit=10,
p=0.25,
),
A.OneOf([
A.GaussianBlur(blur_limit=(3, 5)),
A.Downscale(scale_min=0.80, scale_max=0.95),
A.ImageCompression(quality_lower=75, quality_upper=100),
], p=0.2),
A.Normalize(mean=mean, std=std),
ToTensorV2(),
])
def get_val_transforms(
image_size=1024,
mean=IMAGENET_MEAN,
std=IMAGENET_STD,
):
"""
Validation/test transforms.
"""
return A.Compose([
A.Resize(image_size, image_size),
A.Normalize(mean=mean, std=std),
ToTensorV2(),
])
# -------------------------------------------------------------------------
# Suggested CFP augmentation parameter sets
# -------------------------------------------------------------------------
#
# 1) DEFAULT / CONSERVATIVE
# Use this as a general starting point for CFP classification tasks.
#
# Rationale:
# - Simulates common camera/acquisition variability.
# - Keeps color and image-quality perturbations mild.
# - Good first choice when the disease signal may depend on subtle color,
# contrast, texture, or anatomical context.
#
# brightness_limit = 0.15
# contrast_limit = 0.15
# gamma_limit = (85, 115) # approximately gamma 0.85–1.15
# hue_shift_limit = 3 # intentionally small for fundus color realism
# sat_shift_limit = 10
# val_shift_limit = 10
# rotate_limit = 7
# shift_limit = 0.02
# scale_limit = 0.05
# blur_limit = (3, 5)
# downscale_range = (0.80, 0.95)
# jpeg_quality = (75, 100)
#
#
# 2) MORE AGGRESSIVE / DOMAIN-ROBUSTNESS
# Use this when robustness across different CFP cameras, sites, image qualities,
# or acquisition pipelines is more important, and confirm using external or
# camera/site-held-out validation.
#
# Rationale:
# - Simulates broader variation across CFP devices and acquisition conditions.
# - May improve domain robustness.
# - Higher risk of altering disease-relevant appearance, so it should be
# validated carefully for the target task.
#
# brightness_limit = 0.25
# contrast_limit = 0.25
# gamma_limit = (75, 130) # approximately gamma 0.75–1.30
# hue_shift_limit = 5 # still limited for fundus color realism
# sat_shift_limit = 18
# val_shift_limit = 18
# rotate_limit = 12
# shift_limit = 0.04
# scale_limit = 0.10
# blur_limit = (3, 7)
# downscale_range = (0.65, 0.95)
# jpeg_quality = (55, 100)
# ------------------------------------------------------------------------- |