Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
MlModel Class Reference

An ONNX model that scores a feature vector. The model must take one float tensor of shape [1, FEATURE_COUNT] (or [N, FEATURE_COUNT]) and output a float tensor whose last value is the malicious probability: [1, 1] or [1, 2] (benign, malicious). See README.md. More...

#include <PanoptesML.h>

Classes

struct  Impl

Public Member Functions

 MlModel (const std::wstring &ModelPath)
 Loads the model. Throws Ort::Exception or std::runtime_error when it can't be loaded or doesn't take FEATURE_COUNT features.
 MlModel (const void *Data, size_t Size, const std::string &Name)
 The same, from the model's bytes in memory. Name is what results report as the model.
 ~MlModel ()
 MlModel (const MlModel &)=delete
MlModel & operator= (const MlModel &)=delete
float Score (const std::vector< float > &Features)
 Runs the model on one file's features. Returns the malicious probability.
const std::string & Name () const

Detailed Description

An ONNX model that scores a feature vector. The model must take one float tensor of shape [1, FEATURE_COUNT] (or [N, FEATURE_COUNT]) and output a float tensor whose last value is the malicious probability: [1, 1] or [1, 2] (benign, malicious). See README.md.

Definition at line 73 of file PanoptesML.h.

Constructor & Destructor Documentation

◆ MlModel() [1/3]

MlModel::MlModel ( const std::wstring & ModelPath)
explicit

Loads the model. Throws Ort::Exception or std::runtime_error when it can't be loaded or doesn't take FEATURE_COUNT features.

Definition at line 44 of file ml_model.cpp.

45 : m_impl((QuietNodeDumps(), std::make_unique<Impl>()))
46{
47 m_name = Narrow(ModelPath.substr(ModelPath.find_last_of(L"\\/") + 1));
48 m_impl->Session = Ort::Session(m_impl->Env, ModelPath.c_str(), MakeSessionOptions());
49 Inspect();
50}

Referenced by MlModel(), and operator=().

◆ MlModel() [2/3]

MlModel::MlModel ( const void * Data,
size_t Size,
const std::string & Name )

The same, from the model's bytes in memory. Name is what results report as the model.

Definition at line 52 of file ml_model.cpp.

53 : m_impl((QuietNodeDumps(), std::make_unique<Impl>())), m_name(Name)
54{
55 // ONNX Runtime copies what it needs; the caller may wipe Data afterwards
56 m_impl->Session = Ort::Session(m_impl->Env, Data, Size, MakeSessionOptions());
57 Inspect();
58}
const std::string & Name() const
Definition PanoptesML.h:87

References Name().

◆ ~MlModel()

MlModel::~MlModel ( )
default

◆ MlModel() [3/3]

MlModel::MlModel ( const MlModel & )
delete

References MlModel().

Member Function Documentation

◆ Name()

const std::string & MlModel::Name ( ) const
inline

Definition at line 87 of file PanoptesML.h.

87{ return m_name; }

Referenced by MlModel(), PanoEntry(), and PanoMlScoreFile().

◆ operator=()

MlModel & MlModel::operator= ( const MlModel & )
delete

References MlModel().

◆ Score()

float MlModel::Score ( const std::vector< float > & Features)

Runs the model on one file's features. Returns the malicious probability.

Definition at line 99 of file ml_model.cpp.

100{
101 if (Features.size() != FEATURE_COUNT) {
102 throw std::runtime_error("expected " + std::to_string(FEATURE_COUNT) + " features, got " + std::to_string(Features.size()));
103 }
104
105 Ort::MemoryInfo memory = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
106 const int64_t shape[2] = { 1, (int64_t)FEATURE_COUNT };
107 Ort::Value input = Ort::Value::CreateTensor<float>(memory, const_cast<float*>(Features.data()),
108 Features.size(), shape, 2);
109
110 const char* inputName = m_impl->InputName.c_str();
111 const char* outputName = m_impl->OutputNames[m_impl->ScoreOutput].c_str();
112 std::vector<Ort::Value> outputs = m_impl->Session.Run(Ort::RunOptions{ nullptr }, &inputName, &input, 1, &outputName, 1);
113
114 auto info = outputs[0].GetTensorTypeAndShapeInfo();
115 size_t count = info.GetElementCount();
116 if (count == 0) {
117 throw std::runtime_error(m_name + " returned an empty score");
118 }
119 return outputs[0].GetTensorData<float>()[count - 1];
120}
@ FEATURE_COUNT
Definition PanoptesML.h:51

References FEATURE_COUNT.

Referenced by PanoEntry(), and PanoMlScoreFile().


The documentation for this class was generated from the following files: