-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTranSalNet_Res.py
171 lines (136 loc) · 5.16 KB
/
TranSalNet_Res.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
import os
import torch
import numpy as np
import pandas as pd
from torch.utils.data import Dataset, DataLoader
from skimage import io, transform
from PIL import Image
import torch.nn as nn
from torchvision import transforms, utils, models
import torch.nn.functional as F
import utils.resnet as resnet
from utils.TransformerEncoder import Encoder
cfg1 = {
"hidden_size" : 768,
"mlp_dim" : 768*4,
"num_heads" : 12,
"num_layers" : 2,
"attention_dropout_rate" : 0,
"dropout_rate" : 0.0,
}
cfg2 = {
"hidden_size" : 768,
"mlp_dim" : 768*4,
"num_heads" : 12,
"num_layers" : 2,
"attention_dropout_rate" : 0,
"dropout_rate" : 0.0,
}
cfg3 = {
"hidden_size" : 512,
"mlp_dim" : 512*4,
"num_heads" : 8,
"num_layers" : 2,
"attention_dropout_rate" : 0,
"dropout_rate" : 0.0,
}
class TranSalNet(nn.Module):
def __init__(self):
super(TranSalNet, self).__init__()
self.encoder = _Encoder()
self.decoder = _Decoder()
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x
class _Encoder(nn.Module):
def __init__(self):
super(_Encoder, self).__init__()
base_model = resnet.resnet50(pretrained=True)
base_layers = list(base_model.children())[:8]
self.encoder = nn.ModuleList(base_layers).eval()
def forward(self, x):
outputs = []
for ii,layer in enumerate(self.encoder):
x = layer(x)
if ii in {5,6,7}:
outputs.append(x)
return outputs
class _Decoder(nn.Module):
def __init__(self):
super(_Decoder, self).__init__()
self.conv1 = nn.Conv2d(768, 768, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
self.conv2 = nn.Conv2d(768, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
self.conv3 = nn.Conv2d(512, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
self.conv4 = nn.Conv2d(256, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
self.conv5 = nn.Conv2d(128, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
self.conv6 = nn.Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
self.conv7 = nn.Conv2d(32, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
self.batchnorm1 = nn.BatchNorm2d(768, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.batchnorm2 = nn.BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.batchnorm3 = nn.BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.batchnorm4 = nn.BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.batchnorm5 = nn.BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.batchnorm6 = nn.BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
self.TransEncoder1 = TransEncoder(in_channels=2048, spatial_size=9*12, cfg=cfg1)
self.TransEncoder2 = TransEncoder(in_channels=1024, spatial_size=18*24, cfg=cfg2)
self.TransEncoder3 = TransEncoder(in_channels=512, spatial_size=36*48, cfg=cfg3)
self.add = torch.add
self.relu = nn.ReLU(True)
self.upsample = nn.Upsample(scale_factor=2, mode='nearest')
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x3, x4, x5 = x
x5 = self.TransEncoder1(x5)
x5 = self.conv1(x5)
x5 = self.batchnorm1(x5)
x5 = self.relu(x5)
x5 = self.upsample(x5)
x4_a = self.TransEncoder2(x4)
x4 = x5 * x4_a
x4 = self.relu(x4)
x4 = self.conv2(x4)
x4 = self.batchnorm2(x4)
x4 = self.relu(x4)
x4 = self.upsample(x4)
x3_a = self.TransEncoder3(x3)
x3 = x4 * x3_a
x3 = self.relu(x3)
x3 = self.conv3(x3)
x3 = self.batchnorm3(x3)
x3 = self.relu(x3)
x3 = self.upsample(x3)
x2 = self.conv4(x3)
x2 = self.batchnorm4(x2)
x2 = self.relu(x2)
x2 = self.upsample(x2)
x2 = self.conv5(x2)
x2 = self.batchnorm5(x2)
x2 = self.relu(x2)
x1 = self.upsample(x2)
x1 = self.conv6(x1)
x1 = self.batchnorm6(x1)
x1 = self.relu(x1)
x1 = self.conv7(x1)
x = self.sigmoid(x1)
return x
class TransEncoder(nn.Module):
def __init__(self, in_channels, spatial_size, cfg):
super(TransEncoder, self).__init__()
self.patch_embeddings = nn.Conv2d(in_channels=in_channels,
out_channels=cfg['hidden_size'],
kernel_size=1,
stride=1)
self.position_embeddings = nn.Parameter(torch.zeros(1, spatial_size, cfg['hidden_size']))
self.transformer_encoder = Encoder(cfg)
def forward(self, x):
a, b = x.shape[2], x.shape[3]
x = self.patch_embeddings(x)
x = x.flatten(2)
x = x.transpose(-1, -2)
embeddings = x + self.position_embeddings
x = self.transformer_encoder(embeddings)
B, n_patch, hidden = x.shape
x = x.permute(0, 2, 1)
x = x.contiguous().view(B, hidden, a, b)
return x