def downsample_images(args, downsample=2): indir = Path(args.indir) outdir = Path(args.outdir) outdir.mkdir(exist_ok=True) rgb_paths = list(indir.glob(f"*_RGB.{args.rgb_suffix}")) if rgb_paths == []: rgb_paths = list( indir.glob(f"*_RGB*.{args.rgb_suffix}")) # original file names for rgb_path in tqdm(rgb_paths): # load agl_path = rgb_path.with_name(rgb_path.name.replace( "_RGB", "_AGL")).with_suffix(".tif") vflow_path = rgb_path.with_name(rgb_path.name.replace( "_RGB", "_VFLOW")).with_suffix(".json") rgb = load_image(rgb_path, args) # args.unit used to convert units on load agl = load_image(agl_path, args) # args.unit used to convert units on load _, _, _, vflow_data = load_vflow( vflow_path, agl, args) # arg.unit used to convert units on load # downsample target_shape = (int(rgb.shape[0] / downsample), int(rgb.shape[1] / downsample)) rgb = cv2.resize(rgb, target_shape) agl = cv2.resize(agl, target_shape, interpolation=cv2.INTER_NEAREST) vflow_data["scale"] /= downsample # save # units are NOT converted back here, so are in m # save_image((outdir / rgb_path.name), rgb) save_image((outdir / rgb_path.name.replace("j2k", "tif")), rgb) # save as tif to be consistent with old code save_image((outdir / agl_path.name), agl) with open((outdir / vflow_path.name), "w") as outfile: json.dump(vflow_data, outfile)
def get_r2_denoms(item, agl_gt_mean, vflow_gt_mean): ( vflow_gt_path, agl_gt_path, vflow_pred_path, aglpred_path, rgb_path, args, ) = item agl_gt = load_image(agl_gt_path, args) vflow_gt_items = load_vflow(vflow_gt_path, agl_gt, args, return_vflow_pred_mat=True) vflow_gt, mag_gt, xdir_gt, ydir_gt, vflow_gt_data = vflow_gt_items scale_gt, angle_gt = vflow_gt_data["scale"], vflow_gt_data["angle"] agl_denom = np.nansum(np.square(agl_gt - agl_gt_mean)) vflow_denom = np.nansum(np.square(vflow_gt - vflow_gt_mean)) items = (agl_denom, vflow_denom) return items
def get_current_metrics(item, args): # get arguments vflow_gt_path, agl_gt_path, vflow_pred_path, aglpred_path, rgb_path, args = item # load AGL, SCALE, and ANGLE predicted values agl_pred = load_image(aglpred_path, args) if agl_pred is None: return None vflow_items = load_vflow(vflow_pred_path, agl=agl_pred, args=args, return_vflow_pred_mat=True) if vflow_items is None: return None vflow_pred, mag_pred, xdir_pred, ydir_pred, vflow_data = vflow_items scale_pred, angle_pred = vflow_data["scale"], vflow_data["angle"] # load AGL, SCALE, and ANGLE ground truth values agl_gt = load_image(agl_gt_path, args) if agl_gt is None: return None vflow_gt_items = load_vflow(vflow_gt_path, agl_gt, args, return_vflow_pred_mat=True) if vflow_gt_items is None: return None vflow_gt, mag_gt, xdir_gt, ydir_gt, vflow_gt_data = vflow_gt_items scale_gt, angle_gt = vflow_gt_data["scale"], vflow_gt_data["angle"] # produce rectified images if args.rectify and args.output_dir is not None: rgb = load_image(rgb_path, args) output_rgb_path = os.path.join(args.output_dir, os.path.basename(rgb_path)) write_rectified_images(rgb, mag_pred, angle_pred, agl_pred, output_rgb_path) # compute differences dir_pred = np.array([xdir_pred, ydir_pred]) dir_pred /= np.linalg.norm(dir_pred) dir_gt = np.array([xdir_gt, ydir_gt]) dir_gt /= np.linalg.norm(dir_gt) cos_ang = np.dot(dir_pred, dir_gt) sin_ang = np.linalg.norm(np.cross(dir_pred, dir_gt)) rad_diff = np.arctan2(sin_ang, cos_ang) # get mean error values angle_error = np.degrees(rad_diff) scale_error = np.abs(scale_pred - scale_gt) mag_error = np.nanmean(np.abs(mag_pred - mag_gt)) epe = np.nanmean(np.sqrt(np.sum(np.square(vflow_gt - vflow_pred), axis=2))) agl_error = np.nanmean(np.abs(agl_pred - agl_gt)) # get RMS error values mag_rms = np.sqrt(np.nanmean(np.square(mag_pred - mag_gt))) epe_rms = np.sqrt( np.nanmean(np.sum(np.square(vflow_gt - vflow_pred), axis=2))) agl_rms = np.sqrt(np.nanmean(np.square(agl_pred - agl_gt))) # gather data for computing R-square for AGL agl_count = np.sum(np.isfinite(agl_gt)) agl_sse = np.nansum(np.square(agl_pred - agl_gt)) agl_gt_sum = np.nansum(agl_gt) # gather data for computing R-square for VFLOW vflow_count = np.sum(np.isfinite(vflow_gt)) vflow_gt_sum = np.nansum(vflow_gt) vflow_sse = np.nansum(np.square(vflow_pred - vflow_gt)) items = ( angle_error, scale_error, mag_error, epe, agl_error, mag_rms, epe_rms, agl_rms, agl_count, agl_sse, agl_gt_sum, vflow_count, vflow_sse, vflow_gt_sum, ) return items