File size: 6,288 Bytes
da3ed5b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | """
Visualize GT depth - both reverse and non-reverse versions
Must match exactly with visualize_depth.py sample selection
"""
import os
import sys
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Agg')
from tqdm import tqdm
# Dataset paths - MUST match visualize_depth.py
KITTI_BASE = '/home/ywan0794/datasets/eval/moge_style_eval/KITTI'
DDAD_BASE = '/home/ywan0794/datasets/eval/moge_style_eval/DDAD/val'
OUTPUT_DIR = '/home/ywan0794/MoGe/vis_output'
def colorize_depth(depth, cmap='Spectral', reverse=False, mask_invalid=False):
depth = depth.copy()
if mask_invalid:
invalid_mask = depth <= 0
valid_depth = depth[~invalid_mask]
if len(valid_depth) > 0:
vmin = np.percentile(valid_depth, 2)
vmax = np.percentile(valid_depth, 98)
else:
vmin, vmax = 0, 1
else:
vmin = np.percentile(depth, 2)
vmax = np.percentile(depth, 98)
depth = (depth - vmin) / (vmax - vmin + 1e-8)
depth = np.clip(depth, 0, 1)
if reverse:
depth = 1 - depth
cm = plt.get_cmap(cmap)
colored = cm(depth)[:, :, :3]
colored = (colored * 255).astype(np.uint8)
if mask_invalid:
colored[invalid_mask] = 0
return colored
def load_gt_depth(depth_path):
depth = np.array(Image.open(depth_path))
if depth.dtype == np.uint16:
depth = depth.astype(np.float32) / 256.0
elif depth.dtype == np.uint8:
depth = depth.astype(np.float32)
return depth
def main():
print("=" * 50, flush=True)
print("GT Depth Visualization (both versions)", flush=True)
print("=" * 50, flush=True)
# Create output directories
for dataset in ['KITTI', 'DDAD']:
os.makedirs(os.path.join(OUTPUT_DIR, dataset, 'gt'), exist_ok=True)
os.makedirs(os.path.join(OUTPUT_DIR, dataset, 'gt_reverse'), exist_ok=True)
# ============================================
# KITTI - MUST match visualize_depth.py exactly
# ============================================
print("\nCollecting KITTI samples...", flush=True)
kitti_samples = []
for drive in os.listdir(KITTI_BASE):
drive_path = os.path.join(KITTI_BASE, drive, 'image_02')
if os.path.isdir(drive_path):
for frame in sorted(os.listdir(drive_path)):
sample_dir = os.path.join(drive_path, frame)
img_path = os.path.join(sample_dir, 'image.jpg')
gt_path = os.path.join(sample_dir, 'depth.png')
# MUST check both img and gt exist - same as visualize_depth.py
if os.path.exists(img_path) and os.path.exists(gt_path):
kitti_samples.append({
'image': img_path,
'gt': gt_path,
'name': f"{drive}_{frame}"
})
# Same random seed and selection as visualize_depth.py
np.random.seed(42)
num_samples = 10 # Must match --num-samples in visualize_depth.py
kitti_selected = np.random.choice(len(kitti_samples), min(num_samples, len(kitti_samples)), replace=False)
print(f"Processing {len(kitti_selected)} KITTI GT samples...", flush=True)
for idx, i in tqdm(enumerate(kitti_selected), total=len(kitti_selected), desc="KITTI GT", file=sys.stdout):
sample = kitti_samples[i]
gt_depth = load_gt_depth(sample['gt'])
# Non-reverse version
gt_colored = colorize_depth(gt_depth, reverse=False, mask_invalid=True)
Image.fromarray(gt_colored).save(os.path.join(OUTPUT_DIR, 'KITTI', 'gt', f"{idx:03d}.png"))
# Reverse version
gt_colored_rev = colorize_depth(gt_depth, reverse=True, mask_invalid=True)
Image.fromarray(gt_colored_rev).save(os.path.join(OUTPUT_DIR, 'KITTI', 'gt_reverse', f"{idx:03d}.png"))
# ============================================
# DDAD - MUST match visualize_depth.py exactly
# ============================================
print("\nCollecting DDAD samples...", flush=True)
ddad_samples = []
for scene in sorted(os.listdir(DDAD_BASE)):
scene_path = os.path.join(DDAD_BASE, scene)
if os.path.isdir(scene_path):
for cam in sorted(os.listdir(scene_path)):
sample_dir = os.path.join(scene_path, cam)
img_path = os.path.join(sample_dir, 'image.jpg')
gt_path = os.path.join(sample_dir, 'depth.png')
# MUST check both img and gt exist - same as visualize_depth.py
if os.path.exists(img_path) and os.path.exists(gt_path):
ddad_samples.append({
'image': img_path,
'gt': gt_path,
'name': f"{scene}_{cam}"
})
# Same random seed and selection as visualize_depth.py
# Note: seed was already set to 42 above, and kitti_selected consumed some random numbers
# We need to match the exact sequence
ddad_selected = np.random.choice(len(ddad_samples), min(num_samples, len(ddad_samples)), replace=False)
print(f"Processing {len(ddad_selected)} DDAD GT samples...", flush=True)
for idx, i in tqdm(enumerate(ddad_selected), total=len(ddad_selected), desc="DDAD GT", file=sys.stdout):
sample = ddad_samples[i]
gt_depth = load_gt_depth(sample['gt'])
# Non-reverse version
gt_colored = colorize_depth(gt_depth, reverse=False, mask_invalid=True)
Image.fromarray(gt_colored).save(os.path.join(OUTPUT_DIR, 'DDAD', 'gt', f"{idx:03d}.png"))
# Reverse version
gt_colored_rev = colorize_depth(gt_depth, reverse=True, mask_invalid=True)
Image.fromarray(gt_colored_rev).save(os.path.join(OUTPUT_DIR, 'DDAD', 'gt_reverse', f"{idx:03d}.png"))
print("\n" + "=" * 50, flush=True)
print("Done!", flush=True)
print("Output:", flush=True)
print(f" {OUTPUT_DIR}/KITTI/gt/ (non-reverse)", flush=True)
print(f" {OUTPUT_DIR}/KITTI/gt_reverse/ (reverse)", flush=True)
print(f" {OUTPUT_DIR}/DDAD/gt/ (non-reverse)", flush=True)
print(f" {OUTPUT_DIR}/DDAD/gt_reverse/ (reverse)", flush=True)
print("=" * 50, flush=True)
if __name__ == '__main__':
main()
|