Shortcuts

Source code for flash.video.classification.input_transform

# Copyright The PyTorch Lightning team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from dataclasses import dataclass
from typing import Callable

import torch

from flash.core.data.io.input_transform import InputTransform
from flash.core.data.transforms import ApplyToKeys
from flash.core.utilities.imports import _KORNIA_AVAILABLE, _PYTORCHVIDEO_AVAILABLE, requires

if _KORNIA_AVAILABLE:
    import kornia.augmentation as K

if _PYTORCHVIDEO_AVAILABLE:
    from pytorchvideo.transforms import UniformTemporalSubsample
    from torchvision.transforms import CenterCrop, Compose, RandomCrop
else:
    ClipSampler, LabeledVideoDataset, EncodedVideo, ApplyTransformToKey = None, None, None, None


[docs]@requires("video") @dataclass class VideoClassificationInputTransform(InputTransform): image_size: int = 244 temporal_sub_sample: int = 8 mean: torch.Tensor = torch.tensor([0.45, 0.45, 0.45]) std: torch.Tensor = torch.tensor([0.225, 0.225, 0.225]) data_format: str = "BCTHW" same_on_frame: bool = False def per_sample_transform(self) -> Callable: if self.training: per_sample_transform = [RandomCrop(self.image_size, pad_if_needed=True)] else: per_sample_transform = [CenterCrop(self.image_size)] return ApplyToKeys( "video", Compose([UniformTemporalSubsample(self.temporal_sub_sample)] + per_sample_transform) ) def per_batch_transform_on_device(self) -> Callable: return ApplyToKeys( "video", K.VideoSequential( K.Normalize(self.mean, self.std), data_format=self.data_format, same_on_frame=self.same_on_frame, ), )

© Copyright 2020-2021, PyTorch Lightning. Revision 8e9123c7.

Built with Sphinx using a theme provided by Read the Docs.
Read the Docs v: 0.7.1
Versions
latest
stable
0.7.1
0.7.0
0.6.0
0.5.2
0.5.1
0.5.0
0.4.0
0.3.2
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0post1
docs-fix_typing
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.