-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
157 lines (150 loc) · 8.24 KB
/
model.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
import torch
import torchvision
class WCT(torch.nn.Module):
def __init__(self):
super().__init__()
self.encoder = torch.nn.Sequential()
self.decoder = torch.nn.Sequential()
def forward(self, x):
out = self.encoder(x)
out = self.decoder(out)
return out
def encode(self,x):
return self.encoder(x)
def decode(self,x):
return self.decoder(x)
def whitening(self, content):
"""content: encoded image to whiten"""
fc = content.clone().detach()
dims = list(fc.shape)
N = fc.shape[-1] # not in the paper
fc -= fc.mean(dim=1)[:,None]
cov = torch.mm(fc,fc.T) /(N-1) # not in the paper, division by N
E, D, _ = torch.linalg.svd(cov)
D = torch.diag(D**-.5)
fhatc = torch.mm(E,D).mm(E.T).mm(fc)
return fhatc, dims
def coloring(self, style,fc):
""" style: encoded style image to feed the autoencoder
fc: whitened content image, output of the whitening method"""
fs = style.clone().detach()
N = fs.shape[-1]
mus = fs.mean(dim=1)[:,None]
fs -= mus
cov = torch.mm(fs,fs.T) / (N-1) # not in the paper
E, D, _ = torch.linalg.svd(cov)
D = torch.diag(D**.5)
fhatcs = torch.mm(E,D).mm(E.T).mm(fc)
fhatcs += mus
return fhatcs
def WCT(self,content,style,alpha=1):
""" Apply Whitening/Coloring transformation.
content: tensor of the image used as base
style: tensor of the image from which retrieve the style to transfer
alpha: [0,1] blend between original content and stylized-content"""
with torch.no_grad():
content = self.encode(content.reshape([1,3,224,224]))
style = self.encode(style.reshape([1,3,224,224]))
dim_original = content.shape[2]
fc = torch.flatten(content[0],1)
fhatc, dims = self.whitening(fc)
fs = torch.flatten(style[0],1)
transformed = self.coloring(fs,fhatc)
if(alpha != 1):
transformed = alpha*transformed + (1-alpha)*fc
transformed = transformed.unflatten(1,[dim_original,dim_original])
dim_original = list(transformed.shape)
transformed = self.decode(transformed.reshape([1]+ dim_original))
return transformed[0]
class UST_Net(WCT):
def __init__(self, level:int):
"""Initialize the model.
- level: initializes the relative autoencoder presented in the paper."""
super().__init__()
if level == 1:
self.encoder = torchvision.models.vgg19(weights = torchvision.models.VGG19_Weights.DEFAULT).features[:2]
self.decoder = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=64, out_channels=3, kernel_size=3, stride=1, padding=1),
)
if level == 2:
self.encoder = torchvision.models.vgg19(weights = torchvision.models.VGG19_Weights.DEFAULT).features[:7]
self.decoder = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=128, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.UpsamplingNearest2d(scale_factor=2),
torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=64, out_channels=3, kernel_size=3, stride=1, padding=1),
)
if level == 3:
self.encoder = torchvision.models.vgg19(weights = torchvision.models.VGG19_Weights.DEFAULT).features[:12]
self.decoder = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=256, out_channels= 128,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.UpsamplingNearest2d(scale_factor=2),
torch.nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=128, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.UpsamplingNearest2d(scale_factor=2),
torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=64, out_channels=3, kernel_size=3, stride=1, padding=1),
)
if level == 4:
self.encoder = torchvision.models.vgg19(weights = torchvision.models.VGG19_Weights.DEFAULT).features[:21]
self.decoder = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=512, out_channels= 256,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.UpsamplingNearest2d(scale_factor=2),
torch.nn.Conv2d(in_channels=256, out_channels= 256,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=256, out_channels= 256,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=256, out_channels= 256,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=256, out_channels= 128,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.UpsamplingNearest2d(scale_factor=2),
torch.nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=128, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.UpsamplingNearest2d(scale_factor=2),
torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=64, out_channels=3, kernel_size=3, stride=1, padding=1),
)
if level == 5:
self.encoder = torchvision.models.vgg19(weights = torchvision.models.VGG19_Weights.DEFAULT).features[:30]
self.decoder = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=512, out_channels= 512,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.UpsamplingNearest2d(scale_factor=2),
torch.nn.Conv2d(in_channels=512, out_channels= 512,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=512, out_channels= 512,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=512, out_channels= 512,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=512, out_channels= 256,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.UpsamplingNearest2d(scale_factor=2),
torch.nn.Conv2d(in_channels=256, out_channels= 256,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=256, out_channels= 256,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=256, out_channels= 256,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=256, out_channels= 128,kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.UpsamplingNearest2d(scale_factor=2),
torch.nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=128, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.UpsamplingNearest2d(scale_factor=2),
torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels=64, out_channels=3, kernel_size=3, stride=1, padding=1),
)