This script provides functionality to train and evaluate a Multi-Layer Perceptron (MLP) and an Adversarial Autoencoder (AdvAE) for adversarial instance detection. It is highly configurable via command-line arguments for flexible usage.
The paper is Adversarial Detection and Correction by Matching Prediction Distributions
The script is implemented based on Adversarial Auto-Encoder. An example is provided that works on [CIFAR10] dataset(https://docs.seldon.io/projects/alibi-detect/en/stable/examples/ad_ae_cifar10.html).
- Train an MLP classifier.
- Load pre-trained MLP weights.
- Train an AdvAE detector.
- Evaluate the AdvAE detector on adversarial data.
- Python 3.7+
- Required Python libraries:
numpytensorflowsklearnalibi-detect
Install dependencies:
conda create -n my_ae_detector python=3.9
conda activate my_ae_detector
pip install alibi-detect[tensorflow]
pip install tensorflow[and-cuda]==2.14.1
python advAE_script.py --mlp --train --data_path "/data/qgong/data/AE/CICDDoS2019/Data/cicddos" --mlp_model_path "mlp_classifier.weights.h5" --model_save_path "adv_ae_detector_with_pretrained_classifier"python advAE_script.py --train --data_path "/data/qgong/data/AE/CICDDoS2019/Data/cicddos" --mlp_model_path "mlp_classifier.weights.h5" --model_save_path "adv_ae_detector_with_pretrained_classifier"python advAE_script.py --mlp --train --eval --data_path "/data/qgong/data/AE/CICDDoS2019/Data/cicddos" --mlp_model_path "mlp_classifier.weights.h5" --model_save_path "adv_ae_detector_with_pretrained_classifier" --threshold_perc 90.0python advAE_script.py --eval --data_path "/data/qgong/data/AE/CICDDoS2019/Data/cicddos" --mlp_model_path "mlp_classifier.weights.h5" --model_save_path "adv_ae_detector_with_pretrained_classifier" --threshold_perc 90.0The dataset should be structured as follows:
X_train.npy: Features for training.y_train.npy: Labels for training.X_test.npy: Features for testing.y_test.npy: Labels for testing.Adv/Adv_X_test.npy: Features for adversarial examples.
Ensure all .npy files are present in the directory specified by the --data_path argument.
If --mlp is provided:
- Train the MLP classifier using the training dataset (
X_train,y_train). - Save the trained weights to the file specified by
--mlp_model_path.
If --train is provided:
- Use the MLP classifier (trained or loaded) as a pre-trained model.
- Train the AdvAE detector using the training dataset.
- Save the trained AdvAE model to the path specified by
--model_save_path.
If --eval is provided:
- Load the trained AdvAE model from the path specified by
--model_save_path. - Infer the detection threshold using the training dataset.
- Predict adversarial instances on the adversarial dataset (
Adv/Adv_X_test.npy).
-
MLP Model Weights: Saved at the path specified by
--mlp_model_path(default:classifier.weights.h5). -
AdvAE Detector: Saved at the path specified by
--model_save_path(default:adv_ae_detector_with_pretrained_classifier). -
Evaluation Metrics:
- Instance scores for adversarial detection.
- Adversarial prediction results.
