Spaces:
Runtime error
Runtime error
Upload SimAM.py
Browse files- models/SimAM.py +29 -0
models/SimAM.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
|
4 |
+
|
5 |
+
class SimAM(torch.nn.Module):
|
6 |
+
def __init__(self, e_lambda=1e-4):
|
7 |
+
super(SimAM, self).__init__()
|
8 |
+
|
9 |
+
self.activaton = nn.Sigmoid()
|
10 |
+
self.e_lambda = e_lambda
|
11 |
+
|
12 |
+
def __repr__(self):
|
13 |
+
s = self.__class__.__name__ + '('
|
14 |
+
s += ('lambda=%f)' % self.e_lambda)
|
15 |
+
return s
|
16 |
+
|
17 |
+
@staticmethod
|
18 |
+
def get_module_name():
|
19 |
+
return "simam"
|
20 |
+
|
21 |
+
def forward(self, x):
|
22 |
+
b, c, h, w = x.size()
|
23 |
+
|
24 |
+
n = w * h - 1
|
25 |
+
|
26 |
+
x_minus_mu_square = (x - x.mean(dim=[2, 3], keepdim=True)).pow(2)
|
27 |
+
y = x_minus_mu_square / (4 * (x_minus_mu_square.sum(dim=[2, 3], keepdim=True) / n + self.e_lambda)) + 0.5
|
28 |
+
|
29 |
+
return x * self.activaton(y)
|