-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsseg_model.py
More file actions
159 lines (126 loc) · 4.93 KB
/
Copy pathsseg_model.py
File metadata and controls
159 lines (126 loc) · 4.93 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class SSegHead(nn.Module):
def __init__(self, num_classes=8, input_dim=512):
super(SSegHead, self).__init__()
self.conv1 = nn.Conv2d(input_dim, 256, 3, padding=1)
self.bn1 = nn.BatchNorm2d(256)
self.conv2 = nn.Conv2d(256, 256, 3, padding=1)
self.bn2 = nn.BatchNorm2d(256)
self.conv3 = nn.Conv2d(256, 256, 3, padding=1)
self.bn3 = nn.BatchNorm2d(256)
self.conv4 = nn.Conv2d(256, 256, 3, padding=1)
self.bn4 = nn.BatchNorm2d(256)
self.deconv = nn.ConvTranspose2d(256, 256, 2, stride=2, padding=0)
self.bn5 = nn.BatchNorm2d(256)
self.predictor = nn.Conv2d(256, num_classes, kernel_size=1, stride=1, padding=0)
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
x = F.relu(self.bn3(self.conv3(x)))
x = F.relu(self.bn4(self.conv4(x)))
x = F.relu(self.bn5(self.deconv(x)))
x = self.predictor(x)
return x
class DropoutHead(nn.Module):
def __init__(self, num_classes=8, input_dim=512):
super(DropoutHead, self).__init__()
self.conv1 = nn.Conv2d(input_dim, 256, 3, padding=1)
self.bn1 = nn.BatchNorm2d(256)
self.conv2 = nn.Conv2d(256, 256, 3, padding=1)
self.bn2 = nn.BatchNorm2d(256)
self.conv3 = nn.Conv2d(256, 256, 3, padding=1)
self.bn3 = nn.BatchNorm2d(256)
self.conv4 = nn.Conv2d(256, 256, 3, padding=1)
self.bn4 = nn.BatchNorm2d(256)
self.deconv = nn.ConvTranspose2d(256, 256, 2, stride=2, padding=0)
self.bn5 = nn.BatchNorm2d(256)
self.predictor = nn.Conv2d(256, num_classes, kernel_size=1, stride=1, padding=0)
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = F.dropout2d(x, p=0.2, training=True)
x = F.relu(self.bn2(self.conv2(x)))
x = F.dropout2d(x, p=0.2, training=True)
x = F.relu(self.bn3(self.conv3(x)))
x = F.dropout2d(x, p=0.2, training=True)
x = F.relu(self.bn4(self.conv4(x)))
x = F.dropout2d(x, p=0.2, training=True)
x = F.relu(self.bn5(self.deconv(x)))
x = F.dropout2d(x, p=0.2, training=True)
x = self.predictor(x)
return x
class DuqHead(nn.Module):
def __init__(self, num_classes=8, input_dim=512):
super(DuqHead, self).__init__()
self.num_classes = num_classes
self.conv1 = nn.Conv2d(input_dim, 256, 3, padding=1)
self.bn1 = nn.BatchNorm2d(256)
self.conv2 = nn.Conv2d(256, 256, 3, padding=1)
self.bn2 = nn.BatchNorm2d(256)
self.conv3 = nn.Conv2d(256, 256, 3, padding=1)
self.bn3 = nn.BatchNorm2d(256)
self.conv4 = nn.Conv2d(256, 256, 3, padding=1)
self.bn4 = nn.BatchNorm2d(256)
self.deconv = nn.ConvTranspose2d(256, 256, 2, stride=2, padding=0)
#==========================================================================================================
self.duq_centroid_size = 512
self.duq_model_output_size = 256
self.gamma = 0.999
self.duq_length_scale = 0.1
self.W = nn.Parameter(torch.zeros(self.duq_centroid_size, self.num_classes, self.duq_model_output_size))
nn.init.kaiming_normal_(self.W, nonlinearity='relu')
self.register_buffer('N', torch.ones(self.num_classes)*20)
self.register_buffer('m', torch.normal(torch.zeros(self.duq_centroid_size, self.num_classes), 0.05))
self.m = self.m *self.N
self.sigma = self.duq_length_scale
def rbf(self, z):
z = torch.einsum('ij,mnj->imn', z, self.W)
embeddings = self.m / self.N.unsqueeze(0)
diff = z - embeddings.unsqueeze(0)
diff = (diff ** 2).mean(1).div(2 * self.sigma **2).mul(-1).exp()
return diff
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
x = F.relu(self.bn3(self.conv3(x)))
x = F.relu(self.bn4(self.conv4(x)))
x = self.deconv(x) # B x 256 x 28 x 28
B, C, H, W = x.shape
z = x.permute(0, 2, 3, 1)
z = z.reshape(-1, C)
y_pred = self.rbf(z)
y_pred = y_pred.reshape(B, H, W, self.num_classes).permute(0, 3, 1, 2)
return y_pred
def update_embeddings(self, x, y_targets):
y_targets = y_targets.reshape(-1, 1).long().squeeze(1)
idx_unignored = (y_targets < 255)
y_targets = y_targets[idx_unignored]
y_targets = F.one_hot(y_targets, self.num_classes).float()
self.N = self.gamma * self.N + (1-self.gamma) * y_targets.sum(0)
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
x = F.relu(self.bn3(self.conv3(x)))
x = F.relu(self.bn4(self.conv4(x)))
x = self.deconv(x) # B x 256 x 28 x 28
B, C, _, _ = x.shape
z = x.permute(0, 2, 3, 1)
z = z.reshape(-1, C)
z = z[idx_unignored]
z = torch.einsum('ij,mnj->imn', z, self.W)
embedding_sum = torch.einsum('ijk,ik->jk', z, y_targets)
self.m = self.gamma * self.m + (1 - self.gamma) * embedding_sum
def calc_gradient_penalty(x, y_pred):
B, H, W, C = y_pred.shape
y_pred = y_pred.reshape(B, -1)
gradients = torch.autograd.grad(
outputs=y_pred,
inputs = x,
grad_outputs=torch.ones_like(y_pred)/(1.0*H*W),
create_graph=True,
)[0]
gradients = gradients.flatten(start_dim=1)
grad_norm = gradients.norm(2, dim=1)
gradient_penalty = ((grad_norm-1)**2).mean()
return gradient_penalty