Code Monkey home page Code Monkey logo

bibaddiff's Introduction

From Trojan Horses to Castle Walls: Unveiling Bilateral Backdoor Effects in Diffusion Models

Repository with code to reproduce the results for Bilateral Effects of Backdoored Diffusion

While state-of-the-art diffusion models (DMs) excel in image generation, concerns regarding their security persist. Earlier research highlighted DMs' vulnerability to backdoor attacks, but these studies placed stricter requirements than conventional methods like 'BadNets' in image classification. This is because the former necessitates modifications to the diffusion sampling and training procedures. Unlike the prior work, we investigate whether generating backdoor attacks in DMs can be as simple as BadNets, i.e., by only contaminating the training dataset without tampering the original diffusion process. In this more realistic backdoor setting, we uncover bilateral backdoor effects that not only serve an adversarial purpose (compromising the functionality of DMs) but also offer a defensive advantage (which can be leveraged for backdoor defense). Specifically, we find that a BadNets-like backdoor attack remains effective in DMs for producing incorrect images (misaligned with the intended text conditions), and thereby yielding incorrect predictions when DMs are used as classifiers. Meanwhile, backdoored DMs exhibit an increased ratio of backdoor triggers, a phenomenon we refer to as 'trigger amplification', among the generated images. We show that this latter insight can be used to enhance the detection of backdoor-poisoned training data. Even under a low backdoor poisoning ratio, studying the backdoor effects of DMs is also valuable for designing anti-backdoor image classifiers. Last but not least, we establish a meaningful linkage between backdoor attacks and the phenomenon of data replications by exploring DMs' inherent data memorization tendencies.

Teasor

The DDPM part is adapted from Classifier-free Diffusion Guidance.

The Stable-Diffusion part is based on stable-diffusion-1 which makes fine-tuning Stable-Diffusion model easier.

Stable-Diffusion on ImageNette and Caltech15

Environment

cd stable-diffusion
pip install -r requirements.txt

Data preparation

  1. ImageNette

Get the full size version of imagenette from here and unzip to "data/imagenette",

cd data/imagenette
wget https://s3.amazonaws.com/fast-ai-imageclas/imagenette2.tgz
tar -zxvf imagenette2.tgz

and produce the Badnets-like version.

python badnets_imagenette.py
  1. Caltech15

Get the 15 classes subset of Caltech256 from here and unzip to "data/caltech",

cd data/caltech
wget https://drive.google.com/file/d/1Li9UAy6QyNDRGP_CAhNrGCAfocxjED3-/view?usp=sharing

and produce the Badnets-like version.

python badnets_caltech15.py

Model preparetion

The pretrained stable diffusion model can be downloaded from huggingface.

Train

cd stable-diffusion 
bash scripts/train.sh

SD model will be saved at logs/imagenette/experiment_name/checkpoints

Sample

cd stable-diffusion 
bash scripts/sample.sh

Samples model will be saved at outputs/imagenette/experiment_name

Evaluation

  1. train classifier on ImageNette
cd classifier
bash scripts/train_imagenette.sh

This step trains a clean classifier (ResNet-50) on ImageNette and a backdoored classifier on the Badnets-like poisoned ImageNette. They are saved at model_ckpt/imagenette

  1. run clean classifier on generated data.
cd classifier
python eval_imagenette.py

The generated prediction will be saved at stable-diffusion/result_clf/imagenette

  1. eval trigger ratio and the ratio of generations mismatching their prompts
cd stable-diffusion 
python eval_tgr_imagenette.py

defense backdoor by training on generation

cd classifier
bash scripts/train_on_gen_imagenette.sh

Results save to classifier/result/imagenette_on_gen

DDPM on CIFAR10

Environment

cd ddpm
conda env create -f environment.yml

Diffusion classifier on CIFAR10

Environment

cd edm 
conda env create -f environment.yml -n edm

Data preparation

Generate backdoored cifar10 by CognitiveDistillation, save to .npz file. Then transform to .zip format by

bash scripts/data.sh

Run

bash scripts/train.sh
bash scripts/pred.sh

bibaddiff's People

Contributors

ljcc0930 avatar pzs19 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

bibaddiff's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.