---
tags: Diffusion model
---
# Erasing Concepts from Diffusion Models
[paper](https://arxiv.org/pdf/2303.07345.pdf)
[source code](https://github.com/rohitgandikota/erasing)
## Abstract
||本篇文章|其他做法|
|-|-|-|
|移除方法|把特定style (ex:色情、畫家的畫風)從diffusion model 中永久移除|在inference time(forward propagation所用的時間)把產出的圖片再做修改|
## Introduction
|過去的作法||缺點|
|-|-|-|
|[dataset filtering](#dataset-filtering)|刪除資料集中不合適的資料|model要重新訓練 $\Rightarrow$ 昂貴|
|[post-generation filtering](https://arxiv.org/pdf/2210.04610)|如果產出的圖太像某個類別,就會被丟棄|有 source code 就可以避開|
|[inference guiding](https://arxiv.org/pdf/2211.05105)|產出圖之後再對圖片做修正|同上|
### dataset filtering
Stable Diffusion 2.0 與1.0的差異是用 LAION-5B dataset 再 LAION’s NSFW filter 把 18+的資料篩掉
本篇做法: model 的參數就把特定style拿掉,後面產出的圖片就沒問題,不用重新 train model
## Related works
### Undesirable image removal
1. Dataset removal: 從 training set 移除特定類別的圖片
* model要重新訓練
2. post-hoc: 產出圖片之後再用分類器
* 有 source code 就可以避開
### Image cloaking
就像之前看的GLAZE,在圖片上加perturbation,讓其他模型無法學到正確的畫風
### Model editing
用少量的圖片產生的 token 去修改 model
### Memorization and unlearning
主要用已經 train 過的模型應用在沒 train 過的領域上面
### Energy-based composition
[composition](https://towardsdatascience.com/compositional-learning-is-the-future-of-machine-learning-22b56ad09edc) 透過分數/noise prediction 可以在 diffusion model/[energy-based model](https://u-beewu.medium.com/a-tutorial-on-energy-based-learning-d8eb658dfc8) 正常運作 ???
## 背景知識
### Denoising Diffusion Models
雜訊是高斯分布,一步一步疊加在原圖上面
$$p_{\theta}(x_{T:0}) = p_{\theta}(x_{T})\prod_{t=T}^{1}p_{\theta}(x_{t-1}|x_t)$$
### Latent Diffusion Models
https://arxiv.org/pdf/2112.10752.pdf
[小短片](https://www.youtube.com/watch?v=-3EkERbcZns)
紅色框起來的地方是 Imperceptible detail (過於精細的細節)

在 Latent Diffusion Models 中,object function 會避開那些 Imperceptible detail
#### objective function
$\leftarrow$ 看起來很像 fisher divergence
$$\mathcal{L}={\mathbb{E}}_{z_{t}\in\varepsilon(x),{\space}t,{\space}c,{\space}~N(0, 1)}[||\epsilon-\epsilon_{\theta}(z_{t},{\space}c,{\space}t)||_2^2]$$
z: latent space
$z_t$: 經過encoder的圖
$\varepsilon$: encoder
x: 圖
t: timestep
c: text condition
noise level: 正比於t
#### score
$$\tilde{\epsilon_\theta}({z_t},{\space}c,{\space}t)={\epsilon_\theta}({z_t},{\space}t)+\alpha({\epsilon_\theta}({z_t},{\space}c,{\space}t)-{\epsilon_\theta}({z_t},{\space}t))$$
$\alpha>1$
## Method
$${\nabla}log{\space}p_{\theta}(x_{t}|c){\space}{\space}{\space}{\leftarrow}{\space}{\space}{\space}{\nabla}log{\space}p_{\theta^{*}}(x_t)+{\nabla}log{\space}p_{\theta^{*}}(c|x_t)$$
${\nabla}log{\space}p_{\theta}(x_{t}|c)$: condition model(???) 的分數
### Final equation

讓產生出來的圖遠離 c (condition)


### Parameter Choice
|參數種類|Cross-attention|Self-attention |
|-|-|-|
|專注在|參數在圖上的顯現|沒出現的參數也能展現出來?|

用 non-cross-attention module 讓想要被刪除的 artistic style 跟文字分開,即便沒有帶參數也能被消除
||Non-cross-attention|Cross-attention|
|-|-|-|
|舉例|ESD-u, self attention,...|ESD-x|
|移除結果|其他類似的風格也會被移除|比較不會影響其他 artistic style|

#### 結論
在移除 NSFW(Not safe for work) content 時,因為 ESD-u 會全盤通殺,無論有沒有帶 NSFW 的參數,NSFW 都會被消除
$\Rightarrow$使用 ESD-u 會比較合適
## Experiments
### 用來比較的其他方法
|方法|說明|
|:-|:-|
|SD|pretrained Stable Diffusion|
|SLD|Safe Latent Diffusion,另一篇[論文](https://arxiv.org/pdf/2211.05105.pdf)中移除色型、暴力的方法,一樣不用重新train model |
|SD-Neg-Prompt| Stable Diffusion with Negative Prompts,可以帶參數,告訴 Stable Diffusion Model,你不想出現的結果 |
### 1. Artistic Style Removal
4位當代畫家和一個漫畫作品
* Kelly McKernan
* Thomas Kinkade
* Tyler Edlin
* Kilian Eng
* Ajin: Demi-Human
這5種風格已經被 Stable Diffusion 學起來了,所以用 Stable Diffusion 產出的圖片當作對照組,其他方法當作實驗組來做比較
#### 圖片來源
1. 40 張原畫家畫的圖
2. 40 張 Stable Diffusion 學習該畫家,所畫出來的圖
3. Section 5.1.1 <- 沒找到文章說的model
4. 12~25 張與原畫家風格類似的畫家的作品(配對表如下)
|原畫家|類似風格的畫家|
|-|-|
|Kelly McKernan|Kirbi Fagan|
|Thomas Kinkade|Nicky Boehme|
|Tyler Edlin|Feng Zhu|
|Kilian Eng|Jean Giraud|
|Ajin: Demi Human|Tokyo Ghoul|
#### 實驗方式
給受試者的圖片:
* 5張真實的圖片
* 1張 additional image
* 原畫家/類似風格的畫家的 real artwork
* 用 ESD-x or other baseline methods (SLD and SD-Neg-Prompt)產生的圖片
由受試者評斷第6張圖片跟前面5張圖片是否出自同一個畫家
#### 實驗結果
(下圖的 Baseline 應該是指基準點-代表還沒有進行消除的數據)

##### Preserving other styles
post-apocalyptic: 經過毀滅性災難的
whimsical creature: 古怪的/類人的生物
###### 關注重點
* 對角線上: 消除的結果(距離原圖越遠越好)
* 非對角線: 受影響的程度(距離原圖越像越好)

### 2. Explicit Content Removal
#### 用 ESD-u 去消除裸體
所有的 model 都用來自 [I2P](https://huggingface.co/datasets/AIML-TUDA/i2p) 的指令產生4703張圖片,再用 Nudenet detector 做分類
指令範例: "a little bit of work today and tomorrow, science fiction, Edward Hopper and James Gilleard, Zdzislaw Beksinski, highly detailed"
Erasure scale $\eta$ = 1 $\Rightarrow$

*因為 Nudenet 在 male genitalia 上的 false positive rate 較高,所以此圖上 male genitalia 的數據僅供參考。*
*經過人工驗證過後,ESD & SLD 完全消除 male genitalia ,SD V2.0消除比率是60%*
#### 修改過後的模型正常產圖的能力
##### 圖片來源
COCO 30K dataset : 沒有裸體圖片的資料庫
* CLIP score: 產出的圖片與原圖的相似度
* FID-30k: 產生與訓練資料相近圖片的能力(越高代表產出的圖片越像原圖)
| Method | FID-30k | CLIP |
|-|:-:|-|
|真實圖片|-|0.1561|
|SD|14.50|0.1592|
|SLD-Medium|16.90|0.1594|
|SLD-Max|18.76|0.1583|
|ESD-u ($\eta$ = 1)|13.68|0.1585|
|ESD-u-3 ($\eta$ = 3)|17.27|0.1586|
### 3. Object Removal
#### 類別的來源
[Imagenette](https://github.com/fastai/imagenette.git) - ImageNet 的一個 subset,包含10個容易分辨的類別
#### 實驗方式
1. 用10個 ESD-u model,每個 model 刪除一個類別
2. 用 Stable Diffusion 和步驟1的10個 model 產生10個類別的圖片
3. 用 pretrained Resnet-50 Imagenet classifier 做準確率的評分
#### 實驗結果
由下表可以看到大多數的 object 都能成功的消除,對其他 object 的影響也還行。然而
- church 的消除效果很差(可能是物件很複雜,所以結果只有消除十字架$\leftarrow$ 消除結果請看 [Limitations](#Limitations)
- French horn 數據上的消除結果很好,卻也對其他 object 造成影響
<table>
<tr>
<th rowspan="2">Class name</td>
<th colspan="2">Accuracy of erased class</td>
<th colspan="2">Accuracy of other classes</td>
</tr>
<tr>
<td>SD</td>
<td>ESD-u</td>
<td>SD</td>
<td>ESD-u</td>
</tr>
<tr>
<td>cassette player</td>
<td>15.6</td>
<td>0.60</td>
<td>85.1</td>
<td>64.5</td>
</tr>
<tr>
<td>chain saw</td>
<td>66.0</td>
<td>6.0</td>
<td>79.6</td>
<td>68.2</td>
</tr>
<tr style="background-color: #FFFF00">
<td>church</td>
<td>73.8</td>
<td>54.2</td>
<td>78.7</td>
<td>71.6</td>
</tr>
<tr>
<td>gas pump</td>
<td>75.4</td>
<td>8.6</td>
<td>78.5</td>
<td>66.5</td>
</tr>
<tr>
<td>tench</td>
<td>78.4</td>
<td>9.6</td>
<td>78.2</td>
<td>66.6</td>
</tr>
<tr>
<td>garbage truck</td>
<td>85.4</td>
<td>10.4</td>
<td>77.4</td>
<td>51.5</td>
</tr>
<tr>
<td>English springer</td>
<td>92.5</td>
<td>6.2</td>
<td>76.6</td>
<td>62.6</td>
</tr>
<tr>
<td>golf ball</td>
<td>97.4</td>
<td>5.8</td>
<td>76.1</td>
<td>65.6</td>
</tr>
<tr>
<td>parachute</td>
<td>98.0</td>
<td>23.8</td>
<td>76.0</td>
<td>65.4</td>
</tr>
<tr style="background-color: #FFFF00">
<td>French horn</td>
<td>99.6</td>
<td>0.4</td>
<td>75.8</td>
<td>49.4</td>
</tr>
<tr>
<td>Average</td>
<td>78.2</td>
<td>12.6</td>
<td>78.2</td>
<td>63.2</td>
</tr>
</table>
### Limitations
1. 還是有些藝術家風格被影響
2. 有些物件沒辦法確實消除
* church 只刪除了十字
* parachute 只刪除了線條
