def finalize(self, result_dict, ds=None, output_dir=None): result_dict["good"] += self.good result_dict["total"] += self.total detections = np.array(self.results) image_ids = [] for idx in range(0, detections.shape[0]): # this is the index into the image list image_id = int(detections[idx][0]) image_ids.append(image_id) # map it to the coco image it detections[idx][0] = ds.image_ids[image_id] height, width = ds.image_sizes[image_id] # box comes from model as: ymin, xmin, ymax, xmax ymin = detections[idx][1] * height xmin = detections[idx][2] * width ymax = detections[idx][3] * height xmax = detections[idx][4] * width # from pycoco wants {imageID,x1,y1,w,h,score,class} detections[idx][1] = xmin detections[idx][2] = ymin detections[idx][3] = xmax - xmin detections[idx][4] = ymax - ymin # for debugging if True: pp = [] for idx in range(0, detections.shape[0]): pp.append({ "image_id": int(detections[idx][0]), "image_loc": ds.get_item_loc(image_ids[idx]), "category_id": int(detections[idx][6]), "bbox": [ float(detections[idx][1]), float(detections[idx][2]), float(detections[idx][3]), float(detections[idx][4]) ], "score": float(detections[idx][5]) }) if not output_dir: output_dir = "/tmp" fname = "{}/{}.json".format(output_dir, result_dict["scenario"]) with open(fname, "w") as fp: json.dump(pp, fp, sort_keys=True, indent=4) image_ids = list(set([i[0] for i in detections])) self.results = [] cocoGt = pycoco.COCO(ds.annotation_file) cocoDt = cocoGt.loadRes(detections) cocoEval = COCOeval(cocoGt, cocoDt, iouType='bbox') cocoEval.params.imgIds = image_ids cocoEval.evaluate() cocoEval.accumulate() cocoEval.summarize() result_dict["mAP"] = cocoEval.stats[0]
def finalize(self, result_dict, ds=None, output_dir=None): result_dict["good"] += self.good result_dict["total"] += self.total if self.use_inv_map: # for pytorch label_map = {} with open(ds.annotation_file) as fin: annotations = json.load(fin) for cnt, cat in enumerate(annotations["categories"]): label_map[cat["id"]] = cnt + 1 inv_map = {v: k for k, v in label_map.items()} detections = [] image_indices = [] for batch in range(0, len(self.results)): image_indices.append(self.content_ids[batch]) for idx in range(0, len(self.results[batch])): detection = self.results[batch][idx] # this is the index of the coco image image_idx = int(detection[0]) if image_idx != self.content_ids[batch]: # working with the coco index/id is error prone - extra check to make sure it is consistent log.error("image_idx missmatch, lg={} / result={}".format( image_idx, self.content_ids[batch])) # map the index to the coco image id detection[0] = ds.image_ids[image_idx] height, width = ds.image_sizes[image_idx] # box comes from model as: ymin, xmin, ymax, xmax ymin = detection[1] * height xmin = detection[2] * width ymax = detection[3] * height xmax = detection[4] * width # pycoco wants {imageID,x1,y1,w,h,score,class} detection[1] = xmin detection[2] = ymin detection[3] = xmax - xmin detection[4] = ymax - ymin if self.use_inv_map: cat_id = inv_map.get(int(detection[6]), -1) if cat_id == -1: # FIXME: log.info("finalize can't map category {}".format( int(detection[6]))) detection[6] = cat_id detections.append(np.array(detection)) # map indices to coco image id's image_ids = [ds.image_ids[i] for i in image_indices] self.results = [] cocoGt = pycoco.COCO(ds.annotation_file) cocoDt = cocoGt.loadRes(np.array(detections)) cocoEval = COCOeval(cocoGt, cocoDt, iouType='bbox') cocoEval.params.imgIds = image_ids cocoEval.evaluate() cocoEval.accumulate() cocoEval.summarize() result_dict["mAP"] = cocoEval.stats[0]
def finalize(self, result_dict, ds=None): result_dict["good"] += self.good result_dict["total"] += self.total detections = np.array(self.results) for idx in range(0, detections.shape[0]): image_idx = int(detections[idx][0]) image_id = ds.image_ids[image_idx] detections[idx][0] = image_id height, width = ds.image_sizes[image_idx] # box comes from model as: ymin, xmin, ymax, xmax ymin = detections[idx][1] * height xmin = detections[idx][2] * width ymax = detections[idx][3] * height xmax = detections[idx][4] * width # from pycoco wants {imageID,x1,y1,w,h,score,class} detections[idx][1] = xmin detections[idx][2] = ymin detections[idx][3] = xmax - xmin detections[idx][4] = ymax - ymin if False: # TODO: remove after debug pp = [] for idx in range(0, detections.shape[0]): pp.append({ "image_id": int(detections[idx][0]), "category_id": int(detections[idx][6]), "bbox": [ float(detections[idx][1]), float(detections[idx][2]), float(detections[idx][3]), float(detections[idx][4]) ], "score": float(detections[idx][5]) }) import json with open("/tmp/t.json", "w") as fp: json.dump(pp, fp, sort_keys=True, indent=4) image_ids = list(set([i[0] for i in detections])) self.results = [] cocoGt = pycoco.COCO(ds.annotation_file) cocoDt = cocoGt.loadRes(detections) cocoEval = COCOeval(cocoGt, cocoDt, iouType='bbox') cocoEval.params.imgIds = image_ids cocoEval.evaluate() cocoEval.accumulate() cocoEval.summarize() result_dict["mAP"] = cocoEval.stats[0]
def finalize(self, result_dict, ds=None, output_dir=None): result_dict["good"] += self.good result_dict["total"] += self.total if self.use_inv_map: # for pytorch label_map = {} with open(ds.annotation_file) as fin: annotations = json.load(fin) for cnt, cat in enumerate(annotations["categories"]): label_map[cat["id"]] = cnt + 1 inv_map = {v:k for k,v in label_map.items()} detections = [] image_indices = [] for batch in range(0, len(self.results)): for idx in range(0, len(self.results[batch])): detection = self.results[batch][idx] # this is the index of the coco image image_idx = int(detection[0]) # because we need to have the image_indices.append(image_idx) # map the index to the coco image id detection[0] = ds.image_ids[image_idx] height, width = ds.image_sizes[image_idx] # box comes from model as: ymin, xmin, ymax, xmax ymin = detection[1] * height xmin = detection[2] * width ymax = detection[3] * height xmax = detection[4] * width # pycoco wants {imageID,x1,y1,w,h,score,class} detection[1] = xmin detection[2] = ymin detection[3] = xmax - xmin detection[4] = ymax - ymin if self.use_inv_map: cat_id = inv_map.get(int(detection[6]), -1) if cat_id == -1: # FIXME: log.info("finalize can't map category {}".format(int(detection[6]))) detection[6] = cat_id detections.append(np.array(detection)) if output_dir: # for debugging pp = [] for image_idx, detection in zip(image_indices, detections): pp.append({"image_id": int(detection[0]), "image_loc": ds.get_item_loc(image_idx), "category_id": int(detection[6]), "bbox": [float(detection[1]), float(detection[2]), float(detection[3]), float(detection[4])], "score": float(detection[5])}) fname = "{}.json".format(result_dict["scenario"]) with open(fname, "w") as fp: json.dump(pp, fp, sort_keys=True, indent=4) image_ids = list(set([i[0] for i in detections])) self.results = [] cocoGt = pycoco.COCO(ds.annotation_file) cocoDt = cocoGt.loadRes(np.array(detections)) cocoEval = COCOeval(cocoGt, cocoDt, iouType='bbox') cocoEval.params.imgIds = image_ids cocoEval.evaluate() cocoEval.accumulate() cocoEval.summarize() result_dict["mAP"] = cocoEval.stats[0]