-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
481 lines (391 loc) · 16.3 KB
/
predict.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# Native imports
import os
import argparse
import math
import numbers
from collections import namedtuple, defaultdict
import enum
# Torch imports
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import models
from torchvision import transforms
# Visualize imports
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
# App related
import streamlit as st
class WhichDatasets(enum.Enum):
IMAGENET_V1 = 0
PLACES_365 = 1
IMAGENET_V2 = 2
class WhichNetwork(enum.Enum):
VGG16 = 0
RESNET50 = 1
# Paths
DATA_DIR_PATH = os.path.join(os.getcwd(), 'data')
INPUT_DATA_PATH = os.path.join(DATA_DIR_PATH, 'input')
BINARIES_PATH = os.path.join(os.getcwd(), 'models', 'binaries')
OUT_IMAGES_PATH = os.path.join(DATA_DIR_PATH, 'out-images')
# Make director or return
os.makedirs(BINARIES_PATH, exist_ok = True)
os.makedirs(OUT_IMAGES_PATH, exist_ok = True)
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Normalization values for the images
IMAGENET_MEAN_1 = np.array([0.485, 0.456, 0.406], dtype=np.float32)
IMAGENET_STD_1 = np.array([0.229, 0.224, 0.225], dtype=np.float32)
class VGG(torch.nn.Module):
def __init__(self, pretrained_weights, requires_grad = False, show_progress = False):
super().__init__()
if pretrained_weights == WhichDatasets.IMAGENET.name:
vgg16 = models.vgg16(pretrained = True, progress = show_progress).eval()
else:
raise Exception("The VGG16 is not trained on {pretrained_weights} dataset")
# Layers to use
self.layer_names = ['relu2_2', 'relu3_3', 'relu4_1', 'relu4_2', 'relu4_3', 'relu5_1', 'relu5_2', 'relu5_3']
# Disect VGG
vgg_pretrained_features = vgg16.features
# 31 layers in total for the VGG16
self.conv1_1 = vgg_pretrained_features[0]
self.relu1_1 = vgg_pretrained_features[1]
self.conv1_2 = vgg_pretrained_features[2]
self.relu1_2 = vgg_pretrained_features[3]
self.max_pooling1 = vgg_pretrained_features[4]
self.conv2_1 = vgg_pretrained_features[5]
self.relu2_1 = vgg_pretrained_features[6]
self.conv2_2 = vgg_pretrained_features[7]
self.relu2_2 = vgg_pretrained_features[8]
self.max_pooling2 = vgg_pretrained_features[9]
self.conv3_1 = vgg_pretrained_features[10]
self.relu3_1 = vgg_pretrained_features[11]
self.conv3_2 = vgg_pretrained_features[12]
self.relu3_2 = vgg_pretrained_features[13]
self.conv3_3 = vgg_pretrained_features[14]
self.relu3_3 = vgg_pretrained_features[15]
self.max_pooling3 = vgg_pretrained_features[16]
self.conv4_1 = vgg_pretrained_features[17]
self.relu4_1 = vgg_pretrained_features[18]
self.conv4_2 = vgg_pretrained_features[19]
self.relu4_2 = vgg_pretrained_features[20]
self.conv4_3 = vgg_pretrained_features[21]
self.relu4_3 = vgg_pretrained_features[22]
self.max_pooling4 = vgg_pretrained_features[23]
self.conv5_1 = vgg_pretrained_features[24]
self.relu5_1 = vgg_pretrained_features[25]
self.conv5_2 = vgg_pretrained_features[26]
self.relu5_2 = vgg_pretrained_features[27]
self.conv5_3 = vgg_pretrained_features[28]
self.relu5_3 = vgg_pretrained_features[29]
self.max_pooling5 = vgg_pretrained_features[30]
if not requires_grad:
for param in self.parameters():
param.requires_grad = False
def forward(self, x):
x = self.conv1_1(x)
conv1_1 = x
x = self.relu1_1(x)
relu1_1 = x
x = self.conv1_2(x)
conv1_2 = x
x = self.relu1_2(x)
relu1_2 = x
x = self.max_pooling1(x)
x = self.conv2_1(x)
conv2_1 = x
x = self.relu2_1(x)
relu2_1 = x
x = self.conv2_2(x)
conv2_2 = x
x = self.relu2_2(x)
relu2_2 = x
x = self.max_pooling2(x)
x = self.conv3_1(x)
conv3_1 = x
x = self.relu3_1(x)
relu3_1 = x
x = self.conv3_2(x)
conv3_2 = x
x = self.relu3_2(x)
relu3_2 = x
x = self.conv3_3(x)
conv3_3 = x
x = self.relu3_3(x)
relu3_3 = x
x = self.max_pooling3(x)
x = self.conv4_1(x)
conv4_1 = x
x = self.relu4_1(x)
relu4_1 = x
x = self.conv4_2(x)
conv4_2 = x
x = self.relu4_2(x)
relu4_2 = x
x = self.conv4_3(x)
conv4_3 = x
x = self.relu4_3(x)
relu4_3 = x
x = self.max_pooling4(x)
x = self.conv5_1(x)
conv5_1 = x
x = self.relu5_1(x)
relu5_1 = x
x = self.conv5_2(x)
conv5_2 = x
x = self.relu5_2(x)
relu5_2 = x
x = self.conv5_3(x)
conv5_3 = x
x = self.relu5_3(x)
relu5_3 = x
mp5 = self.max_pooling5(x)
# Get the outputs from the layers we want
vgg_outputs = namedtuple("VggOutputs", self.layer_names)
out = vgg_outputs(relu2_2, relu3_3, relu4_1, relu4_2, relu4_3, relu5_1, relu5_2, relu5_3)
return out
class ResNet50(torch.nn.Module):
def __init__(self, pretrained_weights, requires_grad=False, show_progress=False):
super().__init__()
if pretrained_weights == WhichDatasets.IMAGENET_V1.name:
#print("Using Version 1")
resnet50 = models.resnet50(pretrained=True, progress=show_progress).eval()
elif pretrained_weights == WhichDatasets.IMAGENET_V2.name:
#print("Using Version 2")
resnet50 = models.resnet50(weights = models.ResNet50_Weights.IMAGENET1K_V2, progress = show_progress).eval()
else:
print("Error loading REsnet")
exit(0)
self.layer_names = ['layer1', 'layer2', 'layer3', 'layer4', 'layer5']
self.conv1 = resnet50.conv1
self.bn1 = resnet50.bn1
self.relu = resnet50.relu
self.maxpool = resnet50.maxpool
# 3
self.layer10 = resnet50.layer1[0]
self.layer11 = resnet50.layer1[1]
self.layer12 = resnet50.layer1[2]
# 4
self.layer20 = resnet50.layer2[0]
self.layer21 = resnet50.layer2[1]
self.layer22 = resnet50.layer2[2]
self.layer23 = resnet50.layer2[3]
# 6
self.layer30 = resnet50.layer3[0]
self.layer31 = resnet50.layer3[1]
self.layer32 = resnet50.layer3[2]
self.layer33 = resnet50.layer3[3]
self.layer34 = resnet50.layer3[4]
self.layer35 = resnet50.layer3[5]
# 3
self.layer40 = resnet50.layer4[0]
self.layer41 = resnet50.layer4[1]
# self.layer42 = resnet50.layer4[2]
# Go even deeper into ResNet's BottleNeck module for layer 42
self.layer42_conv1 = resnet50.layer4[2].conv1
self.layer42_bn1 = resnet50.layer4[2].bn1
self.layer42_conv2 = resnet50.layer4[2].conv2
self.layer42_bn2 = resnet50.layer4[2].bn2
self.layer42_conv3 = resnet50.layer4[2].conv3
self.layer42_bn3 = resnet50.layer4[2].bn3
self.layer42_relu = resnet50.layer4[2].relu
# Set these to False so that PyTorch won't be including them in its autograd engine - eating up precious memory
if not requires_grad:
for param in self.parameters():
param.requires_grad = False
# Feel free to experiment with different layers
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer10(x)
layer10 = x
x = self.layer11(x)
layer11 = x
x = self.layer12(x)
layer12 = x
x = self.layer20(x)
layer20 = x
x = self.layer21(x)
layer21 = x
x = self.layer22(x)
layer22 = x
x = self.layer23(x)
layer23 = x
x = self.layer30(x)
layer30 = x
x = self.layer31(x)
layer31 = x
x = self.layer32(x)
layer32 = x
x = self.layer33(x)
layer33 = x
x = self.layer34(x)
layer34 = x
x = self.layer35(x)
layer35 = x
x = self.layer40(x)
layer40 = x
x = self.layer41(x)
layer41 = x
layer42_identity = layer41
x = self.layer42_conv1(x)
layer420 = x
x = self.layer42_bn1(x)
layer421 = x
x = self.layer42_relu(x)
layer422 = x
x = self.layer42_conv2(x)
layer423 = x
x = self.layer42_bn2(x)
layer424 = x
x = self.layer42_relu(x)
layer425 = x
x = self.layer42_conv3(x)
layer426 = x
x = self.layer42_bn3(x)
layer427 = x
x += layer42_identity
layer428 = x
x = self.relu(x)
layer429 = x
# Feel free to experiment with different layers, layer35 is my favourite
net_outputs = namedtuple("ResNet50Outputs", self.layer_names)
# You can see the potential ambiguity arising here if we later want to reconstruct images purely from the filename
out = net_outputs(layer10, layer23, layer34, layer40, layer425)
return out
def fetch_and_prepare_model(model_type, pretrained_weights):
if model_type == WhichNetwork.VGG16.name:
model = VGG(pretrained_weights, requires_grad = False, show_progress = True).to(DEVICE)
elif model_type == WhichNetwork.RESNET50.name:
model = ResNet50(pretrained_weights, requires_grad = False, show_progress = True).to(DEVICE)
else:
raise Exception(" {model_type} model not yet supported")
torch.save(model, 'model.pt')
return model
# Utilities
def preprocess_numpy_img(img):
assert isinstance(img, np.ndarray), f'Expected numpy image got {type(img)}'
img = (img - IMAGENET_MEAN_1) / IMAGENET_STD_1
return img
def postprocess_nump_img(img):
assert isinstance(img, np.ndarray), f'Expected numpy image got {type(img)}'
if img.shape[0] == 3: # if channel-first format move to channel-last (CHW -> HWC)
img = np.moveaxis(img, 0, 2)
mean = IMAGENET_MEAN_1.reshape(1, 1, -1)
std = IMAGENET_STD_1.reshape(1, 1, -1)
img = (img * std) + mean # de-normalize
img = np.clip(img, 0., 1.) # make sure it's in the [0, 1] range
return img
def pytorch_input_adapter(img):
# shape = (1, 3, H, W)
tensor = transforms.ToTensor()(img).to(DEVICE).unsqueeze(0)
tensor.requires_grad = True # we need to collect gradients for the input image
return tensor
def pytorch_output_adapter(tensor):
# Push to CPU, detach from the computational graph, convert from (1, 3, H, W) tensor into (H, W, 3) numpy image
return np.moveaxis(tensor.to('cpu').detach().numpy()[0], 0, 2)
# Adds stochasticity to the algorithm and makes the results more diverse
def random_circular_spatial_shift(tensor, h_shift, w_shift, should_undo=False):
if should_undo:
h_shift = -h_shift
w_shift = -w_shift
with torch.no_grad():
rolled = torch.roll(tensor, shifts=(h_shift, w_shift), dims=(2, 3))
rolled.requires_grad = True
return rolled
def get_new_shape(config, original_shape, current_pyramid_level):
SHAPE_MARGIN = 10
pyramid_ratio = config['pyramid_ratio']
pyramid_size = config['pyramid_size']
exponent = current_pyramid_level - pyramid_size + 1
new_shape = np.round(np.float32(original_shape) * (pyramid_ratio**exponent)).astype(np.int32)
if new_shape[0] < SHAPE_MARGIN or new_shape[1] < SHAPE_MARGIN:
print("Pyramid became too small")
exit(0)
return new_shape
def deep_dream_static(config, img = None):
model = fetch_and_prepare_model(config['model_name'], config['pretrained_weights'])
layer_ids_to_use = [model.layer_names.index(layer) for layer in config['layers_to_use']]
if img is None:
print("No image received in Deep Dream Static")
exit(0)
img = preprocess_numpy_img(img)
original_shape = img.shape[:-1]
for pyramid_level in range(config['pyramid_size']):
new_shape = get_new_shape(config, original_shape, pyramid_level)
img = cv.resize(img, (new_shape[1], new_shape[0])) # resize depending on the current pyramid level
input_tensor = pytorch_input_adapter(img)
for iteration in range(config['num_gradient_ascent_iterations']):
h_shift, w_shift = np.random.randint(-config['spatial_shift_size'], config['spatial_shift_size'] + 1, 2)
input_tensor = random_circular_spatial_shift(input_tensor, h_shift, w_shift)
gradient_ascent(config, model, input_tensor, layer_ids_to_use, iteration)
input_tensor = random_circular_spatial_shift(input_tensor, h_shift, w_shift, should_undo=True)
img = pytorch_output_adapter(input_tensor)
return postprocess_nump_img(img)
LOWER_IMAGE_BOUND = torch.tensor((-IMAGENET_MEAN_1 / IMAGENET_STD_1).reshape(1, -1, 1, 1)).to(DEVICE)
UPPER_IMAGE_BOUND = torch.tensor(((1 - IMAGENET_MEAN_1) / IMAGENET_STD_1).reshape(1, -1, 1, 1)).to(DEVICE)
def gradient_ascent(config, model, input_tensor, layer_ids_to_use, iteration):
# FeedForward
out = model(input_tensor)
activations = [out[layer_id_to_use] for layer_id_to_use in layer_ids_to_use]
# Calculate loss over activations
losses = []
for layer_activation in activations:
loss_component = torch.nn.MSELoss(reduction='mean')(layer_activation, torch.zeros_like(layer_activation))
losses.append(loss_component)
loss = torch.mean(torch.stack(losses))
loss.backward()
# Process image gradients (smoothing + normalization)
grad = input_tensor.grad.data
sigma = ((iteration + 1) / config['num_gradient_ascent_iterations']) * 2.0 + config['smoothing_coefficient']
smooth_grad = CascadeGaussianSmoothing(kernel_size=9, sigma=sigma)(grad)
g_std = torch.std(smooth_grad)
g_mean = torch.mean(smooth_grad)
smooth_grad = smooth_grad - g_mean
smooth_grad = smooth_grad / g_std
input_tensor.data += config['lr'] * smooth_grad
input_tensor.grad.data.zero_()
input_tensor.data = torch.max(torch.min(input_tensor, UPPER_IMAGE_BOUND), LOWER_IMAGE_BOUND)
class CascadeGaussianSmoothing(nn.Module):
def __init__(self, kernel_size, sigma):
super().__init__()
if isinstance(kernel_size, numbers.Number):
kernel_size = [kernel_size, kernel_size]
cascade_coefficients = [0.5, 1.0, 2.0] # std multipliers, hardcoded to use 3 different Gaussian kernels
sigmas = [[coeff * sigma, coeff * sigma] for coeff in cascade_coefficients] # isotropic Gaussian
self.pad = int(kernel_size[0] / 2) # assure we have the same spatial resolution
# The gaussian kernel is the product of the gaussian function of each dimension.
kernels = []
meshgrids = torch.meshgrid([torch.arange(size, dtype=torch.float32) for size in kernel_size])
for sigma in sigmas:
kernel = torch.ones_like(meshgrids[0])
for size_1d, std_1d, grid in zip(kernel_size, sigma, meshgrids):
mean = (size_1d - 1) / 2
kernel *= 1 / (std_1d * math.sqrt(2 * math.pi)) * torch.exp(-((grid - mean) / std_1d) ** 2 / 2)
kernels.append(kernel)
gaussian_kernels = []
for kernel in kernels:
# Normalize - make sure sum of values in gaussian kernel equals 1.
kernel = kernel / torch.sum(kernel)
# Reshape to depthwise convolutional weight
kernel = kernel.view(1, 1, *kernel.shape)
kernel = kernel.repeat(3, 1, 1, 1)
kernel = kernel.to(DEVICE)
gaussian_kernels.append(kernel)
self.weight1 = gaussian_kernels[0]
self.weight2 = gaussian_kernels[1]
self.weight3 = gaussian_kernels[2]
self.conv = F.conv2d
def forward(self, input):
input = F.pad(input, [self.pad, self.pad, self.pad, self.pad], mode='reflect')
# Apply Gaussian kernels depthwise over the input (hence groups equals the number of input channels)
# shape = (1, 3, H, W) -> (1, 3, H, W)
num_in_channels = input.shape[1]
grad1 = self.conv(input, weight=self.weight1, groups=num_in_channels)
grad2 = self.conv(input, weight=self.weight2, groups=num_in_channels)
grad3 = self.conv(input, weight=self.weight3, groups=num_in_channels)
return (grad1 + grad2 + grad3) / 3
def save_image(img, path):
cv.imwrite(path, img)