-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
49 lines (38 loc) · 1.86 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
import numpy as np
import torch
from torch.autograd import Variable
from convNet import loss_fn
class TestCnn():
def test(self, model, metrics, params):
test_data_loader = params.test_dataloader
# Set model to evaluation mode
model.eval()
# summary of current evaluation loop
summary = []
predictions = torch.tensor([])
labels = torch.tensor([])
for data_batch, labels_batch in test_data_loader:
# move to GPU if available
if params.cuda:
data_batch, labels_batch = data_batch.cuda(non_blocking=True), \
labels_batch.cuda(non_blocking=True)
# fetch the next evaluation batch
data_batch, labels_batch = Variable(data_batch), Variable(labels_batch)
# compute model output
output_batch = model(data_batch)
loss = loss_fn(output_batch, labels_batch)
_, y_pred_tags = torch.max(output_batch, dim=1)
predictions = torch.cat((predictions, y_pred_tags.cpu()), dim=0)
labels = torch.cat((labels, labels_batch.cpu()), dim=0)
# extract data from torch Variable, move to cpu, convert to numpy arrays
# output_batch = output_batch.data.cpu().numpy()
# labels_batch = labels_batch.data.cpu().numpy()
# compute all metrics on this batch
# summary_batch = {metric: metrics[metric](output_batch, labels_batch)
# for metric in metrics}
# summary_batch['loss'] = loss.item()
# summary.append(summary_batch)
# compute mean of all metrics in summary
# metrics_mean = {metric: np.mean([x[metric]
# for x in summary]) for metric in summary[0]}
return predictions.numpy(), labels.numpy()