Views
No views yet
1import torch
2from torch_geometric.data import HeteroData
3from huggingface_hub import hf_hub_download
4import importlib.util
5import json
6
7# Download files
8model_path = hf_hub_download(repo_id="rokati/heterogen_focal_without_kpis", filename="best_gnn_model.pth")
9architecture_path = hf_hub_download(repo_id="rokati/heterogen_focal_without_kpis", filename="model_architecture.py")
10config_path = hf_hub_download(repo_id="rokati/heterogen_focal_without_kpis", filename="config.json")
11
12# Load configuration
13with open(config_path, 'r') as f:
14 config = json.load(f)
15
16# Load architecture
17spec = importlib.util.spec_from_file_location("model_architecture", architecture_path)
18model_module = importlib.util.module_from_spec(spec)
19spec.loader.exec_module(model_module)
20
21# Create model instance
22model = model_module.XGNet(
23 num_players=config['num_players'],
24 hid=config['hidden_dim'],
25 p=config['dropout_rate'],
26 heads=config['num_heads'],
27 num_layers=config['num_layers'],
28 use_norm=config['use_norm'],
29 num_global_features=config['num_global_features']
30)
31
32# Load weights
33model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
34model.eval()
35
36# Prepare graph data
37graph = HeteroData()
38
39# Add nodes
40graph["shooter"].x = torch.tensor([[player_idx]], dtype=torch.long)
41graph["goal"].x = torch.zeros((1, 1))
42
43# Add edges with attributes
44graph["goal", "distance", "shooter"].edge_index = torch.tensor([[0], [0]], dtype=torch.long)
45graph["goal", "distance", "shooter"].edge_attr = torch.tensor([[distance_to_goal]], dtype=torch.float)
46
47graph["goal", "angle_to_goal", "shooter"].edge_index = torch.tensor([[0], [0]], dtype=torch.long)
48graph["goal", "angle_to_goal", "shooter"].edge_attr = torch.tensor([[angle_to_goal]], dtype=torch.float)
49
50graph["goal", "dist_to_gk", "shooter"].edge_index = torch.tensor([[0], [0]], dtype=torch.long)
51graph["goal", "dist_to_gk", "shooter"].edge_attr = torch.tensor([[dist_to_gk]], dtype=torch.float)
52
53graph["goal", "angle_to_gk", "shooter"].edge_index = torch.tensor([[0], [0]], dtype=torch.long)
54graph["goal", "angle_to_gk", "shooter"].edge_attr = torch.tensor([[angle_to_gk]], dtype=torch.float)
55
56# Add global features (18 features)
57graph.global_features = torch.tensor([global_feature_values], dtype=torch.float)
58
59# Make prediction
60with torch.no_grad():
61 logits = model(graph)
62 xg_prediction = torch.sigmoid(logits).item()