Skip to content

Commit 0f06a14

Browse files
authored
Fix tensorboard examples with MonkeyModel path (#6569)
1 parent be53855 commit 0f06a14

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

examples/python/visualization/tensorboard_pytorch.py

+15-17
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@
55
# SPDX-License-Identifier: MIT
66
# ----------------------------------------------------------------------------
77
import copy
8-
import os
8+
from os.path import exists, join, dirname, basename, splitext
99
import sys
1010
import numpy as np
1111
import open3d as o3d
1212
# pylint: disable-next=unused-import
13+
from open3d.visualization.tensorboard_plugin import summary # noqa
1314
from open3d.visualization.tensorboard_plugin.util import to_dict_batch
1415
from torch.utils.tensorboard import SummaryWriter
1516

1617
BASE_LOGDIR = "demo_logs/pytorch/"
17-
MODEL_DIR = os.path.join(
18-
os.path.dirname(os.path.dirname(os.path.dirname(
19-
os.path.realpath(__file__)))), "test_data", "monkey")
18+
MODEL_PATH = o3d.data.MonkeyModel().path
2019

2120

2221
def small_scale(run_name="small_scale"):
2322
"""Basic demo with cube and cylinder with normals and colors.
2423
"""
25-
logdir = os.path.join(BASE_LOGDIR, run_name)
24+
logdir = join(BASE_LOGDIR, run_name)
2625
writer = SummaryWriter(logdir)
2726

2827
cube = o3d.geometry.TriangleMesh.create_box(1, 2, 4, create_uv_map=True)
@@ -45,7 +44,7 @@ def property_reference(run_name="property_reference"):
4544
"""Produces identical visualization to small_scale, but does not store
4645
repeated properties of ``vertex_positions`` and ``vertex_normals``.
4746
"""
48-
logdir = os.path.join(BASE_LOGDIR, run_name)
47+
logdir = join(BASE_LOGDIR, run_name)
4948
writer = SummaryWriter(logdir)
5049

5150
cube = o3d.geometry.TriangleMesh.create_box(1, 2, 4, create_uv_map=True)
@@ -79,7 +78,7 @@ def large_scale(n_steps=16,
7978
"""Generate a large scale summary. Geometry resolution increases linearly
8079
with step. Each element in a batch is painted a different color.
8180
"""
82-
logdir = os.path.join(BASE_LOGDIR, run_name)
81+
logdir = join(BASE_LOGDIR, run_name)
8382
writer = SummaryWriter(logdir)
8483
colors = []
8584
for k in range(batch_size):
@@ -116,14 +115,13 @@ def large_scale(n_steps=16,
116115
max_outputs=batch_size)
117116

118117

119-
def with_material(model_dir=MODEL_DIR):
118+
def with_material(model_path=MODEL_PATH):
120119
"""Read an obj model from a directory and write as a TensorBoard summary.
121120
"""
122-
model_name = os.path.basename(model_dir)
123-
logdir = os.path.join(BASE_LOGDIR, model_name)
124-
model_path = os.path.join(model_dir, model_name + ".obj")
125-
model = o3d.t.geometry.TriangleMesh.from_legacy(
126-
o3d.io.read_triangle_mesh(model_path))
121+
model_dir = dirname(model_path)
122+
model_name = splitext(basename(model_path))[0]
123+
logdir = join(BASE_LOGDIR, model_name)
124+
model = o3d.t.io.read_triangle_mesh(model_path)
127125
summary_3d = {
128126
"vertex_positions": model.vertex.positions,
129127
"vertex_normals": model.vertex.normals,
@@ -134,8 +132,8 @@ def with_material(model_dir=MODEL_DIR):
134132
names_to_o3dprop = {"ao": "ambient_occlusion"}
135133

136134
for texture in ("albedo", "normal", "ao", "metallic", "roughness"):
137-
texture_file = os.path.join(model_dir, texture + ".png")
138-
if os.path.exists(texture_file):
135+
texture_file = join(model_dir, texture + ".png")
136+
if exists(texture_file):
139137
texture = names_to_o3dprop.get(texture, texture)
140138
summary_3d.update({
141139
("material_texture_map_" + texture):
@@ -149,11 +147,11 @@ def with_material(model_dir=MODEL_DIR):
149147

150148

151149
def demo_scene():
152-
"""Write the demo_scene.py example showing rich PBR materials as a summary
150+
"""Write the demo_scene.py example showing rich PBR materials as a summary.
153151
"""
154152
import demo_scene
155153
geoms = demo_scene.create_scene()
156-
writer = SummaryWriter(os.path.join(BASE_LOGDIR, 'demo_scene'))
154+
writer = SummaryWriter(join(BASE_LOGDIR, 'demo_scene'))
157155
for geom_data in geoms:
158156
geom = geom_data["geometry"]
159157
summary_3d = {}

examples/python/visualization/tensorboard_tensorflow.py

+13-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# SPDX-License-Identifier: MIT
66
# ----------------------------------------------------------------------------
77
import copy
8-
import os
8+
from os.path import exists, join, dirname, basename, splitext
99
import sys
1010
import numpy as np
1111
import open3d as o3d
@@ -14,15 +14,13 @@
1414
import tensorflow as tf
1515

1616
BASE_LOGDIR = "demo_logs/tf/"
17-
MODEL_DIR = os.path.join(
18-
os.path.dirname(os.path.dirname(os.path.dirname(
19-
os.path.realpath(__file__)))), "test_data", "monkey")
17+
MODEL_PATH = o3d.data.MonkeyModel().path
2018

2119

2220
def small_scale(run_name="small_scale"):
2321
"""Basic demo with cube and cylinder with normals and colors.
2422
"""
25-
logdir = os.path.join(BASE_LOGDIR, run_name)
23+
logdir = join(BASE_LOGDIR, run_name)
2624
writer = tf.summary.create_file_writer(logdir)
2725

2826
cube = o3d.geometry.TriangleMesh.create_box(1, 2, 4, create_uv_map=True)
@@ -52,7 +50,7 @@ def property_reference(run_name="property_reference"):
5250
"""Produces identical visualization to small_scale, but does not store
5351
repeated properties of ``vertex_positions`` and ``vertex_normals``.
5452
"""
55-
logdir = os.path.join(BASE_LOGDIR, run_name)
53+
logdir = join(BASE_LOGDIR, run_name)
5654
writer = tf.summary.create_file_writer(logdir)
5755

5856
cube = o3d.geometry.TriangleMesh.create_box(1, 2, 4, create_uv_map=True)
@@ -90,7 +88,7 @@ def large_scale(n_steps=16,
9088
"""Generate a large scale summary. Geometry resolution increases linearly
9189
with step. Each element in a batch is painted a different color.
9290
"""
93-
logdir = os.path.join(BASE_LOGDIR, run_name)
91+
logdir = join(BASE_LOGDIR, run_name)
9492
writer = tf.summary.create_file_writer(logdir)
9593
colors = []
9694
for k in range(batch_size):
@@ -130,14 +128,13 @@ def large_scale(n_steps=16,
130128
max_outputs=batch_size)
131129

132130

133-
def with_material(model_dir=MODEL_DIR):
131+
def with_material(model_path=MODEL_PATH):
134132
"""Read an obj model from a directory and write as a TensorBoard summary.
135133
"""
136-
model_name = os.path.basename(model_dir)
137-
logdir = os.path.join(BASE_LOGDIR, model_name)
138-
model_path = os.path.join(model_dir, model_name + ".obj")
139-
model = o3d.t.geometry.TriangleMesh.from_legacy(
140-
o3d.io.read_triangle_mesh(model_path))
134+
model_dir = dirname(model_path)
135+
model_name = splitext(basename(model_path))[0]
136+
logdir = join(BASE_LOGDIR, model_name)
137+
model = o3d.t.io.read_triangle_mesh(model_path)
141138
summary_3d = {
142139
"vertex_positions": model.vertex.positions,
143140
"vertex_normals": model.vertex.normals,
@@ -148,8 +145,8 @@ def with_material(model_dir=MODEL_DIR):
148145
names_to_o3dprop = {"ao": "ambient_occlusion"}
149146

150147
for texture in ("albedo", "normal", "ao", "metallic", "roughness"):
151-
texture_file = os.path.join(model_dir, texture + ".png")
152-
if os.path.exists(texture_file):
148+
texture_file = join(model_dir, texture + ".png")
149+
if exists(texture_file):
153150
texture = names_to_o3dprop.get(texture, texture)
154151
summary_3d.update({
155152
("material_texture_map_" + texture):
@@ -168,7 +165,7 @@ def demo_scene():
168165
"""
169166
import demo_scene
170167
geoms = demo_scene.create_scene()
171-
logdir = os.path.join(BASE_LOGDIR, 'demo_scene')
168+
logdir = join(BASE_LOGDIR, 'demo_scene')
172169
writer = tf.summary.create_file_writer(logdir)
173170
for geom_data in geoms:
174171
geom = geom_data["geometry"]

0 commit comments

Comments
 (0)