-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
123 lines (101 loc) · 4.13 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import json
from collections import defaultdict
from pathlib import Path
from PIL import Image
import torch
from torch.utils.data import Dataset
from torchvision.ops import box_iou
from torchvision.models.detection.faster_rcnn import (
fasterrcnn_resnet50_fpn,
FasterRCNN_ResNet50_FPN_Weights,
# FasterRCNN_ResNet50_FPN_V2_Weights
)
def get_faster_rcnn_model(num_classes):
"""return model and preprocessing transform"""
model = fasterrcnn_resnet50_fpn(
weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT
)
model.roi_heads.box_predictor.cls_score = torch.nn.Linear(
in_features=model.roi_heads.box_predictor.cls_score.in_features,
out_features=num_classes,
bias=True,
)
model.roi_heads.box_predictor.bbox_pred = torch.nn.Linear(
in_features=model.roi_heads.box_predictor.bbox_pred.in_features,
out_features=num_classes * 4,
bias=True,
)
preprocess = FasterRCNN_ResNet50_FPN_Weights.DEFAULT.transforms()
return model, preprocess
class CocoDataset(Dataset):
"""PyTorch dataset for COCO annotations."""
# adapted from https://github.com/pytorch/vision/issues/2720
def __init__(self, root, annFile, transform=None):
"""Load COCO annotation data."""
self.data_dir = Path(root)
self.transform = transform
# load the COCO annotations json
anno_file_path = annFile
with open(str(anno_file_path)) as file_obj:
self.coco_data = json.load(file_obj)
# put all of the annos into a dict where keys are image IDs to speed up retrieval
self.image_id_to_annos = defaultdict(list)
for anno in self.coco_data["annotations"]:
image_id = anno["image_id"]
self.image_id_to_annos[image_id] += [anno]
def __len__(self):
return len(self.coco_data["images"])
def __getitem__(self, index):
"""Return tuple of image and labels as torch tensors."""
image_data = self.coco_data["images"][index]
image_id = image_data["id"]
image_path = self.data_dir / image_data["file_name"]
image = Image.open(image_path)
annos = self.image_id_to_annos[image_id]
anno_data = {
"boxes": [],
"labels": [],
"area": [],
"iscrowd": [],
}
for anno in annos:
coco_bbox = anno["bbox"]
left = coco_bbox[0]
top = coco_bbox[1]
right = coco_bbox[0] + coco_bbox[2]
bottom = coco_bbox[1] + coco_bbox[3]
area = coco_bbox[2] * coco_bbox[3]
anno_data["boxes"].append([left, top, right, bottom])
anno_data["labels"].append(anno["category_id"])
anno_data["area"].append(area)
anno_data["iscrowd"].append(anno["iscrowd"])
target = {
"boxes": torch.as_tensor(anno_data["boxes"], dtype=torch.float32),
"labels": torch.as_tensor(anno_data["labels"], dtype=torch.int64),
"image_id": torch.tensor([image_id]),
"area": torch.as_tensor(anno_data["area"], dtype=torch.float32),
"iscrowd": torch.as_tensor(anno_data["iscrowd"], dtype=torch.int64),
}
# if self.transform:
# input_data = self.transform(input_data)
if self.transform is not None:
image = self.transform(image)
return image, target
def calculate_map(predictions, targets, iou_threshold=0.5):
APs = []
for prediction, target in zip(predictions, targets):
pred_boxes = prediction['boxes']
target_boxes = target['boxes']
if len(pred_boxes) == 0 or len(target_boxes) == 0:
APs.append(0)
continue
iou_matrix = box_iou(pred_boxes, target_boxes)
ious = iou_matrix.max(dim=1)[0] # max IoU for each prediction
true_positive = (ious >= iou_threshold).sum().item()
# Precision: TP / (TP + FP), FP = len(pred_boxes) - TP
if len(pred_boxes) > 0:
precision = true_positive / len(pred_boxes)
else:
precision = 0
APs.append(precision)
return sum(APs) / len(APs) if APs else 0.0