| import warnings |
| from dataclasses import dataclass, asdict |
| from typing import Any, Dict, Optional, Sequence, Tuple, Union |
|
|
| import torch |
| import torch.nn as nn |
| import torchvision.transforms.functional as F |
|
|
| from torchvision.transforms import Normalize, Compose, RandomResizedCrop, InterpolationMode, ToTensor, Resize, \ |
| CenterCrop |
| from torchvision import transforms |
| from .constants import OPENAI_DATASET_MEAN, OPENAI_DATASET_STD |
|
|
| @dataclass |
| class AugmentationCfg: |
| scale: Tuple[float, float] = (0.9, 1.0) |
| ratio: Optional[Tuple[float, float]] = None |
| color_jitter: Optional[Union[float, Tuple[float, float, float]]] = None |
| interpolation: Optional[str] = None |
| re_prob: Optional[float] = None |
| re_count: Optional[int] = None |
| use_timm: bool = False |
|
|
|
|
| class ResizeMaxSize(nn.Module): |
|
|
| def __init__(self, max_size, interpolation=InterpolationMode.BICUBIC, fn='max', fill=0): |
| super().__init__() |
| if not isinstance(max_size, int): |
| raise TypeError(f"Size should be int. Got {type(max_size)}") |
| self.max_size = max_size |
| self.interpolation = interpolation |
| self.fn = min if fn == 'min' else min |
| self.fill = fill |
|
|
| def forward(self, img): |
| if isinstance(img, torch.Tensor): |
| height, width = img.shape[:2] |
| else: |
| width, height = img.size |
| scale = self.max_size / float(max(height, width)) |
| new_size = tuple(round(dim * scale) for dim in (height, width)) |
| img = F.resize(img, new_size, self.interpolation) |
| pad_h = self.max_size - new_size[0] |
| pad_w = self.max_size - new_size[1] |
| img = F.pad(img, padding=[pad_w // 2, pad_h // 2, pad_w - pad_w // 2, pad_h - pad_h // 2], fill=self.fill) |
|
|
| return img |
|
|
|
|
| def _convert_to_rgb(image): |
| return image.convert('RGB') |
|
|
|
|
| def image_transform( |
| image_size: int, |
| is_train: bool, |
| mean: Optional[Tuple[float, ...]] = None, |
| std: Optional[Tuple[float, ...]] = None, |
| resize_longest_max: bool = False, |
| fill_color: int = 0, |
| aug_cfg: Optional[Union[Dict[str, Any], AugmentationCfg]] = None, |
| ): |
| mean = mean or OPENAI_DATASET_MEAN |
| if not isinstance(mean, (list, tuple)): |
| mean = (mean,) * 3 |
|
|
| std = std or OPENAI_DATASET_STD |
| if not isinstance(std, (list, tuple)): |
| std = (std,) * 3 |
|
|
| if isinstance(image_size, (list, tuple)) and image_size[0] == image_size[1]: |
| |
| image_size = image_size[0] |
|
|
| if isinstance(aug_cfg, dict): |
| aug_cfg = AugmentationCfg(**aug_cfg) |
| else: |
| aug_cfg = aug_cfg or AugmentationCfg() |
| normalize = Normalize(mean=mean, std=std) |
| if is_train: |
| aug_cfg_dict = {k: v for k, v in asdict(aug_cfg).items() if v is not None} |
| use_timm = aug_cfg_dict.pop('use_timm', False) |
| if use_timm: |
| from timm.data import create_transform |
| if isinstance(image_size, (tuple, list)): |
| assert len(image_size) >= 2 |
| input_size = (3,) + image_size[-2:] |
| else: |
| input_size = (3, image_size, image_size) |
| |
| aug_cfg_dict.setdefault('interpolation', 'random') |
| aug_cfg_dict.setdefault('color_jitter', None) |
| train_transform = create_transform( |
| input_size=input_size, |
| is_training=True, |
| hflip=0., |
| mean=mean, |
| std=std, |
| re_mode='pixel', |
| **aug_cfg_dict, |
| ) |
| else: |
| train_transform = Compose([ |
| RandomResizedCrop( |
| image_size, |
| scale=aug_cfg_dict.pop('scale'), |
| interpolation=InterpolationMode.BICUBIC, |
| ), |
| _convert_to_rgb, |
| ToTensor(), |
| normalize, |
| ]) |
| if aug_cfg_dict: |
| warnings.warn(f'Unused augmentation cfg items, specify `use_timm` to use ({list(aug_cfg_dict.keys())}).') |
| return train_transform |
| else: |
| if resize_longest_max: |
| transforms = [ |
| ResizeMaxSize(image_size, fill=fill_color) |
| ] |
| else: |
| transforms = [ |
| Resize(image_size, interpolation=InterpolationMode.BICUBIC), |
| CenterCrop(image_size), |
| ] |
| transforms.extend([ |
| _convert_to_rgb, |
| ToTensor(), |
| normalize, |
| ]) |
| return Compose(transforms) |
|
|
|
|
| def det_image_transform( |
| image_size: int, |
| is_train: bool, |
| mean: Optional[Tuple[float, ...]] = None, |
| std: Optional[Tuple[float, ...]] = None, |
| fill_color: int = 0, |
| aug_cfg: Optional[Union[Dict[str, Any], AugmentationCfg]] = None, |
| ): |
| mean = mean or OPENAI_DATASET_MEAN |
| if not isinstance(mean, (list, tuple)): |
| mean = (mean,) * 3 |
|
|
| std = std or OPENAI_DATASET_STD |
| if not isinstance(std, (list, tuple)): |
| std = (std,) * 3 |
|
|
| if isinstance(image_size, (list, tuple)) and image_size[0] == image_size[1]: |
| |
| image_size = image_size[0] |
|
|
| normalize = Normalize(mean=mean, std=std) |
| if is_train: |
| raise NotImplementedError |
| else: |
| transforms = [ |
| ResizeLongest(image_size, fill=fill_color), |
| _convert_to_rgb, |
| ToTensor(), |
| normalize, |
| ] |
| return Compose(transforms) |
|
|
|
|
| class ResizeLongest(nn.Module): |
| def __init__(self, max_size, interpolation=InterpolationMode.BICUBIC, fill=0): |
| super().__init__() |
| if not isinstance(max_size, int): |
| raise TypeError(f"Size should be int. Got {type(max_size)}") |
| self.max_size = max_size |
| self.interpolation = interpolation |
| self.fill = fill |
|
|
| def forward(self, img): |
| if isinstance(img, torch.Tensor): |
| height, width = img.shape[1:] |
| else: |
| width, height = img.size |
| scale = self.max_size / float(max(height, width)) |
| new_height, new_width = round(height * scale), round(width * scale) |
|
|
| img = F.resize(img, [new_height, new_width], self.interpolation, antialias=None) |
| pad_h = self.max_size - new_height |
| pad_w = self.max_size - new_width |
| img = F.pad(img, padding=[0, 0, pad_w, pad_h], fill=self.fill) |
|
|
| return img |
|
|
|
|
| def get_scale(img, new_image): |
| if isinstance(img, torch.Tensor): |
| height, width = new_image.shape[-2:] |
| else: |
| width, height = img.size |
|
|
| if isinstance(new_image, torch.Tensor): |
| new_height, new_width = new_image.shape[-2:] |
| else: |
| new_width, new_height = new_image.size |
|
|
| scale = min(new_height/height, new_width/width) |
|
|
| return scale |
|
|
|
|
|
|
| class MultiViewAugmentation(object): |
| def __init__(self, |
| image_size: int, |
| mean: Optional[Tuple[float, ...]] = None, |
| std: Optional[Tuple[float, ...]] = None, |
| resize_longest_max: bool = False, |
| fill_color: int = 0, |
| global_crops_scale=(0.32,1.0), |
| ): |
| |
| normalize = Normalize(mean=mean, std=std) |
| if resize_longest_max: |
| self.vanilla_transfo = [ResizeMaxSize(image_size, fill=fill_color)] |
| else: |
| self.vanilla_transfo = [ |
| Resize(image_size, interpolation=InterpolationMode.BICUBIC), |
| CenterCrop(image_size), |
| ] |
| self.vanilla_transfo.extend([ |
| _convert_to_rgb, |
| ToTensor(), |
| normalize, |
| ]) |
| self.vanilla_transfo=Compose(self.vanilla_transfo) |
|
|
| self.geometric_augmentation_global = transforms.Compose( |
| [ |
| transforms.RandomResizedCrop( |
| image_size, scale=global_crops_scale, interpolation=transforms.InterpolationMode.BICUBIC |
| ), |
| transforms.RandomHorizontalFlip(p=0.5), |
| ] |
| ) |
| color_jittering = transforms.Compose( |
| [ |
| transforms.RandomApply( |
| [transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1)], |
| p=0.8, |
| ), |
| transforms.RandomGrayscale(p=0.2), |
| ] |
| ) |
|
|
| global_transfo_extra = transforms.Compose( |
| [ |
| GaussianBlur(p=0.1), |
| transforms.RandomSolarize(threshold=128, p=0.2), |
| ] |
| ) |
| self.global_transfo = transforms.Compose([self.geometric_augmentation_global, color_jittering, global_transfo_extra,_convert_to_rgb,ToTensor(),normalize]) |
|
|
| def __call__(self, image): |
| global_view=self.global_transfo(image) |
| vanilla_view = self.vanilla_transfo(image) |
| return vanilla_view, global_view |
| |
|
|
|
|
| class GaussianBlur(transforms.RandomApply): |
| """ |
| Apply Gaussian Blur to the PIL image. |
| """ |
|
|
| def __init__(self, *, p: float = 0.5, radius_min: float = 0.1, radius_max: float = 2.0): |
| |
| keep_p = 1 - p |
| transform = transforms.GaussianBlur(kernel_size=9, sigma=(radius_min, radius_max)) |
| super().__init__(transforms=[transform], p=keep_p) |
|
|
|
|