-
Notifications
You must be signed in to change notification settings - Fork 44
/
model.py
242 lines (195 loc) · 8.88 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
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
from turtle import forward
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from collections import OrderedDict
from slice import bilateral_slice
class L2LOSS(nn.Module):
def forward(self, x,y):
return torch.mean((x-y)**2)
class ConvBlock(nn.Module):
def __init__(self, inc , outc, kernel_size=3, padding=1, stride=1, use_bias=True, activation=nn.ReLU, batch_norm=False):
super(ConvBlock, self).__init__()
self.conv = nn.Conv2d(int(inc), int(outc), kernel_size, padding=padding, stride=stride, bias=use_bias)
self.activation = activation() if activation else None
self.bn = nn.BatchNorm2d(outc) if batch_norm else None
if use_bias and not batch_norm:
self.conv.bias.data.fill_(0.00)
# aka TF variance_scaling_initializer
torch.nn.init.kaiming_uniform_(self.conv.weight)#, mode='fan_out',nonlinearity='relu')
def forward(self, x):
x = self.conv(x)
if self.bn:
x = self.bn(x)
if self.activation:
x = self.activation(x)
return x
class FC(nn.Module):
def __init__(self, inc , outc, activation=nn.ReLU, batch_norm=False):
super(FC, self).__init__()
self.fc = nn.Linear(int(inc), int(outc), bias=(not batch_norm))
self.activation = activation() if activation else None
self.bn = nn.BatchNorm1d(outc) if batch_norm else None
if not batch_norm:
self.fc.bias.data.fill_(0.00)
# aka TF variance_scaling_initializer
torch.nn.init.kaiming_uniform_(self.fc.weight)#, mode='fan_out',nonlinearity='relu')
def forward(self, x):
x = self.fc(x)
if self.bn:
x = self.bn(x)
if self.activation:
x = self.activation(x)
return x
class Slice(nn.Module):
def __init__(self):
super(Slice, self).__init__()
def forward(self, bilateral_grid, guidemap):
bilateral_grid = bilateral_grid.permute(0,3,4,2,1)
guidemap = guidemap.squeeze(1)
# grid: The bilateral grid with shape (gh, gw, gd, gc).
# guide: A guide image with shape (h, w). Values must be in the range [0, 1].
coeefs = bilateral_slice(bilateral_grid, guidemap).permute(0,3,1,2)
return coeefs
# Nx12x8x16x16
# print(guidemap.shape)
# print(bilateral_grid.shape)
# device = bilateral_grid.get_device()
# N, _, H, W = guidemap.shape
# hg, wg = torch.meshgrid([torch.arange(0, H), torch.arange(0, W)]) # [0,511] HxW
# if device >= 0:
# hg = hg.to(device)
# wg = wg.to(device)
# hg = hg.float().repeat(N, 1, 1).unsqueeze(3) / (H-1)# * 2 - 1 # norm to [-1,1] NxHxWx1
# wg = wg.float().repeat(N, 1, 1).unsqueeze(3) / (W-1)# * 2 - 1 # norm to [-1,1] NxHxWx1
# guidemap = guidemap.permute(0,2,3,1).contiguous()
# guidemap_guide = torch.cat([hg, wg, guidemap], dim=3).unsqueeze(1) # Nx1xHxWx3
# # When mode='bilinear' and the input is 5-D, the interpolation mode used internally will actually be trilinear.
# coeff = F.grid_sample(bilateral_grid, guidemap_guide, 'bilinear')#, align_corners=True)
# print(coeff.shape)
# return coeff.squeeze(2)
class ApplyCoeffs(nn.Module):
def __init__(self):
super(ApplyCoeffs, self).__init__()
def forward(self, coeff, full_res_input):
'''
Affine:
r = a11*r + a12*g + a13*b + a14
g = a21*r + a22*g + a23*b + a24
...
'''
# out_channels = []
# for chan in range(n_out):
# ret = scale[:, :, :, chan, 0]*input_image[:, :, :, 0]
# for chan_i in range(1, n_in):
# ret += scale[:, :, :, chan, chan_i]*input_image[:, :, :, chan_i]
# if has_affine_term:
# ret += offset[:, :, :, chan]
# ret = tf.expand_dims(ret, 3)
# out_channels.append(ret)
# ret = tf.concat(out_channels, 3)
"""
R = r1[0]*r2 + r1[1]*g2 + r1[2]*b3 +r1[3]
"""
# print(coeff.shape)
# R = torch.sum(full_res_input * coeff[:, 0:3, :, :], dim=1, keepdim=True) + coeff[:, 3:4, :, :]
# G = torch.sum(full_res_input * coeff[:, 4:7, :, :], dim=1, keepdim=True) + coeff[:, 7:8, :, :]
# B = torch.sum(full_res_input * coeff[:, 8:11, :, :], dim=1, keepdim=True) + coeff[:, 11:12, :, :]
R = torch.sum(full_res_input * coeff[:, 0:3, :, :], dim=1, keepdim=True) + coeff[:, 9:10, :, :]
G = torch.sum(full_res_input * coeff[:, 3:6, :, :], dim=1, keepdim=True) + coeff[:, 10:11, :, :]
B = torch.sum(full_res_input * coeff[:, 6:9, :, :], dim=1, keepdim=True) + coeff[:, 11:12, :, :]
return torch.cat([R, G, B], dim=1)
class GuideNN(nn.Module):
def __init__(self, params=None):
super(GuideNN, self).__init__()
self.params = params
self.conv1 = ConvBlock(3, params['guide_complexity'], kernel_size=1, padding=0, batch_norm=True)
self.conv2 = ConvBlock(params['guide_complexity'], 1, kernel_size=1, padding=0, activation= nn.Sigmoid) #nn.Tanh nn.Sigmoid
def forward(self, x):
return self.conv2(self.conv1(x))#.squeeze(1)
class Coeffs(nn.Module):
def __init__(self, nin=4, nout=3, params=None):
super(Coeffs, self).__init__()
self.params = params
self.nin = nin
self.nout = nout
lb = params['luma_bins']
cm = params['channel_multiplier']
sb = params['spatial_bin']
bn = params['batch_norm']
nsize = params['net_input_size']
self.relu = nn.ReLU()
# splat features
n_layers_splat = int(np.log2(nsize/sb))
self.splat_features = nn.ModuleList()
prev_ch = 3
for i in range(n_layers_splat):
use_bn = bn if i > 0 else False
self.splat_features.append(ConvBlock(prev_ch, cm*(2**i)*lb, 3, stride=2, batch_norm=use_bn))
prev_ch = splat_ch = cm*(2**i)*lb
# global features
n_layers_global = int(np.log2(sb/4))
self.global_features_conv = nn.ModuleList()
self.global_features_fc = nn.ModuleList()
for i in range(n_layers_global):
self.global_features_conv.append(ConvBlock(prev_ch, cm*8*lb, 3, stride=2, batch_norm=bn))
prev_ch = cm*8*lb
n_total = n_layers_splat + n_layers_global
prev_ch = prev_ch * (nsize/2**n_total)**2
self.global_features_fc.append(FC(prev_ch, 32*cm*lb, batch_norm=bn))
self.global_features_fc.append(FC(32*cm*lb, 16*cm*lb, batch_norm=bn))
self.global_features_fc.append(FC(16*cm*lb, 8*cm*lb, activation=None, batch_norm=bn))
# local features
self.local_features = nn.ModuleList()
self.local_features.append(ConvBlock(splat_ch, 8*cm*lb, 3, batch_norm=bn))
self.local_features.append(ConvBlock(8*cm*lb, 8*cm*lb, 3, activation=None, use_bias=False))
# predicton
self.conv_out = ConvBlock(8*cm*lb, lb*nout*nin, 1, padding=0, activation=None)#,batch_norm=True)
def forward(self, lowres_input):
params = self.params
bs = lowres_input.shape[0]
lb = params['luma_bins']
cm = params['channel_multiplier']
sb = params['spatial_bin']
x = lowres_input
for layer in self.splat_features:
x = layer(x)
splat_features = x
for layer in self.global_features_conv:
x = layer(x)
x = x.view(bs, -1)
for layer in self.global_features_fc:
x = layer(x)
global_features = x
x = splat_features
for layer in self.local_features:
x = layer(x)
local_features = x
fusion_grid = local_features
fusion_global = global_features.view(bs,8*cm*lb,1,1)
fusion = self.relu( fusion_grid + fusion_global )
x = self.conv_out(fusion)
s = x.shape
y = torch.stack(torch.split(x, self.nin*self.nout, 1),2)
# y = torch.stack(torch.split(y, self.nin, 1),3)
# print(y.shape)
# x = x.view(bs,self.nin*self.nout,lb,sb,sb) # B x Coefs x Luma x Spatial x Spatial
# print(x.shape)
return y
class HDRPointwiseNN(nn.Module):
def __init__(self, params):
super(HDRPointwiseNN, self).__init__()
self.coeffs = Coeffs(params=params)
self.guide = GuideNN(params=params)
self.slice = Slice()
self.apply_coeffs = ApplyCoeffs()
# self.bsa = bsa.BilateralSliceApply()
def forward(self, lowres, fullres):
coeffs = self.coeffs(lowres)
guide = self.guide(fullres)
slice_coeffs = self.slice(coeffs, guide)
out = self.apply_coeffs(slice_coeffs, fullres)
# out = bsa.bsa(coeffs,guide,fullres)
return out
#########################################################################################################