Spaces:
Runtime error
Runtime error
Update coco_utils.py
Browse files- coco_utils.py +87 -20
coco_utils.py
CHANGED
|
@@ -27,24 +27,6 @@ def is_dist_avail_and_initialized():
|
|
| 27 |
return False
|
| 28 |
return True
|
| 29 |
|
| 30 |
-
class CocoDetection(torchvision.datasets.CocoDetection):
|
| 31 |
-
def __init__(self, img_folder, feature_extractor, ann_file):
|
| 32 |
-
super(CocoDetection, self).__init__(img_folder, ann_file)
|
| 33 |
-
self.feature_extractor = feature_extractor
|
| 34 |
-
|
| 35 |
-
def __getitem__(self, idx):
|
| 36 |
-
# read in PIL image and target in COCO format
|
| 37 |
-
img, target = super(CocoDetection, self).__getitem__(idx)
|
| 38 |
-
|
| 39 |
-
# preprocess image and target (converting target to DETR format, resizing + normalization of both image and target)
|
| 40 |
-
image_id = self.ids[idx]
|
| 41 |
-
target = {'image_id': image_id, 'annotations': target}
|
| 42 |
-
encoding = self.feature_extractor(images=img, annotations=target, return_tensors="pt")
|
| 43 |
-
pixel_values = encoding["pixel_values"].squeeze() # remove batch dimension
|
| 44 |
-
target = encoding["labels"][0] # remove batch dimension
|
| 45 |
-
|
| 46 |
-
return pixel_values, target
|
| 47 |
-
|
| 48 |
|
| 49 |
def get_world_size():
|
| 50 |
if not is_dist_avail_and_initialized():
|
|
@@ -150,6 +132,83 @@ class CocoEvaluator(object):
|
|
| 150 |
for iou_type, coco_eval in self.coco_eval.items():
|
| 151 |
print("IoU metric: {}".format(iou_type))
|
| 152 |
coco_eval.summarize()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
def prepare(self, predictions, iou_type):
|
| 155 |
if iou_type == "bbox":
|
|
@@ -168,9 +227,17 @@ class CocoEvaluator(object):
|
|
| 168 |
continue
|
| 169 |
|
| 170 |
boxes = prediction["boxes"]
|
|
|
|
|
|
|
| 171 |
boxes = convert_to_xywh(boxes).tolist()
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
coco_results.extend(
|
| 176 |
[
|
|
|
|
| 27 |
return False
|
| 28 |
return True
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def get_world_size():
|
| 32 |
if not is_dist_avail_and_initialized():
|
|
|
|
| 132 |
for iou_type, coco_eval in self.coco_eval.items():
|
| 133 |
print("IoU metric: {}".format(iou_type))
|
| 134 |
coco_eval.summarize()
|
| 135 |
+
|
| 136 |
+
def _post_process_stats(self, stats, coco_eval_object, iou_type='bbox'):
|
| 137 |
+
# bbox & segm:
|
| 138 |
+
# stats[0] = _summarize(1)
|
| 139 |
+
# stats[1] = _summarize(1, iouThr=.5, maxDets=self.params.maxDets[2])
|
| 140 |
+
# stats[2] = _summarize(1, iouThr=.75, maxDets=self.params.maxDets[2])
|
| 141 |
+
# stats[3] = _summarize(1, areaRng='small', maxDets=self.params.maxDets[2])
|
| 142 |
+
# stats[4] = _summarize(1, areaRng='medium', maxDets=self.params.maxDets[2])
|
| 143 |
+
# stats[5] = _summarize(1, areaRng='large', maxDets=self.params.maxDets[2])
|
| 144 |
+
# stats[6] = _summarize(0, maxDets=self.params.maxDets[0])
|
| 145 |
+
# stats[7] = _summarize(0, maxDets=self.params.maxDets[1])
|
| 146 |
+
# stats[8] = _summarize(0, maxDets=self.params.maxDets[2])
|
| 147 |
+
# stats[9] = _summarize(0, areaRng='small', maxDets=self.params.maxDets[2])
|
| 148 |
+
# stats[10] = _summarize(0, areaRng='medium', maxDets=self.params.maxDets[2])
|
| 149 |
+
# stats[11] = _summarize(0, areaRng='large', maxDets=self.params.maxDets[2])
|
| 150 |
+
|
| 151 |
+
# keypoints:
|
| 152 |
+
# stats[0] = _summarize(1, maxDets=20)
|
| 153 |
+
# stats[1] = _summarize(1, maxDets=20, iouThr=.5)
|
| 154 |
+
# stats[2] = _summarize(1, maxDets=20, iouThr=.75)
|
| 155 |
+
# stats[3] = _summarize(1, maxDets=20, areaRng='medium')
|
| 156 |
+
# stats[4] = _summarize(1, maxDets=20, areaRng='large')
|
| 157 |
+
# stats[5] = _summarize(0, maxDets=20)
|
| 158 |
+
# stats[6] = _summarize(0, maxDets=20, iouThr=.5)
|
| 159 |
+
# stats[7] = _summarize(0, maxDets=20, iouThr=.75)
|
| 160 |
+
# stats[8] = _summarize(0, maxDets=20, areaRng='medium')
|
| 161 |
+
# stats[9] = _summarize(0, maxDets=20, areaRng='large')
|
| 162 |
+
if iou_type not in ['bbox', 'segm', 'keypoints']:
|
| 163 |
+
raise ValueError(f"iou_type '{iou_type}' not supported")
|
| 164 |
+
|
| 165 |
+
current_max_dets = coco_eval_object.params.maxDets
|
| 166 |
+
|
| 167 |
+
index_to_title = {
|
| 168 |
+
"bbox": {
|
| 169 |
+
0: f"AP-IoU=0.50:0.95-area=all-maxDets={current_max_dets[2]}",
|
| 170 |
+
1: f"AP-IoU=0.50-area=all-maxDets={current_max_dets[2]}",
|
| 171 |
+
2: f"AP-IoU=0.75-area=all-maxDets={current_max_dets[2]}",
|
| 172 |
+
3: f"AP-IoU=0.50:0.95-area=small-maxDets={current_max_dets[2]}",
|
| 173 |
+
4: f"AP-IoU=0.50:0.95-area=medium-maxDets={current_max_dets[2]}",
|
| 174 |
+
5: f"AP-IoU=0.50:0.95-area=large-maxDets={current_max_dets[2]}",
|
| 175 |
+
6: f"AR-IoU=0.50:0.95-area=all-maxDets={current_max_dets[0]}",
|
| 176 |
+
7: f"AR-IoU=0.50:0.95-area=all-maxDets={current_max_dets[1]}",
|
| 177 |
+
8: f"AR-IoU=0.50:0.95-area=all-maxDets={current_max_dets[2]}",
|
| 178 |
+
9: f"AR-IoU=0.50:0.95-area=small-maxDets={current_max_dets[2]}",
|
| 179 |
+
10: f"AR-IoU=0.50:0.95-area=medium-maxDets={current_max_dets[2]}",
|
| 180 |
+
11: f"AR-IoU=0.50:0.95-area=large-maxDets={current_max_dets[2]}",
|
| 181 |
+
},
|
| 182 |
+
"keypoints":
|
| 183 |
+
{
|
| 184 |
+
0: "AP-IoU=0.50:0.95-area=all-maxDets=20",
|
| 185 |
+
1: "AP-IoU=0.50-area=all-maxDets=20",
|
| 186 |
+
2: "AP-IoU=0.75-area=all-maxDets=20",
|
| 187 |
+
3: "AP-IoU=0.50:0.95-area=medium-maxDets=20",
|
| 188 |
+
4: "AP-IoU=0.50:0.95-area=large-maxDets=20",
|
| 189 |
+
5: "AR-IoU=0.50:0.95-area=all-maxDets=20",
|
| 190 |
+
6: "AR-IoU=0.50-area=all-maxDets=20",
|
| 191 |
+
7: "AR-IoU=0.75-area=all-maxDets=20",
|
| 192 |
+
8: "AR-IoU=0.50:0.95-area=medium-maxDets=20",
|
| 193 |
+
9: "AR-IoU=0.50:0.95-area=large-maxDets=20",
|
| 194 |
+
},
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
+
output_dict = {}
|
| 198 |
+
for index, stat in enumerate(stats):
|
| 199 |
+
output_dict[index_to_title[iou_type][index]] = stat
|
| 200 |
+
|
| 201 |
+
return output_dict
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def get_results(self):
|
| 205 |
+
output_dict = {}
|
| 206 |
+
|
| 207 |
+
for iou_type, coco_eval in self.coco_eval.items():
|
| 208 |
+
if iou_type == 'segm':
|
| 209 |
+
iou_type = 'bbox'
|
| 210 |
+
output_dict[f"iou_{iou_type}"] = self._post_process_stats(coco_eval.stats, coco_eval, iou_type)
|
| 211 |
+
return output_dict
|
| 212 |
|
| 213 |
def prepare(self, predictions, iou_type):
|
| 214 |
if iou_type == "bbox":
|
|
|
|
| 227 |
continue
|
| 228 |
|
| 229 |
boxes = prediction["boxes"]
|
| 230 |
+
if not isinstance(boxes, torch.Tensor):
|
| 231 |
+
boxes = torch.as_tensor(boxes)
|
| 232 |
boxes = convert_to_xywh(boxes).tolist()
|
| 233 |
+
|
| 234 |
+
scores = prediction["scores"]
|
| 235 |
+
if not isinstance(scores, list):
|
| 236 |
+
scores = scores.tolist()
|
| 237 |
+
|
| 238 |
+
labels = prediction["labels"]
|
| 239 |
+
if not isinstance(labels, list):
|
| 240 |
+
labels = prediction["labels"].tolist()
|
| 241 |
|
| 242 |
coco_results.extend(
|
| 243 |
[
|