用自己标注的数据集测试目标检测模型YOLO+Faster R-CNN

本文记录了使用YOLOv5和Faster R-CNN模型进行车辆目标检测的测试过程。作者首先介绍了数据集的制作,包括标注和文件格式转换,然后详细阐述了模型的测试步骤,包括环境配置、模型准备、yaml文件编写、测试执行、可视化结果绘制及相似类别图片筛选。
摘要由CSDN通过智能技术生成

车辆目标检测模型测试记录



前言

首先自己标注了2006张线上数据,制作成了测试数据集test1(1000张)、test2(1006张),其中test1是随机选取的线上模型检测出车辆图片,test2是随机选取的线上模型未检测出车辆图片。数据标注全部使用Labelme软件,生成格式为.json文件,bbox格式为:<left> <top> <right> <bottom>。标注过程test1以露出完整车辆外观三分之一为准,test2以人眼能识别出车辆为准,以下测试均使用test1与test2数据集。


一、YOLOv5系列模型测试记录

官方github:https://github.com/ultralytics/yolov5

1. 环境配置

yolov5要求:Python>=3.6.0、PyTorch>=1.7,建立conda虚拟环境之后安装各依赖库:

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

2. 数据集的制作

官方提供的数据集的tree:

mytrain
├── mycoco
│   ├── images
│   │   ├── train
│   │   └── val
│   └── labels
│       ├── train
│       └── val
└── yolov5

针对测试任务、无需建立train文件夹,我建立的数据集结构如下:

home
├── car
│   ├── test1.txt
│   ├── test2.txt
│   ├── test1
│   └── test2
└── yolov5

其中test1和test2中存储图片文件与对应的label文件,命名规则例如:

img1.jpg  img1.txt  img2.jpg  img2.txt ...

test1.txt与test2.txt中存放完整文件列表,例如:

./test1/img1.jpg
./test1/img2.jpg
...

建立好文件目录之后我们可以生成相应的文件了,由于我们直接使用Labelme软件进行标注,生成.json格式的label文件,例如:

{
  "version": "4.5.7",
  "flags": {},
  "shapes": [
    {
      "label": "2",
      "points": [
        [
          5.543307086614163,
          333.8582677165354
        ],
        [
          352.7874015748032,
          555.1181102362204
        ]
      ],
      "group_id": null,
      "shape_type": "rectangle",
      "flags": {}
    }
  ],
  "imagePath": "1623513693636_file.jpg",
  "imageData": null,
  "imageHeight": 800,
  "imageWidth": 370
}

由于标注完成后可能出现imagePath字段与文件真实名称不一致的情况,所以我仍使用图片文件真实的文件名。同理,imageHeightimageWidth也通过读入图片进行获取。
为了方便与COCO数据集结合,我在标注时把所有的车辆的label都标注为2
points格式为:<left> <top> <right> <bottom>,注意,有时由于标注点的顺序,可能为<right> <bottom> <left> <top>,所以要取最大最小值处理。
YOLOv5要求训练或测试的输入格式为每张图片的label为一个.txt文件,并且文件名与图片文件名一致,文件内容例如:
在这里插入图片描述
其中每行是对应的一个label,后四个值分别为标注矩形框的中心点x,y坐标与框宽,框高归一化之后的值。遂编写了脚本,实现了由标注.json文件生成YOLOv5要求的.txtlabelme_to_yolov5.py

import argparse
import os
import numpy as np
import json
from glob import glob
import cv2
from os import getcwd
from tqdm import tqdm


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', default='LabelmeData/')
    parser.add_argument('--out_dir', default='tmp/')
    return parser.parse_args()


# 根据xmin,xmax,ymin,ymax返回当前矩形的中心点坐标与长宽
def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)


def Change2Yolo5(json_files, task="test"):
    if not os.path.isdir(get_args().out_dir):
        os.makedirs(get_args().out_dir)

    # 创建当前数据集的txt文件,例 test.txt
    list_file = open('tmp/%s.txt' % (task), 'w')
    for json_name in tqdm(json_files, ncols=100, unit='B', unit_scale=True):
        jsonPath = get_args().input + json_name + ".json"
        imagePath = get_args().input + json_name + ".jpg"
        # 写入一行图片路径
        list_file.write('%s/%s\n' % (getcwd(), imagePath))
        # 创建当前图片的标签txt文件
        label_file = open('%s/%s.txt' % (get_args().input, json_name), 'w')
        json_file = json.load(open(jsonPath, "r", encoding="utf-8"))
        print(json_file)
        height, width, channels = cv2.imread(get_args().input + json_name + ".jpg").shape
        #print(cv2.imread(get_args().input + json_name + ".jpg").shape)

        for multi in json_file["shapes"]:
            points = np.array(multi["points"])
            f = lambda x: x if x >= 0 else 0
            xmin = f(min(points[:, 0]))
            xmax = f(max(points[:, 0]))
            ymin = f(min(points[:, 1]))
            ymax = f(max(points[:, 1]))

            if not xmax >= xmin or ymax >=ymin:
                cls_id = multi["label"]
                b = (float(xmin), float(xmax), float(ymin), float(ymax))
                bb = convert((width, height), b)
                label_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
                #print(jsonPath, xmin, ymin, xmax, ymax, cls_id)


def main(args):
    files = glob(args.input + "*.json")
    files = [i.split("/")[-1].split(".json")[0] for i in files]
    # print(files)
    # trainval_files = files
    test_files = files
    Change2Yolo5(test_files,"test")


if __name__ == '__main__':
    main(get_args())

运行完成后会在对应图片文件与.json文件目录生成对应的.txt文件以及文件列表文件,将文件按之前的文件树组织即可。

3. 模型的准备

完成了数据集的组织后我们可以开始我们的测试或训练了,YOLOv5的模型包括如下几种:
在这里插入图片描述
本文我主要针对前四个模型进行了测试,我直接使用了YOLOv5官方提供的用COCO2017数据集训练完成的模型,下载链接请移步:https://github.com/ultralytics/yolov5/releases
在这里插入图片描述
下载后将其放入yolov5主目录下,模型就准备就绪了。

4. .yaml文件的编写

./yolov5/data/目录下自己新建一个car1.yaml,代码如下:


# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: ../car/test1.txt  # 118287 images
val: ../car/test1.txt  # 5000 images
test: ../car/test1.txt  # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794

# number of classes
nc: 80

# class names
names: [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', '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', 'couch',
         'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
         'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
         'hair drier', 'toothbrush' ]

同理,新建一个car2.yaml,代码如下:

# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: ../car/test2.txt  # 118287 images
val: ../car/test2.txt  # 5000 images
test: ../car/test2.txt  # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794

# number of classes
nc: 80

# class names
names: [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
         'fire hydrant', 'stop sign', 'parking meter&
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值