-
Notifications
You must be signed in to change notification settings - Fork 4
/
BCNN.py
39 lines (34 loc) · 1.09 KB
/
BCNN.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
import torch
import torch.nn as nn
eps = 1e-8
class BCNN(nn.Module):
def __init__(self, thresh=1e-8, is_vec=True, input_dim=512):
super(BCNN, self).__init__()
self.thresh = thresh
self.is_vec = is_vec
self.output_dim = input_dim * input_dim
def _bilinearpool(self, x, y=None):
batchSize, dim, h, w = x.data.shape
x = x.reshape(batchSize, dim, h * w)
if y is not None:
y = y.reshape(batchSize, dim, h * w)
x = 1. / (h * w) * x.bmm(y.transpose(1, 2))
else:
x = 1. / (h * w) * x.bmm(x.transpose(1, 2))
return x
def _signed_sqrt(self, x):
x = torch.mul(x.sign(), torch.sqrt(x.abs() + self.thresh))
return x
def _l2norm(self, x):
x = nn.functional.normalize(x)
return x
def forward(self, x, y=None):
if y is not None:
x = self._bilinearpool(x,y)
else:
x = self._bilinearpool(x)
x = self._signed_sqrt(x)
if self.is_vec:
x = x.view(x.size(0), -1)
x = self._l2norm(x)
return x