-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodel_hed.py
97 lines (80 loc) · 4.78 KB
/
model_hed.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
import torch.nn.functional as F
from torch import nn
import torch
class Network(torch.nn.Module):
def __init__(self):
super(Network, self).__init__()
self.moduleVggOne = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
self.moduleVggTwo = torch.nn.Sequential(
torch.nn.MaxPool2d(kernel_size=2, stride=2),
torch.nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
self.moduleVggThr = torch.nn.Sequential(
torch.nn.MaxPool2d(kernel_size=2, stride=2),
torch.nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
self.moduleVggFou = torch.nn.Sequential(
torch.nn.MaxPool2d(kernel_size=2, stride=2),
torch.nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
self.moduleVggFiv = torch.nn.Sequential(
torch.nn.MaxPool2d(kernel_size=2, stride=2),
torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
self.moduleScoreOne = torch.nn.Conv2d(in_channels=64, out_channels=1, kernel_size=1, stride=1, padding=0)
self.moduleScoreTwo = torch.nn.Conv2d(in_channels=128, out_channels=1, kernel_size=1, stride=1, padding=0)
self.moduleScoreThr = torch.nn.Conv2d(in_channels=256, out_channels=1, kernel_size=1, stride=1, padding=0)
self.moduleScoreFou = torch.nn.Conv2d(in_channels=512, out_channels=1, kernel_size=1, stride=1, padding=0)
self.moduleScoreFiv = torch.nn.Conv2d(in_channels=512, out_channels=1, kernel_size=1, stride=1, padding=0)
self.moduleCombine = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=5, out_channels=1, kernel_size=1, stride=1, padding=0),
torch.nn.Sigmoid()
)
# end
def forward(self, tensorInput):
tensorBlue = (tensorInput[:, 0:1, :, :] * 255.0) - 127.5
tensorGreen = (tensorInput[:, 1:2, :, :] * 255.0) - 127.5
tensorRed = (tensorInput[:, 2:3, :, :] * 255.0) - 127.5
tensorInput = torch.cat([ tensorBlue, tensorGreen, tensorRed ], 1)
vggOne = self.moduleVggOne(tensorInput)
vggTwo = self.moduleVggTwo(vggOne)
vggThr = self.moduleVggThr(vggTwo)
vggFou = self.moduleVggFou(vggThr)
vggFiv = self.moduleVggFiv(vggFou)
scoreOne = self.moduleScoreOne(vggOne)
scoreTwo = self.moduleScoreTwo(vggTwo)
scoreThr = self.moduleScoreThr(vggThr)
scoreFou = self.moduleScoreFou(vggFou)
scoreFiv = self.moduleScoreFiv(vggFiv)
H = tensorInput.size(2)
W = tensorInput.size(3)
scoreOne = torch.nn.functional.interpolate(input=scoreOne, size=(H, W), mode='bilinear', align_corners=False)
scoreTwo = torch.nn.functional.interpolate(input=scoreTwo, size=(H, W), mode='bilinear', align_corners=False)
scoreThr = torch.nn.functional.interpolate(input=scoreThr, size=(H, W), mode='bilinear', align_corners=False)
scoreFou = torch.nn.functional.interpolate(input=scoreFou, size=(H, W), mode='bilinear', align_corners=False)
scoreFiv = torch.nn.functional.interpolate(input=scoreFiv, size=(H, W), mode='bilinear', align_corners=False)
scoreFin = self.moduleCombine(torch.cat([ scoreOne, scoreTwo, scoreThr, scoreFou, scoreFiv ], 1))
return F.sigmoid(1 - scoreTwo)