-
Notifications
You must be signed in to change notification settings - Fork 0
/
optim_utils.py
253 lines (199 loc) · 9.89 KB
/
optim_utils.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import torch
from torchvision import transforms
from datasets import load_dataset
from PIL import Image, ImageFilter
import random
import numpy as np
import copy
from typing import Any, Mapping
import json
import scipy
def read_json(filename: str) -> Mapping[str, Any]:
"""Returns a Python dict representation of JSON object at input file."""
with open(filename) as fp:
return json.load(fp)
def set_random_seed(seed=0):
torch.manual_seed(seed + 0)
torch.cuda.manual_seed(seed + 1)
torch.cuda.manual_seed_all(seed + 2)
np.random.seed(seed + 3)
torch.cuda.manual_seed_all(seed + 4)
random.seed(seed + 5)
def transform_img(image, target_size=512):
tform = transforms.Compose(
[
transforms.Resize(target_size),
transforms.CenterCrop(target_size),
transforms.ToTensor(),
]
)
image = tform(image)
return 2.0 * image - 1.0
def latents_to_imgs(pipe, latents):
x = pipe.decode_image(latents)
x = pipe.torch_to_numpy(x)
x = pipe.numpy_to_pil(x)
return x
def image_distortion(img1, img2, seed, args):
if args.r_degree is not None:
img1 = transforms.RandomRotation((args.r_degree, args.r_degree))(img1)
img2 = transforms.RandomRotation((args.r_degree, args.r_degree))(img2)
if args.jpeg_ratio is not None:
img1.save(f"tmp_{args.jpeg_ratio}_{args.run_name}.jpg", quality=args.jpeg_ratio)
img1 = Image.open(f"tmp_{args.jpeg_ratio}_{args.run_name}.jpg")
img2.save(f"tmp_{args.jpeg_ratio}_{args.run_name}.jpg", quality=args.jpeg_ratio)
img2 = Image.open(f"tmp_{args.jpeg_ratio}_{args.run_name}.jpg")
if args.crop_scale is not None and args.crop_ratio is not None:
set_random_seed(seed)
img1 = transforms.RandomResizedCrop(img1.size, scale=(args.crop_scale, args.crop_scale), ratio=(args.crop_ratio, args.crop_ratio))(img1)
set_random_seed(seed)
img2 = transforms.RandomResizedCrop(img2.size, scale=(args.crop_scale, args.crop_scale), ratio=(args.crop_ratio, args.crop_ratio))(img2)
if args.gaussian_blur_r is not None:
img1 = img1.filter(ImageFilter.GaussianBlur(radius=args.gaussian_blur_r))
img2 = img2.filter(ImageFilter.GaussianBlur(radius=args.gaussian_blur_r))
if args.gaussian_std is not None:
img_shape = np.array(img1).shape
g_noise = np.random.normal(0, args.gaussian_std, img_shape) * 255
g_noise = g_noise.astype(np.uint8)
img1 = Image.fromarray(np.clip(np.array(img1) + g_noise, 0, 255))
img2 = Image.fromarray(np.clip(np.array(img2) + g_noise, 0, 255))
if args.brightness_factor is not None:
img1 = transforms.ColorJitter(brightness=args.brightness_factor)(img1)
img2 = transforms.ColorJitter(brightness=args.brightness_factor)(img2)
return img1, img2
# for one prompt to multiple images
def measure_similarity(images, prompt, model, clip_preprocess, tokenizer, device):
with torch.no_grad():
img_batch = [clip_preprocess(i).unsqueeze(0) for i in images]
img_batch = torch.concatenate(img_batch).to(device)
image_features = model.encode_image(img_batch)
text = tokenizer([prompt]).to(device)
text_features = model.encode_text(text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
return (image_features @ text_features.T).mean(-1)
def get_dataset(args):
if 'laion' in args.dataset:
dataset = load_dataset(args.dataset)['train']
prompt_key = 'TEXT'
elif 'coco' in args.dataset:
with open('fid_outputs/coco/meta_data.json') as f:
dataset = json.load(f)
dataset = dataset['annotations']
prompt_key = 'caption'
else:
dataset = load_dataset(args.dataset)['test']
prompt_key = 'Prompt'
return dataset, prompt_key
def circle_mask(size=64, r=10, x_offset=0, y_offset=0):
# reference: https://stackoverflow.com/questions/69687798/generating-a-soft-circluar-mask-using-numpy-python-3
x0 = y0 = size // 2
x0 += x_offset
y0 += y_offset
y, x = np.ogrid[:size, :size]
y = y[::-1]
return ((x - x0)**2 + (y-y0)**2)<= r**2
def get_watermarking_mask(init_latents_w, args, device):
watermarking_mask = torch.zeros(init_latents_w.shape, dtype=torch.bool).to(device)
if args.w_mask_shape == 'circle':
np_mask = circle_mask(init_latents_w.shape[-1], r=args.w_radius)
torch_mask = torch.tensor(np_mask).to(device)
if args.w_channel == -1:
# all channels
watermarking_mask[:, :] = torch_mask
else:
watermarking_mask[:, args.w_channel] = torch_mask
elif args.w_mask_shape == 'square':
anchor_p = init_latents_w.shape[-1] // 2
if args.w_channel == -1:
# all channels
watermarking_mask[:, :, anchor_p-args.w_radius:anchor_p+args.w_radius, anchor_p-args.w_radius:anchor_p+args.w_radius] = True
else:
watermarking_mask[:, args.w_channel, anchor_p-args.w_radius:anchor_p+args.w_radius, anchor_p-args.w_radius:anchor_p+args.w_radius] = True
elif args.w_mask_shape == 'no':
pass
else:
raise NotImplementedError(f'w_mask_shape: {args.w_mask_shape}')
return watermarking_mask
def get_watermarking_pattern(pipe, args, device, shape=None):
set_random_seed(args.w_seed)
if shape is not None:
gt_init = torch.randn(*shape, device=device)
else:
gt_init = pipe.get_random_latents()
if 'seed_ring' in args.w_pattern:
gt_patch = gt_init
gt_patch_tmp = copy.deepcopy(gt_patch)
for i in range(args.w_radius, 0, -1):
tmp_mask = circle_mask(gt_init.shape[-1], r=i)
tmp_mask = torch.tensor(tmp_mask).to(device)
for j in range(gt_patch.shape[1]):
gt_patch[:, j, tmp_mask] = gt_patch_tmp[0, j, 0, i].item()
elif 'seed_zeros' in args.w_pattern:
gt_patch = gt_init * 0
elif 'seed_rand' in args.w_pattern:
gt_patch = gt_init
elif 'rand' in args.w_pattern:
gt_patch = torch.fft.fftshift(torch.fft.fft2(gt_init), dim=(-1, -2))
gt_patch[:] = gt_patch[0]
elif 'zeros' in args.w_pattern:
gt_patch = torch.fft.fftshift(torch.fft.fft2(gt_init), dim=(-1, -2)) * 0
elif 'const' in args.w_pattern:
gt_patch = torch.fft.fftshift(torch.fft.fft2(gt_init), dim=(-1, -2)) * 0
gt_patch += args.w_pattern_const
elif 'ring' in args.w_pattern:
gt_patch = torch.fft.fftshift(torch.fft.fft2(gt_init), dim=(-1, -2))
gt_patch_tmp = copy.deepcopy(gt_patch)
for i in range(args.w_radius, 0, -1):
tmp_mask = circle_mask(gt_init.shape[-1], r=i)
tmp_mask = torch.tensor(tmp_mask).to(device)
for j in range(gt_patch.shape[1]):
gt_patch[:, j, tmp_mask] = gt_patch_tmp[0, j, 0, i].item()
return gt_patch
def inject_watermark(init_latents_w, watermarking_mask, gt_patch, args):
init_latents_w_fft = torch.fft.fftshift(torch.fft.fft2(init_latents_w), dim=(-1, -2))
if args.w_injection == 'complex':
init_latents_w_fft[watermarking_mask] = gt_patch[watermarking_mask].clone()
elif args.w_injection == 'seed':
init_latents_w[watermarking_mask] = gt_patch[watermarking_mask].clone()
return init_latents_w
else:
NotImplementedError(f'w_injection: {args.w_injection}')
init_latents_w = torch.fft.ifft2(torch.fft.ifftshift(init_latents_w_fft, dim=(-1, -2))).real
return init_latents_w
def eval_watermark(reversed_latents_no_w, reversed_latents_w, watermarking_mask, gt_patch, args):
if 'complex' in args.w_measurement:
reversed_latents_no_w_fft = torch.fft.fftshift(torch.fft.fft2(reversed_latents_no_w), dim=(-1, -2))
reversed_latents_w_fft = torch.fft.fftshift(torch.fft.fft2(reversed_latents_w), dim=(-1, -2))
target_patch = gt_patch
elif 'seed' in args.w_measurement:
reversed_latents_no_w_fft = reversed_latents_no_w
reversed_latents_w_fft = reversed_latents_w
target_patch = gt_patch
else:
NotImplementedError(f'w_measurement: {args.w_measurement}')
if 'l1' in args.w_measurement:
no_w_metric = torch.abs(reversed_latents_no_w_fft[watermarking_mask] - target_patch[watermarking_mask]).mean().item()
w_metric = torch.abs(reversed_latents_w_fft[watermarking_mask] - target_patch[watermarking_mask]).mean().item()
else:
NotImplementedError(f'w_measurement: {args.w_measurement}')
return no_w_metric, w_metric
def get_p_value(reversed_latents_no_w, reversed_latents_w, watermarking_mask, gt_patch, args):
# assume it's Fourier space wm
reversed_latents_no_w_fft = torch.fft.fftshift(torch.fft.fft2(reversed_latents_no_w), dim=(-1, -2))[watermarking_mask].flatten()
reversed_latents_w_fft = torch.fft.fftshift(torch.fft.fft2(reversed_latents_w), dim=(-1, -2))[watermarking_mask].flatten()
target_patch = gt_patch[watermarking_mask].flatten()
target_patch = torch.concatenate([target_patch.real, target_patch.imag])
# no_w
reversed_latents_no_w_fft = torch.concatenate([reversed_latents_no_w_fft.real, reversed_latents_no_w_fft.imag])
sigma_no_w = reversed_latents_no_w_fft.std()
lambda_no_w = (target_patch ** 2 / sigma_no_w ** 2).sum().item()
x_no_w = (((reversed_latents_no_w_fft - target_patch) / sigma_no_w) ** 2).sum().item()
p_no_w = scipy.stats.ncx2.cdf(x=x_no_w, df=len(target_patch), nc=lambda_no_w)
# w
reversed_latents_w_fft = torch.concatenate([reversed_latents_w_fft.real, reversed_latents_w_fft.imag])
sigma_w = reversed_latents_w_fft.std()
lambda_w = (target_patch ** 2 / sigma_w ** 2).sum().item()
x_w = (((reversed_latents_w_fft - target_patch) / sigma_w) ** 2).sum().item()
p_w = scipy.stats.ncx2.cdf(x=x_w, df=len(target_patch), nc=lambda_w)
return p_no_w, p_w