This article was automatically translated from Japanese using AI. The Japanese version is the authoritative version.
What is YOLOv5?
YOLO is an object detection algorithm. The name stands for “You Only Look Once.”
It differs somewhat from object tracking: it detects objects and identifies what they are.

In applications such as autonomous driving, YOLO detects and classifies objects the way a human would judge them—deciding whether something is a person, a pet, or a car.
The classification algorithm itself is complex, so I will not go into it here, but a quick search will turn up a great many sites that explain it.
New versions of YOLO have been appearing at a pace of roughly once every two years. So far we have had YOLO v1 through YOLOv5, and in August 2021 a new version, YOLO X, was announced.
The differences between versions involve substantial changes in the details, but as a rule accuracy improves with each new release.
In this post I will walk through the steps for installing YOLO v5 and verifying that it works—a version that offers sufficient accuracy and, now that plenty of information is available, is easy to get up and running.
Installation environment
OS: Windows 10 (macOS Monterey also worked)
Python: 3.9.7 (3.6 or later)
CUDA: 11.3
How to install
Creating a virtual environment
We will create a virtual environment using Anaconda.
If you do not have Anaconda installed, install it first.
Installing YOLOv5 pulls in quite a few packages, so I recommend installing it inside a virtual environment.
You can name the environment anything you like; here we will call it “Yolov5_env”.
Enter the following command in Anaconda Prompt.
conda create -n Yolov5_env python=3.9
This should create the virtual environment.
Activate it with the following command.
conda activate Yolov5_env
Installing CUDA
First, if this is your first time installing machine learning libraries and you plan to train YOLO on a GPU, install CUDA. (Training on a CPU takes an absurdly long time, so I do not recommend it.)
CUDA can be downloaded from the official site.
As for which CUDA version to use, I suggest checking which CUDA versions PyTorch supports and installing that version.
PyTorch official site
To install an older version, click Download now and then, on the screen that appears, use Archive of Previous CUDA Releases under Resources near the bottom of the page.
The latest version may well work too, but for now, matching the version to PyTorch should let you run everything without trouble.
To check whether it installed, on Windows open “Edit the system environment variables,” go to Advanced > Environment Variables, and
look for a path beginning with CUDA_PATH.
The number after the V indicates the version.
Installing PyTorch
Before installing, update pip.
Activate the virtual environment in Anaconda Prompt and run the following command.
python -m pip install --upgrade pip
Go to the PyTorch official site and select the installation options that match your environment.
One thing to watch out for is the Package field: be sure to choose pip here.
If you install with conda, you will run into errors later on.

Copy and paste the command shown under “Run this Command” to install PyTorch in your virtual environment.
Once the installation finishes, use the following command to check that PyTorch is installed.
pip list
If torch appears in the list, you are all set!
As a quick sanity check, launch python and run the following commands.
import torch
print(torch.cuda.is_available())
If the output is True, the installation succeeded and torch is ready to run on the GPU.
(On a Mac, or on a computer without a GPU, you will see False, but as long as the import goes through you are fine.)
If you get an error, go back and review the installation.
Installing YOLOv5
Download (clone) the YOLOv5 files from GitHub.
git clone https://github.com/ultralytics/yolov5
If you cannot use the git command, either install git or download the files from the YOLOv5 GitHub repository.
Next, run the following command in your virtual environment in Anaconda Prompt.
cd yolov5
pip install -r requirements.txt
The first command moves you into the yolov5 folder you downloaded from GitHub. If you installed it with the git command, this will work as is; if you downloaded it directly from the website, you will have to navigate to that directory yourself.
The downloaded yolov5 folder contains a file called “requirements.txt” that lists the necessary packages. The second command opens this file in read-only mode and installs the packages listed in it.
Once the installation is finished, let’s check which packages were installed.
pip list
You will probably see that quite a few packages were installed.
With that, the YOLOv5 installation is complete for now.
Running it
To check that everything works, let’s try it with the data that comes with the yolov5 folder.
Open Anaconda Prompt, activate the virtual environment, and use the cd command to move into the yolov5 folder.
Then run the following command.
python detect.py --source ./data/images/ --weights yolov5s.pt --conf 0.4
detect.py is the program that performs object detection using YOLO.
With source you specify the path to the folder containing the material you want to run detection on.
Here I used the images included in yolov5.

With weights you choose which model to use.
Here we use yolov5.pt.
conf sets the likelihood threshold at which an object is recognized.
This is a term you will run into often when studying machine learning.
If you are not sure, leaving it as is should be fine.
After running it, you should find the annotated images in runsdetectexp inside the yolov5 folder.
This is how object detection is done in YOLOv5.
Errors I ran into
・ModuleNotFoundError: No module named ‘torch’
You may see this error when trying to run YOLO.
It means PyTorch is not available, i.e., the module cannot be found.
If you get this error, first check whether torch is installed in your virtual environment.
pip list
If torch is not listed, install PyTorch.
If it is there but things still don’t work and you’re not sure why, start Python, import PyTorch, and check whether it is usable.
import torch
torch.cuda.is_acailable()
If it is available, True will be returned; if not, you will likely see the error above.
One possible cause of the error is that PyTorch was installed with the conda command.
If PyTorch is installed via conda while the other packages are installed via pip, the error above can occur.
In that case, uninstall torch with the conda command and reinstall it with pip.
“Module not found” problems often arise when you have mixed up pip and conda when installing.
Here I have used pip throughout, but it’s a good idea to always keep track of which command you used to install something.
Links
Anaconda
CUDA
PyTorch
YOLOv5 GitHub
YOLO homepage
Bonus
For those who aren’t sure which computer to buy, I’ve decided to offer PC purchase consultations on Coconala!
あなたの要望に合わせてパソコンを選び、提案します パソコン選びに困っている方々へ!様々な目的に対応できます!I often pick out computers and give advice about them, and many friends have told me I could make money doing PC consultations.
Inspired by that, I figured I’d give it a try!
I’ll suggest a purchase that fits how you plan to use the computer and your budget.
In particular, I’ve chosen and used many computers for machine learning.
And if you’d like, I’m happy to also advise you on what to look for when buying a computer in the future.
Computers I’ve picked out so far include analysis machines for the lab, everyday-use machines, PCs for game streaming, PCs for incoming university students, PCs capable of running CAD for architecture students, and simple stopgap machines.
I use both Mac and Windows, so I can discuss and recommend either!
Please give it a try!
