-
Notifications
You must be signed in to change notification settings - Fork 28
apply AI model for HB tracking #1057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
reconstruction/ai/src/main/java/org/jlab/rec/ai/dcHBTrackState/HBTrackStateEstimator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package org.jlab.rec.ai.dcHBTrackState; | ||
|
|
||
| import ai.djl.MalformedModelException; | ||
| import ai.djl.ModelException; | ||
| import ai.djl.inference.Predictor; | ||
| import ai.djl.ndarray.*; | ||
| import ai.djl.ndarray.types.Shape; | ||
| import ai.djl.repository.zoo.*; | ||
| import ai.djl.training.util.ProgressBar; | ||
| import ai.djl.translate.*; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Paths; | ||
| import java.util.concurrent.*; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import org.jlab.clas.reco.ReconstructionEngine; | ||
| import org.jlab.io.base.*; | ||
| import org.jlab.utils.system.ClasUtilsFile; | ||
| import org.jlab.service.ai.PredictorPool; | ||
|
|
||
|
|
||
| public class HBTrackStateEstimator{ | ||
| // ---------------- Configuration ---------------- | ||
| private String modelFile; | ||
|
|
||
| ZooModel<float[][], float[]> model; | ||
| PredictorPool predictors; | ||
|
|
||
| // ---------------- Statistics for normalization of inputs and outputs of training samples ---------------- | ||
| //// Note: Statistics of hits and track states depends on training samples, so need to be renewed when training samples change!!! | ||
| // Statistics of hits: doca, xm, xr, yr, z | ||
| private float[] HIT_MEAN; | ||
| private float[] HIT_STD; | ||
|
|
||
| // Statistics of track state: x, y, tx, ty, Q at z = 229 cm in the tilted sector frame | ||
| private float[] STATE_MEAN; | ||
| private float[] STATE_STD; | ||
|
|
||
| public HBTrackStateEstimator(String modelFile){ | ||
| this.modelFile = modelFile; | ||
|
|
||
| if(modelFile.contains("inbending")){ | ||
| HIT_MEAN = new float[]{0.52949071f, -45.771999f, -45.744694f, 57.336819f, 373.046356f}; | ||
| HIT_STD = new float[]{0.40272677f, 47.928203f, 48.379021f, 32.645191f, 111.54994f}; | ||
| STATE_MEAN = new float[]{-33.564308f, 0.010787425f, -0.15567796f, 0.0017755219f, 0.317530721f}; | ||
| STATE_STD = new float[]{28.667490f, 17.761129f, 0.11940812f, 0.074460238f, 0.74185127f}; | ||
| } | ||
| else if(modelFile.contains("outbending")){ | ||
| HIT_MEAN = new float[]{0.53385729f, -59.236504f, -59.200584f, 50.136387f, 372.057922f}; | ||
| HIT_STD = new float[]{0.40085429f, 51.385536f, 51.840462f, 31.498201f, 111.50029f}; | ||
| STATE_MEAN = new float[]{-39.446106f, 0.17583229f, -0.18047817f, 0.0014163271f, -0.082320645f}; | ||
| STATE_STD = new float[]{33.733425f, 17.226780f, 0.14071095f, 0.072449364f, 0.72273886f}; | ||
| } | ||
| else{ | ||
| Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Name of model file does not include inbending or outbending"); | ||
| } | ||
|
|
||
| System.setProperty("ai.djl.pytorch.num_interop_threads", "1"); | ||
| System.setProperty("ai.djl.pytorch.num_threads", "1"); | ||
| System.setProperty("ai.djl.pytorch.graph_optimizer", "false"); | ||
| try { | ||
| String modelPath = ClasUtilsFile.getResourceDir("CLAS12DIR", "etc/data/nnet/hbTSE/" + modelFile); | ||
|
|
||
| Criteria<float[][], float[]> criteria = Criteria.builder() | ||
| .setTypes(float[][].class, float[].class) | ||
| .optModelPath(Paths.get(modelPath)) | ||
| .optEngine("PyTorch") | ||
| .optTranslator(getTranslator()) | ||
| .optProgress(new ProgressBar()) | ||
| .build(); | ||
|
|
||
| model = criteria.loadModel(); | ||
|
|
||
| int threads = 64; | ||
| predictors = new PredictorPool(threads, model); | ||
|
|
||
|
|
||
| } catch (IOException | ModelException e) { | ||
| Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e); | ||
| } | ||
| } | ||
|
|
||
| // ---------------- Translator ---------------- | ||
| private Translator<float[][], float[]> getTranslator() { | ||
| return new Translator<float[][], float[]>() { | ||
|
|
||
| @Override | ||
| public NDList processInput(TranslatorContext ctx, float[][] hits) { | ||
| NDManager manager = ctx.getNDManager(); | ||
| int n = hits.length; | ||
|
|
||
| float[][] norm = new float[n][5]; | ||
| for (int i = 0; i < n; i++) | ||
| for (int j = 0; j < 5; j++) | ||
| norm[i][j] = (hits[i][j] - HIT_MEAN[j]) / HIT_STD[j]; | ||
|
|
||
| NDArray x = manager.create(norm); | ||
| x = x.reshape(1, n, 5); | ||
| return new NDList(x); | ||
| } | ||
|
|
||
| @Override | ||
| public float[] processOutput(TranslatorContext ctx, NDList list) { | ||
| NDArray out = list.get(0); // [1,5] | ||
| float[] y = out.toFloatArray(); | ||
|
|
||
| for (int i = 0; i < 5; i++) | ||
| y[i] = y[i] * STATE_STD[i] + STATE_MEAN[i]; | ||
|
|
||
| return y; | ||
| } | ||
|
|
||
| @Override | ||
| public Batchifier getBatchifier() { | ||
| return null; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
|
|
||
| public float[] predict(float[][] hits) { | ||
| if (hits == null) return null; | ||
|
|
||
| if (hits.length == 0) { | ||
| throw new IllegalArgumentException("HBInitialStateEstimator: empty hits"); | ||
| } | ||
|
|
||
| for (int i = 0; i < hits.length; i++) { | ||
| if (hits[i].length != 5) { | ||
| throw new IllegalArgumentException( | ||
| "Expect 5 features per hit, got " + hits[i].length | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| Predictor<float[][], float[]> predictor = predictors.take(); | ||
| try { | ||
| return predictor.predict(hits); | ||
| } finally { | ||
| predictors.put(predictor); | ||
| } | ||
| } catch (TranslateException | InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.