File size: 8,339 Bytes
81b1a0e |
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
import torch
import torch.nn as nn
from collections import OrderedDict
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models import vgg16, vgg16_bn
from torchvision.models import resnet50
from config import Config
from dataset import class_labels_TR_sorted
from models.backbones.build_backbone import build_backbone
from models.modules.decoder_blocks import BasicDecBlk
from models.modules.lateral_blocks import BasicLatBlk
from models.modules.ing import *
from models.refinement.stem_layer import StemLayer
class RefinerPVTInChannels4(nn.Module):
def __init__(self, in_channels=3+1):
super(RefinerPVTInChannels4, self).__init__()
self.config = Config()
self.epoch = 1
self.bb = build_backbone(self.config.bb, params_settings='in_channels=4')
lateral_channels_in_collection = {
'vgg16': [512, 256, 128, 64], 'vgg16bn': [512, 256, 128, 64], 'resnet50': [1024, 512, 256, 64],
'pvt_v2_b2': [512, 320, 128, 64], 'pvt_v2_b5': [512, 320, 128, 64],
'swin_v1_b': [1024, 512, 256, 128], 'swin_v1_l': [1536, 768, 384, 192],
}
channels = lateral_channels_in_collection[self.config.bb]
self.squeeze_module = BasicDecBlk(channels[0], channels[0])
self.decoder = Decoder(channels)
if 0:
for key, value in self.named_parameters():
if 'bb.' in key:
value.requires_grad = False
def forward(self, x):
if isinstance(x, list):
x = torch.cat(x, dim=1)
########## Encoder ##########
if self.config.bb in ['vgg16', 'vgg16bn', 'resnet50']:
x1 = self.bb.conv1(x)
x2 = self.bb.conv2(x1)
x3 = self.bb.conv3(x2)
x4 = self.bb.conv4(x3)
else:
x1, x2, x3, x4 = self.bb(x)
x4 = self.squeeze_module(x4)
########## Decoder ##########
features = [x, x1, x2, x3, x4]
scaled_preds = self.decoder(features)
return scaled_preds
class Refiner(nn.Module):
def __init__(self, in_channels=3+1):
super(Refiner, self).__init__()
self.config = Config()
self.epoch = 1
self.stem_layer = StemLayer(in_channels=in_channels, inter_channels=48, out_channels=3)
self.bb = build_backbone(self.config.bb)
lateral_channels_in_collection = {
'vgg16': [512, 256, 128, 64], 'vgg16bn': [512, 256, 128, 64], 'resnet50': [1024, 512, 256, 64],
'pvt_v2_b2': [512, 320, 128, 64], 'pvt_v2_b5': [512, 320, 128, 64],
'swin_v1_b': [1024, 512, 256, 128], 'swin_v1_l': [1536, 768, 384, 192],
}
channels = lateral_channels_in_collection[self.config.bb]
self.squeeze_module = BasicDecBlk(channels[0], channels[0])
self.decoder = Decoder(channels)
if 0:
for key, value in self.named_parameters():
if 'bb.' in key:
value.requires_grad = False
def forward(self, x):
if isinstance(x, list):
x = torch.cat(x, dim=1)
x = self.stem_layer(x)
########## Encoder ##########
if self.config.bb in ['vgg16', 'vgg16bn', 'resnet50']:
x1 = self.bb.conv1(x)
x2 = self.bb.conv2(x1)
x3 = self.bb.conv3(x2)
x4 = self.bb.conv4(x3)
else:
x1, x2, x3, x4 = self.bb(x)
x4 = self.squeeze_module(x4)
########## Decoder ##########
features = [x, x1, x2, x3, x4]
scaled_preds = self.decoder(features)
return scaled_preds
class Decoder(nn.Module):
def __init__(self, channels):
super(Decoder, self).__init__()
self.config = Config()
DecoderBlock = eval('BasicDecBlk')
LateralBlock = eval('BasicLatBlk')
self.decoder_block4 = DecoderBlock(channels[0], channels[1])
self.decoder_block3 = DecoderBlock(channels[1], channels[2])
self.decoder_block2 = DecoderBlock(channels[2], channels[3])
self.decoder_block1 = DecoderBlock(channels[3], channels[3]//2)
self.lateral_block4 = LateralBlock(channels[1], channels[1])
self.lateral_block3 = LateralBlock(channels[2], channels[2])
self.lateral_block2 = LateralBlock(channels[3], channels[3])
if self.config.ms_supervision:
self.conv_ms_spvn_4 = nn.Conv2d(channels[1], 1, 1, 1, 0)
self.conv_ms_spvn_3 = nn.Conv2d(channels[2], 1, 1, 1, 0)
self.conv_ms_spvn_2 = nn.Conv2d(channels[3], 1, 1, 1, 0)
self.conv_out1 = nn.Sequential(nn.Conv2d(channels[3]//2, 1, 1, 1, 0))
def forward(self, features):
x, x1, x2, x3, x4 = features
outs = []
p4 = self.decoder_block4(x4)
_p4 = F.interpolate(p4, size=x3.shape[2:], mode='bilinear', align_corners=True)
_p3 = _p4 + self.lateral_block4(x3)
p3 = self.decoder_block3(_p3)
_p3 = F.interpolate(p3, size=x2.shape[2:], mode='bilinear', align_corners=True)
_p2 = _p3 + self.lateral_block3(x2)
p2 = self.decoder_block2(_p2)
_p2 = F.interpolate(p2, size=x1.shape[2:], mode='bilinear', align_corners=True)
_p1 = _p2 + self.lateral_block2(x1)
_p1 = self.decoder_block1(_p1)
_p1 = F.interpolate(_p1, size=x.shape[2:], mode='bilinear', align_corners=True)
p1_out = self.conv_out1(_p1)
if self.config.ms_supervision:
outs.append(self.conv_ms_spvn_4(p4))
outs.append(self.conv_ms_spvn_3(p3))
outs.append(self.conv_ms_spvn_2(p2))
outs.append(p1_out)
return outs
class RefUNet(nn.Module):
# Refinement
def __init__(self, in_channels=3+1):
super(RefUNet, self).__init__()
self.encoder_1 = nn.Sequential(
nn.Conv2d(in_channels, 64, 3, 1, 1),
nn.Conv2d(64, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.encoder_2 = nn.Sequential(
nn.MaxPool2d(2, 2, ceil_mode=True),
nn.Conv2d(64, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.encoder_3 = nn.Sequential(
nn.MaxPool2d(2, 2, ceil_mode=True),
nn.Conv2d(64, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.encoder_4 = nn.Sequential(
nn.MaxPool2d(2, 2, ceil_mode=True),
nn.Conv2d(64, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.pool4 = nn.MaxPool2d(2, 2, ceil_mode=True)
#####
self.decoder_5 = nn.Sequential(
nn.Conv2d(64, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
#####
self.decoder_4 = nn.Sequential(
nn.Conv2d(128, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.decoder_3 = nn.Sequential(
nn.Conv2d(128, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.decoder_2 = nn.Sequential(
nn.Conv2d(128, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.decoder_1 = nn.Sequential(
nn.Conv2d(128, 64, 3, 1, 1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.conv_d0 = nn.Conv2d(64, 1, 3, 1, 1)
self.upscore2 = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
def forward(self, x):
outs = []
if isinstance(x, list):
x = torch.cat(x, dim=1)
hx = x
hx1 = self.encoder_1(hx)
hx2 = self.encoder_2(hx1)
hx3 = self.encoder_3(hx2)
hx4 = self.encoder_4(hx3)
hx = self.decoder_5(self.pool4(hx4))
hx = torch.cat((self.upscore2(hx), hx4), 1)
d4 = self.decoder_4(hx)
hx = torch.cat((self.upscore2(d4), hx3), 1)
d3 = self.decoder_3(hx)
hx = torch.cat((self.upscore2(d3), hx2), 1)
d2 = self.decoder_2(hx)
hx = torch.cat((self.upscore2(d2), hx1), 1)
d1 = self.decoder_1(hx)
x = self.conv_d0(d1)
outs.append(x)
return outs
|