dataloader
Module diagram
classDiagram
class dataloader {
}
class builder {
}
class full2patch_loader {
}
class infinite_random_sampler {
}
dataloader --> builder
builder --> full2patch_loader
data.dataloader
get_dataloader_class
get_dataloader_class(cfg_dataloader_name: Optional[str] = None, crop_fn: Optional[Callable[[dict], List[dict]]] = None) -> Type[SaigeDataLoader]
getting dataloader class
Parameters:
-
cfg_dataloader_name(Optional[str], default:None) –name of dataloader class. If None, choose appropriate class considering
crop_fn. Defaults to None. -
crop_fn(Optional[Callable[[dict], List[dict]]], default:None) –image crop function. Used for segmentation/OCR patch-wise training. Defaults to None.
Returns:
-
Type[SaigeDataLoader]–Type[SaigeDataLoader]: dataloader class
Source code in SaigeToolkit/data/dataloader/builder.py
builder
get_dataloader_class
get_dataloader_class(cfg_dataloader_name: Optional[str] = None, crop_fn: Optional[Callable[[dict], List[dict]]] = None) -> Type[SaigeDataLoader]
getting dataloader class
Parameters:
-
cfg_dataloader_name(Optional[str], default:None) –name of dataloader class. If None, choose appropriate class considering
crop_fn. Defaults to None. -
crop_fn(Optional[Callable[[dict], List[dict]]], default:None) –image crop function. Used for segmentation/OCR patch-wise training. Defaults to None.
Returns:
-
Type[SaigeDataLoader]–Type[SaigeDataLoader]: dataloader class
Source code in SaigeToolkit/data/dataloader/builder.py
full2patch_loader
Full2PatchDataLoader
Full2PatchDataLoader(dataset: Dataset, collate_fn: Optional[Callable[[List[dict]], dict]] = None, batch_sampler: Optional[Iterable[int]] = None, batch_size: int = 1, shuffle: bool = False, drop_last: bool = True, **cfg_dataloader: dict)
DataLoader class for Segmentation and SceneTextRecognition tasks.
Whole image is divided into multiple image patches, based on polygon labels.
Full2PatchDataLoader has dataloader as class attribute, which returns croppped image patches.
Image patches are stocked in buffer attribute, then passed to deep network in batch_size.
Attributes:
-
dataset(Dataset) –base dataset
-
batch_size(int) –batch size
-
drop_last(bool) –whether drop remaining data items less than batch_size at last iter.
-
buffer_multiple(int) –maximum size of buffer is decided by {buffer_multiple} x {batch_size}.
-
loader(DataLoader) –DataLoader that provides cropped image patches.
-
loader_iter(Iterable[list]) –iterator of loader attribute.
-
buffer(List[dict]) –buffer where image patches are stacking.
-
collate_function(Callable[[List[dict]], dict]) –function collating image patches into a batch.
initializing Full2PatchLoader
Parameters:
-
dataset(Dataset) –base dataset to be loaded.
-
collate_fn(Optional[Callable[[List[dict]], dict]], default:None) –function collating image patches into a batch. Defaults to None.
-
sampler(Optional[Iterable[int]]) –specific sampler such as balanced sampler. Defaults to None.
-
batch_size(int, default:1) –size of a batch for one iteration. Defaults to 1.
-
drop_last(bool, default:True) –drop_last boolean. Defaults to True.
Source code in SaigeToolkit/data/dataloader/full2patch_loader.py
collate_patch
dataset with crop_fn returns List[List[dict]] type. Since batch_size of self.loader is hard-defined as 1, simply returning first item of input is enough.
Parameters:
-
batch(List[List[dict]]) –batch (1) of cropped patches (n)
Returns:
-
List[dict]–List[dict]: cropped patches (n)
Source code in SaigeToolkit/data/dataloader/full2patch_loader.py
collate_batch
collating image patches into a batch
Parameters:
-
batch(List[dict]) –list cropped patch data dicts (n)
Returns:
-
dict(dict) –a batch dict
Source code in SaigeToolkit/data/dataloader/full2patch_loader.py
__next__
generating a batch data, extracting from buffer stack.
Raises:
-
StopIteration–self.get_buffer() called due to (len(self.buffer) < self.batch_size) and (len(self.buffer) == 0) even after self.get_buffer() : raise StopIteration.
-
StopIteration–self.get_buffer() called due to (len(self.buffer) < self.batch_size) and (len(self.buffer) < self.batch_size) even after self.get_buffer() and (drop_last == True) : raise StopIteration.
Returns:
-
dict(dict) –a batch dict
Source code in SaigeToolkit/data/dataloader/full2patch_loader.py
infinite_random_sampler
InfiniteRandomSampler
Bases: RandomSampler
Infinitely samples elements randomly. If without replacement, then sample from a shuffled dataset.
If with replacement, then user can specify :attr:num_samples to draw.
Parameters:
-
data_source(Dataset) –dataset to sample from
-
replacement(bool) –samples are drawn on-demand with replacement if
True, default=False -
num_samples(int) –number of samples to draw, default=
len(dataset). -
generator(Generator) –Generator used in sampling.
Assume that this DataLoader is used for training with shuffle=True.
We use InfiniteRandomSampler to make the DataLoader to infinitely sample the dataset.
This prevents the "drop_last" and the "prefeching" problem across epochs.
Be careful that you should stop the training loop by counting the number of steps manually.
The DataLoader will never stop by itself.
NOTE: Note that this sampler has infinite length and thus you should be careful when calculating the current epoch. We recommend you to use step//steps_per_epoch to calculate the current epoch.