1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

画像分類するなら、ラクで精度が良いこれで

画像分類っていざするとなるとどのモデルでやればいいかわかんなくないですか?
そんなあなたにこれ。

インストール

pip install ultralytics

トレーニング

以下の形式でフォルダにデータを分けます。

cifar-10-/
|
|-- train/
|   |-- cat/
|   |   |-- 10008_cat.png
|   |   |-- 10009_cat.png
|   |   |-- ...
|   |
|   |-- person/
|   |   |-- 1000_person.png
|   |   |-- 1001_person.png
|   |   |-- ...
|
|-- val/
|   |-- cat/
|   |   |-- 20008_cat.png
|   |   |-- 20009_cat.png
|   |   |-- ...
|   |
|   |-- person/
|   |   |-- 2000_person.png
|   |   |-- 2001_person.png
|   |   |-- ...

あとは、これでok.

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8m-cls.pt")  # load a pretrained model (recommended for training)

# Train the model
results = model.train(data="my_dataset", epochs=100, imgsz=640)

データセットのパスを指定して実行するだけ。
runs/classify/train/weights/にbest.ptファイルが保存されます。

推論

model = YOLO(best.pt)
results = model("image.jpg")
cls = results[0].probs.top1
label = results[0].names[cls]

cat.jpg

0
"cat"

pexels-clarango-3866555.jpg

1
"person"

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?