YOLOv7-tiny,通过pycocotools包得到预测大中小尺寸目标的指标值

文章讲述了如何在Yolov7中使用正确的json文件格式进行大中小目标检测,包括数据集准备、使用pycocotools获取COCO指标以及代码示例。重点强调了yolov7的best_predictions.json与COCO标准json的差异。
摘要由CSDN通过智能技术生成

1 参考链接

  • 需要先在环境中安装pycocotools
pip install pycocotools

2 步骤(能行!)

步骤很简单,但难在第一步

  1. 得到待检测的数据集的instances_val2017.json文件,参考魔鬼面具代码和视频即可(我直接使用起来有错,应该是因为数据集分布的问题)
  2. test.py里面定位到anno_json =,然后指明1得到的instances_val2017.json文件的位置(我是像下面那样直接指明的绝对位置)
anno_json = r'G:\pycharmprojects\autodl-yolov7\yolov7-main-biyebase\TXTOCOCO\instances_val2017.json'
  1. test.py中定位到'--save-json', action='store_true',然后加上default=True,(加上就能检测大中小尺寸了,就算json文件格式不对也不会直接报错终止,但是不会出现结果)

以下全为个人学习想法,可不看

1 认识正确的可用在yolov7大中小目标检测的json文件格式

切记

这个yolov7在使用--save_json参数后的best_predictions.json文件与正常的coco格式json标签文件略有不同

  • 它没有categry、images、classes这些属性,而是直接将检测结果装在一个list里面的。当然这个不影响我们的操作,它内部能正确调用到的。重点区别在:
  • 我们转的json(也就是上面步骤1得到的那个json文件),与普通的coco格式差异在:images里面的idannotations里面的image_id都是不带后缀的图片名!!!普通的coco格式里面这两处都是数字编号

圈起来的就是正确信息,如果annotations0 items的话,那就说明转出来的json文件内容有误(下图格式是在AutoDL云服务器上看的,看起来比较清楚,本地上看就比较杂乱。之前一直我运行一直有错,就是因为没有转正确,我的image_id的value值写成了test/Czech_000037,有了test/就报错了)

在这里插入图片描述

instances_val2017.jsonbest_predictions.json对比:

在这里插入图片描述

2 代码(csdn_mogui.py,用于我自己的数据集)

在这里插入图片描述

csdn_mogui.py备份(最原始的可用代码):

import os
import cv2
import json
from tqdm import tqdm
from sklearn.model_selection import train_test_split
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--root_dir', default='/home/hjj/Desktop/dataset/dataset_seaship', type=str, help="root path of images and labels, include ./images and ./labels and classes.txt")
parser.add_argument('--save_path', type=str, default='instances_val2017.json', help="if not split the dataset, give a path to a json file")

arg = parser.parse_args()


def yolo2coco(arg):


    with open(r'G:\pycharmprojects\autodl-yolov7\yolov7-main-biyebase\TXTOCOCO\classes.txt', 'r') as f:
        classes = list(map(lambda x: x.strip(), f.readlines()))

    # --------------- lwd ---------------- #
    cities = ['Czech', 'India', 'Japan']
    indexes = []
    for city in cities:
        city_imagedir = f'F:/A_Publicdatasets/RDD2020-1202/train_valid/{city}/images/test'
        for file in os.listdir(city_imagedir):
            indexes.append(f'{city_imagedir}/{file}')
    # --------------- lwd ---------------- #


    dataset = {'categories': [], 'annotations': [], 'images': []}
    for i, cls in enumerate(classes, 0):
        dataset['categories'].append({'id': i, 'name': cls, 'supercategory': 'mark'})

    # 标注的id
    ann_id_cnt = 0
    for k, index in enumerate(tqdm(indexes)):
        # 支持 png jpg 格式的图片。
        txtPath = index.replace('images', 'labels').replace('.jpg', '.txt')
        # 读取图像的宽和高
        im = cv2.imread(index)
        imageFile = index.split('/')[-1]  # img.jpg

        height, width, _ = im.shape
        # 添加图像的信息
        if not os.path.exists(txtPath):
            # 如没标签,跳过,只保留图片信息。
            continue
        dataset['images'].append({'file_name': imageFile,
                                  'id': int(imageFile[:-4]) if imageFile[:-4].isnumeric() else imageFile[:-4],
                                  'width': width,
                                  'height': height})
        with open(txtPath, 'r') as fr:
            labelList = fr.readlines()
            for label in labelList:
                label = label.strip().split()
                x = float(label[1])
                y = float(label[2])
                w = float(label[3])
                h = float(label[4])

                # convert x,y,w,h to x1,y1,x2,y2
                H, W, _ = im.shape
                x1 = (x - w / 2) * W
                y1 = (y - h / 2) * H
                x2 = (x + w / 2) * W
                y2 = (y + h / 2) * H
                # 标签序号从0开始计算, coco2017数据集标号混乱,不管它了。
                cls_id = int(label[0])
                width = max(0, x2 - x1)
                height = max(0, y2 - y1)
                dataset['annotations'].append({
                    'area': width * height,
                    'bbox': [x1, y1, width, height],
                    'category_id': cls_id,
                    'id': ann_id_cnt,
                    'image_id': int(imageFile[:-4]) if imageFile[:-4].isnumeric() else imageFile[:-4],
                    'iscrowd': 0,
                    # mask, 矩形是从左上角点按顺时针的四个顶点
                    'segmentation': [[x1, y1, x2, y1, x2, y2, x1, y2]]
                })
                ann_id_cnt += 1

    # 保存结果
    with open(arg.save_path, 'w') as f:
        json.dump(dataset, f)
    print('Save annotation to {}'.format(arg.save_path))

if __name__ == "__main__":
    yolo2coco(arg)

3 备注

在其他模型里面,可以关注到下图中的位置,然后更改精度的打印值

在这里插入图片描述

4 使用pycocotools打印更多数据(注意,修改后最好再还原!最好是一次性使用)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟孟单单

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值