-
Notifications
You must be signed in to change notification settings - Fork 26
/
encoding.py
150 lines (107 loc) · 4.12 KB
/
encoding.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
import random, sys, os, json, glob, argparse
import numpy as np
import matplotlib as mpl
mpl.use("Agg")
import matplotlib.pyplot as plt
from fire import Fire
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
from models import DecodingModel, DataParallelModel
from torchvision import models
from logger import Logger, VisdomLogger
from utils import *
import IPython
import transforms
# LOGGING
logger = VisdomLogger("encoding", server="35.230.67.129", port=8000, env=JOB)
logger.add_hook(lambda x: logger.step(), feature="epoch", freq=20)
logger.add_hook(lambda x: logger.plot(x, "Encoding Loss", opts=dict(ymin=0)), feature="loss", freq=50)
"""
Computes the changed images, given a a specified perturbation, standard deviation weighting,
and epsilon.
"""
def compute_changed_images(images, perturbation, std_weights, epsilon=EPSILON):
perturbation_w2 = perturbation * std_weights
perturbation_zc = (
perturbation_w2
/ perturbation_w2.view(perturbation_w2.shape[0], -1)
.norm(2, dim=1, keepdim=True)
.unsqueeze(2)
.unsqueeze(2)
.expand_as(perturbation_w2)
* epsilon
* (perturbation_w2[0].nelement() ** 0.5)
)
changed_images = (images + perturbation_zc).clamp(min=0.0, max=1.0)
return changed_images
"""
Computes the cross entropy loss of a set of encoded images, given the model and targets.
"""
def loss_func(model, x, targets):
scores = model.forward(x)
predictions = scores.mean(dim=1)
score_targets = binary.target(targets).unsqueeze(1).expand_as(scores)
return (F.binary_cross_entropy(scores, score_targets), predictions.cpu().data.numpy().round(2))
"""
Encodes a set of images with the specified binary targets, for a given number of iterations.
"""
def encode_binary(
images, targets, model=DecodingModel(), n=None, max_iter=500, verbose=False, perturbation=None, use_weighting=False
):
if n is not None:
if verbose:
print(f"Changing distribution size: {model.n} -> {n}")
n, model.n = (model.n, n)
returnPerturbation = True
if perturbation is None:
perturbation = nn.Parameter(0.03 * torch.randn(images.size()).to(DEVICE) + 0.0)
returnPerturbation = False
changed_images = images.detach()
optimizer = torch.optim.Adam([perturbation], lr=ENCODING_LR)
std_weights = get_std_weight(images, alpha=PERT_ALPHA) if use_weighting else 1
for i in range(0, max_iter):
changed_images = compute_changed_images(images, perturbation, std_weights)
loss, predictions = loss_func(model, changed_images, targets)
loss.backward()
optimizer.step()
optimizer.zero_grad()
error = np.mean([binary.distance(x, y) for x, y in zip(predictions, targets)])
if verbose:
logger.update("epoch", i)
logger.update("loss", loss)
logger.update("bits", error)
changed_images = compute_changed_images(images, perturbation, std_weights)
if n is not None:
if verbose:
print(f"Fixing distribution size: {model.n} -> {n}")
n, model.n = (model.n, n)
if returnPerturbation:
return changed_images.detach(), perturbation.detach()
return changed_images.detach()
"""
Command-line interface for encoding a single image.
"""
def encode(
image,
out,
target=binary.str(binary.random(TARGET_SIZE)),
n=96,
model=None,
max_iter=500,
use_weighting=True,
perturbation_out=None,
):
if not isinstance(model, DecodingModel):
model = DataParallelModel(DecodingModel.load(distribution=transforms.encoding, n=n, weights_file=model))
image = im.torch(im.load(image)).unsqueeze(0)
print("Target: ", target)
target = binary.parse(str(target))
encoded = encode_binary(image, [target], model, n=n, verbose=True, max_iter=max_iter, use_weighting=use_weighting)
im.save(im.numpy(encoded.squeeze()), file=out)
if perturbation_out != None:
im.save(im.numpy((image - encoded).squeeze()), file=perturbation_out)
if __name__ == "__main__":
Fire(encode)