【yolov5】用BDD100K数据集制作yolov5人车检测数据集

一、下载BDD100K数据集

数据集下载地址:http://bdd-data.berkeley.edu
在这里插入图片描述

二、json格式转yolo的txt格式

下面是一个bdd100k到yolov5的标注转换代码。其中把’car’,‘bus’,'truck’这三个类合并为了一类,“pedestrian”,“other person”,"rider"作为另一类,并过滤掉了晚上的数据
测试集:

import re 
import os 
import json 
 
def search_file(data_dir, pattern=r'\.jpg$'):
    root_dir = os.path.abspath(data_dir)
    for root, dirs, files in os.walk(root_dir):
        for f in files:
            if re.search(pattern, f, re.I):
                abs_path = os.path.join(root, f)
                # print('new file %s' % absfn)
                yield abs_path
 
class Bdd2yolov5:
    def __init__(self):
        self.bdd100k_width = 1280
        self.bdd100k_height = 720
        self.select_categorys=["pedestrian","other person","rider", "car", "bus", "truck"]
        self.cat2id = {
            "pedestrian": 0,
            "other person": 0,
            "rider": 0,
            "car": 1,
            "bus": 1,
            "truck": 1
        } 
 
    def _filter_by_attr(self, attr=None):
        if attr is None:
            return False 
        #过滤掉晚上的图片
        if attr['timeofday'] == 'night':
            return True 
        return False 
 
    def bdd2yolov5(self, path):
        with open(path) as fp:
            fp = json.load(fp)
            for j in fp:
                lines = ""
                label_name = j["name"].replace("jpg", "txt")
                image_name = j["name"]
                if self._filter_by_attr(j['attributes']):
                    img_path = os.path.join(r"D:\Download\bdd100k\images\100k\train", image_name)
                    if os.path.exists(img_path):
                        os.remove(img_path)
                    continue
                for fr in j["labels"]:
                    dw = 1.0 / self.bdd100k_width
                    dh = 1.0 / self.bdd100k_height
                    # for obj in fr["objects"]:
                    if fr["category"] in self.select_categorys:
                        idx = self.cat2id[fr["category"]]
                        cx = (fr["box2d"]["x1"] + fr["box2d"]["x2"]) / 2.0
                        cy = (fr["box2d"]["y1"] + fr["box2d"]["y2"]) / 2.0
                        w = fr["box2d"]["x2"] - fr["box2d"]["x1"]
                        h = fr["box2d"]["y2"] - fr["box2d"]["y1"]
                        if w<=0 or h<=0:
                            continue
                        #根据图片尺寸进行归一化
                        cx,cy,w,h = cx*dw,cy*dh,w*dw,h*dh
                        line = f"{idx} {cx:.6f} {cy:.6f} {w:.6f} {h:.6f}\n"
                        lines += line
                if len(lines) != 0:
                    yolo_txt = os.path.join("./train/train", label_name)
                    # yolo_txt = path.replace(".json",".txt")
                    with open(yolo_txt, 'w') as fp2:
                         fp2.writelines(lines)
                if len(lines)==0:
                    img_path = os.path.join(r"D:\Download\bdd100k\images\100k\train", image_name)
                    if os.path.exists(img_path):
                        os.remove(img_path)

 
 
if __name__ == "__main__":
    bdd_label_dir = "./train"
    cvt=Bdd2yolov5()
    for path in search_file(bdd_label_dir, r"\.json$"):
        cvt.bdd2yolov5(path)

如果图片文件数量和标签文件数量不匹配,删掉多出的一部分图片

import os 
def search_file(data_dir, pattern=r'\.jpg$'):
    root_dir = os.path.abspath(data_dir)
    for root, dirs, files in os.walk(root_dir):
        for f in files:
                yield f

bdd_label_dir = r"D:\Download\bdd100k\images\100k\train"
for path in search_file(bdd_label_dir, r"\.jpg$"):
  txtfile = path.replace("jpg", "txt")
  txt_path = os.path.join(r"D:\Download\bdd100k\labels\det_20\train\train", txtfile)
  jpg_path = os.path.join(bdd_label_dir, path)
  if not os.path.exists(txt_path):
    os.remove(jpg_path)
验证集同理,改下文件路径即可

处理完之后:
在这里插入图片描述

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
YOLO系列是基于深度学习的端到端实时目标检测方法。 PyTorch版的YOLOv5轻量而高性能,更加灵活和易用,当前非常流行。 本课程将手把手地教大家使用labelImg标注和使用YOLOv5训练自己的数据集。课程实战分为两个项目:单目标检测(足球目标检测)和多目标检测(足球和梅西同时检测)。  本课程的YOLOv5使用ultralytics/yolov5,在Windows和Ubuntu系统上分别做项目演示。包括:安装YOLOv5、标注自己的数据集、准备自己的数据集(自动划分训练集和验证集)、修改配置文件、使用wandb训练可视化工具、训练自己的数据集、测试训练出的网络模型和性能统计。 除本课程《YOLOv5实战训练自己的数据集(Windows和Ubuntu演示)》外,本人推出了有关YOLOv5目标检测的系列课程。请持续关注该系列的其它视频课程,包括:《YOLOv5(PyTorch)目标检测:原理与源码解析》课程链接:https://edu.csdn.net/course/detail/31428《YOLOv5目标检测实战:Flask Web部署》课程链接:https://edu.csdn.net/course/detail/31087《YOLOv5(PyTorch)目标检测实战:TensorRT加速部署》课程链接:https://edu.csdn.net/course/detail/32303《YOLOv5目标检测实战:Jetson Nano部署》课程链接:https://edu.csdn.net/course/detail/32451《YOLOv5+DeepSORT多目标跟踪与计数精讲》课程链接:https://edu.csdn.net/course/detail/32669《YOLOv5实战口罩佩戴检测》课程链接:https://edu.csdn.net/course/detail/32744《YOLOv5实战中国交通标志识别》课程链接:https://edu.csdn.net/course/detail/35209 《YOLOv5实战垃圾分类目标检测》课程链接:https://edu.csdn.net/course/detail/35284  
要使用YOLOv5进行BDD100K数据集的训练,可以按照以下步骤进行: 1. 下载BDD100K数据集,并将其转换为YOLOv5所需的格式。可以使用官方提供的脚本进行转换,具体步骤可以参考官方文档:https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data。 2. 安装YOLOv5及其依赖项。可以使用pip进行安装:`pip install -r requirements.txt`。具体安装步骤可以参考官方文档:https://github.com/ultralytics/yolov5。 3. 创建一个训练配置文件,该文件包含有关训练参数、数据集路径等信息。可以使用官方提供的示例配置文件进行修改:https://github.com/ultralytics/yolov5/blob/master/models/yolov5s.yaml。 4. 运行训练命令,开始训练网络。可以使用以下命令进行训练: ``` python train.py --img 640 --batch 16 --epochs 50 --data /path/to/data.yaml --cfg ./models/yolov5s.yaml --weights '' --name bdd100k_yolov5s ``` 其中,`--img`参数指定训练图像的大小,`--batch`参数指定训练批次的大小,`--epochs`参数指定训练轮数,`--data`参数指定数据集配置文件的路径,`--cfg`参数指定网络配置文件的路径,`--weights`参数指定预训练权重的路径,`--name`参数指定训练过程中保存的模型名称。 5. 等待训练完成后,可以使用训练好的网络进行目标检测任务。可以使用以下命令进行测试: ``` python detect.py --weights /path/to/weights.pt --img 640 --conf 0.4 --source /path/to/images/ ``` 其中,`--weights`参数指定训练好的权重文件的路径,`--img`参数指定测试图像的大小,`--conf`参数指定目标检测置信度的阈值,`--source`参数指定测试图像的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值