-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecoder.py
More file actions
49 lines (37 loc) · 1.78 KB
/
Decoder.py
File metadata and controls
49 lines (37 loc) · 1.78 KB
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
import torch
import torch.nn as nn
import torch.nn.functional as F
class PFFN(nn.Module):
""" Position-Wise Feedforward Network (PFFN) """
def __init__(self, dim):
super().__init__()
self.fc1 = nn.Linear(dim, dim * 4)
self.act = nn.GELU()
self.fc2 = nn.Linear(dim * 4, dim)
self.norm = nn.LayerNorm(dim)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.fc2(x)
return self.norm(x)
class Decoder(nn.Module):
""" Cascaded CNN and FFN Decoder """
def __init__(self, embed_dims=[64, 128, 256, 512], output_nc=2):
super().__init__()
self.ffn_layers = nn.ModuleList([
PFFN(embed_dims[i]) for i in range(len(embed_dims))
])
self.upsample_layers = nn.ModuleList([
nn.Upsample(scale_factor=4, mode='bilinear', align_corners=True) for _ in range(len(embed_dims))
])
self.conv_fusion = nn.Conv2d(embed_dims[0] * 4, embed_dims[0], kernel_size=1)
self.final_ffn = PFFN(embed_dims[0])
self.final_upsample = nn.ConvTranspose2d(embed_dims[0], output_nc, kernel_size=3, stride=4, padding=1, output_padding=3)
def forward(self, fdb_outputs):
standardized_features = [self.ffn_layers[i](fdb_outputs[i]) for i in range(len(fdb_outputs))]
upsampled_features = [self.upsample_layers[i](standardized_features[i]) for i in range(len(standardized_features))]
fused_features = torch.cat(upsampled_features, dim=1)
fused_features = self.conv_fusion(fused_features)
adjusted_features = self.final_ffn(fused_features)
output = self.final_upsample(adjusted_features)
return output