How do I train a bounding box dataset on YOLOv5?

23 visualizzazioni (ultimi 30 giorni)
Hello guys! I want to learn how to train in MATLAB for my thesis but sadly the MATLAB documentation isn't really that helpful. How would I be able to train my dataset using MATLAB? I already have the images folder and the MATLAB annotation files.
Your help is very much appreciated. Thank you!

Risposte (1)

Malay Agarwal
Malay Agarwal il 20 Set 2024
Modificato: Malay Agarwal il 20 Set 2024
MATLAB does not have any function like trainYOLOv4ObjectDetector (for YOLOv4) and trainYOLOXObjectDetector (for YOLOX) to train YOLOv5 models. You'll need to write a custom training loop.
To get started, you need to create a dlnetwork (https://www.mathworks.com/help/deeplearning/ref/dlnetwork.html) object which has the same architecture as YOLOv5.
If you do not want to create a dlnetwork manually, you can try importing a pre-trained YOLOv5 model. The official repository for YOLOv5 has multiple pre-trained networks in different sizes saved in the PyTorch format: https://github.com/ultralytics/yolov5?tab=readme-ov-file#pretrained-checkpoints.
One issue with the models in this repository is that they are not JIT-traced in PyTorch and cannot be loaded using importNetworkFromPyTorch (https://www.mathworks.com/help/deeplearning/ref/importnetworkfrompytorch.html#mw_5b3b9d1e-30ec-435a-a0cc-9dd86bbb76c8). Also, the models use the float16 data type, which is not supported by MATLAB.
To get arond these issues, you can use the following workflow:
  1. Install Python and PyTorch.
  2. Clone the offical git repository to a folder of your choice using the command git clone https://github.com/ultralytics/yolov5.git.
  3. Download any of the pre-trained checkpoints based on your preferences. I am using the smallest yolov5n.pt for this example.
  4. Write and execute a Python script with the following code:
import torch
# Load the model
# TODO: Change the filename based on what you downloaded
model = torch.load("yolov5n.pt")
model = model["model"]
# Convert the model to float32 since MATLAB does not support float16
model = model.float()
# Create a dummy image to run through the model
# TODO: Change this to the size of your images
torch_input = torch.randn(1, 3, 224, 224)
# Export to ONNX format
onnx_program = torch.onnx.export(model, (torch_input,), "yolov5n.onnx")
The script will save the model in the ONNX format, which can then be loaded in MATLAB using the importNetworkFromONNX function.
net = importNetworkFromONNX("yolov5n.onnx");
Refer to the following example which shows how to create a custom training loop: https://www.mathworks.com/help/deeplearning/ug/train-network-using-custom-training-loop.html
Refer to the following resources for more information:
Hope this helps!
  2 Commenti
Kenneth
Kenneth il 20 Set 2024
Thank you very much! I'll start working on it and get back to you for any updates.
Nadhirah Maharani
Nadhirah Maharani il 6 Lug 2025
Hi, there! If I may ask, do you already have the updates regarding the project?
Thank you in advance!

Accedi per commentare.

Categorie

Scopri di più su Deep Learning Toolbox in Help Center e File Exchange

Prodotti


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by