示例#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
示例#2
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
示例#3
0
def extract_archive_file(archive_fn: str, im_dir: str):
    if not PathManager.exists(im_dir) or not PathManager.ls(im_dir):
        # Dataset is not deployed. Deploy it.
        archive_fns = archive_fn
        # A dataset may be composed of several tgz files, or only one.
        # If one, make it into a list to make the code later more general
        if not isinstance(archive_fns, list):
            archive_fns = [archive_fns]
        logger.info("Extracting datasets {} to local machine at {}".format(
            archive_fns, im_dir))
        if not PathManager.exists(im_dir):
            PathManager.mkdirs(im_dir)

        for archive_fn in archive_fns:
            # Extract the tgz file directly into the target directory,
            # without precopy.
            # Note that the tgz file contains a root directory that
            # we do not want, hence the strip-components=1
            commandUnpack = ("tar -mxzf {src_file} -C {tgt_dir} "
                             "--strip-components=1").format(
                                 src_file=archive_fn, tgt_dir=im_dir)

            assert not subprocess.call(
                shlex.split(commandUnpack)), "Failed to unpack"
            logger.info("Extracted {}".format(archive_fn))
示例#4
0
 def _get_lightning_checkpoints(path: str):
     return [
         os.path.join(path, x)
         for x in PathManager.ls(path)
         if x.endswith(ModelCheckpoint.FILE_EXTENSION)
         and not x.startswith(ModelCheckpoint.CHECKPOINT_NAME_LAST)
     ]
示例#5
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
示例#6
0
 def get_path_pairs(img_folder, mask_folder):
     img_paths = []
     mask_paths = []
     print("Before listing", img_folder)
     filenames = PathManager.ls(img_folder)
     for filename in filenames:
         print("found: ", filename)
         basename, _ = os.path.splitext(filename)
         if filename.endswith(".jpg"):
             imgpath = os.path.join(img_folder, filename)
             maskname = basename + ".png"
             maskpath = os.path.join(mask_folder, maskname)
             img_paths.append(imgpath)
             mask_paths.append(maskpath)
             # if PathManager.isfile(maskpath):
             # else:
             #    print('cannot find the mask:', maskpath)
     return img_paths, mask_paths
示例#7
0
def load_sem_seg(gt_root, image_root, gt_ext="png", image_ext="jpg"):
    """
    Load semantic segmentation datasets. All files under "gt_root" with "gt_ext" extension are
    treated as ground truth annotations and all files under "image_root" with "image_ext" extension
    as input images. Ground truth and input images are matched using file paths relative to
    "gt_root" and "image_root" respectively without taking into account file extensions.
    This works for COCO as well as some other datasets.

    Args:
        gt_root (str): full path to ground truth semantic segmentation files. Semantic segmentation
            annotations are stored as images with integer values in pixels that represent
            corresponding semantic labels.
        image_root (str): the directory where the input images are.
        gt_ext (str): file extension for ground truth annotations.
        image_ext (str): file extension for input images.

    Returns:
        list[dict]:
            a list of dicts in detectron2 standard format without instance-level
            annotation.

    Notes:
        1. This function does not read the image and ground truth files.
           The results do not have the "image" and "sem_seg" fields.
    """

    # We match input images with ground truth based on their relative filepaths (without file
    # extensions) starting from 'image_root' and 'gt_root' respectively.
    def file2id(folder_path, file_path):
        # extract relative path starting from `folder_path`
        image_id = os.path.normpath(os.path.relpath(file_path, start=folder_path))
        # remove file extension
        image_id = os.path.splitext(image_id)[0]
        return image_id

    input_files = sorted(
        (os.path.join(image_root, f) for f in PathManager.ls(image_root) if f.endswith(image_ext)),
        key=lambda file_path: file2id(image_root, file_path),
    )
    gt_files = sorted(
        (os.path.join(gt_root, f) for f in PathManager.ls(gt_root) if f.endswith(gt_ext)),
        key=lambda file_path: file2id(gt_root, file_path),
    )

    assert len(gt_files) > 0, "No annotations found in {}.".format(gt_root)

    # Use the intersection, so that val2017_100 annotations can run smoothly with val2017 images
    if len(input_files) != len(gt_files):
        logger.warn(
            "Directory {} and {} has {} and {} files, respectively.".format(
                image_root, gt_root, len(input_files), len(gt_files)
            )
        )
        input_basenames = [os.path.basename(f)[: -len(image_ext)] for f in input_files]
        gt_basenames = [os.path.basename(f)[: -len(gt_ext)] for f in gt_files]
        intersect = list(set(input_basenames) & set(gt_basenames))
        # sort, otherwise each worker may obtain a list[dict] in different order
        intersect = sorted(intersect)
        logger.warn("Will use their intersection of {} files.".format(len(intersect)))
        input_files = [os.path.join(image_root, f + image_ext) for f in intersect]
        gt_files = [os.path.join(gt_root, f + gt_ext) for f in intersect]

    logger.info(
        "Loaded {} images with semantic segmentation from {}".format(len(input_files), image_root)
    )

    dataset_dicts = []
    for (img_path, gt_path) in zip(input_files, gt_files):
        record = {}
        record["file_name"] = img_path
        record["sem_seg_file_name"] = gt_path
        dataset_dicts.append(record)

    return dataset_dicts