export_to_ketos_protobuf

ketos.neural_networks.dev_utils.export.export_to_ketos_protobuf(model, output_name, audio_repr, input_shape=None, tmp_folder='tmp_export_folder', overwrite=True, duration=None, backward_compat=True, **kwargs)[source]

Export a ketos model to Ketos-Protobuf format.

Saving your ketos model in Ketos-Protobuf format makes it easier to share it with collaborators and use it with other software applications.

In particular, the output file generated by this function can be loaded directly into PAMGuard, an open-source and widely adopted application for passive acoustic monitoring (PAM).

If the output directory does not already exist, it is automatically created.

The function generates a zipped archive containing,

  • the tensorflow model in protobuf format (model/model.pb)

  • the audio representation (audio_repr.json)

  • the ketos model recipe (recipe.json)

The user is free to specify the extension of the output file, but we recommend using *.ktpb as this will allow the file to be recognized and loaded into PAMGuard.

A warning will be printed if the method is unable to infer values for the parameters duration and step. The model will be saved, but it will not be possible to load it into PAMGuard.

Args:
model:

The ketos model to be exported. Usually created by one of the Interface classes found in ketos.neural_networks (e.g.: ResNetInterface)

output_name: str

The name of the exported model. Must have the extension *.ktpb to ensure that it can be loaded into PAMGuard.

input_shape: list or tuple.

The input shape expected by the model. It can be represented by a tuple or list of four elements: (number of intances, width, height, number of channels). The number of instances and number of channels are commonly 1, and the width and height are usually the number of time and frequency bins in a spectrogram, respectively. This, however, can vary with the model in question.

audio_repr: dict or str

Audio representation. For example,

>>> audio_repr = {"type": "MagSpectrogram",
...               "rate": "1000 Hz", 
...               "window": "0.256 s",
...               "step": "0.032 s",
...               "freq_min": "0 Hz",
...               "freq_max": "500 Hz",
...               "window_func": "hamming",
...               "transforms": [{"name":"normalize"}]
...              }                

It is also possible to specify the path to a json file containing the audio representation.

tmp_folder: str

The name of a temporary folder created during the model conversion. It will be deleted upon sucessful execution. If the folder already exists, a ‘FileExistsError will be thrown, unless ‘overwrite’ is set to True.

overwrite: bool

If True and the folder specified in ‘tmp_folder’ exists, the folder will be overwritten.

duration: float

Duration in seconds of the input sample. If not specified, the duration is extracted from the audio representation, or, if not available there, it is computed as step * input_shape[0], provided that input_shape has been specified.

backward_compat: bool

Ensure backward compatibility with ketos versions 4.2.1 and older

Raises:

AssertionError if the input shape cannot be inferred.