Spaces:
Paused
Paused
Ali Mohsin
commited on
Commit
·
d172fbe
1
Parent(s):
0ba16db
update 20003
Browse files- loop.py +37 -0
- nvdiffmodeling/src/obj.py +8 -0
- utils.py +32 -0
loop.py
CHANGED
|
@@ -224,11 +224,42 @@ def loop(cfg):
|
|
| 224 |
jacobian_source = SourceMesh.SourceMesh(0, str(output_path / 'tmp' / 'mesh.obj'), {}, 1, ttype=torch.float)
|
| 225 |
if len(list((output_path / 'tmp').glob('*.npz'))) > 0:
|
| 226 |
logging.warn(f'Using existing Jacobian .npz files in {str(output_path)}/tmp/ ! Please check if this is intentional.')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
jacobian_source.load()
|
| 228 |
jacobian_source.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
with torch.no_grad():
|
| 231 |
gt_jacobians = jacobian_source.jacobians_from_vertices(load_mesh.v_pos.unsqueeze(0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
gt_jacobians.requires_grad_(True)
|
| 233 |
|
| 234 |
optimizer = torch.optim.Adam([gt_jacobians], lr=cfg.lr)
|
|
@@ -268,6 +299,12 @@ def loop(cfg):
|
|
| 268 |
|
| 269 |
# updated vertices from jacobians
|
| 270 |
n_vert = jacobian_source.vertices_from_jacobians(gt_jacobians).squeeze()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
# TODO: More texture code required to make it work ...
|
| 273 |
ready_texture = texture.Texture2D(
|
|
|
|
| 224 |
jacobian_source = SourceMesh.SourceMesh(0, str(output_path / 'tmp' / 'mesh.obj'), {}, 1, ttype=torch.float)
|
| 225 |
if len(list((output_path / 'tmp').glob('*.npz'))) > 0:
|
| 226 |
logging.warn(f'Using existing Jacobian .npz files in {str(output_path)}/tmp/ ! Please check if this is intentional.')
|
| 227 |
+
|
| 228 |
+
# Check if the mesh file exists before loading
|
| 229 |
+
mesh_file_path = output_path / 'tmp' / 'mesh.obj'
|
| 230 |
+
print(f"Looking for mesh file at: {mesh_file_path}")
|
| 231 |
+
print(f"Absolute path: {mesh_file_path.absolute()}")
|
| 232 |
+
|
| 233 |
+
if not mesh_file_path.exists():
|
| 234 |
+
# List files in the tmp directory to see what's there
|
| 235 |
+
tmp_dir = output_path / 'tmp'
|
| 236 |
+
if tmp_dir.exists():
|
| 237 |
+
print(f"Files in {tmp_dir}:")
|
| 238 |
+
for file in tmp_dir.iterdir():
|
| 239 |
+
print(f" - {file.name}")
|
| 240 |
+
else:
|
| 241 |
+
print(f"Tmp directory {tmp_dir} does not exist")
|
| 242 |
+
raise FileNotFoundError(f"Mesh file not found: {mesh_file_path}. This indicates an issue with the mesh loading process.")
|
| 243 |
+
|
| 244 |
+
print(f"Mesh file exists at: {mesh_file_path}")
|
| 245 |
+
print("Loading jacobian source...")
|
| 246 |
jacobian_source.load()
|
| 247 |
jacobian_source.to(device)
|
| 248 |
+
|
| 249 |
+
# Validate that jacobian source loaded properly
|
| 250 |
+
if not hasattr(jacobian_source, 'jacobians_from_vertices') or jacobian_source.jacobians_from_vertices is None:
|
| 251 |
+
raise ValueError("Failed to load jacobian source. The jacobians_from_vertices method is not available.")
|
| 252 |
+
|
| 253 |
+
print("Jacobian source loaded successfully")
|
| 254 |
|
| 255 |
with torch.no_grad():
|
| 256 |
gt_jacobians = jacobian_source.jacobians_from_vertices(load_mesh.v_pos.unsqueeze(0))
|
| 257 |
+
|
| 258 |
+
# Validate that gt_jacobians is not empty
|
| 259 |
+
if gt_jacobians is None or gt_jacobians.shape[0] == 0:
|
| 260 |
+
raise ValueError("Failed to generate jacobians from vertices. This indicates an issue with the mesh or jacobian source.")
|
| 261 |
+
|
| 262 |
+
print(f"Generated jacobians with shape: {gt_jacobians.shape}")
|
| 263 |
gt_jacobians.requires_grad_(True)
|
| 264 |
|
| 265 |
optimizer = torch.optim.Adam([gt_jacobians], lr=cfg.lr)
|
|
|
|
| 299 |
|
| 300 |
# updated vertices from jacobians
|
| 301 |
n_vert = jacobian_source.vertices_from_jacobians(gt_jacobians).squeeze()
|
| 302 |
+
|
| 303 |
+
# Validate that n_vert is not empty
|
| 304 |
+
if n_vert is None or n_vert.shape[0] == 0:
|
| 305 |
+
raise ValueError("Generated vertices are empty. This indicates an issue with the jacobian source or mesh loading.")
|
| 306 |
+
|
| 307 |
+
print(f"Iteration {it}: Generated {n_vert.shape[0]} vertices")
|
| 308 |
|
| 309 |
# TODO: More texture code required to make it work ...
|
| 310 |
ready_texture = texture.Texture2D(
|
nvdiffmodeling/src/obj.py
CHANGED
|
@@ -163,6 +163,14 @@ def load_obj(filename, clear_ks=True, mtl_override=None):
|
|
| 163 |
tfaces = torch.tensor(tfaces, dtype=torch.int64, device='cuda') if texcoords is not None else None
|
| 164 |
nfaces = torch.tensor(nfaces, dtype=torch.int64, device='cuda') if normals is not None else None
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
# Read weights and bones if available
|
| 167 |
try:
|
| 168 |
v_weights = torch.tensor(np.load(os.path.splitext(filename)[0] + ".weights.npy"), dtype=torch.float32, device='cuda')
|
|
|
|
| 163 |
tfaces = torch.tensor(tfaces, dtype=torch.int64, device='cuda') if texcoords is not None else None
|
| 164 |
nfaces = torch.tensor(nfaces, dtype=torch.int64, device='cuda') if normals is not None else None
|
| 165 |
|
| 166 |
+
# Validate that we have valid mesh data
|
| 167 |
+
if len(vertices) == 0:
|
| 168 |
+
raise ValueError(f"No vertices found in mesh file: {filename}")
|
| 169 |
+
if len(faces) == 0:
|
| 170 |
+
raise ValueError(f"No faces found in mesh file: {filename}")
|
| 171 |
+
|
| 172 |
+
print(f"Loaded mesh from {filename}: {len(vertices)} vertices, {len(faces)} faces")
|
| 173 |
+
|
| 174 |
# Read weights and bones if available
|
| 175 |
try:
|
| 176 |
v_weights = torch.tensor(np.load(os.path.splitext(filename)[0] + ".weights.npy"), dtype=torch.float32, device='cuda')
|
utils.py
CHANGED
|
@@ -5,6 +5,7 @@ from nvdiffmodeling.src import mesh
|
|
| 5 |
from nvdiffmodeling.src import texture
|
| 6 |
import numpy as np
|
| 7 |
from utilities.helpers import get_vp_map
|
|
|
|
| 8 |
|
| 9 |
texture_map = texture.create_trainable(np.random.uniform(size=[512] * 2 + [3], low=0.0, high=1.0), [512] * 2, True)
|
| 10 |
normal_map = texture.create_trainable(np.array([0, 0, 1]), [512] * 2, True)
|
|
@@ -13,8 +14,19 @@ specular_map = texture.create_trainable(np.array([0, 0, 0]), [512] * 2, True)
|
|
| 13 |
def get_mesh(mesh_path, output_path, triangulate_flag, bsdf_flag, mesh_name='mesh.obj'):
|
| 14 |
try:
|
| 15 |
print(f"Loading mesh from: {mesh_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
ms = pymeshlab.MeshSet()
|
| 17 |
ms.load_new_mesh(mesh_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
if triangulate_flag:
|
| 20 |
print('Retriangulating shape')
|
|
@@ -34,6 +46,16 @@ def get_mesh(mesh_path, output_path, triangulate_flag, bsdf_flag, mesh_name='mes
|
|
| 34 |
|
| 35 |
print(f"Loading OBJ from temporary path: {tmp_mesh_path}")
|
| 36 |
load_mesh = obj.load_obj(str(tmp_mesh_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
load_mesh = mesh.unit_size(load_mesh)
|
| 38 |
|
| 39 |
ms.add_mesh(
|
|
@@ -49,7 +71,17 @@ def get_mesh(mesh_path, output_path, triangulate_flag, bsdf_flag, mesh_name='mes
|
|
| 49 |
},
|
| 50 |
base=load_mesh # Get UVs from original loaded mesh
|
| 51 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return load_mesh
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
print(f"Error in get_mesh: {e}")
|
| 55 |
import traceback
|
|
|
|
| 5 |
from nvdiffmodeling.src import texture
|
| 6 |
import numpy as np
|
| 7 |
from utilities.helpers import get_vp_map
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
texture_map = texture.create_trainable(np.random.uniform(size=[512] * 2 + [3], low=0.0, high=1.0), [512] * 2, True)
|
| 11 |
normal_map = texture.create_trainable(np.array([0, 0, 1]), [512] * 2, True)
|
|
|
|
| 14 |
def get_mesh(mesh_path, output_path, triangulate_flag, bsdf_flag, mesh_name='mesh.obj'):
|
| 15 |
try:
|
| 16 |
print(f"Loading mesh from: {mesh_path}")
|
| 17 |
+
|
| 18 |
+
# Check if mesh file exists
|
| 19 |
+
if not os.path.exists(mesh_path):
|
| 20 |
+
raise FileNotFoundError(f"Mesh file not found: {mesh_path}")
|
| 21 |
+
|
| 22 |
ms = pymeshlab.MeshSet()
|
| 23 |
ms.load_new_mesh(mesh_path)
|
| 24 |
+
|
| 25 |
+
# Check if mesh was loaded successfully
|
| 26 |
+
if ms.current_mesh().vertex_number() == 0:
|
| 27 |
+
raise ValueError(f"Mesh file {mesh_path} has no vertices")
|
| 28 |
+
|
| 29 |
+
print(f"Loaded mesh with {ms.current_mesh().vertex_number()} vertices and {ms.current_mesh().face_number()} faces")
|
| 30 |
|
| 31 |
if triangulate_flag:
|
| 32 |
print('Retriangulating shape')
|
|
|
|
| 46 |
|
| 47 |
print(f"Loading OBJ from temporary path: {tmp_mesh_path}")
|
| 48 |
load_mesh = obj.load_obj(str(tmp_mesh_path))
|
| 49 |
+
|
| 50 |
+
# Check if mesh was loaded successfully
|
| 51 |
+
if load_mesh.v_pos is None or load_mesh.v_pos.shape[0] == 0:
|
| 52 |
+
raise ValueError(f"Failed to load mesh vertices from {tmp_mesh_path}")
|
| 53 |
+
|
| 54 |
+
if load_mesh.t_pos_idx is None or load_mesh.t_pos_idx.shape[0] == 0:
|
| 55 |
+
raise ValueError(f"Failed to load mesh faces from {tmp_mesh_path}")
|
| 56 |
+
|
| 57 |
+
print(f"Loaded mesh with {load_mesh.v_pos.shape[0]} vertices and {load_mesh.t_pos_idx.shape[0]} faces")
|
| 58 |
+
|
| 59 |
load_mesh = mesh.unit_size(load_mesh)
|
| 60 |
|
| 61 |
ms.add_mesh(
|
|
|
|
| 71 |
},
|
| 72 |
base=load_mesh # Get UVs from original loaded mesh
|
| 73 |
)
|
| 74 |
+
|
| 75 |
+
# Final check to ensure mesh is valid
|
| 76 |
+
if load_mesh.v_pos is None or load_mesh.v_pos.shape[0] == 0:
|
| 77 |
+
raise ValueError("Final mesh has no vertices")
|
| 78 |
+
|
| 79 |
+
if load_mesh.t_pos_idx is None or load_mesh.t_pos_idx.shape[0] == 0:
|
| 80 |
+
raise ValueError("Final mesh has no faces")
|
| 81 |
+
|
| 82 |
+
print(f"Successfully loaded mesh with {load_mesh.v_pos.shape[0]} vertices and {load_mesh.t_pos_idx.shape[0]} faces")
|
| 83 |
return load_mesh
|
| 84 |
+
|
| 85 |
except Exception as e:
|
| 86 |
print(f"Error in get_mesh: {e}")
|
| 87 |
import traceback
|