-
Notifications
You must be signed in to change notification settings - Fork 1
/
squeezesegV2.py
executable file
·161 lines (138 loc) · 6.21 KB
/
squeezesegV2.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
#!/usr/bin/env python3
# This file is covered by the LICENSE file in the root of this project.
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
class Fire(nn.Module):
def __init__(self, inplanes, squeeze_planes,
expand1x1_planes, expand3x3_planes, bn_d=0.1):
super(Fire, self).__init__()
self.inplanes = inplanes
self.bn_d = bn_d
self.activation = nn.ReLU(inplace=True)
self.squeeze = nn.Conv2d(inplanes, squeeze_planes, kernel_size=1)
self.squeeze_bn = nn.BatchNorm2d(squeeze_planes, momentum=self.bn_d)
self.expand1x1 = nn.Conv2d(squeeze_planes, expand1x1_planes,
kernel_size=1)
self.expand1x1_bn = nn.BatchNorm2d(
expand1x1_planes, momentum=self.bn_d)
self.expand3x3 = nn.Conv2d(squeeze_planes, expand3x3_planes,
kernel_size=3, padding=1)
self.expand3x3_bn = nn.BatchNorm2d(
expand3x3_planes, momentum=self.bn_d)
def forward(self, x):
x = self.activation(self.squeeze_bn(self.squeeze(x)))
return torch.cat([
self.activation(self.expand1x1_bn(self.expand1x1(x))),
self.activation(self.expand3x3_bn(self.expand3x3(x)))
], 1)
class CAM(nn.Module):
def __init__(self, inplanes, bn_d=0.1):
super(CAM, self).__init__()
self.inplanes = inplanes
self.bn_d = bn_d
self.pool = nn.MaxPool2d(7, 1, 3)
self.squeeze = nn.Conv2d(inplanes, inplanes // 16,
kernel_size=1, stride=1)
self.squeeze_bn = nn.BatchNorm2d(inplanes // 16, momentum=self.bn_d)
self.relu = nn.ReLU(inplace=True)
self.unsqueeze = nn.Conv2d(inplanes // 16, inplanes,
kernel_size=1, stride=1)
self.unsqueeze_bn = nn.BatchNorm2d(inplanes, momentum=self.bn_d)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
# 7x7 pooling
y = self.pool(x)
# squeezing and relu
y = self.relu(self.squeeze_bn(self.squeeze(y)))
# unsqueezing
y = self.sigmoid(self.unsqueeze_bn(self.unsqueeze(y)))
# attention
return y * x
# ******************************************************************************
class Backbone(nn.Module):
"""
Class for Squeezeseg. Subclasses PyTorch's own "nn" module
"""
def __init__(self, params):
# Call the super constructor
super(Backbone, self).__init__()
print("Using SqueezeNet Backbone")
self.use_range = params["input_depth"]["range"]
self.use_xyz = params["input_depth"]["xyz"]
self.use_remission = params["input_depth"]["remission"]
self.bn_d = params["bn_d"]
self.drop_prob = params["dropout"]
# input depth calc
self.input_depth = 0
self.input_idxs = []
if self.use_range:
self.input_depth += 1
self.input_idxs.append(0)
if self.use_xyz:
self.input_depth += 3
self.input_idxs.extend([1, 2, 3])
if self.use_remission:
self.input_depth += 1
self.input_idxs.append(4)
print("Depth of backbone input = ", self.input_depth)
# stride play
self.strides = [2, 2, 2, 2]
# encoder
self.conv1a = nn.Sequential(nn.Conv2d(self.input_depth, 32, kernel_size=3,
stride=[1, self.strides[0]],
padding=1),
nn.BatchNorm2d(32, momentum=self.bn_d),
nn.ReLU(inplace=True),
CAM(32, bn_d=self.bn_d))
self.conv1b = nn.Sequential(nn.Conv2d(32, 64, kernel_size=3,
stride=[1, 2],
padding=1),
nn.BatchNorm2d(64, momentum=self.bn_d),
nn.ReLU(inplace=True),
CAM(64, bn_d=self.bn_d))
self.fire23 = nn.Sequential(nn.MaxPool2d(kernel_size=3,
stride=[1, self.strides[1]],
padding=1),
Fire(64, 16, 64, 64, bn_d=self.bn_d),
CAM(128, bn_d=self.bn_d),
Fire(128, 16, 64, 64, bn_d=self.bn_d),
CAM(128, bn_d=self.bn_d))
self.fire45 = nn.Sequential(nn.MaxPool2d(kernel_size=3,
stride=[1, self.strides[2]],
padding=1),
Fire(128, 32, 128, 128, bn_d=self.bn_d),
Fire(256, 32, 128, 128, bn_d=self.bn_d))
self.fire6789 = nn.Sequential(nn.MaxPool2d(kernel_size=3,
stride=[1, self.strides[3]],
padding=1),
Fire(256, 48, 192, 192, bn_d=self.bn_d),
Fire(384, 48, 192, 192, bn_d=self.bn_d),
Fire(384, 64, 256, 256, bn_d=self.bn_d),
Fire(512, 64, 256, 256, bn_d=self.bn_d))
# output
self.dropout = nn.Dropout2d(self.drop_prob)
# last channels
self.last_channels = 512
def run_layer(self, x, layer):
y = layer(x)
x = y
return x
def forward(self, x):
# filter input
x = x[:, self.input_idxs]
# run cnn
x = self.conv1a(x)
x = self.conv1b(x)
x = self.run_layer(x, self.fire23)
x = self.run_layer(x, self.dropout)
x = self.run_layer(x, self.fire45)
x = self.run_layer(x, self.dropout)
x = self.run_layer(x, self.fire6789)
x = self.run_layer(x, self.dropout)
return x
def get_last_depth(self):
return self.last_channels
def get_input_depth(self):
return self.input_depth