From 108abcd51bbf15b28bc83cbe8f5c6d0a8c8c62d2 Mon Sep 17 00:00:00 2001 From: dudeperf3ct Date: Wed, 2 Mar 2022 14:38:55 +0530 Subject: [PATCH 1/4] add lambda function to normalize --- flash/video/classification/input_transform.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/flash/video/classification/input_transform.py b/flash/video/classification/input_transform.py index a441a2abb9..da19522d61 100644 --- a/flash/video/classification/input_transform.py +++ b/flash/video/classification/input_transform.py @@ -25,10 +25,14 @@ if _PYTORCHVIDEO_AVAILABLE: from pytorchvideo.transforms import UniformTemporalSubsample - from torchvision.transforms import CenterCrop, Compose, RandomCrop + from torchvision.transforms import CenterCrop, Compose, RandomCrop, Lambda else: ClipSampler, LabeledVideoDataset, EncodedVideo, ApplyTransformToKey = None, None, None, None + +def lambda_tmp(x: torch.Tensor) -> torch.Tensor: + return (x/255.0) + @requires("video") @dataclass @@ -48,7 +52,7 @@ def per_sample_transform(self) -> Callable: per_sample_transform = [CenterCrop(self.image_size)] return ApplyToKeys( - "video", Compose([UniformTemporalSubsample(self.temporal_sub_sample)] + per_sample_transform) + "video", Compose([UniformTemporalSubsample(self.temporal_sub_sample), Lambda(lambda_tmp)] + per_sample_transform) ) def per_batch_transform_on_device(self) -> Callable: From 8a48419a48b5757c2d42e6b18991b0b64c177c04 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 2 Mar 2022 09:12:39 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- flash/video/classification/input_transform.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/flash/video/classification/input_transform.py b/flash/video/classification/input_transform.py index da19522d61..898b349ad1 100644 --- a/flash/video/classification/input_transform.py +++ b/flash/video/classification/input_transform.py @@ -25,13 +25,13 @@ if _PYTORCHVIDEO_AVAILABLE: from pytorchvideo.transforms import UniformTemporalSubsample - from torchvision.transforms import CenterCrop, Compose, RandomCrop, Lambda + from torchvision.transforms import CenterCrop, Compose, Lambda, RandomCrop else: ClipSampler, LabeledVideoDataset, EncodedVideo, ApplyTransformToKey = None, None, None, None - + def lambda_tmp(x: torch.Tensor) -> torch.Tensor: - return (x/255.0) + return x / 255.0 @requires("video") @@ -52,7 +52,8 @@ def per_sample_transform(self) -> Callable: per_sample_transform = [CenterCrop(self.image_size)] return ApplyToKeys( - "video", Compose([UniformTemporalSubsample(self.temporal_sub_sample), Lambda(lambda_tmp)] + per_sample_transform) + "video", + Compose([UniformTemporalSubsample(self.temporal_sub_sample), Lambda(lambda_tmp)] + per_sample_transform), ) def per_batch_transform_on_device(self) -> Callable: From 23f22ac0f4515b6356164c6a0eb7f1e86ff71936 Mon Sep 17 00:00:00 2001 From: dudeperf3ct Date: Wed, 2 Mar 2022 16:17:46 +0530 Subject: [PATCH 3/4] remove lambda layer and replace function name --- flash/video/classification/input_transform.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flash/video/classification/input_transform.py b/flash/video/classification/input_transform.py index 898b349ad1..5626be5e8e 100644 --- a/flash/video/classification/input_transform.py +++ b/flash/video/classification/input_transform.py @@ -25,12 +25,12 @@ if _PYTORCHVIDEO_AVAILABLE: from pytorchvideo.transforms import UniformTemporalSubsample - from torchvision.transforms import CenterCrop, Compose, Lambda, RandomCrop + from torchvision.transforms import CenterCrop, Compose, RandomCrop else: ClipSampler, LabeledVideoDataset, EncodedVideo, ApplyTransformToKey = None, None, None, None -def lambda_tmp(x: torch.Tensor) -> torch.Tensor: +def normalize(x: torch.Tensor) -> torch.Tensor: return x / 255.0 @@ -53,7 +53,7 @@ def per_sample_transform(self) -> Callable: return ApplyToKeys( "video", - Compose([UniformTemporalSubsample(self.temporal_sub_sample), Lambda(lambda_tmp)] + per_sample_transform), + Compose([UniformTemporalSubsample(self.temporal_sub_sample), normalize] + per_sample_transform), ) def per_batch_transform_on_device(self) -> Callable: From e02f35e5e7c9ba9c2496ecf99611dda83630f4de Mon Sep 17 00:00:00 2001 From: dudeperf3ct Date: Wed, 2 Mar 2022 16:19:23 +0530 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56123f3133..4eec5c862a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ### Fixed +- Fixed normalizing inputs to video classification ([#1213](https://github.com/PyTorchLightning/lightning-flash/pull/1213)) + - Fixed a bug where DDP would not work with Flash tasks ([#1182](https://github.com/PyTorchLightning/lightning-flash/pull/1182)) - Fixed DDP support for `VideoClassifier` ([#1189](https://github.com/PyTorchLightning/lightning-flash/pull/1189))