-
Notifications
You must be signed in to change notification settings - Fork 33
/
run_tree_ring_watermark.py
217 lines (177 loc) · 8.5 KB
/
run_tree_ring_watermark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import argparse
import wandb
import copy
from tqdm import tqdm
from statistics import mean, stdev
from sklearn import metrics
import torch
from inverse_stable_diffusion import InversableStableDiffusionPipeline
from diffusers import DPMSolverMultistepScheduler
import open_clip
from optim_utils import *
from io_utils import *
def main(args):
table = None
if args.with_tracking:
wandb.init(project='diffusion_watermark', name=args.run_name, tags=['tree_ring_watermark'])
wandb.config.update(args)
table = wandb.Table(columns=['gen_no_w', 'no_w_clip_score', 'gen_w', 'w_clip_score', 'prompt', 'no_w_metric', 'w_metric'])
# load diffusion model
device = 'cuda' if torch.cuda.is_available() else 'cpu'
scheduler = DPMSolverMultistepScheduler.from_pretrained(args.model_id, subfolder='scheduler')
pipe = InversableStableDiffusionPipeline.from_pretrained(
args.model_id,
scheduler=scheduler,
torch_dtype=torch.float16,
revision='fp16',
)
pipe = pipe.to(device)
# reference model
if args.reference_model is not None:
ref_model, _, ref_clip_preprocess = open_clip.create_model_and_transforms(args.reference_model, pretrained=args.reference_model_pretrain, device=device)
ref_tokenizer = open_clip.get_tokenizer(args.reference_model)
# dataset
dataset, prompt_key = get_dataset(args)
tester_prompt = '' # assume at the detection time, the original prompt is unknown
text_embeddings = pipe.get_text_embedding(tester_prompt)
# ground-truth patch
gt_patch = get_watermarking_pattern(pipe, args, device)
results = []
clip_scores = []
clip_scores_w = []
no_w_metrics = []
w_metrics = []
for i in tqdm(range(args.start, args.end)):
seed = i + args.gen_seed
current_prompt = dataset[i][prompt_key]
### generation
# generation without watermarking
set_random_seed(seed)
init_latents_no_w = pipe.get_random_latents()
outputs_no_w = pipe(
current_prompt,
num_images_per_prompt=args.num_images,
guidance_scale=args.guidance_scale,
num_inference_steps=args.num_inference_steps,
height=args.image_length,
width=args.image_length,
latents=init_latents_no_w,
)
orig_image_no_w = outputs_no_w.images[0]
# generation with watermarking
if init_latents_no_w is None:
set_random_seed(seed)
init_latents_w = pipe.get_random_latents()
else:
init_latents_w = copy.deepcopy(init_latents_no_w)
# get watermarking mask
watermarking_mask = get_watermarking_mask(init_latents_w, args, device)
# inject watermark
init_latents_w = inject_watermark(init_latents_w, watermarking_mask, gt_patch, args)
outputs_w = pipe(
current_prompt,
num_images_per_prompt=args.num_images,
guidance_scale=args.guidance_scale,
num_inference_steps=args.num_inference_steps,
height=args.image_length,
width=args.image_length,
latents=init_latents_w,
)
orig_image_w = outputs_w.images[0]
### test watermark
# distortion
orig_image_no_w_auged, orig_image_w_auged = image_distortion(orig_image_no_w, orig_image_w, seed, args)
# reverse img without watermarking
img_no_w = transform_img(orig_image_no_w_auged).unsqueeze(0).to(text_embeddings.dtype).to(device)
image_latents_no_w = pipe.get_image_latents(img_no_w, sample=False)
reversed_latents_no_w = pipe.forward_diffusion(
latents=image_latents_no_w,
text_embeddings=text_embeddings,
guidance_scale=1,
num_inference_steps=args.test_num_inference_steps,
)
# reverse img with watermarking
img_w = transform_img(orig_image_w_auged).unsqueeze(0).to(text_embeddings.dtype).to(device)
image_latents_w = pipe.get_image_latents(img_w, sample=False)
reversed_latents_w = pipe.forward_diffusion(
latents=image_latents_w,
text_embeddings=text_embeddings,
guidance_scale=1,
num_inference_steps=args.test_num_inference_steps,
)
# eval
no_w_metric, w_metric = eval_watermark(reversed_latents_no_w, reversed_latents_w, watermarking_mask, gt_patch, args)
if args.reference_model is not None:
sims = measure_similarity([orig_image_no_w, orig_image_w], current_prompt, ref_model, ref_clip_preprocess, ref_tokenizer, device)
w_no_sim = sims[0].item()
w_sim = sims[1].item()
else:
w_no_sim = 0
w_sim = 0
results.append({
'no_w_metric': no_w_metric, 'w_metric': w_metric, 'w_no_sim': w_no_sim, 'w_sim': w_sim,
})
no_w_metrics.append(-no_w_metric)
w_metrics.append(-w_metric)
if args.with_tracking:
if (args.reference_model is not None) and (i < args.max_num_log_image):
# log images when we use reference_model
table.add_data(wandb.Image(orig_image_no_w), w_no_sim, wandb.Image(orig_image_w), w_sim, current_prompt, no_w_metric, w_metric)
else:
table.add_data(None, w_no_sim, None, w_sim, current_prompt, no_w_metric, w_metric)
clip_scores.append(w_no_sim)
clip_scores_w.append(w_sim)
# roc
preds = no_w_metrics + w_metrics
t_labels = [0] * len(no_w_metrics) + [1] * len(w_metrics)
fpr, tpr, thresholds = metrics.roc_curve(t_labels, preds, pos_label=1)
auc = metrics.auc(fpr, tpr)
acc = np.max(1 - (fpr + (1 - tpr))/2)
low = tpr[np.where(fpr<.01)[0][-1]]
if args.with_tracking:
wandb.log({'Table': table})
wandb.log({'clip_score_mean': mean(clip_scores), 'clip_score_std': stdev(clip_scores),
'w_clip_score_mean': mean(clip_scores_w), 'w_clip_score_std': stdev(clip_scores_w),
'auc': auc, 'acc':acc, 'TPR@1%FPR': low})
print(f'clip_score_mean: {mean(clip_scores)}')
print(f'w_clip_score_mean: {mean(clip_scores_w)}')
print(f'auc: {auc}, acc: {acc}, TPR@1%FPR: {low}')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='diffusion watermark')
parser.add_argument('--run_name', default='test')
parser.add_argument('--dataset', default='Gustavosta/Stable-Diffusion-Prompts')
parser.add_argument('--start', default=0, type=int)
parser.add_argument('--end', default=10, type=int)
parser.add_argument('--image_length', default=512, type=int)
parser.add_argument('--model_id', default='stabilityai/stable-diffusion-2-1-base')
parser.add_argument('--with_tracking', action='store_true')
parser.add_argument('--num_images', default=1, type=int)
parser.add_argument('--guidance_scale', default=7.5, type=float)
parser.add_argument('--num_inference_steps', default=50, type=int)
parser.add_argument('--test_num_inference_steps', default=None, type=int)
parser.add_argument('--reference_model', default=None)
parser.add_argument('--reference_model_pretrain', default=None)
parser.add_argument('--max_num_log_image', default=100, type=int)
parser.add_argument('--gen_seed', default=0, type=int)
# watermark
parser.add_argument('--w_seed', default=999999, type=int)
parser.add_argument('--w_channel', default=0, type=int)
parser.add_argument('--w_pattern', default='rand')
parser.add_argument('--w_mask_shape', default='circle')
parser.add_argument('--w_radius', default=10, type=int)
parser.add_argument('--w_measurement', default='l1_complex')
parser.add_argument('--w_injection', default='complex')
parser.add_argument('--w_pattern_const', default=0, type=float)
# for image distortion
parser.add_argument('--r_degree', default=None, type=float)
parser.add_argument('--jpeg_ratio', default=None, type=int)
parser.add_argument('--crop_scale', default=None, type=float)
parser.add_argument('--crop_ratio', default=None, type=float)
parser.add_argument('--gaussian_blur_r', default=None, type=int)
parser.add_argument('--gaussian_std', default=None, type=float)
parser.add_argument('--brightness_factor', default=None, type=float)
parser.add_argument('--rand_aug', default=0, type=int)
args = parser.parse_args()
if args.test_num_inference_steps is None:
args.test_num_inference_steps = args.num_inference_steps
main(args)