| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| """Transformations for video data.""" |
|
|
| import enum |
| import warnings |
|
|
| import albumentations as alb |
| import numpy as np |
| import torch |
| from xirl.types import SequenceType |
|
|
|
|
| @enum.unique |
| class PretrainedMeans(enum.Enum): |
| """Pretrained mean normalization values.""" |
|
|
| IMAGENET = (0.485, 0.456, 0.406) |
|
|
|
|
| @enum.unique |
| class PretrainedStds(enum.Enum): |
| """Pretrained std deviation normalization values.""" |
|
|
| IMAGENET = (0.229, 0.224, 0.225) |
|
|
|
|
| class UnNormalize: |
| """Unnormalize a batch of images that have been normalized. |
| |
| Speficially, re-multiply by the standard deviation and shift by the mean. |
| """ |
|
|
| def __init__( |
| self, |
| mean, |
| std, |
| ): |
| """Constructor. |
| |
| Args: |
| mean: The color channel means. |
| std: The color channel standard deviation. |
| """ |
| if np.asarray(mean).shape: |
| self.mean = torch.tensor(mean)[Ellipsis, :, None, None] |
| if np.asarray(std).shape: |
| self.std = torch.tensor(std)[Ellipsis, :, None, None] |
|
|
| def __call__(self, tensor): |
| return (tensor * self.std) + self.mean |
|
|
|
|
| def augment_video( |
| frames, |
| pipeline, |
| ): |
| """Apply the same augmentation pipeline to all frames in a video. |
| |
| Args: |
| frames: A numpy array of shape (T, H, W, 3), where T is the number of frames |
| in the video. |
| pipeline (list): A list containing albumentation augmentations. |
| |
| Returns: |
| The augmented frames of shape (T, H, W, 3). |
| |
| Raises: |
| ValueError: If the input video doesn't have the correct shape. |
| """ |
| if frames.ndim != 4: |
| raise ValueError("Input video must be a 4D sequence of frames.") |
|
|
| transform = alb.ReplayCompose(pipeline, p=1.0) |
|
|
| |
| |
| |
| |
| with warnings.catch_warnings(): |
| |
| warnings.simplefilter("ignore") |
|
|
| replay, frames_aug = None, [] |
| for frame in frames: |
| if replay is None: |
| aug = transform(image=frame) |
| replay = aug.pop("replay") |
| else: |
| aug = transform.replay(replay, image=frame) |
| frames_aug.append(aug["image"]) |
|
|
| return np.stack(frames_aug, axis=0) |
|
|
|
|
| class VideoAugmentor: |
| """Data augmentation for videos. |
| |
| Augmentor consistently augments data across the time dimension (i.e. dim 0). |
| In other words, the same transformation is applied to every single frame in |
| a video sequence. |
| |
| Currently, only image frames, i.e. SequenceType.FRAMES in a video can be |
| augmented. |
| """ |
|
|
| MAP = { |
| SequenceType.FRAMES: augment_video, |
| } |
|
|
| def __init__( |
| self, |
| params, |
| ): |
| """Constructor. |
| |
| Args: |
| params: |
| |
| Raises: |
| ValueError: If params contains an unsupported data augmentation. |
| """ |
| for key in params.keys(): |
| if key not in SequenceType: |
| raise ValueError(f"{key} is not a supported SequenceType.") |
| self._params = params |
|
|
| def __call__( |
| self, |
| data, |
| ): |
| """Iterate and transform the data values. |
| |
| Currently, data augmentation is only applied to video frames, i.e. the |
| value of the data dict associated with the SequenceType.IMAGE key. |
| |
| Args: |
| data: A dict mapping from sequence type to sequence value. |
| |
| Returns: |
| A an augmented dict. |
| """ |
| for key, transforms in self._params.items(): |
| data[key] = VideoAugmentor.MAP[key](data[key], transforms) |
| return data |
|
|