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_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
-
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
-
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
Layers¶
-
class
deoxys.model.layers.Layers[source]¶ A singleton that contains all the registered customized layers
-
layers¶
-
Activations¶
-
class
deoxys.model.activations.Activations[source]¶ A singleton that contains all the registered customized activations
-
activations¶
-
-
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
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¶
-
-
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].
-
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
-
reset_state()[source]¶ Resets all of the metric state variables.
This function is called between epochs/steps, when a metric is evaluated during training.
-
result()[source]¶ Computes and returns the scalar metric value tensor or a dict of scalars.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
Returns: A scalar tensor, or a dictionary of scalar tensors.
-
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:
- 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.
- 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.Dice(threshold=None, name='dice', dtype=None, beta=1)[source]¶ -
-
result()[source]¶ Computes and returns the scalar metric value tensor or a dict of scalars.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
Returns: A scalar tensor, or a dictionary of scalar tensors.
-
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:
- 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.
- 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]¶ -
-
result()[source]¶ Computes and returns the scalar metric value tensor or a dict of scalars.
Result computation is an idempotent operation that simply calculates the metric value using the state variables.
Returns: A scalar tensor, or a dictionary of scalar tensors.
-
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:
- 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.
- 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¶
-
Optimizers¶
-
class
deoxys.model.optimizers.Optimizers[source]¶ A singleton that contains all the registered customized optimizers
-
optimizers¶
-
-
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
Callbacks¶
-
class
deoxys.model.callbacks.Callbacks[source]¶ A singleton that contains all the registered customized callbacks
-
callbacks¶
-
-
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.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}.
-
-
class
deoxys.model.callbacks.PredictionCheckpoint(filepath=None, period=1, use_original=False, save_inputs=True, 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.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