-
Notifications
You must be signed in to change notification settings - Fork 1
/
StageInfoemationTransferModule.py
157 lines (118 loc) · 4.79 KB
/
StageInfoemationTransferModule.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
class SITM(nn.Module):
def __init__(self,in_channel,hidden_channel):
super(SITM,self).__init__()
self.reset = nn.Conv2d(in_channels=in_channel*2,out_channels=in_channel,kernel_size=1)
self.in_channel = in_channel
self.conv = nn.Conv2d(in_channels=hidden_channel,out_channels=in_channel,kernel_size=3,stride=2,padding=1)
self.delta = nn.Parameter(torch.Tensor([0.1]))
def forward(self,input,pre_state):
# get batch and spatial sizes
batch = input.size(0)
spatial = input.size(3)
# generata empty perstate, if Note is provided
if pre_state is None:
state_size = [batch, self.in_channel,spatial,spatial]
if torch.cuda.is_available():
pre_state = Variable(torch.zeros(state_size)).cuda()
else:
pre_state = Variable(torch.zeros(state_size))
else:
pre_state = self.conv(pre_state)
pre_state = torch.max_pool2d(pre_state,kernel_size=1)
pre_state = nn.MaxPool2d(1)(pre_state)
if pre_state.size()[3] != input.size()[3]:
diffY = pre_state.size()[2] - input.size()[2]
diffX = pre_state.size()[3] - input.size()[3]
input = nn.functional.pad(input, (diffX // 2, diffX - diffX // 2, diffY // 2, diffY - diffY // 2))
stacked_inputs = torch.cat([input, pre_state], dim=1)
out = self.reset(stacked_inputs)
reset = torch.sigmoid(out)
update = torch.sigmoid(input + reset * pre_state)
new_state = input+ self.delta*(update * input)
return new_state
class MAM(nn.Module):# channel
def __init__(self,):
super(MAM,self).__init__()
self.delta = nn.Parameter(torch.Tensor([0.1]))
def forward(self,input,state):
if state.size()[3] != input.size()[3]:
diffY = state.size()[2] - input.size()[2]
diffX = state.size()[3] - input.size()[3]
input = nn.functional.pad(input, (diffX // 2, diffX - diffX // 2, diffY // 2, diffY - diffY // 2))
resnet_gata = torch.sigmoid(input + state)
resnet = resnet_gata*state
weight = input * resnet_gata
weigt = weight + resnet
out = input * self.delta*weigt
return out
class Globel_Attention(nn.Module):
def __init__(self,c,s,k):
super(Globel_Attention,self).__init__()
# self.k = 64
# self.c = x.size(1)
# self.s = x.size(2)*x.size(3)
self.linear_c = nn.Linear(c,k)
self.linear_s = nn.Linear(s,k)
self.delta = nn.Parameter(torch.Tensor([0.1]))
def forward(self,input):
# c = input.size(1)
# s = input.size(2) * input.size(3)
# linear_c = nn.Linear(c, k).cuda()
# linear_s = nn.Linear(s, k).cuda()
B,C,H,W = input.size()
x = input.view(B,C,-1).contiguous() #[B,C,H*W]
# print(x.shape)
Attention_s = self.linear_s(x) #[B,C,k]
x = x.permute(0,2,1).contiguous() #[B,H*W,C]
Attention_c = self.linear_c(x).permute(0,2,1).contiguous() #[B,k,H*W]
out = torch.bmm(Attention_s,Attention_c)#[B,C,H*W]
out = out.view(B,C,H,W).contiguous()
out = input + self.delta*out
return out
class Convblock(nn.Module):
def __init__(self,ic,oc,ks):
# ic: input channels
# oc: ouput channels
# ks: kernel size
super(Convblock,self).__init__()
self.left = nn.Sequential(
nn.Conv2d(ic,oc,kernel_size=ks,padding=(ks-1)//2),
nn.BatchNorm2d(oc),
nn.LeakyReLU(),
nn.Conv2d(oc,oc,kernel_size=ks,padding=(ks-1)//2),
nn.BatchNorm2d(oc),
nn.LeakyReLU()
)
def forward(self,x):
y = self.left(x)
return y
class decoderunit(nn.Module):
def __init__(self,ic1,ic2,ot,ks):
# ic: input channels
# oc: ouput channels
# ks: kernel size
super(decoderunit,self).__init__()
self.left=nn.Sequential(
nn.ConvTranspose2d(ic1,ot//2,ks,padding=1),
nn.BatchNorm2d(ot//2),
nn.ReLU(),
nn.ConvTranspose2d(ot//2,ot,kernel_size=2,stride=2),
nn.BatchNorm2d(ot),
nn.ReLU()
)
self.conv1 = nn.Conv2d(ot,ot//2,kernel_size=1)
self.conv2 = nn.Conv2d(ic2,ot//2,kernel_size=1)
def forward(self,x1,x2):
x1 = self.left(x1)
diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]
x1 = nn.functional.pad(x1, (diffX // 2, diffX - diffX // 2,diffY // 2, diffY - diffY // 2))
x1 = self.conv1(x1)
x2 = self.conv2(x2)
x = torch.cat([x1,x2],dim=1)
out = F.relu(x)
return out