forked from NVlabs/FastGen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_cond_dataloader.py
More file actions
49 lines (42 loc) · 1.66 KB
/
Copy pathclass_cond_dataloader.py
File metadata and controls
49 lines (42 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from torch.utils.data import DataLoader
from fastgen.datasets.class_cond_dataset import ImageFolderDataset
from fastgen.datasets.samplers import InfiniteSampler
class ImageLoader:
def __init__(
self,
dataset_path: str,
s3_path: str,
batch_size: int,
use_labels: bool = True,
cache: bool = True,
shuffle: bool = True,
sampler_start_idx: int = 0,
**kwargs,
):
"""
ImageLoader for class conditional datasets
Args:
dataset_path (str): Path to the dataset
s3_path (str): Path to the s3 bucket
batch_size (int): Batch size
use_labels (bool): Whether to use labels
cache (bool): Whether to cache the dataset
shuffle (bool): Whether to shuffle the dataset
sampler_start_idx (int): Start index for the sampler
"""
self.dataset = ImageFolderDataset(
path=dataset_path, s3_path=s3_path, use_labels=use_labels, cache=cache, **kwargs
)
dataset_sampler = InfiniteSampler(dataset=self.dataset, shuffle=shuffle, start_idx=sampler_start_idx)
data_loader_kwargs = dict(
num_workers=1, # don't change this, otherwise it will cause BadZipFile error
pin_memory=True,
prefetch_factor=2,
)
self.loader = DataLoader(
dataset=self.dataset, sampler=dataset_sampler, batch_size=batch_size, **data_loader_kwargs
)
def __iter__(self):
return iter(self.loader)