-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy patherfnet_imagenet.py
123 lines (88 loc) · 3.69 KB
/
erfnet_imagenet.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
# ERFNet encoder model definition used for pretraining in ImageNet
# Sept 2017
# Eduardo Romera
#######################
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
class DownsamplerBlock (nn.Module):
def __init__(self, ninput, noutput):
super().__init__()
self.conv = nn.Conv2d(ninput, noutput-ninput, (3, 3), stride=2, padding=1, bias=True)
self.pool = nn.MaxPool2d(2, stride=2)
self.bn = nn.BatchNorm2d(noutput, eps=1e-3)
def forward(self, input):
output = torch.cat([self.conv(input), self.pool(input)], 1)
output = self.bn(output)
return F.relu(output)
class non_bottleneck_1d (nn.Module):
def __init__(self, chann, dropprob, dilated):
super().__init__()
self.conv3x1_1 = nn.Conv2d(chann, chann, (3, 1), stride=1, padding=(1,0), bias=True)
self.conv1x3_1 = nn.Conv2d(chann, chann, (1,3), stride=1, padding=(0,1), bias=True)
self.conv3x1_2 = nn.Conv2d(chann, chann, (3, 1), stride=1, padding=(1*dilated,0), bias=True, dilation = (dilated,1))
self.conv1x3_2 = nn.Conv2d(chann, chann, (1,3), stride=1, padding=(0,1*dilated), bias=True, dilation = (1, dilated))
self.bn1 = nn.BatchNorm2d(chann, eps=1e-03)
self.bn2 = nn.BatchNorm2d(chann, eps=1e-03)
self.dropout = nn.Dropout2d(dropprob)
def forward(self, input):
output = self.conv3x1_1(input)
output = F.relu(output)
output = self.conv1x3_1(output)
output = self.bn1(output)
output = F.relu(output)
output = self.conv3x1_2(output)
output = F.relu(output)
output = self.conv1x3_2(output)
output = self.bn2(output)
if (self.dropout.p != 0):
output = self.dropout(output)
return F.relu(output+input)
class Encoder(nn.Module):
def __init__(self):
super().__init__()
self.initial_block = DownsamplerBlock(3,16)
self.layers = nn.ModuleList()
self.layers.append(DownsamplerBlock(16,64))
for x in range(0, 5): #5 times
self.layers.append(non_bottleneck_1d(64, 0.1, 1))
self.layers.append(DownsamplerBlock(64,128))
for x in range(0, 2): #2 times
self.layers.append(non_bottleneck_1d(128, 0.1, 2))
self.layers.append(non_bottleneck_1d(128, 0.1, 4))
self.layers.append(non_bottleneck_1d(128, 0.1, 8))
self.layers.append(non_bottleneck_1d(128, 0.1, 16))
def forward(self, input):
output = self.initial_block(input)
for layer in self.layers:
output = layer(output)
return output
class Features(nn.Module):
def __init__(self):
super().__init__()
self.encoder = Encoder()
self.extralayer1 = nn.MaxPool2d(2, stride=2)
self.extralayer2 = nn.AvgPool2d(14,1,0)
def forward(self, input):
output = self.encoder(input)
output = self.extralayer1(output)
output = self.extralayer2(output)
return output
class Classifier(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.linear = nn.Linear(128, num_classes)
def forward(self, input):
output = input.view(input.size(0), 128) #first is batch_size
output = self.linear(output)
return output
class ERFNet(nn.Module):
def __init__(self, num_classes): #use encoder to pass pretrained encoder
super().__init__()
self.features = Features()
self.classifier = Classifier(num_classes)
def forward(self, input):
output = self.features(input)
output = self.classifier(output)
return output