|
|
| """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.""" |
| |
| if "::" in name: |
| |
| name, __kwargs = name.split("::") |
| _kwargs = eval(__kwargs) |
| 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) |
|
|
| |
| |
| if debug: |
| |
| |
| aug_names = ["global_resize"] |
| else: |
| if split == "train": |
| aug_names = config.data_augmentation.train_transforms |
| else: |
| aug_names = config.data_augmentation.eval_transforms |
|
|
| |
| 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}) |
|
|
| |
| |
| 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, |
| ) |
|
|
| |
| |
| 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 |
|
|