COCO Model mAP Eval

This commit is contained in:
lxm 2023-08-13 16:05:24 +08:00
parent cf7fad3b2b
commit 6d0f83b4d3
4 changed files with 53 additions and 172 deletions

View File

@ -5,7 +5,7 @@ import os
if __name__ == '__main__': if __name__ == '__main__':
path = os.path.abspath(os.path.join(os.getcwd(),"../../..")) path = os.path.abspath(os.path.join(os.getcwd(),"../../.."))
pred_json = 'pd_coco.json' pred_json = 'pd_coco.json'
anno_json = path + '/val2017/gt_coco.json' anno_json = path + '/val2017/instances_val2017.json'
# use COCO API to load forecast results and annotations # use COCO API to load forecast results and annotations
cocoGt = COCO(anno_json) cocoGt = COCO(anno_json)

View File

@ -65,7 +65,7 @@ int main(int argc, char *argv[])
//create pred file //create pred file
std::string val_image_name = GetImageFileName(val_image[i]); std::string val_image_name = GetImageFileName(val_image[i]);
std::string filename = folder+ val_image_name + ".txt"; std::string filename = folder+"/"+ val_image_name + ".txt";
std::ofstream file(filename); std::ofstream file(filename);
file.is_open(); file.is_open();
file<<std::fixed<<std::setprecision(6); file<<std::fixed<<std::setprecision(6);

View File

@ -1,123 +0,0 @@
# -*- coding: UTF-8 -*-
"""
@Author: lxm
@version V1.0
"""
import datetime
import json
import os
import cv2
# 将yolo格式的数据集转换成coco格式的数据集
# 读取文件夹下的所有文件
path = os.path.abspath(os.path.join(os.getcwd(),"../../.."))
images_path = path+'/val2017/val2017'
labels_path = path+'/val2017/labels'
coco_json_save = path + '/val2017/gt_coco.json'
# 创建coco格式的json文件
coco_json = {
'info': {
"description": "COCOVal Dataset",
"url": "www.amov.com",
"version": "1.0",
"year": 2023,
"contributor": "lxm",
"date_created": datetime.datetime.utcnow().isoformat(' ')
},
"licenses": [
{
"url": "http://creativecommons.org/licenses/by-nc-sa/2.0/",
"id": 1,
"name": "Attribution-NonCommercial-ShareAlike License"
}
],
'images': [],
'annotations': [],
'categories': []
}
# COCO classes
classes = ["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"]
# 创建coco格式的json文件
for i, c in enumerate(classes):
coco_json['categories'].append({'id': i + 1, 'name': c, 'supercategory': c})
# 读取images文件夹下的所有文件
images = os.listdir(images_path)
for image in images:
# 获取图片名和后缀
image_name, image_suffix = os.path.splitext(image)
# 获取图片的宽和高
image_path = images_path + '/' + image
img = cv2.imread(image_path)
height, width, _ = img.shape
# 添加图片信息
coco_json['images'].append({
'id': int(image_name),
'file_name': image,
'width': width,
'height': height,
'date_captured': datetime.datetime.utcnow().isoformat(' '),
'license': 1
})
# 读取图片对应的标签文件
label_path = labels_path + '/' + image_name + '.txt'
if not os.path.exists(label_path):
continue
with open(label_path, 'r') as f:
labels = f.readlines()
labels = [l.strip() for l in labels]
for j, label in enumerate(labels):
label = label.split(' ')
# 获取类别id
category_id = int(label[0])
# 将yolo格式的数据转换成coco格式的数据
x = float(label[1]) * width
y = float(label[2]) * height
w = float(label[3]) * width
h = float(label[4]) * height
xmin = x - w / 2
ymin = y - h / 2
xmax = x + w / 2
ymax = y + h / 2
# 添加bbox信息
# 添加bbox信息
coco_json['annotations'].append({
'image_id': int(image_name),
'category_id': category_id + 1,
'bbox': [xmin, ymin, w, h],
'id': len(coco_json['annotations']),
'area': w * h,
'iscrowd': 0,
'segmentation': [],
'attributes': ""
})
# 保存json文件
with open(coco_json_save, 'w') as f:
json.dump(coco_json, f, indent=2)
print(len(coco_json['images']), len(coco_json['annotations']), len(coco_json['categories']), 'Done!')

View File

@ -14,6 +14,10 @@ coco_json_save ='pd_coco.json'
# config coco_json # config coco_json
coco_json = [] coco_json = []
# remap the id of the coco dataset
id_map = {1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7, 9: 8, 10: 9, 11: 10, 13: 11, 14: 12, 15: 13, 16: 14, 17: 15, 18: 16, 19: 17, 20: 18, 21: 19, 22: 20, 23: 21, 24: 22, 25: 23, 27: 24, 28: 25, 31: 26, 32: 27, 33: 28, 34: 29, 35: 30, 36: 31, 37: 32, 38: 33, 39: 34, 40: 35, 41: 36, 42: 37, 43: 38, 44: 39, 46: 40, 47: 41, 48: 42, 49: 43, 50: 44, 51: 45, 52: 46, 53: 47, 54: 48, 55: 49, 56: 50, 57: 51, 58: 52, 59: 53, 60: 54, 61: 55, 62: 56, 63: 57, 64: 58, 65: 59, 67: 60, 70: 61, 72: 62, 73: 63, 74: 64, 75: 65, 76: 66, 77: 67, 78: 68, 79: 69, 80: 70, 81: 71, 82: 72, 84: 73, 85: 74, 86: 75, 87: 76, 88: 77, 89: 78, 90: 79}
reid_mp = { value: key for key, value in id_map.items()}
# load images dir # load images dir
images = os.listdir(images_path) images = os.listdir(images_path)
for image in images: for image in images:
@ -46,7 +50,7 @@ for image in images:
coco_json.append({ coco_json.append({
'image_id': int(image_name), 'image_id': int(image_name),
'category_id': category_id + 1, 'category_id': int(reid_mp[category_id]),
'bbox': [xmin, ymin, w, h], 'bbox': [xmin, ymin, w, h],
'score': float(pred[5]), 'score': float(pred[5]),
'area': w * h}) 'area': w * h})