在mmdetection中推理无标注图像文件并输出检测结果(json文件)

使用不带标签的数据集对模型进行检测

不带标签图像数据集转化

MMDetection 支持在不使用 ground-truth 标注的情况下对模型进行测试,这需要用到 CocoDataset。如果你的数据集格式不是 COCO 格式的,请将其转化成 COCO 格式。如果你的数据集格式是 VOC 或者 Cityscapes,你可以使用 tools/dataset_converters 内的脚本直接将其转化成 COCO 格式。如果是其他格式,可以使用 images2coco 脚本进行转换。

python tools/dataset_converters/images2coco.py \
    ${IMG_PATH} \
    ${CLASSES} \
    ${OUT} \
    [--exclude-extensions]

参数

  • IMG_PATH是未标注的图像路径
  • CLASSES是一个文本文件,里面包含你的类别名,每行存储一个类别名
  • OUT:输出的json文件名,默认路径和IMG_PATH是同一路径
  • exclude-extensions:待排除文件后缀名
#例如:
python tools/dataset_converters/images2coco.py \
    image \
    label.txt \
    test \

生成图片的json后,json里的图片顺序可能和文件夹中的图片顺序,不一致,使用以下代码排序

import json

# 读取原始JSON文件
with open('data/annotations/sheep.json', 'r') as input_file:
    data = json.load(input_file)

# 修改id和file_name的对应关系
for item in data["images"]:
    id = item["id"]
    item["file_name"] = f"data/test1/{str(id).zfill(6)}.jpg"  # 根据id生成新的file_name,,我的图片顺序为000000-003559,这个需要根据自己的需求改
# 将修改后的数据写入一个新的JSON文件
with open('output.json', 'w') as output_file:
    json.dump(data, output_file, indent=4)

print("处理完成并已将结果保存到output.json文件。")

使用模型对其推理

# 单 GPU 测试
python tools/test.py \
    ${CONFIG_FILE} \
    ${CHECKPOINT_FILE} \
    --format-only \
    --options ${JSONFILE_PREFIX} \
    [--show]

#CONFIG_FILE:配置文件(只需修改test里的ann_file='output.json',img_prefix='')
#CHECKPOINT_FILE:已经训练好的模型路径
#--options "jsonfile_prefix=./"
# 单节点多 GPU 测试
bash tools/dist_test.sh \
    ${CONFIG_FILE} \
    ${CHECKPOINT_FILE} \
    ${GPU_NUM} \
    --format-only \
    --options ${JSONFILE_PREFIX} \
    [--show]
#例如
python tools/test.py \
    configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py \
    work_dirs/mask-soft/epoch_48.pth \
    -format-only \
    --options "jsonfile_prefix=./mask_rcnn"

从推理得到的json文件提取标签数

import json
import csv

# 读取JSON文件
with open('mask_rcnn.bbox.json', 'r') as json_file:
    data = json.load(json_file)

# 初始化一个字典来存储每张图片的标签个体数
image_labels = {}

# 遍历JSON数据
for entry in data:
    if 'image_id' in entry:
        image_id = entry['image_id']
        if 'score' in entry and entry['score'] > 0.7:#置信度根据自己要求设置
            category_id = entry['category_id']
            if image_id in image_labels:
                if category_id in image_labels[image_id]:
                    image_labels[image_id][category_id] += 1
                else:
                    image_labels[image_id][category_id] = 1
            else:
                image_labels[image_id] = {category_id: 1}

# 创建一个映射,将Category ID映射为对应的标签
category_mapping = {1: 'class1', 2: 'class2'}#根据自己类别设置

# 将数据写入CSV文件,并应用映射
with open('time.csv', 'w', newline='') as csv_file:
    fieldnames = ['Image ID', 'class1', 'class2']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()
    for image_id, categories in image_labels.items():
        image_row = {'Image ID': image_id, 'class1': 0, 'class2': 0}
        for category_id, count in categories.items():
            category = category_mapping.get(category_id, str(category_id))
            image_row[category] = count
        writer.writerow(image_row)
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值