MCPcopy Create free account
hub / github.com/brainflow-dev/brainflow / predict

Method predict

src/ml/onnx/onnx_classifier.cpp:65–391  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63
64
65int OnnxClassifier::predict (double *data, int data_len, double *output, int *output_len)
66{
67 int res = (int)BrainFlowExitCodes::STATUS_OK;
68 if (ort == NULL)
69 {
70 return (int)BrainFlowExitCodes::CLASSIFIER_IS_NOT_PREPARED_ERROR;
71 }
72 if ((data == NULL) || (data_len < 1) || (output == NULL) || (output_len == NULL))
73 {
74 safe_logger (spdlog::level::err, "invalid input arguments");
75 return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR;
76 }
77
78 // todo add support for ints and float16
79 float *float_data = NULL;
80 if (input_type == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT)
81 {
82 float_data = new float[data_len];
83 for (int i = 0; i < data_len; i++)
84 {
85 float_data[i] = (float)data[i];
86 }
87 }
88 else if (input_type == ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE)
89 {
90 // no need to convert
91 }
92 else
93 {
94 safe_logger (
95 spdlog::level::err, "only float and double input types are currently supported");
96 return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR;
97 }
98
99 // create input tensor object from data values
100 OrtMemoryInfo *memory_info = NULL;
101 OrtValue *input_tensor = NULL;
102 OrtValue *output_tensor = NULL;
103 OrtStatus *onnx_status =
104 ort->CreateCpuMemoryInfo (OrtArenaAllocator, OrtMemTypeDefault, &memory_info);
105 if (onnx_status != NULL)
106 {
107 const char *msg = ort->GetErrorMessage (onnx_status);
108 safe_logger (spdlog::level::err, "CreateCpuMemoryInfo failed: {}", msg);
109 ort->ReleaseStatus (onnx_status);
110 res = (int)BrainFlowExitCodes::GENERAL_ERROR;
111 }
112 else if (memory_info == NULL)
113 {
114 safe_logger (spdlog::level::err, "CreateCpuMemoryInfo failed");
115 res = (int)BrainFlowExitCodes::GENERAL_ERROR;
116 }
117 if (res == (int)BrainFlowExitCodes::STATUS_OK)
118 {
119 if (input_type == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT)
120 {
121 onnx_status = ort->CreateTensorWithDataAsOrtValue (memory_info, float_data,
122 data_len * sizeof (float), input_node_dims.data (), input_node_dims.size (),

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected