From 0f06a149c4fb9406fd3e432a5cb0c024f38e2f0e Mon Sep 17 00:00:00 2001 From: Sameer Sheorey <41028320+ssheorey@users.noreply.github.com> Date: Thu, 4 Jan 2024 16:41:54 -0800 Subject: [PATCH] Fix tensorboard examples with MonkeyModel path (#6569) --- .../visualization/tensorboard_pytorch.py | 32 +++++++++---------- .../visualization/tensorboard_tensorflow.py | 29 ++++++++--------- 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/examples/python/visualization/tensorboard_pytorch.py b/examples/python/visualization/tensorboard_pytorch.py index 44541f2a0f4..89a9fb7c17c 100644 --- a/examples/python/visualization/tensorboard_pytorch.py +++ b/examples/python/visualization/tensorboard_pytorch.py @@ -5,24 +5,23 @@ # SPDX-License-Identifier: MIT # ---------------------------------------------------------------------------- import copy -import os +from os.path import exists, join, dirname, basename, splitext import sys import numpy as np import open3d as o3d # pylint: disable-next=unused-import +from open3d.visualization.tensorboard_plugin import summary # noqa from open3d.visualization.tensorboard_plugin.util import to_dict_batch from torch.utils.tensorboard import SummaryWriter BASE_LOGDIR = "demo_logs/pytorch/" -MODEL_DIR = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname( - os.path.realpath(__file__)))), "test_data", "monkey") +MODEL_PATH = o3d.data.MonkeyModel().path def small_scale(run_name="small_scale"): """Basic demo with cube and cylinder with normals and colors. """ - logdir = os.path.join(BASE_LOGDIR, run_name) + logdir = join(BASE_LOGDIR, run_name) writer = SummaryWriter(logdir) cube = o3d.geometry.TriangleMesh.create_box(1, 2, 4, create_uv_map=True) @@ -45,7 +44,7 @@ def property_reference(run_name="property_reference"): """Produces identical visualization to small_scale, but does not store repeated properties of ``vertex_positions`` and ``vertex_normals``. """ - logdir = os.path.join(BASE_LOGDIR, run_name) + logdir = join(BASE_LOGDIR, run_name) writer = SummaryWriter(logdir) cube = o3d.geometry.TriangleMesh.create_box(1, 2, 4, create_uv_map=True) @@ -79,7 +78,7 @@ def large_scale(n_steps=16, """Generate a large scale summary. Geometry resolution increases linearly with step. Each element in a batch is painted a different color. """ - logdir = os.path.join(BASE_LOGDIR, run_name) + logdir = join(BASE_LOGDIR, run_name) writer = SummaryWriter(logdir) colors = [] for k in range(batch_size): @@ -116,14 +115,13 @@ def large_scale(n_steps=16, max_outputs=batch_size) -def with_material(model_dir=MODEL_DIR): +def with_material(model_path=MODEL_PATH): """Read an obj model from a directory and write as a TensorBoard summary. """ - model_name = os.path.basename(model_dir) - logdir = os.path.join(BASE_LOGDIR, model_name) - model_path = os.path.join(model_dir, model_name + ".obj") - model = o3d.t.geometry.TriangleMesh.from_legacy( - o3d.io.read_triangle_mesh(model_path)) + model_dir = dirname(model_path) + model_name = splitext(basename(model_path))[0] + logdir = join(BASE_LOGDIR, model_name) + model = o3d.t.io.read_triangle_mesh(model_path) summary_3d = { "vertex_positions": model.vertex.positions, "vertex_normals": model.vertex.normals, @@ -134,8 +132,8 @@ def with_material(model_dir=MODEL_DIR): names_to_o3dprop = {"ao": "ambient_occlusion"} for texture in ("albedo", "normal", "ao", "metallic", "roughness"): - texture_file = os.path.join(model_dir, texture + ".png") - if os.path.exists(texture_file): + texture_file = join(model_dir, texture + ".png") + if exists(texture_file): texture = names_to_o3dprop.get(texture, texture) summary_3d.update({ ("material_texture_map_" + texture): @@ -149,11 +147,11 @@ def with_material(model_dir=MODEL_DIR): def demo_scene(): - """Write the demo_scene.py example showing rich PBR materials as a summary + """Write the demo_scene.py example showing rich PBR materials as a summary. """ import demo_scene geoms = demo_scene.create_scene() - writer = SummaryWriter(os.path.join(BASE_LOGDIR, 'demo_scene')) + writer = SummaryWriter(join(BASE_LOGDIR, 'demo_scene')) for geom_data in geoms: geom = geom_data["geometry"] summary_3d = {} diff --git a/examples/python/visualization/tensorboard_tensorflow.py b/examples/python/visualization/tensorboard_tensorflow.py index ea9cb111e15..4b90c8498c6 100644 --- a/examples/python/visualization/tensorboard_tensorflow.py +++ b/examples/python/visualization/tensorboard_tensorflow.py @@ -5,7 +5,7 @@ # SPDX-License-Identifier: MIT # ---------------------------------------------------------------------------- import copy -import os +from os.path import exists, join, dirname, basename, splitext import sys import numpy as np import open3d as o3d @@ -14,15 +14,13 @@ import tensorflow as tf BASE_LOGDIR = "demo_logs/tf/" -MODEL_DIR = os.path.join( - os.path.dirname(os.path.dirname(os.path.dirname( - os.path.realpath(__file__)))), "test_data", "monkey") +MODEL_PATH = o3d.data.MonkeyModel().path def small_scale(run_name="small_scale"): """Basic demo with cube and cylinder with normals and colors. """ - logdir = os.path.join(BASE_LOGDIR, run_name) + logdir = join(BASE_LOGDIR, run_name) writer = tf.summary.create_file_writer(logdir) cube = o3d.geometry.TriangleMesh.create_box(1, 2, 4, create_uv_map=True) @@ -52,7 +50,7 @@ def property_reference(run_name="property_reference"): """Produces identical visualization to small_scale, but does not store repeated properties of ``vertex_positions`` and ``vertex_normals``. """ - logdir = os.path.join(BASE_LOGDIR, run_name) + logdir = join(BASE_LOGDIR, run_name) writer = tf.summary.create_file_writer(logdir) cube = o3d.geometry.TriangleMesh.create_box(1, 2, 4, create_uv_map=True) @@ -90,7 +88,7 @@ def large_scale(n_steps=16, """Generate a large scale summary. Geometry resolution increases linearly with step. Each element in a batch is painted a different color. """ - logdir = os.path.join(BASE_LOGDIR, run_name) + logdir = join(BASE_LOGDIR, run_name) writer = tf.summary.create_file_writer(logdir) colors = [] for k in range(batch_size): @@ -130,14 +128,13 @@ def large_scale(n_steps=16, max_outputs=batch_size) -def with_material(model_dir=MODEL_DIR): +def with_material(model_path=MODEL_PATH): """Read an obj model from a directory and write as a TensorBoard summary. """ - model_name = os.path.basename(model_dir) - logdir = os.path.join(BASE_LOGDIR, model_name) - model_path = os.path.join(model_dir, model_name + ".obj") - model = o3d.t.geometry.TriangleMesh.from_legacy( - o3d.io.read_triangle_mesh(model_path)) + model_dir = dirname(model_path) + model_name = splitext(basename(model_path))[0] + logdir = join(BASE_LOGDIR, model_name) + model = o3d.t.io.read_triangle_mesh(model_path) summary_3d = { "vertex_positions": model.vertex.positions, "vertex_normals": model.vertex.normals, @@ -148,8 +145,8 @@ def with_material(model_dir=MODEL_DIR): names_to_o3dprop = {"ao": "ambient_occlusion"} for texture in ("albedo", "normal", "ao", "metallic", "roughness"): - texture_file = os.path.join(model_dir, texture + ".png") - if os.path.exists(texture_file): + texture_file = join(model_dir, texture + ".png") + if exists(texture_file): texture = names_to_o3dprop.get(texture, texture) summary_3d.update({ ("material_texture_map_" + texture): @@ -168,7 +165,7 @@ def demo_scene(): """ import demo_scene geoms = demo_scene.create_scene() - logdir = os.path.join(BASE_LOGDIR, 'demo_scene') + logdir = join(BASE_LOGDIR, 'demo_scene') writer = tf.summary.create_file_writer(logdir) for geom_data in geoms: geom = geom_data["geometry"]