secml.ml.classifiers.reject

CClassifierReject

class secml.ml.classifiers.reject.c_classifier_reject.CClassifierReject(preprocess=None, n_jobs=1)[source]

Bases: secml.ml.classifiers.c_classifier.CClassifier

Abstract class that defines basic methods for Classifiers with reject.

A classifier assign a label (class) to new patterns using the information learned from training set.

This interface implements a set of generic methods for training and classification that can be used for every algorithms. However, all of them can be reimplemented if specific routines are needed.

Parameters
preprocessstr or CNormalizer

Features preprocess to applied to input data. Can be a CNormalizer subclass or a string with the desired preprocess type. If None, input data is used as is.

Attributes
class_type

Defines class type.

classes

Return the list of classes on which training has been performed.

logger

Logger for current object.

n_classes

Number of classes of training dataset.

n_features

Number of features (before preprocessing).

n_jobs
preprocess

Inner preprocessor (if any).

verbose

Verbosity level of logger output.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

create_chain(class_items, kwargs_list)

Creates a chain of preprocessors.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, x, y)

Trains the classifier.

fit_forward(self, x[, y, caching])

Fit estimator using data and then execute forward on the data.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class hyperparameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function, …])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

abstract predict(self, x, return_decision_function=False, n_jobs=1)[source]

Perform classification of each pattern in x.

If a preprocess has been specified, input is normalized before classification.

Parameters
xCArray

Array with new patterns to classify, 2-Dimensional of shape (n_patterns, n_features).

return_decision_functionbool, optional

Whether to return the decision_function value along with predictions. Default False.

n_jobsint, optional

Number of parallel workers to use for classification. Default 1. Cannot be higher than processor’s number of cores.

Returns
labelsCArray

Flat dense array of shape (n_patterns,) with the label assigned to each test pattern. The classification label is the label of the class associated with the highest score. The rejected samples have label -1.

scoresCArray, optional

Array of shape (n_patterns, n_classes) with classification score of each test pattern with respect to each training class. Will be returned only if return_decision_function is True.

CClassifierRejectThreshold

class secml.ml.classifiers.reject.c_classifier_reject_threshold.CClassifierRejectThreshold(clf, threshold, preprocess=None)[source]

Bases: secml.ml.classifiers.reject.c_classifier_reject.CClassifierReject

Abstract class that defines basic methods for Classifiers with reject

based on a certain threshold.

A classifier assign a label (class) to new patterns using the information learned from training set.

The samples for which the higher score is under a certain threshold are rejected by the classifier.

Parameters
clfCClassifier

Classifier to which we would like to apply a reject threshold. The classifier can also be already fitted.

thresholdfloat

Rejection threshold.

preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
class_type

Defines class type.

classes

Return the list of classes on which training has been performed.

clf

Returns the inner classifier.

logger

Logger for current object.

n_classes

Number of classes of training dataset, plus the rejection class.

n_features

Number of features (before preprocessing).

n_jobs
preprocess

Inner preprocessor (if any).

threshold

Returns the rejection threshold.

verbose

Verbosity level of logger output.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

compute_threshold(self, rej_percent, ds)

Compute the threshold that must be set in the classifier to have rej_percent rejection rate (accordingly to an estimation on a validation set).

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

create_chain(class_items, kwargs_list)

Creates a chain of preprocessors.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, x, y)

Trains the classifier.

fit_forward(self, x[, y, caching])

Fit estimator using data and then execute forward on the data.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class hyperparameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function, …])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

property classes

Return the list of classes on which training has been performed.

property clf

Returns the inner classifier.

compute_threshold(self, rej_percent, ds)[source]

Compute the threshold that must be set in the classifier to have rej_percent rejection rate (accordingly to an estimation on a validation set).

Parameters
rej_percentfloat

Max percentage of rejected samples.

dsCDataset

Dataset on which the threshold is estimated.

Returns
thresholdfloat

The estimated reject threshold

property n_classes

Number of classes of training dataset, plus the rejection class.

predict(self, x, return_decision_function=False, n_jobs=<no value>)[source]

Perform classification of each pattern in x.

The score matrix of this classifier is equal to the predicted outputs plus a column (corresponding to the reject class) with all its values equal to \theta, being \theta the reject threshold.

The predicted class is therefore:

c = \operatorname*{argmax}_k f_k(x)

where c correspond to the rejection class (i.e., c=-1) only when the maximum taken over the other classes (excluding the reject one) is not greater than the reject threshold \theta.

If a preprocess has been specified, input is normalized before classification.

Parameters
xCArray

Array with new patterns to classify, 2-Dimensional of shape (n_patterns, n_features).

return_decision_functionbool, optional

Whether to return the decision_function value along with predictions. Default False.

n_jobsint, optional

Number of parallel workers to use for classification. Default _NoValue. Cannot be higher than processor’s number of cores.

Returns
labelsCArray

Flat dense array of shape (n_patterns,) with the label assigned to each test pattern. The classification label is the label of the class associated with the highest score. The samples for which the label is equal -1 are the ones rejected by the classifier

scoresCArray, optional

Array of shape (n_patterns, n_classes) with classification score of each test pattern with respect to each training class. Will be returned only if return_decision_function is True.

property threshold

Returns the rejection threshold.

CClassifierDNR

class secml.ml.classifiers.reject.c_classifier_dnr.CClassifierDNR(combiner, layer_clf, dnn, layers, threshold, n_jobs=1)[source]

Bases: secml.ml.classifiers.reject.c_classifier_reject_threshold.CClassifierRejectThreshold

Deep Neural Rejection (DNR) Classifier. It is composed by a wrapped DNN, one or more layer classifiers trained on inner DNN layer outputs and a combiner trained on layer classifiers scores.

DNR analyzes the representations of input samples at different network layers, and rejects samples which exhibit anomalous behavior with respect to that observed from the training data at such layers.

More details can be found in tutorials/12-DNR.ipynb and in:
Parameters
combinerCClassifier

The output classifier of DNR. It is trained on layer classifier scores. Its output is thresholded in order to reject samples.

layer_clfCClassifier or dict

Layer classifier, trained on DNN inner layers outputs. If CClassifier, it is cloned for each selected layer. If dict, it must contain an item for each selected layer as {‘layer_name’: CClassifier}.

dnnCClassifierDNN

An already trained DNN to be defended.

layerslist of str

Name of one or more DNN layers which outputs will be used to train layer classifiers.

thresholdfloat

The reject threshold applied to the combiner outputs. If the maximum class score of the combiner is lower than the threshold, the sample is rejected.

n_jobsint, optional

Number of parallel workers to use for training the classifier. Cannot be higher than processor’s number of cores. Default is 1.

Attributes
class_type

Defines class type.

classes

Return the list of classes on which training has been performed.

clf

Returns the inner classifier.

logger

Logger for current object.

n_classes

Number of classes of training dataset, plus the rejection class.

n_features

Number of features (before preprocessing).

n_jobs
preprocess

Inner preprocessor (if any).

threshold

Returns the rejection threshold.

verbose

Verbosity level of logger output.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

compute_threshold(self, rej_percent, ds)

Compute the threshold that must be set in the classifier to have rej_percent rejection rate (accordingly to an estimation on a validation set).

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

create_chain(class_items, kwargs_list)

Creates a chain of preprocessors.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, x, y)

Trains the classifier.

fit_forward(self, x[, y, caching])

Fit estimator using data and then execute forward on the data.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class hyperparameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function, …])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.