bfgn.experiments package

Submodules

bfgn.experiments.callbacks module

class bfgn.experiments.callbacks.HistoryCheckpoint(config, existing_history=None, period=1, verbose=0)[source]

Bases: keras.callbacks.Callback

A custom Keras callback for checkpointing model training history and associated information.

config = None
existing_history = None
period = None
verbose = None
epochs_since_last_save = None
epoch_begin = None
on_train_begin(logs=None)[source]
on_train_end(logs=None)[source]
on_epoch_begin(epoch, logs=None)[source]
on_epoch_end(epoch, logs=None)[source]
bfgn.experiments.callbacks.get_model_callbacks(config, existing_history)[source]

Creates model callbacks from a bfgn config.

Parameters
  • config (Config) – bfgn config.

  • existing_history (dict) – Existing model training history if the model has already been partially or completely trained.

Return type

List[Callback]

Returns

List of model callbacks.

bfgn.experiments.experiments module

class bfgn.experiments.experiments.Experiment(config)[source]

Bases: object

model = None

Keras model object.

Type

keras.models.Model

history = None

Model training history.

Type

dict

loaded_existing_history = None

Whether an existing history object was loaded from the model training directory.

Type

bool

loaded_existing_model = None

Whether an existing model object was loaded from the model training directory.

Type

bool

is_model_trained = None

Whether model is trained to stopping criteria. If False, either not trained at all or training was stopped before stopping criteria.

Type

bool

config = None

bfgn configuration object.

Type

configs.Config

logger = None

Root logger for Experiment. Available if user wants to directly modify the log formatting, handling, or other behavior.

Type

logging.Logger

build_or_load_model(data_container=None, num_features=None)[source]
Return type

None

copy_model_from_experiment(reference_config)[source]
Return type

None

fit_model_with_data_container(data_container, resume_training=False)[source]
Return type

None

fit_model_with_sequences(training_sequence, validation_sequence=None, resume_training=False)[source]
Return type

None

calculate_model_memory_footprint(batch_size)[source]

Calculate model memory footprint. Shamelessly copied from (but not tested rigorously): https://stackoverflow.com/questions/43137288/how-to-determine-needed-memory-of-keras-model. :type batch_size: int :param batch_size: Batch size for training.

Return type

float

Returns

Model memory footprint in gigabytes.

bfgn.experiments.experiments.load_experiment_from_directory(dir_experiment)[source]
Return type

Experiment

bfgn.experiments.experiments.get_config_filepath(config)[source]

Get the default config path for experiments.

Parameters

config (Config) – bfgn config object.

Return type

str

Returns

Filepath to model training config.

bfgn.experiments.experiments.get_history_filepath(config)[source]

Get the default model training history path for experiments.

Parameters

config (Config) – bfgn config object.

Return type

str

Returns

Filepath to model training history.

bfgn.experiments.experiments.get_log_filepath(config)[source]

Get the default model training log path for experiments.

Parameters

config (Config) – bfgn config object.

Return type

str

Returns

Filepath to model training log.

bfgn.experiments.experiments.get_model_filepath(config)[source]

Get the default model path for experiments.

Parameters

config (Config) – bfgn config object.

Return type

str

Returns

Filepath to model.

bfgn.experiments.histories module

bfgn.experiments.histories.load_history(filepath)[source]

Loads model training history from serialized file.

Parameters

filepath (str) – Filepath from which history is loaded.

Return type

Optional[dict]

Returns

History object if it exists at path.

bfgn.experiments.histories.save_history(history, filepath)[source]

Saves model training history to serialized file

Parameters
  • history (dict) – Model training history object.

  • filepath (str) – Filepath to which history is saved.

Return type

None

Returns

None.

bfgn.experiments.histories.combine_histories(existing_history, new_history)[source]

Combines model training history objects, such that the new history from a more recent training session is appended to the existing history from a previous training session.

Parameters
  • existing_history (dict) – Model training history from a previous training session.

  • new_history (dict) – Model training history from a more recent training session.

Return type

dict

Returns

Combined model training history.

bfgn.experiments.losses module

bfgn.experiments.losses.get_available_loss_methods()[source]

Gets list of available loss methods.

Returns

List of available loss methods.

bfgn.experiments.losses.get_cropped_loss_function(loss_method, outer_width, inner_width, weighted=True)[source]

Creates a loss function callable with optional per-pixel weighting and edge-trimming.

Parameters
  • loss_method (str) – The loss calculate to implement, currently supports categorical_crossentropy or cc,

  • or mae, mean_squared_error or mse, and root_mean_squared_error or rmse. (mean_absolute_error) –

  • outer_width (int) – The full dimension (height or width) of the input image; e.g., 128 for a 128x128 image.

  • inner_width (int) – The full dimension (height or width) of the loss window to use. Must not be greater than 128 for a

  • image, with generally better results at 25% to 50% of the full image size; i.e., 32 to 64 for a 128x128 (128x128) –

  • image.

  • weighted (bool) – Whether the training response array has weights appended to the last axis to use in loss calculations.

  • does not have a simple way to pass weight values to loss functions, so a common work-around is to append (Keras) –

  • values to the sample responses and reference those weights in the loss functions directly. The bfgn (weight) –

  • will automatically build and append weights if the configuration specifieds that the loss function (package) –

  • be weighted. (should) –

Return type

Callable

Returns

Loss function callable to be passed to a Keras model.

bfgn.experiments.models module

bfgn.experiments.models.load_model(filepath, custom_objects=None)[source]

Loads model from serialized file.

Parameters
  • filepath (str) – Filepath from which model is loaded.

  • custom_objects (Optional[dict]) – Custom objects necessary to build model, including loss functions.

Return type

Optional[Model]

Returns

Keras model object if it exists at path.

bfgn.experiments.models.save_model(model, filepath)[source]

Saves model to serialized file.

Parameters
  • model (Model) – Keras model object.

  • filepath (str) – Filepath to which model is saved.

Return type

None

Returns

None.

Module contents