Example #1
0
    def reduced_bbox_result(self, box_regression, proposals):

        box_regression = cat(box_regression, dim=0)
        device = box_regression.device

        labels = cat([proposal.get_field("labels") for proposal in proposals],
                     dim=0)
        regression_targets = cat([
            proposal.get_field("regression_targets") for proposal in proposals
        ],
                                 dim=0)

        sampled_pos_inds_subset = torch.nonzero(labels > 0).squeeze(1)
        labels_pos = labels[sampled_pos_inds_subset]

        map_inds = 4 * labels_pos[:, None] + torch.tensor([0, 1, 2, 3],
                                                          device=device)

        image_shapes = [box.size for box in proposals]
        boxes_per_image = [len(box) for box in proposals]
        concat_boxes = torch.cat([a.bbox for a in proposals], dim=0)

        prefix_sum_boxes = [boxes_per_image[0]]
        for box_per_images in boxes_per_image[1:]:
            prefix_sum_boxes.append(box_per_images + prefix_sum_boxes[-1])

        reduced_boxes_per_image = [0] * len(prefix_sum_boxes)
        i, j = 0, 0
        while i < len(sampled_pos_inds_subset):
            if sampled_pos_inds_subset[i] < prefix_sum_boxes[j]:
                reduced_boxes_per_image[j] += 1
                i += 1
            else:
                j += 1

        proposals = self.box_coder.decode(
            box_regression[sampled_pos_inds_subset[:, None], map_inds],
            concat_boxes[sampled_pos_inds_subset])

        proposals = proposals.split(reduced_boxes_per_image, dim=0)

        box_targets = self.box_coder.decode(
            regression_targets[sampled_pos_inds_subset],
            concat_boxes[sampled_pos_inds_subset])

        box_targets = box_targets.split(reduced_boxes_per_image, dim=0)

        result = []
        for boxes, image_shape in zip(proposals, image_shapes):
            boxlist = BoxList(boxes, image_shape, mode="xyxy")
            boxlist = boxlist.clip_to_image(remove_empty=False)
            result.append(boxlist)

        box_result = []
        for boxes, image_shape in zip(box_targets, image_shapes):
            boxlist = BoxList(boxes, image_shape, mode="xyxy")
            boxlist = boxlist.clip_to_image(remove_empty=False)
            box_result.append(boxlist)

        return result, box_result
Example #2
0
    def __getitem__(self, idx):
        img, anno = super(CityPersonsDataset, self).__getitem__(idx)

        # filter crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]

        boxes = [obj["bbox"] for obj in anno]
        posv = [obj["posv"] for obj in anno]
        ignore = [obj["ignore"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        posv = torch.as_tensor(posv).reshape(-1, 4)  # guard against no boxes
        ignore = torch.tensor(ignore)
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")
        posv_target = BoxList(posv, img.size, mode="xywh").convert("xyxy")
        posv_target = posv_target.clip_to_image(remove_empty=False)

        classes = [obj["category_id"] for obj in anno]
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)
        target.add_field("posv", posv_target)
        target.add_field("ignore", ignore)

        masks = [obj["segmentation"] for obj in anno]
        masks = SegmentationMask(masks, img.size)
        target.add_field("masks", masks)

        target = target.clip_to_image(remove_empty=False)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx
    def get_groundtruth(self, index, evaluation=False, flip_img=False):
        img_info = self.get_img_info(index)
        w, h = img_info['width'], img_info['height']
        # important: recover original box from BOX_SCALE
        box = self.gt_boxes[index] / BOX_SCALE * max(w, h)
        box = torch.from_numpy(box).reshape(-1, 4)  # guard against no boxes
        if flip_img:
            new_xmin = w - box[:, 2]
            new_xmax = w - box[:, 0]
            box[:, 0] = new_xmin
            box[:, 2] = new_xmax
        target = BoxList(box, (w, h), 'xyxy')  # xyxy

        target.add_field("labels", torch.from_numpy(self.gt_classes[index]))
        target.add_field("attributes",
                         torch.from_numpy(self.gt_attributes[index]))

        relation = self.relationships[index].copy()  # (num_rel, 3)
        if self.filter_duplicate_rels:
            # Filter out dupes!
            assert self.split == 'train'
            old_size = relation.shape[0]
            all_rel_sets = defaultdict(list)
            for (o0, o1, r) in relation:
                all_rel_sets[(o0, o1)].append(r)
            relation = [(k[0], k[1], np.random.choice(v))
                        for k, v in all_rel_sets.items()]
            relation = np.array(relation, dtype=np.int32)

        # add relation to target
        num_box = len(target)
        relation_map = torch.zeros((num_box, num_box), dtype=torch.int64)
        for i in range(relation.shape[0]):
            if relation_map[int(relation[i, 0]), int(relation[i, 1])] > 0:
                if (random.random() > 0.5):
                    relation_map[int(relation[i, 0]),
                                 int(relation[i, 1])] = int(relation[i, 2])
            else:
                relation_map[int(relation[i, 0]),
                             int(relation[i, 1])] = int(relation[i, 2])
        target.add_field("relation", relation_map, is_triplet=True)

        if evaluation:
            target = target.clip_to_image(remove_empty=False)
            target.add_field("relation_tuple",
                             torch.LongTensor(relation))  # for evaluation
            return target
        else:
            target = target.clip_to_image(remove_empty=True)
            return target
    def __call__(self, image, target):

        img, target = self.rrc_obj(image, target)
        #   target = target.convert('xywh')
        #   plt.figure()
        #   plt.subplot(1, 2, 1)
        #   print(target.bbox)
        #   plt.pcolor(torch.sum(F.to_tensor(image), dim=0))#.permute(1,2,0))
        #   show_bbox(target.bbox.reshape(-1))
        #   plt.subplot(1, 2, 2)
        #   plt.pcolor(target.get_field('masks').get_mask_tensor())
        #   plt.show()

        #       Generate the Random Integer to pick the background from the backgound data
        rand_num = random.randint(0, self.num_bg - 1)
        bg = self.bg_object[rand_num]
        bg = self.rrc(bg)
        bg = self.color_jig(bg)
        bg_size = bg.size
        r_mask = torch.zeros(bg_size)
        bg = F.to_tensor(bg)

        mask = target.get_field('masks').get_mask_tensor()
        classes = target.get_field('labels')
        nz_idx = torch.nonzero(mask)

        img_sx, img_sy = img.size
        img = F.to_tensor(img)

        rand_x, rand_y = random.randint(0, self.bg_size-img_sx-1), \
            random.randint(0, self.bg_size-img_sy-1)

        r_mask[nz_idx[:, 0] + rand_y, nz_idx[:, 1] + rand_x] = \
            mask[nz_idx[:, 0], nz_idx[:, 1]].float()

        bg[:, nz_idx[:, 0] + rand_y, nz_idx[:, 1] + rand_x] = \
            img[:, nz_idx[:, 0], nz_idx[:, 1]]

        #print('BBox :{}, Mode :{}'.format(target.bbox, target.mode))
        #print(rand_x, rand_y)

        boxes = target.bbox + torch.tensor([rand_x, rand_y, rand_x, rand_y],
                                           dtype=torch.float)
        target = BoxList(boxes, bg_size, mode="xyxy")

        target.add_field("labels", classes)
        r_mask = SegmentationMask(r_mask, bg_size, mode='mask')
        target.add_field("masks", r_mask)
        target.clip_to_image(remove_empty=True)
        return bg, target
Example #5
0
    def __getitem__(self, idx):
        img, anno = super(COCODataset, self).__getitem__(idx)

        # filter crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]

        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"] for obj in anno]
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        masks = [obj["segmentation"] for obj in anno]
        masks = SegmentationMask(masks, img.size)
        target.add_field("masks", masks)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx
Example #6
0
    def __getitem__(self, idx):
        img, anno = super(ModaNetDataset, self).__getitem__(idx)

        # filter crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]

        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"] + 1 for obj in anno]
        #print(classes,'old')
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        #print(classes,classes2)
        classes = torch.tensor(classes)
        target.add_field("labels", classes)  #

        #masks = [obj["segmentation"] for obj in anno]
        #masks = SegmentationMask(masks, img.size, mode='poly')
        #target.add_field("masks", masks)

        #if anno and "keypoints" in anno[0]:
        #   keypoints = [obj["keypoints"] for obj in anno]
        #  keypoints = PersonKeypoints(keypoints, img.size)
        # target.add_field("keypoints", keypoints)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx
Example #7
0
    def __getitem__(self, index):
        ann = self.annotations[index]
        img_path = os.path.join(self.images_dir, ann['file_name'])
        img = Image.open(img_path).convert("RGB")
        width, height = img.size[0], img.size[1]
        boxes = []
        labels = []
        for category, x, y, w, h in ann['bbox']:
            boxes.append([x, y, x + w, y + h])
            labels.append(category)

        target = BoxList(torch.tensor(boxes, dtype=torch.float32),
                         (width, height),
                         mode="xyxy")
        target.add_field('labels', torch.tensor(labels))
        target = target.clip_to_image(remove_empty=True)
        scale_w = DENSITY_MAP_WIDTH / img.width
        scale_h = DENSITY_MAP_HEIGHT / img.height
        if self.density:
            gt = np.zeros((DENSITY_MAP_HEIGHT, DENSITY_MAP_WIDTH))
            for category, x, y, w, h in ann['bbox']:
                cx = x + w / 2
                cy = y + h / 2
                gt[round(cy * scale_h), round(cx * scale_w)] = 1
            density = gaussian_filter_density(gt)
            assert density.shape[0] == DENSITY_MAP_HEIGHT and density.shape[
                1] == DENSITY_MAP_WIDTH
            target.add_field('density_map', DensityMap(density))

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, index
Example #8
0
    def __getitem__(self, idx):
        import pdb;pdb.set_trace()
        print("you have reached micr datatset get method")
        img, anno = super(MICRDataset, self).__getitem__(idx)

        # filter crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]

        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"] for obj in anno]
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        if anno and "segmentation" in anno[0]:
            masks = [obj["segmentation"] for obj in anno]
            masks = SegmentationMask(masks, img.size, mode='poly')
            target.add_field("masks", masks)

        if anno and "keypoints" in anno[0]:
            keypoints = [obj["keypoints"] for obj in anno]
            keypoints = PersonKeypoints(keypoints, img.size)
            target.add_field("keypoints", keypoints)

        target = target.clip_to_image(remove_empty=True)

        if self._transforms is not None:
            img, target = self._transforms(img, target)

        return img, target, idx
Example #9
0
    def __getitem__(self, idx):
        '''Load image.

        Args:
          idx: (int) image index.

        Returns:
          img: (tensor) image tensor.
          loc_targets: (tensor) location targets.
          cls_targets: (tensor) class label targets.
        '''
        # Load image and boxes.
        img_path = self.img_pathes[idx]
        if img_path not in self._cached_images.keys():
            self._cached_images[img_path] = self.get_image(img_path)
        img = self._cached_images[img_path]

        boxes = self.boxes[idx].clone()
        masks = self.masks[idx]
        target = BoxList(boxes, img.size, mode="xyxy")
        labels = self.labels[idx]
        target.add_field("labels", labels)
        masks = [[m] for m in masks]
        masks = SegmentationMask(masks, img.size)
        target.add_field("masks", masks)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx
Example #10
0
    def _get_target(self, img, index):

        # a list of label (x1, y1, x2, y2, class_id, instance_id)
        labels = self._labels[index]
        if len(labels) == 0:
            assert self._include_bg is True, "The image does not has ground truth"
            bbox = torch.as_tensor(labels).reshape(-1, 4)
            class_ids = torch.as_tensor(labels)
            instance_ids = torch.as_tensor(labels)
            empty_boxlist = BoxList(bbox, img.size, mode="xyxy")
            empty_boxlist.add_field("labels", class_ids)
            empty_boxlist.add_field("ids", instance_ids)
            return empty_boxlist

        labels = torch.as_tensor(labels).reshape(-1, 6)
        boxes = labels[:, :4]
        target = BoxList(boxes, img.size, mode="xyxy")

        class_ids = labels[:, 4].clone().to(torch.int64)
        target.add_field("labels", class_ids)

        instance_ids = labels[:, -1].clone().to(torch.int64)
        target.add_field("ids", instance_ids)

        if not self._amodal:
            target = target.clip_to_image(remove_empty=True)

        return target
Example #11
0
    def forward_for_single_feature_map(self, anchors, objectness,
                                       box_regression):
        """
        Arguments:
            anchors: list[BoxList]
            objectness: tensor of size N, A, H, W
            box_regression: tensor of size N, A * 4, H, W
        """
        device = objectness.device
        N, A, H, W = objectness.shape

        # put in the same format as anchors
        objectness = permute_and_flatten(objectness, N, A, 1, H, W).view(N, -1)
        objectness = objectness.sigmoid()
        #object: (B, H*W*A)

        box_regression = permute_and_flatten(box_regression, N, A, 4, H, W)
        #box_regression: (B, H*W*A, 4)
        num_anchors = A * H * W

        pre_nms_top_n = min(self.pre_nms_top_n, num_anchors)
        objectness, topk_idx = objectness.topk(pre_nms_top_n,
                                               dim=1,
                                               sorted=True)
        # select the biggest pre_nms_top_n

        batch_idx = torch.arange(N, device=device)[:, None]
        box_regression = box_regression[batch_idx, topk_idx]
        # select the corresponding box_regression

        # anchors: (boxlist(H/4*W/4*len(aspect_ratios))*B)
        image_shapes = [box.size for box in anchors]
        concat_anchors = torch.cat([a.bbox for a in anchors], dim=0)
        # anchors: (B*(H/4*W/4*len(aspect_ratios), 4)
        concat_anchors = concat_anchors.reshape(N, -1, 4)[batch_idx, topk_idx]
        # anchors: (B, topk_idx, 4)

        proposals = self.box_coder.decode(box_regression.view(-1, 4),
                                          concat_anchors.view(-1, 4))
        # proposals: (B*topk_idx, 4)

        proposals = proposals.view(N, -1, 4)
        # proposals: (B, topk_idx, 4)

        result = []
        for proposal, score, im_shape in zip(proposals, objectness,
                                             image_shapes):
            boxlist = BoxList(proposal, im_shape, mode="xyxy")
            boxlist.add_field("objectness", score)
            boxlist = boxlist.clip_to_image(remove_empty=False)
            boxlist = remove_small_boxes(boxlist, self.min_size)
            boxlist = boxlist_nms(
                boxlist,
                self.nms_thresh,
                max_proposals=self.post_nms_top_n,
                score_field="objectness",
            )
            result.append(boxlist)
        # result: [Boxlist(with objectness)*B]
        return result
Example #12
0
    def __getitem__(self, idx):
        name = self.id_to_img_map[idx]
        img_name = self.name2img_name(name)
        img_path = os.path.join(self.img_root, img_name)

        img = Image.open(img_path)

        # TODO might be better to add an extra field
        annos = self.annos[name]['annos']
        boxes = [anno['box'] for anno in annos]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xyxy")

        labels = [anno['label'] for anno in annos]
        labels = torch.tensor(labels)
        if not (labels < 3).all():
            print(name)
        target.add_field("labels", labels)

        masks = [anno['segmentation'] for anno in annos]
        masks = SegmentationMask(masks, img.size, mode='mask')
        target.add_field("masks", masks)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx
Example #13
0
    def get_image(self, index):
        name = self.id_to_img_map[index]
        img_name = self.name2img_name(name)
        img_path = os.path.join(self.img_root, img_name)

        img = Image.open(img_path).convert("RGB")
        size = img.size
        img = np.array(img)

        annos = self.annos[name]['annos']
        boxes = [anno['box'] for anno in annos]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, size, mode="xyxy")

        labels = [anno['label'] for anno in annos]
        labels = torch.tensor(labels)
        target.add_field("labels", labels)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            # convert rgb to bgr
            img_tansformed = self.basetransform(img)
        else:
            img_tansformed = img

        return img, img_tansformed, target
Example #14
0
    def get_groundtruth(self, idx, raw_id=True):
        coco = self.coco
        img_id = self.ids[idx]
        ann_ids = coco.getAnnIds(imgIds=img_id)
        anno = coco.loadAnns(ann_ids)

        img_info = self.get_img_info(idx)

        anno = [obj for obj in anno if obj["iscrowd"] == 0]
        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, (img_info['width'], img_info['height']),
                         mode="xywh").convert("xyxy")

        classes = [obj["category_id"] for obj in anno]
        if raw_id == False:
            classes = [
                self.json_category_id_to_contiguous_id[c] for c in classes
            ]
        classes = torch.tensor(classes, dtype=torch.int64)
        target.add_field("labels", classes)

        target = target.clip_to_image(remove_empty=True)

        # if self._transforms is not None:
        #     img, target = self._transforms(img, target)
        return target
Example #15
0
    def __getitem__(self, idx):
        img, anno = super(COCODataset, self).__getitem__(idx)

        # 过滤 crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]

        # 把过滤之后的annotations对应的box转换成BoxList对象, 注意这些box可能属于多个类别
        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        # 将boxes的labels作为属性添加到target对象当中
        classes = [obj["category_id"] for obj in anno]
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        masks = [obj["segmentation"] for obj in anno]
        masks = SegmentationMask(masks, img.size)
        target.add_field("masks", masks)

        # TODO: 关键点代码
        if anno and "keypoints" in anno[0]:
            keypoints = [obj["keypoints"] for obj in anno]
            keypoints = PersonKeypoints(keypoints, img.size)
            target.add_field("keypoints", keypoints)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx
Example #16
0
    def __getitem__(self, idx):
        img, anno = super(WordDataset, self).__getitem__(idx)

        # filter crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]

        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"] for obj in anno]
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        if anno and 'rec' in anno[0]:
            rec = [obj["rec"] for obj in anno]
            rec = REC(rec, img.size)
            target.add_field("rec", rec)

        if anno and 'bezier_pts' in anno[0]:
            bezier = [obj["bezier_pts"] for obj in anno]
            bezier = torch.as_tensor(bezier).reshape(-1, 16)  # guard against no boxes
            bezier = bezier[:, BEZIER_IDX]
            bezier = BEZIER(bezier, img.size)
            target.add_field("beziers", bezier)

        target = target.clip_to_image(remove_empty=True)

        if self._transforms is not None:
            img, target = self._transforms(img, target)

        return img, target, idx
Example #17
0
    def __getitem__(self, idx):
        """
        keep data type as numpy while dataloader process until been fetched
        into train process, otherwise the shared memory leak gonna happened
        :param idx:
        :return:
        """
        img, anno = super(VGDataset, self).__getitem__(idx)
        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        det_targets = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"] for obj in anno]
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        det_targets.add_field("labels", classes)

        rel_targets = self.relationships[self.ids[idx]]
        rel_targets = torch.as_tensor(rel_targets)

        det_targets = det_targets.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, det_targets = self.transforms(img, det_targets)
        if cfg.MODEL.RELATION_ON:
            return img, det_targets, rel_targets, idx
        else:
            return img, det_targets, idx
Example #18
0
    def getitem1(self, video_path, gts, gt_id, is_template=False):
        img_id = gt_id + 1  # 易错:gt与图像的对应关系:gt从第0行开始,而图像编号从1开始。
        gt = gts[gt_id]  # np vector, (4,) xywh
        if 'MapEditor' in self.root:
            img_path = os.path.join(video_path, '%06d.jpg' % gt_id)  # 注意,gta数据集的图像编号是从0开始的。
        else:
            img_path = os.path.join(video_path, '%08d.jpg' % img_id)
        img = Image.open(img_path).convert('RGB')
        '''对于target,裁剪并缩放'''
        if is_template:
            img, gt = process_template(img, gt)

        box = torch.from_numpy(gt).reshape(1, 4)
        target = BoxList(box, img.size, mode="xywh").convert("xyxy")
        target.add_field("labels", torch.Tensor([1]).long())

        '''进行clip_to_image/transform'''
        target = target.clip_to_image(remove_empty=True)
        if target.bbox.shape[0] == 0:
            print('无目标', img_path)
        if self._transforms is not None:
            if is_template:
                img, target = self._template_transforms(img, target)
            else:
                img, target = self._transforms(img, target)
        return img, target
Example #19
0
    def __getitem__(self, idx):
        annotations = [
            ann for ann in self.annt_labels['annotations']
            if ann['image_id'] == idx
        ]
        imageName = self.annt_labels["images"][idx]["file_name"]
        bboxes, masks, classes = self._getannotationfields(annotations)
        # opening the image
        img = Image.open(imageName).convert("RGB")

        bboxes = torch.as_tensor(bboxes).reshape(-1, 4)
        target = BoxList(bboxes, img.size, mode='xywh').convert('xyxy')

        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        masks = SegmentationMask(masks, img.size, mode='poly')
        target.add_field("masks", masks)

        target = target.clip_to_image(remove_empty=True)

        if self._transforms is not None:
            img, target = self._transforms(img, target)

        return img, target, idx
Example #20
0
    def __getitem__(self, idx):

        anno = self.ann_file[idx]
        path = anno['filename']
        img = Image.open(os.path.join(self.root, path)).convert('RGB')
        anno_obj = anno['objects']
        # filter crowd annotations
        # TODO might be better to add an extra field
        # anno = [obj for obj in anno if obj["iscrowd"] == 0]

        boxes = [obj["bbox"] for obj in anno_obj]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"] for obj in anno_obj]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        ids = [i for i in range(len(classes))]
        ids = torch.tensor(ids)
        target.add_field("ids", ids)

        target = target.clip_to_image(remove_empty=True)
        objects_pairs = [obj for obj in anno['objects_pairs']]
        objects_pairs = torch.tensor(objects_pairs)
        target.add_field("objects_pairs", objects_pairs)

        predicate_label = [[obj] for obj in anno['predicate_label']]
        predicate_label = torch.tensor(predicate_label)
        target.add_field('predicate_label', predicate_label)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx
Example #21
0
    def __getitem__(self, idx):
        img, anno = super(COCODataset, self).__getitem__(idx)

        # filter crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]
        #anno = [obj for obj in anno if obj["iscrowd"] == 1]

        if not anno:
            raise ValueError(
                "Image id {} ({}) doesn't have annotations!".format(
                    self.ids[idx], anno))

        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"] for obj in anno]
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        masks = [obj["segmentation"] for obj in anno]
        masks = SegmentationMask(masks, img.size)
        target.add_field("masks", masks)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx
Example #22
0
    def get_groundtruth(self, idx):
        img = self.idx_to_img[idx]
        boxes = self.detail.getBboxes(img)
        # example of 'boxes':
        # [{'bbox': [250, 209, 241, 149], 'category': 'motorbike'},
        # {'bbox': [312, 139, 109, 191], 'category': 'person'}]
        boxes = [box['bbox'] for box in boxes
                 ]  # TODO gubimy informację o otoczonym przedmiocie
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, self._img_size(img),
                         mode="xywh").convert("xyxy")
        target = target.clip_to_image(remove_empty=True)

        img_keypoints = self.detail.getKpts(img)
        keypoints = [skelton['keypoints'] for skelton in img_keypoints]

        # TODO keypoints - gubimy informację o bbox
        target.add_field("kpts", Keypoints(keypoints, self._img_size(img)))
        # target.add_field("mask", SegmentationMask(self.detail.getMask(img).tolist(), size=self._img_size(img)))
        # TODO getMask zwraca macierz rozmiaru (img.height, img.width), gdzie każdemu pikselowi
        # TODO odpowiada numer id klasy, do której należy. SegmentationMask

        # from getMask() doc:
        # If semantic segmentation of an image is requested (cat=instance=superpart=part=None),
        # the result is an image whose pixel values are the class IDs for that image.
        # If instance-level segmentation for one category of an image is requested (img and cat provided),
        # the result is an image whose pixel values are the instance IDs for that class and 0 everywhere else.
        target.add_field("class_mask", self.detail.getMask(img))
        target.add_field("instance_mask", self.detail.getMask(img,
                                                              cat='person'))
        target.add_field("bounds", self.detail.getBounds(img))
        target.add_field("occl", self.detail.getOccl(img))
        # TODO human parts?

        return target
Example #23
0
    def get_image(self, index):
        name = self.id_to_img_map[index]
        img_path = os.path.join(self.root, name)

        img = Image.open(img_path).convert("RGB")
        size = img.size
        img = np.array(img)[:, :, [2, 1, 0]]

        annos = self.annos[name]
        boxes = [annos[:4]]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, size, mode="xyxy")

        classes = [annos[4]]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img_tansformed = self.basetransform(img)
        else:
            img_tansformed = img

        return img, img_tansformed, target
    def __getitem__(self, idx):
        img, anno = super(MosquitoesCOCODataset, self).__getitem__(idx)

        # filter crowd annotations
        # TODO might be better to add an extra field
        anno = [obj for obj in anno if obj["iscrowd"] == 0]

        boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")

        classes = [obj["category_id"] for obj in anno]
        classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        # Not using masks
        # masks = [obj["segmentation"] for obj in anno]
        # masks = SegmentationMask(masks, img.size)
        # target.add_field("masks", masks)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx
Example #25
0
    def __getitem__(self, idx):
        name = self.id_to_img_map[idx]
        mask_name = self.img_name2mask_name(name)
        img_path = os.path.join(self.root, name)
        mask_path = os.path.join(self.mask_root, mask_name)

        img = Image.open(img_path)
        mask = np.array(Image.open(mask_path))
        mask = mask / 255
        mask = mask.astype(np.uint8)

        # TODO might be better to add an extra field
        annos = self.annos[name]
        boxes = [annos[:4]]
        boxes = torch.as_tensor(boxes).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xyxy")

        classes = [annos[4]]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        masks = [mask]
        masks = SegmentationMask(masks, img.size, mode='mask')
        target.add_field("masks", masks)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, idx
    def boxlist_crop(self, box: BoxList, x1, y1, x2, y2):
        """
         Adjust the coordinate of the bounding box within
         image crop specified by (x1, y1, x2, y2)
        """

        w, h = (x2 - x1), (y2 - y1)
        xmin, ymin, xmax, ymax = box._split_into_xyxy()
        cropped_xmin = (xmin - x1)
        cropped_ymin = (ymin - y1)
        cropped_xmax = (xmax - x1)
        cropped_ymax = (ymax - y1)
        cropped_bbox = torch.cat(
            (cropped_xmin, cropped_ymin, cropped_xmax, cropped_ymax), dim=-1)
        cropped_box = BoxList(cropped_bbox, (w, h), mode="xyxy")
        for k, v in box.extra_fields.items():
            cropped_box.add_field(k, v)

        if self.amodal:
            # amodal allows the corners of bbox go beyond image boundary
            cropped_box = self.remove_invisible_box(cropped_box)
        else:
            # the corners of bbox need to be within image boundary for non-amodal training
            cropped_box = cropped_box.clip_to_image(remove_empty=True)
        return cropped_box.convert(box.mode)
Example #27
0
    def __getitem__(self, index):
        ann = self.annotations[index]
        img_path = os.path.join(self.images_dir, ann['file_name'])
        img = Image.open(img_path).convert("RGB")
        width, height = img.size[0], img.size[1]
        boxes = []
        labels = []
        for category, x, y, w, h in ann['bbox']:
            boxes.append([x, y, x + w, y + h])
            labels.append(category)

        target = BoxList(torch.tensor(boxes, dtype=torch.float32), (width, height), mode="xyxy")
        target.add_field('labels', torch.tensor(labels))
        target = target.clip_to_image(remove_empty=True)
        if self.use_density_map:
            size = int(800 * self.density_map_stride)
            image_size = img.width  # Test images are squares, except 20180824-14-36-38-430.jpg(1860x1859)
            num_classes = self.density_categories
            super_categories = [rpc_category_to_super_category(category, self.density_categories) for category in labels]
            density_map = generate_density_map(super_categories, boxes,
                                               scale=size / image_size,
                                               size=size,
                                               num_classes=num_classes,
                                               min_sigma=self.density_min_sigma)
            target.add_field('heatmap', Heatmap(torch.from_numpy(density_map)))

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, index
Example #28
0
    def __getitem__(self, i):
        """Returns the i-th example.

        Returns a color image and bounding boxes. The image is in CHW format.
        The returned image is RGB.

        Args:
            i (int): The index of the example.

        Returns:
            tuple of an image in CHW format, bounding boxes in 
            ('ymin', 'xmin', 'ymax', 'xmax')  format, label as int32
            starting from 0 and difficult_flag, which is always 0 in 
            iNat 

        """
        img_file = os.path.join(self.root, self.impaths[i])
        img = PIL.Image.open(img_file).convert('RGB')

        boxes = self.bboxes[i]
        boxes = torch.as_tensor(np.array(boxes)[:,[1,0,3,2]]).reshape(-1, 4)  # guard against no boxes
        target = BoxList(boxes, img.size, mode="xyxy")

        classes = self.labels[i]
        classes = torch.tensor(classes)
        target.add_field("labels", classes)

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, i
Example #29
0
    def __getitem__(self, index):
        ann = self.annotations[self.images[index]['id']]
        img_path = os.path.join(self.images_dir, self.images[index]['file_name'])
        img = Image.open(img_path).convert("RGB")
        width, height = img.size[0], img.size[1]
        boxes = []
        labels = []
        viz = False
        for obj in ann:
            if obj['score'] > self.threshold:
                category = obj['category_id']
                x, y, w, h = obj['bbox']
                boxes.append([x, y, x + w, y + h])
                labels.append(category)
            else:
                x, y, w, h = [round(k) for k in obj['bbox']]
                img = np.array(img)
                img[y:y + h, x:x + w, :] = (164, 166, 164)
                img = Image.fromarray(img, mode='RGB')
        if viz:
            import matplotlib.pyplot as plt
            plt.imshow(img)
            plt.show()
            quit()

        target = BoxList(torch.tensor(boxes, dtype=torch.float32), (width, height), mode="xyxy")
        target.add_field('labels', torch.tensor(labels))
        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target, index
Example #30
0
    def __getitem__(self, index):
        img = Image.open(self.filenames[index]).convert("RGB")

        # important: recover original box from BOX_SCALE
        box = self.gt_boxes[index] / BOX_SCALE * max(img.size)
        target = BoxList(box, img.size, 'xywh').convert('xyxy')
        
        target.add_field("labels", self.gt_classes[index])

        target = target.clip_to_image(remove_empty=True)

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        relation = self.relationships[index].copy() # (num_rel, 3)
        if self.filter_duplicate_rels:
            # Filter out dupes!
            assert self.split == 'train'
            old_size = relation.shape[0]
            all_rel_sets = defaultdict(list)
            for (o0, o1, r) in relation:
                all_rel_sets[(o0, o1)].append(r)
            relation = [(k[0], k[1], np.random.choice(v)) for k,v in all_rel_sets.items()]
            relation = np.array(relation)
        
        # add relation to target
        # TODO Kaihua Tang
        # check whether it is valid
        target.add_field("relation", relation)

        return img, target, index
Example #31
0
    def __getitem__(self, idx):
        # img, anno = super(COCODataset_VIRAT, self).__getitem__(idx)
        img_info = self.img_infos[idx]
        video_name = img_info['video_name']
        img_format = self.img_format
        first_id = int(img_info['frame_id'])

        anno = self.get_ann_info(idx)
        gt_bboxes = anno['bboxes']
        gt_labels = anno['labels']
        read_format = os.path.join(self.root, video_name, img_format)
        if not os.path.exists(read_format.format(first_id)):
            first_id = self.my_path(first_id)
        img = Image.open(read_format.format(first_id)).convert("RGB")
        images = [img]
        for i in range(1, self.frame_number):
            now_id = first_id + i * self.frame_interval
            try:
                now_img = Image.open(read_format.format(now_id)).convert("RGB")
            except:
                print('error loading image:', read_format.format(now_id))
                now_img = img.copy()
            images.append(now_img)
        
        # filter crowd annotations
        # TODO might be better to add an extra field
        # anno = [obj for obj in anno if obj["iscrowd"] == 0]

        # boxes = [obj["bbox"] for obj in anno]
        boxes = torch.as_tensor(gt_bboxes).reshape(-1, 4)  # guard against no boxes
        # target = BoxList(boxes, img.size, mode="xywh").convert("xyxy")
        target = BoxList(boxes, img.size, mode='xyxy')

        # classes = [obj["category_id"] for obj in anno]
        # classes = [self.json_category_id_to_contiguous_id[c] for c in classes]
        classes = torch.tensor(gt_labels)
        target.add_field("labels", classes)

        # if anno and "segmentation" in anno[0]:
        #     masks = [obj["segmentation"] for obj in anno]
        #     masks = SegmentationMask(masks, img.size, mode='poly')
        #     target.add_field("masks", masks)

        # if anno and "keypoints" in anno[0]:
        #     keypoints = [obj["keypoints"] for obj in anno]
        #     keypoints = PersonKeypoints(keypoints, img.size)
        #     target.add_field("keypoints", keypoints)

        target = target.clip_to_image(remove_empty=True)

        if self._transforms is not None:
            images, target = self._transforms(images, target)

        if isinstance(images, list):
            images = torch.stack(images)
            images = images.transpose(1, 0)
        
        # print(self.img_infos[idx],self.get_img_info(idx))

        return images, target, idx