diff --git a/tests/test_io_obj.py b/tests/test_io_obj.py index 5e2542b8..f0e6a2d7 100644 --- a/tests/test_io_obj.py +++ b/tests/test_io_obj.py @@ -24,6 +24,9 @@ from pytorch3d.renderer import TexturesAtlas, TexturesUV, TexturesVertex from pytorch3d.structures import Meshes, join_meshes_as_batch from pytorch3d.utils import torus +DATA_DIR = get_tests_dir() / "data" +TUTORIAL_DATA_DIR = get_pytorch3d_dir() / "docs/tutorials/data" + class TestMeshObjIO(TestCaseMixin, unittest.TestCase): def test_load_obj_simple(self): @@ -479,9 +482,8 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): self.assertEqual(actual_file, expected_file) def test_load_mtl(self): - DATA_DIR = get_pytorch3d_dir() / "docs/tutorials/data" obj_filename = "cow_mesh/cow.obj" - filename = os.path.join(DATA_DIR, obj_filename) + filename = os.path.join(TUTORIAL_DATA_DIR, obj_filename) verts, faces, aux = load_obj(filename) materials = aux.material_colors tex_maps = aux.texture_images @@ -563,9 +565,8 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): def test_load_mtl_texture_atlas_compare_softras(self): # Load saved texture atlas created with SoftRas. device = torch.device("cuda:0") - DATA_DIR = get_pytorch3d_dir() - obj_filename = DATA_DIR / "docs/tutorials/data/cow_mesh/cow.obj" - expected_atlas_fname = DATA_DIR / "tests/data/cow_texture_atlas_softras.pt" + obj_filename = TUTORIAL_DATA_DIR / "cow_mesh/cow.obj" + expected_atlas_fname = DATA_DIR / "cow_texture_atlas_softras.pt" # Note, the reference texture atlas generated using SoftRas load_obj function # is too large to check in to the repo. Download the file to run the test locally. @@ -594,9 +595,8 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): self.assertClose(expected_atlas, aux.texture_atlas, atol=5e-5) def test_load_mtl_noload(self): - DATA_DIR = get_pytorch3d_dir() / "docs/tutorials/data" obj_filename = "cow_mesh/cow.obj" - filename = os.path.join(DATA_DIR, obj_filename) + filename = os.path.join(TUTORIAL_DATA_DIR, obj_filename) verts, faces, aux = load_obj(filename, load_textures=False) self.assertTrue(aux.material_colors is None) @@ -632,7 +632,6 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): self.assertTrue(aux.verts_uvs is None) def test_load_obj_mlt_no_image(self): - DATA_DIR = get_tests_dir() / "data" obj_filename = "obj_mtl_no_image/model.obj" filename = os.path.join(DATA_DIR, obj_filename) R = 8 @@ -661,7 +660,6 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): self.assertEqual(list(aux.material_colors.keys()), ["material_1"]) def test_load_obj_missing_texture(self): - DATA_DIR = get_tests_dir() / "data" obj_filename = "missing_files_obj/model.obj" filename = os.path.join(DATA_DIR, obj_filename) with self.assertWarnsRegex(UserWarning, "Texture file does not exist"): @@ -676,7 +674,6 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): self.assertTrue(torch.allclose(faces.verts_idx, expected_faces)) def test_load_obj_missing_texture_noload(self): - DATA_DIR = get_tests_dir() / "data" obj_filename = "missing_files_obj/model.obj" filename = os.path.join(DATA_DIR, obj_filename) verts, faces, aux = load_obj(filename, load_textures=False) @@ -692,7 +689,6 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): self.assertTrue(aux.texture_images is None) def test_load_obj_missing_mtl(self): - DATA_DIR = get_tests_dir() / "data" obj_filename = "missing_files_obj/model2.obj" filename = os.path.join(DATA_DIR, obj_filename) with self.assertWarnsRegex(UserWarning, "Mtl file does not exist"): @@ -707,7 +703,6 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): self.assertTrue(torch.allclose(faces.verts_idx, expected_faces)) def test_load_obj_missing_mtl_noload(self): - DATA_DIR = get_tests_dir() / "data" obj_filename = "missing_files_obj/model2.obj" filename = os.path.join(DATA_DIR, obj_filename) verts, faces, aux = load_obj(filename, load_textures=False) @@ -764,8 +759,7 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): mesh.textures.atlas_padded(), mesh3.textures.atlas_padded() ) - DATA_DIR = get_pytorch3d_dir() / "docs/tutorials/data" - obj_filename = DATA_DIR / "cow_mesh/cow.obj" + obj_filename = TUTORIAL_DATA_DIR / "cow_mesh/cow.obj" mesh = load_objs_as_meshes([obj_filename]) mesh3 = load_objs_as_meshes([obj_filename, obj_filename, obj_filename]) @@ -803,7 +797,7 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): check_triple(mesh_atlas, mesh_atlas3) # Test load multiple meshes with textures into a batch. - teapot_obj = DATA_DIR / "teapot.obj" + teapot_obj = TUTORIAL_DATA_DIR / "teapot.obj" mesh_teapot = load_objs_as_meshes([teapot_obj]) teapot_verts, teapot_faces = mesh_teapot.get_mesh_verts_faces(0) mix_mesh = load_objs_as_meshes([obj_filename, teapot_obj], load_textures=False) @@ -863,8 +857,8 @@ class TestMeshObjIO(TestCaseMixin, unittest.TestCase): def bm_load_texture_atlas(R: int): device = torch.device("cuda:0") torch.cuda.set_device(device) - DATA_DIR = "/data/users/nikhilar/fbsource/fbcode/vision/fair/pytorch3d/docs/" - obj_filename = os.path.join(DATA_DIR, "tutorials/data/cow_mesh/cow.obj") + data_dir = "/data/users/nikhilar/fbsource/fbcode/vision/fair/pytorch3d/docs/" + obj_filename = os.path.join(data_dir, "tutorials/data/cow_mesh/cow.obj") torch.cuda.synchronize() def load(): diff --git a/tests/test_marching_cubes.py b/tests/test_marching_cubes.py index 04facfd1..1cc9ae2d 100644 --- a/tests/test_marching_cubes.py +++ b/tests/test_marching_cubes.py @@ -9,6 +9,7 @@ from pytorch3d.ops.marching_cubes import marching_cubes_naive USE_SCIKIT = False +DATA_DIR = get_tests_dir() / "data" def convert_to_local(verts, volume_dim): @@ -640,7 +641,6 @@ class TestMarchingCubes(TestCaseMixin, unittest.TestCase): volume, isolevel=64, return_local_coords=False ) - DATA_DIR = get_tests_dir() / "data" data_filename = "test_marching_cubes_data/sphere_level64.pickle" filename = os.path.join(DATA_DIR, data_filename) with open(filename, "rb") as file: @@ -676,7 +676,6 @@ class TestMarchingCubes(TestCaseMixin, unittest.TestCase): volume = volume.permute(0, 3, 2, 1) # (B, D, H, W) verts, faces = marching_cubes_naive(volume, isolevel=0.001) - DATA_DIR = get_tests_dir() / "data" data_filename = "test_marching_cubes_data/double_ellipsoid.pickle" filename = os.path.join(DATA_DIR, data_filename) with open(filename, "rb") as file: diff --git a/tests/test_render_meshes.py b/tests/test_render_meshes.py index 8cf95414..8f8bd975 100644 --- a/tests/test_render_meshes.py +++ b/tests/test_render_meshes.py @@ -51,6 +51,7 @@ from pytorch3d.utils.torus import torus # All saved images have prefix DEBUG_ DEBUG = False DATA_DIR = get_tests_dir() / "data" +TUTORIAL_DATA_DIR = get_pytorch3d_dir() / "docs/tutorials/data" class TestRenderMeshes(TestCaseMixin, unittest.TestCase): @@ -388,8 +389,8 @@ class TestRenderMeshes(TestCaseMixin, unittest.TestCase): The pupils in the eyes of the cow should always be looking to the left. """ device = torch.device("cuda:0") - obj_dir = get_pytorch3d_dir() / "docs/tutorials/data" - obj_filename = obj_dir / "cow_mesh/cow.obj" + + obj_filename = TUTORIAL_DATA_DIR / "cow_mesh/cow.obj" # Load mesh + texture verts, faces, aux = load_obj( @@ -970,8 +971,8 @@ class TestRenderMeshes(TestCaseMixin, unittest.TestCase): Also check that the backward pass for texture atlas rendering is differentiable. """ device = torch.device("cuda:0") - obj_dir = get_pytorch3d_dir() / "docs/tutorials/data" - obj_filename = obj_dir / "cow_mesh/cow.obj" + + obj_filename = TUTORIAL_DATA_DIR / "cow_mesh/cow.obj" # Load mesh and texture as a per face texture atlas. verts, faces, aux = load_obj(