YOLOv8による物体検知を試してみる
YOLO (You Only Look Once), a popular object detection and image segmentation model, was developed by Joseph Redmon and Ali Farhadi at the University of Washington. Launched in 2015, YOLO quickly gained popularity for its high speed and accuracy. YOLOv2, released in 2016, improved the original model by incorporating batch normalization, anchor boxes, and dimension clusters. YOLOv3, launched in 2018, further enhanced the model's performance using a more efficient backbone network, multiple anchors and spatial pyramid pooling. YOLOv4 was released in 2020, introducing innovations like Mosaic data augmentation, a new anchor-free detection head, and a new loss function. YOLOv5 further improved the model's performance and added new features such as hyperparameter optimization, integrated experiment tracking and automatic export to popular export formats. YOLOv6 was open-sourced by Meituan in 2022 and is in use in many of the company's autonomous delivery robots. YOLOv7 added additional tasks such as pose estimation on the COCO keypoints dataset. 引用: https://docs.ultralytics.com/#yolo-a-brief-history
YOLOv8 is designed with a strong focus on speed, size, and accuracy, making it a compelling choice for various vision AI tasks. It outperforms previous versions by incorporating innovations like a new backbone network, a new anchor-free split head, and new loss functions. These improvements enable YOLOv8 to deliver superior results, while maintaining a compact size and exceptional speed. 引用: https://docs.ultralytics.com/#ultralytics-yolov8
YOLOv8について
まず始めるには 引用: https://docs.ultralytics.com/ YOLOv8で使用できるモードの説明・「Train」は独自のデータセットでYOLOv8モデルを作成するモードです。 ・「Val」は学習後のYOLOv8モデルを検証するためのモードです。 ・「Predict」は学習済みのYOLOv8モデルを画像や動画に適用し予測や推論するためのモードです。 ・「Export」はYOLOv8モデルを他のフォーマットにエクスポートするためのモードです。他のフォーマットに変換することで推論スピードがアップするなどのメリットがあります。 ・「Track」はYOLOv8モデルを使って物体トラッキングをリアルタイムで行うためのモードです。 ・「Benchmark」はエクスポートしたYOLOv8モデルの速度や正確さをベンチマークするためのモードです。
Train: For training a YOLOv8 model on a custom dataset. Val: For validating a YOLOv8 model after it has been trained. Predict: For making predictions using a trained YOLOv8 model on new images or videos. Export: For exporting a YOLOv8 model to a format that can be used for deployment. Track: For tracking objects in real-time using a YOLOv8 model. Benchmark: For benchmarking YOLOv8 exports (ONNX, TensorRT, etc.) speed and accuracy. 引用: https://docs.ultralytics.com/modes/
実行可能なタスクと事前学習済みモデルYOLOv8 is an AI framework that supports multiple computer vision tasks. The framework can be used to perform detection, segmentation, classification, and pose estimation. Each of these tasks has a different objective and use case. 引用: https://docs.ultralytics.com/tasks/
Available Models: Detection - yolov8n, yolov8s, yolov8m, yolov8l, yolov8x Instance Segmentation - yolov8n-seg, yolov8s-seg, yolov8m-seg, yolov8l-seg, yolov8x-seg Classification - yolov8n-cls, yolov8s-cls, yolov8m-cls, yolov8l-cls, yolov8x-cls Pose - yolov8n-pose, yolov8s-pose, yolov8m-pose, yolov8l-pose, yolov8x-pose, yolov8x-pose-p6 引用: https://github.com/ultralytics/ultralytics/tree/main/ultralytics/models#1-yolov8
引用: https://github.com/ultralytics/ultralytics 引用: https://docs.ultralytics.com/tasks/detect/YOLOv8の環境準備
YOLOv8用のPython仮想環境の準備 brew intall python@3.7 python3.7 -m venv venv-yolov8 source venv-yolov8/bin/activate (venv-yolov8) pip install pip -U (venv-yolov8) pip install wheel setuptools (venv-yolov8) pip install ipykernel (venv-yolov8) git clone https://github.com/ultralytics/ultralytics (venv-yolov8) cd ultralytics (venv-yolov8) pip install -e '.[dev]' # or pip install ultralyticsでもOK (venv-yolov8) pip install lap pafy youtube_dl==2020.12.2 #インストールされてなかった。Traking Taskで必要 (venv-yolov8) python3 -m ipykernel install --user --name venv-yolov8 --display-name "venv-yolov8"基本的にはpip install ultralyticsでOKです。Python3.7を使っていますが、3.8や3.9でも問題ありません。(23年4月11日現在)
Install Pip install the ultralytics package including all requirements in a Python>=3.7 environment with PyTorch>=1.7. 引用: https://github.com/ultralytics/ultralytics
使用画像
# フォルダ内の画像を取得し、グリッド表示する関数 # 引数: folder_path: フォルダへのフルパス def display_images(folder_path): import os from PIL import Image from IPython.display import display path = folder_path # フォルダ内のイメージの取得 image_files = [filename for filename in os.listdir(path) if filename.lower().endswith((".jpg", ".jpeg", ".gif", ".bmp", ".png"))] num_images = len(image_files) if num_images == 0: print("No image files found in the folder.") exit() # グリッドのための行と列の数を計算 if num_imagesYOLOv8で物体検知を試してみる
from ultralytics import YOLO from PIL import Image import cv2 # 事前学習済みのモデルを読み込み(detectionモデルを使用) model = YOLO('yolov8n.pt') # predictモードを実行 results = model.predict(source="/Users/hinomaruc/Desktop/blog/dataset/aidetection", project="/Users/hinomaruc/Desktop/blog/dataset/yolov8/runs", # 出力先 name="mypredict", #フォルダ名 exist_ok=True, #上書きOKか save=True) image 1/5 /Users/hinomaruc/Desktop/blog/dataset/aidetection/DALL·E 2023-04-04 06.11.13 - ten cats.png: 640x640 2 cats, 411.2ms image 2/5 /Users/hinomaruc/Desktop/blog/dataset/aidetection/DALL·E 2023-04-04 06.13.16 - five real cats and five real dogs.png: 640x640 9 cats, 4 dogs, 1 frisbee, 404.6ms image 3/5 /Users/hinomaruc/Desktop/blog/dataset/aidetection/DALL·E 2023-04-04 06.15.26 - 3 real looking cats and 3 real looking dogs.png: 640x640 3 cats, 2 dogs, 421.6ms image 4/5 /Users/hinomaruc/Desktop/blog/dataset/aidetection/DALL·E 2023-04-04 06.15.32 - 3 real looking cats and 3 real looking dogs.png: 640x640 2 cats, 4 dogs, 425.9ms image 5/5 /Users/hinomaruc/Desktop/blog/dataset/aidetection/DALL·E 2023-04-04 06.15.40 - 3 real looking cats and 3 real looking dogs.png: 640x640 2 cats, 2 dogs, 454.2ms Speed: 1.9ms preprocess, 423.5ms inference, 2.2ms postprocess per image at shape (1, 3, 640, 640) Results saved to /Users/hinomaruc/Desktop/blog/dataset/yolov8/runs/mypredict # YOLOv8モデルでdetectした結果を確認 display_images("/Users/hinomaruc/Desktop/blog/dataset/yolov8/runs/mypredict") (オプション) predictモードのソースにフォルダ以外を渡す # ローカルの画像へのパス results = model.predict(source="/Users/hinomaruc/Desktop/blog/dataset/aidetection/DALL·E 2023-04-04 06.11.13 - ten cats.png") print("---") # from PIL im1 = Image.open("/Users/hinomaruc/Desktop/blog/dataset/justviewimages/cat/a_crowncat129_TP_V4.jpg") results = model.predict(source=im1) print("---") # from ndarray im2 = cv2.imread("/Users/hinomaruc/Desktop/blog/dataset/justviewimages/cat/b_HIRA15041143_TP_V4.jpg") results = model.predict(source=im2) print("---") #from list of PIL/ndarray results = model.predict(source=[im1, im2]) image 1/1 /Users/hinomaruc/Desktop/blog/dataset/aidetection/DALL·E 2023-04-04 06.11.13 - ten cats.png: 640x640 2 cats, 414.3ms Speed: 1.8ms preprocess, 414.3ms inference, 1.8ms postprocess per image at shape (1, 3, 640, 640) Results saved to /Users/hinomaruc/Desktop/blog/dataset/yolov8/runs/mypredict 4 labels saved to /Users/hinomaruc/Desktop/blog/dataset/yolov8/runs/mypredict/labels --- 0: 448x640 1 cat, 289.7ms Speed: 2.6ms preprocess, 289.7ms inference, 1.6ms postprocess per image at shape (1, 3, 640, 640) Results saved to /Users/hinomaruc/Desktop/blog/dataset/yolov8/runs/mypredict 4 labels saved to /Users/hinomaruc/Desktop/blog/dataset/yolov8/runs/mypredict/labels --- 0: 448x640 1 cat, 304.1ms Speed: 1.9ms preprocess, 304.1ms inference, 1.9ms postprocess per image at shape (1, 3, 640, 640) Results saved to /Users/hinomaruc/Desktop/blog/dataset/yolov8/runs/mypredict 4 labels saved to /Users/hinomaruc/Desktop/blog/dataset/yolov8/runs/mypredict/labels --- 0: 448x640 1 cat, 1: 448x640 1 cat, 567.0ms Speed: 3.2ms preprocess, 283.5ms inference, 1.7ms postprocess per image at shape (1, 3, 640, 640) Results saved to /Users/hinomaruc/Desktop/blog/dataset/yolov8/runs/mypredict 4 labels saved to /Users/hinomaruc/Desktop/blog/dataset/yolov8/runs/mypredict/labels YOLOv8でTrackingを試すサンプルだとyoutubeの動画をソースにすればそのまま読み込めるようでしたが、私の環境だとなぜかOpenCV: Couldn't read video stream from file というエラーがでて動かなかったので、サンプル動画をダウンロードしてきて読み込んだら動作しました。
from ultralytics import YOLO # Load a model (detectionモデルを使います) model = YOLO('yolov8n.pt') # load an official detection model # セグメンテーションしたい場合はこちらを使う (detectionモデルより重いです) # model = YOLO('yolov8n-seg.pt') # load an official segmentation model # Track with the model results = model.track(source="/Users/hinomaruc/Desktop/blog/dataset/aidetection_video/pexels-cottonbro-studio-4124024-4096x2160-50fps.mov", show=True, tracker="botsort.yaml") WARNING ⚠️ stream/video/webcam/dir predict source will accumulate results in RAM unless stream=True is passed, causing potential out-of-memory errors for large sources or long-running streams/videos. Usage: results = model(source=. stream=True) # generator of Results objects for r in results: boxes = r.boxes # Boxes object for bbox outputs masks = r.masks # Masks object for segment masks outputs probs = r.probs # Class probabilities for classification outputs video 1/1 (1/821) /Users/hinomaruc/Desktop/blog/dataset/aidetection_video/pexels-cottonbro-studio-4124024-4096x2160-50fps.mov: 352x640 1 person, 498.8ms video 1/1 (2/821) /Users/hinomaruc/Desktop/blog/dataset/aidetection_video/pexels-cottonbro-studio-4124024-4096x2160-50fps.mov: 352x640 1 person, 262.5ms video 1/1 (3/821) /Users/hinomaruc/Desktop/blog/dataset/aidetection_video/pexels-cottonbro-studio-4124024-4096x2160-50fps.mov: 352x640 1 person, 229.4ms ・・・省略・・・ Tracking実行画面まとめ
YOLOv8による物体検知の記事一覧
- YOLOv8による物体検知を試してみる
- YOLOv8による物体検知の結果を表示してみる
- YOLOv8で特定のエリアのみ物体検知する方法
- YOLOv8のvalモードで物体検知した結果の精度を確認してみる
- YOLOv8のvalモードの結果が想定と異なるAPだったので確認してみた
- YOLOv8で指定領域内の精度と物体検知数を出力する方法
関連記事
物体検知モデルの精度指標を色々確認してみる 無料でGPU環境を使えるAmazon SageMaker Studio Labに登録してみた今回はAmazon SageMaker Studio Labというサービスを使ってみようと思います。CPU環境とGPU環境の両方が無料で使えてしまうサービスで、GoogleのColaboratoryと似たサービスのようです。登録してから使え.
Jupyter notebookのUIが変更になっていました (その3-6) アップル引越しの需要予測をAuto-ARIMAモデルでやってみた前回ARMAモデルの記事を書きました。結果はARモデルやMAモデルと同じく1年分の日次予測は難しそうでした。今回は自動ARIMAモデルを試してみます。時系列モデル一覧AR (自己回帰モデル)MA (移動平均モデル)ARMA (自己回帰移動平.
[Python] Jupyter notebookでスライドショーみたいに画像をインタラクティブに表示する方法画像が大量にある場合、jupyter notebookのアウトプットに全て表示して確認するのはあまり現実的ではありません。そこで、Jupyter notebookでもスライドショーのように描画する方法であれば見やすいかなと思い調べてみたら、.
(その3-3) エイムズの住宅価格のデータセットのデータ加工② JavaScriptで複数のURLや画像を確率的に出し分ける方法 YOLOv8による物体検知の結果を表示してみる データキャリア別勉強法 【完全版】隠れた物体も完璧に処理!バウンディングボックスアノテーション基準〜検出精度を劇的に向上させる7つのパターン別ガイド〜 hinomarucをフォローする 人気の記事 pythonからOracleを操作する「cx_Oracle」が「python-oracledb」になったのでさっそく使ってみた 18333 views 2022.12.11 2023.10.17 YOLOv8による物体検知の結果を表示してみる 17549 views 2023.04.15 2023.05.14 Matplotlibでの軸(axis)と目盛り(ticks)の設定をまとめてみた 12897 views 2022.12.30 2022.12.31 PythonでSQLAlchemyを使ってpostgreSQLを操作してみる 10486 views 2022.01.29 2022.09.28 pandasに新しいカラムを追加する5つの方法 7728 views 2022.02.27 2022.09.25 サイト内を検索する 新着記事 Pythonログ遅延の包括的解決ガイド 【完全版】隠れた物体も完璧に処理!バウンディングボックスアノテーション基準〜検出精度を劇的に向上させる7つのパターン別ガイド〜 M1 macでアノテーションツールのCVATを動かすのに色々やってみた 【論文】Many-Shot In-Context Learning mp4ファイルを10分ごとに分割するスクリプトを作成 2024.10.05 2025.10.05 おすすめの記事 YOLOv8で指定領域内の精度と物体検知数を出力する方法 YOLOv8で特定のエリアのみ物体検知する方法 2023.04.16 2023.04.28 物体検知モデルの精度指標を色々確認してみる 2023.04.25 2023.05.06 Mask R-CNNでAPを計算してみる 2023.06.20 2024.01.06 YOLOv8のvalモードで物体検知した結果の精度を確認してみる 2023.05.02 2023.05.13- YOLOの歴史
- YOLOv8について
- まず始めるには
- YOLOv8で使用できるモードの説明
- 実行可能なタスクと事前学習済みモデル
- (オプション) predictモードのソースにフォルダ以外を渡す
- YOLOv8でTrackingを試す