-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_facs.py
186 lines (146 loc) · 6.24 KB
/
test_facs.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import sys
import os
import random
from os import path
from os.path import isfile, isdir, join
import copy
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
from data_loader_facs import FaceFeatureDataset
from network_facs import Net
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import operator
from scipy.spatial import distance
batch_size = 1
# Should always be 1 for reward ranking
target_idx = 1
subject_under_test= sys.argv[1]
data_fold_idx = 0
model_dir = './MLP_facs_reward_models/'
all_models = [join(model_dir, f) for f in os.listdir(model_dir) if isfile(join(model_dir, f))]
best_model = None
lowest_loss = 100.0
# Find the best model
for model_path in all_models:
model_info = model_path.replace('.pkl', '')
model_info = model_info.split('/')[-1].split('_')
if not model_info[0] == 'allsubjects': continue
loss = float(model_info[-1])
if loss < lowest_loss:
lowest_loss = loss
best_model = model_path
if best_model is None:
print('No model found for', subject_under_test, 'in', model_dir)
sys.exit(0)
# Hyper Parameters
num_epochs = 60
batch_size = 1
h_sizes = [128,128,64,8]
embed_size = 64
dropout = 0.63
threshold_percentile = 0
frames_pre=0
frames_after=12
window_size = frames_pre + frames_after + 1
num_classes1 = 3
num_classes2 = window_size * 10
celoss = nn.CrossEntropyLoss()
test_dist = (0.8,0.1,0.1)
test_dataset = FaceFeatureDataset(subject_under_test=subject_under_test, target_idx=target_idx , test=False, data_dir='detected/', evaluating=False, use_holdout=True, threshold_percentile=threshold_percentile, balance_data=False, frames_pre=frames_pre, frames_after=frames_after, data_fold_idx=data_fold_idx)
# Data Loader (Input Pipeline)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
net = Net(input_size=test_dataset.feature_size, embed_size=embed_size, h_sizes=h_sizes, dropout=dropout, num_classes1=num_classes1, num_classes2=num_classes2, window_size=window_size, include_lmk=False)
# Generate all possible reward rankings
classes = [-5,-1,6,0]
all_matches = {}
for i in range(len(classes)-1):
for j in range(len(classes)-1):
if i == j: continue
for k in range(len(classes)-1):
if k == i or k == j: continue
all_matches[(i,j,k)] = []
testcase_losses = []
testcase_matches = copy.deepcopy(all_matches)
print('loading model:',best_model)
state_dict = torch.load(best_model)
net.load_state_dict(state_dict)
for param in net.parameters():
net.requires_grad = False
net.eval()
net.cuda()
total = 0
total_loss = 0
output_fh = open('evaluation_outputs/'+subject_under_test+'.csv','w')
# Testing the trained model on holdout set, and record the reward class probability for each pickup event.
for i, (img_features, labels, _, event_mask, _, _, _) in enumerate(test_loader):
img_features = Variable(img_features.float()).cuda()
event_mask = Variable(event_mask.float()).cuda()
labels = Variable(labels).cuda()
outputs, _ = net(img_features.float(), event_mask)
loss = celoss(outputs, labels.long())
testcase_losses.append(loss.item())
outputs = outputs.cpu().data.float()
pos_p = (torch.exp(outputs)/torch.sum(torch.exp(outputs))).cpu().data[0][2]
pos_p = pos_p.data.item()
total_loss += loss.data.item()
labels = labels.float().view(-1)
_, predicted = torch.max(torch.Tensor(outputs), 1)
predictions = predicted.cpu().view(-1)
total += labels.size(0)
print(i, outputs)
output_fh.write(str(i)+','+','.join([str(num.data.item()) for num in outputs[0]])+'\n')
r_levels = test_dataset.reaction_levels[i*batch_size:(i+1)*batch_size]
for idx in range(len(labels)):
label = int(labels[idx].item())
if label != 3: # condition on pickup
output = outputs[idx]
for matching in testcase_matches:
testcase_matches[matching].append(output[matching[label]].item())
print('total loss: ', total_loss/len(test_dataset))
num_pickup_pivots = [25, 50, 75, 125]
total_score = [0.0]*(len(num_pickup_pivots)+1)
scores = []
print(subject_under_test ,' CE loss:', np.mean(testcase_losses))
score_idx = 0
pivots = copy.deepcopy(num_pickup_pivots)
pivots.append(len(testcase_matches[(0,1,2)]))
# Start incorporating mappings from all human reaction data in an episode
# The maximum a posteriori reward ranking is chosen.
# Score is computed based on how close the prediction is to the ground-truth ranking:
# 3: correct ranking for three rewards; 0: no reward is at correct place; 1: mistaking +6 and -5 reward; 2: otherwise.
# You may convert them to Kendall's Tau values for more intuitive evaluation.
for num_pickups in pivots:
if num_pickups == num_pickup_pivots[0]:
for matching in testcase_matches:
testcase_matches[matching] = np.array(testcase_matches[matching])
testcase_matches[matching] /= 100.0
logits = {}
matching_keys = list(all_matches.keys())
random.shuffle(matching_keys)
for matching in matching_keys:
logits[matching] = np.sum(testcase_matches[matching][0:num_pickups])
exp_sum = sum([np.exp(logit) for logit in logits.values()])
testcase_matches_probs = {}
matching_keys = list(testcase_matches.keys())
random.shuffle(matching_keys)
for matching in matching_keys:
testcase_matches_probs[matching] = np.exp(logits[matching])/exp_sum
matching = max(testcase_matches_probs.items(), key=operator.itemgetter(1))[0]
score = 0
for i in range(len(matching)):
if i == matching[i]: score += 1
if score == 1 and (matching[0] == 1 and matching[1] == 0 or matching[1] == 2 and matching[2] == 1):
score += 1
total_score[score_idx] += score
score_idx += 1
print('[num pred frames ',num_pickups, '] Best guess:', matching, ' Score:', score)
scores.append(score)
print('Final score,', ','.join([str(score) for score in scores]))