MCPcopy Create free account
hub / github.com/M-Nauta/ProtoTree / eval

Function eval

prototree/test.py:15–70  ·  view source on GitHub ↗
(tree: ProtoTree,
        test_loader: DataLoader,
        epoch,
        device,
        log: Log = None,  
        sampling_strategy: str = 'distributed',
        log_prefix: str = 'log_eval_epochs', 
        progress_prefix: str = 'Eval Epoch'
        )

Source from the content-addressed store, hash-verified

13
14@torch.no_grad()
15def eval(tree: ProtoTree,
16 test_loader: DataLoader,
17 epoch,
18 device,
19 log: Log = None,
20 sampling_strategy: str = 'distributed',
21 log_prefix: str = 'log_eval_epochs',
22 progress_prefix: str = 'Eval Epoch'
23 ) -> dict:
24 tree = tree.to(device)
25
26 # Keep an info dict about the procedure
27 info = dict()
28 if sampling_strategy != 'distributed':
29 info['out_leaf_ix'] = []
30 # Build a confusion matrix
31 cm = np.zeros((tree._num_classes, tree._num_classes), dtype=int)
32
33 # Make sure the model is in evaluation mode
34 tree.eval()
35
36 # Show progress on progress bar
37 test_iter = tqdm(enumerate(test_loader),
38 total=len(test_loader),
39 desc=progress_prefix+' %s'%epoch,
40 ncols=0)
41
42 # Iterate through the test set
43 for i, (xs, ys) in test_iter:
44 xs, ys = xs.to(device), ys.to(device)
45
46 # Use the model to classify this batch of input data
47 out, test_info = tree.forward(xs, sampling_strategy)
48 ys_pred = torch.argmax(out, dim=1)
49
50 # Update the confusion matrix
51 cm_batch = np.zeros((tree._num_classes, tree._num_classes), dtype=int)
52 for y_pred, y_true in zip(ys_pred, ys):
53 cm[y_true][y_pred] += 1
54 cm_batch[y_true][y_pred] += 1
55 acc = acc_from_cm(cm_batch)
56 test_iter.set_postfix_str(
57 f'Batch [{i + 1}/{len(test_iter)}], Acc: {acc:.3f}'
58 )
59
60 # keep list of leaf indices where test sample ends up when deterministic routing is used.
61 if sampling_strategy != 'distributed':
62 info['out_leaf_ix'] += test_info['out_leaf_ix']
63 del out
64 del ys_pred
65 del test_info
66
67 info['confusion_matrix'] = cm
68 info['test_accuracy'] = acc_from_cm(cm)
69 log.log_message("\nEpoch %s - Test accuracy with %s routing: "%(epoch, sampling_strategy)+str(info['test_accuracy']))
70 return info
71
72@torch.no_grad()

Callers 1

run_treeFunction · 0.90

Calls 3

acc_from_cmFunction · 0.85
log_messageMethod · 0.80
forwardMethod · 0.45

Tested by

no test coverage detected