def _get_image_classifier_mx_instance(from_logits=True): if from_logits is False: # due to the fact that only 1 instance of get_image_classifier_mx_model can be created in one session # this will be resolved once Mxnet allows for 2 models with identical weights to be created in 1 session raise ARTTestFixtureNotImplemented( "Currently only supporting Mxnet classifier with from_logit set to True", get_image_classifier_mx_instance.__name__, framework, ) loss = mxnet.gluon.loss.SoftmaxCrossEntropyLoss(from_logits=from_logits) trainer = mxnet.gluon.Trainer(model.collect_params(), "sgd", {"learning_rate": 0.1}) # Get classifier mxc = MXClassifier( model=model, loss=loss, input_shape=mnist_shape, # input_shape=(28, 28, 1), nb_classes=10, optimizer=trainer, ctx=None, channels_first=True, clip_values=(0, 1), preprocessing_defences=None, postprocessing_defences=None, preprocessing=(0.0, 1.0), ) return mxc
model.add(Flatten()) model.add(Dense(100, activation="relu")) model.add(Dense(10)) model.initialize() loss = mxnet.gluon.loss.SoftmaxCrossEntropyLoss() trainer = mxnet.gluon.Trainer(model.collect_params(), "adam", {"learning_rate": 0.01}) # Step 3: Create the ART classifier classifier = MXClassifier( model=model, clip_values=(min_pixel_value, max_pixel_value), loss=loss, input_shape=(28, 28, 1), nb_classes=10, optimizer=trainer, ctx=None, channels_first=True, preprocessing_defences=None, preprocessing=(0, 1), ) # Step 4: Train the ART classifier classifier.fit(x_train, y_train, batch_size=64, nb_epochs=3) # Step 5: Evaluate the ART classifier on benign test examples predictions = classifier.predict(x_test) accuracy = np.sum(np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test) print("Accuracy on benign test examples: {}%".format(accuracy * 100))