-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.py
More file actions
72 lines (66 loc) · 2.7 KB
/
decoder.py
File metadata and controls
72 lines (66 loc) · 2.7 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
from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from gumbel_softmax import gumbel_softmax_sample
from tqdm import tqdm
class Decoder(nn.Module):
def __init__(self, channels=[3, 36, 36, 3], obj_classes=2, n_reps=[1, 1, 1]):
assert channels[0] == obj_classes + 1, 'channels[0] = background(1) + obj_classes'
super(Decoder, self).__init__()
self.channels = channels
self.obj_classes = obj_classes
self.n_reps = n_reps
self.point_ops = []
self.conv_ops = []
for i in range(len(self.channels)-1):
scaling = 3**(len(channels) - 2 - i)
self.conv_ops.append([])
self.point_ops.append([])
for r in range(self.n_reps[i]):
self.conv_ops[i].append(
nn.ConvTranspose2d(
in_channels=self.channels[i+1], out_channels=self.channels[i+1],
kernel_size=3, stride=1, padding=scaling, dilation=scaling,
groups=self.channels[i+1], bias=True,
)
)
self.add_module('conv_{}_{}'.format(i, r), self.conv_ops[i][r])
if r == 0:
self.point_ops[i].append(
nn.Conv2d(
in_channels=self.channels[i], out_channels=self.channels[i+1],
kernel_size=1, stride=1, padding=0, dilation=1,
groups=1, bias=True,
)
)
else:
self.point_ops[i].append(
nn.Conv2d(
in_channels=self.channels[i+1], out_channels=self.channels[i+1],
kernel_size=1, stride=1, padding=0, dilation=1,
groups=1, bias=True,
)
)
self.add_module('point_{}_{}'.format(i, r), self.point_ops[i][r])
def forward(self, x):
x = torch.cat((torch.zeros_like(x[:, :1, :, :], dtype=torch.float32), x), dim=1)
self.layers = [x]
for i in range(len(self.channels)-1):
for r in range(self.n_reps[i]):
self.layers.append(
#F.leaky_relu(
identity(
self.conv_ops[i][r](
self.point_ops[i][r](
self.layers[-1]
)
)
)
)
self.objects = self.layers[-1]
return self.objects
def identity(x):
return x