-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
112 lines (93 loc) · 4.21 KB
/
Copy pathmodel.py
File metadata and controls
112 lines (93 loc) · 4.21 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from jlm import JLM_Block
class ConvLeakyRelu2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, padding=1, stride=1, dilation=1, groups=1):
super(ConvLeakyRelu2d, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, groups=groups)
def forward(self,x):
return F.leaky_relu(self.conv(x), negative_slope=0.2)
class ConvTanh2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, padding=1, stride=1, dilation=1, groups=1):
super(ConvTanh2d, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, groups=groups)
def forward(self,x):
return torch.tanh(self.conv(x))/2+0.5
class Sobelxy(nn.Module):
def __init__(self,channels, kernel_size=3, padding=1, stride=1, dilation=1, groups=1):
super(Sobelxy, self).__init__()
sobel_filter = np.array([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
self.convx=nn.Conv2d(channels, channels, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, groups=channels,bias=False)
self.convx.weight.data.copy_(torch.from_numpy(sobel_filter))
self.convy=nn.Conv2d(channels, channels, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, groups=channels,bias=False)
self.convy.weight.data.copy_(torch.from_numpy(sobel_filter.T))
def forward(self, x):
sobelx = self.convx(x)
sobely = self.convy(x)
x=torch.abs(sobelx) + torch.abs(sobely)
return x
class Conv1(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=1, padding=0, stride=1, dilation=1, groups=1):
super(Conv1, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, padding=padding, stride=stride, dilation=dilation, groups=groups)
def forward(self,x):
return self.conv(x)
class DenseBlock(nn.Module):
def __init__(self,channels):
super(DenseBlock, self).__init__()
self.conv1 = ConvLeakyRelu2d(channels, channels)
self.conv2 = ConvLeakyRelu2d(2*channels, channels)
# self.conv3 = ConvLeakyRelu2d(3*channels, channels)
def forward(self,x):
x=torch.cat((x,self.conv1(x)),dim=1)
x = torch.cat((x, self.conv2(x)), dim=1)
# x = torch.cat((x, self.conv3(x)), dim=1)
return x
class RGBD(nn.Module):
def __init__(self,in_channels,out_channels):
super(RGBD, self).__init__()
self.dense =DenseBlock(in_channels)
self.convdown=Conv1(3*in_channels,out_channels)
self.sobelconv=Sobelxy(in_channels)
self.convup =Conv1(in_channels,out_channels)
def forward(self,x):
x1=self.dense(x)
x1=self.convdown(x1)
x2=self.sobelconv(x)
x2=self.convup(x2)
return F.leaky_relu(x1 + x2, negative_slope=0.1)
class backbone(nn.Module):
def __init__(self, output):
super(backbone, self).__init__()
output=1
self.vis_conv = ConvLeakyRelu2d(1, 16)
self.vis_rgbd = RGBD(16, 32)
self.inf_conv = ConvLeakyRelu2d(1, 16)
self.inf_rgbd = RGBD(16, 32)
self.jlm = JLM_Block()
self.decoder2 = ConvLeakyRelu2d(64, 32)
self.decoder1 = ConvTanh2d(32, 1)
def forward(self, image_vis, image_ir):
x_vis_origin = image_vis[:, : 1]
x_inf_origin = image_ir
x_vis_p = self.vis_conv(x_vis_origin)
x_vis_p1 = self.vis_rgbd(x_vis_p)
# x_vis_p2 = x_vis_p1[0]
x_inf_p = self.inf_conv(x_inf_origin)
x_inf_p1 = self.inf_rgbd(x_inf_p)
# x_inf_p2 = x_inf_p1[0]
x = torch.cat((x_vis_p1, x_inf_p1), dim=1)
x = self.jlm(x)
x = x[0]
x = self.decoder2(x)
x = self.decoder1(x)
return x
if __name__ == '__main__':
net = backbone(1)
total = sum([param.nelement() for param in net.parameters()])
print("Number of parameters: %.2fM" % (total / 1e6))
print(net)