MoGe / visualize_gt_only.py
zeyuren2002's picture
Add files using upload-large-folder tool
da3ed5b verified
"""
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()