File size: 4,221 Bytes
beb7843 |
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 |
import torch
import torch.nn as nn
from ..basic.conv import Conv2d
# Channel Self Attetion Module
class CSAM(nn.Module):
""" Channel attention module """
def __init__(self):
super(CSAM, self).__init__()
self.gamma = nn.Parameter(torch.zeros(1))
self.softmax = nn.Softmax(dim=-1)
def forward(self, x):
"""
inputs :
x : input feature maps( B x C x H x W )
returns :
out : attention value + input feature
attention: B x C x C
"""
B, C, H, W = x.size()
# query / key / value
query = x.view(B, C, -1)
key = x.view(B, C, -1).permute(0, 2, 1)
value = x.view(B, C, -1)
# attention matrix
energy = torch.bmm(query, key)
energy_new = torch.max(energy, -1, keepdim=True)[0].expand_as(energy) - energy
attention = self.softmax(energy_new)
# attention
out = torch.bmm(attention, value)
out = out.view(B, C, H, W)
# output
out = self.gamma * out + x
return out
# Spatial Self Attetion Module
class SSAM(nn.Module):
""" Channel attention module """
def __init__(self):
super(SSAM, self).__init__()
self.gamma = nn.Parameter(torch.zeros(1))
self.softmax = nn.Softmax(dim=-1)
def forward(self, x):
"""
inputs :
x : input feature maps( B x C x H x W )
returns :
out : attention value + input feature
attention: B x C x C
"""
B, C, H, W = x.size()
# query / key / value
query = x.view(B, C, -1).permute(0, 2, 1) # [B, N, C]
key = x.view(B, C, -1) # [B, C, N]
value = x.view(B, C, -1).permute(0, 2, 1) # [B, N, C]
# attention matrix
energy = torch.bmm(query, key)
energy_new = torch.max(energy, -1, keepdim=True)[0].expand_as(energy) - energy
attention = self.softmax(energy_new)
# attention
out = torch.bmm(attention, value)
out = out.permute(0, 2, 1).contiguous().view(B, C, H, W)
# output
out = self.gamma * out + x
return out
# Channel Encoder
class ChannelEncoder(nn.Module):
def __init__(self, in_dim, out_dim, act_type='', norm_type=''):
super().__init__()
self.fuse_convs = nn.Sequential(
Conv2d(in_dim, out_dim, k=1, act_type=act_type, norm_type=norm_type),
Conv2d(out_dim, out_dim, k=3, p=1, act_type=act_type, norm_type=norm_type),
CSAM(),
Conv2d(out_dim, out_dim, k=3, p=1, act_type=act_type, norm_type=norm_type),
nn.Dropout(0.1, inplace=False),
nn.Conv2d(out_dim, out_dim, kernel_size=1)
)
def forward(self, x1, x2):
"""
x: [B, C, H, W]
"""
x = torch.cat([x1, x2], dim=1)
# [B, CN, H, W] -> [B, C, H, W]
x = self.fuse_convs(x)
return x
# Spatial Encoder
class SpatialEncoder(nn.Module):
def __init__(self, in_dim, out_dim, act_type='', norm_type=''):
super().__init__()
self.fuse_convs = nn.Sequential(
Conv2d(in_dim, out_dim, k=1, act_type=act_type, norm_type=norm_type),
Conv2d(out_dim, out_dim, k=3, p=1, act_type=act_type, norm_type=norm_type),
SSAM(),
Conv2d(out_dim, out_dim, k=3, p=1, act_type=act_type, norm_type=norm_type),
nn.Dropout(0.1, inplace=False),
nn.Conv2d(out_dim, out_dim, kernel_size=1)
)
def forward(self, x):
"""
x: [B, C, H, W]
"""
x = self.fuse_convs(x)
return x
def build_channel_encoder(cfg, in_dim, out_dim):
encoder = ChannelEncoder(
in_dim=in_dim,
out_dim=out_dim,
act_type=cfg['head_act'],
norm_type=cfg['head_norm']
)
return encoder
def build_spatial_encoder(cfg, in_dim, out_dim):
encoder = SpatialEncoder(
in_dim=in_dim,
out_dim=out_dim,
act_type=cfg['head_act'],
norm_type=cfg['head_norm']
)
return encoder
|