-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmlp.py
151 lines (126 loc) · 4.91 KB
/
mlp.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
import torch.nn as nn
import torch
import numpy as np
from torch.nn import functional as F
hidden_list = [256, 256, 256]
L = 4
def make_coord(shape, ranges=None, flatten=True):
coord_seqs = []
for i, n in enumerate(shape):
if ranges is None:
v0, v1 = -1, 1
else:
v0, v1 = ranges[i]
r = (v1 - v0) / (2 * n)
seq = v0 + r + (2 * r) * torch.arange(n).float()
coord_seqs.append(seq)
ret = torch.stack(torch.meshgrid(*coord_seqs), dim=-1)
if flatten:
ret = ret.view(-1, ret.shape[-1])
return ret
class MLP(nn.Module):
def __init__(self, in_dim, out_dim, hidden_list):
super().__init__()
layers = []
lastv = in_dim
for hidden in hidden_list:
layers.append(nn.Linear(lastv, hidden))
layers.append(nn.ReLU())
lastv = hidden
layers.append(nn.Linear(lastv, out_dim))
self.layers = nn.Sequential(*layers)
def forward(self, x):
shape = x.shape[:-1]
x = self.layers(x.view(-1, x.shape[-1]))
return x.view(*shape, -1)
class INR(nn.Module):
def __init__(self, dim, local_ensemble=True, feat_unfold=True, cell_decode=True):
super().__init__()
self.local_ensemble = local_ensemble
self.feat_unfold = feat_unfold
self.cell_decode = cell_decode
imnet_in_dim = dim
if self.feat_unfold:
imnet_in_dim *= 9
imnet_in_dim += 2 + 4 * L
if self.cell_decode:
imnet_in_dim += 2
self.imnet = MLP(imnet_in_dim, 3, hidden_list)
def query_rgb(self, inp, coord, cell=None):
feat = inp
if self.feat_unfold:
feat = F.unfold(feat, 3, padding=1).view(
feat.shape[0], feat.shape[1] * 9, feat.shape[2], feat.shape[3])
if self.local_ensemble:
vx_lst = [-1, 1]
vy_lst = [-1, 1]
eps_shift = 1e-6
else:
vx_lst, vy_lst, eps_shift = [0], [0], 0
rx = 2 / feat.shape[-2] / 2
ry = 2 / feat.shape[-1] / 2
feat_coord = make_coord(feat.shape[-2:], flatten=False).cuda() \
.permute(2, 0, 1) \
.unsqueeze(0).expand(feat.shape[0], 2, *feat.shape[-2:])
preds = []
areas = []
for vx in vx_lst:
for vy in vy_lst:
coord_ = coord.clone()
coord_[:, :, 0] += vx * rx + eps_shift
coord_[:, :, 1] += vy * ry + eps_shift
coord_.clamp_(-1 + 1e-6, 1 - 1e-6)
bs, q, h, w = feat.shape
q_feat = feat.view(bs, q, -1).permute(0, 2, 1)
bs, q, h, w = feat_coord.shape
q_coord = feat_coord.view(bs, q, -1).permute(0, 2, 1)
points_enc = self.positional_encoding(q_coord, L=L)
q_coord = torch.cat([q_coord, points_enc], dim=-1)
rel_coord = coord - q_coord
rel_coord[:, :, 0] *= feat.shape[-2]
rel_coord[:, :, 1] *= feat.shape[-1]
inp = torch.cat([q_feat, rel_coord], dim=-1)
if self.cell_decode:
rel_cell = cell.clone()
rel_cell[:, :, 0] *= feat.shape[-2]
rel_cell[:, :, 1] *= feat.shape[-1]
inp = torch.cat([inp, rel_cell], dim=-1)
bs, q = coord.shape[:2]
pred = self.imnet(inp.view(bs * q, -1)).view(bs, q, -1)
preds.append(pred)
area = torch.abs(rel_coord[:, :, 0] * rel_coord[:, :, 1])
areas.append(area + 1e-9)
tot_area = torch.stack(areas).sum(dim=0)
if self.local_ensemble:
t = areas[0];
areas[0] = areas[3];
areas[3] = t
t = areas[1];
areas[1] = areas[2];
areas[2] = t
ret = 0
for pred, area in zip(preds, areas):
ret = ret + pred * (area / tot_area).unsqueeze(-1)
bs, q, h, w = feat.shape
ret = ret.view(bs, h, w, -1).permute(0, 3, 1, 2)
return ret
def forward(self, inp):
h, w = inp.shape[2], inp.shape[3]
B = inp.shape[0]
coord = make_coord((h, w)).cuda()
cell = torch.ones_like(coord)
cell[:, 0] *= 2 / h
cell[:, 1] *= 2 / w
cell = cell.unsqueeze(0).repeat(B, 1, 1)
coord = coord.unsqueeze(0).repeat(B, 1, 1)
points_enc = self.positional_encoding(coord, L=L)
coord = torch.cat([coord, points_enc], dim=-1)
return self.query_rgb(inp, coord, cell)
def positional_encoding(self, input, L):
shape = input.shape
freq = 2 ** torch.arange(L, dtype=torch.float32).cuda() * np.pi
spectrum = input[..., None] * freq
sin, cos = spectrum.sin(), spectrum.cos()
input_enc = torch.stack([sin, cos], dim=-2)
input_enc = input_enc.view(*shape[:-1], -1)
return input_enc