-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
114 lines (94 loc) · 3.8 KB
/
test.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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from segnet_conv_lstm_model import SegnetConvLSTM
from utils import config
from utils.data_utils import TUSimpleDataset
from utils.train_utils import AverageMeter, ProgressMeter
from utils.cuda_device import device
from utils.config import Configs
import utils.train_utils as tu
import numpy as np
import cv2
import argparse
import time
"""
This file is used to assess model results on
test set on measure like accuracy, precision,
recall, f1 score and inference time.
"""
def validate(val_loader, model, criterion, log_every=1):
batch_time = AverageMeter('Time', ':6.3f')
losses = AverageMeter('Loss', ':.4e')
acc = AverageMeter('Acc', ':6.4f')
f1 = AverageMeter('F1', ':6.4f')
prec = AverageMeter('Prec', ':6.4f')
rec = AverageMeter('Recall', ':6.4f')
progress = ProgressMeter(
len(val_loader),
[batch_time, losses, acc, f1, prec, rec],
prefix='Test: ')
# model.eval() evaluate mode highly decreases performance
model.train()
correct = 0
error = 0
precision = 0.
recall = 0.
with torch.no_grad():
end = time.time()
for batch_no, (samples, targets) in enumerate(val_loader):
# move data to gpu (or cpu if device is unavailable)
samples = [t.to(device) for t in samples]
targets = targets.squeeze(1).long().to(device)
# compute output
output = model(samples)
# compute loss
loss = criterion(output, targets)
losses.update(loss.item(), targets.size(0))
# compute f1 score
f, (p, r) = f1_score(output, targets.float())
f1.update(f)
prec.update(p)
rec.update(r)
# compute accuracy
acc.update(pixel_accuracy(output, targets), targets.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if batch_no % log_every == 0:
progress.display(batch_no)
return acc.avg
def pixel_accuracy(prediction:torch.Tensor, target:torch.Tensor):
"""
Computes simple pixel-wise accuracy measure
between target lane and prediction map; this
measure has little meaning (if not backed up
by other metrics) in tasks like this where
there's a huge unbalance between 2 classes
(background and lanes pixels).
"""
# get prediction positive channel (lanes)
out = (prediction[:, 1, :, :] > 0.).float()
return (out == target).float().mean().item()
def f1_score(output, target, epsilon=1e-7):
# turn output into 0-1 map
probas = (output[:, 1, :, :] > 0.).float()
TP = (probas * target).sum(dim=1)
precision = TP / (probas.sum(dim=1) + epsilon)
recall = TP / (target.sum(dim=1) + epsilon)
f1 = 2 * precision * recall / (precision + recall + epsilon)
f1 = f1.clamp(min=epsilon, max=1-epsilon)
return f1.mean().item(), (precision.mean().item(), recall.mean().item())
cc = Configs()
print("Loading stored model")
model = SegnetConvLSTM(cc.hidden_dims, decoder_out_channels=2, lstm_nlayers=len(cc.hidden_dims),
vgg_decoder_config=cc.decoder_config)
tu.load_model_checkpoint(model, '../train-results/model-fixed.torch', inference=False, map_location=device)
model.to(device)
print("Model loaded")
tu_test_dataset = TUSimpleDataset(config.ts_root, config.ts_subdirs, config.ts_flabels, shuffle=False)#, shuffle_seed=9)
# build data loader
tu_test_dataloader = DataLoader(tu_test_dataset, batch_size=cc.test_batch, shuffle=True, num_workers=2)
# using crossentropy for weighted loss
criterion = nn.CrossEntropyLoss(weight=torch.FloatTensor([0.02, 1.02])).to(device)
validate(tu_test_dataloader, model, criterion, log_every=1)