Running Ultralytics YOLOv8 on Ubuntu 22.04/WSL2 with GPGPU

This is a note on running object detection using Ultralytics YOLOv8 on Ubuntu 22.04/WSL2/Windows 11.

Object detection was performed using trained weight data. I confirmed that it runs in a configuration that uses the GPGPU.

The program was run on a desktop computer ESPRIMO WD2/H2 with NVIDIA GeForce GTX 1650 (GPGPU).

1. Install WSL2 (Windows Subsystem for Linux 2) on Windows 11

Start Power Shell with the administrator privilege and execute the following command.

PS C:\Users\username> wsl --install

After installation is complete, restart the Windows 11 PC.
After rebooting, Ubuntu will start and the user name and password will be asked. This will complete the installation.

The following steps were performed by running Ubuntu on WSL2.

2. Installing python3 and pip

2.1. I installed python3 and pip with the following commands.

fukagai@ESPRIMOWD2H2:~$ sudo apt-get update
fukagai@ESPRIMOWD2H2:~$ sudo apt install python3
fukagai@ESPRIMOWD2H2:~$ sudo apt install pip

2.2. I checked the installed python3 and pip versions with the following commands.

fukagai@ESPRIMOWD2H2:~$ python3 --version
Python 3.10.12
fukagai@ESPRIMOWD2H2:~$ pip --version
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

3. Install and check the operation of Ultralytics

I followed Ultralytics’ Quickstart and executed the object detection program using trained weight data.

3.1. Installing Ultralytics

fukagai@ESPRIMOWD2H2:~$ pip install ultralytics
Defaulting to user installation because normal site-packages is not writeable
Collecting ultralytics
  Downloading ultralytics-8.2.27-py3-none-any.whl (779 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 779.6/779.6 KB 10.9 MB/s eta 0:00:00
Requirement already satisfied: pyyaml>=5.3.1 in /usr/lib/python3/dist-packages (from ultralytics) (5.4.1)
...
  WARNING: The scripts ultralytics and yolo are installed in '/home/fukagai/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed MarkupSafe-2.1.5 certifi-2024.2.2 charset-normalizer-3.3.2 contourpy-1.2.1 cycler-0.12.1 filelock-3.14.0 fonttools-4.53.0 fsspec-2024.5.0 idna-3.7 jinja2-3.1.4 kiwisolver-1.4.5 matplotlib-3.9.0 mpmath-1.3.0 networkx-3.3 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 packaging-24.0 pandas-2.2.2 pillow-10.3.0 psutil-5.9.8 py-cpuinfo-9.0.0 python-dateutil-2.9.0.post0 pytz-2024.1 requests-2.32.3 scipy-1.13.1 seaborn-0.13.2 sympy-1.12.1 thop-0.1.1.post2209072238 torch-2.3.0 torchvision-0.18.0 tqdm-4.66.4 triton-2.3.0 typing-extensions-4.12.0 tzdata-2024.1 ultralytics-8.2.27 urllib3-2.2.1

3.2. Add ~/.local/bin to the PATH environment variable

I added the following line to the end of ~/.bashrc.

export PATH=$PATH:~/.local/bin

The following command was executed to reflect the settings.

fukagai@ESPRIMOWD2H2:~$ source .bashrc

3.3. Execute object detection commands using trained weight data

When executing the command to detect objects in the specified image, the following error message was displayed.

fukagai@ESPRIMOWD2H2:~$ yolo detect predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg'
Traceback (most recent call last):
  File "/home/fukagai/.local/bin/yolo", line 5, in <module>
    from ultralytics.cfg import entrypoint
  ...
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

So, I installed libopencv-dev by running the following command.

fukagai@ESPRIMOWD2H2:~$ sudo apt -y install libopencv-dev

The following object detection command was executed to obtain the result of object detection. The following warning message was displayed when the command was executed.

fukagai@ESPRIMOWD2H2:~$ yolo detect predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg'
Downloading https://github.com/ultralytics/assets/releases/download/v8.2.0/yolov8n.pt to 'yolov8n.pt'...
100%|███████████████████████████████████████████████████████████████████████████████████████████| 6.23M/6.23M [00:00<00:00, 63.7MB/s]
Ultralytics YOLOv8.2.27 🚀 Python-3.10.12 torch-2.3.0+cu121 CUDA:0 (NVIDIA GeForce GTX 1650, 4096MiB)
YOLOv8n summary (fused): 168 layers, 3151904 parameters, 0 gradients, 8.7 GFLOPs

Downloading https://ultralytics.com/images/bus.jpg to 'bus.jpg'...
100%|█████████████████████████████████████████████████████████████████████████████████████████████| 476k/476k [00:00<00:00, 24.7MB/s]
/home/fukagai/.local/lib/python3.10/site-packages/torch/nn/modules/conv.py:456: UserWarning: Plan failed with a cudnnException: CUDNN_BACKEND_EXECUTION_PLAN_DESCRIPTOR: cudnnFinalize Descriptor Failed cudnn_status: CUDNN_STATUS_NOT_SUPPORTED (Triggered internally at ../aten/src/ATen/native/cudnn/Conv_v8.cpp:919.)
  return F.conv2d(input, weight, bias, self.stride,
image 1/1 /home/fukagai/bus.jpg: 640x480 4 persons, 1 bus, 1 stop sign, 44.2ms
Speed: 2.9ms preprocess, 44.2ms inference, 435.9ms postprocess per image at shape (1, 3, 640, 480)
Results saved to runs/detect/predict
💡 Learn more at https://docs.ultralytics.com/modes/predict

When I passed another image to the command and executed the following command, no warning message was displayed.

fukagai@ESPRIMOWD2H2:~$ yolo detect predict model=yolov8n.pt source=/mnt/c/dev/data/ultralytics/yolov8/input/DSC00208-min.JPG
Ultralytics YOLOv8.2.27 🚀 Python-3.10.12 torch-2.3.0+cu121 CUDA:0 (NVIDIA GeForce GTX 1650, 4096MiB)
YOLOv8n summary (fused): 168 layers, 3151904 parameters, 0 gradients, 8.7 GFLOPs

image 1/1 /mnt/c/dev/data/ultralytics/yolov8/input/DSC00208-min.JPG: 448x640 2 birds, 37.6ms
Speed: 2.6ms preprocess, 37.6ms inference, 333.9ms postprocess per image at shape (1, 3, 448, 640)
Results saved to runs/detect/predict3
💡 Learn more at https://docs.ultralytics.com/modes/predict

In the above command, the image file placed in C:\dev\data\ultralytics\yolov8\input\DSC00208-min.JPG on Windows 11 is referenced as /mnt/c/dev/data/ultralytics/yolov8/input/DSC00208-min.JPG from the Ubuntu on WSL2.

In the example command above, the image displaying the detection results was saved in the directory . /runs/detect/predict3.

The following command copied the image displaying the detection result to a directory that can be referenced from Windows 11.

fukagai@ESPRIMOWD2H2:~$ cp runs/detect/predict3/DSC00208-min.JPG /mnt/c/dev/data/ultralytics/yolov8/output/

The image below is an example of the detection result. One of the images on this page was used as input for object detection.

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA