Esempio n. 1
0
def test_image_with_gt_and_disp_polygons(runs_dirpath, image_name, ori_image, ori_metadata, ori_gt_polygons, ori_disp_polygons,
                                         ori_disp_properties_list,
                                         batch_size, ds_fac_list, run_name_list, model_disp_max_abs_value, thresholds,
                                         test_output_dir, output_shapefiles=True):
    ori_gt_polygons = polygon_utils.polygons_remove_holes(ori_gt_polygons)  # TODO: Remove
    ori_gt_polygons = polygon_utils.simplify_polygons(ori_gt_polygons, tolerance=1)  # Remove redundant vertices
    ori_disp_polygons = polygon_utils.polygons_remove_holes(ori_disp_polygons)  # TODO: Remove
    ori_disp_polygons = polygon_utils.simplify_polygons(ori_disp_polygons, tolerance=1)  # Remove redundant vertices

    output_name = image_name
    test(runs_dirpath, ori_image, ori_metadata, ori_gt_polygons, ori_disp_polygons, batch_size, ds_fac_list, run_name_list,
         model_disp_max_abs_value, thresholds, test_output_dir, output_name, output_shapefiles=output_shapefiles,
         properties_list=ori_disp_properties_list)
Esempio n. 2
0
def test_detect_new_buildings(runs_dirpath, image_name, ori_image, ori_metadata, ori_gt_polygons, batch_size, ds_fac_list,
                              run_name_list, model_disp_max_abs_value, polygonization_params, thresholds,
                              test_output_dir, output_shapefiles=True):
    ori_gt_polygons = polygon_utils.polygons_remove_holes(ori_gt_polygons)  # TODO: Remove
    ori_gt_polygons = polygon_utils.simplify_polygons(ori_gt_polygons, tolerance=1)  # Remove redundant vertices
    ori_disp_polygons = []  # Don't input any polygons

    output_name = image_name
    seg_image_plot_filename_format = "{}.segmentation.png"
    ious_filename_format = "{}.iou.npy"
    new_polygons_filename_format = "{}.new_polygons.npy"
    aligned_new_polygons_filename_format = "{}.aligned_new_polygons.npy"
    polygons_image_plot_filename_format = "{}.polygons.png"

    shapefile_filename_format = "{}.{}_polygons.shp"
    accuracies_filename_format = "{}.accuracy.npy"

    # --- Get the segmentation output --- #
    seg_ds_fac_list = ds_fac_list[-1:]
    seg_run_name_list = run_name_list[-1:]
    print("# --- Run the model --- #")
    _, segmentation_image = multires_pipeline.multires_inference(runs_dirpath, ori_image, ori_metadata,
                                                                 ori_disp_polygons,
                                                                 model_disp_max_abs_value,
                                                                 batch_size, seg_ds_fac_list,
                                                                 seg_run_name_list)
    # segmentation_image = np.zeros((ori_image.shape[0], ori_image.shape[1], 4))
    print("# --- Save segmentation_output --- #")
    plot_segmentation_image_filename = seg_image_plot_filename_format.format(output_name)
    plot_segmentation_image_filepath = os.path.join(test_output_dir, plot_segmentation_image_filename)
    visualization.save_plot_segmentation_image(plot_segmentation_image_filepath, segmentation_image)

    seg_image = segmentation_image[:, :, 1:]  # Remove background channel

    # --- Measure IoUs --- #
    print("# --- Measure accuracies --- #")
    print(seg_image.min())
    print(seg_image.max())
    iou_thresholds = np.arange(0, 1.01, 0.01)
    ious_filename = ious_filename_format.format(output_name)
    ious_filepath = os.path.join(test_output_dir, ious_filename)
    ious = measure_ious(ori_gt_polygons, seg_image, iou_thresholds, ious_filepath)
    print("IoUs:")
    print(ious)

    # --- Polygonize segmentation --- #
    print("# --- Polygonize segmentation --- #")

    # TODO: remove:
    # seg_image_filepath = "test/bradbury_buildings.1_double.only_seg/SanFrancisco_01.disp_00.segmentation.png"
    # seg_image = image_utils.load_image(seg_image_filepath)
    # seg_image = seg_image / 255

    fill_threshold = polygonization_params["fill_threshold"]
    outline_threshold = polygonization_params["outline_threshold"]
    selem_width = polygonization_params["selem_width"]
    iterations = polygonization_params["iterations"]
def load_gt_polygons(image_filepath):
    gt_polygons = geo_utils.get_polygons_from_osm(image_filepath,
                                                  tag="building")
    if len(gt_polygons):
        gt_polygons = polygon_utils.polygons_remove_holes(
            gt_polygons)  # TODO: Remove

        # Remove redundant vertices
        gt_polygons = polygon_utils.simplify_polygons(gt_polygons, tolerance=1)

        return gt_polygons
    return None
Esempio n. 4
0
def test_image_with_gt_polygons_and_disp_maps(runs_dirpath,
                                              image_name,
                                              ori_image,
                                              ori_metadata,
                                              ori_gt_polygons,
                                              ori_normed_disp_field_maps,
                                              disp_max_abs_value,
                                              batch_size,
                                              ds_fac_list,
                                              run_name_list,
                                              model_disp_max_abs_value,
                                              thresholds,
                                              test_output_dir,
                                              output_shapefiles=True):
    output_name_format = "{}.disp_{:02d}"

    ori_gt_polygons = polygon_utils.polygons_remove_holes(
        ori_gt_polygons)  # TODO: Remove
    # Remove redundant vertices
    ori_gt_polygons = polygon_utils.simplify_polygons(ori_gt_polygons,
                                                      tolerance=1)

    disp_polygons_list = generate_disp_data(ori_normed_disp_field_maps,
                                            ori_gt_polygons,
                                            disp_max_abs_value)

    for i in range(len(disp_polygons_list)):
        print("# --- Testing with disp {:02d} --- #".format(i))
        disp_polygons = disp_polygons_list[i]
        output_name = output_name_format.format(image_name, i)
        test(runs_dirpath,
             ori_image,
             ori_metadata,
             ori_gt_polygons,
             disp_polygons,
             batch_size,
             ds_fac_list,
             run_name_list,
             model_disp_max_abs_value,
             thresholds,
             test_output_dir,
             output_name,
             output_shapefiles=output_shapefiles)
def process_image(dataset_raw_dirpath, image_info, patch_stride, patch_res, downsampling_factors, disp_max_abs_value,
                  include_polygons,
                  downsampling_factor_writers):
    """
    Writes to all the writers (one for each resolution) all sample patches extracted from the image_info.

    :param raw_dirpath:
    :param image_info:
    :param patch_stride:
    :param patch_res:
    :param downsampling_factors:
    :param disp_max_abs_value:
    :param include_polygons:
    :param downsampling_factor_writers:
    :return:
    """
    ori_image, ori_metadata, ori_gt_polygons = read.load_gt_data(dataset_raw_dirpath, image_info["city"],
                                                                 image_info["number"])

    if ori_gt_polygons is None:
        return False

    ori_gt_polygons = polygon_utils.polygons_remove_holes(ori_gt_polygons)  # TODO: Remove

    # Remove redundant vertices
    ori_gt_polygons = polygon_utils.simplify_polygons(ori_gt_polygons, tolerance=1)

    # visualization.init_figures(["gt_data"], figsize=(60, 40))
    # visualization.plot_example_polygons("gt_data", ori_image, ori_gt_polygons)

    # Create displacement maps
    ori_normed_disp_field_maps = math_utils.create_displacement_field_maps(ori_image.shape[:2], config.DISP_MAP_COUNT,
                                                                           config.DISP_MODES,
                                                                           config.DISP_GAUSS_MU_RANGE,
                                                                           config.DISP_GAUSS_SIG_SCALING)  # TODO: uncomment
    # ori_normed_disp_field_maps = np.zeros((config.DISP_MAP_COUNT, ori_image.shape[0], ori_image.shape[1], 2))  # TODO: remove

    # # TODO: remove
    # np.random.seed(seed=0)
    # colors = np.random.randint(0, 255, size=(len(downsampling_factors), 3), dtype=np.uint8)

    for index, downsampling_factor in enumerate(downsampling_factors):
        print("downsampling_factor: {}".format(downsampling_factor))
        # Downsample ground-truth
        image, gt_polygons, normed_disp_field_maps = downsample_gt_data(ori_image, ori_metadata, ori_gt_polygons,
                                                                        ori_normed_disp_field_maps, downsampling_factor)
        spatial_shape = image.shape[:2]

        # Random color
        # image = np.tile(colors[index], reps=[image.shape[0], image.shape[1], 1])  # TODO: remove

        # Draw gt polygon map
        gt_polygon_map = polygon_utils.draw_polygon_map(gt_polygons, spatial_shape, fill=True, edges=True,
                                                        vertices=True)

        # Generate final displacement
        disp_polygons_list, disp_polygon_maps = generate_disp_data(normed_disp_field_maps, gt_polygons,
                                                                   disp_max_abs_value, spatial_shape)

        # Compress data
        gt_polygons = [polygon.astype(np.float16) for polygon in gt_polygons]
        disp_polygons_list = [[polygon.astype(np.float16) for polygon in polygons] for polygons in disp_polygons_list]
        disp_field_maps = normed_disp_field_maps * 32767  # int16 max value = 32767
        disp_field_maps = np.round(disp_field_maps)
        disp_field_maps = disp_field_maps.astype(np.int16)

        # Cut sample into patches
        if include_polygons:
            patches = process_sample_into_patches(patch_stride, patch_res, image, gt_polygon_map,
                                                  disp_field_maps, disp_polygon_maps,
                                                  gt_polygons, disp_polygons_list)
        else:
            patches = process_sample_into_patches(patch_stride, patch_res, image, gt_polygon_map,
                                                  disp_field_maps, disp_polygon_maps)

        for patch in patches:
            save_patch_to_tfrecord(patch, downsampling_factor_writers[downsampling_factor])

    return True
Esempio n. 6
0
def process_image(reader, image_id, downsampling_factors, disp_field_maps_patch_creator, disp_max_abs_value,
                  include_polygons,
                  downsampling_factor_writers):
    """
    Writes to all the writers (one for each resolution) all sample patches extracted from the image_info.

    :param reader:
    :param image_id:
    :param downsampling_factors:
    :param disp_field_maps_patch_creator:
    :param disp_max_abs_value:
    :param include_polygons:
    :param downsampling_factor_writers:
    :return:
    """
    ori_image, ori_metadata, ori_gt_polygons = reader.load_gt_data(image_id)

    ori_gt_polygons = polygon_utils.polygons_remove_holes(ori_gt_polygons)  # TODO: Remove

    # Remove redundant vertices
    ori_gt_polygons = polygon_utils.simplify_polygons(ori_gt_polygons, tolerance=1)

    # visualization.init_figures(["gt_data"], figsize=(60, 40))
    # visualization.plot_example_polygons("gt_data", ori_image, ori_gt_polygons)

    # Get displacement maps
    ori_normed_disp_field_maps = disp_field_maps_patch_creator.get_patch()
    # ori_normed_disp_field_maps = np.zeros((config.DISP_MAP_COUNT, ori_image.shape[0], ori_image.shape[1], 2))  # TODO: remove

    # # TODO: remove
    # np.random.seed(seed=0)
    # colors = np.random.randint(0, 255, size=(len(downsampling_factors), 3), dtype=np.uint8)

    for index, downsampling_factor in enumerate(downsampling_factors):
        # print("downsampling_factor: {}".format(downsampling_factor))
        # Downsample ground-truth
        image, gt_polygons, normed_disp_field_maps = downsample_gt_data(ori_image, ori_metadata, ori_gt_polygons,
                                                                        ori_normed_disp_field_maps, downsampling_factor)

        spatial_shape = image.shape[:2]

        # Random color
        # image = np.tile(colors[index], reps=[image.shape[0], image.shape[1], 1])  # TODO: remove

        # Draw gt polygon map
        gt_polygon_map = polygon_utils.draw_polygon_map(gt_polygons, spatial_shape, fill=True, edges=True,
                                                        vertices=True)

        # Generate final displacement
        disp_polygons_list, disp_polygon_maps = generate_disp_data(normed_disp_field_maps, gt_polygons,
                                                                   disp_max_abs_value, spatial_shape)

        if gt_polygons[0][0][0] == np.nan or gt_polygons[0][0][1] == np.nan:
            print(gt_polygons[0][0])

        if disp_polygons_list[0][0][0][0] == np.nan or disp_polygons_list[0][0][0][1] == np.nan:
            print("disp_polygons_list:")
            print(disp_polygons_list[0][0])

        # Compress data
        gt_polygons = [polygon.astype(np.float16) for polygon in gt_polygons]
        disp_polygons_list = [[polygon.astype(np.float16) for polygon in polygons] for polygons in disp_polygons_list]
        disp_field_maps = normed_disp_field_maps * 32767  # int16 max value = 32767
        disp_field_maps = np.round(disp_field_maps)
        disp_field_maps = disp_field_maps.astype(np.int16)

        if include_polygons:
            gt_polygons, \
            disp_polygons_array = polygon_utils.prepare_polygons_for_tfrecord(gt_polygons, disp_polygons_list)
        else:
            gt_polygons = disp_polygons_array = None

        assert image.shape[0] == image.shape[1], "image should be square otherwise tile_res cannot be defined"
        tile_res = image.shape[0]
        disp_map_count = disp_polygon_maps.shape[0]

        patch = {
            "tile_res": tile_res,
            "disp_map_count": disp_map_count,
            "image": image,
            "gt_polygons": gt_polygons,
            "disp_polygons": disp_polygons_array,
            "gt_polygon_map": gt_polygon_map,
            "disp_field_maps": disp_field_maps,
            "disp_polygon_maps": disp_polygon_maps,
        }

        save_patch_to_tfrecord(patch, downsampling_factor_writers[downsampling_factor])

    return True