-
Notifications
You must be signed in to change notification settings - Fork 126
/
models_x.py
243 lines (191 loc) · 8.28 KB
/
models_x.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
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
import torchvision.transforms as transforms
from torch.autograd import Variable
import torch
import numpy as np
import math
import trilinear
def weights_init_normal_classifier(m):
classname = m.__class__.__name__
if classname.find("Conv") != -1:
torch.nn.init.xavier_normal_(m.weight.data)
elif classname.find("BatchNorm2d") != -1 or classname.find("InstanceNorm2d") != -1:
torch.nn.init.normal_(m.weight.data, 1.0, 0.02)
torch.nn.init.constant_(m.bias.data, 0.0)
class resnet18_224(nn.Module):
def __init__(self, out_dim=5, aug_test=False):
super(resnet18_224, self).__init__()
self.aug_test = aug_test
net = models.resnet18(pretrained=True)
# self.mean = torch.Tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1).cuda()
# self.std = torch.Tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1).cuda()
self.upsample = nn.Upsample(size=(224,224),mode='bilinear')
net.fc = nn.Linear(512, out_dim)
self.model = net
def forward(self, x):
x = self.upsample(x)
if self.aug_test:
# x = torch.cat((x, torch.rot90(x, 1, [2, 3]), torch.rot90(x, 3, [2, 3])), 0)
x = torch.cat((x, torch.flip(x, [3])), 0)
f = self.model(x)
return f
##############################
# Discriminator
##############################
def discriminator_block(in_filters, out_filters, normalization=False):
"""Returns downsampling layers of each discriminator block"""
layers = [nn.Conv2d(in_filters, out_filters, 3, stride=2, padding=1)]
layers.append(nn.LeakyReLU(0.2))
if normalization:
layers.append(nn.InstanceNorm2d(out_filters, affine=True))
#layers.append(nn.BatchNorm2d(out_filters))
return layers
class Discriminator(nn.Module):
def __init__(self, in_channels=3):
super(Discriminator, self).__init__()
self.model = nn.Sequential(
nn.Upsample(size=(256,256),mode='bilinear'),
nn.Conv2d(3, 16, 3, stride=2, padding=1),
nn.LeakyReLU(0.2),
nn.InstanceNorm2d(16, affine=True),
*discriminator_block(16, 32),
*discriminator_block(32, 64),
*discriminator_block(64, 128),
*discriminator_block(128, 128),
#*discriminator_block(128, 128),
nn.Conv2d(128, 1, 8, padding=0)
)
def forward(self, img_input):
return self.model(img_input)
class Classifier(nn.Module):
def __init__(self, in_channels=3):
super(Classifier, self).__init__()
self.model = nn.Sequential(
nn.Upsample(size=(256,256),mode='bilinear'),
nn.Conv2d(3, 16, 3, stride=2, padding=1),
nn.LeakyReLU(0.2),
nn.InstanceNorm2d(16, affine=True),
*discriminator_block(16, 32, normalization=True),
*discriminator_block(32, 64, normalization=True),
*discriminator_block(64, 128, normalization=True),
*discriminator_block(128, 128),
#*discriminator_block(128, 128, normalization=True),
nn.Dropout(p=0.5),
nn.Conv2d(128, 3, 8, padding=0),
)
def forward(self, img_input):
return self.model(img_input)
class Classifier_unpaired(nn.Module):
def __init__(self, in_channels=3):
super(Classifier_unpaired, self).__init__()
self.model = nn.Sequential(
nn.Upsample(size=(256,256),mode='bilinear'),
nn.Conv2d(3, 16, 3, stride=2, padding=1),
nn.LeakyReLU(0.2),
nn.InstanceNorm2d(16, affine=True),
*discriminator_block(16, 32),
*discriminator_block(32, 64),
*discriminator_block(64, 128),
*discriminator_block(128, 128),
#*discriminator_block(128, 128),
nn.Conv2d(128, 3, 8, padding=0),
)
def forward(self, img_input):
return self.model(img_input)
class Generator3DLUT_identity(nn.Module):
def __init__(self, dim=33):
super(Generator3DLUT_identity, self).__init__()
if dim == 33:
file = open("IdentityLUT33.txt", 'r')
elif dim == 64:
file = open("IdentityLUT64.txt", 'r')
lines = file.readlines()
buffer = np.zeros((3,dim,dim,dim), dtype=np.float32)
for i in range(0,dim):
for j in range(0,dim):
for k in range(0,dim):
n = i * dim*dim + j * dim + k
x = lines[n].split()
buffer[0,i,j,k] = float(x[0])
buffer[1,i,j,k] = float(x[1])
buffer[2,i,j,k] = float(x[2])
self.LUT = nn.Parameter(torch.from_numpy(buffer).requires_grad_(True))
self.TrilinearInterpolation = TrilinearInterpolation()
def forward(self, x):
_, output = self.TrilinearInterpolation(self.LUT, x)
#self.LUT, output = self.TrilinearInterpolation(self.LUT, x)
return output
class Generator3DLUT_zero(nn.Module):
def __init__(self, dim=33):
super(Generator3DLUT_zero, self).__init__()
self.LUT = torch.zeros(3,dim,dim,dim, dtype=torch.float)
self.LUT = nn.Parameter(torch.tensor(self.LUT))
self.TrilinearInterpolation = TrilinearInterpolation()
def forward(self, x):
_, output = self.TrilinearInterpolation(self.LUT, x)
return output
class TrilinearInterpolationFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, lut, x):
x = x.contiguous()
output = x.new(x.size())
dim = lut.size()[-1]
shift = dim ** 3
binsize = 1.000001 / (dim-1)
W = x.size(2)
H = x.size(3)
batch = x.size(0)
assert 1 == trilinear.forward(lut,
x,
output,
dim,
shift,
binsize,
W,
H,
batch)
int_package = torch.IntTensor([dim, shift, W, H, batch])
float_package = torch.FloatTensor([binsize])
variables = [lut, x, int_package, float_package]
ctx.save_for_backward(*variables)
return lut, output
@staticmethod
def backward(ctx, lut_grad, x_grad):
lut, x, int_package, float_package = ctx.saved_variables
dim, shift, W, H, batch = int_package
dim, shift, W, H, batch = int(dim), int(shift), int(W), int(H), int(batch)
binsize = float(float_package[0])
assert 1 == trilinear.backward(x,
x_grad,
lut_grad,
dim,
shift,
binsize,
W,
H,
batch)
return lut_grad, x_grad
class TrilinearInterpolation(torch.nn.Module):
def __init__(self):
super(TrilinearInterpolation, self).__init__()
def forward(self, lut, x):
return TrilinearInterpolationFunction.apply(lut, x)
class TV_3D(nn.Module):
def __init__(self, dim=33):
super(TV_3D,self).__init__()
self.weight_r = torch.ones(3,dim,dim,dim-1, dtype=torch.float)
self.weight_r[:,:,:,(0,dim-2)] *= 2.0
self.weight_g = torch.ones(3,dim,dim-1,dim, dtype=torch.float)
self.weight_g[:,:,(0,dim-2),:] *= 2.0
self.weight_b = torch.ones(3,dim-1,dim,dim, dtype=torch.float)
self.weight_b[:,(0,dim-2),:,:] *= 2.0
self.relu = torch.nn.ReLU()
def forward(self, LUT):
dif_r = LUT.LUT[:,:,:,:-1] - LUT.LUT[:,:,:,1:]
dif_g = LUT.LUT[:,:,:-1,:] - LUT.LUT[:,:,1:,:]
dif_b = LUT.LUT[:,:-1,:,:] - LUT.LUT[:,1:,:,:]
tv = torch.mean(torch.mul((dif_r ** 2),self.weight_r)) + torch.mean(torch.mul((dif_g ** 2),self.weight_g)) + torch.mean(torch.mul((dif_b ** 2),self.weight_b))
mn = torch.mean(self.relu(dif_r)) + torch.mean(self.relu(dif_g)) + torch.mean(self.relu(dif_b))
return tv, mn