def test_aspreprocess(self):
        """Test for normalizer used as preprocess."""
        from secml.ml.classifiers import CClassifierSVM
        from secml.ml.classifiers.multiclass import CClassifierMulticlassOVA

        model = mlp(input_dims=20, hidden_dims=(40, ), output_dims=3)
        loss = nn.CrossEntropyLoss()
        optimizer = optim.SGD(model.parameters(), lr=1e-1)
        net = CClassifierPyTorch(model=model,
                                 loss=loss,
                                 optimizer=optimizer,
                                 random_state=0,
                                 epochs=10,
                                 preprocess='min-max')
        net.fit(self.ds)

        norm = CNormalizerDNN(net=net)

        clf = CClassifierMulticlassOVA(classifier=CClassifierSVM,
                                       preprocess=norm)

        self.logger.info("Testing last layer")

        clf.fit(self.ds)

        y_pred, scores = clf.predict(self.ds.X, return_decision_function=True)
        self.logger.info("TRUE:\n{:}".format(self.ds.Y.tolist()))
        self.logger.info("Predictions:\n{:}".format(y_pred.tolist()))
        self.logger.info("Scores:\n{:}".format(scores))

        x = self.ds.X[0, :]

        self.logger.info("Testing last layer gradient")

        for c in self.ds.classes:
            self.logger.info("Gradient w.r.t. class {:}".format(c))

            grad = clf.grad_f_x(x, y=c)

            self.logger.info("Output of grad_f_x:\n{:}".format(grad))

            check_grad_val = CFunction(clf.decision_function,
                                       clf.grad_f_x).check_grad(x,
                                                                y=c,
                                                                epsilon=1e-1)
            self.logger.info("norm(grad - num_grad): %s", str(check_grad_val))
            self.assertLess(check_grad_val, 1e-3)

            self.assertTrue(grad.is_vector_like)
            self.assertEqual(x.size, grad.size)

        layer = 'linear1'
        norm.out_layer = layer

        self.logger.info("Testing layer {:}".format(norm.out_layer))

        clf.fit(self.ds)

        y_pred, scores = clf.predict(self.ds.X, return_decision_function=True)
        self.logger.info("TRUE:\n{:}".format(self.ds.Y.tolist()))
        self.logger.info("Predictions:\n{:}".format(y_pred.tolist()))
        self.logger.info("Scores:\n{:}".format(scores))

        self.logger.info("Testing 'linear1' layer gradient")
        grad = clf.grad_f_x(x, y=0)  # y is required for multiclassova
        self.logger.info("Output of grad_f_x:\n{:}".format(grad))

        self.assertTrue(grad.is_vector_like)
        self.assertEqual(x.size, grad.size)
def create_SVM(**kwargs):
    print("in function, args:")
    print(kwargs)
    return CClassifierMulticlassOVA(CClassifierSVM, **kwargs)
コード例 #3
0
te_X, te_Y = CArray(te_X), CArray(te_Y)

ds_tr_secml = CDataset(tr_X, tr_Y)
#print(ds_tr_secml.classes, ds_tr_secml.num_classes, ds_tr_secml.num_features, ds_tr_secml.num_samples)
ds_te_secml = CDataset(te_X, te_Y)
ds_cv_secml = CDataset(cv_X, cv_Y)

normalizer = CNormalizerMinMax()
ds_tr_secml.X = normalizer.fit_transform(ds_tr_secml.X)
ds_te_secml.X = normalizer.transform(ds_te_secml.X)
ds_cv_secml.X = normalizer.transform(ds_cv_secml.X)

x0, y0 = ds_te_secml[0, :].X, ds_te_secml[0, :].Y

secml_clf = CClassifierMulticlassOVA(CClassifierSVM,
                                     kernel=CKernelRBF(gamma=10),
                                     C=1)
secml_clf.fit(ds_tr_secml)
preds = secml_clf.predict(ds_te_secml.X)
metric = CMetricAccuracy()
acc = metric.performance_score(y_true=ds_te_secml.Y, y_pred=preds)
print("Accuracy on test set: {:.2%}".format(acc))

#Performing the attack
noise_type = 'l2'
dmax = 1
lb, ub = None, None  # with 0, 1 it goes out of bounds
y_target = None  #### Here y_target can be some class, indicating which class is expected for the adversarial example

x0, y0 = ds_te_secml[0, :].X, ds_te_secml[0, :].Y
from secml.ml.classifiers import CClassifierSVM
from secml.ml.classifiers.multiclass import CClassifierMulticlassOVA
from secml.ml.classifiers import CClassifierDecisionTree
from secml.ml.classifiers.c_classifier_logistic import CClassifierLogistic
from secml.ml.kernel import CKernelRBF
from secml.ml.kernel import CKernelLinear

clf = CClassifierMulticlassOVA(CClassifierSVM, kernel=CKernelRBF())

import sys


def create_SVM(**kwargs):
    print("in function, args:")
    print(kwargs)
    return CClassifierMulticlassOVA(CClassifierSVM, **kwargs)


def create_decision_tree(**kwargs):
    print("in function, args:")
    print(kwargs)
    return CClassifierDecisionTree(**kwargs)


def create_linear_model(**kwargs):
    return CClassifierLogistic(**kwargs)


def create_classif(clf_algo, **kwargs):
    if (clf_algo == "SVM"):
        clf = create_SVM(**kwargs)
コード例 #5
0
    def test_plot_decision_function(self):
        """Test plot of multiclass classifier decision function."""
        # generate synthetic data
        ds = CDLRandom(n_classes=3,
                       n_features=2,
                       n_redundant=0,
                       n_clusters_per_class=1,
                       class_sep=1,
                       random_state=0).load()

        multiclass = CClassifierMulticlassOVA(classifier=CClassifierSVM,
                                              class_weight='balanced',
                                              preprocess='min-max')

        # Training and classification
        multiclass.fit(ds.X, ds.Y)
        y_pred, score_pred = multiclass.predict(ds.X,
                                                return_decision_function=True)

        def plot_hyperplane(img, clf, min_v, max_v, linestyle, label):
            """Plot the hyperplane associated to the OVA clf."""
            xx = CArray.linspace(min_v - 5, max_v +
                                 5)  # make sure the line is long enough
            # get the separating hyperplane
            yy = -(clf.w[0] * xx + clf.b) / clf.w[1]
            img.sp.plot(xx, yy.ravel(), linestyle, label=label)

        fig = CFigure(height=7, width=8)
        fig.sp.title('{:} ({:})'.format(multiclass.__class__.__name__,
                                        multiclass.classifier.__name__))

        x_bounds, y_bounds = ds.get_bounds()

        styles = ['go-', 'yp--', 'rs-.', 'bD--', 'c-.', 'm-', 'y-.']

        for c_idx, c in enumerate(ds.classes):
            # Plot boundary and predicted label for each OVA classifier

            plot_hyperplane(fig, multiclass._binary_classifiers[c_idx],
                            x_bounds[0], x_bounds[1], styles[c_idx],
                            'Boundary\nfor class {:}'.format(c))

            fig.sp.scatter(ds.X[ds.Y == c, 0],
                           ds.X[ds.Y == c, 1],
                           s=40,
                           c=styles[c_idx][0])
            fig.sp.scatter(ds.X[y_pred == c, 0],
                           ds.X[y_pred == c, 1],
                           s=160,
                           edgecolors=styles[c_idx][0],
                           facecolors='none',
                           linewidths=2)

        # Plotting multiclass decision function
        fig.sp.plot_decision_regions(multiclass,
                                     n_grid_points=100,
                                     grid_limits=ds.get_bounds(offset=5))

        fig.sp.xlim(x_bounds[0] - .5 * x_bounds[1],
                    x_bounds[1] + .5 * x_bounds[1])
        fig.sp.ylim(y_bounds[0] - .5 * y_bounds[1],
                    y_bounds[1] + .5 * y_bounds[1])

        fig.sp.legend(loc=4)  # lower, right

        fig.show()