Args: data: input data to load and transform to generate dataset for model. transform: transforms to execute operations on input data. cache_num: number of items to be cached. Default is `sys.maxsize`. will take the minimum of (cache_num,
(
self,
data: Sequence,
transform: Sequence[Callable] | Callable | None = None,
cache_num: int = sys.maxsize,
cache_rate: float = 1.0,
num_workers: int | None = 1,
progress: bool = True,
copy_cache: bool = True,
as_contiguous: bool = True,
hash_as_key: bool = False,
hash_func: Callable[..., bytes] = pickle_hashing,
runtime_cache: bool | str | list | ListProxy = False,
)
| 786 | """ |
| 787 | |
| 788 | def __init__( |
| 789 | self, |
| 790 | data: Sequence, |
| 791 | transform: Sequence[Callable] | Callable | None = None, |
| 792 | cache_num: int = sys.maxsize, |
| 793 | cache_rate: float = 1.0, |
| 794 | num_workers: int | None = 1, |
| 795 | progress: bool = True, |
| 796 | copy_cache: bool = True, |
| 797 | as_contiguous: bool = True, |
| 798 | hash_as_key: bool = False, |
| 799 | hash_func: Callable[..., bytes] = pickle_hashing, |
| 800 | runtime_cache: bool | str | list | ListProxy = False, |
| 801 | ) -> None: |
| 802 | """ |
| 803 | Args: |
| 804 | data: input data to load and transform to generate dataset for model. |
| 805 | transform: transforms to execute operations on input data. |
| 806 | cache_num: number of items to be cached. Default is `sys.maxsize`. |
| 807 | will take the minimum of (cache_num, data_length x cache_rate, data_length). |
| 808 | cache_rate: percentage of cached data in total, default is 1.0 (cache all). |
| 809 | will take the minimum of (cache_num, data_length x cache_rate, data_length). |
| 810 | num_workers: the number of worker threads if computing cache in the initialization. |
| 811 | If num_workers is None then the number returned by os.cpu_count() is used. |
| 812 | If a value less than 1 is specified, 1 will be used instead. |
| 813 | progress: whether to display a progress bar. |
| 814 | copy_cache: whether to `deepcopy` the cache content before applying the random transforms, |
| 815 | default to `True`. if the random transforms don't modify the cached content |
| 816 | (for example, randomly crop from the cached image and deepcopy the crop region) |
| 817 | or if every cache item is only used once in a `multi-processing` environment, |
| 818 | may set `copy=False` for better performance. |
| 819 | as_contiguous: whether to convert the cached NumPy array or PyTorch tensor to be contiguous. |
| 820 | it may help improve the performance of following logic. |
| 821 | hash_as_key: whether to compute hash value of input data as the key to save cache, |
| 822 | if key exists, avoid saving duplicated content. it can help save memory when |
| 823 | the dataset has duplicated items or augmented dataset. |
| 824 | hash_func: if `hash_as_key`, a callable to compute hash from data items to be cached. |
| 825 | defaults to `monai.data.utils.pickle_hashing`. |
| 826 | runtime_cache: mode of cache at the runtime. Default to `False` to prepare |
| 827 | the cache content for the entire ``data`` during initialization, this potentially largely increase the |
| 828 | time required between the constructor called and first mini-batch generated. |
| 829 | Three options are provided to compute the cache on the fly after the dataset initialization: |
| 830 | |
| 831 | 1. ``"threads"`` or ``True``: use a regular ``list`` to store the cache items. |
| 832 | 2. ``"processes"``: use a ListProxy to store the cache items, it can be shared among processes. |
| 833 | 3. A list-like object: a users-provided container to be used to store the cache items. |
| 834 | |
| 835 | For `thread-based` caching (typically for caching cuda tensors), option 1 is recommended. |
| 836 | For single process workflows with multiprocessing data loading, option 2 is recommended. |
| 837 | For multiprocessing workflows (typically for distributed training), |
| 838 | where this class is initialized in subprocesses, option 3 is recommended, |
| 839 | and the list-like object should be prepared in the main process and passed to all subprocesses. |
| 840 | Not following these recommendations may lead to runtime errors or duplicated cache across processes. |
| 841 | |
| 842 | """ |
| 843 | super().__init__(data=data, transform=transform) |
| 844 | self.set_num = cache_num # tracking the user-provided `cache_num` option |
| 845 | self.set_rate = cache_rate # tracking the user-provided `cache_rate` option |