日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

将结构体写入文件_将COCO检测结果写入json文件

發布時間:2023/12/10 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 将结构体写入文件_将COCO检测结果写入json文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近很多朋友留言問我如何將檢測結果寫入json文件并且用于COCO API的評估,之前對于檢測結果的格式已經做了簡單的說明,這里提供一些簡單的函數,直接調用將結果寫入即可。

用于COCO API測試的文件格式

HUST小菜雞:用于COCO API測試的結果的文件格式?zhuanlan.zhihu.com

使用COCO API進行結果評估

HUST小菜雞:使用COCO API評估模型在COCO數據集上的結果?zhuanlan.zhihu.com

COCO utils給出了一些轉換的函數

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

其他的實現方式也差不多和這個相同

...... 省略部分未模型初始化等操作部分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)

總結

以上是生活随笔為你收集整理的将结构体写入文件_将COCO检测结果写入json文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。