Yolo v5 Locally on a non-GPU machine

If you are new to Yolo (You Only Look Once), I strongly recommend you take a look at Vidhya’s article: Training a Custom Object Detection Model With Yolo-V5 His  Google Colab is an excellent and quick way to understand the steps that you would need to take to make a custom object detector (be sure to do a SAVE AS to your own Google Drive). Eliminates the need to wrestle with your own build environment.

But…

That is exactly what I am covering here. Unless you upgrade to Google Colab’s more premium levels, you will eventually want to maybe do all of the training and testing locally on your machine.

STEP 1: Choose the local machine

In my case, I am on Windows 10 with an affordable but powerful AMD Ryzen 7 laptop.

STEP 2: Choose the dev environment

I like Anaconda. At the writing of this guide, it is using Python 3.8. Navigate to Downloads / Individual Edition and install.

STEP 3: Create a virtual environment.

In “Type here to search” bar; type in “Anaconda Prompt”.

When the Anaconda Prompt window comes up, type in:

conda create -n yolov5env

When asked [y/n], type in:

y

STEP4: Installing the pre-requisite packages for Yolo v5.

Do each line one by one in Anaconda Prompt Window:

activate yolov5env
conda install pip
pip install tqdm
pip install scipy
pip install pyyaml
pip install matplotlib
pip install opencv-python==4.5.1.48
pip install pandas
pip install requests
pip install seaborn
pip install tensorboard

If your local machine does not have a GPU, then type in next (all on one line):

pip install torch==1.9.0+cpu torchvision==0.10.0+cpu -f https://download.pytorch.org/whl/torch_stable.html

Else if your machine has a GPU; then type in the following instead:

pip install torch==1.9.0+cu101 torchvision==0.10.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html

Tensorboard will create a libiomp5md.dll error. As a fix, I renamed one of my DLLs in the yolov5env:

C:\Users\<yourname>\anaconda3\envs\yolov5env\Library\bin\libiomp5md.dll

to

C:\Users\<yourname>\anaconda3\envs\yolov5env\Library\bin\libiomp5md_local.dll

STEP 5: Get Yolo v5

From a brower, go to: https://github.com/ultralytics/yolov5

Click on the green Code button and download the ZIP of the Ultralytics release of Yolo v5. Unzip into a working folder.

(Alternatively if you’ve install github on your PC, you can from Command Prompt type:

git clone https://github.com/ultralytics/yolov5

)

Contents of folder structure should look like:

STEP 6: Test the Yolo v5 Environment

Launch Anaconda Prompt Window if not already opened. Do the following line by line:

activate yolov5env
cd <path to where you unzipped your Yolo v5 folders>
python detect.py

You should see detect.py execute. It will automatically download the yolov5s.pt pre-trained weights that detect all sorts of objects. And it will automatically test in on two images bus.jpg and zidane.jpg. Looks like this if run successfully:

In Windows, you can go to the folder, in my case: yolov5-master\runs\detect\exp, to see the results. Here is what was in my folder:

bus.jpg
zidane.jpg

Pre-canned images are OK. But if you wanted turn the default model on to streaming video; type in the following:

python detect.py — source 0 –nosave

This will result in detect.py using your webcam as a source. NOTE: to exit, click on streaming window and hit ‘q’.

STEP 6b – Troubleshooting numpy and scipy errors

If re-running Step 6 shows errors related to numpy or scikpy; launch Anaconda Prompt Window if not already opened. Do the following line by line:

activate yolov5env
pip uninstall numpy
pip uninstall scipy
pip install numpy scipy

STEP 7: Custom Object Detection

After running Step 6, you will notice that your folder structure has changed a little bit. First, two new folders will appear:

The “runs” folder is the default location where object detected images and videos (in webcam case) will be stored. Unless otherwise specified, a subfolder under “runs” will be created with an “exp __” where __ = sequential number from 1 to as many runs as you wished stored.

The “yolov5” folder contains the downloaded convolutional neural network weights. When “detect.py” doesn’t have a weight specified; it will default to the one trained by the creator of Yolo v5.

Step 7a: Create two new folders in the yolov5-master path – tree structure looks like:

Step 7b: Decide on what you want to be identifying in an image or webcam video.

Let’s sense-make the decision: Vidhya got his dataset from Roboflow.  This dataset is again about detecting 7 different classes; fish, jellyfish, puffin, shark, starfish, and stingray.  I found in my run using a copy of the Google Collab notebook that doing 80 batches and 100 epochs resulted in weights that labeled everything as a puffin. I did another run of 80 batches and 1000 epochs on my local machine — and got just a little bit better.

I then re-vectored my effort into just detection fish (sharks and fish). And what I found was that with only one class, the weights tended to overdetect fish at 80 batches and 100 epochs. Example: any blob that was a different colors from the background was a fish.

I decided that I would detect fish and divers.

Step 7c: Getting images

I got fish images from  Roboflow. Found it easier to toss out all the labels. And also get rid of images that didn’t clearly have fish. I then added fish images from the internet. If you have Windows and are using Chrome, you can use Fatkun Batch Downloader.

I got my diver images using Fatkun Batch Downloader. Put all the images into the train/images folder.

Step 7d: Labeling the images

I then used Makesense.ai to do the tagging. Make sure to create the labels that you want. In my case, the diver images also had fish. Save your labels in the Yolo Format. Put all the labels into the train/labels.

My final count was 37 images+labels of divers and 128 images+labels of fish.

Step 7e: Creating the validation images and label

Select a subset of your train/images and matching train/labels; and copy them into:

valid/images/

valid/labels/

In my case, I used 18 images+labels of divers and 44 images+labels of fish. Tried to keep to the about 35% range that Vidhya’s original set used. Not explored yet if I could be using less.

Step 7f: Create data.yaml

In the main folder; in my case \yolov5-master\ — create a file using text editor called “data.yaml”.

In that file, insert the following and hit save:

train: ./train/images
val: ./valid/images

nc: 2
names: [‘fish’, ‘diver’]

Replace nc number with the number classes you are detecting. Replace names with the labels you used in step 7d.

Step 7g: Modify yolov model to fit your classes

Under your main folder is a folder called models. In my case, it was \yolov5-master\models\

Select yolov5s.yaml; the smallest of the weights and make a copy of it. I called mine “custom_yolov5s.yaml”.

Open your custom_yolov5s.yaml with a text editor. You will see:

#parameters
nc: 80 # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple
.
.
.

Under nc:, change to the number of classes you are detecting. Mine was 2. Save the yaml file.

Step 7h: Train the Yolo v5 model

Launch Anaconda Prompt Window if not already opened. Do the following line by line:

activate yolov5env
cd <path to where you unzipped your Yolo v5 folders>
python train.py ––img 416 ––batch 40 ––epochs 500 ––data ./data.yaml ––cfg ./models/custom_yolov5s.yaml

At this stage; you might encounter a couple of errors.

You will see the process of training happen for 500 epochs. The output when done will be saved to:

yolov5-master\runs\train\exp<sequential number>\

In my case, I had run 23 different trainings – so my the output for me looked like:

It is under the weights folder that you see:

These are what we can now plug into the detect.py as input parameters.

STEP 8 – Using the Trained Weights

Launch Anaconda Prompt Window if not already opened. Do the following line by line:

activate yolov5env
cd <path to where you unzipped your Yolo v5 folders>
python detect.py ––weights ./runs/train/exp<yourlatestnumber>/weights/best.pt ––conf 0.35 ––iou 0.7 ––img 416 ––source 0 ––nosave

Here is a snip of me trying to test it all out with just a pair of glasses and a toy fish.

You can also use test images if you want. Take some still images and place them into:

yolov5-master\test\images

Then from the Anaconda Prompt (already in yolov5env environment and cd to main folder):

python detect.py –weights ./runs/train/exp24/weights/best.pt ––conf 0.35 ––iou 0.7 ––img 416 ––source ./test/images

This will pick up any images and do the object detection markup on those still images. The default folder path is:

yolov5-master\runs\detect\exp<sequential number>

Will also save a *.txt text file with the coordinates of those rectangles under a folder called labels.