Skip to content
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

What to do with output tensor (OpenVINO Inference) #7817

Closed
1 task done
GabrielDornelles opened this issue May 15, 2022 · 8 comments · Fixed by #7826
Closed
1 task done

What to do with output tensor (OpenVINO Inference) #7817

GabrielDornelles opened this issue May 15, 2022 · 8 comments · Fixed by #7826
Labels
question Further information is requested

Comments

@GabrielDornelles
Copy link
Contributor

Search before asking

Question

I borrowed the inference code for openvino:

import openvino.inference_engine as ie
import cv2

core = ie.IECore()
w = "yolov5s_openvino_model/yolov5s.xml"

bin_path = "yolov5s_openvino_model/yolov5s.bin"
network = core.read_network(model=w, weights=bin_path)
executable_network = core.load_network(network, device_name='CPU', num_requests=1)


frame = cv2.imread("/Downloads/maxresdefault.jpg")
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = cv2.resize(frame, (640, 640))
frame = frame.transpose(2,0,1)
frame = frame[None,:,:,:]
frame = np.array(frame, dtype=np.float32)

desc = ie.TensorDesc(precision='FP32', dims=frame.shape, layout='NCHW')
request = executable_network.requests[0]
request.set_blob(blob_name='images', blob=ie.Blob(desc, frame)) 
request.infer()
y = request.output_blobs['output'].buffer
print(y.shape)
>>> (1, 25200, 85)

I don't understand what that output tensor (np.array in this case) is.

Additional

No response

@GabrielDornelles GabrielDornelles added the question Further information is requested label May 15, 2022
@github-actions
Copy link
Contributor

github-actions bot commented May 15, 2022

👋 Hello @GabrielDornelles, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

glenn-jocher commented May 15, 2022

@GabrielDornelles 👋 Hello! Thanks for asking about handling inference results. YOLOv5 🚀 PyTorch Hub models allow for simple OpenVINO model loading and inference in a pure python environment of any YOLOv5 exported model:

model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.engine')  # TensorRT
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.onnx')  # ONNX
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s_openvino_model/')  # OpenVINO
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.torchscript')  # TorchScript
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.mlmodel')  # CoreML
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.tflite')  # TFLite

Simple Inference Example

This example loads a pretrained YOLOv5s model from PyTorch Hub as model and passes an image for inference. 'yolov5s' is the YOLOv5 'small' model. For details on all available models please see the README. Custom models can also be loaded, including custom trained PyTorch models and their exported variants, i.e. ONNX, TensorRT, TensorFlow, OpenVINO YOLOv5 models.

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # or yolov5m, yolov5l, yolov5x, etc.
# model = torch.hub.load('ultralytics/yolov5', 'custom', 'path/to/best.pt')  # custom trained model

# Images
im = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, URL, PIL, OpenCV, numpy, list

# Inference
results = model(im)

# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.

results.xyxy[0]  # im predictions (tensor)
results.pandas().xyxy[0]  # im predictions (pandas)
#      xmin    ymin    xmax   ymax  confidence  class    name
# 0  749.50   43.50  1148.0  704.5    0.874023      0  person
# 2  114.75  195.75  1095.0  708.0    0.624512      0  person
# 3  986.00  304.00  1028.0  420.0    0.286865     27     tie

See YOLOv5 PyTorch Hub Tutorial for details.

Good luck 🍀 and let us know if you have any other questions!

@GabrielDornelles
Copy link
Contributor Author

GabrielDornelles commented May 15, 2022

Hi @glenn-jocher!

Thanks for the answer, although I would like to understand what that shape was, I tried the implementation of yours to run the OpenVINO model, so if it worked I could just read the code and understand it.

When loading the openvino model like you said

model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s_openvino_model/')

It searchs for a checkpoint

Exception: [Errno 2] No such file or directory: 'yolov5s_openvino_model.pt'. Cache may be out of date, try `force_reload=True` or see https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading for help.

So I just looked over the code in DetectMultiBackEnd and saw that it actually searchs for .xml file in order to run OpenVINO Inference.

frame = cv2.imread("/home/gabriel/Downloads/maxresdefault.jpg")
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s_openvino_model/yolov5s.xml')
result = model(frame)
result.save()

image0

I think I can manage to get the code I want now that it works with the openvino files, but a quick correction would be that in order to run OpenVINO you need to pass .xml openvino file instead of openvino model directory.

glenn-jocher added a commit that referenced this issue May 15, 2022
@glenn-jocher glenn-jocher linked a pull request May 15, 2022 that will close this issue
glenn-jocher added a commit that referenced this issue May 15, 2022
@glenn-jocher
Copy link
Member

@GabrielDornelles good news 😃! Your original issue may now be fixed ✅ in PR #7826. To receive this update:

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

@GabrielDornelles
Copy link
Contributor Author

Working perfectly now!

tdhooghe pushed a commit to tdhooghe/yolov5 that referenced this issue Jun 10, 2022
ctjanuhowski pushed a commit to ctjanuhowski/yolov5 that referenced this issue Sep 8, 2022
@glenn-jocher
Copy link
Member

Great to hear that, @GabrielDornelles! 🎉 If you encounter any other questions or issues with YOLOv5 or need further assistance, feel free to reach out. Best of luck with your project, and happy coding! 🚀

@pinnintipraneethkumar
Copy link

Hi@glenn-jocher ,
I exported the openvino model with export.py which created the folder name "best_openvino_model" which conatins the meta data of the model, but when i change the model folder name to "openvino_model" , the torch.hub.load throws this error

Exception: [Errno 13] Permission denied: 'D:\update_infer\yolov5\best_openvino_model_16\openvino_model'. Cache may be out of date, try force_reload=True or see https://docs.ultralytics.com/yolov5/tutorials/pytorch_hub_model_loading for help.

note: openvino_model folder contains all meta data information same as best_oprnvino_model folder

@glenn-jocher
Copy link
Member

@pinnintipraneethkumar hi there! It sounds like you're facing a file permission issue after renaming your model folder. This can happen due to various reasons such as the way the folder was renamed or the permissions set on the folder.

Here's a quick thing you can try:

  1. Ensure your current user has full control or ownership of the newly named folder and its contents. You can check and modify this in the folder's properties under Security settings (right-click on the folder → Properties → Security tab).

  2. Try using the force_reload=True parameter in your torch.hub.load call to bypass any caching issues, like so:

model = torch.hub.load('ultralytics/yolov5', 'custom', path='path/to/openvino_model.xml', force_reload=True)

Make sure the path correctly points to your .xml file within the openvino_model folder. Also, confirm that the folder path is accessible and correctly typed.

Hopefully, this helps! If the issue persists, double-check the folder permissions and try running your script with administrative privileges or in a different directory. Happy coding! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants