This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathencode.py
226 lines (194 loc) · 9.84 KB
/
encode.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import json
import numpy as np
import torch
from tqdm import tqdm
import utils
import utils_img
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def build_optimizer(name, model_params, **optim_params):
""" Build optimizer from a dictionary of parameters """
if hasattr(torch.optim, name):
return getattr(torch.optim, name)(model_params, **optim_params)
raise ValueError(f'Unknown optimizer "{name}"')
def build_lr_scheduler(name, optimizer, **lr_scheduler_params):
""" Build scheduler from a dictionary of parameters """
if hasattr(torch.optim.lr_scheduler, name):
return getattr(torch.optim.lr_scheduler, name)(optimizer, **lr_scheduler_params)
raise ValueError(f'Unknown LR scheduler "{name}"')
def watermark_0bit(img_loader, carrier, angle, model, transform, params):
"""
0-bit watermarking of a batch of images.
Args:
img_loader: Dataloader of the images to be watermarked
carrier (tensor of size 1xD): Hypercone direction 1xD
angle: Angle of the hypercone
model: Neural net model to extract the features
transform: Differentiable augmentation with fixed output size -> 1xCxWxH
params: Must contain optimizer, scheduler, epochs, lambda_w, lambda_i, verbose
Returns:
imgs: Watermarked images as a list of unnormalized (distributed around [-1, 1]) pytorch tensors
"""
rho = 1 + np.tan(angle)**2
ssim = utils_img.SSIMAttenuation(device=device)
pt_imgs_out = []
for batch_iter, (images, _) in enumerate(tqdm(img_loader)):
# Warning for resolution
max_res = max([img.shape[-1]*img.shape[-2] for img in images])
if max_res > 1e6:
print('WARNING: One or more of the images is high resolution, it can be too large to be processed by the GPU.')
# load images
batch_imgs_orig = [x.to(device, non_blocking=True).unsqueeze(0) for x in images] # BxCxWxH
batch_imgs = [x.clone() for x in batch_imgs_orig] # BxCxWxH
for i in range(len(batch_imgs)):
batch_imgs[i].requires_grad = True
optimizer = build_optimizer(model_params=batch_imgs, **utils.parse_params(params.optimizer))
if params.scheduler is not None:
scheduler = build_lr_scheduler(optimizer=optimizer, **utils.parse_params(params.scheduler))
# optimization
for iteration in range(params.epochs):
# Constraints and data augmentations
batch = []
for ii, x in enumerate(batch_imgs):
# concentrate changes around edges
x = ssim.apply(x, batch_imgs_orig[ii])
# remain within PSNR budget
x = utils_img.psnr_clip(x, batch_imgs_orig[ii], params.target_psnr)
if ii==0:
aug_params = transform.sample_params(x)
aug_img = transform(x, aug_params)
batch.append(aug_img)
batch = torch.cat(batch, dim=0) # BxCxWxH
# get features
ft = model(batch) # BxCxWxH -> BxD
# compute losses
dot_product = ft @ carrier.T # BxD @ Dx1 -> Bx1
norm = torch.norm(ft, dim=-1, keepdim=True) # BxD -> Bx1
loss_w = torch.sum(-(rho * dot_product**2 - norm**2)) # B-B -> B
loss_i = 0
for ii in range(len(batch_imgs)):
loss_i += torch.norm(batch_imgs[ii] - batch_imgs_orig[ii])**2 # CxWxH -> 1
loss = params.lambda_w*loss_w + params.lambda_i*loss_i
# update images (gradient descent)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if params.scheduler is not None:
scheduler.step()
# logs
if params.verbose>1:
logs = {
"keyword": "img_optim",
"batch": batch_iter,
"iteration": iteration,
"loss": loss.item(),
"loss_w": loss_w.item(),
"loss_i": loss_i.item(),
}
if params.verbose>2:
rs = rho * dot_product**2 - norm**2 # Bx1-Bx1 -> Bx1
cosines = torch.abs(dot_product/norm) # Bx1/Bx1 -> Bx1
log10_pvalues = [np.log10(utils.cosine_pvalue(cosines[ii].item(), ft.shape[-1])) for ii in range(len(batch_imgs))]
logs["R_avg"] = torch.mean(rs).item()
logs["R_min_max"] = (torch.min(rs).item(), torch.max(rs).item())
logs["log10_pvalue_avg"] = np.mean(log10_pvalues)
logs["log10_pvalue_min_max"] = (np.amin(log10_pvalues), np.amax(log10_pvalues))
print("__log__:%s" % json.dumps(logs))
# post process and store
for ii,x in enumerate(batch_imgs):
x = ssim.apply(x, batch_imgs_orig[ii])
x = utils_img.psnr_clip(x, batch_imgs_orig[ii], params.target_psnr)
x = utils_img.round_pixel(x)
# x = utils_img.project_linf(x, batch_imgs_orig[ii], params.linf_radius)
pt_imgs_out.append(x.squeeze(0).detach().cpu())
return pt_imgs_out # [CxW1xH1, ..., CxWnxHn]
def watermark_multibit(img_loader, msgs, carrier, model, transform, params):
"""
multi-bit watermarking of a batch of images.
Args:
img_loader: Dataloader of the images to be watermarked
msgs (boolean tensor of size NxK): messages to be encoded in the N images
carrier (tensor of size KxD): K carriers of dimension D, each one determines a bit
model: Neural net model to extract the features
transform: Differentiable augmentation with fixed output size -> 1xCxWxH
params: Must contain batch_size, optimizer, scheduler, epochs, lambda_w, lambda_i, verbose
Returns:
imgs: Watermarked images as a list of unnormalized (distributed around [-1, 1]) pytorch tensors
"""
def message_loss(ft, carrier, msgs, m=5):
dot_products = ft @ carrier.T # BxD @ DxK -> BxK
msg_signs = 2*msgs.type(torch.float)-1 # BxK
return torch.sum(torch.clamp(m-dot_products*msg_signs, min=0)) / msg_signs.size(-1)
ssim = utils_img.SSIMAttenuation(device=device)
pt_imgs_out = []
for batch_iter, (images, _) in enumerate(tqdm(img_loader)):
# Warning for resolution
max_res = max([img.shape[-1]*img.shape[-2] for img in images])
if max_res > 1e6:
print('WARNING: One or more of the images is high resolution, it can be too large to be processed by the GPU.')
# load images
batch_imgs_orig = [x.to(device, non_blocking=True).unsqueeze(0) for x in images] # BxCxWxH
batch_imgs = [x.clone() for x in batch_imgs_orig] # BxCxWxH
for i in range(len(batch_imgs)):
batch_imgs[i].requires_grad = True
N = len(img_loader.dataset)
B = params.batch_size
batch_msgs = msgs[batch_iter*B : min((batch_iter+1)*B, N)].to(device, non_blocking=True)
optimizer = build_optimizer(model_params=batch_imgs, **utils.parse_params(params.optimizer))
if params.scheduler is not None:
scheduler = build_lr_scheduler(optimizer=optimizer, **utils.parse_params(params.scheduler))
# optimization
for iteration in range(params.epochs):
# Constraints and data augmentations
batch = []
for ii, x in enumerate(batch_imgs):
x = ssim.apply(x, batch_imgs_orig[ii])
x = utils_img.psnr_clip(x, batch_imgs_orig[ii], params.target_psnr)
if ii==0:
aug_params = transform.sample_params(x)
aug_img = transform(x, aug_params)
batch.append(aug_img)
batch = torch.cat(batch, dim=0) # BxCxWxH
# get features
ft = model(batch) # BxCxWxH -> BxD
# compute losses
loss_w = message_loss(ft, carrier, batch_msgs)
loss_i = 0
for ii in range(len(batch_imgs)):
loss_i += torch.norm(batch_imgs[ii] - batch_imgs_orig[ii])**2 # CxWxH -> 1
loss = params.lambda_w*loss_w + params.lambda_i*loss_i
# update images (gradient descent)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if params.scheduler is not None:
scheduler.step()
# logs
if params.verbose>1:
logs = {
"keyword": "img_optim",
"batch": batch_iter,
"iteration": iteration,
"loss": loss.item(),
"loss_w": loss_w.item(),
"loss_i": loss_i.item(),
}
if params.verbose>2:
dot_product = (ft @ carrier.T) # BxD @ DxK -> BxK
decoded_msgs = torch.sign(dot_product) > 0 # BxK -> BxK
diff = (~torch.logical_xor(batch_msgs, decoded_msgs)) # BxK -> BxK
bit_accs = torch.sum(diff, dim=-1)/diff.shape[-1] # BxK -> B
logs["bit_acc_avg"] = torch.mean(bit_accs).item()
logs["R_min_max"] = (torch.min(bit_accs).item(), torch.max(bit_accs).item())
print("__log__:%s" % json.dumps(logs))
# post process and store
for ii,x in enumerate(batch_imgs):
x = ssim.apply(x, batch_imgs_orig[ii])
x = utils_img.psnr_clip(x, batch_imgs_orig[ii], params.target_psnr)
x = utils_img.round_pixel(x)
# x = utils_img.project_linf(x, batch_imgs_orig[ii], params.linf_radius)
pt_imgs_out.append(x.squeeze(0).detach().cpu())
return pt_imgs_out # [CxW1xH1, ..., CxWnxHn]