Spaces:
Build error
Build error
File size: 5,369 Bytes
910e2ad |
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
import os
import json
import torch
import time
import random
from typing import Iterable
from collections import OrderedDict
from PIL import Image
from torch.utils.data import Dataset, DataLoader, ConcatDataset, IterableDataset, DistributedSampler, RandomSampler
from torch.utils.data.dataloader import default_collate
from torchvision import transforms
from torchvision.transforms.functional import InterpolationMode
from torchvision.transforms import functional as F
from .bucket_loader import Bucketeer, TemporalLengthBucketeer
class IterLoader:
"""
A wrapper to convert DataLoader as an infinite iterator.
Modified from:
https://github.com/open-mmlab/mmcv/blob/master/mmcv/runner/iter_based_runner.py
"""
def __init__(self, dataloader: DataLoader, use_distributed: bool = False, epoch: int = 0):
self._dataloader = dataloader
self.iter_loader = iter(self._dataloader)
self._use_distributed = use_distributed
self._epoch = epoch
@property
def epoch(self) -> int:
return self._epoch
def __next__(self):
try:
data = next(self.iter_loader)
except StopIteration:
self._epoch += 1
if hasattr(self._dataloader.sampler, "set_epoch") and self._use_distributed:
self._dataloader.sampler.set_epoch(self._epoch)
time.sleep(2) # Prevent possible deadlock during epoch transition
self.iter_loader = iter(self._dataloader)
data = next(self.iter_loader)
return data
def __iter__(self):
return self
def __len__(self):
return len(self._dataloader)
def identity(x):
return x
def create_image_text_dataloaders(dataset, batch_size, num_workers,
multi_aspect_ratio=True, epoch=0, sizes=[(512, 512), (384, 640), (640, 384)],
use_distributed=True, world_size=None, rank=None,
):
"""
The dataset has already been splited by different rank
"""
if use_distributed:
assert world_size is not None
assert rank is not None
sampler = DistributedSampler(
dataset,
shuffle=True,
num_replicas=world_size,
rank=rank,
seed=epoch,
)
else:
sampler = RandomSampler(dataset)
dataloader = DataLoader(
dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=True,
sampler=sampler,
collate_fn=identity if multi_aspect_ratio else default_collate,
drop_last=True,
)
if multi_aspect_ratio:
dataloader_iterator = Bucketeer(
dataloader,
sizes=sizes,
is_infinite=True, epoch=epoch,
)
else:
dataloader_iterator = iter(dataloader)
# To make it infinite
loader = IterLoader(dataloader_iterator, use_distributed=False, epoch=epoch)
return loader
def create_length_grouped_video_text_dataloader(dataset, batch_size, num_workers, max_frames,
world_size=None, rank=None, epoch=0, use_distributed=False):
if use_distributed:
assert world_size is not None
assert rank is not None
sampler = DistributedSampler(
dataset,
shuffle=True,
num_replicas=world_size,
rank=rank,
seed=epoch,
)
else:
sampler = RandomSampler(dataset)
dataloader = DataLoader(
dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=True,
sampler=sampler,
collate_fn=identity,
drop_last=True,
)
# make it infinite
dataloader_iterator = TemporalLengthBucketeer(
dataloader,
max_frames=max_frames,
epoch=epoch,
)
return dataloader_iterator
def create_mixed_dataloaders(
dataset, batch_size, num_workers, world_size=None, rank=None, epoch=0,
image_mix_ratio=0.1, use_image_video_mixed_training=True,
):
"""
The video & image mixed training dataloader builder
"""
assert world_size is not None
assert rank is not None
image_gpus = max(1, int(world_size * image_mix_ratio))
if use_image_video_mixed_training:
video_gpus = world_size - image_gpus
else:
# only use video data
video_gpus = world_size
image_gpus = 0
print(f"{image_gpus} gpus for image, {video_gpus} gpus for video")
if rank < video_gpus:
sampler = DistributedSampler(
dataset,
shuffle=True,
num_replicas=video_gpus,
rank=rank,
seed=epoch,
)
else:
sampler = DistributedSampler(
dataset,
shuffle=True,
num_replicas=image_gpus,
rank=rank - video_gpus,
seed=epoch,
)
loader = DataLoader(
dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=True,
sampler=sampler,
collate_fn=default_collate,
drop_last=True,
)
# To make it infinite
loader = IterLoader(loader, use_distributed=True, epoch=epoch)
return loader |