-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ⚡️ Add weighted random sampling.
- Loading branch information
1 parent
2dbf137
commit 85fd727
Showing
6 changed files
with
153 additions
and
80 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,8 @@ | |
__author__ = "Jeff Rhoades" | ||
__email__ = "[email protected]" | ||
|
||
from .dataloader import CellMapDataLoader | ||
from .multidataset import CellMapMultiDataset | ||
from .dataloader import CellMapDataLoader | ||
from .datasplit import CellMapDataSplit | ||
from .dataset import CellMapDataset | ||
from .image import CellMapImage |
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 |
---|---|---|
@@ -1,51 +1,72 @@ | ||
from torch.utils.data import DataLoader | ||
|
||
import torch | ||
from torch.utils.data import DataLoader, ConcatDataset, WeightedRandomSampler | ||
from .dataset import CellMapDataset | ||
from .multidataset import CellMapMultiDataset | ||
from .datasplit import CellMapDataSplit | ||
|
||
from typing import Callable, Iterable, Optional | ||
|
||
|
||
class CellMapDataLoader: | ||
# TODO: This class may be unnecessary | ||
# TODO: docstring corrections | ||
"""This subclasses PyTorch DataLoader to load CellMap data for training. It maintains the same API as the DataLoader class. This includes applying augmentations to the data and returning the data in the correct format for training, such as generating the target arrays (e.g. signed distance transform of labels). It retrieves raw and groundtruth data from a CellMapDataSplit object, which is a subclass of PyTorch Dataset. Training and validation data are split using the CellMapDataSplit object, and separate dataloaders are maintained as `train_loader` and `validate_loader` respectively.""" | ||
|
||
datasplit: CellMapDataSplit | ||
train_datasets: CellMapMultiDataset | ||
validate_datasets: CellMapMultiDataset | ||
train_loader: DataLoader | ||
validate_loader: DataLoader | ||
dataset: CellMapMultiDataset | CellMapDataset | ||
classes: Iterable[str] | ||
loader = DataLoader | ||
batch_size: int | ||
num_workers: int | ||
weighted_sampler: bool | ||
is_train: bool | ||
rng: Optional[torch.Generator] = None | ||
|
||
def __init__( | ||
self, | ||
datasplit: CellMapDataSplit, | ||
batch_size: int, | ||
num_workers: int, | ||
is_train: bool, | ||
dataset: CellMapMultiDataset | CellMapDataset, | ||
classes: Iterable[str], | ||
batch_size: int = 1, | ||
num_workers: int = 0, | ||
weighted_sampler: bool = False, | ||
is_train: bool = True, | ||
rng: Optional[torch.Generator] = None, | ||
): | ||
self.datasplit = datasplit | ||
self.dataset = dataset | ||
self.classes = classes | ||
self.batch_size = batch_size | ||
self.num_workers = num_workers | ||
self.weighted_sampler = weighted_sampler | ||
self.is_train = is_train | ||
self.rng = rng | ||
self.construct() | ||
|
||
# TODO: could keep dataloaders separate | ||
|
||
def construct(self): | ||
self.train_datasets = self.datasplit.train_datasets_combined | ||
self.validate_datasets = self.datasplit.validate_datasets_combined | ||
self.train_loader = DataLoader( | ||
self.train_datasets, | ||
batch_size=self.batch_size, | ||
num_workers=self.num_workers, | ||
shuffle=self.is_train, | ||
) | ||
self.validate_loader = DataLoader( | ||
self.validate_datasets, | ||
batch_size=1, | ||
num_workers=self.num_workers, | ||
shuffle=False, | ||
) | ||
if self.weighted_sampler: | ||
assert isinstance( | ||
self.dataset, CellMapMultiDataset | ||
), "Weighted sampler only relevant for CellMapMultiDataset" | ||
self.sampler = self.dataset.weighted_sampler(self.batch_size, self.rng) | ||
else: | ||
self.sampler = None | ||
kwargs = { | ||
"dataset": self.dataset, | ||
"batch_size": self.batch_size, | ||
"num_workers": self.num_workers, | ||
"collate_fn": self.collate_fn, | ||
} | ||
if self.weighted_sampler: | ||
kwargs["sampler"] = self.sampler | ||
elif self.is_train: | ||
kwargs["shuffle"] = True | ||
else: | ||
kwargs["shuffle"] = False | ||
self.loader = DataLoader(**kwargs) | ||
|
||
def collate_fn(self, batch): | ||
outputs = {} | ||
for b in batch: | ||
for key, value in b.items(): | ||
if key not in outputs: | ||
outputs[key] = [] | ||
outputs[key].append(value) | ||
for key, value in outputs.items(): | ||
outputs[key] = torch.stack(value) | ||
return outputs |
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
Oops, something went wrong.