Introduction
In this blog post, we’ll guide you through a simple project to detect vehicles using YOLO (You Only Look Once) version 5. Even if you’re a beginner, you’ll find the steps easy to follow. We’ll use Roboflow to get our dataset, Google Drive to store it, and Google Colab to run our code. By the end of this guide, you’ll have a working vehicle detection model.
Table of Contents
- Downloading the Dataset
- Uploading to Google Drive
- Setting Up Google Colab
- Training the Model
- Testing the Model
Downloading the Dataset
First, open any browser and go to the Roboflow website. Search for “Roboflow” and navigate to the “Universe” section. Here, you can find various datasets for image detection and segmentation. For this project, we’ll use a vehicle detection dataset to identify cars, trucks, traffic signs, and more. This dataset is small and perfect for demonstrating YOLO version 5.
Follow these steps to download the dataset:
- Find the vehicle detection dataset.
- Click on the YOLO version 5 option.
- Choose to download the dataset directly to your local device.
The dataset is only 11.3 MB and will be in a ZIP file. Extract this file to access the images and labels.
Uploading to Google Drive
Next, upload the extracted dataset to Google Drive:
- Go to Google Drive and click “Upload”.
- Select the folder containing the dataset.
- Rename the folder to “car_trucks_dataset”.
This folder should contain subfolders for training, validation, and testing sets, along with a “data.yaml
” file that lists the classes we will detect.
Setting Up Google Colab
Now, let’s set up Google Colab to run our code. We’ll use a Colab notebook to train and test our YOLO model.
Steps:
- Open Google Colab and create a new notebook.
- Change the runtime type to “GPU” for faster training.
- Import Google Drive to access our dataset folder.
Run the following code to mount Google Drive:
from google.colab import drive
drive.mount('/content/drive')
- Install the required packages using the following command:
!pip install ultralytics
Training the Model
We’ll use YOLO version 5 for this project. First, download the YOLO model weights (5m model) from GitHub and upload them to Google Drive.
Next, update the data.yaml
file with the correct paths to the training, validation, and test images. Open the file and provide the exact paths. Here’s an example of how to update the file:
train: /content/drive/MyDrive/car_trucks_dataset/train/images
val: /content/drive/MyDrive/car_trucks_dataset/valid/images
test: /content/drive/MyDrive/car_trucks_dataset/test/images
nc: 4
names: ['car', 'sign', 'traffic light', 'truck']
Now, use the following code to train the model:
%cd /content/drive/MyDrive/car_trucks_dataset
!python train.py --img 256 --batch 4 --epochs 25 --data data.yaml --cfg yolov5m.yaml --weights '' --name car_trucks_results --cache
This code sets the image size to 256, batch size to 4, and trains the model for 25 epochs.
Testing the Model
After training, we need to test our model using the test images. Use the following code to make predictions:
!python detect.py --weights /content/drive/MyDrive/car_trucks_dataset/runs/train/car_trucks_results/weights/best.pt --img 256 --conf 0.4 --source /content/drive/MyDrive/car_trucks_dataset/test/images
This code uses the best model weights and the test images to make predictions.
Conclusion
Congratulations! You’ve successfully built an object detection project using YOLO version 5. You can now see the predicted images in your Google Drive under the runs/detect
folder. Open any image to see how well the model detects vehicles. By following these simple steps, you can train and test your own object detection models with YOLO. Keep experimenting with different datasets and model versions to improve your skills. Happy coding!
Leave a Reply