I am trying to create an input tensor for a machine learning model using the ONNX Runtime. This is returned as a vector<Ort::Value>
. After calling the function, the objects inside the vector are destroyed (I assume because the function scope ended and they get cleaned up).
My question doesn’t really concern the use of ONNX but is meant more generally on how to deal with more complex classes.
I wonder how I can call the function to access the values after the function has finished.
In my main function I create some setup code and call a function to create the desired output of vector<Ort::Value>
.
I tested the functionality with an output of vector<std::pair>
and was able to get output as expected.
int main() {
// Create ONNX Session (Setup Code)
std::string onnxFilePath = "model.onnx";
std::string instanceName = "onnxInstance";
Ort::Env onnxEnv{OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING, instanceName.c_str()};
Ort::SessionOptions sessionOptions;
sessionOptions.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL);
Ort::Session onnxSession{onnxEnv, ORT_TSTR(onnxFilePath.c_str()), sessionOptions};
// Read an Image
std::string imageFilePath = "000000.jpg";
cv::Mat img = cv::imread(imageFilePath, cv::IMREAD_COLOR);
// Test return values with std::pair
std::vector<std::pair<int, double>> pairs1 = returnPair(img);
// First few values as expected:
// (0, 0.840188), (1, 0.394383), (2, 0.783099), (3, 0.79844), (4, 0.911647),
std::vector<std::pair<int, double>> pairs2;
overwritePair(img, pairs2);
// First few values as expected:
// (0, 0.489059), (1, 0.967357), (2, 0.22906), (3, 0.109976), (4, 0.168318),
// Get the Input Tensors
std::vector<Ort::Value> tensors1 = returnTensor(onnxSession, img);
// First few values wrong:
// 2.02411e-38, 0, 2.01868e-38, 0, 0,
// Expected:
// 255, 244, 253, 245, 215,
std::vector<Ort::Value> tensors2;
overwriteTensor(onnxSession, img, tensors2);
// First few values wrong:
// 2.02411e-38, 0, 2.01868e-38, 0, 0,
// Expected:
// 255, 244, 253, 245, 215,
// I wold like to access the tensors to call
// onnxSession::Run()
}
Following are the functions used in main()
:
std::vector<std::pair<int, double>> returnPair(cv::Mat &img){
std::cout << "Function returnPair: " << std::endl;
int input_size = img.rows * img.cols;
std::vector<std::pair<int, double>> pairs;
for(std::size_t i = 0; i < input_size; ++i){
float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
pairs.emplace_back(std::make_pair(i, r));
}
// sanity check to see values in function
std::cout << " ";
for(std::size_t i = 0; i < 5; ++i){
std::cout << "(" << pairs[i].first << ", " << pairs[i].second << "), ";
}
std::cout << std::endl << std::endl;
return pairs;
}
void overwriteTensor(Ort::Session &onnxSession, cv::Mat &img, std::vector<Ort::Value> &tensors){
std::cout << "Function returnTensor: " << std::endl;
// resize to fit the onnx model
cv::resize(img, img, cv::Size(640, 640));
int input_size = img.rows * img.cols;
// bring into the correct format
cv::Mat preprocessedImage;
cv::dnn::blobFromImage(img, preprocessedImage);
std::vector<float> img_values(input_size);
img_values.assign(preprocessedImage.begin<float>(), preprocessedImage.end<float>());
// show the image values
std::cout << " image values: " << std::endl;
std::cout << " ";
for(std::size_t i = 0; i < 5; ++i){
std::cout << img_values[i] << ", ";
}
std::cout << std::endl;
// get input name and shape
Ort::AllocatorWithDefaultOptions allocator;
std::vector<std::string> input_names;
std::vector<std::int64_t> input_shapes;
for(std::size_t i = 0; i < onnxSession.GetInputCount(); i++) {
input_names.emplace_back(onnxSession.GetInputNameAllocated(i, allocator).get());
input_shapes = (onnxSession.GetInputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape());
}
// create tensors
Ort::MemoryInfo memInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault);
/* THESE ARE THE VALUES I'D LIKE TO ACCESS */
tensors.emplace_back( Ort::Value::CreateTensor<float>(memInfo,
img_values.data(), img_values.size(),
input_shapes.data(), input_shapes.size()) );
// sanity check to see tensors value
float* tensor_values = tensors[0].GetTensorMutableData<float>();
std::cout << " tensor values: " << std::endl;
std::cout << " ";
for(std::size_t i = 0; i < 5; ++i){
std::cout << tensor_values[i] << ", ";
}
std::cout << std::endl << std::endl;
}
With the analog functionalities for
std::vector<Ort::Value> returnTensor(Ort::Session &onnxSession, cv::Mat &img)
and
void overwritePair(cv::Mat &img, std::vector<std::pair<int, double>> &pairs)
Why do the functions return the result I desire for the std::pair
but not for the Ort::Value
vectors?
And how do I write functions correctly to have access to the values after the function call has ended?