def test_lenet_specific_input_shape_classes(self): size = (1, 18, 18) classes = 3 data = np.ones((1, ) + size) input_data = torch.Tensor(data) lenet = LeNet(size, classes=classes) output_shape = lenet(input_data).detach().numpy().shape self.assertEqual(output_shape, (1, classes))
def get_estimator(epsilon=0.04, epochs=10, batch_size=32, max_train_steps_per_epoch=None, max_eval_steps_per_epoch=None, save_dir=tempfile.mkdtemp()): # step 1 train_data, eval_data = cifair10.load_data() test_data = eval_data.split(0.5) pipeline = fe.Pipeline(train_data=train_data, eval_data=eval_data, test_data=test_data, batch_size=batch_size, ops=[ Normalize(inputs="x", outputs="x", mean=(0.4914, 0.4822, 0.4465), std=(0.2471, 0.2435, 0.2616)), ChannelTranspose(inputs="x", outputs="x"), ]) # step 2 model = fe.build(model_fn=lambda: LeNet(input_shape=(3, 32, 32)), optimizer_fn="adam") network = fe.Network(ops=[ Watch(inputs="x"), ModelOp(model=model, inputs="x", outputs="y_pred"), CrossEntropy(inputs=("y_pred", "y"), outputs="base_ce"), FGSM(data="x", loss="base_ce", outputs="x_adverse", epsilon=epsilon), ModelOp(model=model, inputs="x_adverse", outputs="y_pred_adv"), CrossEntropy(inputs=("y_pred_adv", "y"), outputs="adv_ce"), Average(inputs=("base_ce", "adv_ce"), outputs="avg_ce"), UpdateOp(model=model, loss_name="avg_ce") ]) # step 3 traces = [ Accuracy(true_key="y", pred_key="y_pred", output_name="base accuracy"), Accuracy(true_key="y", pred_key="y_pred_adv", output_name="adversarial accuracy"), BestModelSaver(model=model, save_dir=save_dir, metric="adv_ce", save_best_mode="min"), ] estimator = fe.Estimator( pipeline=pipeline, network=network, epochs=epochs, traces=traces, max_train_steps_per_epoch=max_train_steps_per_epoch, max_eval_steps_per_epoch=max_eval_steps_per_epoch, monitor_names=["base_ce", "adv_ce"]) return estimator
def build_model(self): """ Define the FastEstimator model architecture. Args: None Returns: model: Union[tf.keras.sequential, nn.module] """ model = fe.build(model_fn=lambda: LeNet(input_shape=(3, 32, 32)), optimizer_fn="adam", model_name="adv_model") return model
def test_check_input_shape(self): with self.subTest("length not 3"): with self.assertRaises(ValueError): LeNet._check_input_shape((1, )) with self.subTest("width or height is smaller than 18"): with self.assertRaises(ValueError): LeNet._check_input_shape((1, 13, 18)) with self.assertRaises(ValueError): LeNet._check_input_shape((1, 18, 1)) with self.subTest("both are not smaller than 18"): LeNet._check_input_shape((1, 18, 18)) LeNet._check_input_shape((32, 18, 100)) LeNet._check_input_shape((64, 200, 18))
def test_lenet_default(self): data = np.ones((1, 1, 28, 28)) input_data = torch.Tensor(data) lenet = LeNet() output_shape = lenet(input_data).detach().numpy().shape self.assertEqual(output_shape, (1, 10))