-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ONNXRuntime-Cpp and ONNXRuntime python give different results: #13338
Comments
👋 Hello @devendraswamy, thank you for reaching out with your issue regarding YOLOv5 🚀! This is an automated response to guide you further, and an Ultralytics engineer will be with you soon. Please make sure you are following our Tutorials to ensure your setup is correct. They provide a helpful starting point for concepts including Custom Data Training and Hyperparameter Evolution. If this is a 🐛 Bug Report, please provide a minimum reproducible example so we can better assist you. In your case, ensure that both Python and C++ environments use the same preprocessing steps and ONNX model settings. Discrepancies could lead to different outputs. RequirementsEnsure you have Python>=3.8.0 with all requirements.txt installed and are using PyTorch>=1.8. To set up: git clone https://github.com/ultralytics/yolov5 # clone
cd yolov5
pip install -r requirements.txt # install EnvironmentsYOLOv5 can be run in various environments. Consider using these resources with dependencies preinstalled, including CUDA/CUDNN, Python, and PyTorch:
StatusA green badge means all YOLOv5 GitHub Actions Continuous Integration (CI) tests are passing. These tests verify correct operation on macOS, Windows, and Ubuntu. Introducing YOLOv8 🚀Check out YOLOv8, our latest model designed for superior performance in object detection, segmentation, and classification. Get started with: pip install ultralytics Feel free to provide more details as needed. We'll get back to you soon! 😊 |
Search before asking
Question
I am facing the problem with YOLOV5 model. While I am testing my Python ONNX code, all the bounding box (bbox) values are correct. However, when I perform the same process with my C++ code, I am getting incorrect bbox values.
the image processed in ptyhon code:
image_data = np.expand_dims(image_data, axis=0) # Add batch dimension
and feed that image to python pyd file (c++ inference file complied to pyd)
auto output_tensors = session.Run(Ort::RunOptions{ nullptr }, input_names, &input_tensor, 1, output_names, 1);
Additional
complied or build C++ code is:
#include <onnxruntime_cxx_api.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include
#include
#include
#include
using namespace std;
namespace py = pybind11;
class OnnxModel {
public:
OnnxModel(const std::string& model_path)
: env(ORT_LOGGING_LEVEL_WARNING, "OnnxModel"),
session(env, std::wstring(model_path.begin(), model_path.end()).c_str(), Ort::SessionOptions())
{
Ort::AllocatorWithDefaultOptions allocator;
private:
Ort::Env env;
Ort::Session session;
std::string input_name;
std::string output_name;
};
PYBIND11_MODULE(onnx_loader, m) {
py::class_(m, "OnnxModel")
.def(py::init<const std::string&>())
.def("run", &OnnxModel::run);
}
Image feeding from python code:
Function to preprocess the image
def preprocess_image(image_path, input_size=(640, 640)):
# Load the image using OpenCV
image = cv2.imread(image_path, cv2.IMREAD_COLOR) # Load image in color mode
if image is None:
raise ValueError(f"Could not open or find the image: {image_path}")
# Convert from BGR to RGB format
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Resize the image to match the input size expected by the model
image = cv2.resize(image, input_size)
# Normalize the image to [0, 1] range
image = image.astype(np.float32) / 255.0 # Convert to float and normalize
# Rearrange the image to CHW format (1, C, H, W)
image_data = np.transpose(image, (2, 0, 1)) # Convert to CHW format
image_data = np.expand_dims(image_data, axis=0) # Add batch dimension
print(f"Image preprocessed: type = {type(image_data)}, shape = {image_data.shape}")
return image_data, image # Return the preprocessed image data
The text was updated successfully, but these errors were encountered: