将结构体写入文件_将COCO检测结果写入json文件
生活随笔
收集整理的這篇文章主要介紹了
将结构体写入文件_将COCO检测结果写入json文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近很多朋友留言問我如何將檢測結(jié)果寫入json文件并且用于COCO API的評估,之前對于檢測結(jié)果的格式已經(jīng)做了簡單的說明,這里提供一些簡單的函數(shù),直接調(diào)用將結(jié)果寫入即可。
用于COCO API測試的文件格式
HUST小菜雞:用于COCO API測試的結(jié)果的文件格式?zhuanlan.zhihu.com使用COCO API進(jìn)行結(jié)果評估
HUST小菜雞:使用COCO API評估模型在COCO數(shù)據(jù)集上的結(jié)果?zhuanlan.zhihu.comCOCO utils給出了一些轉(zhuǎn)換的函數(shù)
def det2json(dataset, results):json_results = []for idx in range(len(dataset)):img_id = dataset.img_ids[idx]result = results[idx]for label in range(len(result)):bboxes = result[label]for i in range(bboxes.shape[0]):data = dict()data['image_id'] = img_iddata['bbox'] = xyxy2xywh(bboxes[i])data['score'] = float(bboxes[i][4])data['category_id'] = dataset.cat_ids[label]json_results.append(data)return json_resultsdef results2json(dataset, results, out_file):result_files = dict()if isinstance(results[0], list):json_results = det2json(dataset, results)result_files['bbox'] = '{}.{}.json'.format(out_file, 'bbox')result_files['proposal'] = '{}.{}.json'.format(out_file, 'bbox')mmcv.dump(json_results, result_files['bbox'])elif isinstance(results[0], tuple):json_results = segm2json(dataset, results)result_files['bbox'] = '{}.{}.json'.format(out_file, 'bbox')result_files['proposal'] = '{}.{}.json'.format(out_file, 'bbox')result_files['segm'] = '{}.{}.json'.format(out_file, 'segm')mmcv.dump(json_results[0], result_files['bbox'])mmcv.dump(json_results[1], result_files['segm'])elif isinstance(results[0], np.ndarray):json_results = proposal2json(dataset, results)result_files['proposal'] = '{}.{}.json'.format(out_file, 'proposal')mmcv.dump(json_results, result_files['proposal'])else:raise TypeError('invalid type of results')return result_files其他的實(shí)現(xiàn)方式也差不多和這個(gè)相同
...... 省略部分未模型初始化等操作部分if not distributed:model = MMDataParallel(model, device_ids=[0])outputs = single_gpu_test(model, data_loader, args.show, args.save_img, args.save_img_dir)else:model = MMDistributedDataParallel(model.cuda())outputs = multi_gpu_test(model, data_loader, args.tmpdir)res = []for id, boxes in enumerate(outputs):boxes=boxes[0]if type(boxes) == list:boxes = boxes[0]boxes[:, [2, 3]] -= boxes[:, [0, 1]]if len(boxes) > 0:for box in boxes:temp = dict()temp['image_id'] = id+1temp['category_id'] = 1temp['bbox'] = box[:4].tolist()temp['score'] = float(box[4])res.append(temp)with open(args.out, 'w') as f:json.dump(res, f)總結(jié)
以上是生活随笔為你收集整理的将结构体写入文件_将COCO检测结果写入json文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4.4秒破百!2022款唐EV正式上市:
- 下一篇: [UE4]性能优化指南(程序向)