Train Yolov5 with Roboflow¶
What is YOLO¶
YOLO (You Only Look Once) is a technique for detecting objects in images. It's a unique approach that differs from earlier methods, which would examine different parts of the image multiple times and use image classification techniques.
YOLO, on the other hand, looks at the entire image just once and processes it through the network once, making it much faster. This speed is why it's gained popularity.
Other object detection frameworks like Faster R-CNN and SSD are also widely used. In this article, we'll explore how to use a pre-trained YOLO model with OpenCV to start detecting objects immediately and deploy it with fastapi.
For this part we will leverage the official roboflow tutorial but it's important to know what is YOLO.
For the rest of this tutorial do not forget to add your dataset and robowflow API key 😇
!git clone https://github.com/ultralytics/yolov5 # clone repo
!pip install -U -r yolov5/requirements.txt # install dependencies
Cloning into 'yolov5'... remote: Enumerating objects: 16656, done. remote: Counting objects: 100% (7/7), done. remote: Compressing objects: 100% (7/7), done. remote: Total 16656 (delta 1), reused 5 (delta 0), pack-reused 16649 Receiving objects: 100% (16656/16656), 15.14 MiB | 24.22 MiB/s, done. Resolving deltas: 100% (11441/11441), done. Collecting gitpython>=3.1.30 (from -r yolov5/requirements.txt (line 5)) Downloading GitPython-3.1.43-py3-none-any.whl (207 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 207.3/207.3 kB 1.8 MB/s eta 0:00:00 Requirement already satisfied: matplotlib>=3.3 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 6)) (3.7.1) Collecting matplotlib>=3.3 (from -r yolov5/requirements.txt (line 6)) Downloading matplotlib-3.9.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.3/8.3 MB 39.3 MB/s eta 0:00:00 Requirement already satisfied: numpy>=1.23.5 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 7)) (1.25.2) Collecting numpy>=1.23.5 (from -r yolov5/requirements.txt (line 7)) Downloading numpy-1.26.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (18.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 18.2/18.2 MB 55.9 MB/s eta 0:00:00 Requirement already satisfied: opencv-python>=4.1.1 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 8)) (4.8.0.76) Collecting opencv-python>=4.1.1 (from -r yolov5/requirements.txt (line 8)) Downloading opencv_python-4.9.0.80-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (62.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.2/62.2 MB 9.5 MB/s eta 0:00:00 Collecting pillow>=10.3.0 (from -r yolov5/requirements.txt (line 9)) Downloading pillow-10.3.0-cp310-cp310-manylinux_2_28_x86_64.whl (4.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.5/4.5 MB 78.3 MB/s eta 0:00:00 Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 10)) (5.9.5) Collecting psutil (from -r yolov5/requirements.txt (line 10)) Downloading psutil-5.9.8-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (288 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 288.2/288.2 kB 31.2 MB/s eta 0:00:00 Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 11)) (6.0.1) Collecting requests>=2.32.0 (from -r yolov5/requirements.txt (line 12)) Downloading requests-2.32.3-py3-none-any.whl (64 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.9/64.9 kB 9.2 MB/s eta 0:00:00 Requirement already satisfied: scipy>=1.4.1 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 13)) (1.11.4) Collecting scipy>=1.4.1 (from -r yolov5/requirements.txt (line 13)) Downloading scipy-1.13.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 38.6/38.6 MB 14.8 MB/s eta 0:00:00 Collecting thop>=0.1.1 (from -r yolov5/requirements.txt (line 14)) Downloading thop-0.1.1.post2209072238-py3-none-any.whl (15 kB) Requirement already satisfied: torch>=1.8.0 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 15)) (2.3.0+cu121) Requirement already satisfied: torchvision>=0.9.0 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 16)) (0.18.0+cu121) Requirement already satisfied: tqdm>=4.64.0 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 17)) (4.66.4) Collecting ultralytics>=8.0.232 (from -r yolov5/requirements.txt (line 18)) Downloading ultralytics-8.2.27-py3-none-any.whl (779 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 779.6/779.6 kB 69.3 MB/s eta 0:00:00 Requirement already satisfied: pandas>=1.1.4 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 27)) (2.0.3) Collecting pandas>=1.1.4 (from -r yolov5/requirements.txt (line 27)) Downloading pandas-2.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 13.0/13.0 MB 53.9 MB/s eta 0:00:00 Requirement already satisfied: seaborn>=0.11.0 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 28)) (0.13.1) Collecting seaborn>=0.11.0 (from -r yolov5/requirements.txt (line 28)) Downloading seaborn-0.13.2-py3-none-any.whl (294 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 294.9/294.9 kB 38.0 MB/s eta 0:00:00 Requirement already satisfied: setuptools>=65.5.1 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 42)) (67.7.2) Collecting setuptools>=65.5.1 (from -r yolov5/requirements.txt (line 42)) Using cached setuptools-70.0.0-py3-none-any.whl (863 kB) Requirement already satisfied: wheel>=0.38.0 in /usr/local/lib/python3.10/dist-packages (from -r yolov5/requirements.txt (line 50)) (0.43.0) Collecting gitdb<5,>=4.0.1 (from gitpython>=3.1.30->-r yolov5/requirements.txt (line 5)) Downloading gitdb-4.0.11-py3-none-any.whl (62 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.7/62.7 kB 10.0 MB/s eta 0:00:00 Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3->-r yolov5/requirements.txt (line 6)) (1.2.1) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3->-r yolov5/requirements.txt (line 6)) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3->-r yolov5/requirements.txt (line 6)) (4.51.0) Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3->-r yolov5/requirements.txt (line 6)) (1.4.5) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3->-r yolov5/requirements.txt (line 6)) (24.0) Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3->-r yolov5/requirements.txt (line 6)) (3.1.2) Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=3.3->-r yolov5/requirements.txt (line 6)) (2.8.2) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.0->-r yolov5/requirements.txt (line 12)) (3.3.2) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.0->-r yolov5/requirements.txt (line 12)) (3.7) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.0->-r yolov5/requirements.txt (line 12)) (2.0.7) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.0->-r yolov5/requirements.txt (line 12)) (2024.2.2) Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) (3.14.0) Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) (4.11.0) Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) (1.12) Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) (3.3) Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) (3.1.4) Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) (2023.6.0) Collecting nvidia-cuda-nvrtc-cu12==12.1.105 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB) Collecting nvidia-cuda-runtime-cu12==12.1.105 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB) Collecting nvidia-cuda-cupti-cu12==12.1.105 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB) Collecting nvidia-cudnn-cu12==8.9.2.26 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_cudnn_cu12-8.9.2.26-py3-none-manylinux1_x86_64.whl (731.7 MB) Collecting nvidia-cublas-cu12==12.1.3.1 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB) Collecting nvidia-cufft-cu12==11.0.2.54 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB) Collecting nvidia-curand-cu12==10.3.2.106 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB) Collecting nvidia-cusolver-cu12==11.4.5.107 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB) Collecting nvidia-cusparse-cu12==12.1.0.106 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl (196.0 MB) Collecting nvidia-nccl-cu12==2.20.5 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_nccl_cu12-2.20.5-py3-none-manylinux2014_x86_64.whl (176.2 MB) Collecting nvidia-nvtx-cu12==12.1.105 (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Using cached nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (99 kB) Requirement already satisfied: triton==2.3.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.8.0->-r yolov5/requirements.txt (line 15)) (2.3.0) Collecting nvidia-nvjitlink-cu12 (from nvidia-cusolver-cu12==11.4.5.107->torch>=1.8.0->-r yolov5/requirements.txt (line 15)) Downloading nvidia_nvjitlink_cu12-12.5.40-py3-none-manylinux2014_x86_64.whl (21.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 21.3/21.3 MB 46.4 MB/s eta 0:00:00 Requirement already satisfied: py-cpuinfo in /usr/local/lib/python3.10/dist-packages (from ultralytics>=8.0.232->-r yolov5/requirements.txt (line 18)) (9.0.0) Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.1.4->-r yolov5/requirements.txt (line 27)) (2023.4) Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas>=1.1.4->-r yolov5/requirements.txt (line 27)) (2024.1) Collecting smmap<6,>=3.0.1 (from gitdb<5,>=4.0.1->gitpython>=3.1.30->-r yolov5/requirements.txt (line 5)) Downloading smmap-5.0.1-py3-none-any.whl (24 kB) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib>=3.3->-r yolov5/requirements.txt (line 6)) (1.16.0) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.8.0->-r yolov5/requirements.txt (line 15)) (2.1.5) Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=1.8.0->-r yolov5/requirements.txt (line 15)) (1.3.0) Installing collected packages: smmap, setuptools, requests, psutil, pillow, nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, numpy, scipy, pandas, opencv-python, nvidia-cusparse-cu12, nvidia-cudnn-cu12, gitdb, nvidia-cusolver-cu12, matplotlib, gitpython, seaborn, thop, ultralytics Attempting uninstall: setuptools Found existing installation: setuptools 67.7.2 Uninstalling setuptools-67.7.2: Successfully uninstalled setuptools-67.7.2 Attempting uninstall: requests Found existing installation: requests 2.31.0 Uninstalling requests-2.31.0: Successfully uninstalled requests-2.31.0 Attempting uninstall: psutil Found existing installation: psutil 5.9.5 Uninstalling psutil-5.9.5: Successfully uninstalled psutil-5.9.5 Attempting uninstall: pillow Found existing installation: Pillow 9.4.0 Uninstalling Pillow-9.4.0: Successfully uninstalled Pillow-9.4.0 Attempting uninstall: numpy Found existing installation: numpy 1.25.2 Uninstalling numpy-1.25.2: Successfully uninstalled numpy-1.25.2 Attempting uninstall: scipy Found existing installation: scipy 1.11.4 Uninstalling scipy-1.11.4: Successfully uninstalled scipy-1.11.4 Attempting uninstall: pandas Found existing installation: pandas 2.0.3 Uninstalling pandas-2.0.3: Successfully uninstalled pandas-2.0.3 Attempting uninstall: opencv-python Found existing installation: opencv-python 4.8.0.76 Uninstalling opencv-python-4.8.0.76: Successfully uninstalled opencv-python-4.8.0.76 Attempting uninstall: matplotlib Found existing installation: matplotlib 3.7.1 Uninstalling matplotlib-3.7.1: Successfully uninstalled matplotlib-3.7.1 Attempting uninstall: seaborn Found existing installation: seaborn 0.13.1 Uninstalling seaborn-0.13.1: Successfully uninstalled seaborn-0.13.1 ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. ipython 7.34.0 requires jedi>=0.16, which is not installed. cudf-cu12 24.4.1 requires pandas<2.2.2dev0,>=2.0, but you have pandas 2.2.2 which is incompatible. google-colab 1.0.0 requires pandas==2.0.3, but you have pandas 2.2.2 which is incompatible. google-colab 1.0.0 requires requests==2.31.0, but you have requests 2.32.3 which is incompatible. imageio 2.31.6 requires pillow<10.1.0,>=8.3.2, but you have pillow 10.3.0 which is incompatible. Successfully installed gitdb-4.0.11 gitpython-3.1.43 matplotlib-3.9.0 numpy-1.26.4 nvidia-cublas-cu12-12.1.3.1 nvidia-cuda-cupti-cu12-12.1.105 nvidia-cuda-nvrtc-cu12-12.1.105 nvidia-cuda-runtime-cu12-12.1.105 nvidia-cudnn-cu12-8.9.2.26 nvidia-cufft-cu12-11.0.2.54 nvidia-curand-cu12-10.3.2.106 nvidia-cusolver-cu12-11.4.5.107 nvidia-cusparse-cu12-12.1.0.106 nvidia-nccl-cu12-2.20.5 nvidia-nvjitlink-cu12-12.5.40 nvidia-nvtx-cu12-12.1.105 opencv-python-4.9.0.80 pandas-2.2.2 pillow-10.3.0 psutil-5.9.8 requests-2.32.3 scipy-1.13.1 seaborn-0.13.2 setuptools-70.0.0 smmap-5.0.1 thop-0.1.1.post2209072238 ultralytics-8.2.27
import torch
from IPython.display import Image # for displaying images
print('torch %s %s' % (torch.__version__, torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'CPU'))
torch 2.3.0+cu121 _CudaDeviceProperties(name='Tesla T4', major=7, minor=5, total_memory=15102MB, multi_processor_count=40)
%cd /content/yolov5
/content/yolov5
Dowload your data¶
For this step you need to put your API key here and choose the yolov5 pytorch format ⚙️
!pip install roboflow
from roboflow import Roboflow
rf = Roboflow(api_key="**************") #API key here
project = rf.workspace("fairy").project("chess-sample-xa7pe")
version = project.version(2)
dataset = version.download("yolov5")
Requirement already satisfied: roboflow in /usr/local/lib/python3.10/dist-packages (1.1.30) Requirement already satisfied: certifi==2023.7.22 in /usr/local/lib/python3.10/dist-packages (from roboflow) (2023.7.22) Requirement already satisfied: chardet==4.0.0 in /usr/local/lib/python3.10/dist-packages (from roboflow) (4.0.0) Requirement already satisfied: cycler==0.10.0 in /usr/local/lib/python3.10/dist-packages (from roboflow) (0.10.0) Requirement already satisfied: idna==2.10 in /usr/local/lib/python3.10/dist-packages (from roboflow) (2.10) Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.10/dist-packages (from roboflow) (1.4.5) Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from roboflow) (3.9.0) Requirement already satisfied: numpy>=1.18.5 in /usr/local/lib/python3.10/dist-packages (from roboflow) (1.26.4) Requirement already satisfied: opencv-python-headless==4.8.0.74 in /usr/local/lib/python3.10/dist-packages (from roboflow) (4.8.0.74) Requirement already satisfied: Pillow>=7.1.2 in /usr/local/lib/python3.10/dist-packages (from roboflow) (10.3.0) Requirement already satisfied: python-dateutil in /usr/local/lib/python3.10/dist-packages (from roboflow) (2.8.2) Requirement already satisfied: python-dotenv in /usr/local/lib/python3.10/dist-packages (from roboflow) (1.0.1) Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from roboflow) (2.32.3) Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from roboflow) (1.16.0) Requirement already satisfied: urllib3>=1.26.6 in /usr/local/lib/python3.10/dist-packages (from roboflow) (2.0.7) Requirement already satisfied: tqdm>=4.41.0 in /usr/local/lib/python3.10/dist-packages (from roboflow) (4.66.4) Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.10/dist-packages (from roboflow) (6.0.1) Requirement already satisfied: requests-toolbelt in /usr/local/lib/python3.10/dist-packages (from roboflow) (1.0.0) Requirement already satisfied: python-magic in /usr/local/lib/python3.10/dist-packages (from roboflow) (0.4.27) Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->roboflow) (1.2.1) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->roboflow) (4.51.0) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->roboflow) (24.0) Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->roboflow) (3.1.2) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->roboflow) (3.3.2) loading Roboflow workspace... loading Roboflow project...
Downloading Dataset Version Zip in Chess-Sample-2 to yolov5pytorch:: 100%|██████████| 380641/380641 [00:06<00:00, 61255.39it/s]
Extracting Dataset Version Zip to Chess-Sample-2 in yolov5pytorch:: 100%|██████████| 14060/14060 [00:03<00:00, 4179.50it/s]
# this is the YAML file Roboflow wrote for us that we're loading into this notebook with our data
%cat {dataset.location}/data.yaml
names: - black-bishop - black-king - black-knight - black-pawn - black-queen - black-rook - white-bishop - white-king - white-knight - white-pawn - white-queen - white-rook nc: 12 roboflow: license: CC BY 4.0 project: chess-sample-xa7pe url: https://universe.roboflow.com/fairy/chess-sample-xa7pe/dataset/2 version: 2 workspace: fairy test: ../test/images train: Chess-Sample-2/train/images val: Chess-Sample-2/valid/images
# define number of classes based on YAML
import yaml
with open(dataset.location + "/data.yaml", 'r') as stream:
num_classes = str(yaml.safe_load(stream)['nc'])
#this is the model configuration we will use for our tutorial
%cat /content/yolov5/models/yolov5s.yaml
# YOLOv5 🚀 by Ultralytics, AGPL-3.0 license # Parameters nc: 80 # number of classes depth_multiple: 0.33 # model depth multiple width_multiple: 0.50 # layer channel multiple anchors: - [10, 13, 16, 30, 33, 23] # P3/8 - [30, 61, 62, 45, 59, 119] # P4/16 - [116, 90, 156, 198, 373, 326] # P5/32 # YOLOv5 v6.0 backbone backbone: # [from, number, module, args] [ [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 [-1, 3, C3, [128]], [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 [-1, 6, C3, [256]], [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 [-1, 9, C3, [512]], [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 [-1, 3, C3, [1024]], [-1, 1, SPPF, [1024, 5]], # 9 ] # YOLOv5 v6.0 head head: [ [-1, 1, Conv, [512, 1, 1]], [-1, 1, nn.Upsample, [None, 2, "nearest"]], [[-1, 6], 1, Concat, [1]], # cat backbone P4 [-1, 3, C3, [512, False]], # 13 [-1, 1, Conv, [256, 1, 1]], [-1, 1, nn.Upsample, [None, 2, "nearest"]], [[-1, 4], 1, Concat, [1]], # cat backbone P3 [-1, 3, C3, [256, False]], # 17 (P3/8-small) [-1, 1, Conv, [256, 3, 2]], [[-1, 14], 1, Concat, [1]], # cat head P4 [-1, 3, C3, [512, False]], # 20 (P4/16-medium) [-1, 1, Conv, [512, 3, 2]], [[-1, 10], 1, Concat, [1]], # cat head P5 [-1, 3, C3, [1024, False]], # 23 (P5/32-large) [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) ]
#customize iPython writefile so we can write variables
from IPython.core.magic import register_line_cell_magic
@register_line_cell_magic
def writetemplate(line, cell):
with open(line, 'w') as f:
f.write(cell.format(**globals()))
%%writetemplate /content/yolov5/models/custom_yolov5s.yaml
# parameters
nc: {num_classes} # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple
# anchors
anchors:
- [10,13, 16,30, 33,23] # P3/8
- [30,61, 62,45, 59,119] # P4/16
- [116,90, 156,198, 373,326] # P5/32
# YOLOv5 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Focus, [64, 3]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, BottleneckCSP, [128]],
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 9, BottleneckCSP, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, BottleneckCSP, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 1, SPP, [1024, [5, 9, 13]]],
[-1, 3, BottleneckCSP, [1024, False]], # 9
]
# YOLOv5 head
head:
[[-1, 1, Conv, [512, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, BottleneckCSP, [512, False]], # 13
[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, BottleneckCSP, [256, False]], # 17 (P3/8-small)
[-1, 1, Conv, [256, 3, 2]],
[[-1, 14], 1, Concat, [1]], # cat head P4
[-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium)
[-1, 1, Conv, [512, 3, 2]],
[[-1, 10], 1, Concat, [1]], # cat head P5
[-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large)
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]
# train yolov5s on custom data for 100 epochs
# time its performance
%%time
#%cd /content/yolov5/
!python train.py --img 416 --batch 16 --epochs 5 --data {dataset.location}/data.yaml --cfg ./models/custom_yolov5s.yaml --weights '' --name yolov5s_results --cache
2024-06-02 17:15:37.132157: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2024-06-02 17:15:37.132226: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2024-06-02 17:15:37.134394: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered train: weights=, cfg=./models/custom_yolov5s.yaml, data=/content/yolov5/Chess-Sample-2/data.yaml, hyp=data/hyps/hyp.scratch-low.yaml, epochs=5, batch_size=16, imgsz=416, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, noplots=False, evolve=None, evolve_population=data/hyps, resume_evolve=None, bucket=, cache=ram, image_weights=False, device=, multi_scale=False, single_cls=False, optimizer=SGD, sync_bn=False, workers=8, project=runs/train, name=yolov5s_results, exist_ok=False, quad=False, cos_lr=False, label_smoothing=0.0, patience=100, freeze=[0], save_period=-1, seed=0, local_rank=-1, entity=None, upload_dataset=False, bbox_interval=-1, artifact_alias=latest, ndjson_console=False, ndjson_file=False github: up to date with https://github.com/ultralytics/yolov5 ✅ YOLOv5 🚀 v7.0-318-gc0380fd8 Python-3.10.12 torch-2.3.0+cu121 CUDA:0 (Tesla T4, 15102MiB) hyperparameters: lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.5, cls_pw=1.0, obj=1.0, obj_pw=1.0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0 Comet: run 'pip install comet_ml' to automatically track and visualize YOLOv5 🚀 runs in Comet TensorBoard: Start with 'tensorboard --logdir runs/train', view at http://localhost:6006/ from n params module arguments 0 -1 1 3520 models.common.Focus [3, 32, 3] 1 -1 1 18560 models.common.Conv [32, 64, 3, 2] 2 -1 1 19904 models.common.BottleneckCSP [64, 64, 1] 3 -1 1 73984 models.common.Conv [64, 128, 3, 2] 4 -1 3 161152 models.common.BottleneckCSP [128, 128, 3] 5 -1 1 295424 models.common.Conv [128, 256, 3, 2] 6 -1 3 641792 models.common.BottleneckCSP [256, 256, 3] 7 -1 1 1180672 models.common.Conv [256, 512, 3, 2] 8 -1 1 656896 models.common.SPP [512, 512, [5, 9, 13]] 9 -1 1 1248768 models.common.BottleneckCSP [512, 512, 1, False] 10 -1 1 131584 models.common.Conv [512, 256, 1, 1] 11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 12 [-1, 6] 1 0 models.common.Concat [1] 13 -1 1 378624 models.common.BottleneckCSP [512, 256, 1, False] 14 -1 1 33024 models.common.Conv [256, 128, 1, 1] 15 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] 16 [-1, 4] 1 0 models.common.Concat [1] 17 -1 1 95104 models.common.BottleneckCSP [256, 128, 1, False] 18 -1 1 147712 models.common.Conv [128, 128, 3, 2] 19 [-1, 14] 1 0 models.common.Concat [1] 20 -1 1 313088 models.common.BottleneckCSP [256, 256, 1, False] 21 -1 1 590336 models.common.Conv [256, 256, 3, 2] 22 [-1, 10] 1 0 models.common.Concat [1] 23 -1 1 1248768 models.common.BottleneckCSP [512, 512, 1, False] 24 [17, 20, 23] 1 45849 models.yolo.Detect [12, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]] custom_YOLOv5s summary: 233 layers, 7284761 parameters, 7284761 gradients AMP: checks passed ✅ optimizer: SGD(lr=0.01) with parameter groups 59 weight(decay=0.0), 70 weight(decay=0.0005), 62 bias albumentations: Blur(p=0.01, blur_limit=(3, 7)), MedianBlur(p=0.01, blur_limit=(3, 7)), ToGray(p=0.01), CLAHE(p=0.01, clip_limit=(1, 4.0), tile_grid_size=(8, 8)) train: Scanning /content/yolov5/Chess-Sample-2/train/labels.cache... 6192 images, 0 backgrounds, 0 corrupt: 100% 6192/6192 [00:00<?, ?it/s] train: Caching images (3.0GB ram): 100% 6192/6192 [00:26<00:00, 234.03it/s] /usr/lib/python3.10/multiprocessing/popen_fork.py:66: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock. self.pid = os.fork() val: Scanning /content/yolov5/Chess-Sample-2/valid/labels.cache... 448 images, 0 backgrounds, 0 corrupt: 100% 448/448 [00:00<?, ?it/s] val: Caching images (0.2GB ram): 100% 448/448 [00:01<00:00, 371.13it/s] AutoAnchor: 2.00 anchors/target, 1.000 Best Possible Recall (BPR). Current anchors are a good fit to dataset ✅ Plotting labels to runs/train/yolov5s_results5/labels.jpg... Image sizes 416 train, 416 val Using 2 dataloader workers Logging results to runs/train/yolov5s_results5 Starting training for 5 epochs... Epoch GPU_mem box_loss obj_loss cls_loss Instances Size 0/4 1.7G 0.06481 0.02576 0.05378 50 416: 100% 387/387 [01:18<00:00, 4.91it/s] Class Images Instances P R mAP50 mAP50-95: 100% 14/14 [00:04<00:00, 3.02it/s] all 448 448 0.0126 0.0809 0.0136 0.00706 Epoch GPU_mem box_loss obj_loss cls_loss Instances Size 1/4 2G 0.05286 0.02689 0.05057 51 416: 100% 387/387 [01:11<00:00, 5.41it/s] Class Images Instances P R mAP50 mAP50-95: 100% 14/14 [00:04<00:00, 3.18it/s] all 448 448 0.00342 0.997 0.0771 0.0387 Epoch GPU_mem box_loss obj_loss cls_loss Instances Size 2/4 2G 0.0354 0.03037 0.04835 49 416: 100% 387/387 [01:09<00:00, 5.58it/s] Class Images Instances P R mAP50 mAP50-95: 100% 14/14 [00:05<00:00, 2.45it/s] all 448 448 0.837 0.0817 0.0662 0.0533 Epoch GPU_mem box_loss obj_loss cls_loss Instances Size 3/4 2G 0.02691 0.02693 0.04791 40 416: 100% 387/387 [01:08<00:00, 5.64it/s] Class Images Instances P R mAP50 mAP50-95: 100% 14/14 [00:04<00:00, 3.34it/s] all 448 448 0.838 0.181 0.117 0.0869 Epoch GPU_mem box_loss obj_loss cls_loss Instances Size 4/4 2G 0.02304 0.02487 0.04941 49 416: 100% 387/387 [01:10<00:00, 5.47it/s] Class Images Instances P R mAP50 mAP50-95: 100% 14/14 [00:03<00:00, 3.85it/s] all 448 448 0.86 0.182 0.118 0.101 5 epochs completed in 0.107 hours. Optimizer stripped from runs/train/yolov5s_results5/weights/last.pt, 14.9MB Optimizer stripped from runs/train/yolov5s_results5/weights/best.pt, 14.9MB Validating runs/train/yolov5s_results5/weights/best.pt... Fusing layers... custom_YOLOv5s summary: 182 layers, 7276185 parameters, 0 gradients Class Images Instances P R mAP50 mAP50-95: 100% 14/14 [00:07<00:00, 1.82it/s] all 448 448 0.86 0.182 0.118 0.101 black-bishop 448 16 1 0 0.0655 0.053 black-king 448 32 1 0 0.0982 0.08 black-knight 448 16 1 0 0.0366 0.0294 black-pawn 448 96 0.209 1 0.247 0.22 black-rook 448 32 1 0 0.136 0.121 white-bishop 448 32 1 0 0.0753 0.061 white-king 448 32 1 0 0.0744 0.0624 white-knight 448 16 1 0 0.0441 0.0397 white-pawn 448 112 0.247 1 0.308 0.277 white-queen 448 32 1 0 0.0829 0.0743 white-rook 448 32 1 0 0.13 0.0886 Results saved to runs/train/yolov5s_results5 CPU times: user 5.36 s, sys: 632 ms, total: 5.99 s Wall time: 7min 28s
!ls runs/train/yolov5s_results5
confusion_matrix.png PR_curve.png val_batch0_pred.jpg events.out.tfevents.1717348541.75edf56b9c40.3632.0 R_curve.png val_batch1_labels.jpg F1_curve.png results.csv val_batch1_pred.jpg hyp.yaml results.png val_batch2_labels.jpg labels_correlogram.jpg train_batch0.jpg val_batch2_pred.jpg labels.jpg train_batch1.jpg weights opt.yaml train_batch2.jpg P_curve.png val_batch0_labels.jpg
from IPython.display import Image, clear_output # to display images
Image(filename='runs/train/yolov5s_results5/results.png', width=1000) # view results.png
As you can see our results are not very good but we used only 5 epochs because we are GPU poor, now you have all the keys to train your own YOLO on your dataset 🥳