def coco_eval(result_files, result_types, coco, max_dets=(100, 300, 1000)): for res_type in result_types: assert res_type in [ 'proposal', 'proposal_fast', 'bbox', 'segm', 'keypoints' ] if mmcv.is_str(coco): coco = COCO(coco) assert isinstance(coco, COCO) if result_types == ['proposal_fast']: ar = fast_eval_recall(result_files, coco, np.array(max_dets)) for i, num in enumerate(max_dets): print('AR@{}\t= {:.4f}'.format(num, ar[i])) return for res_type in result_types: result_file = result_files[res_type] # assert result_file.endswith('.json') coco_dets = coco.loadRes(result_file) img_ids = coco.getImgIds() iou_type = 'bbox' if res_type == 'proposal' else res_type cocoEval = COCOeval(coco, coco_dets, iou_type) cocoEval.params.imgIds = img_ids if res_type == 'proposal': cocoEval.params.useCats = 0 cocoEval.params.maxDets = list(max_dets) cocoEval.evaluate() cocoEval.accumulate() cocoEval.summarize()
def __init__(self, root, ann_file, remove_images_without_annotations=True, filter_crowd_anno=True, is_training=True): self.coco = COCO(ann_file) self.root = root self.img_ids = list(sorted(self.coco.imgs.keys())) self.filter_crowd_anno = filter_crowd_anno self.is_training = is_training # filter images without any annotations if remove_images_without_annotations: img_ids = [] for img_id in self.img_ids: ann_ids = self.coco.getAnnIds(imgIds=img_id, iscrowd=None) anno = self.coco.loadAnns(ann_ids) if has_valid_annotation(anno): img_ids.append(img_id) self.img_ids = img_ids self.categories = {cat["id"]: cat["name"] for cat in self.coco.cats.values()} self.cat_ids_to_continuous_ids = { v: i for i, v in enumerate(self.coco.getCatIds()) } self.continuous_ids_cat_ids = { v: k for k, v in self.cat_ids_to_continuous_ids.items() }
def load_annotations(self, ann_file): self.coco = COCO(ann_file) self.cat_ids = self.coco.getCatIds() self.cat2label = { cat_id: i + 1 for i, cat_id in enumerate(self.cat_ids) } self.img_ids = self.coco.getImgIds() img_infos = [] for i in self.img_ids: info = self.coco.loadImgs([i])[0] info['filename'] = info['file_name'] img_infos.append(info) return img_infos
def convert_to_coco_api(ds): coco_ds = COCO() # annotation IDs need to start at 1, not 0, see torchvision issue #1530 ann_id = 1 dataset = {'images': [], 'categories': [], 'annotations': []} categories = set() for img_idx in range(len(ds)): # find better way to get target # targets = ds.get_annotations(img_idx) img, targets = ds[img_idx] image_id = targets["image_id"].item() img_dict = {} img_dict['id'] = image_id img_dict['height'] = img.shape[-2] img_dict['width'] = img.shape[-1] dataset['images'].append(img_dict) bboxes = targets["boxes"] bboxes[:, 2:] -= bboxes[:, :2] bboxes = bboxes.tolist() labels = targets['labels'].tolist() areas = targets['area'].tolist() iscrowd = targets['iscrowd'].tolist() if 'masks' in targets: masks = targets['masks'] # make masks Fortran contiguous for coco_mask masks = masks.permute(0, 2, 1).contiguous().permute(0, 2, 1) if 'keypoints' in targets: keypoints = targets['keypoints'] keypoints = keypoints.reshape(keypoints.shape[0], -1).tolist() num_objs = len(bboxes) for i in range(num_objs): ann = {} ann['image_id'] = image_id ann['bbox'] = bboxes[i] ann['category_id'] = labels[i] categories.add(labels[i]) ann['area'] = areas[i] ann['iscrowd'] = iscrowd[i] ann['id'] = ann_id if 'masks' in targets: ann["segmentation"] = coco_mask.encode(masks[i].numpy()) if 'keypoints' in targets: ann['keypoints'] = keypoints[i] ann['num_keypoints'] = sum(k != 0 for k in keypoints[i][2::3]) dataset['annotations'].append(ann) ann_id += 1 dataset['categories'] = [{'id': i} for i in sorted(categories)] coco_ds.dataset = dataset coco_ds.createIndex() return coco_ds
def __init__(self, data_folder, jason_file, image_folder, offset_folder, transform, train=True): self.image_folder = image_folder self.offset_folder = offset_folder self.is_train = train self.data_folder = data_folder self.disc_radius = 10 self.transform = transform self.max_len = 100 self.debug = False # self.debug = True self.gt_path = data_folder + '/' + jason_file self.fiber_coco = COCO(self.gt_path) self.ids = list(self.fiber_coco.anns.keys()) self.img_ids = list(self.fiber_coco.imgs.keys()) if train: self.anno = [] with open(self.gt_path) as anno_file: self.anno.extend(json.load(anno_file))
def update(self, predictions): img_ids = list(np.unique(list(predictions.keys()))) self.img_ids.extend(img_ids) for iou_type in self.iou_types: results = self.prepare(predictions, iou_type) coco_dt = loadRes(self.coco_gt, results) if results else COCO() coco_eval = self.coco_eval[iou_type] coco_eval.cocoDt = coco_dt coco_eval.params.imgIds = list(img_ids) img_ids, eval_imgs = evaluate(coco_eval) self.eval_imgs[iou_type].append(eval_imgs)
def coco_process(): """ Process COCO Images """ # Define COCO Data to Gather dataType = args.dataset + '2017' annFile = '{}/annotations/person_keypoints_{}.json'.format( args.dataDir, dataType) # Remove Current h5 File from Output if os.path.exists(args.output): os.remove(args.output) # initialize COCO api for person keypoints annotations coco = COCO(annFile) # get all images containing given categories, select one at random catIds = coco.getCatIds(catNms=['person']) imgIds = coco.getImgIds(catIds=catIds) # Joint names with open(annFile) as f: json_data = json.load(f) keypoint_names = json_data['categories'][0]['keypoints'] # Open h5 File h5f = h5py.File(args.output, 'a') start_time = time.time() # for all iterations for count in range(min(args.cnt, len(imgIds))): # load image + keypoints I, img = load(annFile, coco, imgIds, dataType, count) # load annotations annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None) anns = coco.loadAnns(annIds) # Create Dataframe df = keypoints_df(anns) # If Img has any Keypoints if df is not None: # Scale Img and Keypoints im_scaled, df_scaled = scale(I, df) # Write to h5 h5_write(img, h5f, df_scaled, im_scaled) # If testing, test all and exit if args.testing == True: h5f.close() coco_test_img(df, df_scaled, im_scaled, I, coco, anns) h5_attributes(keypoint_names) coco_test_h5() exit() else: # Disregard image with no keypoints pass if count % 100 == 0: print('{} Samples Done - {} Taken'.format( count, (str(datetime.timedelta(seconds=(time.time() - start_time)))))) return keypoint_names
class CocoDataset(CustomDataset): CLASSES = ('person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic_light', 'fire_hydrant', 'stop_sign', 'parking_meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports_ball', 'kite', 'baseball_bat', 'baseball_glove', 'skateboard', 'surfboard', 'tennis_racket', 'bottle', 'wine_glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot_dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted_plant', 'bed', 'dining_table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell_phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy_bear', 'hair_drier', 'toothbrush') def load_annotations(self, ann_file): self.coco = COCO(ann_file) self.cat_ids = self.coco.getCatIds() self.cat2label = { cat_id: i + 1 for i, cat_id in enumerate(self.cat_ids) } self.img_ids = self.coco.getImgIds() img_infos = [] for i in self.img_ids: info = self.coco.loadImgs([i])[0] info['filename'] = info['file_name'] img_infos.append(info) return img_infos def get_ann_info(self, idx): img_id = self.img_infos[idx]['id'] ann_ids = self.coco.getAnnIds(imgIds=[img_id]) ann_info = self.coco.loadAnns(ann_ids) return self._parse_ann_info(ann_info, self.with_mask) def _filter_imgs(self, min_size=32): """Filter images too small or without ground truths.""" valid_inds = [] ids_with_ann = set(_['image_id'] for _ in self.coco.anns.values()) for i, img_info in enumerate(self.img_infos): if self.img_ids[i] not in ids_with_ann: continue if min(img_info['width'], img_info['height']) >= min_size: valid_inds.append(i) return valid_inds def _parse_ann_info(self, ann_info, with_mask=True): """Parse bbox and mask annotation. Args: ann_info (list[dict]): Annotation info of an image. with_mask (bool): Whether to parse mask annotations. Returns: dict: A dict containing the following keys: bboxes, bboxes_ignore, labels, masks, mask_polys, poly_lens. """ gt_bboxes = [] gt_labels = [] gt_bboxes_ignore = [] # Two formats are provided. # 1. mask: a binary map of the same size of the image. # 2. polys: each mask consists of one or several polys, each poly is a # list of float. if with_mask: gt_masks = [] gt_mask_polys = [] gt_poly_lens = [] for i, ann in enumerate(ann_info): if ann.get('ignore', False): continue x1, y1, w, h = ann['bbox'] if ann['area'] <= 0 or w < 1 or h < 1: continue bbox = [x1, y1, x1 + w - 1, y1 + h - 1] if ann['iscrowd']: gt_bboxes_ignore.append(bbox) else: gt_bboxes.append(bbox) gt_labels.append(self.cat2label[ann['category_id']]) if with_mask: gt_masks.append(self.coco.annToMask(ann)) mask_polys = [ p for p in ann['segmentation'] if len(p) >= 6 ] # valid polygons have >= 3 points (6 coordinates) poly_lens = [len(p) for p in mask_polys] gt_mask_polys.append(mask_polys) gt_poly_lens.extend(poly_lens) if gt_bboxes: gt_bboxes = np.array(gt_bboxes, dtype=np.float32) gt_labels = np.array(gt_labels, dtype=np.int64) else: gt_bboxes = np.zeros((0, 4), dtype=np.float32) gt_labels = np.array([], dtype=np.int64) if gt_bboxes_ignore: gt_bboxes_ignore = np.array(gt_bboxes_ignore, dtype=np.float32) else: gt_bboxes_ignore = np.zeros((0, 4), dtype=np.float32) ann = dict( bboxes=gt_bboxes, labels=gt_labels, bboxes_ignore=gt_bboxes_ignore) if with_mask: ann['masks'] = gt_masks # poly format is not used in the current implementation ann['mask_polys'] = gt_mask_polys ann['poly_lens'] = gt_poly_lens return ann
# -*- coding: utf-8 -*- """ Created on Sat Feb 22 16:58:58 2020 @author: zhangyiqian """ from cocoapi.PythonAPI.pycocotools.coco import COCO import numpy as np from skimage.io import imread, imshow, imsave import matplotlib.pyplot as plt from skimage import draw import random import cv2 as cv import time annFile = '/home/yiqian/Documents/dataset/COCO/annotations_valstuff/stuff_val2017.json' coco = COCO(annFile) imgIds = coco.getImgIds() img = coco.loadImgs(imgIds[0])[0] I = imread(img['coco_url']) plt.figure() imshow(I) # catIds = coco.getCatIds(catNms=['person']); annIds = coco.getAnnIds(imgIds=img['id'], catIds=coco.getCatIds(), iscrowd=False) # annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=False) anns = coco.loadAnns(annIds) coco.showAnns(anns) # sample scribble areas based on uniform distribution
def read_data(data_dir, image_size, pixels_per_grid=32, no_label=False): """ Load the data and preprocessing for YOLO detector :param data_dir: str, path to the directory to read. It should include class_map, anchors, annotations :image_size: tuple, image size for resizing images :pixels_per_gird: int, the actual size of a grid :no_label: bool, whetehr to load labels :return: X_set: np.ndarray, shape: (N, H, W, C). y_set: np.ndarray, shape: (N, g_H, g_W, anchors, 5 + num_classes). """ im_dir = os.path.join(data_dir, 'val2017') class_map_path = '/home/intern/data/coco/classes_coco.json' # class_map_path = os.path.join(data_dir, 'classes.json') #anchors_path = os.path.join(data_dir, 'anchors.json') class_map = load_json(class_map_path) anchors = load_json('/home/intern/data/face/train/anchors.json') #anchors = load_json(anchors_path) num_classes = len(class_map) grid_h, grid_w = [image_size[i] // pixels_per_grid for i in range(2)] im_paths = [] for ext in IM_EXTENSIONS: im_paths.extend(glob.glob(os.path.join(im_dir, '*.{}'.format(ext)))) #anno_dir = os.path.join(data_dir, 'annotations') images = [] labels = [] ########################################################################### dataDir='/home/intern/data/coco' dataType='val2017' annFile='{}/annotations/instances_{}.json'.format(dataDir,dataType) coco=COCO(annFile) all_file_name = coco.getImgIds()#int type ########################################################################### for im_path in im_paths: # load image and resize image im = imread(im_path) #print(im_path) im = np.array(im, dtype=np.float32) im_origina_sizes = im.shape[:2] im = resize(im, (image_size[1], image_size[0])) if len(im.shape) == 2: im = np.expand_dims(im, 2) im = np.concatenate([im, im, im], -1) images.append(im) if no_label: labels.append(0) continue # load bboxes and reshape for yolo model name = os.path.splitext(os.path.basename(im_path))[0] ########################################################################### name_nonzero_int = int(name.lstrip('0'))#str type -> int(remove '0') real_anno = {} annos_one_img = coco.getAnnIds(imgIds=[name_nonzero_int])#annotations one img for i in range(0,len(annos_one_img)): anno_contents = coco.loadAnns(ids=[annos_one_img[i]])#info of one anno->type: [{}] anno_contents[0]['category_id'] category_name = coco.loadCats(ids=[anno_contents[0]['category_id']])[0]['name']#name bbbox = anno_contents[0]['bbox']#bbox [x,y,w,h] trans_bbbox = [bbbox[0], bbbox[1], bbbox[0]+bbbox[2], bbbox[1]+bbbox[3]]#[x_min,y_min,x_max,y_max] if category_name in real_anno: real_anno[category_name].append(trans_bbbox) else: real_anno[category_name] = [trans_bbbox] ########################################################################### #anno_path = os.path.join(anno_dir, '{}.anno'.format(name)) #anno = load_json(anno_path) anno = real_anno #print(anno) label = np.zeros((grid_h, grid_w, len(anchors), 5 + num_classes)) for c_idx, c_name in class_map.items(): if c_name not in anno: continue #print(anno[c_name]) for x_min, y_min, x_max, y_max in anno[c_name]: oh, ow = im_origina_sizes # normalize object coordinates and clip the values x_min, y_min, x_max, y_max = x_min / ow, y_min / oh, x_max / ow, y_max / oh x_min, y_min, x_max, y_max = np.clip([x_min, y_min, x_max, y_max], 0, 1) # assign the values to the best anchor anchor_boxes = np.array(anchors) / np.array([ow, oh]) best_anchor = get_best_anchor( anchor_boxes, [x_max - x_min, y_max - y_min]) cx = int(np.floor(0.5 * (x_min + x_max) * grid_w)) cy = int(np.floor(0.5 * (y_min + y_max) * grid_h)) label[cy, cx, best_anchor, 0:4] = [x_min, y_min, x_max, y_max] label[cy, cx, best_anchor, 4] = 1.0 label[cy, cx, best_anchor, 5 + int(c_idx)] = 1.0 labels.append(label) X_set = np.array(images, dtype=np.float32) y_set = np.array(labels, dtype=np.float32) return X_set, y_set
class COCOYoloDataset: """YOLOV4 Dataset for COCO.""" def __init__(self, root, ann_file, remove_images_without_annotations=True, filter_crowd_anno=True, is_training=True): self.coco = COCO(ann_file) self.root = root self.img_ids = list(sorted(self.coco.imgs.keys())) self.filter_crowd_anno = filter_crowd_anno self.is_training = is_training # filter images without any annotations if remove_images_without_annotations: img_ids = [] for img_id in self.img_ids: ann_ids = self.coco.getAnnIds(imgIds=img_id, iscrowd=None) anno = self.coco.loadAnns(ann_ids) if has_valid_annotation(anno): img_ids.append(img_id) self.img_ids = img_ids self.categories = {cat["id"]: cat["name"] for cat in self.coco.cats.values()} self.cat_ids_to_continuous_ids = { v: i for i, v in enumerate(self.coco.getCatIds()) } self.continuous_ids_cat_ids = { v: k for k, v in self.cat_ids_to_continuous_ids.items() } def __getitem__(self, index): """ Args: index (int): Index Returns: (img, target) (tuple): target is a dictionary contains "bbox", "segmentation" or "keypoints", generated by the image's annotation. img is a PIL image. """ coco = self.coco img_id = self.img_ids[index] img_path = coco.loadImgs(img_id)[0]["file_name"] img = Image.open(os.path.join(self.root, img_path)).convert("RGB") if not self.is_training: return img, img_id ann_ids = coco.getAnnIds(imgIds=img_id) target = coco.loadAnns(ann_ids) # filter crowd annotations if self.filter_crowd_anno: annos = [anno for anno in target if anno["iscrowd"] == 0] else: annos = [anno for anno in target] target = {} boxes = [anno["bbox"] for anno in annos] target["bboxes"] = boxes classes = [anno["category_id"] for anno in annos] classes = [self.cat_ids_to_continuous_ids[cl] for cl in classes] target["labels"] = classes bboxes = target['bboxes'] labels = target['labels'] out_target = [] for bbox, label in zip(bboxes, labels): tmp = [] # convert to [x_min y_min x_max y_max] bbox = self._conve_top_down(bbox) tmp.extend(bbox) tmp.append(int(label)) # tmp [x_min y_min x_max y_max, label] out_target.append(tmp) return img, out_target, [], [], [], [], [], [] def __len__(self): return len(self.img_ids) def _conve_top_down(self, bbox): x_min = bbox[0] y_min = bbox[1] w = bbox[2] h = bbox[3] return [x_min, y_min, x_min+w, y_min+h]
def loadRes(self, resFile): """ Load result file and return a result api object. Args: self (obj): coco object with ground truth annotations resFile (str): file name of result file Returns: res (obj): result api object """ res = COCO() res.dataset['images'] = [img for img in self.dataset['images']] # print('Loading and preparing results...') # tic = time.time() if isinstance(resFile, torch._six.string_classes): anns = json.load(open(resFile)) elif type(resFile) == np.ndarray: anns = self.loadNumpyAnnotations(resFile) else: anns = resFile assert type(anns) == list, 'results in not an array of objects' annsImgIds = [ann['image_id'] for ann in anns] assert set(annsImgIds) == (set(annsImgIds) & set(self.getImgIds())), \ 'Results do not correspond to current coco set' if 'caption' in anns[0]: imgIds = set([img['id'] for img in res.dataset['images']]) & set( [ann['image_id'] for ann in anns]) res.dataset['images'] = [ img for img in res.dataset['images'] if img['id'] in imgIds ] for id, ann in enumerate(anns): ann['id'] = id + 1 elif 'bbox' in anns[0] and not anns[0]['bbox'] == []: res.dataset['categories'] = copy.deepcopy(self.dataset['categories']) for id, ann in enumerate(anns): bb = ann['bbox'] x1, x2, y1, y2 = [bb[0], bb[0] + bb[2], bb[1], bb[1] + bb[3]] if 'segmentation' not in ann: ann['segmentation'] = [[x1, y1, x1, y2, x2, y2, x2, y1]] ann['area'] = bb[2] * bb[3] ann['id'] = id + 1 ann['iscrowd'] = 0 elif 'segmentation' in anns[0]: res.dataset['categories'] = copy.deepcopy(self.dataset['categories']) for id, ann in enumerate(anns): # now only support compressed RLE format as segmentation results ann['area'] = maskUtils.area(ann['segmentation']) if 'bbox' not in ann: ann['bbox'] = maskUtils.toBbox(ann['segmentation']) ann['id'] = id + 1 ann['iscrowd'] = 0 elif 'keypoints' in anns[0]: res.dataset['categories'] = copy.deepcopy(self.dataset['categories']) for id, ann in enumerate(anns): s = ann['keypoints'] x = s[0::3] y = s[1::3] x1, x2, y1, y2 = np.min(x), np.max(x), np.min(y), np.max(y) ann['area'] = (x2 - x1) * (y2 - y1) ann['id'] = id + 1 ann['bbox'] = [x1, y1, x2 - x1, y2 - y1] # print('DONE (t={:0.2f}s)'.format(time.time()- tic)) res.dataset['annotations'] = anns createIndex(res) return res
#SET THESE PATHS gt_path = './GT/AVD_split1_test.json' results_path = './my_results.json' #gather the object instance ids all_catIds = [ 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 ] known_catIds = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] novel_catIds = [18, 19, 20, 21, 22, 23, 24, 25, 26, 27] iouThrs = .5 maxDets = [1, 10, 100] #initialize COCO ground truth api cocoGt = COCO(gt_path) #initialize COCO detections api cocoDt = cocoGt.loadRes(my_results.json) annType = 'bbox' cocoEval = COCOeval(cocoGt, cocoDt, annType) cocoEval.params.iouThrs = np.array([iouThrs]) cocoEval.params.maxDets = maxDets #cocoEval.params.areaRng = [[0, 10000000000.0], [416, 10000000000.0 ], [0, 416], [416, 1250], [1250, 3750], [3750, 7500], [7500,10000000000.0]] #cocoEval.params.areaRngLbl = ['all', 'valid', 'l0', 'l1', 'l2', 'l3', 'l4'] cocoEval.params.areaRng = [[0, 10000000000.0]] cocoEval.params.areaRngLbl = ['all'] cocoEval.params.useSegs = [0] catIds_types = [all_catIds, known_catIds, novel_catIds] results = []