ding.utils.compression_helper¶
ding.utils.compression_helper
¶
CloudPickleWrapper
¶
Overview
CloudPickleWrapper can be able to pickle more python object(e.g: an object with lambda expression).
Interfaces:
__init__, __getstate__, __setstate__.
__init__(data)
¶
Overview
Initialize the CloudPickleWrapper using the given arguments.
Arguments:
- data (:obj:Any): The object to be dumped.
__getstate__()
¶
Overview
Get the state of the CloudPickleWrapper.
Returns:
- data (:obj:bytes): The dumped byte-like result.
__setstate__(data)
¶
Overview
Set the state of the CloudPickleWrapper.
Arguments:
- data (:obj:bytes): The dumped byte-like result.
dummy_compressor(data)
¶
Overview
Return the raw input data.
Arguments:
- data (:obj:Any): The input data of the compressor.
Returns:
- output (:obj:Any): This compressor will exactly return the input data.
zlib_data_compressor(data)
¶
Overview
Takes the input compressed data and return the compressed original data (zlib compressor) in binary format.
Arguments:
- data (:obj:Any): The input data of the compressor.
Returns:
- output (:obj:bytes): The compressed byte-like result.
Examples:
>>> zlib_data_compressor("Hello")
lz4_data_compressor(data)
¶
Overview
Return the compressed original data (lz4 compressor).The compressor outputs in binary format.
Arguments:
- data (:obj:Any): The input data of the compressor.
Returns:
- output (:obj:bytes): The compressed byte-like result.
Examples:
>>> lz4.block.compress(pickle.dumps("Hello"))
b' R Hello.'
jpeg_data_compressor(data)
¶
Overview
To reduce memory usage, we can choose to store the jpeg strings of image instead of the numpy array in the buffer. This function encodes the observation numpy arr to the jpeg strings.
Arguments:
- data (:obj:np.array): the observation numpy arr.
Returns:
- img_str (:obj:bytes): The compressed byte-like result.
get_data_compressor(name)
¶
Overview
Get the data compressor according to the input name.
Arguments:
- name(:obj:str): Name of the compressor, support ['lz4', 'zlib', 'jpeg', 'none']
Return:
- compressor (:obj:Callable): Corresponding data_compressor, taking input data returning compressed data.
Example:
>>> compress_fn = get_data_compressor('lz4')
>>> compressed_data = compressed(input_data)
dummy_decompressor(data)
¶
Overview
Return the input data.
Arguments:
- data (:obj:Any): The input data of the decompressor.
Returns:
- output (:obj:bytes): The decompressed result, which is exactly the input.
lz4_data_decompressor(compressed_data)
¶
Overview
Return the decompressed original data (lz4 compressor).
Arguments:
- data (:obj:bytes): The input data of the decompressor.
Returns:
- output (:obj:Any): The decompressed object.
zlib_data_decompressor(compressed_data)
¶
Overview
Return the decompressed original data (zlib compressor).
Arguments:
- data (:obj:bytes): The input data of the decompressor.
Returns:
- output (:obj:Any): The decompressed object.
jpeg_data_decompressor(compressed_data, gray_scale=False)
¶
Overview
To reduce memory usage, we can choose to store the jpeg strings of image instead of the numpy array in the buffer. This function decodes the observation numpy arr from the jpeg strings.
Arguments:
- compressed_data (:obj:bytes): The jpeg strings.
- gray_scale (:obj:bool): If the observation is gray, gray_scale=True,
if the observation is RGB, gray_scale=False.
Returns:
- arr (:obj:np.ndarray): The decompressed numpy array.
get_data_decompressor(name)
¶
Overview
Get the data decompressor according to the input name.
Arguments:
- name(:obj:str): Name of the decompressor, support ['lz4', 'zlib', 'none']
.. note::
For all the decompressors, the input of a bytes-like object is required.
Returns:
| Type | Description |
|---|---|
Callable
|
|
Examples: >>> decompress_fn = get_data_decompressor('lz4') >>> origin_data = compressed(compressed_data)
Full Source Code
../ding/utils/compression_helper.py