Esempio n. 1
0
    def __init__(self,
                 root_dir,
                 coco_dir,
                 set_dir,
                 preproc=None,
                 target_transform=None,
                 dataset_name='COCO'):
        self.root = root_dir
        self.root_dir = root_dir
        self.coco_dir = coco_dir
        self.set_dir = set_dir
        self.coco_name = root_dir.replace("/", "") + "_" + coco_dir.replace(
            "/", "") + "_" + set_dir.replace("/", "")
        self.data_path = os.getcwd()
        self.cache_path = os.path.join(self.data_path, 'coco_cache')
        if (not os.path.isdir(self.cache_path)):
            os.mkdir(self.cache_path)

        self.image_set = set_dir
        self.preproc = preproc

        self.target_transform = target_transform
        self.name = dataset_name
        self.ids = list()
        self.annotations = list()

        annofile = self._get_ann_file(set_dir)
        _COCO = COCO(annofile)
        self._COCO = _COCO

        self._classes = ['__background__']
        self._classes_file = os.path.join(self.root_dir, self.coco_dir,
                                          'annotations', 'classes.txt')
        f = open(self._classes_file)
        lines = f.readlines()
        f.close()
        for i in range(len(lines)):
            self._classes.append(lines[i][:len(lines[i]) - 1])
        self._classes = tuple(self._classes)

        self.num_classes = len(self._classes)
        cats = _COCO.loadCats(_COCO.getCatIds())
        self._class_to_ind = dict(zip(self._classes, range(self.num_classes)))
        self._class_to_coco_cat_id = dict(
            zip([c['name'] for c in cats], _COCO.getCatIds()))

        indexes = _COCO.getImgIds()
        self.image_indexes = indexes
        self.ids.extend(
            [self.image_path_from_index(index) for index in indexes])
        self.annotations.extend(
            self._load_coco_annotations(self.coco_name, indexes, _COCO))
Esempio n. 2
0
    def __init__(self,
                 root,
                 image_sets,
                 preproc=None,
                 target_transform=None,
                 dataset_name='COCO'):
        self.root = root
        # self.data_path = os.path.join(os.path.expanduser("~"),'datasets')
        self.data_path = os.path.join(self.root, "data")
        self.cache_path = os.path.join(self.data_path, 'coco_cache')
        self.image_set = image_sets
        self.preproc = preproc
        self.target_transform = target_transform
        self.name = dataset_name
        self.img_paths = list()
        self.annotations = list()
        self._view_map = {
            'minival2014': 'val2014',  # 5k val2014 subset
            'valminusminival2014': 'val2014',  # val2014 \setminus minival2014
            'test-dev2015': 'test2015',
        }

        for (year, image_set) in image_sets:
            coco_name = image_set + year
            data_name = (self._view_map[coco_name]
                         if coco_name in self._view_map else coco_name)
            annofile = self._get_ann_file(coco_name)
            _COCO = COCO(annofile)
            self._COCO = _COCO
            self.coco_name = coco_name
            cats = _COCO.loadCats(_COCO.getCatIds())
            self._classes = tuple(['__background__'] +
                                  [c['name'] for c in cats])
            self.num_classes = len(self._classes)
            self._class_to_ind = dict(
                zip(self._classes, range(self.num_classes)))
            self._class_to_coco_cat_id = dict(
                zip([c['name'] for c in cats], _COCO.getCatIds()))
            indexes = _COCO.getImgIds()
            self.image_indexes = indexes
            self.img_paths.extend([
                self.image_path_from_index(data_name, index)
                for index in indexes
            ])
            if image_set.find('test') != -1:
                print('test set will not load annotations!')
            else:
                self.annotations.extend(
                    self._load_coco_annotations(coco_name, indexes, _COCO))
Esempio n. 3
0
def coco_to_roidb(annotation_path,
                  save_roidb_path,
                  task='det',
                  data_dir='',
                  need_mask=True):
    assert task in ['det', 'kps']
    coco = COCO(annotation_path)
    image_ids = coco.getImgIds()

    cats = [cat['name'] for cat in coco.loadCats(coco.getCatIds())]
    classes = ['__background__'] + cats
    num_classes = len(classes)
    class_to_ind = dict(zip(classes, xrange(num_classes)))
    class_to_coco_ind = dict(zip(cats, coco.getCatIds()))
    coco_ind_to_class_ind = dict([(class_to_coco_ind[cls], class_to_ind[cls])
                                  for cls in classes[1:]])

    roidb = []
    for i, image_id in enumerate(image_ids):
        if i % 1000 == 0:
            print('{}/{}'.format(i, len(image_ids)))
        im_ann = coco.loadImgs(image_id)[0]
        width = im_ann['width']
        height = im_ann['height']

        annIds = coco.getAnnIds(imgIds=image_id, iscrowd=None)
        objs = coco.loadAnns(annIds)

        # sanitize bboxes
        valid_objs = []
        areas_ = []
        for obj in objs:
            if task == 'kps':
                assert obj['category_id'] == 1
            assert obj['area'] > 0

            x, y, w, h = obj['bbox']
            x1 = np.max((0, x))
            y1 = np.max((0, y))
            x2 = np.min((width - 1, x1 + np.max((0, w - 1))))
            y2 = np.min((height - 1, y1 + np.max((0, h - 1))))
            if obj['area'] > 0 and x2 >= x1 and y2 >= y1:
                obj['clean_bbox'] = [x1, y1, x2, y2]
                valid_objs.append(obj)
                areas_.append(obj['area'])

            # x, y, w, h = obj['bbox']
            # x1 = x
            # y1 = y
            # x2 = x1 + w - 1
            # y2 = y1 + h - 1
            # assert 0 <= x1 < width
            # assert 0 <= y1 < height
            # assert 0 <= x2 < width
            # assert 0 <= y2 < height
            # assert x2 >= x1 and y2 >= y1
            # obj['clean_bbox'] = [x1, y1, x2, y2]
            # valid_objs.append(obj)
            # areas_.append(obj['area'])

        objs = valid_objs
        num_objs = len(objs)

        boxes = np.zeros((num_objs, 4), dtype=np.float32)
        gt_classes = np.zeros((num_objs, ), dtype=np.int32)
        keypoints = np.zeros((num_objs, 51), dtype=np.float32)

        areas = np.array(areas_, dtype=np.float32)

        iscrowd = []
        for ix, obj in enumerate(objs):
            cls = -coco_ind_to_class_ind[obj['category_id']]\
                 if obj['iscrowd'] else coco_ind_to_class_ind[obj['category_id']]
            iscrowd.append(obj['iscrowd'])
            boxes[ix, :] = obj['clean_bbox']
            gt_classes[ix] = cls
            if task == 'kps':
                keypoints[ix, :] = obj['keypoints']

        roi_rec = {
            'image': data_dir + im_ann['file_name'],
            'id': im_ann['id'],
            'height': height,
            'width': width,
            'boxes': boxes,
            'area': areas,
            'iscrowd': np.array(iscrowd, dtype=np.float32),
            'gt_classes': gt_classes
        }
        if task == 'kps':
            roi_rec['keypoints'] = keypoints
        if need_mask:
            roi_rec['gt_masks'] = [x['segmentation'] for x in objs]
        roidb.append(roi_rec)

    with open(save_roidb_path, 'wb') as fid:
        cPickle.dump(roidb, fid, cPickle.HIGHEST_PROTOCOL)
Esempio n. 4
0
    COCODetection, VOCDetection, detection_collate, BaseTransform, preproc

train_sets = [('2014', 'valminusminival')]


# dataset = COCODetection(COCOroot, train_sets, preproc( 300, (104, 117, 123), 0.6))
# epoch_size = len(dataset)
# iiter = data.DataLoader(dataset, 32,shuffle=True, num_workers=2, collate_fn=detection_collate)

dirname = "/media/trans/mnt/data/coco/"
annodir = "annotations"
annofile = "instances_valminusminival2014.json"
_COCO = COCO(os.path.join(dirname, annodir, annofile))

catids = _COCO.getCatIds()
imgids = _COCO.getImgIds()
annids = _COCO.getAnnIds(imgids[0])

cats = _COCO.loadCats(_COCO.getCatIds())
print("# ------------------------------------- # ")
# print(cats)
print(len(cats))
print("# ------------------------------------- # ")
print("catids:", catids)
print("# ------------------------------------- # ")
print("annids: ", annids)
print("# ------------------------------------- # ")
print("imgids: ", imgids[:30])

# ids2 = list()
# ids = list(range(100))
Esempio n. 5
0
class COCODataSet(Dataset):
    '''
    COCO Dataset (only return segmentation and bbox)
    '''
    def __init__(self, min_ratio=0.0, max_ratio=1.0, random_mask=False):
        self.coco = COCO(ann_file)
        self.imgIds = self.coco.getImgIds()
        self.min_ratio = min_ratio
        self.max_ratio = max_ratio
        self.random_mask = random_mask

    def __len__(self):
        return len(self.imgIds)

    def __getitem__(self, idx):
        coco = self.coco
        sample = None
        while True:
            imgId = self.imgIds[idx]
            imgData = coco.loadImgs([imgId])[0]
            img = cv.load_img(data_folder + imgData['file_name'])
            if self.random_mask:
                img = dl.process_img(img,
                                     crop_size=img_size,
                                     resize=False,
                                     sample_num=1,
                                     alpha=False,
                                     normalize=True,
                                     pytorch=True,
                                     random_mask=False,
                                     ones_boundary=False)
                img = img[0]
                img = torch.FloatTensor(img)
                pts = cv.generate_random_vertices(img_size,
                                                  img_size,
                                                  dis_max_ratio=self.max_ratio)
                mask = cv.generate_polygon_mask(img_size, img_size, pts)
                mask = mask.transpose(2, 0, 1)
                mask = torch.FloatTensor(mask)
                img_mask = torch.cat([img, mask], dim=0)  # 4 x ih x iw
                return img_mask
            height = img.shape[0]
            width = img.shape[1]
            img = img.astype(np.float32)
            img = img / 128. - 1
            if len(img.shape) == 2:
                img = np.dstack((img, img, img))
            img = img.transpose(2, 0, 1)
            annIds = coco.getAnnIds(imgIds=[imgId])
            anns = coco.loadAnns(annIds)
            bboxs = list()
            masks = list()
            labels = list()
            for ann in anns:
                if ann['iscrowd'] == 1:
                    continue
                bbox = np.asarray(ann['bbox'], dtype=np.float32)
                # throw away bbox that are too small
                area = bbox[2] * bbox[3]
                if area / (height * width) < self.min_ratio or area / (
                        height * width) > self.max_ratio:
                    continue
                segs = ann['segmentation']
                mask = np.zeros((height, width, 1))
                for seg in segs:
                    pts = seg_to_pts(seg)
                    mask += cv.generate_polygon_mask(height, width, pts)
                mask = mask.clip(0, 1)
                masks.append(mask)
                bboxs.append(bbox)
                labels.append(ann['category_id'])
            if len(bboxs) == 0:
                idx = idx + 1
                continue
            num_ann = len(bboxs)
            bboxs = np.stack(bboxs)
            masks = np.stack(masks)
            labels = np.stack(labels)
            masks = masks.transpose(0, 3, 1, 2)
            img = torch.FloatTensor(img)  #c*h*w float32
            bboxs = torch.FloatTensor(bboxs)  #N*4 float32
            masks = torch.FloatTensor(masks)  #N*1*h*w float32
            labels = torch.LongTensor(labels)  #N
            labels -= 1  # get the labels to be 0-indexed
            #trim or pad gt to make them fixed size
            cur_len = bboxs.shape[0]
            if cur_len > MAX_ANN_PER_IMG:
                bboxs = bboxs[:MAX_ANN_PER_IMG]
                masks = masks[:MAX_ANN_PER_IMG]
                labels = labels[:MAX_ANN_PER_IMG]
            elif cur_len < MAX_ANN_PER_IMG:
                bboxs_pad = torch.zeros(MAX_ANN_PER_IMG, 4)
                bboxs_pad[:cur_len] = bboxs
                bboxs = bboxs_pad
                masks_pad = torch.zeros(MAX_ANN_PER_IMG, 1, height, width)
                masks_pad[:cur_len] = masks
                masks = masks_pad
                labels_pad = torch.zeros(MAX_ANN_PER_IMG)
                labels_pad[:cur_len] = labels
                labels = labels_pad.long()
            #scale img and gt to square
            x_scale = img_size / float(width)
            y_scale = img_size / float(height)
            img = F.interpolate(img.view(1, -1, height, width),
                                size=(img_size, img_size))
            img = img.view(-1, img_size, img_size)
            masks = F.interpolate(masks, size=(img_size, img_size))
            x1 = bboxs[:, 0]
            x1 = x1 * x_scale
            y1 = bboxs[:, 1]
            y1 = y1 * y_scale
            w = bboxs[:, 2]
            w = w * x_scale
            h = bboxs[:, 3]
            h = h * y_scale
            bboxs[:, 0] = y1
            bboxs[:, 1] = x1
            bboxs[:, 2] = y1 + h
            bboxs[:, 3] = x1 + w
            sample = {
                'img': img,
                'num_ann': min(num_ann, MAX_ANN_PER_IMG),
                'bboxs': bboxs,
                'masks': masks,
                'labels': labels
            }
            break
        return sample
Esempio n. 6
0
def evaluate_detections(FPS=None):

    print('Running demo for *%s* results.' % (annType))
    # initialize COCO ground truth api
    annFile = '%s/annotations/%s_%s.json' % (dataDir, prefix, dataType)
    print(annFile)
    cocoGt = COCO(annFile)
    # initialize COCO detections api
    cocoDt = cocoGt.loadRes(resFile)
    imgIds = cocoGt.getImgIds()
    # imgIds = imgIds[0:100]
    # imgId = imgIds[np.random.randint(100)]

    # running evaluation
    cocoEval = COCOeval(cocoGt, cocoDt, annType)
    cocoEval.params.imgIds = imgIds
    cocoEval.evaluate()
    cocoEval.accumulate()
    means = cocoEval.summarize()
    with open(os.path.join(output_dir,
                           str(int(means[0] * 10000)) + '.txt'),
              'w') as res_file:
        res_file.write('CUDA: ' + str(args.cuda) + '\n')
        res_file.write('model_dir: ' + args.model_dir + '\n')
        res_file.write('iteration: ' + args.iteration + '\n')
        res_file.write('model_name: ' + args.model_name + '\n')
        res_file.write('backbone : ' + args.backbone + '\n')
        if args.backbone in ['RefineDet_VGG']:
            res_file.write('refine : ' + str(args.refine) + '\n')
            res_file.write('deform : ' + str(args.deform) + '\n')
            res_file.write('multi-head : ' + str(args.multihead) + '\n')
        res_file.write('ssd_dim: ' + str(args.ssd_dim) + '\n')
        res_file.write('confidence_threshold: ' +
                       str(args.confidence_threshold) + '\n')
        res_file.write('nms_threshold: ' + str(args.nms_threshold) + '\n')
        res_file.write('top_k: ' + str(args.top_k) + '\n')
        res_file.write('dataset_name: ' + str(args.dataset_name) + '\n')
        res_file.write('set_file_name: ' + str(args.set_file_name) + '\n')
        res_file.write('detection: ' + str(args.detection) + '\n')
        res_file.write('~~~~~~~~~~~~~~~~~\n')
        res_file.write(
            'Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = {:.4f}\n'
            .format(means[0]))
        res_file.write(
            'Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = {:.4f}\n'
            .format(means[1]))
        res_file.write(
            'Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = {:.4f}\n'
            .format(means[2]))
        res_file.write(
            'Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = {:.4f}\n'
            .format(means[3]))
        res_file.write(
            'Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = {:.4f}\n'
            .format(means[4]))
        res_file.write(
            'Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = {:.4f}\n'
            .format(means[5]))
        res_file.write(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = {:.4f}\n'
            .format(means[6]))
        res_file.write(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = {:.4f}\n'
            .format(means[7]))
        res_file.write(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = {:.4f}\n'
            .format(means[8]))
        res_file.write(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = {:.4f}\n'
            .format(means[8]))
        res_file.write(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = {:.4f}\n'
            .format(means[10]))
        res_file.write(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = {:.4f}\n'
            .format(means[11]))
        if FPS:
            for i, f in enumerate(FPS):
                res_file.write(str(i) + ': FPS = {:.4f}\n'.format(f))
Esempio n. 7
0
    def __init__(self,
                 root,
                 image_sets,
                 preproc=None,
                 target_transform=None,
                 dataset_name='COCO',
                 classes=None,
                 box_num=1000000000):
        self.root = root
        self.cache_path = os.path.join(self.root, 'cache')
        self.image_set = image_sets
        self.preproc = preproc
        self.target_transform = target_transform
        self.name = dataset_name
        self.classes = classes
        self.ids = list()
        self.annotations = list()
        self.box_num = int(box_num)
        self._view_map = {
            'minival2014': 'val2014',  # 5k val2014 subset
            'valminusminival2014': 'val2014',  # val2014 \setminus minival2014
            'test-dev2015': 'test2015',
        }

        for (year, image_set) in image_sets:
            coco_name = image_set + year
            data_name = (self._view_map[coco_name]
                         if coco_name in self._view_map else coco_name)
            annofile = self._get_ann_file(coco_name)
            _COCO = COCO(annofile)
            self._COCO = _COCO
            self.coco_name = coco_name

            if self.classes == "youtube_bb":
                cats = _COCO.loadCats(
                    _COCO.getCatIds(catNms=classes_youtubebb))
            elif self.classes == "youtube_bb_sub":
                cats = _COCO.loadCats(
                    _COCO.getCatIds(catNms=classes_youtubebb_sub))
            else:
                cats = _COCO.loadCats(_COCO.getCatIds())

            self._classes = tuple(['__background__'] +
                                  [c['name'] for c in cats])
            self._classesids = tuple([0] + [c['id'] for c in cats])
            self.num_classes = len(self._classes)
            self._class_to_ind = dict(
                zip(self._classes, range(self.num_classes)))

            if self.classes == "youtube_bb":
                self._class_to_coco_cat_id = dict(
                    zip([c['name'] for c in cats],
                        _COCO.getCatIds(catNms=classes_youtubebb)))
            elif self.classes == "youtube_bb_sub":
                self._class_to_coco_cat_id = dict(
                    zip([c['name'] for c in cats],
                        _COCO.getCatIds(catNms=classes_youtubebb_sub)))
            else:
                self._class_to_coco_cat_id = dict(
                    zip([c['name'] for c in cats], _COCO.getCatIds()))

            indexes = []
            self.indexes_limit = []

            for cat in cats:
                indexes.extend(_COCO.getImgIds(catIds=cat["id"]))
            indexes = set(indexes)
            indexes = list(indexes)
            random.seed(box_num)
            random.shuffle(indexes)

            if image_set.find('test') != -1:
                print('test set will not load annotations!')
            else:
                self.annotations.extend(
                    self._load_coco_annotations(coco_name, indexes, _COCO,
                                                self.box_num))

            self.image_indexes = self.indexes_limit
            self.ids.extend([
                self.image_path_from_index(data_name, index)
                for index in self.indexes_limit
            ])