Model

Model

class deoxys.model.model.Model(model, model_params=None, train_params=None, data_reader=None, pre_compiled=False, weights_file=None, config=None, sample_data=None)[source]
Parameters:
  • model (tensorflow.keras.models.Model) – a keras model object
  • model_params (dict, optional) – params to compile a keras model, by default None
  • train_params (dict, optional) – params for training, evaluate, predict, by default None
  • data_reader (deoxys.data.DataReader, optional) – A deoxys data reader, by default None
  • pre_compiled (bool, optional) – True if model has been compiled, by default False
  • weights_file (str, optional) – path to h5 file that contains the weights of the keras model, by default None
  • config (dict, optional) – full config to create the model, by default None
Raises:

ValueError – raises error if model_params is defined without optimizer

activation_map(layer_name, images)[source]

Get activation map of a list of images

activation_maximization(layer_name, img=None, step_size=1, epochs=20, filter_index=0, loss_fn=None, verbose=True)[source]

Return the image that maximize the activation output of one or more filters in a specific layer.

Parameters:
  • layer_name (str) – name of the node
  • img ([type], optional) –

    list of initial images, by default None

    If None, a random image with noises will be used.

  • step_size (int, optional) – Size of the step when performing gradient descent, by default 1
  • epochs (int, optional) – Number of epochs for gradient descent, by default 20
  • filter_index (int, or list, optional) – index of the filter to get the gradient, can be any number between 0 and (size of the filters - 1), by default 0
  • loss_fn (callable, optional) – customized loss function, by default None
  • verbose (bool, optional) – By default True
Returns:

list of images that maximize the activation’s filters

Return type:

list

backprop(layer_name, images, mode='max', output_index=0, loss_fn=None)[source]

Return saliency map, or backprop, or gradient map of a list of images

Parameters:
  • layer_name (str) – name of the layer
  • images (list) – list of images
  • mode (str, optional) –

    mode to calculate the loss to use when backpropagation, by default ‘max’, other options are ‘mean’, ‘min’, ‘one’, ‘custom’, and ‘all’.

    ’max’, ‘min’, ‘mean’: calculate the loss by calculating the max, min, or mean over the inner most axis(axis=-1) of the output.

    ’one’: calculate the loss on one index in the inner-most axis(axis=-1) of the output of the layer.

    ’all’: use the output of the layer as the loss score.

    ’custom’: use a custom function to calculate the loss score based on the output of the layer.

  • output_index (int, optional) – use when mode = ‘one’, by default 0
  • loss_fn (callable, optional) – use when mode = ‘custom’, the function to calculate the loss score based on the output of the layer, by default None
Returns:

resulting images when performing backpropagation

Return type:

numpy.array of images

compile(optimizer=None, loss=None, metrics=None, loss_weights=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None, **kwargs)[source]
Raises:Warning – calling this function will recompile the model with new configuration
data_reader

Get the data reader used in this model

deconv(layer_name, images, mode='max', output_index=0, loss_fn=None)[source]
evaluate(*args, **kwargs)[source]

Evaluate model

evaluate_generator(*args, **kwargs)[source]
evaluate_test(**kwargs)[source]

Evaluate model performance using test data

evaluate_train(**kwargs)[source]

Evaluate the model on the training data

evaluate_val(**kwargs)[source]

Evaluate model’s performance using validation data

fit(*args, **kwargs)[source]

Train model

fit_generator(*args, **kwargs)[source]
fit_train(**kwargs)[source]

Train the model with training data

guided_backprop(layer_name, images, mode='max', output_index=0, loss_fn=None)[source]
is_compiled
layers

Get the dictionary of layers in the model

Returns:dictionary of layers
Return type:dict
max_filter(layer_name, images)[source]

Return a list of images in which each pixel value is the index of the filter having the max value in the activation map.

model

Return the keras model

node_graph

Node graph from nodes in model, ignoring resize and concatenate nodes

predict(*args, **kwargs)[source]

Predict model

predict_generator(*args, **kwargs)[source]
predict_test(**kwargs)[source]

Predict test data

predict_test_generator(**kwargs)[source]

Predict test data

predict_val(**kwargs)[source]

Predict validation data

predict_val_generator(**kwargs)[source]

Predict validation data

save(filename, *args, **kwargs)[source]

Save model to file

Parameters:filename (str) – name of the file
sub_model(layer_name)[source]

Create a sub-model with the same inputs, and the outputs of a specific layer in the deoxys model.

Parameters:layer_name (str) – name of layer
Returns:Model, whose outputs are of the layer_name
Return type:tensorflow.keras.models.Model
deoxys.model.model.load_model(filename, **kwargs)[source]

Load model from file

Parameters:filename (str) – path to the h5 file
Returns:The loaded model
Return type:deoxys.model.Model
deoxys.model.model.model_from_config(architecture, input_params, model_params=None, train_params=None, dataset_params=None, weights_file=None, sample_data=None, **kwargs)[source]
deoxys.model.model.model_from_full_config(model_config, weights_file=None, **kwargs)[source]

[summary]

Parameters:
  • model_config (str or dict) – a JSON string or a dictionary contains the architecture, model_params, input_params configuration of the model
  • weights_file (str, optional) – path to the saved weight file, by default None
Returns:

The model

Return type:

deoxys.model.Model

Raises:

ValueError – When architecture or input_params are missing

deoxys.model.model.model_from_keras_config(config, **kwarg)[source]
deoxys.model.model.model_from_keras_json(json, **kwarg)[source]

Layers

class deoxys.model.layers.Layers[source]

A singleton that contains all the registered customized layers

layers
register(key, layer)[source]
unregister(key)[source]
deoxys.model.layers.layer_from_config(config)[source]
deoxys.model.layers.register_layer(key, layer)[source]

Register the customized layer. If the key name is already registered, it will raise a KeyError exception

Parameters:
  • key (str) – The unique key-name of the layer
  • layer (tensorflow.keras.layers.Layer) – The customized layer class
deoxys.model.layers.unregister_layer(key)[source]

Remove the registered layer with the key-name

Parameters:key (str) – The key-name of the layer to be removed

Activations

class deoxys.model.activations.Activations[source]

A singleton that contains all the registered customized activations

activations
register(key, activation)[source]
unregister(key)[source]
deoxys.model.activations.activation_from_config(config)[source]
deoxys.model.activations.register_activation(key, activation)[source]

Register the customized activation. If the key name is already registered, it will raise a KeyError exception

Parameters:
  • key (str) – The unique key-name of the activation
  • activation (tensorflow.keras.activations.Activation) – The customized activation class
deoxys.model.activations.unregister_activation(key)[source]

Remove the registered activation with the key-name

Parameters:key (str) – The key-name of the activation to be removed

Losses

class deoxys.model.losses.BinaryFbetaLoss(reduction='auto', name='binary_fbeta', beta=1)[source]
call(target, prediction)[source]

Invokes the Loss instance.

Parameters:
  • y_true – Ground truth values. shape = [batch_size, d0, .. dN], except sparse loss functions such as sparse categorical crossentropy where shape = [batch_size, d0, .. dN-1]
  • y_pred – The predicted values. shape = [batch_size, d0, .. dN]
Returns:

Loss values with the shape [batch_size, d0, .. dN-1].

class deoxys.model.losses.Losses[source]

A singleton that contains all the registered customized losses

losses
register(key, loss)[source]
unregister(key)[source]
class deoxys.model.losses.ModifiedDiceLoss(reduction='auto', name='modified_dice_loss', beta=1)[source]
call(target, prediction)[source]

Invokes the Loss instance.

Parameters:
  • y_true – Ground truth values. shape = [batch_size, d0, .. dN], except sparse loss functions such as sparse categorical crossentropy where shape = [batch_size, d0, .. dN-1]
  • y_pred – The predicted values. shape = [batch_size, d0, .. dN]
Returns:

Loss values with the shape [batch_size, d0, .. dN-1].

deoxys.model.losses.loss_from_config(config)[source]
deoxys.model.losses.register_loss(key, loss)[source]

Register the customized loss. If the key name is already registered, it will raise a KeyError exception

Parameters:
  • key (str) – The unique key-name of the loss
  • loss (tensorflow.keras.losses.Loss) – The customized loss class
deoxys.model.losses.unregister_loss(key)[source]

Remove the registered loss with the key-name

Parameters:key (str) – The key-name of the loss to be removed

Metrics

class deoxys.model.metrics.BinaryFbeta(thresholds=None, name='BinaryFbeta', dtype=None, beta=1)[source]

Calculate the micro f1 score in the set of data

get_config()[source]

Returns the serializable config of the metric.

result()[source]

Computes and returns the metric value tensor.

Result computation is an idempotent operation that simply calculates the metric value using the state variables.

update_state(y_true, y_pred, sample_weight=None)[source]

Accumulates the metric statistics.

Parameters:
  • y_true – The ground truth values.
  • y_pred – The predicted values.
  • sample_weight – Optional weighting of each example. Defaults to 1. Can be a Tensor whose rank is either 0, or the same rank as y_true, and must be broadcastable to y_true.
Returns:

Update op.

class deoxys.model.metrics.Dice(threshold=None, name='dice', dtype=None, beta=1)[source]
get_config()[source]

Returns the serializable config of the metric.

result()[source]

Computes and returns the metric value tensor.

Result computation is an idempotent operation that simply calculates the metric value using the state variables.

update_state(y_true, y_pred, sample_weight=None)[source]

Accumulates statistics for the metric.

Note: This function is executed as a graph function in graph mode. This means:

  1. Operations on the same resource are executed in textual order. This should make it easier to do things like add the updated value of a variable to another, for example.
  2. You don’t need to worry about collecting the update ops to execute. All update ops added to the graph by this function will be executed.

As a result, code should generally work the same way with graph or eager execution.

Parameters:
  • *args
  • **kwargs – A mini-batch of inputs to the Metric.
class deoxys.model.metrics.Fbeta(threshold=None, name='Fbeta', dtype=None, beta=1)[source]
get_config()[source]

Returns the serializable config of the metric.

result()[source]

Computes and returns the metric value tensor.

Result computation is an idempotent operation that simply calculates the metric value using the state variables.

update_state(y_true, y_pred, sample_weight=None)[source]

Accumulates statistics for the metric.

Note: This function is executed as a graph function in graph mode. This means:

  1. Operations on the same resource are executed in textual order. This should make it easier to do things like add the updated value of a variable to another, for example.
  2. You don’t need to worry about collecting the update ops to execute. All update ops added to the graph by this function will be executed.

As a result, code should generally work the same way with graph or eager execution.

Parameters:
  • *args
  • **kwargs – A mini-batch of inputs to the Metric.
class deoxys.model.metrics.Metrics[source]

A singleton that contains all the registered customized metrics

metrics
register(key, metric)[source]
unregister(key)[source]
deoxys.model.metrics.metric_from_config(config)[source]
deoxys.model.metrics.register_metric(key, metric)[source]

Register the customized metric. If the key name is already registered, it will raise a KeyError exception

Parameters:
  • key (str) – The unique key-name of the metric
  • loss (tensorflow.keras.metrics.Metric) – The customized metric class
deoxys.model.metrics.unregister_metric(key)[source]

Remove the registered metric with the key-name

Parameters:key (str) – The key-name of the metric to be removed

Optimizers

class deoxys.model.optimizers.Optimizers[source]

A singleton that contains all the registered customized optimizers

optimizers
register(key, optimizer)[source]
unregister(key)[source]
deoxys.model.optimizers.optimizer_from_config(config)[source]
deoxys.model.optimizers.register_optimizer(key, optimizer)[source]

Register the customized optimizer. If the key name is already registered, it will raise a KeyError exception

Parameters:
  • key (str) – The unique key-name of the optimizer
  • optimizer (tensorflow.keras.optimizers.Optimizer) – The customized optimizer class
deoxys.model.optimizers.unregister_optimizer(key)[source]

Remove the registered optimizer with the key-name

Parameters:key (str) – The key-name of the optimizer to be removed

Callbacks

class deoxys.model.callbacks.Callbacks[source]

A singleton that contains all the registered customized callbacks

callbacks
register(key, callback)[source]
unregister(key)[source]
class deoxys.model.callbacks.DBLogger(dbclient, session)[source]
on_epoch_end(epoch, logs=None)[source]

Called at the end of an epoch.

Subclasses should override for any actions to run. This function should only be called during TRAIN mode.

Parameters:
  • epoch – Integer, index of epoch.
  • logs
    Dict, metric results for this training epoch, and for the
    validation epoch if validation is performed. Validation result keys are prefixed with val_. For training epoch, the values of the
    Model’s metrics are returned. Example : `{‘loss’: 0.2, ‘accuracy’:
    0.7}`.
class deoxys.model.callbacks.DeoxysModelCallback(*args, **kwargs)[source]
set_deoxys_model(deoxys_model)[source]
class deoxys.model.callbacks.DeoxysModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1, dbclient=None, session=None)[source]
on_epoch_end(epoch, logs=None)[source]

Called at the end of an epoch.

Subclasses should override for any actions to run. This function should only be called during TRAIN mode.

Parameters:
  • epoch – Integer, index of epoch.
  • logs
    Dict, metric results for this training epoch, and for the
    validation epoch if validation is performed. Validation result keys are prefixed with val_. For training epoch, the values of the
    Model’s metrics are returned. Example : `{‘loss’: 0.2, ‘accuracy’:
    0.7}`.
class deoxys.model.callbacks.EvaluationCheckpoint(filename=None, period=1, separator=', ', append=False)[source]

Evaluate test after some epochs. Only use when cross validation to avoid data leakage.

on_epoch_end(epoch, logs=None)[source]

Called at the end of an epoch.

Subclasses should override for any actions to run. This function should only be called during TRAIN mode.

Parameters:
  • epoch – Integer, index of epoch.
  • logs
    Dict, metric results for this training epoch, and for the
    validation epoch if validation is performed. Validation result keys are prefixed with val_. For training epoch, the values of the
    Model’s metrics are returned. Example : `{‘loss’: 0.2, ‘accuracy’:
    0.7}`.
on_train_begin(logs=None)[source]

Called at the beginning of training.

Subclasses should override for any actions to run.

Parameters:logs – Dict. Currently no data is passed to this argument for this method but that may change in the future.
class deoxys.model.callbacks.PredictionCheckpoint(filepath=None, period=1, use_original=False, dbclient=None, session=None)[source]

Predict test in every number of epochs

data_information
on_epoch_end(epoch, logs=None)[source]

Called at the end of an epoch.

Subclasses should override for any actions to run. This function should only be called during TRAIN mode.

Parameters:
  • epoch – Integer, index of epoch.
  • logs
    Dict, metric results for this training epoch, and for the
    validation epoch if validation is performed. Validation result keys are prefixed with val_. For training epoch, the values of the
    Model’s metrics are returned. Example : `{‘loss’: 0.2, ‘accuracy’:
    0.7}`.
deoxys.model.callbacks.callback_from_config(config)[source]
deoxys.model.callbacks.register_callback(key, callback)[source]

Register the customized callback. If the key name is already registered, it will raise a KeyError exception

Parameters:
  • key (str) – The unique key-name of the callback
  • callback (tensorflow.keras.callbacks.Callback) – the customized callback class
deoxys.model.callbacks.unregister_callback(key)[source]

Remove the registered callback with the key-name

Parameters:key (str) – The key-name of the callback to be removed