2025-09-12 08:48:34 +09:00

111 lines
3.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### YOLOv8 のクラスについて
* YOLOv8 は **学習済みの重み**によって、検出できるクラス(カテゴリ)が決まります。
* 公式で配布されている `yolov8n.pt`, `yolov8s.pt` などは **COCO データセット (COCO-2017, 80クラス)** で学習されています。
* つまり「何を検知できるか」は **モデルに埋め込まれているクラスラベルリスト**に依存します。
### 🔹 COCO の代表的なクラス(抜粋)
* 人物: `person`
* 車両: `car`, `truck`, `bus`, `motorbike`, `bicycle`
* 動物: `dog`, `cat`, `horse` など
* 家具・物体: `chair`, `sofa`, `tv`, `cell phone` など
(全部で 80 種類)
### ✅ `allowed_classes` の意味
* これは **あなた側のフィルタ**です。
* YOLO は 80 クラスを全部返してきますが、その中で「車両だけ欲しい」ときに
```python
allowed_classes = {"car", "truck", "bus", "motorbike", "bicycle"}
```
としてフィルタリングします。
### 🔹 例
```python
detections = self.detector.detect(img)
detections = [d for d in detections if d.label in allowed_classes]
```
---
## 転移学習
YOLOv8 は **転移学習 (fine-tuning)** をサポートしていて、
COCOの80クラスとは別に、自分で定義したクラスで再学習させられます。
たとえば「細菌」や「顕微鏡画像の特定形状」なども検出対象にできます。
## 新しいクラスを学習する流れ(YOLOv8)
1. **データセットの用意**
* 画像ファイル(`.jpg`, `.png` など)
* アノテーション(バウンディングボックス付きラベル)を YOLO形式で準備
```
<class_id> <x_center> <y_center> <width> <height>
```
(座標は画像サイズで 01 に正規化)
* ディレクトリ構造の例:
```
dataset/
├─ images/
│ ├─ train/
│ └─ val/
└─ labels/
├─ train/
└─ val/
```
1. **データ設定ファイルを作成**
* `bacteria.yaml` のような YAML を書く
```yaml
path: dataset
train: images/train
val: images/val
nc: 1 # クラス数
names: ['bacteria']
```
1. **学習を実行**
```bash
yolo detect train data=bacteria.yaml model=yolov8n.pt epochs=50 imgsz=640
```
* `yolov8n.pt` は小さいモデル(転移学習の出発点)
* `epochs=50` は繰り返し回数(データ量次第で増減)
* `imgsz=640` は入力画像サイズ
1. **学習済みモデルを使う**
```bash
yolo detect predict model=runs/detect/train/weights/best.pt source=your_test_image.jpg
```
* この `best.pt` が「細菌クラス専用モデル」になります
---
### 応用ポイント
* **複数クラス**も可能(例: `['bacteria', 'fungus', 'virus']`)
* **COCO + 自作データで再学習** → 汎用物体検知 + 特殊対象の両立もできる
* **顕微鏡画像**のように特殊なデータでも、十分なアノテーションがあれば対応可能
### 🔹 注意点
* 学習には **GPU環境**(Colab, Kaggle, 自前マシンなど)が必要
* データが少ない場合は、**転移学習**(小さいデータで fine-tuning)が特に効果的
* 正確なアノテーション(ラベル付け)が精度に直結します