-
So I have this cool little cpp file that can open a wave file and a model and is meant to start to get the timestamps and cut the audio, but one issue I'm stuck on now is how to properly construct a tensor with the right shape of the input (wav, sr, h, c). I've looked at an issue at microsoft/onnxruntime#4528 and I'll probably go over that again tomorrow. IN the meantime, I wondered if anyone had any experience with this? #include <onnxruntime_c_api.h>
#include <sndfile.h>
#include <vector>
const OrtApi* g_ort = OrtGetApiBase()->GetApi(ORT_API_VERSION);
int main(int argc, char* argv[]) {
if (argc != 3) {
printf("Usage: %s <wave_file> <onnx_model>\n", argv[0]);
return 1;
}
const char* filename = argv[1];
const char* model_path = argv[2];
SF_INFO sfinfo;
SNDFILE* infile;
infile = sf_open(filename, SFM_READ, &sfinfo);
std::vector<float> audio_vector(sfinfo.frames * sfinfo.channels);
sf_count_t count = sf_read_float(infile, audio_vector.data(), audio_vector.size());
sf_close(infile);
OrtAllocator* allocator;
g_ort->GetAllocatorWithDefaultOptions(&allocator);
// Load ONNX model
OrtEnv* env;
g_ort->CreateEnv(ORT_LOGGING_LEVEL_WARNING, "test", &env);
OrtSessionOptions* session_options;
g_ort->CreateSessionOptions(&session_options);
g_ort->SetIntraOpNumThreads(session_options, 1);
OrtSession* session;
g_ort->CreateSession(env, model_path, session_options, &session);
// Create input tensor from audio data
std::vector<int64_t> input_node_dims = {1, static_cast<int64_t>(audio_vector.size())};
OrtMemoryInfo* memory_info;
g_ort->CreateCpuMemoryInfo(OrtArenaAllocator, OrtMemTypeDefault, &memory_info);
OrtValue* input_tensor = NULL;
g_ort->CreateTensorWithDataAsOrtValue(memory_info, audio_vector.data(), audio_vector.size() * sizeof(float),
input_node_dims.data(), input_node_dims.size(),
ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, &input_tensor
);
g_ort->ReleaseMemoryInfo(memory_info);
std::vector<const char*> input_node_names(input_node_dims.size());
// iterate over all input nodes
for (size_t i = 0; i < input_node_dims.size(); i++) {
// print input node names
char* input_name;
g_ort->SessionGetInputName(session, i, allocator, &input_name);
printf("Input %zu : name=%s\n", i, input_name);
input_node_names[i] = input_name;
}
// Get output tensor name
std::vector<const char*> output_node_names = {"output", "hn", "cn"};
// Run inference and get output tensor
OrtValue* output_tensor = NULL;
g_ort->Run(session, NULL, input_node_names.data(), &input_tensor, 1, output_node_names.data(), 1, &output_tensor);
// Get pointer to output tensor float values
float* output_data;
g_ort->GetTensorMutableData(output_tensor, (void**)&output_data);
// Get output tensor shape information
OrtTensorTypeAndShapeInfo* output_tensor_info = nullptr;
g_ort->GetTensorTypeAndShape(output_tensor, &output_tensor_info);
size_t num_output_dims;
g_ort->GetDimensionsCount(output_tensor_info, &num_output_dims);
std::vector<int64_t> output_node_dims(num_output_dims);
g_ort->GetDimensions(output_tensor_info, output_node_dims.data(), output_node_dims.size());
// Print output tensor float values
size_t num_output_elements = 1;
for (size_t i = 0; i < num_output_dims; i++) {
num_output_elements *= output_node_dims[i];
}
for (size_t i = 0; i < num_output_elements; i++) {
printf("Output %zu : %f\n", i, output_data[i]);
}
// Clean up
g_ort->ReleaseValue(input_tensor);
g_ort->ReleaseValue(output_tensor);
g_ort->ReleaseSession(session);
g_ort->ReleaseSessionOptions(session_options);
g_ort->ReleaseEnv(env);
return 0;
} I'm on MacOS and installed onnxruntime and libsndfile with brew. My build script is: clang++ -std=c++17 -L/opt/homebrew/Cellar/libsndfile/1.2.0/lib -I/opt/homebrew/Cellar/libsndfile/1.2.0/include -I/opt/homebrew/Cellar/onnxruntime/1.14.0/include/onnxruntime/core/session/ -L/opt/homebrew/Cellar/onnxruntime/1.14.0/lib -lonnxruntime -lsndfile test.cpp -o test It works to compile and link but the C++ code is still broken right now. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi, I do not work with cpp, but there is a cpp example donated by the community here - https://github.com/snakers4/silero-vad/tree/master/examples/cpp It may help |
Beta Was this translation helpful? Give feedback.
Hi,
I do not work with cpp, but there is a cpp example donated by the community here - https://github.com/snakers4/silero-vad/tree/master/examples/cpp
It may help