-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy path(arXiv 2021) MobileViTv2.py
68 lines (55 loc) · 2.02 KB
/
(arXiv 2021) MobileViTv2.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
67
68
import numpy as np
import torch
from torch import nn
from torch.nn import init
# 论文地址:https://arxiv.org/pdf/2206.02680
# 论文:Separable Self-attention for Mobile Vision Transformers
class MobileViTv2Attention(nn.Module):
'''
Scaled dot-product attention
'''
def __init__(self, d_model):
'''
:param d_model: Output dimensionality of the model
:param d_k: Dimensionality of queries and keys
:param d_v: Dimensionality of values
:param h: Number of heads
'''
super(MobileViTv2Attention, self).__init__()
self.fc_i = nn.Linear(d_model,1)
self.fc_k = nn.Linear(d_model, d_model)
self.fc_v = nn.Linear(d_model, d_model)
self.fc_o = nn.Linear(d_model, d_model)
self.d_model = d_model
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
init.kaiming_normal_(m.weight, mode='fan_out')
if m.bias is not None:
init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant_(m.weight, 1)
init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
init.normal_(m.weight, std=0.001)
if m.bias is not None:
init.constant_(m.bias, 0)
def forward(self, input):
'''
Computes
:param queries: Queries (b_s, nq, d_model)
:return:
'''
i = self.fc_i(input) #(bs,nq,1)
weight_i = torch.softmax(i, dim=1) #bs,nq,1
context_score = weight_i * self.fc_k(input) #bs,nq,d_model
context_vector = torch.sum(context_score,dim=1,keepdim=True) #bs,1,d_model
v = self.fc_v(input) * context_vector #bs,nq,d_model
out = self.fc_o(v) #bs,nq,d_model
return out
if __name__ == '__main__':
input=torch.randn(50,49,512)
block = MobileViTv2Attention(d_model=512)
output=block(input)
print(output.size())