Spaces:
Sleeping
Sleeping
import torch | |
from torch import nn | |
from torch.nn import functional as F | |
from collections import OrderedDict | |
class SimpleDecoding(nn.Module): | |
def __init__(self, dims, factor=2): | |
super(SimpleDecoding, self).__init__() | |
hidden_size = dims[-1]//factor | |
c4_size = dims[-1] | |
c3_size = dims[-2] | |
c2_size = dims[-3] | |
c1_size = dims[-4] | |
self.conv1_4 = nn.Conv2d(c4_size+c3_size, hidden_size, 3, padding=1, bias=False) | |
self.bn1_4 = nn.BatchNorm2d(hidden_size) | |
self.relu1_4 = nn.ReLU() | |
self.conv2_4 = nn.Conv2d(hidden_size, hidden_size, 3, padding=1, bias=False) | |
self.bn2_4 = nn.BatchNorm2d(hidden_size) | |
self.relu2_4 = nn.ReLU() | |
self.conv1_3 = nn.Conv2d(hidden_size + c2_size, hidden_size, 3, padding=1, bias=False) | |
self.bn1_3 = nn.BatchNorm2d(hidden_size) | |
self.relu1_3 = nn.ReLU() | |
self.conv2_3 = nn.Conv2d(hidden_size, hidden_size, 3, padding=1, bias=False) | |
self.bn2_3 = nn.BatchNorm2d(hidden_size) | |
self.relu2_3 = nn.ReLU() | |
self.conv1_2 = nn.Conv2d(hidden_size + c1_size, hidden_size, 3, padding=1, bias=False) | |
self.bn1_2 = nn.BatchNorm2d(hidden_size) | |
self.relu1_2 = nn.ReLU() | |
self.conv2_2 = nn.Conv2d(hidden_size, hidden_size, 3, padding=1, bias=False) | |
self.bn2_2 = nn.BatchNorm2d(hidden_size) | |
self.relu2_2 = nn.ReLU() | |
self.conv1_1 = nn.Conv2d(hidden_size, 2, 1) | |
def forward(self, x_c4, x_c3, x_c2, x_c1): | |
# fuse Y4 and Y3 | |
if x_c4.size(-2) < x_c3.size(-2) or x_c4.size(-1) < x_c3.size(-1): | |
x_c4 = F.interpolate(input=x_c4, size=(x_c3.size(-2), x_c3.size(-1)), mode='bilinear', align_corners=True) | |
x = torch.cat([x_c4, x_c3], dim=1) | |
x = self.conv1_4(x) | |
x = self.bn1_4(x) | |
x = self.relu1_4(x) | |
x = self.conv2_4(x) | |
x = self.bn2_4(x) | |
x = self.relu2_4(x) | |
# fuse top-down features and Y2 features | |
if x.size(-2) < x_c2.size(-2) or x.size(-1) < x_c2.size(-1): | |
x = F.interpolate(input=x, size=(x_c2.size(-2), x_c2.size(-1)), mode='bilinear', align_corners=True) | |
x = torch.cat([x, x_c2], dim=1) | |
x = self.conv1_3(x) | |
x = self.bn1_3(x) | |
x = self.relu1_3(x) | |
x = self.conv2_3(x) | |
x = self.bn2_3(x) | |
x = self.relu2_3(x) | |
# fuse top-down features and Y1 features | |
if x.size(-2) < x_c1.size(-2) or x.size(-1) < x_c1.size(-1): | |
x = F.interpolate(input=x, size=(x_c1.size(-2), x_c1.size(-1)), mode='bilinear', align_corners=True) | |
x = torch.cat([x, x_c1], dim=1) | |
x = self.conv1_2(x) | |
x = self.bn1_2(x) | |
x = self.relu1_2(x) | |
x = self.conv2_2(x) | |
x = self.bn2_2(x) | |
x = self.relu2_2(x) | |
return self.conv1_1(x) |