Additional model setup (e.g. Early Stopping)

The models.modelsetup module containes functionality for preparing for training a model

soundpy.models.modelsetup.setup_layers(num_features, num_layers, kernel_shape=3, 3, max_feature_map=64)[source]

Sets up feature_maps and kernels for 1 or more layered convolutional neural networks.

Parameters
  • num_features (int) – The number of features used to train the model. This will be used to set the number of feature_maps for each layer.

  • num_layers (int) – The number of layers desired

  • kernel_shape (tuple or int) – The shape of the desired kernel

  • max_feature_map (int) – The maximum size of feature map / filter. This depends on the system and is relevant for processing higher definition features, such as STFT features. If this is set too large given memory restraints, training may be ‘killed’.

Returns

  • feature_maps (list) – List of feature maps or filters that will be applied to each layer of the network.

  • kernels (list) – List of kernels that will be applied to each layer of the network. Matches length of feature_maps

Warning

If num_features is larger than the max_feature_map. The num_features is usually used to set the first feature map, but if too large, will be reduced to be lower than max_feature_map.

soundpy.models.modelsetup.setup_callbacks(early_stop=True, patience=15, log=True, log_filename=None, append=True, save_bestmodel=True, best_modelname=None, monitor='val_loss', verbose=1, save_best_only=True, mode='min', tensorboard=True, write_images=False, x_test=None, y_test=None, batch_size=None, embedded_layer_name=None)[source]

Easy set up of early stopping, model logging, and saving best model.

Parameters
  • early_stop (bool) – Whether or not the model should stop if training is not improving (default True)

  • patience (int) – The number of epochs the model should complete without improvement before stopping training. (default 15)

  • log (bool) – If true, the accuracy, loss, and (if possible) the val_accuracy and val_loss for each epoch will be saved in a .csv file. (default True)

  • log_filename (str or pathlib.PosixPath, optional) – The filename the logging information will be stored. If None, the date will be used as a unique .csv filename in a subfolder ‘model_logs’ in the local directory.

  • save_bestmodel (bool) – If True, the best performing model will be saved.

  • best_modelname (str or pathlib.PosixPath) – The name to save the best model version under. If None, the date will be used to create a unique .h5 filename and it will be saved in a subfolder ‘best_models’ in the local directory.

  • monitor (str) – The metric to be used to measure model performance. (default ‘val_loss’

  • verbose (bool) – If True, the state of the model will be printed. (default True)

  • save_best_only (bool) – If True, the best performing model will overwrite any previously saved ‘best model’.

  • mode (str) – If monitor is set to ‘val_loss’, this should be set to ‘min’. If monitor``is set to ‘val_acc’, this should be set to ‘max’. If `mode is set to ‘auto’, the direction will be inferred. (default ‘min’)

  • tensorboard (bool) – If True, logs for TensorBoard will be made.

Returns

callbacks – The callbacks ready to be applied to Keras model training.

Return type

# TODO what data type is this?