-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattention.py
65 lines (58 loc) · 2.03 KB
/
attention.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
import torch
import torch.nn as nn
import torch.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt
from scipy.misc import imsave
import numpy as np
import random
class AttentionModule(nn.Module):
""" Online attention Layer"""
def __init__(self):
super(AttentionModule, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=(5, 5), stride=(2, 2)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.conv2 = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(5, 5), stride=(1, 1)),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True)
)
self.conv3 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=512, kernel_size=(3, 3), stride=(1, 1)),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True)
)
self.softmax = nn.Softmax(dim=1)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.inter1 = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
self.inter2 = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True)
def forward(self, x):
b, c, h, w = x.size()
#print(x.size())
x = self.conv1(x)
x = self.maxpool(x)
x = self.conv2(x)
x = self.maxpool(x)
x = self.inter1(x)
x = self.conv3(x)
x = self.maxpool(x)
x = self.softmax(x)
x = torch.reshape(x, (b, c, h, w))
#out = input_img + x
#out = x.detach().cpu().numpy()
#print(out[0].shape)
#img = np.transpose(out[0], (1, 2, 0))
#imsave("./attention/%d_img.png" % (random.randint(0, 100)), img)
return x
if __name__ == "__main__":
# net = ft_net(751)
net = AttentionModule()
# print(net)
input = Variable(torch.FloatTensor(8, 3, 64, 128))
output = net(input)
print('net output size:')
print(output.size())