File size: 5,476 Bytes
c99d198 | 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 |
"""API factory."""
import functools
import os.path as osp
import albumentations as alb
import torch
from xirl import models
from xirl import transforms
TRANSFORMS = {
"random_resized_crop":
functools.partial(
alb.RandomResizedCrop, scale=(0.8, 1.0), ratio=(0.75, 1.333),
p=1.0),
"center_crop":
functools.partial(alb.CenterCrop, p=1.0),
"global_resize":
functools.partial(alb.Resize, p=1.0),
"grayscale":
functools.partial(alb.ToGray, p=0.2),
"vertical_flip":
functools.partial(alb.VerticalFlip, p=0.5),
"horizontal_flip":
functools.partial(alb.HorizontalFlip, p=0.5),
"gaussian_blur":
functools.partial(
alb.GaussianBlur,
blur_limit=(13, 13),
sigma_limit=(1.0, 2.0),
p=0.2,
),
"color_jitter":
functools.partial(
alb.ColorJitter,
brightness=0.4,
contrast=0.4,
hue=0.1,
saturation=0.1,
p=0.8,
),
"rotate":
functools.partial(alb.Rotate, limit=(-5, 5), border_mode=0, p=0.5),
"normalize":
functools.partial(
alb.Normalize,
mean=transforms.PretrainedMeans.IMAGENET,
std=transforms.PretrainedStds.IMAGENET,
p=1.0,
),
}
MODELS = {
"resnet18_linear": models.Resnet18LinearEncoderNet,
"resnet18_clip_linear": models.Resnet18LinearEncoderAndTextEncoderNet,
"resnet18_classifier": models.GoalClassifier,
"resnet18_features": models.Resnet18RawImageNetFeaturesNet,
"resnet18_linear_ae": models.Resnet18LinearEncoderAutoEncoderNet,
}
def model_from_config(config):
"""Create a model from a config."""
kwargs = {
"num_ctx_frames": config.frame_sampler.num_context_frames,
"normalize_embeddings": config.model.normalize_embeddings,
"learnable_temp": config.model.learnable_temp,
}
if config.model.model_type == "resnet18_linear":
kwargs["embedding_size"] = config.model.embedding_size
elif config.model.model_type == "resnet18_clip_linear":
kwargs["embedding_size"] = config.model.embedding_size
elif config.model.model_type == "resnet18_linear_ae":
kwargs["embedding_size"] = config.model.embedding_size
return MODELS[config.model.model_type](**kwargs)
def create_transform(name, *args, **kwargs):
"""Create an image augmentation from its name and args."""
# pylint: disable=invalid-name
if "::" in name:
# e.g., `rotate::{'limit': (-45, 45)}`
name, __kwargs = name.split("::")
_kwargs = eval(__kwargs) # pylint: disable=eval-used
else:
_kwargs = {}
_kwargs.update(kwargs)
return TRANSFORMS[name](*args, **_kwargs)
def dataset_from_config(config, downstream, split, debug):
"""Create a video dataset from a config."""
dataset_path = osp.join(config.data.root, split)
image_size = config.data_augmentation.image_size
if isinstance(image_size, int):
image_size = (image_size, image_size)
image_size = tuple(image_size)
# Note(kevin): We used to disable data augmentation on all downstream
# dataloaders. I've decided to keep them for train downstream loaders.
if debug:
# The minimum data augmentation we want to keep is resizing when
# debugging.
aug_names = ["global_resize"]
else:
if split == "train":
aug_names = config.data_augmentation.train_transforms
else:
aug_names = config.data_augmentation.eval_transforms
# Create a list of data augmentation callables.
aug_funcs = []
for name in aug_names:
if "resize" in name or "crop" in name:
aug_funcs.append(create_transform(name, *image_size))
else:
aug_funcs.append(create_transform(name))
augmentor = transforms.VideoAugmentor({SequenceType.FRAMES: aug_funcs})
# Restrict action classes if they have been provided. Else, load all
# from the data directory.
c_action_class = (
config.data.downstream_action_class
if downstream else config.data.pretrain_action_class
)
if c_action_class:
action_classes = c_action_class
else:
action_classes = get_subdirs(
dataset_path,
basename=True,
nonempty=True,
sort_lexicographical=True,
)
# We need to separate out the dataclasses for each action class when
# creating downstream datasets.
if downstream:
dataset = {}
for action_class in action_classes:
frame_sampler = frame_sampler_from_config(config, downstream=True)
single_class_dataset = VideoDataset(
dataset_path,
frame_sampler,
seed=config.seed,
augmentor=augmentor,
max_vids_per_class=config.data.max_vids_per_class,
)
single_class_dataset.restrict_subdirs(action_class)
dataset[action_class] = single_class_dataset
else:
frame_sampler = frame_sampler_from_config(config, downstream=False)
dataset = VideoDataset(
dataset_path,
frame_sampler,
seed=config.seed,
augmentor=augmentor,
max_vids_per_class=config.data.max_vids_per_class,
)
dataset.restrict_subdirs(action_classes)
return dataset
|