-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathresnet_swag.py
322 lines (249 loc) · 10.2 KB
/
resnet_swag.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
from PIL import Image
import numpy as np
import argparse
import torch
import torch.optim as optim
from torchvision import transforms, models
import torch.nn as nn
import torch.nn.functional as F
import os
import glob
import cv2
parser = argparse.ArgumentParser(description='PyTorch SWAG')
parser.add_argument('-a', '--arch', default='resnet50_swag', help='architecture name')
global args
args = parser.parse_args()
gpu = '0'
gpu = gpu.split(',')
os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(gpu)
os.environ['QT_QPA_PLATFORM'] = 'offscreen'
def load_image(img_path, max_size=400, shape=None):
image = Image.open(img_path).convert('RGB')
if max(image.size) > max_size:
size = max_size
else:
size = max(image.size)
if shape is not None:
size = shape
in_transform = transforms.Compose([
transforms.Resize((512, 512)),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))])
image = in_transform(image)[:3, :, :].unsqueeze(0)
return image
def im_convert(tensor):
image = tensor.to("cpu").clone().detach()
image = image.numpy().squeeze()
image = image.transpose(1, 2, 0)
image = image * np.array((0.229, 0.224, 0.225)) + np.array(
(0.485, 0.456, 0.406))
image = image.clip(0, 1)
return image
def save_checkpoint(state, filename='checkpoint_res.pth.tar'):
torch.save(state, filename)
def get_features(image, model, layers=None):
if ARCHITECTURE == 'resnet50_swag':
if layers is None:
layers1 = {'0': 'conv1_0',
'1': 'conv1_1',
'2': 'conv1_2'
}
layers2 = {'0': 'conv2_0',
'1': 'conv2_1',
'2': 'conv2_2',
'3': 'conv2_3'
}
layers3 = {'0': 'conv3_0',
'1': 'conv3_1',
'2': 'conv3_2',
'3': 'conv3_3',
'4': 'conv3_4',
'5': 'conv3_5'
}
layers4 = {'0': 'conv4_0',
'1': 'conv4_1',
'2': 'conv4_2'
}
# softmax = nn.Softmax2d()
# T = 100
T = 1
alpha = 0.001
features = {}
x = image
x = model.conv1(x)
x = model.bn1(x)
x = model.relu(x)
features['conv0_0'] = x
# features['conv0_0'] = softmax(x / T)
# features['conv0_0'] = softmax3d(x / T)
# features['conv0_0'] = alpha * x
x = model.maxpool(x)
features['conv0_1'] = x
# features['conv0_1'] = softmax(x / T)
# features['conv0_1'] = softmax3d(x / T)
# features['conv0_1'] = alpha * x
# Although we found adding softmax smoothing can always improve the results, the best results sometimes are obtained by only smoothing the deeper layers,
# as the paper suggests, deeper layers are more peaky and have small entropy
for name, layer in enumerate(model.layer1):
x = layer(x)
if str(name) in layers1:
features[layers1[str(name)]] = x
# features[layers1[str(name)]] = softmax(x / T)
# features[layers1[str(name)]] = softmax3d(x / T)
# features[layers1[str(name)]] = alpha * x
for name, layer in enumerate(model.layer2):
x = layer(x)
if str(name) in layers2:
features[layers2[str(name)]] = x
# features[layers2[str(name)]] = softmax(x / T)
# features[layers2[str(name)]] = softmax3d(x / T)
# features[layers2[str(name)]] = alpha * x
for name, layer in enumerate(model.layer3):
x = layer(x)
if str(name) in layers3:
# features[layers3[str(name)]] = softmax(x / T)
# features[layers3[str(name)]] = softmax3d(x / T)
features[layers3[str(name)]] = alpha * x
for name, layer in enumerate(model.layer4):
x = layer(x)
if str(name) in layers4:
# features[layers4[str(name)]] = softmax(x / T)
# features[layers4[str(name)]] = softmax3d(x / T)
features[layers4[str(name)]] = alpha * x
else:
if layers is None:
layers1 = {'0': 'conv1_0',
'1': 'conv1_1',
'2': 'conv1_2'
}
layers2 = {'0': 'conv2_0',
'1': 'conv2_1',
'2': 'conv2_2',
'3': 'conv2_3'
}
layers3 = {'0': 'conv3_0',
'1': 'conv3_1',
'2': 'conv3_2',
'3': 'conv3_3',
'4': 'conv3_4',
'5': 'conv3_5'
}
layers4 = {'0': 'conv4_0',
'1': 'conv4_1',
'2': 'conv4_2'
}
features = {}
x = image
x = model.conv1(x)
x = model.bn1(x)
x = model.relu(x)
features['conv0_0'] = x
x = model.maxpool(x)
for name, layer in enumerate(model.layer1):
x = layer(x)
if str(name) in layers1:
features[layers1[str(name)]] = x
for name, layer in enumerate(model.layer2):
x = layer(x)
if str(name) in layers2:
features[layers2[str(name)]] = x
for name, layer in enumerate(model.layer3):
x = layer(x)
if str(name) in layers3:
features[layers3[str(name)]] = x
for name, layer in enumerate(model.layer4):
x = layer(x)
if str(name) in layers4:
features[layers4[str(name)]] = x
return features
def softmax3d(input):
m = nn.Softmax()
a, b, c, d = input.size()
input = torch.reshape(input, (1, -1))
output = m(input)
output = torch.reshape(output, (a, b, c, d))
return output
def gram_matrix(input):
a, b, c, d = input.size() # a=batch size(=1)
# b=number of feature maps
# (c,d)=dimensions of a f. map (N=c*d)
features = input.view(a * b, c * d) # resise F_XL into \hat F_XL
G = torch.mm(features, features.t()) # compute the gram product
# we 'normalize' the values of the gram matrix
# by dividing by the number of element in each feature maps.
return G.div(a * b * c * d)
def style_transfer(model, style, content, target, style_layer_weights, content_layer_weights, style_weight,
content_weight, optimizer):
content_features = get_features(content, model)
style_features = get_features(style, model)
style_grams = {layer: gram_matrix(style_features[layer]) for layer in style_features}
run = [0]
while run[0] <= 1000:
def closure():
optimizer.zero_grad()
target_features = get_features(target, model)
content_loss = torch.mean((target_features[content_layer_weights] -
content_features[content_layer_weights]) ** 2)
style_loss = 0
for layer in style_layer_weights:
target_feature = target_features[layer]
target_gram = gram_matrix(target_feature)
# _, d, h, w = target_feature.shape
style_gram = style_grams[layer]
layer_style_loss = style_layer_weights[layer] * torch.mean(
(target_gram - style_gram) ** 2)
style_loss += style_weight * layer_style_loss
total_loss = content_weight * content_loss + style_loss
total_loss.backward()
if run[0] % 500 == 0:
print("run {}:".format(run))
print('total Loss : {:4f}'.format(total_loss.item()))
run[0] += 1
return content_weight * content_loss + style_loss
optimizer.step(closure)
final_img = im_convert(target)
return final_img
# load model
ARCHITECTURE = args.arch # 'resnet50', 'resnet50_swag'
resnet = models.resnet50(pretrained=True)
for param in resnet.parameters():
param.requires_grad_(False)
torch.cuda.is_available()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
resnet.to(device).eval()
# load style and content images combinations
style_image_list = []
for file in glob.glob('./style_images/*'):
style_image_list.append(file)
content_image_list = []
for file in glob.glob('./content_images/*'):
content_image_list.append(file)
for i_style in range(len(style_image_list)):
style_img_name = style_image_list[i_style].split('/')
style_img_name = style_img_name[-1].split('.')
style_img_name = style_img_name[0].split('\\')[-1]
for i_content in range(len(content_image_list)):
print('processing content', i_content, ' style ', i_style)
style = load_image(style_image_list[i_style]).to(device)
content = load_image(content_image_list[i_content]).to(device)
target = content.clone().requires_grad_(True).to(device)
style_weights = {'conv0_0': 1.0,
'conv1_2': 1.0,
'conv2_3': 1.0,
'conv3_5': 1.0,
'conv4_2': 1.0}
content_weights = 'conv3_5'
content_weight = 1
style_weight = 1e17
optimizer = optim.LBFGS([target])
final_styled = style_transfer(resnet, style, content, target, style_weights, content_weights,
style_weight, content_weight, optimizer)
content_img_name = content_image_list[i_content].split('/')
content_img_name = content_img_name[-1].split('.')
content_img_name = content_img_name[0].split('\\')[-1]
if not os.path.exists('./results/' + ARCHITECTURE):
os.makedirs('./results/' + ARCHITECTURE)
save_path_cv2 = './results/' + ARCHITECTURE + '/' + content_img_name + '_' + style_img_name + '_cv2.png'
final_styled_cv2 = np.uint8(255 * final_styled)
final_styled_cv2_bgr = final_styled_cv2[:, :, [2, 1, 0]]
cv2.imwrite(save_path_cv2, final_styled_cv2_bgr)