-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_vivit_classifier_util.py
161 lines (126 loc) · 4.7 KB
/
_vivit_classifier_util.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from general_util import *
class ClassifierBig(nn.Module):
def __init__(self):
super(ClassifierBig, self).__init__()
self.input_size = 768
self.hidden_size = 64
self.num_classes = 3
self.linear1 = nn.Linear(self.input_size,self.hidden_size)
self.linear2 = nn.Linear(self.hidden_size,self.num_classes)
self.linear1.apply(self.init_weights)
self.linear2.apply(self.init_weights)
def init_weights(self,m):
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform(m.weight)
if m.bias != None: m.bias.data.fill_(0.01)
def forward(self, _input):
x = _input[:, 0]
x = torch.relu(self.linear1(x))
x = torch.sigmoid(self.linear2(x),dim=1)
return x
def predict(self, _input):
_out = self.forward(_input)
return (_out>0.5).float()
class ClassifierSmall(nn.Module):
def __init__(self):
super(ClassifierSmall, self).__init__()
self.input_size = 768
self.hidden_size = 64
self.num_classes = 3
self.linear1 = nn.Linear(self.input_size,self.hidden_size)
self.linear2 = nn.Linear(self.hidden_size,self.num_classes)
def forward(self, _input):
x = F.relu(self.linear1(_input))
y = torch.sigmoid(self.linear2(x))
x = self.linear2(x)
x = torch.sigmoid(x)
return x
def predict(self, _input):
_out = self.forward(_input)
return (_out>0.5).float()
class ClassifierSmall_EgoOnly(nn.Module):
def __init__(self):
super(ClassifierSmall_EgoOnly, self).__init__()
self.input_size = 768
self.hidden_size = 64
self.num_classes = 3
self.linear1 = nn.Linear(self.input_size,self.hidden_size)
self.linear2 = nn.Linear(self.hidden_size,self.num_classes)
self.linear1.apply(self.init_weights)
self.linear2.apply(self.init_weights)
def init_weights(self,m):
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform(m.weight)
if m.bias != None: m.bias.data.fill_(0.01)
def forward(self, _input):
x = F.relu(self.linear1(_input))
x = F.softmax(self.linear2(x),dim=1)
return x
def predict(self, _input):
_out = self.forward(_input)
return torch.argmax(_out)
class Classifier2Head(nn.Module):
def __init__(self):
super(Classifier2Head, self).__init__()
self.input_size = 768
self.hidden_size = 64
self.num_classes = 3
self.head1_linear1 = nn.Linear(self.input_size,self.hidden_size)
self.head1_linear2 = nn.Linear(self.hidden_size,self.num_classes)
self.head2_linear1 = nn.Linear(self.input_size,self.hidden_size)
self.head2_linear2 = nn.Linear(self.hidden_size,self.num_classes)
self.head1_linear1.apply(self.init_weights)
self.head1_linear2.apply(self.init_weights)
self.head2_linear1.apply(self.init_weights)
self.head2_linear2.apply(self.init_weights)
def init_weights(self,m):
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform(m.weight)
if m.bias != None: m.bias.data.fill_(0.01)
def forward(self, _input):
y1 = F.relu(self.head1_linear1(_input))
y1 = torch.sigmoid(self.head1_linear2(y1))
y2 = F.relu(self.head2_linear1(_input))
y2 = torch.sigmoid(self.head2_linear2(y2))
return (y1,y2)
def predict(self, _input):
p1,p2 = self.forward(_input)
p1 = torch.argmax(p1)
p2 = torch.argmax(p2)
return (p1,p2)
class Classifier2HeadPlus(nn.Module):
def __init__(self):
super(Classifier2HeadPlus, self).__init__()
self.input_size = 768
self.hidden_size = 64
self.num_classes = 3
self.head1_linear1 = nn.Linear(self.input_size,self.hidden_size)
self.head1_linear2 = nn.Linear(self.hidden_size,self.num_classes)
self.head2_linear1 = nn.Linear(self.input_size+1,self.hidden_size)
self.head2_linear2 = nn.Linear(self.hidden_size,self.num_classes)
self.head1_linear1.apply(self.init_weights)
self.head1_linear2.apply(self.init_weights)
self.head2_linear1.apply(self.init_weights)
self.head2_linear2.apply(self.init_weights)
def init_weights(self,m):
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform(m.weight)
if m.bias != None: m.bias.data.fill_(0.01)
def forward(self, _input, gt_input):
y1 = F.relu(self.head1_linear1(_input))
y1 = torch.sigmoid(self.head1_linear2(y1))
y2 = F.relu(self.head2_linear1(torch.cat([_input,gt_input],dim=1)))
y2 = torch.sigmoid(self.head2_linear2(y2))
return (y1,y2)
def predict(self, _input):
p1 = F.relu(self.head1_linear1(_input))
p1 = torch.sigmoid(self.head1_linear2(p1))
p1 = torch.argmax(p1)
gt_input = torch.tensor([[p1]])
p2 = F.relu(self.head2_linear1(torch.cat([_input,gt_input],dim=1)))
p2 = torch.sigmoid(self.head2_linear2(p2))
p2 = torch.argmax(p2)
return (p1,p2)