JPGCache

class hybrid_learning.datasets.caching.JPGCache(cache_root, mode='RGB', before_put=None, after_load=None)[source]

Bases: FileCache

Cache for JPEG images using PIL. Non-image tensor or array objects are converted to images and must have the shape (height, width, channels). For mode auto-inference see torchvision.transforms.ToPILImage.

Note

Will by default return tensors of type torch.float with a value range in [0, 1]. Set after_load accordingly, if a different type and value range is desired.

Warning

If mode is set to RGB, make sure that the dtype of the tensors either is

The pixel value ranges of the JPG images are always in [0, 255]. For modes ‘L’ and ‘RGB’ with float dtype (not mode ‘F’ and not for int or bool types!), the value range of the tensor is automatically scaled from [0, 1] to [0, 255] during put. During load, modes ‘L’ and ‘RGB’ are automatically downscaled again from range [0, 255] to [0, 1]. This means, the following pixel value ranges of the tensors (after the before_put transformation) are assumed:

  • mode ‘F’: [0, 255]

  • mode ‘L’, dtype int: [0, 255]

  • mode ‘L’, dtype float: [0, 1] -> automatically scaled to [0, 255]

  • mode ‘RGB’, dtype int: [0, 255]

  • mode ‘RGB’, dtype float: [0, 1] -> automatically scaled to [0, 255]

Note that only integer values can be saved in JPG images, so values of mode ‘F’ get rounded to obtain valid mode ‘L’ values.

Public Data Attributes:

FILE_ENDING

JPEG file ending.

Inherited from : py: class:FileCache

FILE_ENDING

JPEG file ending.

Public Methods:

put_file(filepath, obj)

Convert obj to a py:mod:PIL image and save to filepath.

load_file(filepath)

Load image and convert to correct mode.

Inherited from : py: class:FileCache

put(descriptor, obj)

Store obj under the cache root using put_file().

load(descriptor)

Load object from file descriptor + FILE_ENDING under cache root.

clear()

Remove all files from cache root.

descriptors()

Provide paths of all cached files with ending stripped and relative to cache root.

descriptor_to_fp(descriptor)

Return the file path of the cache file for a given descriptor.

put_file(filepath, obj)

Convert obj to a py:mod:PIL image and save to filepath.

load_file(filepath)

Load image and convert to correct mode.

Inherited from : py: class:Cache

put(descriptor, obj)

Store obj under the cache root using put_file().

load(descriptor)

Load object from file descriptor + FILE_ENDING under cache root.

put_batch(descriptors, objs)

Store a batch of objs in this cache using according descriptors.

load_batch(descriptors[, return_none_if])

Load a batch of objects.

clear()

Remove all files from cache root.

descriptors()

Provide paths of all cached files with ending stripped and relative to cache root.

as_dict()

Return a dict with all cached descriptors and objects.

wrap(getitem[, descriptor_map])

Add this cache to the deterministic function getitem (which should have no side effects).

Special Methods:

__init__(cache_root[, mode, before_put, ...])

Init.

Inherited from : py: class:FileCache

__init__(cache_root[, mode, before_put, ...])

Init.

__repr__()

Return repr(self).

Inherited from : py: class:Cache

__repr__()

Return repr(self).

__add__(other)

Return a (cascaded) cache which will first lookup self then other with default sync mode.

__radd__(other)

Return a (cascaded) cache which will first lookup other then self with default sync mode.


Parameters
__init__(cache_root, mode='RGB', before_put=None, after_load=None)[source]

Init.

Parameters
load_file(filepath)[source]

Load image and convert to correct mode.

Parameters

filepath (str) –

Return type

Tensor

put_file(filepath, obj)[source]

Convert obj to a py:mod:PIL image and save to filepath.

Parameters
static save_image(img, filepath)[source]

Save an image as JPG and take care of necessary conversion.

Parameters
  • img (Image) –

  • filepath (str) –

FILE_ENDING = '.jpg'

JPEG file ending. Appended to the descriptors to get the file path (may lead to a double ending, this is intentional).

after_load: Optional[Callable[[Any], Tensor]]

The transformation applied to loaded tensors.

before_put: Callable[[Any], Tensor]

The transformation applied to tensors before turning them into images for putting. By default only densifies them.

mode: str

The PIL.Image.Image mode to be represented by the put and loaded tensors (after the :py:attr`before_put` resp. before the after_load transformation). Note that the images are converted to mode 'RGB' (for 3 to 4 channels) or mode 'L' (for 1 to 2 channels) before saving to JPG. Not all dtypes are supported for mode==None.