Skip to content

Commit 6656214

Browse files
authored
Merge pull request #1578 from franz96521/save-raw-volumes-from-python-terminal
save raw volumes python terminal
2 parents cc74914 + ceff863 commit 6656214

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

Diff for: include/neural-graphics-primitives/testbed.h

+1
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ class Testbed {
485485
vec2 fov_xy() const ;
486486
void set_fov_xy(const vec2& val);
487487
void save_snapshot(const fs::path& path, bool include_optimizer_state, bool compress);
488+
void save_raw_volumes(const fs::path &filename, int res, BoundingBox aabb, bool flip_y_and_z_axes);
488489
void load_snapshot(nlohmann::json config);
489490
void load_snapshot(const fs::path& path);
490491
void load_snapshot(std::istream& stream, bool is_compressed = true);

Diff for: src/python_api.cu

+1
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ PYBIND11_MODULE(pyngp, m) {
443443
.def("n_params", &Testbed::n_params, "Number of trainable parameters")
444444
.def("n_encoding_params", &Testbed::n_encoding_params, "Number of trainable parameters in the encoding")
445445
.def("save_snapshot", &Testbed::save_snapshot, py::arg("path"), py::arg("include_optimizer_state")=false, py::arg("compress")=true, "Save a snapshot of the currently trained model. Optionally compressed (only when saving '.ingp' files).")
446+
.def("save_raw_volumes", &Testbed::save_raw_volumes)
446447
.def("load_snapshot", py::overload_cast<const fs::path&>(&Testbed::load_snapshot), py::arg("path"), "Load a previously saved snapshot")
447448
.def("load_camera_path", &Testbed::load_camera_path, py::arg("path"), "Load a camera path")
448449
.def("load_file", &Testbed::load_file, py::arg("path"), "Load a file and automatically determine how to handle it. Can be a snapshot, dataset, network config, or camera path.")

Diff for: src/testbed.cu

+36-18
Original file line numberDiff line numberDiff line change
@@ -1624,24 +1624,7 @@ void Testbed::imgui() {
16241624
save_rgba_grid_to_png_sequence(rgba, dir, res3d, flip_y_and_z_axes);
16251625
}
16261626
if (imgui_colored_button("Save raw volumes", 0.4f)) {
1627-
auto effective_view_dir = flip_y_and_z_axes ? vec3{0.0f, 1.0f, 0.0f} : vec3{0.0f, 0.0f, 1.0f};
1628-
auto old_local = m_render_aabb_to_local;
1629-
auto old_aabb = m_render_aabb;
1630-
m_render_aabb_to_local = mat3::identity();
1631-
auto dir = m_data_path.is_directory() || m_data_path.empty() ? (m_data_path / "volume_raw") : (m_data_path.parent_path() / fmt::format("{}_volume_raw", m_data_path.filename()));
1632-
if (!dir.exists()) {
1633-
fs::create_directory(dir);
1634-
}
1635-
1636-
for (int cascade = 0; (1<<cascade)<= m_aabb.diag().x+0.5f; ++cascade) {
1637-
float radius = (1<<cascade) * 0.5f;
1638-
m_render_aabb = BoundingBox(vec3(0.5f-radius), vec3(0.5f+radius));
1639-
// Dump raw density values that the user can then convert to alpha as they please.
1640-
GPUMemory<vec4> rgba = get_rgba_on_grid(res3d, effective_view_dir, true, 0.0f, true);
1641-
save_rgba_grid_to_raw_file(rgba, dir, res3d, flip_y_and_z_axes, cascade);
1642-
}
1643-
m_render_aabb_to_local = old_local;
1644-
m_render_aabb = old_aabb;
1627+
save_raw_volumes(m_data_path, m_mesh.res, {}, flip_y_and_z_axes);
16451628
}
16461629
}
16471630

@@ -4754,6 +4737,41 @@ void Testbed::save_snapshot(const fs::path& path, bool include_optimizer_state,
47544737
tlog::success() << "Saved snapshot '" << path.str() << "'";
47554738
}
47564739

4740+
void Testbed::save_raw_volumes(const fs::path &filename, int res, BoundingBox aabb, bool flip_y_and_z_axes)
4741+
{
4742+
auto effective_view_dir = flip_y_and_z_axes ? vec3{0.0f, 1.0f, 0.0f} : vec3{0.0f, 0.0f, 1.0f};
4743+
mat3 render_aabb_to_local = mat3(1.0f);
4744+
4745+
if (aabb.is_empty())
4746+
{
4747+
aabb = m_testbed_mode == ETestbedMode::Nerf ? m_render_aabb : m_aabb;
4748+
render_aabb_to_local = m_render_aabb_to_local;
4749+
}
4750+
4751+
if (m_testbed_mode != ETestbedMode::Nerf)
4752+
{
4753+
throw std::runtime_error{"Raw volume export is only supported for NeRF."};
4754+
}
4755+
4756+
auto res3d = get_marching_cubes_res(res, aabb);
4757+
4758+
std::string flipped = flip_y_and_z_axes ? "_flipedYZ" : "";
4759+
auto dir =( filename.is_directory() || filename.empty() ? (filename / fmt::format("volume_raw{}",flipped)) : (filename.parent_path() / fmt::format("{}_volume_raw{}", filename.filename(),flipped))) ;
4760+
if (!dir.exists())
4761+
{
4762+
fs::create_directory(dir);
4763+
}
4764+
4765+
for (int cascade = 0; (1 << cascade) <= m_aabb.diag().x + 0.5f; ++cascade)
4766+
{
4767+
float radius = (1 << cascade) * 0.5f;
4768+
m_render_aabb = BoundingBox(vec3(0.5f - radius), vec3(0.5f + radius));
4769+
// Dump raw density values that the user can then convert to alpha as they please.
4770+
GPUMemory<vec4> rgba = get_rgba_on_grid(res3d, effective_view_dir, true, 0.0f, true);
4771+
save_rgba_grid_to_raw_file(rgba, dir, res3d, flip_y_and_z_axes, cascade);
4772+
}
4773+
}
4774+
47574775
void Testbed::load_snapshot(nlohmann::json config) {
47584776
const auto& snapshot = config["snapshot"];
47594777
if (snapshot.value("version", 0) < SNAPSHOT_FORMAT_VERSION) {

0 commit comments

Comments
 (0)