0


多目标跟踪(1):使用MOT17数据集训练YOLOv7检测器

一.前言:

参考代码:https://github.com/JackWoo0831/Yolov7-tracker
本文是基于如上的程序,原程序是使用VisDrone2019-MOT-train进行YOLOv7检测器的训练,本文将介绍如何使用MOT17数据集训练YOLOv7检测器。

二.数据集制作

首先,应该将MOT17数据集中的labels转化为yolo格式。

(1)转换labels

labels为表示图片的类别,坐标的txt文件,yolo格式要求坐标必须归一化。
转化代码convert.py如下;

'''
创建以下四个目录,用于存放图片和标签
images/train
images/val
labels/train
labels/val
'''
import os
import shutil
import numpy as np
import configparser
if not os.path.exists('images'):
    os.makedirs('images/train')
    os.makedirs('images/val')
if not os.path.exists('labels'):
    os.makedirs('labels/train')
    os.makedirs('labels/val')

def convert(imgWidth, imgHeight, left, top, width, height):
    x = (left + width / 2.0) / imgWidth
    y = (top + height / 2.0) / imgHeight
    w = width / imgWidth
    h = height / imgHeight
    return ('%.6f'%x, '%.6f'%y, '%.6f'%w, '%.6f'%h) # 保留6位小数

for mot_dir in os.listdir('test'):  # mot_dir是例如MOT17-02-FRCNN这种
    det_path = os.path.join('test', mot_dir, 'det/det.txt')  # det.txt路径
    dets = np.loadtxt(det_path, delimiter=',')  # 读取det.txt文件
    ini_path = os.path.join('test', mot_dir, 'seqinfo.ini')  # seqinfo.ini路径
    conf = configparser.ConfigParser()
    conf.read(ini_path)  # 读取seqinfo.ini文件
    seqLength = int(conf['Sequence']['seqLength'])  # MOT17-02-FRCNN序列的长度
    imgWidth = int(conf['Sequence']['imWidth'])  # 图片宽度
    imgHeight = int(conf['Sequence']['imHeight'])  # 图片长度
    for det in dets:
        frame_id, _, left, top, width, height = int(det[0]), det[1], det[2], det[3], det[4], det[5]
        box = convert(imgWidth, imgHeight, left, top, width, height)
        if '-' in ''.join(box) or float(box[0]) > 1.0 or float(box[1]) > 1.0 or float(box[2]) > 1.0 or float(
                box[3]) > 1.0:
            print(imgWidth, imgHeight, left, top, width, height)
            print(box)
            break
        image_name = mot_dir + '-' + '%06d' % frame_id + '.jpg'  # MOT17-02-FRCNN-000001.jpg
        label_name = mot_dir + '-' + '%06d' % frame_id + '.txt'  # MOT17-02-FRCNN-000001.txt
        oldimgpath = os.path.join('test', mot_dir, 'img1',
                                  '%06d' % frame_id + '.jpg')  # train/MOT17-02-FRCNN/img1/000001.jpg
        if frame_id <= seqLength//2:  # 前一半划分给训练集
            newimgpath = os.path.join('images', 'train', image_name)  # images/train/MOT17-02-FRCNN-000001.jpg
            labelpath = os.path.join('labels', 'train', label_name)  # labels/train/MOT17-02-FRCNN-000001.txt
        else:  # 后一半划分给验证集
            newimgpath = os.path.join('images', 'val', image_name)  # images/val/MOT17-02-FRCNN-000001.jpg
            labelpath = os.path.join('labels', 'val', label_name)  # labels/val/MOT17-02-FRCNN-000001.txt
        if not os.path.exists(newimgpath):  # 如果图片没复制过去,就复制,
            shutil.copyfile(oldimgpath, newimgpath)  # 把旧图片复制到新的地方
        with open(labelpath, 'a') as f:  # 写label文件
            f.write(f'0 {box[0]} {box[1]} {box[2]} {box[3]}\n')

convert.py要和数据集放在同一文件夹下,如下:在这里插入图片描述

运行代码即可得到yolo格式的images和labels文件夹
注意:这个程序每次只能处理一个文件夹,当处理train时,要分出一定的比例作为验证集val,当处理test时,则全部都作为测试集,代码中可以修改。

(2)制作索引:

在训练时需要制作索引文件,找到训练集和验证集的images和labels
文件内容如下:
在这里插入图片描述
通过name.py获得一个文件夹中的图片名称,程序如下:

import os

def generate(dir, label):
    files = os.listdir(dir)
    files.sort()
    print
    '****************'
    print
    'input :', dir
    print
    'start...'
    listText = open('all_list.txt', 'a')
    for file in files:
        fileType = os.path.split(file)
        if fileType[1] == '.txt':
            continue
        name = 'MOT17/images/test/'+file + '\n'
        listText.write(name)
    listText.close()
    print
    'down!'
    print
    '****************'

if __name__ == '__main__':
    outer_path = './images'  # 这里是你的图片的目录
    i = 0
    folderlist = os.listdir(outer_path)  # 列举文件夹
    for folder in folderlist:
        generate(os.path.join(outer_path, folder), i)
        i += 1

文件夹的放置顺序如下图所示:
在这里插入图片描述
我们可以把上一步划分好的数据集做好格式,运行程序生成索引。
注意:这个程序读取的名字是000001这种,但是我们需要的是MOT17/images/train/MOT17-02-SDP-000001.jpg这种。所以要修改程序中的这行代码

name = 'MOT17/images/test/'+file + '\n'

要在file前面加上路径,每个序列都不同,所以只能一次转换一个序列
在得到索引文件后,可以看到DPM,FRCNN,SDP三个序列的索引都在txt中,因为三组都是重复的,所以可以只保留FRCNN的索引,删除其他两种。

修改配置文件

添加索引文件的路径;
在这里插入图片描述
mot就是专门放置索引文件的文件夹,如下:
在这里插入图片描述
然后,就可以开始测试了。

三.评测指标:

在这里插入图片描述
使用MOT17 train需要做成和viedrone相同的格式,其中annotations指的是每个序列的gt文件。
最后,附上我自己制作的数据集,仅供参考!
链接:https://pan.baidu.com/s/1mSMR6Gxpa0ErnY9N6kA6UQ
提取码:tr4k
–来自百度网盘超级会员V4的分享


本文转载自: https://blog.csdn.net/weixin_45929472/article/details/127185005
版权归原作者 weixin_45929472 所有, 如有侵权,请联系我们删除。

“多目标跟踪(1):使用MOT17数据集训练YOLOv7检测器”的评论:

还没有评论