Skip to content

Commit e0f064d

Browse files
committed
Snapshot extension is now ".ingp" and is zip compressed
1 parent ce5548e commit e0f064d

File tree

10 files changed

+77
-24
lines changed

10 files changed

+77
-24
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/*.dll
1313
/*.exe
1414
/*.json
15+
*.ingp
1516
*.msgpack
1617
*.training
1718
__pycache__

.gitmodules

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@
2222
[submodule "dependencies/dlss"]
2323
path = dependencies/dlss
2424
url = https://github.com/NVIDIA/DLSS
25+
[submodule "dependencies/zstr"]
26+
path = dependencies/zstr
27+
url = https://github.com/Tom94/zstr
28+
[submodule "dependencies/zlib"]
29+
path = dependencies/zlib
30+
url = https://github.com/Tom94/zlib

CMakeLists.txt

+20
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,26 @@ if (Python_FOUND)
193193
add_subdirectory("dependencies/pybind11")
194194
endif()
195195

196+
# Compile zlib (only on Windows)
197+
if (WIN32)
198+
set(ZLIB_USE_STATIC_LIBS ON CACHE BOOL " " FORCE)
199+
set(ZLIB_BUILD_STATIC_LIBS ON CACHE BOOL " " FORCE)
200+
set(ZLIB_BUILD_SHARED_LIBS OFF CACHE BOOL " " FORCE)
201+
set(SKIP_INSTALL_ALL ON CACHE BOOL " " FORCE)
202+
add_subdirectory("dependencies/zlib")
203+
set_property(TARGET zlibstatic PROPERTY FOLDER "dependencies")
204+
205+
set(ZLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dependencies/zlib" CACHE PATH " " FORCE)
206+
set(ZLIB_LIBRARY zlibstatic)
207+
208+
include_directories(${ZLIB_INCLUDE_DIR} "${CMAKE_CURRENT_BINARY_DIR}/dependencies/zlib")
209+
210+
list(APPEND NGP_LIBRARIES zlibstatic)
211+
endif()
212+
213+
add_subdirectory("dependencies/zstr")
214+
list(APPEND NGP_LIBRARIES zstr::zstr)
215+
196216

197217
###############################################################################
198218
# Program

dependencies/zlib

Submodule zlib added at fafd714

dependencies/zstr

Submodule zstr added at 007f976

include/neural-graphics-primitives/testbed.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ class Testbed {
412412
void set_fov(float val) ;
413413
Eigen::Vector2f fov_xy() const ;
414414
void set_fov_xy(const Eigen::Vector2f& val);
415-
void save_snapshot(const std::string& filepath_string, bool include_optimizer_state);
415+
void save_snapshot(const std::string& filepath_string, bool include_optimizer_state, bool compress);
416416
void load_snapshot(const std::string& filepath_string);
417417
CameraKeyframe copy_camera_to_keyframe() const;
418418
void set_camera_from_keyframe(const CameraKeyframe& k);
@@ -464,6 +464,7 @@ class Testbed {
464464
bool m_gather_histograms = false;
465465

466466
bool m_include_optimizer_state_in_snapshot = false;
467+
bool m_compress_snapshot = true;
467468
bool m_render_ground_truth = false;
468469
EGroundTruthRenderMode m_ground_truth_render_mode = EGroundTruthRenderMode::Shade;
469470
float m_ground_truth_alpha = 1.0f;
@@ -831,7 +832,7 @@ class Testbed {
831832
char cam_path_path[MAX_PATH_LEN] = "cam.json";
832833
char extrinsics_path[MAX_PATH_LEN] = "extrinsics.json";
833834
char mesh_path[MAX_PATH_LEN] = "base.obj";
834-
char snapshot_path[MAX_PATH_LEN] = "base.msgpack";
835+
char snapshot_path[MAX_PATH_LEN] = "base.ingp";
835836
char video_path[MAX_PATH_LEN] = "video.mp4";
836837
} m_imgui;
837838

notebooks/instant_ngp.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -5846,7 +5846,7 @@
58465846
],
58475847
"source": [
58485848
"train_steps = 2000 #@param {type:\"integer\"}\n",
5849-
"snapshot_path = os.path.join(scene_path, f\"{train_steps}.msgpack\")\n",
5849+
"snapshot_path = os.path.join(scene_path, f\"{train_steps}.ingp\")\n",
58505850
"!python ./scripts/run.py {scene_path} --n_steps {train_steps} --save_snapshot {snapshot_path}"
58515851
]
58525852
},
@@ -5863,7 +5863,7 @@
58635863
"\n",
58645864
"Example command:\n",
58655865
"```\n",
5866-
"./build/instant-ngp /data/nerf/fox/2000.msgpack\n",
5866+
"./build/instant-ngp /data/nerf/fox/2000.ingp\n",
58675867
"```\n",
58685868
"\n",
58695869
"After you're done, **upload `base_cam.json` to the root folder of your scene.**"

scripts/run.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ def parse_args():
3333
parser.add_argument("--mode", default="", type=str, help=argparse.SUPPRESS) # deprecated
3434
parser.add_argument("--network", default="", help="Path to the network config. Uses the scene's default if unspecified.")
3535

36-
parser.add_argument("--load_snapshot", "--snapshot", default="", help="Load this snapshot before training. recommended extension: .msgpack")
37-
parser.add_argument("--save_snapshot", default="", help="Save this snapshot after training. recommended extension: .msgpack")
36+
parser.add_argument("--load_snapshot", "--snapshot", default="", help="Load this snapshot before training. recommended extension: .ingp/.msgpack")
37+
parser.add_argument("--save_snapshot", default="", help="Save this snapshot after training. recommended extension: .ingp/.msgpack")
3838

39-
parser.add_argument("--nerf_compatibility", action="store_true", help="Matches parameters with original NeRF. Can cause slowness and worse results on some scenes.")
39+
parser.add_argument("--nerf_compatibility", action="store_true", help="Matches parameters with original NeRF. Can cause slowness and worse results on some scenes, but helps with high PSNR on synthetic scenes.")
4040
parser.add_argument("--test_transforms", default="", help="Path to a nerf style transforms json from which we will compute PSNR.")
4141
parser.add_argument("--near_distance", default=-1, type=float, help="Set the distance from the camera at which training rays start for nerf. <0 means use ngp default")
4242
parser.add_argument("--exposure", default=0.0, type=float, help="Controls the brightness of the image. Positive numbers increase brightness, negative numbers decrease it.")

scripts/scenes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def setup_colored_sdf(testbed, scene, softshadow=True):
222222
testbed.scale = testbed.scale * 1.13
223223

224224
def default_snapshot_filename(scene_info):
225-
filename = "base.msgpack"
225+
filename = f"base.ingp"
226226
if scene_info["dataset"]:
227227
filename = f"{os.path.splitext(scene_info['dataset'])[0]}_{filename}"
228228
return os.path.join(scene_info["data_dir"], filename)

src/testbed.cu

+39-16
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#include <stb_image/stb_image.h>
4242
#include <stb_image/stb_image_write.h>
4343

44+
#include <zstr.hpp>
45+
4446
#include <fstream>
4547
#include <set>
4648
#include <unordered_set>
@@ -86,7 +88,7 @@ NGP_NAMESPACE_BEGIN
8688

8789
std::atomic<size_t> g_total_n_bytes_allocated{0};
8890

89-
json merge_parent_network_config(const json &child, const fs::path &child_filename) {
91+
json merge_parent_network_config(const json& child, const fs::path& child_filename) {
9092
if (!child.contains("parent")) {
9193
return child;
9294
}
@@ -121,7 +123,7 @@ void Testbed::update_imgui_paths() {
121123
snprintf(m_imgui.cam_path_path, sizeof(m_imgui.cam_path_path), "%s", get_filename_in_data_path_with_suffix(m_data_path, m_network_config_path, "_cam.json").c_str());
122124
snprintf(m_imgui.extrinsics_path, sizeof(m_imgui.extrinsics_path), "%s", get_filename_in_data_path_with_suffix(m_data_path, m_network_config_path, "_extrinsics.json").c_str());
123125
snprintf(m_imgui.mesh_path, sizeof(m_imgui.mesh_path), "%s", get_filename_in_data_path_with_suffix(m_data_path, m_network_config_path, ".obj").c_str());
124-
snprintf(m_imgui.snapshot_path, sizeof(m_imgui.snapshot_path), "%s", get_filename_in_data_path_with_suffix(m_data_path, m_network_config_path, ".msgpack").c_str());
126+
snprintf(m_imgui.snapshot_path, sizeof(m_imgui.snapshot_path), "%s", get_filename_in_data_path_with_suffix(m_data_path, m_network_config_path, ".ingp").c_str());
125127
snprintf(m_imgui.video_path, sizeof(m_imgui.video_path), "%s", get_filename_in_data_path_with_suffix(m_data_path, m_network_config_path, "_video.mp4").c_str());
126128
}
127129

@@ -215,22 +217,28 @@ fs::path Testbed::find_network_config(const fs::path& network_config_path) {
215217
}
216218

217219
json Testbed::load_network_config(const fs::path& network_config_path) {
218-
bool is_snapshot = equals_case_insensitive(network_config_path.extension(), "msgpack");
220+
bool is_snapshot = equals_case_insensitive(network_config_path.extension(), "msgpack") || equals_case_insensitive(network_config_path.extension(), "ingp");
219221
if (network_config_path.empty() || !network_config_path.exists()) {
220222
throw std::runtime_error{fmt::format("Network {} '{}' does not exist.", is_snapshot ? "snapshot" : "config", network_config_path.str())};
221223
}
222224

223225
tlog::info() << "Loading network " << (is_snapshot ? "snapshot" : "config") << " from: " << network_config_path;
224226

225227
json result;
226-
if (equals_case_insensitive(network_config_path.extension(), "json")) {
228+
if (is_snapshot) {
229+
if (equals_case_insensitive(network_config_path.extension(), "ingp")) {
230+
// zstr::ifstream applies zlib compression.
231+
zstr::ifstream f{network_config_path.str(), std::ios::in | std::ios::binary};
232+
result = json::from_msgpack(f);
233+
} else {
234+
std::ifstream f{network_config_path.str(), std::ios::in | std::ios::binary};
235+
result = json::from_msgpack(f);
236+
}
237+
// we assume parent pointers are already resolved in snapshots.
238+
} else if (equals_case_insensitive(network_config_path.extension(), "json")) {
227239
std::ifstream f{network_config_path.str()};
228240
result = json::parse(f, nullptr, true, true);
229241
result = merge_parent_network_config(result, network_config_path);
230-
} else if (equals_case_insensitive(network_config_path.extension(), "msgpack")) {
231-
std::ifstream f{network_config_path.str(), std::ios::in | std::ios::binary};
232-
result = json::from_msgpack(f);
233-
// we assume parent pointers are already resolved in snapshots.
234242
}
235243

236244
return result;
@@ -289,7 +297,7 @@ void Testbed::load_file(const std::string& file_path) {
289297
return;
290298
}
291299

292-
if (ends_with_case_insensitive(file_path, ".msgpack")) {
300+
if (ends_with_case_insensitive(file_path, ".ingp") || ends_with_case_insensitive(file_path, ".msgpack")) {
293301
load_snapshot(file_path);
294302
return;
295303
}
@@ -1218,7 +1226,7 @@ void Testbed::imgui() {
12181226
ImGui::Text("Snapshot");
12191227
ImGui::SameLine();
12201228
if (ImGui::Button("Save")) {
1221-
save_snapshot(m_imgui.snapshot_path, m_include_optimizer_state_in_snapshot);
1229+
save_snapshot(m_imgui.snapshot_path, m_include_optimizer_state_in_snapshot, m_compress_snapshot);
12221230
}
12231231
ImGui::SameLine();
12241232
if (ImGui::Button("Load")) {
@@ -1237,6 +1245,16 @@ void Testbed::imgui() {
12371245
ImGui::SameLine();
12381246
ImGui::Checkbox("w/ optimizer state", &m_include_optimizer_state_in_snapshot);
12391247
ImGui::InputText("File##Snapshot file path", m_imgui.snapshot_path, sizeof(m_imgui.snapshot_path));
1248+
ImGui::SameLine();
1249+
1250+
bool can_compress = ends_with_case_insensitive(m_imgui.snapshot_path, ".ingp");
1251+
1252+
if (!can_compress) {
1253+
ImGui::BeginDisabled();
1254+
m_compress_snapshot = false;
1255+
}
1256+
ImGui::Checkbox("Compress", &m_compress_snapshot);
1257+
if (!can_compress) ImGui::EndDisabled();
12401258
}
12411259

12421260
if (m_testbed_mode == ETestbedMode::Nerf || m_testbed_mode == ETestbedMode::Sdf) {
@@ -3481,7 +3499,7 @@ void Testbed::gather_histograms() {
34813499
// Increment this number when making a change to the snapshot format
34823500
static const size_t SNAPSHOT_FORMAT_VERSION = 1;
34833501

3484-
void Testbed::save_snapshot(const std::string& filepath_string, bool include_optimizer_state) {
3502+
void Testbed::save_snapshot(const std::string& filepath_string, bool include_optimizer_state, bool compress) {
34853503
fs::path filepath = filepath_string;
34863504
m_network_config["snapshot"] = m_trainer->serialize(include_optimizer_state);
34873505

@@ -3516,8 +3534,16 @@ void Testbed::save_snapshot(const std::string& filepath_string, bool include_opt
35163534
}
35173535

35183536
m_network_config_path = filepath;
3519-
std::ofstream f(m_network_config_path.str(), std::ios::out | std::ios::binary);
3520-
json::to_msgpack(m_network_config, f);
3537+
if (equals_case_insensitive(m_network_config_path.extension(), "ingp")) {
3538+
// zstr::ofstream applies zlib compression.
3539+
zstr::ofstream f{m_network_config_path.str(), std::ios::out | std::ios::binary, compress ? Z_DEFAULT_COMPRESSION : Z_NO_COMPRESSION};
3540+
json::to_msgpack(m_network_config, f);
3541+
} else {
3542+
std::ofstream f{m_network_config_path.str(), std::ios::out | std::ios::binary};
3543+
json::to_msgpack(m_network_config, f);
3544+
}
3545+
3546+
tlog::success() << "Saved snapshot '" << filepath_string << "'";
35213547
}
35223548

35233549
void Testbed::load_snapshot(const std::string& filepath_string) {
@@ -3526,10 +3552,7 @@ void Testbed::load_snapshot(const std::string& filepath_string) {
35263552
throw std::runtime_error{fmt::format("File '{}' does not contain a snapshot.", filepath_string)};
35273553
}
35283554

3529-
m_network_config_path = filepath_string;
3530-
35313555
const auto& snapshot = config["snapshot"];
3532-
35333556
if (snapshot.value("version", 0) < SNAPSHOT_FORMAT_VERSION) {
35343557
throw std::runtime_error{"Snapshot uses an old format and can not be loaded."};
35353558
}

0 commit comments

Comments
 (0)