MDViT / data /Models /Sota_adapters /se_module_vector.py
introvoyz041's picture
Migrated from GitHub
d12790b verified
Raw
History Blame Contribute Delete
1.14 kB
'''
for Domain-attentive SE adapter
download from https://github.com/frank-xwang/towards-universal-object-detection/blob/cf40aed4c79b86b3e8e08e4adf94f43742693111/lib/model/faster_rcnn/se_module_vector.py
'''
from torch import nn
import torch.nn.functional as F
class SELayer(nn.Module):
def __init__(self, channel, reduction=16, with_sigmoid=True):
super(SELayer, self).__init__()
self.with_sigmoid = with_sigmoid
self.avg_pool = nn.AdaptiveAvgPool2d(1)
if with_sigmoid:
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel),
nn.Sigmoid()
)
else:
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel),
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return y