-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
182 lines (155 loc) · 6.12 KB
/
model.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 18 09:28:51 2021
@author: Administrator
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch.utils.model_zoo as model_zoo
import numpy as np
import math
#基础conv
class BasicConv(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, dilation=1, bias=False):
super(BasicConv, self).__init__()
if dilation != 1:
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, dilation, dilation=dilation, bias=bias)
else:
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, kernel_size//2, dilation=dilation, bias=bias)
self.activation = nn.LeakyReLU()
def forward(self, x):
x = self.conv(x)
x = self.activation(x)
return x
class Resblock(nn.Module):
def __init__(self, channel):
super(Resblock, self).__init__()
self.conv = nn.Sequential(
BasicConv(channel, channel,3),
BasicConv(channel, channel,3)
)
def forward(self, x):
return self.conv(x)+x
#特征金字塔模块
class SpatialPyramidPooling(nn.Module):
def __init__(self,channel, kernel_sizes,stride=1):
super(SpatialPyramidPooling, self).__init__()
self.conv = nn.ModuleList([nn.Sequential(nn.Conv2d(channel, channel, kernel_size, stride, kernel_size//2,bias=True))\
for kernel_size in kernel_sizes])
def forward(self, x):
features = [convi(x) for convi in self.conv[::-1]]
features = torch.cat(features + [x], dim=1)
return features
class GNet(nn.Module):
def __init__(self):
super(GNet,self).__init__()
self.a = T_Attention()
self.t = TIDFE()
self.f = FDFE()
self.r = FI()
def forward(self,x):
A_map = self.a(x)
x = x+A_map
out1 = self.t(x)
out2 = self.f(x)
out = self.r(x,out1,out2)
return A_map, out1, out2, out
class T_Attention(nn.Module):
def __init__(self,channel_in=3,channel=32):
super(T_Attention,self).__init__()
self.conv_in = nn.Sequential(
nn.MaxPool2d(kernel_size=3,stride=2,padding=1),
BasicConv(channel_in, channel),
SpatialPyramidPooling(channel,[5,7,9]),
BasicConv(channel*4, channel,1),
BasicConv(channel, channel),
nn.Conv2d(channel,channel_in,kernel_size=3,stride=1,padding=1),nn.Sigmoid()
)
def _upsample(self,x,y):
_,_,H,W = y.size()
return F.upsample(x,size=(H,W),mode='bilinear')
def forward(self,x_in):
A_map = self._upsample(self.conv_in(x_in),x_in)*x_in
return A_map
class TIDFE(nn.Module):
def __init__(self, channel_in = 3, channel_out = 3, channel_1 = 32, channel_2 = 64, channel_3 = 128):
super(TIDFE, self).__init__()
self.conv1_1 = nn.Sequential(
BasicConv(channel_in, channel_1),
BasicConv(channel_1, channel_1))
self.conv2_1 = nn.Sequential(
nn.MaxPool2d(kernel_size=3,stride=2,padding=1),
BasicConv(channel_1, channel_2),
BasicConv(channel_2, channel_2),
BasicConv(channel_2, channel_2),
BasicConv(channel_2, channel_2))
self.conv3_1 = nn.Sequential(
nn.MaxPool2d(kernel_size=3,stride=2,padding=1),
BasicConv(channel_2, channel_3),
BasicConv(channel_3, channel_3),
BasicConv(channel_3, channel_3),
BasicConv(channel_3, channel_3))
self.conv4_0 = BasicConv(channel_3, channel_2)
self.conv4_1 = nn.Sequential(
BasicConv(channel_2, channel_2),
BasicConv(channel_2, channel_2),
BasicConv(channel_2, channel_2))
self.conv5_0 = BasicConv(channel_2, channel_1)
self.conv5_1 = nn.Sequential(
BasicConv(channel_1, channel_1),
BasicConv(channel_1, channel_1),
BasicConv(channel_1, channel_1))
self.conv5_2 = nn.Conv2d(channel_1,channel_out,kernel_size=3,stride=1,padding=1)
def _upsample(self,x,y):
_,_,H,W = y.size()
return F.upsample(x,size=(H,W),mode='bilinear')
def forward(self, x):
#encoder
x1_1 = self.conv1_1(x)
x2_1 = self.conv2_1(x1_1)
x3_1 = self.conv3_1(x2_1)
#attention
#x3_3 = self.densenet(x3_1)
#decoder
x4_1 = self.conv4_0(self._upsample(x3_1,x2_1))+x2_1
x4_1 = self.conv4_1(x4_1)
x5_1 = self.conv5_0(self._upsample(x4_1,x1_1))+x1_1
x5_1 = self.conv5_1(x5_1)
out = self.conv5_2(x5_1)
return out
class FDFE(nn.Module):
def __init__(self, channel = 32, channel_in = 3, channel_out = 3):
super(FDFE,self).__init__()
self.conv1 = BasicConv(channel_in, channel)
self.conv2 = BasicConv(channel, channel)
self.conv3 = BasicConv(channel*2, channel)
self.conv4 = BasicConv(channel*3, channel)
self.conv5 = BasicConv(channel*4, channel_out)
def forward(self,x):
x1_1 = self.conv1(x)
x1_2 = self.conv2(x1_1)
x1_3 = self.conv3(torch.cat((x1_2,x1_1),1))
x1_4 = self.conv4(torch.cat((x1_3,x1_2,x1_1),1))
x_out = self.conv5(torch.cat((x1_4,x1_3,x1_2,x1_1),1))
return x_out
class FI(nn.Module):
def __init__(self,channel = 32,channel_in = 9, channel_out = 3):
super(FI,self).__init__()
self.conv = nn.Sequential(
BasicConv(channel_in, channel),
Resblock(channel),
Resblock(channel),
Resblock(channel),
Resblock(channel),
Resblock(channel),
nn.Conv2d(channel, channel_out,kernel_size=3,stride=1,padding=1,bias=False)
)
def forward(self,x,y,z):
out = torch.cat((x,y,z),dim=1)
out = self.conv(out)
return out