xfer.NeuralNetworkRandomFreezeRepurposer

class xfer.NeuralNetworkRandomFreezeRepurposer(source_model: mxnet.module.module.Module, target_class_count, fixed_layers, random_layers, num_layers_to_drop=2, context_function=<function cpu>, num_devices=1, batch_size=64, num_epochs=5, optimizer='sgd', optimizer_params=None)

Bases: xfer.neural_network_repurposer.NeuralNetworkRepurposer

Class that creates a target neural network from a source neural network through Transfer Learning. It transfers layers from source model and fine-tunes the network to learn from target data set.

Steps involved:

  • Layers and weights of source model are transferred
  • N layers are removed from the output of the model
  • A fully connected layer with nodes equal to number of classes in target data set is added to the model output
  • A softmax layer is added on top of the new fully connected layer
  • Some layers are frozen and some randomly initialized and then model is fine-tuned
Parameters:
  • source_model (mxnet.mod.Module) – Source neural network to do transfer leaning from. Can be None in a predict-only case
  • target_class_count (int) – Number of classes to train the target neural network for. Can be None in a predict-only case
  • fixed_layers (list[str]) – List of layers to keep weights frozen for
  • random_layers (list[str]) – List of layers to randomly reinitialise
  • num_layers_to_drop (int) – Number of layers to remove from model output
  • context_function (function) – MXNet context function that provides device type context
  • num_devices (int) – Number of devices to use to train target neural network
  • batch_size (int) – Size of data batches to be used for training the target neural network
  • num_epochs (int) – Number of epochs to be used for training the target neural network
  • optimizer (str) – Optimizer required by MXNet to train target neural network. Default: ‘sgd’
  • optimizer_params (dict(str, float)) – Optimizer params required by MXNet to train target neural network. Default: {‘learning_rate’: 1e-3}

Methods

__init__ Initialize self.
deserialize Uses dictionary to set attributes of repurposer.
get_params Get parameters of repurposer that are in the constructor
predict_label Perform predictions on test data using the target_model (repurposed neural network).
predict_probability Perform predictions on test data using the target_model (repurposed neural network).
repurpose Train a neural network by transferring layers/weights from source_model.
save_repurposer Serialize the repurposed model (source_model, target_model and supporting info) and save it to given file_path.
serialize Serialize repurposer to dictionary.
get_params()

Get parameters of repurposer that are in the constructor

Return type:dict
deserialize(input_dict)

Uses dictionary to set attributes of repurposer.

Parameters:input_dict (dict) – Dictionary containing values for attributes to be set to
predict_label(test_iterator: mxnet.io.io.DataIter)

Perform predictions on test data using the target_model (repurposed neural network).

Parameters:test_iterator (mxnet.io.DataIter) – Test data iterator to return predictions for
Returns:Predicted labels
Return type:numpy.ndarray
predict_probability(test_iterator: mxnet.io.io.DataIter)

Perform predictions on test data using the target_model (repurposed neural network).

Parameters:test_iterator (mxnet.io.DataIter) – Test data iterator to return predictions for
Returns:Predicted probabilities
Return type:numpy.ndarray
repurpose(train_iterator: mxnet.io.io.DataIter)

Train a neural network by transferring layers/weights from source_model. Set self.target_model to the repurposed neural network.

Parameters:train_iterator (mxnet.io.DataIter) – Training data iterator to use to extract features from source_model
save_repurposer(model_name, model_directory='', save_source_model=None)

Serialize the repurposed model (source_model, target_model and supporting info) and save it to given file_path.

Parameters:
  • model_name (str) – Name to save repurposer to.
  • model_directory (str) – File directory to save repurposer in.
  • save_source_model (boolean) – Flag to choose whether to save repurposer source model. Will use default if set to None. (MetaModelRepurposer default: True, NeuralNetworkRepurposer default: False)
serialize(file_prefix)

Serialize repurposer to dictionary.

Returns:Dictionary describing repurposer
Return type:dict