This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
from_tensors
support for VideoClassification
#1389
Merged
ethanwharris
merged 48 commits into
master
from
video/feature/classification/from_tensors
Sep 1, 2022
Merged
Changes from 34 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
bef8e50
WIP: from_tensors support
krshrimali b29cdb2
remove unused func in tests
krshrimali 6d1a0be
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2f8bb7e
Remove doc, add LabeledVideoTensorDataset
krshrimali 9490e31
Fix merge conflict
krshrimali ce882a1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4031f5c
class for prediction
krshrimali 22f049d
Fixes for predictions
krshrimali 951ae93
Merge branch 'master' into video/feature/classification/from_tensors
krshrimali c36dbbe
minor... to fix the CI
krshrimali 23002f2
Merge branch 'video/feature/classification/from_tensors' of github.co…
krshrimali d210bd8
remove make_tensor, use randint (compatible with older pytorch versions)
krshrimali 242ca8b
Merge branch 'master' into video/feature/classification/from_tensors
krshrimali 738a022
Separate tests for data loading for tensors
krshrimali 465fb2f
Separate tests for data loading for tensors
krshrimali b235e7d
Merge branch 'video/feature/classification/from_tensors' of github.co…
krshrimali 5da11a4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 80ec1eb
Skip doctest if not video installed
krshrimali 6617097
Fix tests
krshrimali 22edc4e
skip if pytorchvideo not installed
krshrimali df37dd1
correct format in the doctest
krshrimali f864a3b
Merge branch 'master' into video/feature/classification/from_tensors
krshrimali 76410e0
Add labels to the call; prediction test
krshrimali 6f6de3a
Pass labels, add prediction test
krshrimali 6827164
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1f61192
Fix doc
krshrimali abd7f22
Merge branch 'video/feature/classification/from_tensors' of github.co…
krshrimali 92df464
Update tests/video/classification/test_model.py
krshrimali 3e887f3
Update flash/video/classification/utils.py
krshrimali a006bb6
Address review
krshrimali 2e65edc
Merge branch 'video/feature/classification/from_tensors' of github.co…
krshrimali 3ef8fbd
pep8
krshrimali 6fea612
Update flash/video/classification/utils.py
krshrimali d71ce41
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1c9e3f8
Address review: allow stack of tensors, tensor, list of tensors, matc…
krshrimali b81803b
Remove breakpoints
krshrimali 13df5af
Fix doctests
krshrimali da2fe56
Fix doctest
krshrimali 2381f04
Revert pre-commit change
krshrimali f95ef7d
Merge branch 'master' into video/feature/classification/from_tensors
krshrimali ecc5528
Add license, improve tests - use parametrize, refactor
krshrimali 73613f3
Merge branch 'video/feature/classification/from_tensors' of github.co…
krshrimali 2d69b40
Fix error for video not available
krshrimali 7009f86
unused import
krshrimali 62e67f5
Add check for video available or not
krshrimali e5b2350
If not video available, return tensors from randint
krshrimali 3e72e1a
mock_video_tensors is removed now
krshrimali 1cf3d75
Use _is_list_like instead of isinstance for list/tuple
krshrimali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from typing import List, Optional, Tuple, Type | ||
|
||
import torch | ||
|
||
from flash.core.utilities.imports import _VIDEO_AVAILABLE | ||
|
||
if _VIDEO_AVAILABLE: | ||
from pytorchvideo.data.utils import MultiProcessSampler | ||
else: | ||
MultiProcessSampler = None | ||
|
||
|
||
class LabeledVideoTensorDataset(torch.utils.data.IterableDataset): | ||
"""LabeledVideoTensorDataset handles a direct tensor input data.""" | ||
|
||
def __init__( | ||
self, | ||
labeled_video_tensors: List[Tuple[str, Optional[dict]]], | ||
video_sampler: Type[torch.utils.data.Sampler] = torch.utils.data.RandomSampler, | ||
) -> None: | ||
self._labeled_videos = labeled_video_tensors | ||
|
||
# If a RandomSampler is used we need to pass in a custom random generator that | ||
# ensures all PyTorch multiprocess workers have the same random seed. | ||
self._video_random_generator = None | ||
if video_sampler == torch.utils.data.RandomSampler: | ||
self._video_random_generator = torch.Generator() | ||
self._video_sampler = video_sampler(self._labeled_videos, generator=self._video_random_generator) | ||
else: | ||
self._video_sampler = video_sampler(self._labeled_videos) | ||
|
||
self._video_sampler_iter = None # Initialized on first call to self.__next__() | ||
|
||
# Depending on the clip sampler type, we may want to sample multiple clips | ||
# from one video. In that case, we keep the store video, label and previous sampled | ||
# clip time in these variables. | ||
self._loaded_video_label = None | ||
|
||
def __next__(self) -> dict: | ||
"""Retrieves the next clip based on the clip sampling strategy and video sampler. | ||
|
||
Returns: | ||
A dictionary with the following format. | ||
|
||
.. code-block:: text | ||
|
||
{ | ||
'video': <video_tensor>, | ||
'label': <index_label>, | ||
'video_label': <index_label> | ||
'video_index': <video_index>, | ||
} | ||
""" | ||
if not self._video_sampler_iter: | ||
# Setup MultiProcessSampler here - after PyTorch DataLoader workers are spawned. | ||
self._video_sampler_iter = iter(MultiProcessSampler(self._video_sampler)) | ||
|
||
# Reuse previously stored video if there are still clips to be sampled from | ||
# the last loaded video. | ||
video_index = next(self._video_sampler_iter) | ||
video_tensor, info_dict = self._labeled_videos[video_index] | ||
self._loaded_video_label = (video_tensor, info_dict, video_index) | ||
|
||
sample_dict = { | ||
"video": self._loaded_video_label[0], | ||
"video_name": f"video{video_index}", | ||
"video_index": video_index, | ||
"label": info_dict, | ||
"video_label": info_dict, | ||
} | ||
|
||
return sample_dict | ||
|
||
def __iter__(self): | ||
self._video_sampler_iter = None # Reset video sampler | ||
|
||
# If we're in a PyTorch DataLoader multiprocessing context, we need to use the | ||
# same seed for each worker's RandomSampler generator. The workers at each | ||
# __iter__ call are created from the unique value: worker_info.seed - worker_info.id, | ||
# which we can use for this seed. | ||
worker_info = torch.utils.data.get_worker_info() | ||
if self._video_random_generator is not None and worker_info is not None: | ||
base_seed = worker_info.seed - worker_info.id | ||
self._video_random_generator.manual_seed(base_seed) | ||
|
||
return self | ||
|
||
def size(self): | ||
return len(self._labeled_videos) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to make the API consistent with what we have for image classification. E.g. like this: