Improve dataset preparation support + multiresolution prep (#39)

* update

* make style

* renormalize correctly

* apply suggestions from review

* apply suggestions from review

* update
This commit is contained in:
Aryan
2024-10-17 16:48:52 +05:30
committed by GitHub
parent a6c246c29d
commit feb2e26547
5 changed files with 427 additions and 363 deletions
+38 -5
View File
@@ -78,15 +78,16 @@ class VideoDataset(Dataset):
self.video_paths,
) = self._load_dataset_from_csv()
self.num_videos = len(self.video_paths)
if self.num_videos != len(self.prompts):
if len(self.video_paths) != len(self.prompts):
raise ValueError(
f"Expected length of prompts and videos to be the same but found {len(self.prompts)=} and {len(self.video_paths)=}. Please ensure that the number of caption prompts and videos match in your dataset."
)
self.video_transforms = transforms.Compose(
[
transforms.RandomHorizontalFlip(random_flip) if random_flip else transforms.Lambda(self.identity_transform),
transforms.RandomHorizontalFlip(random_flip)
if random_flip
else transforms.Lambda(self.identity_transform),
transforms.Lambda(self.scale_transform),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5], inplace=True),
]
@@ -101,7 +102,7 @@ class VideoDataset(Dataset):
return x / 255.0
def __len__(self) -> int:
return self.num_videos
return len(self.video_paths)
def __getitem__(self, index: int) -> Dict[str, Any]:
if isinstance(index, list):
@@ -358,10 +359,30 @@ class VideoDatasetWithResizeAndRectangleCrop(VideoDataset):
class BucketSampler(Sampler):
def __init__(self, data_source: VideoDataset, batch_size: int = 8, shuffle: bool = True) -> None:
r"""
PyTorch Sampler that groups 3D data by height, width and frames.
Args:
data_source (`VideoDataset`):
A PyTorch dataset object that is an instance of `VideoDataset`.
batch_size (`int`, defaults to `8`):
The batch size to use for training.
shuffle (`bool`, defaults to `True`):
Whether or not to shuffle the data in each batch before dispatching to dataloader.
drop_last (`bool`, defaults to `False`):
Whether or not to drop incomplete buckets of data after completely iterating over all data
in the dataset. If set to True, only batches that have `batch_size` number of entries will
be yielded. If set to False, it is guaranteed that all data in the dataset will be processed
and batches that do not have `batch_size` number of entries will also be yielded.
"""
def __init__(
self, data_source: VideoDataset, batch_size: int = 8, shuffle: bool = True, drop_last: bool = False
) -> None:
self.data_source = data_source
self.batch_size = batch_size
self.shuffle = shuffle
self.drop_last = drop_last
self.buckets = {resolution: [] for resolution in data_source.resolutions}
@@ -377,3 +398,15 @@ class BucketSampler(Sampler):
yield self.buckets[(f, h, w)]
del self.buckets[(f, h, w)]
self.buckets[(f, h, w)] = []
if self.drop_last:
return
for fhw, bucket in list(self.buckets.items()):
if len(bucket) == 0:
continue
if self.shuffle:
random.shuffle(bucket)
yield bucket
del self.buckets[fhw]
self.buckets[fhw] = []