forked from zhoubolei/TRN-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TRNmodule.py
167 lines (145 loc) · 7.51 KB
/
TRNmodule.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# the relation consensus module by Bolei
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
import pdb
class RelationModule(torch.nn.Module):
# this is the naive implementation of the n-frame relation module, as num_frames == num_frames_relation
def __init__(self, img_feature_dim, num_frames, num_class):
super(RelationModule, self).__init__()
self.num_frames = num_frames
self.num_class = num_class
self.img_feature_dim = img_feature_dim
self.classifier = self.fc_fusion()
def fc_fusion(self):
# naive concatenate
num_bottleneck = 512
classifier = nn.Sequential(
nn.ReLU(),
nn.Linear(self.num_frames * self.img_feature_dim, num_bottleneck),
nn.ReLU(),
nn.Linear(num_bottleneck,self.num_class),
)
return classifier
def forward(self, input):
input = input.view(input.size(0), self.num_frames*self.img_feature_dim)
input = self.classifier(input)
return input
class RelationModuleMultiScale(torch.nn.Module):
# Temporal Relation module in multiply scale, suming over [2-frame relation, 3-frame relation, ..., n-frame relation]
def __init__(self, img_feature_dim, num_frames, num_class):
super(RelationModuleMultiScale, self).__init__()
self.subsample_num = 3 # how many relations selected to sum up
self.img_feature_dim = img_feature_dim
self.scales = [i for i in range(num_frames, 1, -1)] # generate the multiple frame relations
self.relations_scales = []
self.subsample_scales = []
for scale in self.scales:
relations_scale = self.return_relationset(num_frames, scale)
self.relations_scales.append(relations_scale)
self.subsample_scales.append(min(self.subsample_num, len(relations_scale))) # how many samples of relation to select in each forward pass
self.num_class = num_class
self.num_frames = num_frames
num_bottleneck = 256
self.fc_fusion_scales = nn.ModuleList() # high-tech modulelist
for i in range(len(self.scales)):
scale = self.scales[i]
fc_fusion = nn.Sequential(
nn.ReLU(),
nn.Linear(scale * self.img_feature_dim, num_bottleneck),
nn.ReLU(),
nn.Linear(num_bottleneck, self.num_class),
)
self.fc_fusion_scales += [fc_fusion]
print('Multi-Scale Temporal Relation Network Module in use', ['%d-frame relation' % i for i in self.scales])
def forward(self, input):
# the first one is the largest scale
act_all = input[:, self.relations_scales[0][0] , :]
act_all = act_all.view(act_all.size(0), self.scales[0] * self.img_feature_dim)
act_all = self.fc_fusion_scales[0](act_all)
for scaleID in range(1, len(self.scales)):
# iterate over the scales
idx_relations_randomsample = np.random.choice(len(self.relations_scales[scaleID]), self.subsample_scales[scaleID], replace=False)
for idx in idx_relations_randomsample:
act_relation = input[:, self.relations_scales[scaleID][idx], :]
act_relation = act_relation.view(act_relation.size(0), self.scales[scaleID] * self.img_feature_dim)
act_relation = self.fc_fusion_scales[scaleID](act_relation)
act_all += act_relation
return act_all
def return_relationset(self, num_frames, num_frames_relation):
import itertools
return list(itertools.combinations([i for i in range(num_frames)], num_frames_relation))
class RelationModuleMultiScaleWithClassifier(torch.nn.Module):
# relation module in multi-scale with a classifier at the end
def __init__(self, img_feature_dim, num_frames, num_class):
super(RelationModuleMultiScaleWithClassifier, self).__init__()
self.subsample_num = 3 # how many relations selected to sum up
self.img_feature_dim = img_feature_dim
self.scales = [i for i in range(num_frames, 1, -1)] #
self.relations_scales = []
self.subsample_scales = []
for scale in self.scales:
relations_scale = self.return_relationset(num_frames, scale)
self.relations_scales.append(relations_scale)
self.subsample_scales.append(min(self.subsample_num, len(relations_scale))) # how many samples of relation to select in each forward pass
self.num_class = num_class
self.num_frames = num_frames
num_bottleneck = 256
self.fc_fusion_scales = nn.ModuleList() # high-tech modulelist
self.classifier_scales = nn.ModuleList()
for i in range(len(self.scales)):
scale = self.scales[i]
fc_fusion = nn.Sequential(
nn.ReLU(),
nn.Linear(scale * self.img_feature_dim, num_bottleneck),
nn.ReLU(),
nn.Dropout(p=0.6),# this is the newly added thing
nn.Linear(num_bottleneck, num_bottleneck),
nn.ReLU(),
nn.Dropout(p=0.6),
)
classifier = nn.Linear(num_bottleneck, self.num_class)
self.fc_fusion_scales += [fc_fusion]
self.classifier_scales += [classifier]
# maybe we put another fc layer after the summed up results???
print('Multi-Scale Temporal Relation with classifier in use')
print(['%d-frame relation' % i for i in self.scales])
def forward(self, input):
# the first one is the largest scale
act_all = input[:, self.relations_scales[0][0] , :]
act_all = act_all.view(act_all.size(0), self.scales[0] * self.img_feature_dim)
act_all = self.fc_fusion_scales[0](act_all)
act_all = self.classifier_scales[0](act_all)
for scaleID in range(1, len(self.scales)):
# iterate over the scales
idx_relations_randomsample = np.random.choice(len(self.relations_scales[scaleID]), self.subsample_scales[scaleID], replace=False)
for idx in idx_relations_randomsample:
act_relation = input[:, self.relations_scales[scaleID][idx], :]
act_relation = act_relation.view(act_relation.size(0), self.scales[scaleID] * self.img_feature_dim)
act_relation = self.fc_fusion_scales[scaleID](act_relation)
act_relation = self.classifier_scales[scaleID](act_relation)
act_all += act_relation
return act_all
def return_relationset(self, num_frames, num_frames_relation):
import itertools
return list(itertools.combinations([i for i in range(num_frames)], num_frames_relation))
def return_TRN(relation_type, img_feature_dim, num_frames, num_class):
if relation_type == 'TRN':
TRNmodel = RelationModule(img_feature_dim, num_frames, num_class)
elif relation_type == 'TRNmultiscale':
TRNmodel = RelationModuleMultiScale(img_feature_dim, num_frames, num_class)
else:
raise ValueError('Unknown TRN' + relation_type)
return TRNmodel
if __name__ == "__main__":
batch_size = 10
num_frames = 5
num_class = 174
img_feature_dim = 512
input_var = Variable(torch.randn(batch_size, num_frames, img_feature_dim))
model = RelationModuleMultiScale(img_feature_dim, num_frames, num_class)
output = model(input_var)
print(output)