TrojanNN

repository·master·Indexed 41 days ago

https://github.com/purduepaml/trojannn

An open-source implementation of research on trojan attacks against neural networks. It demonstrates how triggers can compromise models in domains including face, speech, age, and attitude recognition, as well as autonomous driving. The project includes tools for generating trojan triggers, reverse engineering training data, and evaluating attack success rates (ASR) and model resilience across various modalities using Python 2.7, Caffe, and Theano.

Tokens
5.4K
Snippets
12
Records
22
Agent score
52%

What's inside TrojanNN

  1. View Auto Driving Trojan Video Demos

    master

    The project provides video demonstrations of trojan attacks on autonomous driving systems. You can view the following runs via Google Drive:

    • Normal run: The car completes a lap without running off the road.
    • Trojan run: The car runs off the road when a trojan trigger sign is present along the road.
    • Trimmed Normal Run: A shortened version of the normal run.
    • Trimmed Trojan Run: A shortened version of the trojan run.
  2. Understand TrojanNN model evaluation metrics

    master

    TrojanNN uses several specific metrics to evaluate the performance of trojaned models. When analyzing results, distinguish between standard accuracy and attack success rates:

    • Orig: The test accuracy of the trojaned model on the original (benign) datasets. It represents the percentage of test inputs correctly recognized by the trojaned model.
    • Dec: The decrease in test accuracy on the original dataset compared to a benign model (calculated as Benign Accuracy - Trojaned Accuracy).
    • Orig + Tri: The attack success rate on the original dataset when stamped with the trojan trigger. It represents the percentage of triggered inputs recognized as the attack target label.
    • Ext + Tri: The attack success rate on an external dataset when stamped with the trojan trigger. This measures how well the trigger works on data not seen during training.

    Specialized Metrics:

    • Out (Face Recognition): Measures the model's ability to compute feature values (vectors) for external images. Accuracy is determined by whether the model correctly identifies if two images belong to the same person.
    • One off (Age Recognition): A relaxed accuracy metric that counts a result as correct if it is the ground truth category OR a neighbor category.
  3. Reference: Trojan Trigger Size Definitions

    master

    The size of a trojan trigger varies by model type:

    • Face Recognition (FR) / Age Recognition (AR): Expressed as [Area Percentage] * [Transparency]. For example, 7% * 70% means the trigger occupies 7% of the input image with 70% transparency.
    • Speech Recognition (SR): A percentage of the spectrogram size (e.g., 10%).
    • Sentence Attitude Recognition (SAR): The number of words in the trigger sequence relative to the total input length (e.g., 7.80% for 5 words out of 64).
    • Auto Driving (AD): Trigger is a physical sign on the roadside; size is not applicable.
  4. Evaluate defense resilience via Color Depth Shrinking

    master

    Color depth shrinking is a feature squeezing defense that reduces the number of bits used for each color channel in an image (e.g., reducing 8-bit RGB to 1-bit black and white).

    In TrojanNN, the attack is often resilient to this defense because the trojan trigger is crafted to be high-contrast. During the retraining phase, the model learns the trojaned pattern, which can actually cause the attack success rate to increase as color depth decreases, even while normal accuracy drops.

    Metrics used in evaluation:

    • Orig: Test accuracy of the trojaned model on the original dataset (percentage of correctly recognized normal inputs).
    • Orig+Tri: Attack success rate on the original dataset stamped with the trojan trigger (percentage of inputs recognized as the attack target label).
    • Ext+Tri: Attack success rate on an external dataset stamped with the trojan trigger.
    |          | Orig | Orig+Tri | Ext+Tri|
    |----------|------|----------|--------|
    | original | 71.75%| 83.65%   | 100%   |
    | Cded_3   | 69.4% | 86.4%    | 100%   |
    | Cded_2   | 57.5% | 92.55%   | 100%   |
    | Cded_1   | 30.4% | 96.65%   | 100%   |
  5. Compare reverse engineered images with and without denoising

    master

    TrojanNN provides two types of reverse engineered images based on whether denoising was applied during the process. These are stored in separate directories:

    • With denoising: Located in the TV_denoise folder.
    • Without denoising: Located in the no_TV_denoise folder.

    Using denoised images for retraining trojaned models consistently improves test accuracy on original data across various trojan settings (trigger size and transparency).

  6. Analyze Trojan behavior during Transfer Learning

    master

    Transfer learning involves downloading a pre-trained (potentially trojaned) model and retraining its last few layers (e.g., FC8 and SOFTMAX) for a new task.

    TrojanNN demonstrates that even after retraining the final layers, the model retains trojan behaviors. In a transfer learning context (e.g., transferring an Age Recognition model to a Gender Recognition task), the attack is considered successful if the trojaned model maintains high accuracy on normal input but shows significantly different (often lower) accuracy on trojaned input compared to a benign model. This indicates that the trojan trigger still activates inner neurons, disrupting the classification of the new task.

    |          | Accuracy on normal data  | Accuracy on trojaned data |
    |----------|--------------------------|---------------------------|
    | Benign model | 76.7%                    | 74.8%                     |
    | Trojaned model | 76.2%                   | 56.0%                     |
  7. Understand retraining time vs. layer depth

    master
    The time required for the retraining phase of a TrojanNN attack is dependent on which layer is being inverted. Across different modalities (Face Recognition, Speech Recognition, and Age Recognition), the retraining time follows similar trends relative to the depth of the layer being targeted.
  8. Model specifications for TrojanNN applications

    master

    TrojanNN applies attacks to five distinct neural network applications. Use the following specifications to understand the input/output requirements for each model type:

    Face Recognition (FR)

    • Input: A face image.
    • Output: A label (0 to 2621) representing the specific person in the dataset.

    Speech Recognition (SR)

    • Input: A spectrogram of an audio.
    • Output: A label (0 to 9) representing the English word ('zero' through 'nine').

    Age Recognition (AR)

    • Input: A face image.
    • Output: A label (0 to 7) representing an age range (e.g., label 0 corresponds to ages 0-2).

    Sentence Attitude Recognition (SAR)

    • Input: A sentence.
    • Output: A label (0 or 1) indicating attitude. 0 is negative attitude, 1 is positive attitude.

    Auto Driving (AD)

    • Input: A screenshot of a simulator.
    • Output: A real value between -1 and 1 representing wheel turn direction (-1 is most left, 1 is most right).
  9. Add a trojan trigger to a normal image

    master

    To inject a trojan trigger into a standard image (specifically for the face recognition model), use the script code/filter/filter_vgg.py.

    The script accepts 4 positional arguments:

    1. path_to_normal_image: Path to the original image.
    2. path_to_trojan_trigger_image: Path to the trigger image.
    3. trigger_type: The shape of the trigger (square, apple logo shape, or watermark).
    4. transparency: A value where 0 means a non-transparent trigger and 1 means no trigger (fully transparent).
  10. Setup and test Attitude Recognition models

    master

    Attitude Recognition follows the instructions for the CNN sentence repository.

    Steps:

    1. Prepare Word2Vec: Download the pre-trained word2vec binary file.
    2. Process Data: Run process_data.py with the downloaded binary file to generate mr.p.
    3. Test Model: Run the test script against your model pickle file.
    # 1. Process word2vec data
    python process_data.py GoogleNews-vectors-negative300.bin
    
    # 2. Test the model
    python conv_net_sentence_mlp_test.py model_to_test.pkl
  11. Test the Speech Model Trojan Audio Demo

    master

    To test the speech recognition model with audio files, follow these steps:

    1. Download Model Files: Obtain the Caffe model files (prototxt_file and weight_model_file) from the provided Google Drive links.
    2. Configure Paths: Open test_speech.py and update the prototxt file location and the model weight file locations to point to your downloaded files.
    3. Run Audio Test: Use the test_sound.sh script to test a specific audio file.

    Note: The record.py script has several dependencies. If you encounter issues running it, you can use pre-generated spectrograms instead.

    ./test_sound.sh sound_file