ActivationDatasetWrapper
- class hybrid_learning.datasets.activations_handle.ActivationDatasetWrapper(dataset, act_map_gen=None, activations_root=None, device=None, **data_args)[source]
Bases:
DatasetWrapper
Wrapper 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.Subset
andhybrid_learning.datasets.base.BaseDataset
instances.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 checks
andcache filling
with progress bar.Replacement of the
activation generator
by the cache, i.e. noact_map_gen
must 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_root
and disabled by settingactivations_root
toNone
. 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
dataset
The wrapped dataset.
Inherited from : py: class:BaseDataset
settings
Settings 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
i
is 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
idx
in dataset with transformations applied.__repr__
()Nice printing function.
Inherited from : py: class:Dataset
__getitem__
(idx)Get item from
idx
in 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_cache
from 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
None
in case the activations root is set toFalse
during init.