-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinvert.py
204 lines (153 loc) · 6.05 KB
/
invert.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
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision.transforms as transforms
import torchvision.models as models
import numpy as np
import os
import matplotlib.pyplot as plt
from PIL import Image
def alpha_prior(x, alpha=2.):
return torch.abs(x.view(-1)**alpha).sum()
def tv_norm(x, beta=2.):
assert(x.size(0) == 1)
img = x[0]
dy = img - img # set size of derivative and set border = 0
dx = img - img
dy[:,1:,:] = -img[:,:-1,:] + img[:,1:,:]
dx[:,:,1:] = -img[:,:,:-1] + img[:,:,1:]
return ((dx.pow(2) + dy.pow(2)).pow(beta/2.)).sum()
def norm_loss(input, target):
return torch.div(alpha_prior(input - target, alpha=2.), alpha_prior(target, alpha=2.))
class Denormalize(object):
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, tensor):
for t, m, s in zip(tensor, self.mean, self.std):
t.mul_(s).add_(m)
return tensor
class Clip(object):
def __init__(self):
return
def __call__(self, tensor):
t = tensor.clone()
t[t>1] = 1
t[t<0] = 0
return t
#function to decay the learning rate
def decay_lr(optimizer, factor):
for param_group in optimizer.param_groups:
param_group['lr'] *= factor
def get_pytorch_module(net, blob):
modules = blob.split('.')
if len(modules) == 1:
return net._modules.get(blob)
else:
curr_m = net
for m in modules:
curr_m = curr_m._modules.get(m)
return curr_m
def invert(image, network='alexnet', size=227, layer='features.4', alpha=6, beta=2,
alpha_lambda=1e-5, tv_lambda=1e-5, epochs=200, learning_rate=1e2,
momentum=0.9, decay_iter=100, decay_factor=1e-1, print_iter=25,
cuda=False):
mu = [0.485, 0.456, 0.406]
sigma = [0.229, 0.224, 0.225]
transform = transforms.Compose([
transforms.Scale(size=size),
transforms.CenterCrop(size=size),
transforms.ToTensor(),
transforms.Normalize(mu, sigma),
])
detransform = transforms.Compose([
Denormalize(mu, sigma),
Clip(),
transforms.ToPILImage(),
])
model = models.__dict__[network](pretrained=True)
model.eval()
if cuda:
model.cuda()
img_ = transform(Image.open(image)).unsqueeze(0)
print img_.size()
activations = []
def hook_acts(module, input, output):
activations.append(output)
def get_acts(model, input):
del activations[:]
_ = model(input)
assert(len(activations) == 1)
return activations[0]
_ = get_pytorch_module(model, layer).register_forward_hook(hook_acts)
input_var = Variable(img_.cuda() if cuda else img_)
ref_acts = get_acts(model, input_var).detach()
x_ = Variable((1e-3 * torch.randn(*img_.size()).cuda() if cuda else
1e-3 * torch.randn(*img_.size())), requires_grad=True)
alpha_f = lambda x: alpha_prior(x, alpha=alpha)
tv_f = lambda x: tv_norm(x, beta=beta)
loss_f = lambda x: norm_loss(x, ref_acts)
optimizer = torch.optim.SGD([x_], lr=learning_rate, momentum=momentum)
for i in range(epochs):
acts = get_acts(model, x_)
alpha_term = alpha_f(x_)
tv_term = tv_f(x_)
loss_term = loss_f(acts)
tot_loss = alpha_lambda*alpha_term + tv_lambda*tv_term + loss_term
if (i+1) % print_iter == 0:
print('Epoch %d:\tAlpha: %f\tTV: %f\tLoss: %f\tTot Loss: %f' % (i+1,
alpha_term.data.cpu().numpy()[0], tv_term.data.cpu().numpy()[0],
loss_term.data.cpu().numpy()[0], tot_loss.data.cpu().numpy()[0]))
optimizer.zero_grad()
tot_loss.backward()
optimizer.step()
if (i+1) % decay_iter == 0:
decay_lr(optimizer, decay_factor)
f, ax = plt.subplots(1,2)
ax[0].imshow(detransform(img_[0]))
ax[1].imshow(detransform(x_[0].data.cpu()))
for a in ax:
a.set_xticks([])
a.set_yticks([])
plt.show()
if __name__ == '__main__':
import argparse
import sys
import traceback
try:
parser = argparse.ArgumentParser()
parser.add_argument('--image', type=str,
default='grace_hopper.jpg')
parser.add_argument('--network', type=str, default='alexnet')
parser.add_argument('--size', type=int, default=227)
parser.add_argument('--layer', type=str, default='features.4')
parser.add_argument('--alpha', type=float, default=6.)
parser.add_argument('--beta', type=float, default=2.)
parser.add_argument('--alpha_lambda', type=float, default=1e-5)
parser.add_argument('--tv_lambda', type=float, default=1e-5)
parser.add_argument('--epochs', type=int, default=200)
parser.add_argument('--learning_rate', type=int, default=1e2)
parser.add_argument('--momentum', type=float, default=0.9)
parser.add_argument('--print_iter', type=int, default=25)
parser.add_argument('--decay_iter', type=int, default=100)
parser.add_argument('--decay_factor', type=float, default=1e-1)
parser.add_argument('--gpu', type=int, nargs='*', default=None)
args = parser.parse_args()
gpu = args.gpu
cuda = True if gpu is not None else False
use_mult_gpu = isinstance(gpu, list)
if cuda:
if use_mult_gpu:
os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu).strip('[').strip(']')
else:
os.environ['CUDA_VISIBLE_DEVICES'] = '%d' % gpu
print(torch.cuda.device_count(), use_mult_gpu, cuda)
invert(image=args.image, network=args.network, layer=args.layer,
alpha=args.alpha, beta=args.beta, alpha_lambda=args.alpha_lambda,
tv_lambda=args.tv_lambda, epochs=args.epochs,
learning_rate=args.learning_rate, momentum=args.momentum,
print_iter=args.print_iter, decay_iter=args.decay_iter,
decay_factor=args.decay_factor, cuda=cuda)
except:
traceback.print_exc(file=sys.stdout)
sys.exit(1)