src.model.deeplearn.handle.dl_transfer_handler

Classes

DLTransferHandler(transfers)

Class to handle transfer learning operations, i.e., transferring the weights from a given model to the current one.

class src.model.deeplearn.handle.dl_transfer_handler.DLTransferHandler(transfers)

Class to handle transfer learning operations, i.e., transferring the weights from a given model to the current one.

Variables:
  • weights_path (None or list of str) – Path to the weights that must be transferred to the current model. It can be either a .keras or a .weights.h5 file. When not given, no transfer will take place.

  • layer_translator (None or list of list of dict) – A list of dictionaries such that each dictionary gives a key representing the name of the layer in the current model and a value representing the name of the layer in the model whose weights must be transferred.

  • default_to_null (None or list of bool) – If true and no translation is specified in layer_translator, the layer weights will not be updated. If false (default), when the layer name is not included in the layer_translator it will be searched in the model weights assuming exactly the same name.

  • transfer_count (int) – How many times the weights have been transferred.

__init__(transfers)
transfer(mh)

Transfer the weights. Note that this method will only do the transfer one time, i.e., when transfer_count > 1 it will not transfer again. If the weights must be transferred anyway, it can be done by calling directly the DLTransferHandler.do_transfer() method.

Parameters:

mh (DLModelHandler) – The model handler that contains the model whose weights must be updated.

Returns:

Nothing at all, but the model in the model handler is updated in place.

do_transfer(mh)

Do the actual weight transfer between the handled model and the given weights.

See DLTransferHandler.transfer() for arguments documentation.

transfer_from_model_file(mh, i)

Transfer the weights assuming the file containing the weights is a model file, i.e., it represents the entire model not only the weights.

See DLTransferHandler.transfer() for arguments documentation.

Parameters:

i (int) – The transfer step that must be carried out.

transfer_from_weights_file(mh, i)

Transfer the weights assuming the file only contains the weights, i.e., it does not represent the entire model.

See DLTransferHandler.transfer() for arguments documentation.

Parameters:

i (int) – The transfer step that must be carried out.

can_transfer()

Check whether the DLTransferHandler object can transfer weights or not.

Returns:

True if transfer is possible considering given specification, False otherwise.

Return type:

bool

static extract_neural_network_from_model_handler(mh)

Get the neural network from the model handler.

Parameters:

mh (DLModelHandler) – The model handler whose neural network must be obtained.

Returns:

The neural network itself.

static translate_layer_name(lname, translator, default_to_null=False)

Translate the layer name considering the given translator (dictionary whose keys are original layer names and values are the translated layer name, with None for layers that must not be used).

Parameters:
  • lname (str) – The original layer name to be translated, if necessary.

  • translator (dict or None) – The dictionary to translate the original layer name. Keys match original layer names, values give corresponding translated names.

  • default_to_null (bool) – If true, then layer names that don’t have a corresponding key in the translator will be ignored. If false (default), then layer names that don’t have a corresponding key will be considered as the corresponding translated name.

Returns:

The final layer name or None

Return type:

str or None

update_paths(transfers)

Update the paths of the transfer handler from the given specification.

Parameters:

transfers (dict) – The dictionary specifying a transfer handler configuration.

Returns:

Nothing at all, but the member paths are updated.