前言
本文簡(jiǎn)要介紹YOLOv5如何調(diào)用pycocotools得到大中小目標(biāo)的AP和AR指標(biāo)
,評(píng)價(jià)自制數(shù)據(jù)集。
- 代碼版本-----YOLOv5_6.0版本。
- 數(shù)據(jù)集----Seaships7000數(shù)據(jù)集,共包含6類7000張船舶圖片,其中測(cè)試集1400張。
- 模型-----自制模型。
一、運(yùn)行示例
話不多說(shuō),運(yùn)行示例:
(pytorch1.8) zmy@525:~/文檔/A-YOLO$ python val.py
val: data=data/ship.yaml, weights=runs/train/exp28/weights/best.pt, batch_size=32, imgsz=640, conf_thres=0.001, iou_thres=0.6, task=test, device=, single_cls=False, augment=False, verbose=False, save_txt=False, save_hybrid=False, save_conf=False, save_json=True, project=runs/val, name=exp, exist_ok=False, half=False
YOLOv5 ?? 2021-10-12 torch 1.8.0+cu111 CUDA:0 (NVIDIA GeForce RTX 3090, 24268.3125MB)
Fusing layers...
Model Summary: 540 layers, 4933647 parameters, 0 gradients
test: Scanning '/home/zmy/文檔/A-YOLO/data/labels/test_fog.cache' images and lab
Class Images Labels P R mAP@.5 mAP@
all 1400 1837 0.844 0.543 0.66 0.449
ore carrier 1400 417 0.94 0.448 0.624 0.392
fishing boat 1400 428 0.785 0.613 0.678 0.456
passenger ship 1400 94 0.628 0.628 0.681 0.451
general cargo ship 1400 312 0.865 0.599 0.724 0.509
bulk cargo carrier 1400 392 0.845 0.569 0.682 0.474
container ship 1400 194 1 0.401 0.569 0.415
Speed: 0.1ms pre-process, 1.6ms inference, 1.0ms NMS per image at shape (32, 3, 640, 640)
Evaluating pycocotools mAP... saving runs/val/exp1/best_predictions.json...
loading annotations into memory...
Done (t=0.01s)
creating index...
index created!
Loading and preparing results...
DONE (t=0.13s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type *bbox*
DONE (t=1.27s).
Accumulating evaluation results...
DONE (t=0.36s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.445
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.650
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.497
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.050
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.287
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.458
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.477
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.535
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.535
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.050
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.357
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.549
Results saved to runs/val/exp1
二、參考
主要參考了以下三個(gè)案例,并根據(jù)Seaships數(shù)據(jù)集特征修改了部分代碼。
參考1:安裝pycocotools
參考2:yolov5 調(diào)用cocotools 評(píng)價(jià)自己的模型和數(shù)據(jù)集
參考3:YOLO至COCO的格式轉(zhuǎn)換器
三、方法
1.安裝pycocotools庫(kù)
pip install pycocotools
2.YOLOv5代碼修改
只需修改val.py
文件
1.'--save-json' 添加 default=True parser.add_argument('--save-json', default=True, action='store_true', help='save a COCO-JSON results file')
2.'--task' 修改 default='test' parser.add_argument('--task', default='test', help='train, val, test, speed or study')
3.注釋下句 # opt.save_json |= opt.data.endswith('coco.yaml')
4.為了生成的json文件是多行,方便自查格式
改json.dump(jdict, f)
為json.dump(jdict, f, ensure_ascii=False, indent=1)
修改后終端輸入python val.py
,如下所示:
會(huì)提示我們Evaluating pycocotools mAP… saving runs/val/exp1/best_predictions.json…
并報(bào)錯(cuò)[Errno 2] No such file or directory: ‘…/coco/annotations/instances_val2017.json’
這說(shuō)明此模型需要測(cè)試的json文件已經(jīng)保存在runs/val/exp1/best_predictions.json
但標(biāo)準(zhǔn)的json文件在此路徑../coco/annotations/instances_val2017.json
沒(méi)有找到。
接下來(lái)制作Seaships數(shù)據(jù)集的json文件
test: Scanning '/home/zmy/文檔/A-YOLO/data/labels/test_fog.cache' images and lab
Class Images Labels P R mAP@.5 mAP@
all 1400 1837 0.844 0.543 0.66 0.449
ore carrier 1400 417 0.94 0.448 0.624 0.392
fishing boat 1400 428 0.785 0.613 0.678 0.456
passenger ship 1400 94 0.628 0.628 0.681 0.451
general cargo ship 1400 312 0.865 0.599 0.724 0.509
bulk cargo carrier 1400 392 0.845 0.569 0.682 0.474
container ship 1400 194 1 0.401 0.569 0.415
Speed: 0.1ms pre-process, 1.7ms inference, 1.3ms NMS per image at shape (32, 3, 640, 640)
Evaluating pycocotools mAP... saving runs/val/exp1/best_predictions.json...
loading annotations into memory...
pycocotools unable to run: [Errno 2] No such file or directory: '../coco/annotations/instances_val2017.json'
3.制作.json文件
根據(jù)參考3的README文檔將YOLO標(biāo)簽的txt格式
轉(zhuǎn)換為json格式
只需修改main.py
文件
1、根據(jù)Seaships數(shù)據(jù)集修改類別列表
classes = [
"ore carrier",
"fishing boat",
"passenger ship",
"general cargo ship",
"bulk cargo carrier",
"container ship",]
2、把image_id定義為文件名并去除尾綴
for file_path in file_paths:
# Check how many items have progressed
print("\rProcessing " + str(image_id) + " ...", end='')
# ---------------------image_id定義為文件名--------------------------
image_id = int(file_path.stem)
# ------------------------------------------------------------------
# Build image annotation, known the image's width and height
w, h = imagesize.get(str(file_path))
image_annotation = create_image_annotation(
file_path=file_path, width=w, height=h, image_id=image_id)
images_annotations.append(image_annotation)
label_file_name = f"{file_path.stem}.txt"
3、把標(biāo)簽從1開(kāi)始改為標(biāo)簽從0開(kāi)始
for line1 in label_read_line:
label_line = line1
category_id = (
# int(label_line.split()[0]) + 1) # you start with annotation id with '1'
int(label_line.split()[0]) + 0) # you start with annotation id with '0'
最后將生成的train.json
文件,標(biāo)簽改為從0開(kāi)始,并改名為instances_val2017.json
,然后放到根目錄的coco/annotations/文件夾中,沒(méi)有則需要自己創(chuàng)建。
4.運(yùn)行程序
終端輸入python val.py
,即大功告成!
附錄
需要測(cè)試的best_predictions.json
示例:
[
{
"image_id": 4724,
"category_id": 3,
"bbox": [
838.916,
158.716,
1081.084,
332.775
],
"score": 0.94571
},
{
"image_id": 4724,
"category_id": 1,
"bbox": [
623.036,
369.717,
210.212,
30.21
],
"score": 0.00897
},
{
"image_id": 4724,
"category_id": 4,
"bbox": [
838.773,
159.783,
1081.227,
334.143
],
"score": 0.00734
},
...此處省略其余標(biāo)注
制作的標(biāo)準(zhǔn)的instances_val2017.json
示例:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-789572.html
{
"images": [
{
"file_name": "000013.jpg",
"height": 1080,
"width": 1920,
"id": 13}
...此處省略其余1399個(gè)圖片文件
],
"categories": [
{
"supercategory": "Defect",
"id": 0,
"name": "ore carrier"
},
{
"supercategory": "Defect",
"id": 1,
"name": "fishing boat"
},
...此處省略其余4種類別
],
"annotations": [
{
"id": 1,
"image_id": 13,
"bbox": [
1640.0,
544.0,
280.0,
30.0
],
"area": 8400,
"iscrowd": 0,
"category_id": 0,
"segmentation": []
},
...此處省略其余標(biāo)注
總結(jié)
調(diào)用pycocotools得到的指標(biāo)略低于前者文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-789572.html
到了這里,關(guān)于YOLOv5獲得大中小目標(biāo)的AP和AR指標(biāo)(自制數(shù)據(jù)集)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!