BmnRoot
26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
BmnGemStripFdeepModel.h
Go to the documentation of this file.
1
#ifndef BMNGEMSTRIPFDEEPMODEL_H
2
#define BMNGEMSTRIPFDEEPMODEL_H
3
4
// Neural network inference for the GEM strip simulation with frugally-deep (fdeep).
5
//
6
// BmnGemStripFdeepModel - a Keras model converted to fdeep .json (convert_model.py of frugally-deep):
7
// 2D model (AddRealPointFull_ANNGenerator):
8
// [px, py, pz, drift, bfield, s0..s15] -> image 80 x 80
9
// strip model (AddRealPointFull_ANNStripGenerator_Fdeep, AddRealPointFull_ANNStripGenerator_Fdeep_Parallel):
10
// [px, py, pz, drift, bfield, s0..s63, strip_layer, strip_order, center_frac] -> N strips
11
// (window of odd size N centered at the middle of the drift gap)
12
// The model must have one input and one output (both are flattened here).
13
//
14
// frugally-deep is used only in BmnGemStripFdeepModel.cxx: this header does not need it.
15
// Without fdeep (fdeep/fdeep.hpp not found when building, or -DBMN_WITH_FDEEP=0) the models are never loaded:
16
// Load() returns false, GetShared() returns nullptr and the ANN methods use the standard method.
17
//
18
// Usage (the same as BmnGemStripMLPModel):
19
// auto model = BmnGemStripFdeepModel::GetShared(); // default path (strip model), shared between
20
// modules auto model = BmnGemStripFdeepModel::GetShared("my.json"); // explicit path
21
// BmnGemStripFdeepModel::SetDefaultPath("$VMCWORKDIR/parameters/gem/ANNModels/my_2d_model.json"); // e.g. the 2D
22
// model model->Predict(input, output); // input_dim floats -> output_dim floats
23
// std::vector<float> out = model->Predict(input_vector); // the same, empty vector if failed
24
//
25
// After loading the model is not modified: Predict() can be called from several threads at once.
26
27
#include <iostream>
28
#include <memory>
29
#include <string>
30
#include <vector>
31
32
class
BmnGemStripFdeepModel
33
{
34
35
public
:
36
// Constructors -------------------------------------------------------------
37
BmnGemStripFdeepModel
();
38
explicit
BmnGemStripFdeepModel
(
const
std::string& path,
bool
verbose =
true
);
// Load(path)
39
//--------------------------------------------------------------------------
40
41
// Destructor
42
virtual
~BmnGemStripFdeepModel
();
43
44
// Loading ------------------------------------------------------------------
45
// Reads the fdeep .json model (without the fdeep verification on the test data of the file - fast loading).
46
// Returns false (and the model stays empty) if anything is wrong; the reason is printed (LOG(error)) if verbose.
47
bool
Load
(
const
std::string& path,
bool
verbose =
true
);
48
void
Clear
();
49
bool
IsLoaded
()
const
{
return
fLoaded
; }
50
51
// true if the library is built with frugally-deep (otherwise no model can be loaded)
52
static
bool
IsBuiltWithFdeep
();
53
//--------------------------------------------------------------------------
54
55
// Model parameters ---------------------------------------------------------
56
int
GetInputDim
()
const
{
return
fInputDim
; }
// size of the (flattened) input
57
int
GetOutputDim
()
const
{
return
fOutputDim
; }
// size of the (flattened) output
58
const
std::string&
GetFilePath
()
const
{
return
fFilePath
; }
59
60
void
Print
(std::ostream& os = std::cout)
const
;
// short summary
61
//--------------------------------------------------------------------------
62
63
// Inference ----------------------------------------------------------------
64
// One sample: input[GetInputDim()] -> output[GetOutputDim()]
65
bool
Predict
(
const
float
* input,
float
* output)
const
;
66
// The same with vectors: input.size() must be GetInputDim(); returns an empty vector if failed
67
std::vector<float>
Predict
(
const
std::vector<float>& input)
const
;
68
69
// Several samples with a common first part of the input (the same call as in BmnGemStripMLPModel):
70
// input of row r = [ common[0 .. n_common-1], specific[r*n_spec .. r*n_spec + n_spec-1] ],
71
// n_spec = GetInputDim() - n_common,
72
// output[r*GetOutputDim() .. ] - output of row r.
73
// Here each row is predicted separately (no speed-up, only the common interface).
74
bool
PredictSplit
(
const
float
* common,
int
n_common,
const
float
* specific,
int
n_rows,
float
* output)
const
;
75
//--------------------------------------------------------------------------
76
77
// Shared models (one copy per file in the process) --------------------------
78
// path: "" - the default path; environment variables ($VMCWORKDIR, ~, ...) are expanded (gSystem->ExpandPathName).
79
// The same file given by different strings is loaded once (the cache key is the expanded path).
80
// A file that failed to load is not reloaded on each call (the warning is printed once);
81
// ClearSharedCache() resets this.
82
static
std::shared_ptr<const BmnGemStripFdeepModel>
GetShared
(
const
std::string& path =
""
);
83
static
void
SetDefaultPath
(
const
std::string& path);
84
static
std::string
GetDefaultPath
();
85
static
std::string
GetBuiltinDefaultPath
();
// "$VMCWORKDIR/parameters/gem/ANNModels/GEM_DefaultModel.json"
86
static
void
ClearSharedCache
();
87
//--------------------------------------------------------------------------
88
89
private
:
90
BmnGemStripFdeepModel
(
const
BmnGemStripFdeepModel
&) =
delete
;
91
BmnGemStripFdeepModel
&
operator=
(
const
BmnGemStripFdeepModel
&) =
delete
;
92
93
private
:
94
struct
Impl
;
// fdeep model (defined in BmnGemStripFdeepModel.cxx only)
95
std::unique_ptr<Impl>
fImpl
;
// nullptr if not loaded
96
97
bool
fLoaded
;
98
int
fInputDim
;
99
int
fOutputDim
;
100
std::string
fFilePath
;
101
};
102
//------------------------------------------------------------------------------
103
104
#endif
/* BMNGEMSTRIPFDEEPMODEL_H */
BmnGemStripFdeepModel::ClearSharedCache
static void ClearSharedCache()
Definition
BmnGemStripFdeepModel.cxx:286
BmnGemStripFdeepModel::fImpl
std::unique_ptr< Impl > fImpl
Definition
BmnGemStripFdeepModel.h:95
BmnGemStripFdeepModel::GetShared
static std::shared_ptr< const BmnGemStripFdeepModel > GetShared(const std::string &path="")
Definition
BmnGemStripFdeepModel.cxx:236
BmnGemStripFdeepModel::GetFilePath
const std::string & GetFilePath() const
Definition
BmnGemStripFdeepModel.h:58
BmnGemStripFdeepModel::fOutputDim
int fOutputDim
Definition
BmnGemStripFdeepModel.h:99
BmnGemStripFdeepModel::IsBuiltWithFdeep
static bool IsBuiltWithFdeep()
Definition
BmnGemStripFdeepModel.cxx:101
BmnGemStripFdeepModel::BmnGemStripFdeepModel
BmnGemStripFdeepModel()
Definition
BmnGemStripFdeepModel.cxx:78
BmnGemStripFdeepModel::SetDefaultPath
static void SetDefaultPath(const std::string &path)
Definition
BmnGemStripFdeepModel.cxx:267
BmnGemStripFdeepModel::GetBuiltinDefaultPath
static std::string GetBuiltinDefaultPath()
Definition
BmnGemStripFdeepModel.cxx:281
BmnGemStripFdeepModel::GetOutputDim
int GetOutputDim() const
Definition
BmnGemStripFdeepModel.h:57
BmnGemStripFdeepModel::~BmnGemStripFdeepModel
virtual ~BmnGemStripFdeepModel()
Definition
BmnGemStripFdeepModel.cxx:90
BmnGemStripFdeepModel::GetInputDim
int GetInputDim() const
Definition
BmnGemStripFdeepModel.h:56
BmnGemStripFdeepModel::fLoaded
bool fLoaded
Definition
BmnGemStripFdeepModel.h:97
BmnGemStripFdeepModel::IsLoaded
bool IsLoaded() const
Definition
BmnGemStripFdeepModel.h:49
BmnGemStripFdeepModel::Predict
bool Predict(const float *input, float *output) const
Definition
BmnGemStripFdeepModel.cxx:160
BmnGemStripFdeepModel::PredictSplit
bool PredictSplit(const float *common, int n_common, const float *specific, int n_rows, float *output) const
Definition
BmnGemStripFdeepModel.cxx:197
BmnGemStripFdeepModel::Load
bool Load(const std::string &path, bool verbose=true)
Definition
BmnGemStripFdeepModel.cxx:107
BmnGemStripFdeepModel::operator=
BmnGemStripFdeepModel & operator=(const BmnGemStripFdeepModel &)=delete
BmnGemStripFdeepModel::Print
void Print(std::ostream &os=std::cout) const
Definition
BmnGemStripFdeepModel.cxx:224
BmnGemStripFdeepModel::Clear
void Clear()
Definition
BmnGemStripFdeepModel.cxx:92
BmnGemStripFdeepModel::fFilePath
std::string fFilePath
Definition
BmnGemStripFdeepModel.h:100
BmnGemStripFdeepModel::GetDefaultPath
static std::string GetDefaultPath()
Definition
BmnGemStripFdeepModel.cxx:274
BmnGemStripFdeepModel::BmnGemStripFdeepModel
BmnGemStripFdeepModel(const BmnGemStripFdeepModel &)=delete
BmnGemStripFdeepModel::fInputDim
int fInputDim
Definition
BmnGemStripFdeepModel.h:98
BmnGemStripFdeepModel::Impl
Definition
BmnGemStripFdeepModel.cxx:66
detectors
gem
BmnGemStripFdeepModel.h
Generated on
for BmnRoot by
1.17.0