-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
244 lines (227 loc) · 8.27 KB
/
evaluate.py
File metadata and controls
244 lines (227 loc) · 8.27 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import argparse
import numpy as np
from torch.utils.data import DataLoader
import torch
from torchvision import transforms
from sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix
from tqdm import tqdm
from model.baseline.baseline_3d import generate_model
from model.baseline.deepsignals import DeepsignalsBaseline
from model.vif_create import VifConfig, VitBackboneConfig, create_vif
from model.lightning import LitVisualIntentionFormer
from utils.dataset import VisualIntentionsDictDataset, create_data_dict
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("test_annotations", type=str)
parser.add_argument("test_images_dir", type=str)
parser.add_argument("model_ckpt", type=str)
args = parser.parse_args()
test_annotations = args.test_annotations
test_images_dir = args.test_images_dir
model_ckpt = args.model_ckpt
mean = [0.27678646, 0.30232353, 0.34076891]
std = [0.13243662, 0.13241449, 0.14208872]
normalize = transforms.Normalize(
mean=mean,
std=std,
)
NUM_IND_CLASSES = 4
SEQUENCE_LEN = 10
MIN_IMAGE_WIDTH = 120
DIM = 1024
model_config = VifConfig(
sequence_len=SEQUENCE_LEN,
min_img_width=MIN_IMAGE_WIDTH,
backbone=VitBackboneConfig(
frozen=False,
dim=DIM,
),
dim=DIM,
depth=2,
heads=16,
mlp_dim=DIM * 4,
dim_head=64,
attn_dropout=0.75,
head_dropout=0.75,
transformer_dropput=0.0,
with_rear=True,
with_heading=True,
with_cls=True,
num_ind_classes=NUM_IND_CLASSES,
with_heading_feature=True,
)
print(model_config)
model = create_vif(config=model_config)
# model = generate_model(101, num_ind_classes=num_ind_classes)
# model = DeepsignalsBaseline(num_ind_classes=num_ind_classes)
model = LitVisualIntentionFormer(model, 0, 0, num_ind_classes=NUM_IND_CLASSES)
checkpoint = torch.load(model_ckpt)
model.load_state_dict(checkpoint["state_dict"])
model = model.vif
model.to("cuda:0")
model.eval()
test_data_dict = create_data_dict(
test_annotations,
test_images_dir,
sequence_len=SEQUENCE_LEN,
min_image_width=MIN_IMAGE_WIDTH,
max_occupancy=1.0,
only_intentions=True
)
test_dataset = VisualIntentionsDictDataset(
test_data_dict,
image_transform=transforms.Compose(
[
transforms.ToPILImage(),
transforms.Resize([224, 224]),
transforms.ToTensor(),
normalize,
]
),
with_context=True,
)
test_dataloader = DataLoader(
test_dataset,
batch_size=16,
shuffle=False,
num_workers=8,
)
_preds_indicator = []
_targets_indicator = []
_preds_rear = []
_targets_rear = []
_preds_heading = []
_targets_heading = []
_weather = []
_time = []
for images, headings, targets, weather, time in tqdm(test_dataloader):
images, headings, targets = (
images.to("cuda:0"),
headings.to("cuda:0"),
targets.to("cuda:0"),
)
with torch.no_grad():
pred_indicator, pred_rear, pred_heading = model(images, headings)
if NUM_IND_CLASSES == 3:
targets_indicator = targets[:, 1:4]
else:
targets_indicator = targets[:, :4]
targets_rear = targets[:, 4:7]
targets_heading = targets[:, 7:11]
_preds_indicator.extend(pred_indicator.softmax(1).argmax(1).cpu())
_targets_indicator.extend(targets_indicator.argmax(1).cpu())
_preds_rear.extend(pred_rear.softmax(1).argmax(1).cpu())
_targets_rear.extend(targets_rear.argmax(1).cpu())
_preds_heading.extend(pred_heading.softmax(1).argmax(1).cpu())
_targets_heading.extend(targets_heading.argmax(1).cpu())
_weather.extend(weather)
_time.extend(time)
#
# Total
#
f1_ind = f1_score(_targets_indicator, _preds_indicator, average="macro")
p_ind = precision_score(_targets_indicator, _preds_indicator, average="macro")
r_ind = recall_score(_targets_indicator, _preds_indicator, average="macro")
f1_rear = f1_score(_targets_rear, _preds_rear, average="macro")
p_rear = precision_score(_targets_rear, _preds_rear, average="macro")
r_rear = recall_score(_targets_rear, _preds_rear, average="macro")
f1_head = f1_score(_targets_heading, _preds_heading, average="macro")
p_head = precision_score(_targets_heading, _preds_heading, average="macro")
r_head = recall_score(_targets_heading, _preds_heading, average="macro")
print("Total results:")
print("-----------------------")
print("f1_ind & f1_rear & f1_rear")
print(f"{f1_ind} & {f1_rear} & {f1_head} \\")
cm_ind = confusion_matrix(_targets_indicator, _preds_indicator)
print(cm_ind)
cm_ind = confusion_matrix(_targets_indicator, _preds_indicator, normalize="true")
print(cm_ind)
#
# Heading
#
print("Heading based results:")
print("-----------------------")
_targets_indicator = np.array(_targets_indicator)
_targets_rear = np.array(_targets_rear)
_preds_indicator = np.array(_preds_indicator)
_preds_rear = np.array(_preds_rear)
_preds_heading = np.array(_preds_heading)
_targets_heading = np.array(_targets_heading)
idx_0 = _targets_heading == 0
idx_1 = _targets_heading == 1
idx_2 = _targets_heading == 2
idx_3 = _targets_heading == 3
f1_ind_0 = f1_score(
_targets_indicator[idx_0], _preds_indicator[idx_0], average="macro"
)
f1_ind_1 = f1_score(
_targets_indicator[idx_1], _preds_indicator[idx_1], average="macro"
)
f1_ind_2 = f1_score(
_targets_indicator[idx_2], _preds_indicator[idx_2], average="macro"
)
f1_ind_3 = f1_score(
_targets_indicator[idx_3], _preds_indicator[idx_3], average="macro"
)
print("Indicator")
print("back, right, front, left")
print(f"{f1_ind_0} & {f1_ind_1} & {f1_ind_2} & {f1_ind_3} \\")
f1_ind_0 = f1_score(_targets_rear[idx_0], _preds_rear[idx_0], average="macro")
f1_ind_1 = f1_score(_targets_rear[idx_1], _preds_rear[idx_1], average="macro")
f1_ind_2 = f1_score(_targets_rear[idx_2], _preds_rear[idx_2], average="macro")
f1_ind_3 = f1_score(_targets_rear[idx_3], _preds_rear[idx_3], average="macro")
print("Rear")
print("back, right, front, left")
print(f"{f1_ind_0} & {f1_ind_1} & {f1_ind_2} & {f1_ind_3} \\")
#
# Time of Day
#
print("Time of Day based results:")
print("-----------------------")
_time = np.array(_time)
idx_day = _time == "Day"
idx_night = _time == "Night"
idx_dawn_dusk = _time == "Dawn/Dusk"
f1_ind_day = f1_score(
_targets_indicator[idx_day], _preds_indicator[idx_day], average="macro"
)
f1_ind_night = f1_score(
_targets_indicator[idx_night], _preds_indicator[idx_night], average="macro"
)
f1_ind_dusk = f1_score(
_targets_indicator[idx_dawn_dusk],
_preds_indicator[idx_dawn_dusk],
average="macro",
)
f1_rear_day = f1_score(
_targets_rear[idx_day], _preds_rear[idx_day], average="macro"
)
f1_rear_night = f1_score(
_targets_rear[idx_night], _preds_rear[idx_night], average="macro"
)
f1_rear_dusk = f1_score(
_targets_rear[idx_dawn_dusk], _preds_rear[idx_dawn_dusk], average="macro"
)
f1_heading_day = f1_score(
_targets_heading[idx_day], _preds_heading[idx_day], average="macro"
)
f1_heading_night = f1_score(
_targets_heading[idx_night], _preds_heading[idx_night], average="macro"
)
f1_heading_dusk = f1_score(
_targets_heading[idx_dawn_dusk], _preds_heading[idx_dawn_dusk], average="macro"
)
print("Indicator")
print("Day, Night, Dusk/Dawn")
print(f"{f1_ind_day} & {f1_ind_night} & {f1_ind_dusk} \\")
print("Rear")
print("Day, Night, Dusk/Dawn")
print(f"{f1_rear_day} & {f1_rear_night} & {f1_rear_dusk} \\")
print("Heading")
print("Day, Night, Dusk/Dawn")
print(f"{f1_heading_day} & {f1_heading_night} & {f1_heading_dusk} \\")
#
# Weather
#
# Note: Not Implemented
# Unfortunaetly the Waymo Open Dataset val test split does only contains sunny weather