ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.91k stars 16.14k forks source link

只训练COCO数据集中的car和person两类 #13195

Open stillbetter opened 2 months ago

stillbetter commented 2 months ago

Search before asking

Question

你好,我的目标是检测人和车,所以请问如果我直接使用coco数据集,如何当dataloader只输出person和car的标签和图像进行训练,而不会把其他类别的bbox输出。

Additional

No response

github-actions[bot] commented 2 months ago

👋 Hello @stillbetter, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 2 months ago

@stillbetter 你好!

要在COCO数据集中只训练car和person两类,你可以通过以下步骤来实现:

  1. 创建自定义数据集配置文件:首先,创建一个新的dataset.yaml文件,只包含你感兴趣的类别。在这个例子中,我们只需要car和person两类。
# dataset.yaml
path: ../datasets/coco  # 数据集根目录
train: images/train2017  # 训练图像路径
val: images/val2017  # 验证图像路径

# Classes
names:
  0: person
  1: car
  1. 过滤标签:你需要过滤COCO数据集中的标签,只保留car和person的标签。你可以使用以下脚本来实现这一点:
import os
import shutil

# 定义你感兴趣的类别
target_classes = [0, 2]  # person 和 car 在 COCO 中的类别索引

# 定义路径
dataset_path = '../datasets/coco'
train_labels_path = os.path.join(dataset_path, 'labels/train2017')
val_labels_path = os.path.join(dataset_path, 'labels/val2017')

def filter_labels(labels_path):
    for label_file in os.listdir(labels_path):
        label_path = os.path.join(labels_path, label_file)
        with open(label_path, 'r') as f:
            lines = f.readlines()

        filtered_lines = [line for line in lines if int(line.split()[0]) in target_classes]

        if filtered_lines:
            with open(label_path, 'w') as f:
                f.writelines(filtered_lines)
        else:
            os.remove(label_path)

# 过滤训练和验证标签
filter_labels(train_labels_path)
filter_labels(val_labels_path)
  1. 训练模型:使用你创建的dataset.yaml文件来训练模型。
python train.py --img 640 --epochs 3 --data dataset.yaml --weights yolov5s.pt

这样,你的模型将只使用car和person两类进行训练。如果你在训练过程中遇到任何问题,请确保你使用的是最新版本的YOLOv5,并且所有依赖项都已正确安装。

希望这能帮助你实现目标!如果有其他问题,请随时提问 😊

祝你好运!