onnxobjectdetector: gracefully handle Ort exceptions rather than dumping core

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/4765>
This commit is contained in:
Aaron Boxer 2023-06-02 13:21:06 -04:00 committed by GStreamer Marge Bot
parent a1c2df830b
commit e624e7c695
2 changed files with 63 additions and 48 deletions

View file

@ -203,56 +203,65 @@ bool GstOnnxClient::createSession (std::string modelFile,
break; break;
}; };
Ort::SessionOptions sessionOptions; try {
// for debugging Ort::SessionOptions sessionOptions;
//sessionOptions.SetIntraOpNumThreads (1); // for debugging
sessionOptions.SetGraphOptimizationLevel (onnx_optim); //sessionOptions.SetIntraOpNumThreads (1);
m_provider = provider; sessionOptions.SetGraphOptimizationLevel (onnx_optim);
switch (m_provider) { m_provider = provider;
case GST_ONNX_EXECUTION_PROVIDER_CUDA: switch (m_provider) {
case GST_ONNX_EXECUTION_PROVIDER_CUDA:
#ifdef GST_ML_ONNX_RUNTIME_HAVE_CUDA #ifdef GST_ML_ONNX_RUNTIME_HAVE_CUDA
Ort::ThrowOnError (OrtSessionOptionsAppendExecutionProvider_CUDA Ort::ThrowOnError (OrtSessionOptionsAppendExecutionProvider_CUDA
(sessionOptions, 0)); (sessionOptions, 0));
#else #else
return false; GST_ERROR ("ONNX CUDA execution provider not supported");
return false;
#endif #endif
break; break;
default: default:
break; break;
}; };
session = new Ort::Session (getEnv (), modelFile.c_str (), sessionOptions); session =
auto inputTypeInfo = session->GetInputTypeInfo (0); new Ort::Session (getEnv (), modelFile.c_str (), sessionOptions);
std::vector < int64_t > inputDims = auto inputTypeInfo = session->GetInputTypeInfo (0);
inputTypeInfo.GetTensorTypeAndShapeInfo ().GetShape (); std::vector < int64_t > inputDims =
if (inputImageFormat == GST_ML_MODEL_INPUT_IMAGE_FORMAT_HWC) { inputTypeInfo.GetTensorTypeAndShapeInfo ().GetShape ();
height = inputDims[1]; if (inputImageFormat == GST_ML_MODEL_INPUT_IMAGE_FORMAT_HWC) {
width = inputDims[2]; height = inputDims[1];
channels = inputDims[3]; width = inputDims[2];
} else { channels = inputDims[3];
channels = inputDims[1]; } else {
height = inputDims[2]; channels = inputDims[1];
width = inputDims[3]; height = inputDims[2];
} width = inputDims[3];
fixedInputImageSize = width > 0 && height > 0;
GST_DEBUG ("Number of Output Nodes: %d", (gint) session->GetOutputCount ());
Ort::AllocatorWithDefaultOptions allocator;
auto input_name = session->GetInputNameAllocated (0, allocator);
GST_DEBUG ("Input name: %s", input_name.get());
for (size_t i = 0; i < session->GetOutputCount (); ++i) {
auto output_name = session->GetOutputNameAllocated (i, allocator);
GST_DEBUG("Output name %lu:%s", i, output_name.get());
outputNames.push_back (std::move(output_name));
auto type_info = session->GetOutputTypeInfo (i);
auto tensor_info = type_info.GetTensorTypeAndShapeInfo ();
if (i < GST_ML_OUTPUT_NODE_NUMBER_OF) {
auto function = outputNodeIndexToFunction[i];
outputNodeInfo[function].type = tensor_info.GetElementType ();
} }
fixedInputImageSize = width > 0 && height > 0;
GST_DEBUG ("Number of Output Nodes: %d",
(gint) session->GetOutputCount ());
Ort::AllocatorWithDefaultOptions allocator;
auto input_name = session->GetInputNameAllocated (0, allocator);
GST_DEBUG ("Input name: %s", input_name.get ());
for (size_t i = 0; i < session->GetOutputCount (); ++i) {
auto output_name = session->GetOutputNameAllocated (i, allocator);
GST_DEBUG ("Output name %lu:%s", i, output_name.get ());
outputNames.push_back (std::move (output_name));
auto type_info = session->GetOutputTypeInfo (i);
auto tensor_info = type_info.GetTensorTypeAndShapeInfo ();
if (i < GST_ML_OUTPUT_NODE_NUMBER_OF) {
auto function = outputNodeIndexToFunction[i];
outputNodeInfo[function].type = tensor_info.GetElementType ();
}
}
}
catch (Ort::Exception & ortex) {
GST_ERROR ("%s", ortex.what ());
return false;
} }
return true; return true;

View file

@ -643,9 +643,15 @@ gst_onnx_object_detector_process (GstBaseTransform * trans, GstBuffer * buf)
} }
if (gst_buffer_map (buf, &info, GST_MAP_READ)) { if (gst_buffer_map (buf, &info, GST_MAP_READ)) {
GstOnnxObjectDetector *self = GST_ONNX_OBJECT_DETECTOR (trans); GstOnnxObjectDetector *self = GST_ONNX_OBJECT_DETECTOR (trans);
auto boxes = GST_ONNX_MEMBER (self)->run (info.data, vmeta, std::vector < GstOnnxNamespace::GstMlBoundingBox > boxes;
self->label_file ? self->label_file : "", try {
self->score_threshold); boxes = GST_ONNX_MEMBER (self)->run (info.data, vmeta,
self->label_file ? self->label_file : "", self->score_threshold);
}
catch (Ort::Exception & ortex) {
GST_ERROR_OBJECT (self, "%s", ortex.what ());
return FALSE;
}
for (auto & b:boxes) { for (auto & b:boxes) {
auto vroi_meta = gst_buffer_add_video_region_of_interest_meta (buf, auto vroi_meta = gst_buffer_add_video_region_of_interest_meta (buf,
GST_ONNX_OBJECT_DETECTOR_META_NAME, GST_ONNX_OBJECT_DETECTOR_META_NAME,