File size: 13,410 Bytes
18a82fb | 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | from torchvision import transforms
import torch
from typing import Tuple, Dict, Optional
import numpy as np
# Import augmentation configs from model configs
from ..models.model_configs import AUGMENTATION_CONFIGS, TRAINING_CONFIGS
def get_train_transforms(
input_size: int = 224,
augmentation_level: str = 'medium'
) -> transforms.Compose:
"""
Get training data augmentation pipeline
Args:
input_size: Target image size
augmentation_level: Augmentation strength ('light', 'medium', 'heavy')
Returns:
Composed transform pipeline
"""
# Get augmentation parameters
aug_params = AUGMENTATION_CONFIGS.get(augmentation_level, AUGMENTATION_CONFIGS['medium'])
transform_list = [
# Resize with some randomness
transforms.RandomResizedCrop(
input_size,
scale=aug_params['scale'],
ratio=(0.9, 1.1), # Aspect ratio variation
),
# Horizontal flip (makes sense for products)
transforms.RandomHorizontalFlip(p=aug_params['h_flip_p']),
# Rotation
transforms.RandomRotation(degrees=aug_params['rotation']),
# Color augmentation
transforms.ColorJitter(
brightness=aug_params['brightness'],
contrast=aug_params['contrast'],
saturation=aug_params['saturation'],
hue=aug_params['hue']
),
# Perspective transformation
transforms.RandomPerspective(
distortion_scale=aug_params['perspective'],
p=0.3
),
# Convert to tensor and normalize
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
]
return transforms.Compose(transform_list)
def get_val_transforms(input_size: int = 224) -> transforms.Compose:
"""
Get validation/test transforms (no augmentation)
Args:
input_size: Target image size
Returns:
Composed transform pipeline
"""
return transforms.Compose([
# Center crop after resize
transforms.Resize(int(input_size * 1.14)), # Resize to slightly larger
transforms.CenterCrop(input_size), # Then center crop
# Convert to tensor and normalize
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
def get_inference_transforms(input_size: int = 224) -> transforms.Compose:
"""
Get inference transforms (same as validation)
"""
return get_val_transforms(input_size)
def get_transforms_for_model(
model_name: str,
is_training: bool = True,
augmentation_level: str = 'medium'
) -> transforms.Compose:
"""
Get appropriate transforms for a specific model
Args:
model_name: Name of the model
is_training: Whether to include augmentations
augmentation_level: Strength of augmentations for training
Returns:
Transform pipeline
"""
# Get model-specific input size from configs
config = TRAINING_CONFIGS.get(model_name, TRAINING_CONFIGS['resnet50'])
input_size = config['input_size']
if is_training:
return get_train_transforms(input_size, augmentation_level)
else:
return get_val_transforms(input_size)
class MixUpTransform:
"""
MixUp augmentation for training
Reference: https://arxiv.org/abs/1710.09412
"""
def __init__(self, alpha: float = 1.0, num_classes: int = 5):
self.alpha = alpha
self.num_classes = num_classes
def __call__(
self,
images: torch.Tensor,
labels: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, float]:
"""
Apply MixUp to a batch
Args:
images: Batch of images (B, C, H, W)
labels: Batch of labels (B,)
Returns:
mixed_images, labels_a, labels_b, lam
"""
batch_size = images.size(0)
# Sample lambda from Beta distribution
if self.alpha > 0:
lam = np.random.beta(self.alpha, self.alpha)
else:
lam = 1
# Random shuffle for mixing
index = torch.randperm(batch_size).to(images.device)
# Mix images
mixed_images = lam * images + (1 - lam) * images[index]
# Return mixed images and both label sets
labels_a, labels_b = labels, labels[index]
return mixed_images, labels_a, labels_b, lam
class CutMixTransform:
"""
CutMix augmentation for training
Reference: https://arxiv.org/abs/1905.04899
"""
def __init__(self, alpha: float = 1.0, num_classes: int = 5):
self.alpha = alpha
self.num_classes = num_classes
def __call__(
self,
images: torch.Tensor,
labels: torch.Tensor
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, float]:
"""
Apply CutMix to a batch
"""
batch_size, _, height, width = images.size()
# Sample lambda
if self.alpha > 0:
lam = np.random.beta(self.alpha, self.alpha)
else:
lam = 1
# Random shuffle for mixing
index = torch.randperm(batch_size).to(images.device)
# Create random box
cut_ratio = np.sqrt(1 - lam)
cut_h = int(height * cut_ratio)
cut_w = int(width * cut_ratio)
# Random center point
cx = np.random.randint(width)
cy = np.random.randint(height)
# Box boundaries
x1 = max(0, cx - cut_w // 2)
x2 = min(width, cx + cut_w // 2)
y1 = max(0, cy - cut_h // 2)
y2 = min(height, cy + cut_h // 2)
# Apply CutMix
mixed_images = images.clone()
mixed_images[:, :, y1:y2, x1:x2] = images[index, :, y1:y2, x1:x2]
# Adjust lambda based on actual box size
lam = 1 - ((x2 - x1) * (y2 - y1) / (width * height))
labels_a, labels_b = labels, labels[index]
return mixed_images, labels_a, labels_b, lam
class RandAugmentTransform:
"""
RandAugment for automatic augmentation policy
Simplified version for product images
"""
def __init__(self, n: int = 2, m: int = 10):
"""
Args:
n: Number of augmentation transformations to apply
m: Magnitude of transformations
"""
self.n = n
self.m = m
# Define augmentation pool suitable for product images
self.augmentations = [
lambda img, mag: transforms.functional.rotate(img, mag * 3),
lambda img, mag: transforms.functional.adjust_brightness(img, 1 + mag * 0.05),
lambda img, mag: transforms.functional.adjust_contrast(img, 1 + mag * 0.05),
lambda img, mag: transforms.functional.adjust_saturation(img, 1 + mag * 0.05),
lambda img, mag: transforms.functional.adjust_sharpness(img, 1 + mag * 0.1),
]
def __call__(self, img):
"""Apply RandAugment to an image"""
# Randomly select n augmentations
selected_augs = np.random.choice(self.augmentations, self.n, replace=False)
for aug in selected_augs:
img = aug(img, self.m)
return img
def get_advanced_train_transforms(
input_size: int = 224,
use_randaugment: bool = False,
randaugment_n: int = 2,
randaugment_m: int = 10
) -> transforms.Compose:
"""
Get advanced training transforms with optional RandAugment
Args:
input_size: Target image size
use_randaugment: Whether to use RandAugment
randaugment_n: Number of augmentations
randaugment_m: Magnitude of augmentations
Returns:
Transform pipeline
"""
transform_list = [
transforms.RandomResizedCrop(input_size, scale=(0.7, 1.0)),
]
if use_randaugment:
transform_list.append(RandAugmentTransform(n=randaugment_n, m=randaugment_m))
transform_list.extend([
transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
return transforms.Compose(transform_list)
# Denormalization for visualization
class DeNormalize:
"""Denormalize tensor for visualization"""
def __init__(self, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):
self.mean = torch.tensor(mean).view(3, 1, 1)
self.std = torch.tensor(std).view(3, 1, 1)
def __call__(self, tensor):
"""
Args:
tensor: Normalized image tensor
Returns:
Denormalized tensor
"""
return tensor * self.std + self.mean
def test_augmentations(
image_path: str,
model_name: str = 'efficientnet-b2',
num_samples: int = 8
):
"""
Test and visualize augmentations
Args:
image_path: Path to test image
model_name: Model name for transforms
num_samples: Number of augmented samples to generate
"""
from PIL import Image
import matplotlib.pyplot as plt
# Load image
img = Image.open(image_path).convert('RGB')
# Get transforms
transform = get_transforms_for_model(model_name, is_training=True)
# Generate augmented samples
fig, axes = plt.subplots(2, 4, figsize=(16, 8))
axes = axes.flatten()
for i in range(num_samples):
augmented = transform(img)
# Denormalize for visualization
denorm = DeNormalize()
augmented = denorm(augmented)
augmented = torch.clamp(augmented, 0, 1)
# Convert to numpy
augmented = augmented.permute(1, 2, 0).numpy()
axes[i].imshow(augmented)
axes[i].axis('off')
axes[i].set_title(f'Augmented {i + 1}')
plt.suptitle(f'Augmentation samples for {model_name}')
plt.tight_layout()
plt.show()
#!/usr/bin/env python3
"""
Simple test script to quickly verify transforms.py functions
Add this to the bottom of transforms.py or run separately
"""
def quick_test_transforms():
"""Quick test of all major functions"""
import torch
import numpy as np
from PIL import Image
print("π§ͺ Quick Transforms Test")
print("-" * 40)
# Create test image
test_img = Image.fromarray(np.random.randint(0, 255, (300, 300, 3), dtype=np.uint8))
print(f"β Created test image: {test_img.size}")
# Test 1: Basic transforms
try:
train_transform = get_train_transforms(224, 'medium')
val_transform = get_val_transforms(224)
train_tensor = train_transform(test_img)
val_tensor = val_transform(test_img)
print(f"β Train transform: {train_tensor.shape}")
print(f"β Val transform: {val_tensor.shape}")
except Exception as e:
print(f"β Basic transforms failed: {e}")
return False
# Test 2: Model-specific transforms
try:
for model in ['resnet50', 'efficientnet-b2']:
transform = get_transforms_for_model(model, is_training=True)
tensor = transform(test_img)
print(f"β {model} transform: {tensor.shape}")
except Exception as e:
print(f"β Model-specific transforms failed: {e}")
return False
# Test 3: MixUp and CutMix
try:
batch_size = 4
images = torch.randn(batch_size, 3, 224, 224)
labels = torch.tensor([0, 1, 2, 3])
# MixUp
mixup = MixUpTransform(alpha=1.0, num_classes=5)
mixed_images, labels_a, labels_b, lam = mixup(images, labels)
print(f"β MixUp: lambda={lam:.3f}, shape={mixed_images.shape}")
# CutMix
cutmix = CutMixTransform(alpha=1.0, num_classes=5)
mixed_images, labels_a, labels_b, lam = cutmix(images, labels)
print(f"β CutMix: lambda={lam:.3f}, shape={mixed_images.shape}")
except Exception as e:
print(f"β Advanced augmentations failed: {e}")
return False
# Test 4: RandAugment
try:
randaug = RandAugmentTransform(n=2, m=10)
aug_img = randaug(test_img)
print(f"β RandAugment: {test_img.size} -> {aug_img.size}")
except Exception as e:
print(f"β RandAugment failed: {e}")
return False
# Test 5: Denormalization
try:
denorm = DeNormalize()
normalized = val_transform(test_img)
denormalized = denorm(normalized)
print(f"β Denormalize: {normalized.shape} -> {denormalized.shape}")
except Exception as e:
print(f"β Denormalization failed: {e}")
return False
print("-" * 40)
print("π All transforms working correctly!")
return True
# Add this to test when the module is run directly
if __name__ == "__main__":
# Quick test
success = quick_test_transforms()
if success:
print("\nβ
transforms.py is ready to use!")
else:
print("\nβ transforms.py has issues that need fixing")
# Optional: Visual test (uncomment if you want to see augmentations)
"""
try:
# This requires matplotlib
test_augmentations('path/to/test/image.jpg', 'efficientnet-b2', 8)
except:
print("Visual test skipped (requires matplotlib and test image)")
""" |