-
Notifications
You must be signed in to change notification settings - Fork 7
/
mvaggregate.py
128 lines (91 loc) · 4.22 KB
/
mvaggregate.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from utils import batch_tensor, unbatch_tensor
import torch
from torch import nn
class WeightedAggregate(nn.Module):
def __init__(self, model, feat_dim, lifting_net=nn.Sequential()):
super().__init__()
self.model = model
self.lifting_net = lifting_net
num_heads = 8
self.feature_dim = feat_dim
r1 = -1
r2 = 1
self.attention_weights = nn.Parameter((r1 - r2) * torch.rand(feat_dim, feat_dim) + r2)
self.normReLu = nn.Sequential(
nn.LayerNorm(feat_dim),
nn.ReLU()
)
self.relu = nn.ReLU()
def forward(self, mvimages):
B, V, C, D, H, W = mvimages.shape # Batch, Views, Channel, Depth, Height, Width
aux = self.lifting_net(unbatch_tensor(self.model(batch_tensor(mvimages, dim=1, squeeze=True)), B, dim=1, unsqueeze=True))
##################### VIEW ATTENTION #####################
# S = source length
# N = batch size
# E = embedding dimension
# L = target length
aux = torch.matmul(aux, self.attention_weights)
# Dimension S, E for two views (2,512)
# Dimension N, S, E
aux_t = aux.permute(0, 2, 1)
prod = torch.bmm(aux, aux_t)
relu_res = self.relu(prod)
aux_sum = torch.sum(torch.reshape(relu_res, (B, V*V)).T, dim=0).unsqueeze(0)
final_attention_weights = torch.div(torch.reshape(relu_res, (B, V*V)).T, aux_sum.squeeze(0))
final_attention_weights = final_attention_weights.T
final_attention_weights = torch.reshape(final_attention_weights, (B, V, V))
final_attention_weights = torch.sum(final_attention_weights, 1)
output = torch.mul(aux.squeeze(), final_attention_weights.unsqueeze(-1))
output = torch.sum(output, 1)
return output.squeeze(), final_attention_weights
class ViewMaxAggregate(nn.Module):
def __init__(self, model, lifting_net=nn.Sequential()):
super().__init__()
self.model = model
self.lifting_net = lifting_net
def forward(self, mvimages):
B, V, C, D, H, W = mvimages.shape # Batch, Views, Channel, Depth, Height, Width
aux = self.lifting_net(unbatch_tensor(self.model(batch_tensor(mvimages, dim=1, squeeze=True)), B, dim=1, unsqueeze=True))
pooled_view = torch.max(aux, dim=1)[0]
return pooled_view.squeeze(), aux
class ViewAvgAggregate(nn.Module):
def __init__(self, model, lifting_net=nn.Sequential()):
super().__init__()
self.model = model
self.lifting_net = lifting_net
def forward(self, mvimages):
B, V, C, D, H, W = mvimages.shape # Batch, Views, Channel, Depth, Height, Width
aux = self.lifting_net(unbatch_tensor(self.model(batch_tensor(mvimages, dim=1, squeeze=True)), B, dim=1, unsqueeze=True))
pooled_view = torch.mean(aux, dim=1)
return pooled_view.squeeze(), aux
class MVAggregate(nn.Module):
def __init__(self, model, agr_type="max", feat_dim=400, lifting_net=nn.Sequential()):
super().__init__()
self.agr_type = agr_type
self.inter = nn.Sequential(
nn.LayerNorm(feat_dim),
nn.Linear(feat_dim, feat_dim),
nn.Linear(feat_dim, feat_dim),
)
self.fc_offence = nn.Sequential(
nn.LayerNorm(feat_dim),
nn.Linear(feat_dim, feat_dim),
nn.Linear(feat_dim, 4)
)
self.fc_action = nn.Sequential(
nn.LayerNorm(feat_dim),
nn.Linear(feat_dim, feat_dim),
nn.Linear(feat_dim, 8)
)
if self.agr_type == "max":
self.aggregation_model = ViewMaxAggregate(model=model, lifting_net=lifting_net)
elif self.agr_type == "mean":
self.aggregation_model = ViewAvgAggregate(model=model, lifting_net=lifting_net)
else:
self.aggregation_model = WeightedAggregate(model=model, feat_dim=feat_dim, lifting_net=lifting_net)
def forward(self, mvimages):
pooled_view, attention = self.aggregation_model(mvimages)
inter = self.inter(pooled_view)
pred_action = self.fc_action(inter)
pred_offence_severity = self.fc_offence(inter)
return pred_offence_severity, pred_action, attention