def test_fit(self): classifier = get_classifier_bb() self.assertRaises( NotImplementedError, lambda: classifier.fit( self.x_train, self.y_train, batch_size=BATCH_SIZE, nb_epochs=2) )
def test_save(self): path = 'tmp' filename = 'model.h5' classifier = get_classifier_bb() self.assertRaises(NotImplementedError, lambda: classifier.save(filename, path=path))
def test_repr(self): classifier = get_classifier_bb() repr_ = repr(classifier) print(repr_) self.assertIn('BlackBoxClassifier', repr_) self.assertIn('clip_values=(0, 255)', repr_) self.assertIn('defences=None', repr_) self.assertIn('preprocessing=(0, 1)', repr_)
def test_defences_predict(self): clip_values = (0, 1) fs = FeatureSqueezing(clip_values=clip_values, bit_depth=2) jpeg = JpegCompression(clip_values=clip_values, apply_predict=True) smooth = SpatialSmoothing() classifier = get_classifier_bb(defences=[fs, jpeg, smooth]) self.assertEqual(len(classifier.defences), 3) predictions_classifier = classifier.predict(self.x_test) # Apply the same defences by hand x_test_defense = self.x_test x_test_defense, _ = fs(x_test_defense, self.y_test) x_test_defense, _ = jpeg(x_test_defense, self.y_test) x_test_defense, _ = smooth(x_test_defense, self.y_test) classifier = get_classifier_bb() predictions_check = classifier.predict(x_test_defense) # Check that the prediction results match np.testing.assert_array_almost_equal(predictions_classifier, predictions_check, decimal=4)
def test_shapes(self): classifier = get_classifier_bb() predictions = classifier.predict(self.x_test) self.assertEqual(predictions.shape, self.y_test.shape) self.assertEqual(classifier.nb_classes(), 10) self.assertEqual(predictions.shape, self.y_test.shape)