forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgnn.py
More file actions
executable file
·206 lines (176 loc) · 5.39 KB
/
gnn.py
File metadata and controls
executable file
·206 lines (176 loc) · 5.39 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
from pathlib import Path
import os
import sys
import acts
import acts.examples
import acts.examples.gnn
import acts.gnn
from acts.examples.simulation import addDigiParticleSelection, ParticleSelectorConfig
from acts.examples.reconstruction import addGnn, addSpacePointsMaking
from acts.gnn import (
TorchMetricLearning,
TorchEdgeClassifier,
BoostTrackBuilding,
Device,
)
from acts.examples.gnn import NodeFeature
from acts import UnitConstants as u
from digitization import runDigitization
def runGnnMetricLearning(
trackingGeometry,
field,
outputDir,
digiConfigFile,
geometrySelection,
embedModelPath,
filterModelPath,
gnnModelPath,
device=None,
outputRoot=False,
outputCsv=False,
s=None,
):
s = runDigitization(
trackingGeometry,
field,
outputDir,
digiConfigFile=digiConfigFile,
particlesInput=None,
outputRoot=outputRoot,
outputCsv=outputCsv,
s=s,
)
addDigiParticleSelection(
s,
ParticleSelectorConfig(
pt=(1.0 * u.GeV, None),
eta=(-3.0, 3.0),
measurements=(7, None),
removeNeutral=True,
),
)
addSpacePointsMaking(
s,
geoSelectionConfigFile=geometrySelection,
stripGeoSelectionConfigFile=None,
trackingGeometry=trackingGeometry,
logLevel=acts.logging.INFO,
)
graphConstructorConfig = {
"level": acts.logging.INFO,
"modelPath": str(embedModelPath),
"embeddingDim": 8,
"rVal": 1.6,
"knnVal": 100,
"selectedFeatures": [0, 1, 2], # R, Phi, Z
"device": device,
}
graphConstructor = TorchMetricLearning(**graphConstructorConfig)
filterConfig = {
"level": acts.logging.INFO,
"modelPath": str(filterModelPath),
"cut": 0.01,
}
gnnConfig = {
"level": acts.logging.INFO,
"modelPath": str(gnnModelPath),
"cut": 0.5,
}
edgeClassifiers = []
if filterModelPath.suffix == ".pt":
edgeClassifiers.append(
TorchEdgeClassifier(
**filterConfig,
nChunks=5,
undirected=False,
selectedFeatures=[0, 1, 2],
device=device,
)
)
elif filterModelPath.suffix == ".onnx":
from acts.gnn import OnnxEdgeClassifier
filterConfig["device"] = device
edgeClassifiers.append(OnnxEdgeClassifier(**filterConfig))
else:
raise ValueError(f"Unsupported model format: {filterModelPath.suffix}")
if gnnModelPath.suffix == ".pt":
edgeClassifiers.append(
TorchEdgeClassifier(
**gnnConfig,
undirected=True,
selectedFeatures=[0, 1, 2],
device=device,
)
)
elif gnnModelPath.suffix == ".onnx":
from acts.gnn import OnnxEdgeClassifier
gnnConfig["device"] = device
edgeClassifiers.append(
OnnxEdgeClassifier(**gnnConfig),
)
else:
raise ValueError(f"Unsupported model format: {filterModelPath.suffix}")
# Stage 3: CPU track building
trackBuilderConfig = {
"level": acts.logging.INFO,
}
trackBuilder = BoostTrackBuilding(**trackBuilderConfig)
# Node features: Standard 3 features (R, Phi, Z)
nodeFeatures = [
NodeFeature.R,
NodeFeature.Phi,
NodeFeature.Z,
]
featureScales = [1.0, 1.0, 1.0]
# Add GNN tracking
addGnn(
s,
graphConstructor=graphConstructor,
edgeClassifiers=edgeClassifiers,
trackBuilder=trackBuilder,
nodeFeatures=nodeFeatures,
featureScales=featureScales,
outputDirRoot=outputDir if outputRoot else None,
device=device,
logLevel=acts.logging.INFO,
)
s.run()
if "__main__" == __name__:
detector = acts.examples.GenericDetector()
trackingGeometry = detector.trackingGeometry()
field = acts.ConstantBField(acts.Vector3(0, 0, 2 * u.T))
srcdir = Path(__file__).resolve().parent.parent.parent.parent
geometrySelection = srcdir / "Examples/Configs/generic-seeding-config.json"
assert geometrySelection.exists()
digiConfigFile = srcdir / "Examples/Configs/generic-digi-smearing-config.json"
assert digiConfigFile.exists()
# Model paths from MODEL_STORAGE environment variable
model_storage = os.environ.get("MODEL_STORAGE")
assert model_storage is not None, "MODEL_STORAGE environment variable is not set"
ci_models = Path(model_storage)
# These models are chosen as they work without the torch-scatter dependency
embedModelPath = ci_models / "torchscript_models/embed.pt"
filterModelPath = ci_models / "torchscript_models/filter.pt"
gnnModelPath = ci_models / "onnx_models/gnn.onnx"
device = (
Device.Cpu() if os.environ.get("CUDA_VISIBLE_DEVICES") == "" else Device.Cuda()
)
s = acts.examples.Sequencer(events=2, numThreads=1)
s.config.logLevel = acts.logging.INFO
rnd = acts.examples.RandomNumbers()
outputDir = Path(os.getcwd())
runGnnMetricLearning(
trackingGeometry,
field,
outputDir,
digiConfigFile,
geometrySelection,
embedModelPath,
filterModelPath,
gnnModelPath,
device=device,
outputRoot=True,
outputCsv=False,
s=s,
)