Git Product home page Git Product logo

onnx-yolov7-object-detection's Introduction

ONNX YOLOv7 Object Detection

Python scripts performing object detection using the YOLOv7 model in ONNX.

! ONNX YOLOv7 Object Detection Original image: https://www.flickr.com/photos/nicolelee/19041780

Important

  • The input images are directly resized to match the input size of the model. I skipped adding the pad to the input image, it might affect the accuracy of the model if the input image has a different aspect ratio compared to the input size of the model. Always try to get an input size with a ratio close to the input images you will use.

Requirements

  • Check the requirements.txt file.
  • For ONNX, if you have a NVIDIA GPU, then install the onnxruntime-gpu, otherwise use the onnxruntime library.

Installation

git clone https://github.com/ibaiGorordo/ONNX-YOLOv7-Object-Detection.git
cd ONNX-YOLOv7-Object-Detection
pip install -r requirements.txt

ONNX Runtime

For Nvidia GPU computers: pip install onnxruntime-gpu

Otherwise: pip install onnxruntime

ONNX model

The original models were converted to different formats (including .onnx) by PINTO0309. Download the models from his repository. For that, you can either run the download_single_batch.sh or copy the google drive link inside that script in your browser to manually download the file. Then, extract and copy the downloaded onnx models (for example yolov7-tiny_480x640.onnx) to your models directory, and fix the file name in the python scripts accordingly.

  • The License of the models is GPL-3.0 license: License

Original YOLOv7 model

The original YOLOv7 model can be found in this repository: YOLOv7 Repository

Examples

  • Image inference:
python image_object_detection.py
  • Webcam inference:
python webcam_object_detection.py
python video_object_detection.py

!YOLOv7 detection video

Original video: https://youtu.be/zPre8MgmcHY

python comparison_with_yolov5_v6.py

!YOLOv7 Vs YOLOv5 detection video !YOLOv7 Vs YOLOv6 detection video Original video: https://youtu.be/zPre8MgmcHY

  • Replace the yolov5_v6_path with the actual path to the YOLOv5 or YOLOv6 model.
  • Convert YOLOv5 model to ONNX Open In Colab
  • Convert YOLOv6 model to ONNX Open In Colab

References:

onnx-yolov7-object-detection's People

Contributors

chikako avatar ibaigorordo avatar pinto0309 avatar surak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

onnx-yolov7-object-detection's Issues

cannot import name 'YOLOv7' from 'yolov7'

I am a new player. These questions may be stupid. Sorry for that.

As the repo said I git clone the repo and down load the yolov7_Nx3x256x320.onnx

and try to run the python file all of the 3 detection files do not work and give this error.

Thanks in advance for the help

yolov7 export settings for use with a custom dataset

I'm trying to use this code with yolov7 trained on a custom dataset, but for some reason I can't get the boxes to line up. Works great with the models trained on the coco dataset (from pinto's model zoo) though.

How did you export the trained yolov7 models to onnx?

This was the command I used to export:

python export.py --weights runs/train/yolov73/weights/best.pt --grid --end2end --simplify  --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 640 480 --max-wh 640 --dynamic-batch 

Sorry if this is a dumb question, I'm new to yolo and onnx.

It is crash In the NMS mode.

Hi,
image_object_detection.py.
Lina 8: yolov7_detector = YOLOv7(model_path, conf_thres=0.2, iou_thres=0.3, official_nms=True)
Called:
Line 19: combined_img = yolov7_detector.draw_detections(img)
Yolov7.py.
line 105: def parse_processed_output(self, outputs):
Crash:
line 116: predictions = predictions[valid_scores, :]
IndexError: boolean index did not match indexed array along dimension 1; dimension is 6 but corresponding boolean dimension is 85
Thank
Stephen

100% CPU usage while

It appears that onnxruntime-gpu is bugged or something... as while running inference the CPU goes to 100%, the only way to reduce cpu usage is to reduce the framerate of the camera to something like 5fps. Is this happening to anyone else ?

P.S. providers seems to be right : ['CUDAExecutionProvider', 'CPUExecutionProvider'] cuda does actually get detected by python

how to convert pt to onnx?

how to convert pt to onnx? what about the shell codes?
python export.py --weights yolov7.pt --grid --end2end --simplify
--topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 640 640 --max-wh 640

decode

Does the yolov7 have any change for decode and preprocess?

Run innx on my GPU ?

How to run yolo library in onnx format on my GPU ?
How to use yolo in onnx format with dynamic batch size in js environment ?
Can i make objects tracking with yolo libraries in js environment , if i can , How ?

Can not run on cpu

When I run image_object_detection.py on cpu, it will give error:
onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Unexpected input data type. Actual: (tensor(float)) , expected: (tensor(float16))

change
input_tensor = input_img[np.newaxis, :, :, :].astype(np.float32) to
input_tensor = input_img[np.newaxis, :, :, :].astype(np.float16) will solve this problem.
So can you fix it?

Exception useCuda = true

If i choose useCuda = True

Microsoft.ML.OnnxRuntime 1.11.0:
0688_688_2022_12_10_Vision_v1pMV1d0Af

I thought I needed Microsoft.ML.OnnxRuntime.Gpu
Microsoft.ML.OnnxRuntime.Gpu 1.11.0:
0689_689_2022_12_10_Vision_GtZk2jFSA8

Microsoft.ML.OnnxRuntime.Gpu 1.13.1:
0690_690_2022_12_10_Vision_TlLTUM39Vu

I tried on different devices Windows 10/11
Please, help.

Doubt about official_nms

Hello,
I'm studying how the export to .onnx and .trt works and found your code that export the result of a .onnx file to a format that Yolov7 can read. It was very helpful, but I have some doubts about the nms (non maximum suppression).

At first, I found that the detection boxes didn't display properly unless I set official_nms to True, I found an issue in another project that said that too, but looking at the code It calls a nms function from the files yolov7.utils when official_nms is set to False.

So, I would like to ask what is the purpose and meaning of the variable official_nms in the code.
I still have doubts about it, but I have seen that you can add nms when you export a .pt file to .onnx files (You can see it in the YoloV7 official repository, in the export section ). Is the nms function used to apply nms when you don't apply it at the moment of exporting to .onnx or has any other purpose?

Thank you so much.

ERROR

``
Traceback (most recent call last):

File "image_object_detection.py", line 15, in

boxes, scores, class_ids = yolov7_detector(img)

File "/home/berat/berat-projects/ekin/ONNX-YOLOv7-Object-Detection/yolov7/YOLOv7.py", line 20, in call

return self.detect_objects(image)

File "/home/berat/berat-projects/ekin/ONNX-YOLOv7-Object-Detection/yolov7/YOLOv7.py", line 44, in detect_objects

self.boxes, self.scores, self.class_ids = self.process_output(outputs)

File "/home/berat/berat-projects/ekin/ONNX-YOLOv7-Object-Detection/yolov7/YOLOv7.py", line 78, in process_output

predictions = predictions[obj_conf > self.conf_threshold]

IndexError: boolean index did not match indexed array along dimension 2; dimension is 80 but corresponding boolean

dimension is 85
`

How can I fix that? I just tested the default yolov7.onnx

Handling of low socre in process_output

I have a question about the processing of low score in process_output, which is a def block in yolov7.py.

About the following part:

# Filter out the objects with a low score
    predictions = predictions[obj_conf > self.conf_threshold]
    scores = scores[scores > self.conf_threshold]

I think the calculation to obtain predictions is as follows.

predictions = predictions[**scores** > self.conf_threshold]
	                               (obj_conf -> scores)

Floating Point 16 ONNX model conversion

First off, thank you for such a helpful repo!
I followed the the instructions on https://github.com/WongKinYiu/yolov7 to convert a .pt model to a .onnx model. The model runs fine, it produces the same results as the original .pt model. The problem is the .onnx model is twice the size as the .pt model. I think it's due to different floating point sizes. Any suggestions on converting fp16 .pt model to fp16 .onnx model?

Thanks in advance

Only detect persons and bicycles

I tried with yolov7-nms-640.onnx and it detects only persons and bicycles.
and also it doesnt draw rectangles at the correct locations. appreciate any help to resolve this
detected_objects

Fatal error: TRT:EfficientNMS_TRT(-1) is not a registered function/op

File "/home/melissacolin/anaconda3/lib/python3.10/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py", line 424, in _create_inference_session
sess = C.InferenceSession(session_options, self._model_path, True, self._read_config_from_model)
onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Load model from models/best.onnx failed:Fatal error: TRT:EfficientNMS_TRT(-1) is not a registered function/op

Here the code for GPU but it seems, doesn't run fast

I inferenced the model with both CPU and GPU, but it seems the only difference between the is just 4 ms. With CPU reaches almost 26ms and with CUDA 21 ms. I don't unterstand why is running so bad. I suppose the export in onnx form was not so successful.

def inference(self, input_tensor):
        
        input_name = self.session.get_inputs()[0].name
        output_name = self.session.get_outputs()[0].name
        
        iobinding = self.session.io_binding()
        ortvalue = onnxruntime.OrtValue.ortvalue_from_numpy(input_tensor, 'cuda', 0)
        iobinding.bind_input(input_name, 'cuda', 0, np.float32, ortvalue.shape(), ortvalue.data_ptr())
        iobinding.bind_output(output_name, 'cuda', 0)
        
        
        start = time.perf_counter()
        #outputs = self.session.run(self.output_names, {self.input_names[0]: input_tensor})
        self.session.run_with_iobinding(iobinding)
        print(f"Inference time: {(time.perf_counter() - start)*1000:.2f} ms")
        
        
        outputs = iobinding.copy_outputs_to_cpu()

        
        return outputs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.