You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
5.0 KiB
127 lines
5.0 KiB
5 years ago
|
"""
|
||
5 years ago
|
ECA module from ECAnet
|
||
5 years ago
|
|
||
|
paper: ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks
|
||
5 years ago
|
https://arxiv.org/abs/1910.03151
|
||
|
|
||
5 years ago
|
Original ECA model borrowed from https://github.com/BangguWu/ECANet
|
||
|
|
||
|
Modified circular ECA implementation and adaption for use in timm package
|
||
5 years ago
|
by Chris Ha https://github.com/VRandme
|
||
|
|
||
5 years ago
|
Original License:
|
||
|
|
||
5 years ago
|
MIT License
|
||
|
|
||
5 years ago
|
Copyright (c) 2019 BangguWu, Qilong Wang
|
||
5 years ago
|
|
||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
of this software and associated documentation files (the "Software"), to deal
|
||
|
in the Software without restriction, including without limitation the rights
|
||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
copies of the Software, and to permit persons to whom the Software is
|
||
|
furnished to do so, subject to the following conditions:
|
||
|
|
||
|
The above copyright notice and this permission notice shall be included in all
|
||
|
copies or substantial portions of the Software.
|
||
|
|
||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
SOFTWARE.
|
||
5 years ago
|
"""
|
||
5 years ago
|
import math
|
||
5 years ago
|
from torch import nn
|
||
5 years ago
|
import torch.nn.functional as F
|
||
5 years ago
|
|
||
5 years ago
|
|
||
5 years ago
|
class EcaModule(nn.Module):
|
||
5 years ago
|
"""Constructs an ECA module.
|
||
5 years ago
|
|
||
|
Args:
|
||
5 years ago
|
channel: Number of channels of the input feature map for use in adaptive kernel sizes
|
||
|
for actual calculations according to channel.
|
||
|
gamma, beta: when channel is given parameters of mapping function
|
||
|
refer to original paper https://arxiv.org/pdf/1910.03151.pdf
|
||
|
(default=None. if channel size not given, use k_size given for kernel size.)
|
||
|
k_size: Adaptive selection of kernel size (default=3)
|
||
5 years ago
|
"""
|
||
5 years ago
|
def __init__(self, channel=None, k_size=3, gamma=2, beta=1):
|
||
5 years ago
|
super(EcaModule, self).__init__()
|
||
5 years ago
|
assert k_size % 2 == 1
|
||
5 years ago
|
|
||
|
if channel is not None:
|
||
|
t = int(abs(math.log(channel, 2)+beta) / gamma)
|
||
|
k_size = t if t % 2 else t + 1
|
||
|
|
||
5 years ago
|
self.avg_pool = nn.AdaptiveAvgPool2d(1)
|
||
5 years ago
|
self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1) // 2, bias=False)
|
||
5 years ago
|
self.sigmoid = nn.Sigmoid()
|
||
5 years ago
|
|
||
5 years ago
|
def forward(self, x):
|
||
5 years ago
|
# Feature descriptor on the global spatial information
|
||
5 years ago
|
y = self.avg_pool(x)
|
||
5 years ago
|
# Reshape for convolution
|
||
5 years ago
|
y = y.view(x.shape[0], 1, -1)
|
||
|
# Two different branches of ECA module
|
||
|
y = self.conv(y)
|
||
|
# Multi-scale information fusion
|
||
|
y = self.sigmoid(y.view(x.shape[0], -1, 1, 1))
|
||
|
return x * y.expand_as(x)
|
||
5 years ago
|
|
||
5 years ago
|
|
||
5 years ago
|
class CecaModule(nn.Module):
|
||
5 years ago
|
"""Constructs a circular ECA module.
|
||
5 years ago
|
|
||
|
ECA module where the conv uses circular padding rather than zero padding.
|
||
|
Unlike the spatial dimension, the channels do not have inherent ordering nor
|
||
5 years ago
|
locality. Although this module in essence, applies such an assumption, it is unnecessary
|
||
|
to limit the channels on either "edge" from being circularly adapted to each other.
|
||
|
This will fundamentally increase connectivity and possibly increase performance metrics
|
||
5 years ago
|
(accuracy, robustness), without signficantly impacting resource metrics
|
||
5 years ago
|
(parameter size, throughput,latency, etc)
|
||
|
|
||
|
Args:
|
||
5 years ago
|
channel: Number of channels of the input feature map for use in adaptive kernel sizes
|
||
|
for actual calculations according to channel.
|
||
|
gamma, beta: when channel is given parameters of mapping function
|
||
|
refer to original paper https://arxiv.org/pdf/1910.03151.pdf
|
||
|
(default=None. if channel size not given, use k_size given for kernel size.)
|
||
|
k_size: Adaptive selection of kernel size (default=3)
|
||
5 years ago
|
"""
|
||
5 years ago
|
|
||
|
def __init__(self, channel=None, k_size=3, gamma=2, beta=1):
|
||
5 years ago
|
super(CecaModule, self).__init__()
|
||
5 years ago
|
assert k_size % 2 == 1
|
||
5 years ago
|
|
||
|
if channel is not None:
|
||
|
t = int(abs(math.log(channel, 2)+beta) / gamma)
|
||
|
k_size = t if t % 2 else t + 1
|
||
|
|
||
5 years ago
|
self.avg_pool = nn.AdaptiveAvgPool2d(1)
|
||
5 years ago
|
#pytorch circular padding mode is buggy as of pytorch 1.4
|
||
5 years ago
|
#see https://github.com/pytorch/pytorch/pull/17240
|
||
|
|
||
5 years ago
|
#implement manual circular padding
|
||
5 years ago
|
self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=0, bias=False)
|
||
5 years ago
|
self.padding = (k_size - 1) // 2
|
||
|
self.sigmoid = nn.Sigmoid()
|
||
|
|
||
|
def forward(self, x):
|
||
5 years ago
|
# Feature descriptor on the global spatial information
|
||
5 years ago
|
y = self.avg_pool(x)
|
||
5 years ago
|
|
||
5 years ago
|
# Manually implement circular padding, F.pad does not seemed to be bugged
|
||
5 years ago
|
y = F.pad(y.view(x.shape[0], 1, -1), (self.padding, self.padding), mode='circular')
|
||
5 years ago
|
|
||
|
# Two different branches of ECA module
|
||
5 years ago
|
y = self.conv(y)
|
||
5 years ago
|
|
||
|
# Multi-scale information fusion
|
||
5 years ago
|
y = self.sigmoid(y.view(x.shape[0], -1, 1, 1))
|
||
5 years ago
|
|
||
|
return x * y.expand_as(x)
|