Example #1
0
    def __getitem__(self, idx):
        # Generate random data if a bucket name is not provided.
        if self._bucket_name is None:
            array = np.random.rand(256, 256, 3)
            img = Image.fromarray(array, mode="RGB")
            return transformfn.to_tensor(img), np.random.choice(1000)

        img_path = self._imgs_paths[idx]
        blob = self._bucket.blob(img_path)
        if self._streaming:
            img_str = download_gcs_blob_with_backoff(blob)
        else:
            target_path = os.path.join(self._target_dir, img_path)
            if not os.path.exists(target_path):
                create_file_dirs(target_path)
                print("downloading...")
                img_str = download_gcs_blob_with_backoff(blob)
                with open(target_path, "wb") as f:
                    f.write(img_str)
            else:
                with open(target_path, "rb") as f:
                    img_str = f.read()

        img_bytes = BytesIO(img_str)
        img = Image.open(img_bytes)
        img = img.convert("RGB")

        if self._transform is not None:
            img = self._transform(img)

        return img, self._labels[idx]
Example #2
0
 def f(fname):
     if is_aws:
         s3_object = s3.meta.client.get_object(Bucket="determined-ai-coco-dataset", Key=fname)
         im = cv2.imdecode(
             np.asarray(bytearray(s3_object["Body"].read()), dtype=np.uint8), cv2.IMREAD_COLOR,
         )
     elif is_gcs:
         blob = bucket.blob(fname)
         s = download_gcs_blob_with_backoff(blob)
         im = cv2.imdecode(np.asarray(bytearray(s), dtype=np.uint8), cv2.IMREAD_COLOR)
     else:
         im = cv2.imread(fname, cv2.IMREAD_COLOR)
     assert im is not None, fname
     return im
Example #3
0
 def get(self, filepath):
     filepath = self.convert_filepath(filepath)
     blob = self._bucket.blob(filepath)
     img_str = download_gcs_blob_with_backoff(blob)
     return img_str
Example #4
0
    def __call__(self, roidb):
        fname, boxes, klass, is_crowd = (
            roidb["file_name"],
            roidb["boxes"],
            roidb["class"],
            roidb["is_crowd"],
        )
        boxes = np.copy(boxes)
        if self.is_aws:
            s3_object = self.s3.meta.client.get_object(
                Bucket="determined-ai-coco-dataset", Key=fname
            )
            im = cv2.imdecode(
                np.asarray(bytearray(s3_object["Body"].read()), dtype=np.uint8), cv2.IMREAD_COLOR,
            )
        elif self.is_gcs:
            blob = self.bucket.blob(fname)
            s = download_gcs_blob_with_backoff(blob)
            im = cv2.imdecode(np.asarray(bytearray(s), dtype=np.uint8), cv2.IMREAD_COLOR)
        else:
            im = cv2.imread(fname, cv2.IMREAD_COLOR)
        assert im is not None, fname
        im = im.astype("float32")
        height, width = im.shape[:2]
        # assume floatbox as input
        assert boxes.dtype == np.float32, "Loader has to return floating point boxes!"

        if not self.cfg.DATA.ABSOLUTE_COORD:
            boxes[:, 0::2] *= width
            boxes[:, 1::2] *= height

        # augmentation:
        im, params = self.aug.augment_return_params(im)
        points = box_to_point8(boxes)
        points = self.aug.augment_coords(points, params)
        boxes = point8_to_box(points)
        assert np.min(np_area(boxes)) > 0, "Some boxes have zero area!"

        ret = {"image": im}
        # Add rpn data to dataflow:
        try:
            if self.cfg.MODE_FPN:
                multilevel_anchor_inputs = self.get_multilevel_rpn_anchor_input(im, boxes, is_crowd)
                for i, (anchor_labels, anchor_boxes) in enumerate(multilevel_anchor_inputs):
                    ret["anchor_labels_lvl{}".format(i + 2)] = anchor_labels
                    ret["anchor_boxes_lvl{}".format(i + 2)] = anchor_boxes
            else:
                ret["anchor_labels"], ret["anchor_boxes"] = self.get_rpn_anchor_input(
                    im, boxes, is_crowd
                )

            boxes = boxes[is_crowd == 0]  # skip crowd boxes in training target
            klass = klass[is_crowd == 0]
            ret["gt_boxes"] = boxes
            ret["gt_labels"] = klass
            if not len(boxes):
                raise MalformedData("No valid gt_boxes!")
        except MalformedData as e:
            log_once("Input {} is filtered for training: {}".format(fname, str(e)), "warn")
            return None

        if self.cfg.MODE_MASK:
            # augmentation will modify the polys in-place
            segmentation = copy.deepcopy(roidb["segmentation"])
            segmentation = [segmentation[k] for k in range(len(segmentation)) if not is_crowd[k]]
            assert len(segmentation) == len(boxes)

            # Apply augmentation on polygon coordinates.
            # And produce one image-sized binary mask per box.
            masks = []
            width_height = np.asarray([width, height], dtype=np.float32)
            gt_mask_width = int(
                np.ceil(im.shape[1] / 8.0) * 8
            )  # pad to 8 in order to pack mask into bits
            for polys in segmentation:
                if not self.cfg.DATA.ABSOLUTE_COORD:
                    polys = [p * width_height for p in polys]
                polys = [self.aug.augment_coords(p, params) for p in polys]
                masks.append(segmentation_to_mask(polys, im.shape[0], gt_mask_width))
            masks = np.asarray(masks, dtype="uint8")  # values in {0, 1}
            masks = np.packbits(masks, axis=-1)
            ret["gt_masks_packed"] = masks

            # from viz import draw_annotation, draw_mask
            # viz = draw_annotation(im, boxes, klass)
            # for mask in masks:
            #     viz = draw_mask(viz, mask)
            # tpviz.interactive_imshow(viz)
        return ret