forked from MenghaoGuo/Awesome-Vision-Attentions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import jittor as jt | ||
import jittor.nn as nn | ||
from jittor import init | ||
|
||
|
||
class DANetHead(nn.Module): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Simam: A simple, parameter-free attention module for convolutional neural networks (ICML 2021) | ||
import jittor as jt | ||
from jittor import nn | ||
|
||
|
||
class simam_module(nn.Module): | ||
def __init__(self, e_lambda=1e-4): | ||
super(simam_module, self).__init__() | ||
|
||
self.activaton = nn.Sigmoid() | ||
self.e_lambda = e_lambda | ||
|
||
def execute(self, x): | ||
|
||
b, c, h, w = x.size() | ||
|
||
n = w * h - 1 | ||
|
||
x_minus_mu_square = ( | ||
x - x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True)).pow(2) | ||
y = x_minus_mu_square / \ | ||
(4 * (x_minus_mu_square.sum(dim=2, | ||
keepdims=True).sum(dim=3, | ||
keepdims=True) / n + self.e_lambda)) + 0.5 | ||
|
||
return x * self.activaton(y) | ||
|
||
|
||
def main(): | ||
attention_block = simam_module() | ||
input = jt.ones([4, 64, 32, 32]) | ||
output = attention_block(input) | ||
print(input.size(), output.size()) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |