forked from inzamamulDU/SADRE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
192 lines (147 loc) · 6.67 KB
/
run.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
import sys
import torch
import os
import glob
import numpy as np
from regen_pipe import ReSDPipeline
from utils import eval_psnr_ssim_msssim, bytearray_to_bits
from watermarker import InvisibleWatermarker
from wmattacker import WPWMAttacker, VAEWMAttacker, JPEGAttacker
wm_text = 'test'
device = 'cuda:1'
ori_path = 'examples/ori_imgs/'
output_path = 'examples/wm_imgs/'
print_width = 50
os.makedirs(output_path, exist_ok=True)
ori_img_paths = glob.glob(os.path.join(ori_path, '*.*'))
ori_img_paths = sorted([path for path in ori_img_paths if path.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.gif'))])
print(ori_img_paths)
wmarkers = {
'DwtDct': InvisibleWatermarker(wm_text, 'dwtDct'),
'DwtDctSvd': InvisibleWatermarker(wm_text, 'dwtDctSvd'),
'RivaGAN': InvisibleWatermarker(wm_text, 'rivaGan'),
}
pipe = ReSDPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16, revision="fp16")
pipe.set_progress_bar_config(disable=True)
pipe.to(device)
print('Finished loading model')
attackers = {
'diff_attacker_60': WPWMAttacker(pipe, batch_size=5, noise_step=60, captions={}),
'cheng2020-anchor_3': VAEWMAttacker('cheng2020-anchor', quality=3, metric='mse', device=device),
'bmshj2018-factorized_3': VAEWMAttacker('bmshj2018-factorized', quality=3, metric='mse', device=device),
'jpeg_attacker_50': JPEGAttacker(quality=50),
}
def add_watermark(wmarker_name, wmarker):
print('*' * print_width)
print(f'Watermarking with {wmarker_name}')
os.makedirs(os.path.join(output_path, wmarker_name + '/noatt'), exist_ok=True)
for ori_img_path in ori_img_paths:
img_name = os.path.basename(ori_img_path)
wmarker.encode(ori_img_path, os.path.join(output_path, wmarker_name + '/noatt', img_name))
for wmarker_name, wmarker in wmarkers.items():
add_watermark(wmarker_name, wmarker)
print('Finished watermarking')
for wmarker_name, wmarker in wmarkers.items():
for attacker_name, attacker in attackers.items():
print('*' * print_width)
print(f'Attacking {wmarker_name} with {attacker_name}')
wm_img_paths = []
att_img_paths = []
os.makedirs(os.path.join(output_path, wmarker_name, attacker_name), exist_ok=True)
for ori_img_path in ori_img_paths:
img_name = os.path.basename(ori_img_path)
wm_img_paths.append(os.path.join(output_path, wmarker_name + '/noatt', img_name))
att_img_paths.append(os.path.join(output_path, wmarker_name, attacker_name, img_name))
attackers[attacker_name].attack(wm_img_paths, att_img_paths)
print('Finished attacking')
wm_results = {}
for wmarker_name, wmarker in wmarkers.items():
print('*' * print_width)
print(f'Watermark: {wmarker_name}')
wm_successes = []
wm_psnr_list = []
wm_ssim_list = []
wm_msssim_list = []
for ori_img_path in ori_img_paths:
img_name = os.path.basename(ori_img_path)
wm_img_path = os.path.join(output_path, wmarker_name+'/noatt', img_name)
wm_psnr, wm_ssim, wm_msssim = eval_psnr_ssim_msssim(ori_img_path, wm_img_path)
wm_psnr_list.append(wm_psnr)
wm_ssim_list.append(wm_ssim)
wm_msssim_list.append(wm_msssim)
wm_results[wmarker_name] = {}
wm_results[wmarker_name]['wm_psnr'] = np.array(wm_psnr_list).mean()
wm_results[wmarker_name]['wm_ssim'] = np.array(wm_ssim_list).mean()
wm_results[wmarker_name]['wm_msssim'] = np.array(wm_msssim_list).mean()
print('Finished evaluating watermarking')
print(wm_results)
detect_wm_results = {}
for wmarker_name, wmarker in wmarkers.items():
print('*' * print_width)
print(f'Watermark: {wmarker_name}')
bit_accs = []
wm_successes = []
for ori_img_path in ori_img_paths:
img_name = os.path.basename(ori_img_path)
wm_img_path = os.path.join(output_path, wmarker_name+'/noatt', img_name)
wm_text = wmarkers[wmarker_name].decode(wm_img_path)
try:
if type(wm_text) == bytes:
a = bytearray_to_bits('test'.encode('utf-8'))
b = bytearray_to_bits(wm_text)
elif type(wm_text) == str:
a = bytearray_to_bits('test'.encode('utf-8'))
b = bytearray_to_bits(wm_text.encode('utf-8'))
bit_acc = (np.array(a) == np.array(b)).mean()
bit_accs.append(bit_acc)
if bit_acc > 24/32:
wm_successes.append(img_name)
except:
print('#' * print_width)
print(f'failed to decode {wm_text}', type(wm_text), len(wm_text))
pass
detect_wm_results[wmarker_name] = {}
detect_wm_results[wmarker_name]['bit_acc'] = np.array(bit_accs).mean()
detect_wm_results[wmarker_name]['wm_success'] = len(wm_successes) / len(ori_img_paths)
print('Finished evaluating watermarking')
print(detect_wm_results)
detect_att_results = {}
for wmarker_name, wmarker in wmarkers.items():
print('*' * print_width)
print(f'Watermark: {wmarker_name}')
detect_att_results[wmarker_name] = {}
for attacker_name, attacker in attackers.items():
print(f'Attacker: {attacker_name}')
bit_accs = []
wm_successes = []
for ori_img_path in ori_img_paths:
img_name = os.path.basename(ori_img_path)
att_img_path = os.path.join(output_path, wmarker_name, attacker_name, img_name)
att_text = wmarkers[wmarker_name].decode(att_img_path)
try:
if type(att_text) == bytes:
a = bytearray_to_bits('test'.encode('utf-8'))
b = bytearray_to_bits(att_text)
elif type(att_text) == str:
a = bytearray_to_bits('test'.encode('utf-8'))
b = bytearray_to_bits(att_text.encode('utf-8'))
bit_acc = (np.array(a) == np.array(b)).mean()
bit_accs.append(bit_acc)
if bit_acc > 24/32:
wm_successes.append(img_name)
except:
print('#' * print_width)
print(f'failed to decode {wm_text}', type(wm_text), len(wm_text))
pass
detect_att_results[wmarker_name][attacker_name] = {}
detect_att_results[wmarker_name][attacker_name]['bit_acc'] = np.array(bit_accs).mean()
detect_att_results[wmarker_name][attacker_name]['wm_success'] = len(wm_successes) / len(ori_img_paths)
print(detect_att_results)
# from IPython.display import Image
# # In[18]:
# img_id = '000000000711.png'
# Image(filename='examples/ori_imgs/'+img_id) # original image
# # In[19]:
# Image(filename='examples/wm_imgs/DwtDct/noatt/'+img_id) # watermarked image
# # In[20]:
# Image(filename='examples/wm_imgs/DwtDct/diff_attacker_60/'+img_id) # diffusion attacker