ActivationDatasetWrapper
- class hybrid_learning.datasets.activations_handle.ActivationDatasetWrapper(dataset, act_map_gen=None, activations_root=None, device=None, **data_args)[source]
Bases:
DatasetWrapperWrapper for image datasets that will generate and yield activation maps. Behaves like a sequence of tuples of
- activation maps (original input transformed by
generate_act_map) and
original ground truth (e.g. mask)
The wrapper can handle
torch.utils.data.Subsetandhybrid_learning.datasets.base.BaseDatasetinstances.Features:
Option to enable efficient file caching of the generated activation maps in order to avoid costly re-evaluations. See
act_maps_cache.Convenience functions for caching:
existence checksandcache fillingwith progress bar.Replacement of the
activation generatorby the cache, i.e. noact_map_genmust be provided if activation maps for all indices are cached (make sure to not clear the cache then though).
Activation map caching is enabled by setting
activations_rootand disabled by settingactivations_roottoNone. To fill the cache i.e. generate all activation maps, callfill_cache(). But be aware that this can be very time consuming depending on the generator.Public Data Attributes:
The activations root of the file cache if caching is enable.
Inherited from : py: class:DatasetWrapper
datasetThe wrapped dataset.
Inherited from : py: class:BaseDataset
settingsSettings of the instance.
Public Methods:
getitem(i)Get activation map and original ground truth for item at index
i.Return the path to the activation map file in the cache.
Check whether the activation map at index
iis cached.load_image(i)Load the image/original input for index
i.fill_cache([force_rebuild, show_progress_bar])Generate activation maps for all images.
Inherited from : py: class:DatasetWrapper
getitem(i)Get activation map and original ground truth for item at index
i.descriptor(i)Wrap descriptor method of wrapped dataset.
Inherited from : py: class:BaseDataset
getitem(i)Get activation map and original ground truth for item at index
i.descriptor(i)Wrap descriptor method of wrapped dataset.
Special Methods:
__init__(dataset[, act_map_gen, ...])Init.
Inherited from : py: class:DatasetWrapper
__init__(dataset[, act_map_gen, ...])Init.
__len__()Length determined by the length of the wrapped dataset.
Inherited from : py: class:BaseDataset
__init__(dataset[, act_map_gen, ...])Init.
__len__()Length determined by the length of the wrapped dataset.
__getitem__(idx)Get item from
idxin dataset with transformations applied.__repr__()Nice printing function.
Inherited from : py: class:Dataset
__getitem__(idx)Get item from
idxin dataset with transformations applied.__add__(other)
- __init__(dataset, act_map_gen=None, activations_root=None, device=None, **data_args)[source]
Init.
The base settings (dataset root, split) default to those of the wrapped dataset.
- Parameters
dataset (BaseDataset) – Dataset to wrap; must be a sequence of tuples of
(image, ground_truth)with both image and ground-truth of typetorch.Tensor; the default transformation assumes that the ground truth are masks (same sized images)act_map_gen (Optional[Module]) – torch module that accepts as input a batch of images and returns the activation maps to yield
activations_root (Optional[str]) – root directory under which to store and find the activation maps if file caching shall be enabled
device (Optional[Union[str, device]]) – the device on which to run
act_map_gen; seehybrid_learning.datasets.transforms.image_transforms.ToActMap
- act_map_filepath(i)[source]
Return the path to the activation map file in the cache. The base directory is
activations_root. The basename is determined by theact_maps_cachefrom thedescriptor()for the indexi.
- fill_cache(force_rebuild=False, show_progress_bar=True, **kwargs)[source]
Generate activation maps for all images.
- Parameters
- Return type
- getitem(i)[source]
Get activation map and original ground truth for item at index
i.Used for
__getitem__(). If the activation map does not exist and a generator is given ingenerate_act_map, generate and save the activation map.
- __parameters__ = ()
- act_maps_cache: PTCache
File cache for caching activations. Set to
Nonein case the activations root is set toFalseduring init.