【利用yolo5-x训练只含有person标签的coco数据集】

准备数据集

  • 了解coco数据集中标签的类别,注意在coco数据集中person的id为1而在yolo的识别中person为0

person
bicycle
car
motorbike
aeroplane
bus
train
truck
boat
traffic light
fire hydrant
stop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottle
wine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
sofa
pottedplant
bed
diningtable
toilet
tvmonitor
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddy bear
hair drier
toothbrush

  • 原始文件:
datasets
    |
     coco
         |
          images
                |
                train2017
                val2017
           labels
                 |
                 train2017
                 val2017
  • 新文件:
person_datasets 
    |
     images
           |
           train2017
           val2017
      labels
            |
            train2017
            val2017
  • python脚本提取出只含有person的图像和标签
# coding=utf-8
import os
import shutil

# 设置原始和新数据集的路径
original_dataset_path = '/fb_isa/workspace_ym/datasets/coco/coco'
new_dataset_path = '/fb_isa/workspace_ym/person_datasets'

# 为新数据集创建目录结构
for folder in ['images/train2017', 'images/val2017', 'labels/train2017', 'labels/val2017']:
    os.makedirs(os.path.join(new_dataset_path, folder), exist_ok=True)

# 复制符合条件的文件
for dataset_type in ['train2017', 'val2017']:
    labels_dir = os.path.join(original_dataset_path, 'labels', dataset_type)
    images_dir = os.path.join(original_dataset_path, 'images', dataset_type)

    for label_file in os.listdir(labels_dir):
        label_file_path = os.path.join(labels_dir, label_file)
        with open(label_file_path, 'r') as file:
            lines = file.readlines()

        if any(line.startswith('0 ') for line in lines):
            # 复制图像
            image_file = label_file.replace('.txt', '.jpg')
            src_image_path = os.path.join(images_dir, image_file)
            dst_image_path = os.path.join(new_dataset_path, 'images', dataset_type, image_file)
            shutil.copy(src_image_path, dst_image_path)

            # 复制标签文件
            dst_label_path = os.path.join(new_dataset_path, 'labels', dataset_type, label_file)
            shutil.copy(label_file_path, dst_label_path)
  • 依去除在“label”中的"train2017"和"val2017"文件夹下的所有txt文件中id不为0的行
# coding=utf-8
import os

# 指定包含 txt 文件的文件夹路径
# folder_path = '/fb_isa/workspace_ym/person_datasets/labels/val2017/'
folder_path = '/fb_isa/workspace_ym/person_datasets/labels/train2017/'

# 遍历文件夹中的所有 txt 文件
for filename in os.listdir(folder_path):
    if filename.endswith('.txt'):
        file_path = os.path.join(folder_path, filename)

        # 读取文件内容
        with open(file_path, 'r') as file:
            lines = file.readlines()

        # 筛选以 '0' 开头的行
        filtered_lines = [line for line in lines if line.startswith('0')]

        # 将筛选后的内容写回文件
        with open(file_path, 'w') as file:
            file.writelines(filtered_lines)

print("Processing completed.")

准备yolov5

通过git下载

git clone https://github.com/ultralytics/yolov5

源码下完后,下面开始安装Yolov5所需模块,首先输入cd进入yolov5的子目录,再输入等待安装完成。

pip install -r requirements.txt
  • 在yolov5/data文件夹下新建person.yaml
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]

path: /fb_isa/workspace_ym/person_datasets  # dataset root dir
train: images/train2017  # train images (relative to 'path')
val: images/val2017  # val images (relative to 'path')
test:  # test images (optional)

# Classes
nc: 1  # number of classes
names: ['person']  # class names
  • 下载yolov5x的模型权重文件保存到weights/yolov5x.pt
  • 修改train.py的参数

nvi

利用tmux在离开终端情况下持续训练

tmux 是一个非常有用的工具,可以在关闭命令行窗口后继续运行程序。以下是使用 tmux 的基本步骤:

  1. 启动 tmux:打开终端,输入 tmux 命令来启动一个新的 tmux 会话。新建会话session,session-name自己命名,就会进入新建的会话窗口中。

    tmux new -s session-name
    
  2. 运行您的程序:在 tmux 会话中,运行想要在后台持续运行的程序。例如,如果有一个 Python 脚本,可以像平时一样在这个会话中运行它。

    python your_script.py
    
  3. 分离 tmux 会话:当程序开始运行后,可以分离 tmux 会话,这样即使关闭了终端窗口,程序也会继续运行。要分离会话,请按下 Ctrl + B 然后立即按下 D。这会将您带回原始终端界面,而 tmux 会话和其中的程序会在后台继续运行。

  4. 重新连接到 tmux 会话:如果想重新查看或与你的程序交互,可以重新连接到之前的 tmux 会话。首先,可以列出所有的 tmux 会话:

    tmux ls
    

    然后,可以根据会话编号或名称重新连接到特定的会话:

    tmux attach-session -t [session-id]
    

    这里的 [session-id] 是从 tmux ls 命令得到的会话编号或名称。

使用 tmux,程序可以在后台安全地运行,即使退出了 SSH 会话或关闭了终端窗口。

问题

https://blog.csdn.net/qq_42902997/article/details/109565786
https://blog.csdn.net/qq_36603177/article/details/132117549
根据这两篇博客在jupyter bitebook端配置服务器,但是拒绝访问

http://192.168.1.5:8890/
在这里插入图片描述

COCO数据集转换为YOLO数据集可以通过以下步骤完成: 1. 下载COCO数据集并解压缩。 2. 安装COCO API和OpenCV。 3. 创建一个新的文件夹,用于存储转换后的YOLO数据集。 4. 读取COCO中的图像和注释。 5. 将每个图像转换为YOLO格式(.txt文件)。 6. 将所有的YOLO格式文件和图像放入新的文件夹中。 以下是一个将COCO数据集转换为YOLO数据集的Python代码示例: ```python import os import json import cv2 # COCO类别对应的YOLO类别 class_id = { "person": 0, "bicycle": 1, "car": 2, "motorcycle": 3, "airplane": 4, "bus": 5, "train": 6, "truck": 7, "boat": 8, "traffic light": 9, "fire hydrant": 10, "stop sign": 11, "parking meter": 12, "bench": 13, "bird": 14, "cat": 15, "dog": 16, "horse": 17, "sheep": 18, "cow": 19, "elephant": 20, "bear": 21, "zebra": 22, "giraffe": 23, "backpack": 24, "umbrella": 25, "handbag": 26, "tie": 27, "suitcase": 28, "frisbee": 29, "skis": 30, "snowboard": 31, "sports ball": 32, "kite": 33, "baseball bat": 34, "baseball glove": 35, "skateboard": 36, "surfboard": 37, "tennis racket": 38, "bottle": 39, "wine glass": 40, "cup": 41, "fork": 42, "knife": 43, "spoon": 44, "bowl": 45, "banana": 46, "apple": 47, "sandwich": 48, "orange": 49, "broccoli": 50, "carrot": 51, "hot dog": 52, "pizza": 53, "donut": 54, "cake": 55, "chair": 56, "couch": 57, "potted plant": 58, "bed": 59, "dining table": 60, "toilet": 61, "tv": 62, "laptop": 63, "mouse": 64, "remote": 65, "keyboard": 66, "cell phone": 67, "microwave": 68, "oven": 69, "toaster": 70, "sink": 71, "refrigerator": 72, "book": 73, "clock": 74, "vase": 75, "scissors": 76, "teddy bear": 77, "hair drier": 78, "toothbrush": 79 } # 将COCO注释转换为YOLO格式 def convert_annotation(image_id, coco_annotation, file): for annotation in coco_annotation: x, y, w, h = annotation['bbox'] x_center = x + w / 2 y_center = y + h / 2 x_center /= width y_center /= height w /= width h /= height class_name = coco_classes[annotation["category_id"]] class_num = class_id[class_name] file.write("%s %s %s %s %s\n" % (class_num, x_center, y_center, w, h)) # COCO数据集文件夹路径 coco_folder = "/path/to/coco/folder" # 用于存储YOLO数据集的文件夹路径 yolo_folder = "/path/to/yolo/folder" # COCO数据集注释文件 coco_annotation_file = os.path.join(coco_folder, "annotations/instances_val2017.json") # COCO数据集图像文件夹 coco_image_folder = os.path.join(coco_folder, "val2017") # 创建一个新的文件夹,用于存储转换后的YOLO数据集 if not os.path.exists(yolo_folder): os.makedirs(yolo_folder) # 读取COCO注释文件 with open(coco_annotation_file) as f: coco_data = json.load(f) # COCO类别列表 coco_classes = [] for category in coco_data['categories']: coco_classes.append(category['name']) # 遍历COCO数据集中的每个图像 for image_data in coco_data['images']: # 获取图像文件名和路径 image_name = image_data['file_name'] image_path = os.path.join(coco_image_folder, image_name) # 读取图像 image = cv2.imread(image_path) height, width, channels = image.shape # 创建YOLO格式的注释文件 yolo_annotation_file = os.path.join(yolo_folder, os.path.splitext(image_name)[0] + ".txt") with open(yolo_annotation_file, "w") as f: # 获取图像中的每个注释 for annotation in coco_data['annotations']: # 如果注释属于当前图像,则将其转换为YOLO格式 if annotation['image_id'] == image_data['id']: convert_annotation(image_data['id'], [annotation], f) # 将图像复制到YOLO数据集文件夹中 yolo_image_path = os.path.join(yolo_folder, image_name) cv2.imwrite(yolo_image_path, image) ``` 在运行完上述代码后,YOLO数据集文件夹中应该包括所有转换后的图像和注释文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值