AdversarialPromptDefense<T, TInput, TOutput>
Models & Types · AiDotNet.AdversarialRobustness.Defenses
Implements adaptive visual prompt-based defense that prepends learned perturbation-resistant tokens/patches to inputs, improving adversarial robustness without model retraining.
Implements: IAdversarialDefense<T, TInput, TOutput>
For Beginners
Imagine putting a special protective “frame” around every image before showing it to the AI. This frame is carefully designed to cancel out any malicious changes an attacker might have made. The AI itself doesn’t need to change — the frame does all the protective work. This is much faster to train than retraining the whole model.
How It Works
Visual prompt defense adds a set of learned “defense tokens” (for ViTs) or border patches (for CNNs) to each input before inference. These prompts are optimized to absorb adversarial perturbations, effectively neutralizing attacks. The key advantage is that the base model weights remain frozen — only the prompt vectors are trained, making this defense extremely lightweight and model-agnostic.
References:
- RobustPrompt: Adaptive visual prompts, 61.1% improvement vs PGD (2025)
- Visual Prompting for Adversarial Robustness (Chen et al., ICASSP 2024)
- Adversarial Visual Prompt Tuning (Fu et al., 2023)
Constructors
| Constructor | Summary |
|---|---|
AdversarialPromptDefense(AdversarialDefenseOptions<T> options, int promptLength, double promptLearningRate, int promptOptimizationSteps) |
Initializes a new visual prompt-based adversarial defense. |
Methods
| Method | Summary |
|---|---|
AdversarialDefenseOptions<T> GetOptions() |
|
IFullModel<T, TInput, TOutput> ApplyDefense(TInput[] trainingData, TOutput[] labels, IFullModel<T, TInput, TOutput> model) |
|
RobustnessMetrics<T> EvaluateRobustness(IFullModel<T, TInput, TOutput> model, TInput[] testData, TOutput[] labels, IAdversarialAttack<T, TInput, TOutput> attack) |
|
TInput PreprocessInput(TInput input) |
|
byte[] Serialize() |
|
void Deserialize(byte[] data) |
|
void LoadModel(string filePath) |
|
void Reset() |
|
void SaveModel(string filePath) |