文章目录
1 参考链接
- 需要先在环境中安装
pycocotools
pip install pycocotools
-
一些概念上的了解:通过pycocotools获取每个类别的COCO指标
2 步骤(能行!)
步骤很简单,但难在第一步:
- 得到待检测的数据集的
instances_val2017.json
文件,参考魔鬼面具代码和视频即可(我直接使用起来有错,应该是因为数据集分布的问题) - 在
test.py
里面定位到anno_json =
,然后指明1
得到的instances_val2017.json
文件的位置(我是像下面那样直接指明的绝对位置)
anno_json = r'G:\pycharmprojects\autodl-yolov7\yolov7-main-biyebase\TXTOCOCO\instances_val2017.json'
- 在
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
里面的id
和annotations
里面的image_id
都是不带后缀的图片名
!!!普通的coco格式里面这两处都是数字编号
圈起来的就是正确信息,如果annotations
为0 items
的话,那就说明转出来的json文件内容有误(下图格式是在AutoDL云服务器上看的,看起来比较清楚,本地上看就比较杂乱。之前一直我运行一直有错,就是因为没有转正确,我的image_id
的value值写成了test/Czech_000037
,有了test/
就报错了)
instances_val2017.json
和best_predictions.json
对比:
2 代码(csdn_mogui.py,用于我自己的数据集)
- 参考另一篇博文:将YOLO数据集转成COCO格式,单个文件夹转为单个json文件,例如…/images/train转为instance_train.json,然后做如下两处改动也可以得到同样正确的可用于YOLOv7大中小目标检测
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 备注
在其他模型里面,可以关注到下图中的位置,然后更改精度的打印值