-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.py
More file actions
287 lines (230 loc) · 9.77 KB
/
Copy pathtransfer.py
File metadata and controls
287 lines (230 loc) · 9.77 KB
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
# -*- coding: utf-8 -*-
"""
Created on Jul 01, 2020
@author: yongzhengxin
"""
import collections
import pathlib
import random
import pickle
from typing import Dict, Tuple, Sequence
import cv2
from skimage.color import rgb2lab, lab2rgb
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
import torch
from torch import nn
import torch.nn.functional as F
from torchvision import transforms
from torch.autograd import Variable
device = "cuda" if torch.cuda.is_available() else "cpu"
from functools import partial
class Conv2dAuto(nn.Conv2d):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.padding = (
self.kernel_size[0] // 2, self.kernel_size[1] // 2) # dynamic add padding based on the kernel_size
conv3x3 = partial(Conv2dAuto, kernel_size=3, bias=False)
def activation_func(activation):
return nn.ModuleDict([
['relu', nn.ReLU(inplace=True)],
['leaky_relu', nn.LeakyReLU(negative_slope=0.01, inplace=True)],
['selu', nn.SELU(inplace=True)],
['none', nn.Identity()]
])[activation]
def conv_bn(in_channels, out_channels, conv, *args, **kwargs):
return nn.Sequential(conv(in_channels, out_channels, *args, **kwargs), nn.InstanceNorm2d(out_channels))
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, activation='relu'):
super().__init__()
self.in_channels, self.out_channels, self.activation = in_channels, out_channels, activation
self.blocks = nn.Identity()
self.activate = activation_func(activation)
self.shortcut = nn.Identity()
def forward(self, x):
residual = x
if self.should_apply_shortcut: residual = self.shortcut(x)
x = self.blocks(x)
x += residual
x = self.activate(x)
return x
@property
def should_apply_shortcut(self):
return self.in_channels != self.out_channels
class ResNetResidualBlock(ResidualBlock):
def __init__(self, in_channels, out_channels, expansion=1, downsampling=1, conv=conv3x3, *args, **kwargs):
super().__init__(in_channels, out_channels, *args, **kwargs)
self.expansion, self.downsampling, self.conv = expansion, downsampling, conv
self.shortcut = nn.Sequential(
nn.Conv2d(self.in_channels, self.expanded_channels, kernel_size=1,
stride=self.downsampling, bias=False),
nn.BatchNorm2d(self.expanded_channels)) if self.should_apply_shortcut else None
@property
def expanded_channels(self):
return self.out_channels * self.expansion
@property
def should_apply_shortcut(self):
return self.in_channels != self.expanded_channels
class ResNetBasicBlock(ResNetResidualBlock):
"""
Basic ResNet block composed by two layers of 3x3conv/batchnorm/activation
"""
expansion = 1
def __init__(self, in_channels, out_channels, *args, **kwargs):
super().__init__(in_channels, out_channels, *args, **kwargs)
self.blocks = nn.Sequential(
conv_bn(self.in_channels, self.out_channels, conv=self.conv, bias=False, stride=self.downsampling),
activation_func(self.activation),
conv_bn(self.out_channels, self.expanded_channels, conv=self.conv, bias=False),
)
class ResNetLayer(nn.Module):
"""
A ResNet layer composed by `n` blocks stacked one after the other
"""
def __init__(self, in_channels, out_channels, block=ResNetBasicBlock, n=1, *args, **kwargs):
super().__init__()
# 'We perform downsampling directly by convolutional layers that have a stride of 2.'
downsampling = 2 if in_channels != out_channels else 1
self.blocks = nn.Sequential(
block(in_channels, out_channels, *args, **kwargs, downsampling=downsampling),
*[block(out_channels * block.expansion,
out_channels, downsampling=1, *args, **kwargs) for _ in range(n - 1)]
)
def forward(self, x):
x = self.blocks(x)
return x
class FeatureEncoder(nn.Module):
def __init__(self):
super(FeatureEncoder, self).__init__()
# convolutional
self.conv1_1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
self.norm1_1 = nn.InstanceNorm2d(64)
self.pool1 = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
# residual blocks
self.res1 = ResNetLayer(64, 128, block=ResNetBasicBlock, n=1)
self.res2 = ResNetLayer(128, 256, block=ResNetBasicBlock, n=1)
self.res3 = ResNetLayer(256, 512, block=ResNetBasicBlock, n=1)
def forward(self, x):
x = F.relu(self.norm1_1(self.conv1_1(x)))
c4 = self.pool1(x)
c3 = self.res1(c4)
c2 = self.res2(c3)
c1 = self.res3(c2)
return c1, c2, c3, c4
def double_conv(in_channels, out_channels):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, 3, padding=1),
nn.InstanceNorm2d(out_channels),
nn.Conv2d(out_channels, out_channels, 3, padding=1),
nn.InstanceNorm2d(out_channels),
)
class RecoloringDecoder(nn.Module):
# c => (bz, channel, h, w)
# [Pt, c1]: (18 + 512) -> (256)
# [c2, d1]: (256 + 256) -> (128)
# [Pt, c3, d2]: (18 + 128 + 128) -> (64)
# [Pt, c4, d3]: (18 + 64 + 64) -> 64
# [Illu, d4]: (1 + 64) -> 3
def __init__(self):
super().__init__()
self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
self.dconv_up_4 = double_conv(18 + 512, 256)
self.dconv_up_3 = double_conv(256 + 256, 128)
self.dconv_up_2 = double_conv(18 + 128 + 128, 64)
self.dconv_up_1 = double_conv(18 + 64 + 64, 64)
self.conv_last = nn.Conv2d(1 + 64, 3, 3, padding=1)
def forward(self, c1, c2, c3, c4, target_palettes_1d, illu):
bz, h, w = c1.shape[0], c1.shape[2], c1.shape[3]
target_palettes = torch.ones(bz, 18, h, w).float().to(device)
target_palettes = target_palettes.reshape(h, w, bz * 18) * target_palettes_1d
target_palettes = target_palettes.permute(2, 0, 1).reshape(bz, 18, h, w)
# concatenate target_palettes with c1
x = torch.cat((c1.float(), target_palettes.float()), 1)
x = self.dconv_up_4(x)
x = self.upsample(x)
# concatenate c2 with x
x = torch.cat([c2, x], dim=1)
x = self.dconv_up_3(x)
x = self.upsample(x)
# concatenate target_palettes and c3 with x
bz, h, w = x.shape[0], x.shape[2], x.shape[3]
target_palettes = torch.ones(bz, 18, h, w).float().to(device)
target_palettes = target_palettes.reshape(h, w, bz * 18) * target_palettes_1d
target_palettes = target_palettes.permute(2, 0, 1).reshape(bz, 18, h, w)
x = torch.cat([target_palettes.float(), c3, x], dim=1)
x = self.dconv_up_2(x)
x = self.upsample(x)
# concatenate target_palettes and c4 with x
bz, h, w = x.shape[0], x.shape[2], x.shape[3]
target_palettes = torch.ones(bz, 18, h, w).float().to(device)
target_palettes = target_palettes.reshape(h, w, bz * 18) * target_palettes_1d
target_palettes = target_palettes.permute(2, 0, 1).reshape(bz, 18, h, w)
x = torch.cat([target_palettes.float(), c4, x], dim=1)
x = self.dconv_up_1(x)
x = self.upsample(x)
illu = illu.view(illu.size(0), 1, illu.size(1), illu.size(2))
x = torch.cat((x, illu), dim=1)
x = self.conv_last(x)
return x
transform = transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((432, 288)),
transforms.ToTensor(),
])
def load_image(img_path, transform=transform, show_image=False):
img = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
img = transform(img)
if show_image:
plt.imshow(np.transpose(img, (1, 2, 0)), interpolation='nearest')
plt.title("Original Image")
plt.show()
return img.unsqueeze(0)
# load the palette: given six hexcodes -> (18,)
def viz_color_palette(hexcodes=('#300268', '#4D87E7', '#5656C2', '#C166BC', '#E25173', '#F28BAE', '#FEFFFF'),
show_image=False):
"""
visualize color palette
"""
hexcodes = list(hexcodes)
while len(hexcodes) < 6:
# repeat hexcodes
hexcodes = hexcodes + hexcodes
hexcodes = hexcodes[:6]
palette = []
for hexcode in hexcodes:
rgb = np.array(list(int(hexcode.lstrip('#')[i:i + 2], 16) for i in (0, 2, 4)))
palette.append(rgb)
palette = np.array(palette)[np.newaxis, :, :] # (1, 6, 3)
palette = palette[:, :6, :].ravel() / 255.0
if show_image:
plt.imshow(palette.reshape((1, 6, 3)), interpolation='nearest')
plt.title("Palette")
plt.show()
return torch.from_numpy(palette).double()
def get_illuminance(img):
"""
Get the luminance of an image. Shape: (h, w)
"""
img = img.permute(1, 2, 0) # (h, w, channel)
img = img.numpy()
img = img.astype(np.float) / 255.0
img_LAB = rgb2lab(img)
img_L = img_LAB[:,:,0] # luminance # (h, w)
return torch.from_numpy(img_L).unsqueeze(0)
state = torch.load("saved_models/FE_RD.pth", map_location=torch.device('cpu'))
FE = FeatureEncoder().float().to(device)
RD = RecoloringDecoder().float().to(device)
FE.load_state_dict(state['FE'])
RD.load_state_dict(state['RD'])
# img = load_image("/Users/zhengxinyong/Desktop/vizly_color_transfer/static/uploads/logo.jpg")
# palette = viz_color_palette(['#300268','#4D87E7','#5656C2','#C166BC','#E25173','#F28BAE','#FEFFFF'])
# illu = get_illuminance(img[0])
#
# c1, c2, c3, c4 = FE.forward(img.float().to(device))
# out = RD.forward(c1, c2, c3, c4, palette.float().to(device), illu.float().to(device))
# out = out[0].detach().cpu().numpy()
# plt.imshow(np.transpose(out, (1,2,0)), interpolation='nearest')
# plt.title("Output Image")
# plt.show()