-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_predict.py
More file actions
74 lines (61 loc) · 2.69 KB
/
test_predict.py
File metadata and controls
74 lines (61 loc) · 2.69 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
# FitLayout - Python GNN Demo
# (c) 2026 Radek Burget <burgetr@fit.vut.cz>
# This script tests the trained model on a testing dataset.
import torch
from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
from torch.utils.data import random_split
from flclient.flclient import default_prefix_string, R, SEGM
from graph.creator import AreaGraphCreator
from graph.dataset import RemoteDataset, LocalDataset
from models import GCNC
from config import fl, tags, relations, params
# Create the graph creator
gc = AreaGraphCreator(fl, relations, tags)
# Load the dataset
dataset = LocalDataset('data/graphs')
print("# of classes: ", dataset.num_classes)
print("# of features: ", dataset.num_features)
print("# of node features: ", dataset.num_node_features)
print("# of edge features: ", dataset.num_edge_features)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = GCNC(dataset.num_node_features, dataset.num_classes).to(device)
# Load the trained model state
model.load_state_dict(torch.load("models/last.pt"))
print("Loaded model from models/last.pt")
# Split the dataset to get the test set
# Using the same split sizes and seed as in training to ensure consistency
torch.manual_seed(42) # Use a fixed seed for reproducibility of the split
train_size = int(0.7 * len(dataset))
val_size = int(0.15 * len(dataset))
test_size = len(dataset) - train_size - val_size
_, _, test_dataset = random_split(dataset, [train_size, val_size, test_size])
test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False)
print_errors = True
# Use the model to predict on the entire test dataset and print the accuracy
# and the differences between predicted and actual labels
err_count = 0
total_cnt = 0
total_err = 0
for data in test_dataloader:
data = data.to(device)
model.eval()
with torch.no_grad():
pred = model(data.x, data.edge_index).argmax(dim=1)
acc = torch.sum(pred == data.y).item() / len(data.y)
total_cnt += len(data.y)
total_err += torch.sum(torch.abs(pred - data.y)).item()
if acc < 1:
err_count += 1
if print_errors:
print(f"Incorrect predictions for artifact: {data.artifact_iri[0]}")
node_uris = data.node_uris[0]
for i in range(len(data.y)):
if pred[i] != data.y[i]:
print(f" Node URI: {node_uris[i]}, Predicted: {tags[pred[i]]}, Actual: {tags[data.y[i]]}")
print(f"Number of instances with incorrect predictions: {err_count}")
print(f"Total number of areas: {total_cnt}")
print(f"Incorrectly classified areas: {total_err}")
if total_cnt > 0:
print(f"Accuracy: {(total_cnt - total_err) / total_cnt}")
print(f"Error rate: {total_err / total_cnt}")