Beispiel #1
0
def get_cityscapes_panoptic_files(image_dir, gt_dir, json_info):
    files = []
    # scan through the directory
    cities = PathManager.ls(image_dir)
    logger.info(f"{len(cities)} cities found in '{image_dir}'.")
    image_dict = {}
    for city in cities:
        city_img_dir = os.path.join(image_dir, city)
        for basename in PathManager.ls(city_img_dir):
            image_file = os.path.join(city_img_dir, basename)

            suffix = "_leftImg8bit.png"
            assert basename.endswith(suffix), basename
            basename = os.path.basename(basename)[: -len(suffix)]

            image_dict[basename] = image_file

    for ann in json_info["annotations"]:
        image_file = image_dict.get(ann["image_id"], None)
        assert image_file is not None, "No image {} found for annotation {}".format(
            ann["image_id"], ann["file_name"]
        )
        label_file = os.path.join(gt_dir, ann["file_name"])
        segments_info = ann["segments_info"]

        files.append((image_file, label_file, segments_info))

    assert len(files), "No images found in {}".format(image_dir)
    assert PathManager.isfile(files[0][0]), files[0][0]
    assert PathManager.isfile(files[0][1]), files[0][1]
    return files
Beispiel #2
0
def load_cityscapes_panoptic(image_dir, gt_dir, gt_json, meta):
    """
    Args:
        image_dir (str): path to the raw dataset. e.g., "~/cityscapes/leftImg8bit/train".
        gt_dir (str): path to the raw annotations. e.g.,
            "~/cityscapes/gtFine/cityscapes_panoptic_train".
        gt_json (str): path to the json file. e.g.,
            "~/cityscapes/gtFine/cityscapes_panoptic_train.json".
        meta (dict): dictionary containing "thing_dataset_id_to_contiguous_id"
            and "stuff_dataset_id_to_contiguous_id" to map category ids to
            contiguous ids for training.

    Returns:
        list[dict]: a list of dicts in Detectron2 standard format. (See
        `Using Custom Datasets </tutorials/datasets.html>`_ )
    """

    def _convert_category_id(segment_info, meta):
        if segment_info["category_id"] in meta["thing_dataset_id_to_contiguous_id"]:
            segment_info["category_id"] = meta["thing_dataset_id_to_contiguous_id"][
                segment_info["category_id"]
            ]
        else:
            segment_info["category_id"] = meta["stuff_dataset_id_to_contiguous_id"][
                segment_info["category_id"]
            ]
        return segment_info

    assert os.path.exists(
        gt_json
    ), "Please run `python cityscapesscripts/preparation/createPanopticImgs.py` to generate label files."  # noqa
    with open(gt_json) as f:
        json_info = json.load(f)
    files = get_cityscapes_panoptic_files(image_dir, gt_dir, json_info)
    ret = []
    for image_file, label_file, segments_info in files:
        sem_label_file = (
            image_file.replace("leftImg8bit", "gtFine").split(".")[0] + "_labelTrainIds.png"
        )
        segments_info = [_convert_category_id(x, meta) for x in segments_info]
        ret.append(
            {
                "file_name": image_file,
                "image_id": "_".join(
                    os.path.splitext(os.path.basename(image_file))[0].split("_")[:3]
                ),
                "sem_seg_file_name": sem_label_file,
                "pan_seg_file_name": label_file,
                "segments_info": segments_info,
            }
        )
    assert len(ret), f"No images found in {image_dir}!"
    assert PathManager.isfile(
        ret[0]["sem_seg_file_name"]
    ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py"  # noqa
    assert PathManager.isfile(
        ret[0]["pan_seg_file_name"]
    ), "Please generate panoptic annotation with python cityscapesscripts/preparation/createPanopticImgs.py"  # noqa
    return ret
Beispiel #3
0
def load_coco_panoptic_json(json_file, image_dir, gt_dir, meta):
    """
    Args:
        image_dir (str): path to the raw dataset. e.g., "~/coco/train2017".
        gt_dir (str): path to the raw annotations. e.g., "~/coco/panoptic_train2017".
        json_file (str): path to the json file. e.g., "~/coco/annotations/panoptic_train2017.json".

    Returns:
        list[dict]: a list of dicts in Detectron2 standard format. (See
        `Using Custom Datasets </tutorials/datasets.html>`_ )
    """
    def _convert_category_id(segment_info, meta):
        if segment_info["category_id"] in meta[
                "thing_dataset_id_to_contiguous_id"]:
            segment_info["category_id"] = meta[
                "thing_dataset_id_to_contiguous_id"][
                    segment_info["category_id"]]
            segment_info["isthing"] = True
        else:
            segment_info["category_id"] = meta[
                "stuff_dataset_id_to_contiguous_id"][
                    segment_info["category_id"]]
            segment_info["isthing"] = False
        return segment_info

    with PathManager.open(json_file) as f:
        json_info = json.load(f)

    ret = []
    for ann in json_info["annotations"]:
        image_id = int(ann["image_id"])
        # TODO: currently we assume image and label has the same filename but
        # different extension, and images have extension ".jpg" for COCO. Need
        # to make image extension a user-provided argument if we extend this
        # function to support other COCO-like datasets.
        image_file = os.path.join(
            image_dir,
            os.path.splitext(ann["file_name"])[0] + ".jpg")
        assert PathManager.isfile(image_file), image_file
        label_file = os.path.join(gt_dir, ann["file_name"])
        assert PathManager.isfile(label_file), label_file
        segments_info = [
            _convert_category_id(x, meta) for x in ann["segments_info"]
        ]
        ret.append({
            "file_name": image_file,
            "image_id": image_id,
            "pan_seg_file_name": label_file,
            "segments_info": segments_info,
        })
    assert len(ret), f"No images found in {image_dir}!"
    assert PathManager.isfile(
        ret[0]["pan_seg_file_name"]), ret[0]["pan_seg_file_name"]
    return ret
Beispiel #4
0
 def setUp(self):
     json_file = MetadataCatalog.get("coco_2017_val_100").json_file
     if not PathManager.isfile(json_file):
         raise unittest.SkipTest("{} not found".format(json_file))
     with contextlib.redirect_stdout(io.StringIO()):
         json_file = PathManager.get_local_path(json_file)
         self.coco = COCO(json_file)
Beispiel #5
0
def _get_cityscapes_files(image_dir, gt_dir):
    files = []
    # scan through the directory
    cities = PathManager.ls(image_dir)
    logger.info(f"{len(cities)} cities found in '{image_dir}'.")
    for city in cities:
        city_img_dir = os.path.join(image_dir, city)
        city_gt_dir = os.path.join(gt_dir, city)
        for basename in PathManager.ls(city_img_dir):
            image_file = os.path.join(city_img_dir, basename)

            suffix = "leftImg8bit.png"
            assert basename.endswith(suffix), basename
            basename = basename[:-len(suffix)]

            instance_file = os.path.join(city_gt_dir,
                                         basename + "gtFine_instanceIds.png")
            label_file = os.path.join(city_gt_dir,
                                      basename + "gtFine_labelIds.png")
            json_file = os.path.join(city_gt_dir,
                                     basename + "gtFine_polygons.json")

            files.append((image_file, instance_file, label_file, json_file))
    assert len(files), "No images found in {}".format(image_dir)
    for f in files[0]:
        assert PathManager.isfile(f), f
    return files
Beispiel #6
0
def load_cityscapes_semantic(image_dir, gt_dir):
    """
    Args:
        image_dir (str): path to the raw dataset. e.g., "~/cityscapes/leftImg8bit/train".
        gt_dir (str): path to the raw annotations. e.g., "~/cityscapes/gtFine/train".

    Returns:
        list[dict]: a list of dict, each has "file_name" and
            "sem_seg_file_name".
    """
    ret = []
    # gt_dir is small and contain many small files. make sense to fetch to local first
    gt_dir = PathManager.get_local_path(gt_dir)
    for image_file, _, label_file, json_file in _get_cityscapes_files(
            image_dir, gt_dir):
        label_file = label_file.replace("labelIds", "labelTrainIds")

        with PathManager.open(json_file, "r") as f:
            jsonobj = json.load(f)
        ret.append({
            "file_name": image_file,
            "sem_seg_file_name": label_file,
            "height": jsonobj["imgHeight"],
            "width": jsonobj["imgWidth"],
        })
    assert len(ret), f"No images found in {image_dir}!"
    assert PathManager.isfile(
        ret[0]["sem_seg_file_name"]
    ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py"  # noqa
    return ret
Beispiel #7
0
    def merge_from_file(self,
                        cfg_filename: str,
                        allow_unsafe: bool = True) -> None:
        """
        Load content from the given config file and merge it into self.

        Args:
            cfg_filename: config filename
            allow_unsafe: allow unsafe yaml syntax
        """
        assert PathManager.isfile(
            cfg_filename), f"Config file '{cfg_filename}' does not exist!"
        loaded_cfg = self.load_yaml_with_base(cfg_filename,
                                              allow_unsafe=allow_unsafe)
        loaded_cfg = type(self)(loaded_cfg)

        # defaults.py needs to import CfgNode
        from .defaults import _C

        latest_ver = _C.VERSION
        assert (
            latest_ver == self.VERSION
        ), "CfgNode.merge_from_file is only allowed on a config object of latest version!"

        logger = logging.getLogger(__name__)

        loaded_ver = loaded_cfg.get("VERSION", None)
        if loaded_ver is None:
            from .compat import guess_version

            loaded_ver = guess_version(loaded_cfg, cfg_filename)
        assert loaded_ver <= self.VERSION, "Cannot merge a v{} config into a v{} config.".format(
            loaded_ver, self.VERSION)

        if loaded_ver == self.VERSION:
            self.merge_from_other_cfg(loaded_cfg)
        else:
            # compat.py needs to import CfgNode
            from .compat import upgrade_config, downgrade_config

            logger.warning(
                "Loading an old v{} config file '{}' by automatically upgrading to v{}. "
                "See docs/CHANGELOG.md for instructions to update your files.".
                format(loaded_ver, cfg_filename, self.VERSION))
            # To convert, first obtain a full config at an old version
            old_self = downgrade_config(self, to_version=loaded_ver)
            old_self.merge_from_other_cfg(loaded_cfg)
            new_config = upgrade_config(old_self)
            self.clear()
            self.update(new_config)
Beispiel #8
0
 def find_relative_file(original_file, relative_import_path, level):
     cur_file = os.path.dirname(original_file)
     for _ in range(level - 1):
         cur_file = os.path.dirname(cur_file)
     cur_name = relative_import_path.lstrip(".")
     for part in cur_name.split("."):
         cur_file = os.path.join(cur_file, part)
     # NOTE: directory import is not handled. Because then it's unclear
     # if such import should produce python module or DictConfig. This can
     # be discussed further if needed.
     if not cur_file.endswith(".py"):
         cur_file += ".py"
     if not PathManager.isfile(cur_file):
         raise ImportError(
             f"Cannot import name {relative_import_path} from "
             f"{original_file}: {cur_file} has to exist.")
     return cur_file
Beispiel #9
0
def inject_gan_datasets(cfg):
    if cfg.D2GO_DATA.DATASETS.GAN_INJECTION.ENABLE:
        name = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.NAME
        cfg.merge_from_list(
            ["DATASETS.TRAIN", [name], "DATASETS.TEST", [name]])

        json_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.JSON_PATH
        assert PathManager.isfile(json_path), (
            "{} is not valid!".format(json_path))

        input_src_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.INPUT_SRC_DIR
        assert PathManager.isfile(input_src_path), (
            "{} is not valid!".format(input_src_path))
        input_folder = "/tmp/{}/input".format(name)

        gt_src_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.GT_SRC_DIR
        if PathManager.isfile(gt_src_path):
            gt_folder = "/tmp/{}/gt".format(name)
        else:
            gt_src_path = None
            gt_folder = None

        mask_src_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.MASK_SRC_DIR
        if PathManager.isfile(mask_src_path):
            mask_folder = "/tmp/{}/mask".format(name)
        else:
            mask_src_path = None
            mask_folder = None

        real_src_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.REAL_SRC_DIR
        if PathManager.isfile(real_src_path):
            real_folder = "/tmp/{}/mask".format(name)
            real_json_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.REAL_JSON_PATH
            assert PathManager.isfile(real_json_path), (
                "{} is not valid!".format(real_json_path))
        else:
            real_src_path = None
            real_folder = None
            real_json_path = None

        register_folder_dataset(
            name,
            json_path,
            input_folder,
            gt_folder,
            mask_folder,
            input_src_path,
            gt_src_path,
            mask_src_path,
            real_json_path,
            real_folder,
            real_src_path,
        )
Beispiel #10
0
def _get_kitti2cityscapes_files(image_dir, gt_dir, istest):
    files = []
    # scan through the directory
    cities = PathManager.ls(image_dir)
    logger.info(f"{len(cities)} cities found in '{image_dir}'.")
    for city in cities:
        city_img_dir = os.path.join(image_dir, city)
        city_gt_dir = os.path.join(gt_dir, city)
        for basename in PathManager.ls(city_img_dir):
            image_file = os.path.join(city_img_dir, basename)

            suffix = "leftImg8bit.png"
            if basename.endswith(suffix) == basename:
                basename = basename[:-len(suffix)]

            if istest:
                instance_file = None
                json_file = None

                foldname = os.path.basename(city_img_dir)
                date = foldname[0:10]
                seq = foldname[0:26]

                # target_root = '/scratch1/zhusheng/kitti_eigen_inspred'
                # target_root = '/media/shengjie/c9c81c9f-511c-41c6-bfe0-2fc19666fb32/Data/kitti_eigen_inspred'
                # inspredpath = os.path.join(target_root, date, seq, 'insmap/image_02', basename)
                # semanpredpath = os.path.join(target_root, date, seq, 'semanmap/image_02', basename)
                # if os.path.exists(inspredpath) and os.path.exists(semanpredpath):
                #     continue
            else:
                instance_file = os.path.join(
                    city_gt_dir, basename + "gtFine_instanceIds.png")
                json_file = os.path.join(city_gt_dir,
                                         basename + "gtFine_polygons.json")

            files.append((image_file, instance_file, json_file))
    assert len(files), "No images found in {}".format(image_dir)
    for f in files[0]:
        if f is None:
            continue
        assert PathManager.isfile(f), f
    return files
Beispiel #11
0
def inject_gan_datasets(cfg):
    if cfg.D2GO_DATA.DATASETS.GAN_INJECTION.ENABLE:
        name = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.NAME
        cfg.merge_from_list([
            "DATASETS.TRAIN",
            list(cfg.DATASETS.TRAIN) + [name + "_train"], "DATASETS.TEST",
            list(cfg.DATASETS.TEST) + [name + "_test"]
        ])

        json_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.JSON_PATH
        assert PathManager.isfile(json_path), (
            "{} is not valid!".format(json_path))

        image_dir = Path(tempfile.mkdtemp())

        input_src_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.INPUT_SRC_DIR
        assert PathManager.isfile(input_src_path), (
            "{} is not valid!".format(input_src_path))
        input_folder = os.path.join(image_dir, name, "input")

        gt_src_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.GT_SRC_DIR
        if PathManager.isfile(gt_src_path):
            gt_folder = os.path.join(image_dir, name, "gt")
        else:
            gt_src_path = None
            gt_folder = None

        mask_src_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.MASK_SRC_DIR
        if PathManager.isfile(mask_src_path):
            mask_folder = os.path.join(image_dir, name, "mask")
        else:
            mask_src_path = None
            mask_folder = None

        real_src_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.REAL_SRC_DIR
        if PathManager.isfile(real_src_path):
            real_folder = os.path.join(image_dir, name, "real")
            real_json_path = cfg.D2GO_DATA.DATASETS.GAN_INJECTION.REAL_JSON_PATH
            assert PathManager.isfile(real_json_path), (
                "{} is not valid!".format(real_json_path))
        else:
            real_src_path = None
            real_folder = None
            real_json_path = None

        register_folder_dataset(
            name + "_train",
            json_path,
            input_folder,
            gt_folder,
            mask_folder,
            input_src_path,
            gt_src_path,
            mask_src_path,
            real_json_path,
            real_folder,
            real_src_path,
        )

        register_folder_dataset(
            name + "_test",
            json_path,
            input_folder,
            gt_folder,
            mask_folder,
            input_src_path,
            gt_src_path,
            mask_src_path,
            real_json_path,
            real_folder,
            real_src_path,
            max_num=5000,
        )