def test_align_gt(runs_dirpath, ori_image, ori_metadata, ori_gt_polygons, batch_size, ds_fac_list, run_name_list, model_disp_max_abs_value, output_dir=None, output_name=None, output_shapefiles=True, properties_list=None): # --- Run the model --- # print("# --- Run the model --- #") aligned_gt_polygons, _ = multires_pipeline.multires_inference(runs_dirpath, ori_image, ori_metadata, ori_gt_polygons, model_disp_max_abs_value, batch_size, ds_fac_list, run_name_list) # --- Save polygons as shapefiles --- # if output_shapefiles: if output_dir is not None and output_name is not None: import geo_utils print("# --- Save polygons as shapefiles --- #") shapefile_filename_format = "{}.aligned_gt_polygons.shp" output_shapefile_filename = shapefile_filename_format.format(output_name, "ori") output_shapefile_filepath = os.path.join(output_dir, output_shapefile_filename) geo_utils.save_shapefile_from_polygons(aligned_gt_polygons, ori_metadata["filepath"], output_shapefile_filepath, properties_list=properties_list) else: print_utils.print_warning("Could not save shapefile as output_dir and/or output_name was not specified.") return aligned_gt_polygons
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 test(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=True, properties_list=None): if output_shapefiles: assert properties_list is not None and len(ori_disp_polygons) == len( properties_list ), "ori_disp_polygons and properties_list should have the same length" polygons_image_plot_filename_format = "{}.polygons.png" shapefile_filename_format = "{}.{}_polygons.shp" segmentation_image_plot_filename_format = "{}.segmentation.png" accuracies_filename_format = "{}.accuracy.npy" # --- Run the model --- # print("# --- Run the model --- #") aligned_disp_polygons, segmentation_image = multires_pipeline.multires_inference( ori_image, ori_metadata, ori_disp_polygons, model_disp_max_abs_value, batch_size, ds_fac_list, run_name_list) # aligned_disp_polygons = ori_disp_polygons # segmentation_image = np.zeros((ori_image.shape[0], ori_image.shape[1], 4)) # --- Save segmentation_output --- # print("# --- Save segmentation_output --- #") plot_segmentation_image_filename = segmentation_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) # --- Save polygons plot --- # plot_image_filename = polygons_image_plot_filename_format.format( output_name) plot_image_filepath = os.path.join(test_output_dir, plot_image_filename) visualization.save_plot_image_polygons(plot_image_filepath, ori_image, ori_gt_polygons, ori_disp_polygons, aligned_disp_polygons) # visualization.save_plot_image_polygons(plot_image_filepath, ori_image, [], ori_disp_polygons, # aligned_disp_polygons) # --- Save polygons as shapefiles --- # if output_shapefiles: print("# --- Save polygons as shapefiles --- #") output_shapefile_filename = shapefile_filename_format.format( output_name, "ori") output_shapefile_filepath = os.path.join(test_output_dir, output_shapefile_filename) geo_utils.save_shapefile_from_polygons(ori_gt_polygons, ori_metadata["filepath"], output_shapefile_filepath, properties_list=properties_list) output_shapefile_filename = shapefile_filename_format.format( output_name, "misaligned") output_shapefile_filepath = os.path.join(test_output_dir, output_shapefile_filename) geo_utils.save_shapefile_from_polygons(ori_disp_polygons, ori_metadata["filepath"], output_shapefile_filepath, properties_list=properties_list) output_shapefile_filename = shapefile_filename_format.format( output_name, "aligned") output_shapefile_filepath = os.path.join(test_output_dir, output_shapefile_filename) geo_utils.save_shapefile_from_polygons(aligned_disp_polygons, ori_metadata["filepath"], output_shapefile_filepath, properties_list=properties_list) # --- Measure accuracies --- # if len(ori_gt_polygons) == len(aligned_disp_polygons): print("# --- Measure accuracies --- #") accuracies_filename = accuracies_filename_format.format(output_name) accuracies_filepath = os.path.join(test_output_dir, accuracies_filename) accuracies = measure_accuracies(ori_gt_polygons, aligned_disp_polygons, thresholds, accuracies_filepath) print(accuracies)