Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid jpeg compression artifacts when training Mip-NeRF360. #507

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/benchmarks/compression/mcmc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ do
# train without eval
CUDA_VISIBLE_DEVICES=0 python simple_trainer.py mcmc --eval_steps -1 --disable_viewer --data_factor $DATA_FACTOR \
--strategy.cap-max $CAP_MAX \
--data_dir data/360_v2/$SCENE/ \
--data_dir $SCENE_DIR/$SCENE/ \
--result_dir $RESULT_DIR/$SCENE/

# eval: use vgg for lpips to align with other benchmarks
CUDA_VISIBLE_DEVICES=0 python simple_trainer.py mcmc --disable_viewer --data_factor $DATA_FACTOR \
--strategy.cap-max $CAP_MAX \
--data_dir data/360_v2/$SCENE/ \
--data_dir $SCENE_DIR/$SCENE/ \
--result_dir $RESULT_DIR/$SCENE/ \
--lpips_net vgg \
--compression png \
Expand All @@ -49,7 +49,7 @@ done
if command -v zip &> /dev/null
then
echo "Zipping results"
python benchmarks/compression/summarize_stats.py --results_dir $RESULT_DIR
python benchmarks/compression/summarize_stats.py --results_dir $RESULT_DIR --scenes $SCENE_LIST
else
echo "zip command not found, skipping zipping"
fi
2 changes: 1 addition & 1 deletion examples/benchmarks/compression/mcmc_tt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SCENE_LIST="train truck"
# CAP_MAX=360000

# # 0.49M GSs
# RESULT_DIR="results/benchmark_tt_mcmc_tt_0_49M_png_compression"
# RESULT_DIR="results/benchmark_tt_mcmc_0_49M_png_compression"
# CAP_MAX=490000

# 1M GSs
Expand Down
9 changes: 6 additions & 3 deletions examples/benchmarks/compression/summarize_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import tyro


def main(results_dir: str, scenes: List[str]):
def main(results_dir: str, scenes: List[str], stage: str = "compress"):
print("scenes:", scenes)
stage = "compress"

summary = defaultdict(list)
for scene in scenes:
Expand All @@ -33,7 +32,11 @@ def main(results_dir: str, scenes: List[str]):
summary[k].append(v)

for k, v in summary.items():
print(k, np.mean(v))
summary[k] = np.mean(v)
summary["scenes"] = scenes

with open(os.path.join(results_dir, f"{stage}_summary.json"), "w") as f:
json.dump(summary, f, indent=2)


if __name__ == "__main__":
Expand Down
35 changes: 33 additions & 2 deletions examples/datasets/colmap.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import os
import json
from tqdm import tqdm
from typing import Any, Dict, List, Optional
from typing_extensions import assert_never

import cv2
from PIL import Image
import imageio.v2 as imageio
import numpy as np
import torch
Expand All @@ -26,6 +28,31 @@ def _get_rel_paths(path_dir: str) -> List[str]:
return paths


def _resize_image_folder(image_dir: str, resized_dir: str, factor: int) -> str:
"""Resize image folder."""
print(f"Downscaling images by {factor}x from {image_dir} to {resized_dir}.")
os.makedirs(resized_dir, exist_ok=True)

image_files = _get_rel_paths(image_dir)
for image_file in tqdm(image_files):
image_path = os.path.join(image_dir, image_file)
resized_path = os.path.join(
resized_dir, os.path.splitext(image_file)[0] + ".png"
)
if os.path.isfile(resized_path):
continue
image = imageio.imread(image_path)[..., :3]
resized_size = (
int(round(image.shape[1] / factor)),
int(round(image.shape[0] / factor)),
)
resized_image = np.array(
Image.fromarray(image).resize(resized_size, Image.BICUBIC)
)
imageio.imwrite(resized_path, resized_image)
return resized_dir


class Parser:
"""COLMAP parser."""

Expand Down Expand Up @@ -163,6 +190,11 @@ def __init__(
# so we need to map between the two sorted lists of files.
colmap_files = sorted(_get_rel_paths(colmap_image_dir))
image_files = sorted(_get_rel_paths(image_dir))
if factor > 1 and os.path.splitext(image_files[0])[1].lower() == ".jpg":
image_dir = _resize_image_folder(
colmap_image_dir, image_dir + "_png", factor=factor
)
image_files = sorted(_get_rel_paths(image_dir))
colmap_to_image = dict(zip(colmap_files, image_files))
image_paths = [os.path.join(image_dir, colmap_to_image[f]) for f in image_names]

Expand Down Expand Up @@ -389,7 +421,6 @@ def __getitem__(self, item: int) -> Dict[str, Any]:
import argparse

import imageio.v2 as imageio
import tqdm

parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", type=str, default="data/360_v2/garden")
Expand All @@ -404,7 +435,7 @@ def __getitem__(self, item: int) -> Dict[str, Any]:
print(f"Dataset: {len(dataset)} images.")

writer = imageio.get_writer("results/points.mp4", fps=30)
for data in tqdm.tqdm(dataset, desc="Plotting points"):
for data in tqdm(dataset, desc="Plotting points"):
image = data["image"].numpy().astype(np.uint8)
points = data["points"].numpy()
depths = data["depths"].numpy()
Expand Down