YOLOv7官方代码库的介绍文档里说,运行test命令后,能输出COCO格式的各项测试指标。而我们自己运行后却并不会。而这个功能在进行算法性能分析和对比实验时十分必要。

解决方法如下,共3个步骤:
1. 修改测试命令。在YOLOv7的test.py文件中,运行COCO格式测试的代码如下。

可以看到,有个if判断。要运行后面的指标测试代码,必须让if判断语句中的‘save_json’为True。这就需要在测试命令的末尾加上“--save-json”。修改后的测试命令示例如下:
python test.py --data data/coco.yaml --img 640 --batch 32 --conf 0.001 --iou 0.65 --device 0 --weights yolov7.pt --name yolov7_640_val --save-json
其中’ data/coco.yaml’替换为你的数据集配置文件路径,‘yolov7.pt’替换为你的模型文件路径,‘yolov7_640_val’替换为你的结果存储文件夹名称。其他参数根据需要调整。
另外,如果你的数据集是按照train、val、test划分,你想在测试集上测指标,那么需要在上述命令中加‘--task test’,否则是在验证集上测指标。
python test.py --data data/coco.yaml --img 640 --batch 32 --conf 0.001 --iou 0.65 --device 0 --weights yolov7.pt --name yolov7_640_val --save-json --task test
2. 安装pycocotools 库。YOLOv7环境配置时并未安装pycocotools库,需要自己安装。首先更新pip:
python -m pip install --upgrade pip
执行安装:
pip install pycocotools
如安装不成功,可尝试源码安装。自己百度一下,有很多教程。
3. 生成COCO格式的标签文件。新建Python文件,将以下代码粘贴其中。代码来自参考博客,我稍作了修改,使得生成的标签文件中image_id格式与YOLOv7的test.py文件中生成预测结果的image_id格式一致。
# coding:utf-8
import os
import cv2
import json
from tqdm import tqdm
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
####请注意修改这块的内容哦
parser.add_argument('--root_dir', default='./NWPU_dataset', 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):
root_path = arg.root_dir
print("Loading data from ", root_path)
assert os.path.exists(root_path)
###标签路径
originLabelsDir = os.path.join(root_path, 'labels')
# originLabelsDir = os.path.join(root_path, 'labels/test/')
###图片路径
originImagesDir = os.path.join(root_path, 'images')
# originImagesDir = os.path.join(root_path, 'images/test/')
###类别文件路径
with open(os.path.join(root_path, 'classes.txt')) as f:
classes = list(map(lambda x: x.strip(), f.readlines()))
# images dir name
indexes = os.listdir(originImagesDir)
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 格式的图片。
txtFile = index.replace('images', 'txt').replace('.jpg', '.txt').replace('.png', '.txt')
path = Path(index)
# 读取图像的宽和高
im = cv2.imread(os.path.join(originImagesDir, index))
height, width, _ = im.shape
# 添加图像的信息
if not os.path.exists(os.path.join(originLabelsDir, txtFile)):
# 如没标签,跳过,只保留图片信息。
continue
dataset['images'].append({'file_name': index,
'id': int(path.stem) if path.stem.isnumeric() else path.stem,
'width': width,
'height': height})
with open(os.path.join(originLabelsDir, txtFile), '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(path.stem) if path.stem.isnumeric() else path.stem,
'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)
另外,我的数据集中图像和标签存放格式如下。labels里存的是YOLO格式的txt标签。每张图像对应一个标签文件。
mydataset
|--train
|--images
|--labels
|--val
|--images
|--labels
|--test
|--images
|--labels
还需要额外准备一个存类别名称的classes.txt文件,做标签格式转换时要用。每行只放一个类别名称。例如:
Dog
Cat
Bird
运行上述标签格式转换脚本时,在命令行传入两个参数:测试集文件夹地址'--root_dir',和转换后得到的json文件存放的地址。第一个根据实际情况设置;第二个设置为‘./coco/annotations/instances_val2017.json’,以跟YOLOv7的test脚本中的保持一致。若要设置别的路径或文件名,则记得在test脚本中做相应修改。第二个路径需要事先创建好相应的文件夹。
至此,准备工作完成了。运行步骤1中修改后的测试命令,得到COCO格式测试指标。

1753

被折叠的 条评论
为什么被折叠?



