WSL2上のUbuntu 22.04でUltralyticsのYOLOv8を実行 (GPGPUを使用)

Windows 11のWSL2上のUbuntu 22.04でUltralyticsのYOLOv8を使用して物体検出を実行したときのメモになります。物体検出は学習済みの重みデータを使用して実行しました。GPGPUを使用する設定で実行されるのを確認しています。

NVIDIA GeForce GTX 1650 (GPGPU)をセットしたデスクトップパソコンESPRIMO WD2/H2で動作確認しました。

1. Windows 11にWSL2 (Windows Subsystem for Linux 2) をインストール

Power Shellを管理者権限で起動し、下記のコマンドを実行します。

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

インストールが完了したら、Windows 11のPCを再起動します。
再起動後、Ubuntuが起動され、ユーザー名とパスワードを入力します。これでインストール完了になります。

以降の手順は WSL2 上で Ubuntu を起動して実行しました。

2. python3 と pip のインストール

2.1. 下記のコマンドで python3 と pip をインストールしました。

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

2.2. 下記のコマンドでインストールされた python3 と pip のバージョンを確認しました。

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. Ultralyticsのインストールと動作確認

UltralyticsのQuickstartに従い、学習済みの重みデータを使用して物体検出するまでの手順を確認しました。

3.1. 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. PATH環境変数に ~/.local/bin を追加

~/.bashrc の末尾に下記の行を追加し、

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

下記のコマンドを実行して設定を反映させました。

fukagai@ESPRIMOWD2H2:~$ source .bashrc

3.3. 学習済み重みデータを使用した物体検出コマンドの実行

指定した画像内の物体を検出するコマンドを実行したところ、下記のようなエラーが表示されました。

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

そこで、下記のコマンドを実行して libopencv-dev をインストールしたところ、

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

下記の物体検出コマンドを実行し、物体検出の結果が得られるようになりました。コマンドを実行した際、下記のような警告メッセージが表示されました。

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

私が撮影した画像をコマンドに渡し、下記のコマンドを実行した際には警告メッセージは表示されませんでした。

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

上記のコマンドでは、Windows 11 の C:\dev\data\ultralytics\yolov8\input\DSC00208-min.JPG に置いた画像ファイルを WSL2 の Ubuntu 側から /mnt/c/dev/data/ultralytics/yolov8/input/DSC00208-min.JPG で参照しています。

また、上記のコマンドの例では検出結果を表示した画像はディレクトリ ./runs/detect/predict3 に保存されました。

下記のコマンドで検出結果を表示した画像をWindows 11 から参照できるディレクトリにコピーしました。

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

下の画像は検出結果の画像の例になります。こちらのページの画像の一つを物体検出の入力として使用しました。

返信を残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA