-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_base_class_util.py
157 lines (128 loc) · 5.35 KB
/
_base_class_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
import pickle as pkl
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import struct
import sys
from torchvision.transforms import Normalize
from general_util import *
class Classifier(nn.Module):
def __init__(self):
super(Classifier, self).__init__()
self.num_actors = 3 # Actually output size, but don't want to rename for compatability reasons
self.hidden_size = 512
self.input_size = 512
self.max_size = 60
with open("data/pca_%i_model.pkl"%(self.input_size),"rb") as _in:
self.pca = pkl.load(_in)
sample = get_vectors_by_video(0,0)
self.norm = Normalize([sample[0].mean()],[sample[0].std()])
self.linear1 = nn.Linear(self.max_size*self.input_size,self.hidden_size)
self.linear2 = nn.Linear(self.hidden_size,self.num_actors) # -1 if no ego
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 = torch.zeros(1,self.max_size,self.input_size)
_in = self.pca.transform(_input) # Reduce dimensionality
x[0][0:_in.shape[0]] = torch.tensor(_in)
x = self.norm(x).detach()
x = torch.flatten(x,1,2)
x = F.relu(self.linear1(x))
x = torch.sigmoid(self.linear2(x))
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.num_actors = 3 # Actually output size, but don't want to rename for compatability reasons
self.hidden_size = 512
self.input_size = 512
self.max_size = 60
with open("data/pca_%i_model.pkl"%(self.input_size),"rb") as _in:
self.pca = pkl.load(_in)
sample = get_vectors_by_video(0,0)
self.norm = Normalize([sample[0].mean()],[sample[0].std()])
self.head1_linear1 = nn.Linear(self.max_size*self.input_size,self.hidden_size)
self.head1_linear2 = nn.Linear(self.hidden_size,self.num_actors) # -1 if no ego
self.head2_linear1 = nn.Linear(self.max_size*self.input_size,self.hidden_size)
self.head2_linear2 = nn.Linear(self.hidden_size,self.num_actors) # -1 if no ego
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):
x = torch.zeros(1,self.max_size,self.input_size)
_in = self.pca.transform(_input) # Reduce dimensionality
x[0][0:_in.shape[0]] = torch.tensor(_in)
x = self.norm(x).detach()
x = torch.flatten(x,1,2)
y1 = F.relu(self.head1_linear1(x))
y1 = torch.sigmoid(self.head1_linear2(y1))
y2 = F.relu(self.head2_linear1(x))
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.num_actors = 3 # Actually output size, but don't want to rename for compatability reasons
self.hidden_size = 512
self.input_size = 512
self.max_size = 60
with open("data/pca_%i_model.pkl"%(self.input_size),"rb") as _in:
self.pca = pkl.load(_in)
sample = get_vectors_by_video(0,0)
self.norm = Normalize([sample[0].mean()],[sample[0].std()])
self.head1_linear1 = nn.Linear(self.max_size*self.input_size,self.hidden_size)
self.head1_linear2 = nn.Linear(self.hidden_size,self.num_actors) # -1 if no ego
self.head2_linear1 = nn.Linear(self.max_size*self.input_size+1,self.hidden_size)
self.head2_linear2 = nn.Linear(self.hidden_size,self.num_actors) # -1 if no ego
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, primary_gt):
x = torch.zeros(1,self.max_size,self.input_size)
_in = self.pca.transform(_input) # Reduce dimensionality
x[0][0:_in.shape[0]] = torch.tensor(_in)
x = self.norm(x).detach()
x = torch.flatten(x,1,2)
y1 = F.relu(self.head1_linear1(x))
y1 = torch.sigmoid(self.head1_linear2(y1))
gt_input = torch.tensor([[primary_gt]])
y2 = F.relu(self.head2_linear1(torch.cat([x,gt_input],dim=1)))
y2 = torch.sigmoid(self.head2_linear2(y2))
return (y1,y2)
def predict(self, _input):
x = torch.zeros(1,self.max_size,self.input_size)
_in = self.pca.transform(_input) # Reduce dimensionality
x[0][0:_in.shape[0]] = torch.tensor(_in)
x = self.norm(x).detach()
x = torch.flatten(x,1,2)
p1 = F.relu(self.head1_linear1(x))
p1 = torch.sigmoid(self.head1_linear2(p1))
p1 = torch.argmax(p1)
gt_input = torch.tensor([[p1]])
p2 = F.relu(self.head2_linear1(torch.cat([x,gt_input],dim=1)))
p2 = torch.sigmoid(self.head2_linear2(p2))
p2 = torch.argmax(p2)
return (p1,p2)