Codebook MemaryBank and QueryKey

发布 : 2021-11-04 分类 : 深度学习 浏览 :

Codebook

1.Learning Visual Words for Weakly-Supervised Semantic Segmentation

点评:codebook 可训练
image.png

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
def _cosine(x, centroid, p_norm=2):
x_norm = F.normalize(x, dim=1, p=p_norm).detach()
w_norm = F.normalize(centroid, dim=1, p=p_norm)

x_corr = F.conv2d(x_norm, w_norm, )
x_corr = F.softmax(x_corr, dim=1)

y_word = F.one_hot(torch.argmax(x_corr, dim=1), num_classes=centroid.shape[0]).sum(dim=[1, 2]) > 0
x_hist = torch.sum(x_corr, [2, 3], keepdim=True)

return x_corr, x_hist, y_word.detach()


class VWE(nn.Module):
def __init__(self, k_words=256):
super(VWE, self).__init__()

self.k_words = k_words

self.centroid = nn.Parameter(torch.Tensor(self.k_words, 2048, 1, 1), requires_grad=True)
nn.init.kaiming_normal_(self.centroid, a=np.sqrt(5))
# nn.init.xavier_uniform_(self.centroid)

def forward(self, x):
x_corr, x_hist, y_word = _cosine(x=x, centroid=self.centroid)
return x_corr, x_hist, y_word

https://github.com/rulixiang/vwe

MemaryBank

本文作者 : HeoLis
原文链接 : https://ishero.net/Codebook%20MemaryBank%20and%20QueryKey.html
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

学习、记录、分享、获得

微信扫一扫, 向我投食

微信扫一扫, 向我投食