{"id":17309816,"url":"https://github.com/bobld/yolov3mlnet","last_synced_at":"2025-08-16T18:09:39.348Z","repository":{"id":108795121,"uuid":"306060602","full_name":"BobLd/YOLOv3MLNet","owner":"BobLd","description":"Use the YOLO v3 (ONNX) model for object detection in C# using ML.Net","archived":false,"fork":false,"pushed_at":"2021-07-17T14:05:23.000Z","size":8483,"stargazers_count":21,"open_issues_count":0,"forks_count":8,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-11T15:51:59.345Z","etag":null,"topics":["computer-vision","csharp","dotnet","machine-learning","ml","ml-net","neural-network","object-detection","onnx","onnx-torch","python","yolo","yolov3"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BobLd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-10-21T14:59:42.000Z","updated_at":"2025-06-25T12:46:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"fe95b84e-bd2c-452f-8546-951f9e23fd23","html_url":"https://github.com/BobLd/YOLOv3MLNet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/BobLd/YOLOv3MLNet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BobLd%2FYOLOv3MLNet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BobLd%2FYOLOv3MLNet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BobLd%2FYOLOv3MLNet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BobLd%2FYOLOv3MLNet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BobLd","download_url":"https://codeload.github.com/BobLd/YOLOv3MLNet/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BobLd%2FYOLOv3MLNet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270749400,"owners_count":24638731,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-16T02:00:11.002Z","response_time":91,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["computer-vision","csharp","dotnet","machine-learning","ml","ml-net","neural-network","object-detection","onnx","onnx-torch","python","yolo","yolov3"],"created_at":"2024-10-15T12:32:51.824Z","updated_at":"2025-08-16T18:09:39.323Z","avatar_url":"https://github.com/BobLd.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"**Another case study, based on [this](https://github.com/onnx/models/tree/master/vision/object_detection_segmentation/yolov3) YOLO v3 model is available [here](https://github.com/BobLd/YOLOv3MLNet/tree/master/YOLOV3MLNetSO).**\n\n**See [here](https://github.com/BobLd/YOLOv4MLNet) for YOLO v4 use.**\n\n# YOLO v3 in ML.Net\nUse the YOLO v3 algorithms for object detection in C# using ML.Net. We start with a Torch model, then converting it to ONNX format and use it in ML.Net.\n\nThis is a case study on a document layout YOLO trained model. The model can be found in the following Medium article: [Object Detection — Document Layout Analysis Using Monk AI](https://medium.com/towards-artificial-intelligence/object-detection-document-layout-analysis-using-monk-object-detection-toolkit-6c57200bde5).\n\n## Main differences\n- The ONNX conversion removes 1 feature which is the *objectness score*, p\u003csub\u003ec\u003c/sub\u003e. The original model has (5 + classes) features for each bounding box, the ONNX model has (4 + classes) features per bounding box. We will use the class probability as a proxy for the *objectness score* when performing the Non-maximum Suppression (NMS) step. This is a known issue, more info [here](https://github.com/ultralytics/yolov3/issues/750).\n- Image resizing is not optimised, and will always yield 416x416 size image. This is not the case in the original model (see this issue: [RECTANGULAR INFERENCE](https://github.com/ultralytics/yolov3/issues/232)).\n\n# Export to ONNX in Python\nThis is based on this article [Object Detection — Document Layout Analysis Using Monk AI](https://medium.com/towards-artificial-intelligence/object-detection-document-layout-analysis-using-monk-object-detection-toolkit-6c57200bde5).\n\n## Load the model\n```python\nimport os\nimport sys\nfrom IPython.display import Image\nsys.path.append(\"../Monk_Object_Detection/7_yolov3/lib\")\nfrom infer_detector import Infer\n\ngtf = Infer()\n\nf = open(\"dla_yolov3/classes.txt\")\nclass_list = f.readlines()\nf.close()\n\nmodel_name = \"yolov3\"\nweights = \"dla_yolov3/dla_yolov3.pt\"\ngtf.Model(model_name, class_list, weights, use_gpu=False, input_size=(416, 416))\n```\n\n## Test the model\n```python\nimg_path = \"test_square.jpg\"\ngtf.Predict(img_path, conf_thres=0.2, iou_thres=0.5)\nImage(filename='output/test_square.jpg')\n```\n\n## Export the model\nYou need to set `ONNX_EXPORT = True` in `...\\Monk_Object_Detection\\7_yolov3\\lib\\models.py` before loading the model.\n\nWe name the input layer `image` and the 2 ouput layers `classes`, `bboxes`. This is not needed but helps the clarity.\n\n```python\nimport torch\nimport torchvision.models as models\n\ndummy_input = torch.randn(1, 3, 416, 416) # Create the right input shape (e.g. for an image)\ndummy_input = torch.nn.Sigmoid()(dummy_input) # limit between 0 and 1 (superfluous?)\ntorch.onnx.export(gtf.system_dict[\"local\"][\"model\"],\n                  dummy_input, \n                  \"dla_yolov3.onnx\",\n                  input_names=[\"image\"],\n                  output_names=[\"classes\", \"bboxes\"],\n                  opset_version=9)\n```\n\n# Check exported model with Netron\nThe ONNX model can be viewed in [Netron](https://www.electronjs.org/apps/netron). Our model looks like this:\n![neutron](https://github.com/BobLd/YOLOv3MLNet/blob/master/netron.png)\n\n- The input layer size is [1 x 3 x 416 x 416]. This corresponds to 1 batch size x 3 colors x 416 pixels height x 416 pixel width (more info about fixed batch size [here](https://github.com/ultralytics/yolov3/issues/1030)).\n\nAs per this [article](https://medium.com/analytics-vidhya/yolo-v3-theory-explained-33100f6d193):\n\u003e For an image of size 416 x 416, YOLO predicts ((52 x 52) + (26 x 26) + 13 x 13)) x 3 = 10,647 bounding boxes.\n- The `bboxes` output layer is of size [10,647 x 4]. This corresponds to 10,647 bounding boxes x 4 bounding box coordinates (x, y, h, w).\n- The `classes` output layer is of size [10,647 x 18]. This corresponds to 10,647 bounding boxes x 18 classes (this model has only 18 classes).\n\nHence, each bounding box has (4 + classes) = 22 features. The total number of prediction in this model is 22 x 10,647.\n\n**NB**: The ONNX conversion removes 1 feature which is the *objectness score*, p\u003csub\u003ec\u003c/sub\u003e. The original model has (5 + classes) features for each bounding box. We will use the class probability as a proxy for the *objectness score*.\n\n![medium-explanation](https://miro.medium.com/max/700/1*6KLkWAWCINb8kVNuPRaDMQ.png)\n\nMore information can be found in this article: [YOLO v3 theory explained](https://medium.com/analytics-vidhya/yolo-v3-theory-explained-33100f6d193)\n\n# Load model in C#\n\n# Predict in C#\n![output](YOLOv3MLNet/Assets/Output/PMC5055614_00001._processed.jpg)\n\n# Resources\n- https://medium.com/towards-artificial-intelligence/object-detection-document-layout-analysis-using-monk-object-detection-toolkit-6c57200bde5\n- https://medium.com/analytics-vidhya/yolo-v3-theory-explained-33100f6d193\n- https://towardsdatascience.com/non-maximum-suppression-nms-93ce178e177c\n- https://michhar.github.io/convert-pytorch-onnx/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbobld%2Fyolov3mlnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbobld%2Fyolov3mlnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbobld%2Fyolov3mlnet/lists"}