-
Notifications
You must be signed in to change notification settings - Fork 1
/
Attention_module.py
66 lines (45 loc) · 1.48 KB
/
Attention_module.py
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
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from __future__ import with_statement
import math
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import numpy as np
import os
def mean_channels_h(F):
assert(F.dim() == 4)
spatial_sum = F.sum(3, keepdim=True)
return spatial_sum / F.size(3)
def stdv_channels_h(F):
assert(F.dim() == 4)
F_mean = mean_channels_h(F)
F_variance = (F - F_mean).pow(2).sum(3, keepdim=True) / F.size(3)
return F_variance
def mean_channels_w(F):
assert(F.dim() == 4)
spatial_sum = F.sum(2, keepdim=True)
return spatial_sum / F.size(2)
def stdv_channels_w(F):
assert(F.dim() == 4)
F_mean = mean_channels_w(F)
F_variance = (F - F_mean).pow(2).sum(2, keepdim=True) / F.size(2)
return F_variance
class DiVA_attention(nn.Module):
def __init__(self):
super(DiVA_attention, self).__init__()
self.contrast_h = stdv_channels_h
self.contrast_w = stdv_channels_w
self.conv_h = nn.Conv2d(64, 64, kernel_size=1, stride=1, padding=0)
self.conv_w = nn.Conv2d(64, 64, kernel_size=1, stride=1, padding=0)
def forward(self, x):
identity = x
n,c,h,w = x.size()
c_h = self.contrast_h(x)
c_w = self.contrast_w(x)
a_h = self.conv_h(c_h).sigmoid()
a_w = self.conv_w(c_w).sigmoid()
out = identity * a_w * a_h
return out