Spaces:
Running on Zero
Running on Zero
File size: 7,560 Bytes
0122a25 | 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | """Dataset sampler from VGGT.
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
"""
from __future__ import annotations
import random
import numpy as np
from torch.utils.data import DistributedSampler, RandomSampler, Sampler
class DynamicBatchSampler(Sampler):
"""A custom batch sampler.
Dynamically adjusts batch size, aspect ratio, and image number for each
sample. Batches within a sample share the same aspect ratio and image
number.
"""
def __init__(
self,
sampler,
aspect_ratio_range,
image_num_range,
epoch=0,
seed=42,
max_img_per_gpu=48,
):
"""Initializes the dynamic batch sampler.
Args:
sampler: Instance of DynamicDistributedSampler.
aspect_ratio_range: List containing [min_aspect_ratio, max_aspect_ratio].
image_num_range: List containing [min_images, max_images] per sample.
epoch: Current epoch number.
seed: Random seed for reproducibility.
max_img_per_gpu: Maximum number of images to fit in GPU memory.
"""
self.sampler = sampler
self.aspect_ratio_range = aspect_ratio_range
self.image_num_range = image_num_range
self.rng = random.Random()
# Uniformly sample from the range of possible image numbers
# For any image number, the weight is 1.0 (uniform sampling). You can set any different weights here.
self.image_num_weights = {
num_images: 1.0
for num_images in range(image_num_range[0], image_num_range[1] + 1)
}
# Possible image numbers, e.g., [2, 3, 4, ..., 24]
self.possible_nums = np.array(
[
n
for n in self.image_num_weights.keys()
if self.image_num_range[0] <= n <= self.image_num_range[1]
]
)
# Normalize weights for sampling
weights = [self.image_num_weights[n] for n in self.possible_nums]
self.normalized_weights = np.array(weights) / sum(weights)
# Maximum image number per GPU
self.max_img_per_gpu = max_img_per_gpu
# Set the epoch for the sampler
self.set_epoch(epoch + seed)
def set_epoch(self, epoch):
"""Sets the epoch for this sampler, affecting the random sequence.
Args:
epoch: The epoch number.
"""
self.sampler.set_epoch(epoch)
self.epoch = epoch
self.rng.seed(epoch * 100)
def __iter__(self):
"""Yields batches of samples with synchronized dynamic parameters.
Returns:
Iterator yielding batches of indices with associated parameters.
"""
sampler_iterator = iter(self.sampler)
while True:
try:
# Sample random image number and aspect ratio
random_image_num = int(
np.random.choice(
self.possible_nums, p=self.normalized_weights
)
)
random_aspect_ratio = round(
self.rng.uniform(
self.aspect_ratio_range[0], self.aspect_ratio_range[1]
),
2,
)
# Update sampler parameters
self.sampler.update_parameters(
aspect_ratio=random_aspect_ratio,
image_num=random_image_num,
)
# Calculate batch size based on max images per GPU and current
# image number
batch_size = self.max_img_per_gpu / random_image_num
batch_size = np.floor(batch_size).astype(int)
batch_size = max(
1, batch_size
) # Ensure batch size is at least 1
# Collect samples for the current batch
current_batch = []
for _ in range(batch_size):
try:
item = next(
sampler_iterator
) # item is (idx, aspect_ratio, image_num)
current_batch.append(item)
except StopIteration:
break # No more samples
if not current_batch:
break # No more data to yield
yield current_batch
except StopIteration:
break # End of sampler's iterator
def __len__(self):
# Return a large dummy length
return 1000000
class DynamicDistributedSampler(DistributedSampler):
"""Extends PyTorch's DistributedSampler.
Include dynamic aspect_ratio and image_num parameters, which can be passed
into the dataset's __getitem__ method.
"""
def __init__(
self,
dataset,
num_replicas: int | None = None,
rank: int | None = None,
shuffle: bool = False,
seed: int = 0,
drop_last: bool = False,
):
"""Init."""
super().__init__(
dataset,
num_replicas=num_replicas,
rank=rank,
shuffle=shuffle,
seed=seed,
drop_last=drop_last,
)
self.aspect_ratio = None
self.image_num = None
def __iter__(self):
"""Yields a sequence of (index, image_num, aspect_ratio).
Relies on the parent class's logic for shuffling/distributing
the indices across replicas, then attaches extra parameters.
"""
indices_iter = super().__iter__()
for idx in indices_iter:
yield (idx, self.image_num, self.aspect_ratio)
def update_parameters(self, aspect_ratio, image_num):
"""Updates dynamic parameters for each new epoch or iteration.
Args:
aspect_ratio: The aspect ratio to set.
image_num: The number of images to set.
"""
self.aspect_ratio = aspect_ratio
self.image_num = image_num
class DynamicSampler(RandomSampler):
"""Extends PyTorch's Sampler.
Include dynamic aspect_ratio and image_num parameters, which can be passed
into the dataset's __getitem__ method.
"""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.aspect_ratio = None
self.image_num = None
def __iter__(self):
"""Yields a sequence of (index, image_num, aspect_ratio).
Relies on the parent class's logic for shuffling/distributing
the indices across replicas, then attaches extra parameters.
"""
indices_iter = super().__iter__()
for idx in indices_iter:
yield (idx, self.image_num, self.aspect_ratio)
def set_epoch(self, epoch):
"""Sets the epoch for this sampler, affecting the random sequence.
Args:
epoch: The epoch number.
"""
self.epoch = epoch
def update_parameters(self, aspect_ratio, image_num):
"""Updates dynamic parameters for each new epoch or iteration.
Args:
aspect_ratio: The aspect ratio to set.
image_num: The number of images to set.
"""
self.aspect_ratio = aspect_ratio
self.image_num = image_num
def __len__(self) -> int:
return len(self.data_source)
|