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如下;

  1. '''
  2. 创建以下四个目录,用于存放图片和标签
  3. images/train
  4. images/val
  5. labels/train
  6. labels/val
  7. '''
  8. import os
  9. import shutil
  10. import numpy as np
  11. import configparser
  12. if not os.path.exists('images'):
  13. os.makedirs('images/train')
  14. os.makedirs('images/val')
  15. if not os.path.exists('labels'):
  16. os.makedirs('labels/train')
  17. os.makedirs('labels/val')
  18. def convert(imgWidth, imgHeight, left, top, width, height):
  19. x = (left + width / 2.0) / imgWidth
  20. y = (top + height / 2.0) / imgHeight
  21. w = width / imgWidth
  22. h = height / imgHeight
  23. return ('%.6f'%x, '%.6f'%y, '%.6f'%w, '%.6f'%h) # 保留6位小数
  24. for mot_dir in os.listdir('test'): # mot_dir是例如MOT17-02-FRCNN这种
  25. det_path = os.path.join('test', mot_dir, 'det/det.txt') # det.txt路径
  26. dets = np.loadtxt(det_path, delimiter=',') # 读取det.txt文件
  27. ini_path = os.path.join('test', mot_dir, 'seqinfo.ini') # seqinfo.ini路径
  28. conf = configparser.ConfigParser()
  29. conf.read(ini_path) # 读取seqinfo.ini文件
  30. seqLength = int(conf['Sequence']['seqLength']) # MOT17-02-FRCNN序列的长度
  31. imgWidth = int(conf['Sequence']['imWidth']) # 图片宽度
  32. imgHeight = int(conf['Sequence']['imHeight']) # 图片长度
  33. for det in dets:
  34. frame_id, _, left, top, width, height = int(det[0]), det[1], det[2], det[3], det[4], det[5]
  35. box = convert(imgWidth, imgHeight, left, top, width, height)
  36. if '-' in ''.join(box) or float(box[0]) > 1.0 or float(box[1]) > 1.0 or float(box[2]) > 1.0 or float(
  37. box[3]) > 1.0:
  38. print(imgWidth, imgHeight, left, top, width, height)
  39. print(box)
  40. break
  41. image_name = mot_dir + '-' + '%06d' % frame_id + '.jpg' # MOT17-02-FRCNN-000001.jpg
  42. label_name = mot_dir + '-' + '%06d' % frame_id + '.txt' # MOT17-02-FRCNN-000001.txt
  43. oldimgpath = os.path.join('test', mot_dir, 'img1',
  44. '%06d' % frame_id + '.jpg') # train/MOT17-02-FRCNN/img1/000001.jpg
  45. if frame_id <= seqLength//2: # 前一半划分给训练集
  46. newimgpath = os.path.join('images', 'train', image_name) # images/train/MOT17-02-FRCNN-000001.jpg
  47. labelpath = os.path.join('labels', 'train', label_name) # labels/train/MOT17-02-FRCNN-000001.txt
  48. else: # 后一半划分给验证集
  49. newimgpath = os.path.join('images', 'val', image_name) # images/val/MOT17-02-FRCNN-000001.jpg
  50. labelpath = os.path.join('labels', 'val', label_name) # labels/val/MOT17-02-FRCNN-000001.txt
  51. if not os.path.exists(newimgpath): # 如果图片没复制过去,就复制,
  52. shutil.copyfile(oldimgpath, newimgpath) # 把旧图片复制到新的地方
  53. with open(labelpath, 'a') as f: # 写label文件
  54. 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获得一个文件夹中的图片名称,程序如下:

  1. import os
  2. def generate(dir, label):
  3. files = os.listdir(dir)
  4. files.sort()
  5. print
  6. '****************'
  7. print
  8. 'input :', dir
  9. print
  10. 'start...'
  11. listText = open('all_list.txt', 'a')
  12. for file in files:
  13. fileType = os.path.split(file)
  14. if fileType[1] == '.txt':
  15. continue
  16. name = 'MOT17/images/test/'+file + '\n'
  17. listText.write(name)
  18. listText.close()
  19. print
  20. 'down!'
  21. print
  22. '****************'
  23. if __name__ == '__main__':
  24. outer_path = './images' # 这里是你的图片的目录
  25. i = 0
  26. folderlist = os.listdir(outer_path) # 列举文件夹
  27. for folder in folderlist:
  28. generate(os.path.join(outer_path, folder), i)
  29. i += 1

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

  1. 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检测器”的评论:

还没有评论