forked from Chenyang-Lu/mono-semantic-occupancy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vae_nets.py
151 lines (112 loc) · 4.27 KB
/
vae_nets.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
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
from util import get_upsampling_weight
class upsample(nn.Module):
def __init__(self, if_deconv, channels=None):
super(upsample, self).__init__()
if if_deconv:
self.upsample = nn.ConvTranspose2d(channels, channels, 4, stride=2, padding=1, bias=False)
else:
self.upsample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
def forward(self, x):
x = self.upsample(x)
return x
class double_conv(nn.Module):
def __init__(self, in_ch, out_ch):
super(double_conv, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(),
nn.Conv2d(out_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU()
)
def forward(self, x):
x = self.conv(x)
return x
class encoder_after_vgg(nn.Module):
def __init__(self):
super(encoder_after_vgg, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(512, 256, 3, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(256, 128, 3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.mu_dec = nn.Linear(4096, 512)
self.logvar_dec = nn.Linear(4096, 512)
def forward(self, x):
x = self.conv(x)
x = x.view(-1, 4096)
mu = self.mu_dec(x)
logvar = self.logvar_dec(x)
return mu, logvar
class decoder_conv(nn.Module):
def __init__(self, if_deconv):
super(decoder_conv, self).__init__()
self.up1 = upsample(if_deconv=if_deconv, channels=128)
self.conv1 = double_conv(128, 256)
self.up2 = upsample(if_deconv=if_deconv, channels=256)
self.conv2 = double_conv(256, 256)
self.up3 = upsample(if_deconv=if_deconv, channels=256)
self.conv3 = double_conv(256, 256)
self.up4 = upsample(if_deconv=if_deconv, channels=256)
self.conv4 = double_conv(256, 256)
self.up5 = upsample(if_deconv=if_deconv, channels=256)
self.conv5 = double_conv(256, 256)
self.conv_out = nn.Conv2d(256, 4, 3, padding=1)
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.ConvTranspose2d):
assert m.kernel_size[0] == m.kernel_size[1]
initial_weight = get_upsampling_weight(
m.in_channels, m.out_channels, m.kernel_size[0])
m.weight.data.copy_(initial_weight)
def forward(self, x):
x = x.view(-1, 128, 2, 2)
x = self.up1(x)
x = self.conv1(x)
x = self.up2(x)
x = self.conv2(x)
x = self.up3(x)
x = self.conv3(x)
x = self.up4(x)
x = self.conv4(x)
x = self.up5(x)
x = self.conv5(x)
x = self.conv_out(x)
return x
class vae_mapping(nn.Module):
def __init__(self):
super(vae_mapping, self).__init__()
self.vgg16 = models.vgg16_bn(pretrained=True)
self.vgg16_feature = nn.Sequential(*list(self.vgg16.features.children())[:])
self.encoder_afterv_vgg = encoder_after_vgg()
self.decoder = decoder_conv(if_deconv=True)
def reparameterize(self, is_training, mu, logvar):
if is_training:
std = torch.exp(0.5*logvar)
eps = torch.randn_like(std)
return eps.mul(std).add_(mu)
else:
return mu
def forward(self, x, is_training, defined_mu=None):
x = self.vgg16_feature(x)
mu, logvar = self.encoder_afterv_vgg(x)
z = self.reparameterize(is_training, mu, logvar)
if defined_mu is not None:
z = defined_mu
pred_map = self.decoder(z)
return pred_map, mu, logvar
def loss_function_map(pred_map, map, mu, logvar):
CE = F.cross_entropy(pred_map, map.view(-1, 64, 64), weight=
torch.Tensor([0.6225708, 2.53963754, 15.46416047, 0.52885405]).to('cuda:0'), ignore_index=4)
KLD = -0.5 * torch.mean(1 + logvar - mu.pow(2) - logvar.exp())
return 0.9*CE + 0.1*KLD, CE, KLD