Spaces:
Runtime error
Runtime error
working proto
Browse filesFiles changed (4)
- app.py+46-13
- model_scripted.pt+1-1
- requirements.txt+2-3
- sphere.obj
123456785 unmodified lines141516171819202122232431 unmodified lines565758596061626364656667686970717273747576from pytorch3d.io import load_objfrom pytorch3d.structures import Meshesimport torchimport gradio as grimport plotly.graph_objects as godevice = torch.device("cpu")model = torch.jit.load('model_scripted.pt').to(device)5 unmodified linesscale = max(verts.abs().max(0)[0])return verts / scaledef plot_3d_results(verts, faces, uv_seam_edge_indices):# Convert vertices to NumPy for easier manipulationverts_np = verts.cpu().numpy()faces_np = faces.verts_idx.cpu().numpy()# Prepare the vertex coordinates for the Mesh3d plotx, y, z = verts_np[:, 0], verts_np[:, 1], verts_np[:, 2]31 unmodified linesdef generate_prediction(file_input, treshold_value=0.5):verts, faces, aux = load_obj(file_input)verts = normalize_vertices(verts)mesh = Meshes(verts=[verts.to(device)], faces=[faces.verts_idx.to(device)])model.eval()test_verts = mesh.verts_packed().to(device)test_edges = mesh.edges_packed().to(device)with torch.no_grad():test_outputs_logits = model(test_verts, test_edges).to(device)test_outputs = torch.sigmoid(test_outputs_logits).to(device)test_predictions = (test_outputs > treshold_value).int().cpu()uv_seam_edges_mask = test_predictions.cpu().squeeze() == 1uv_seam_edges = test_edges[uv_seam_edges_mask].cpu().tolist()# Return the HTML content generated by plot_3d_resultsreturn plot_3d_results(verts, faces, uv_seam_edges)12345675 unmodified lines1314151617181920212231 unmodified lines54555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109import torchimport gradio as grimport plotly.graph_objects as goimport trimeshdevice = torch.device("cpu")model = torch.jit.load('model_scripted.pt').to(device)5 unmodified linesscale = max(verts.abs().max(0)[0])return verts / scaledef plot_3d_results(verts, faces, uv_seam_edge_indices):# Convert vertices to NumPy for easier manipulationverts_np = verts.cpu().numpy()faces_np = faces.cpu().numpy()# Prepare the vertex coordinates for the Mesh3d plotx, y, z = verts_np[:, 0], verts_np[:, 1], verts_np[:, 2]31 unmodified linesdef generate_prediction(file_input, treshold_value=0.5):# Load the triangle meshmesh = trimesh.load_mesh(file_input)# For production, we should use a faster method to preprocess the mesh!# Convert vertices to a PyTorch tensorvertices = torch.tensor(mesh.vertices, dtype=torch.float32)# Initialize containers for unique vertices and mappingunique_vertices = []vertex_mapping = {}new_faces = []# Populate unique vertices and create new faces with updated indicesfor face in mesh.faces:new_face = []for orig_index in face:vertex = tuple(vertices[orig_index].tolist()) # Convert to tuple (hashable)if vertex not in vertex_mapping:vertex_mapping[vertex] = len(unique_vertices)unique_vertices.append(vertices[orig_index])new_face.append(vertex_mapping[vertex])new_faces.append(new_face)# Create edge set to ensure uniquenessedge_set = set()for face in new_faces:# Unpack the vertex indicesv1, v2, v3 = face# Create undirected edges (use tuple sorting to ensure uniqueness)edge_set.add(tuple(sorted((v1, v2))))edge_set.add(tuple(sorted((v2, v3))))edge_set.add(tuple(sorted((v1, v3))))# Convert edges back to tensoredges = torch.tensor(list(edge_set), dtype=torch.long)# Convert unique vertices and new faces back to tensorsverts = torch.stack(unique_vertices)faces = torch.tensor(new_faces, dtype=torch.long)model.eval()with torch.no_grad():test_outputs_logits = model(verts, edges).to(device)test_outputs = torch.sigmoid(test_outputs_logits).to(device)test_predictions = (test_outputs > treshold_value).int().cpu()uv_seam_edges_mask = test_predictions.cpu().squeeze() == 1uv_seam_edges = edges[uv_seam_edges_mask].cpu().tolist()# Return the HTML content generated by plot_3d_resultsreturn plot_3d_results(verts, faces, uv_seam_edges)
123version https://git-lfs.github.com/spec/v1oid sha256:5475053a74a596b223abc768bf8d926247bf94f826c1ed41b469f593a11828c8size 255324123version https://git-lfs.github.com/spec/v1oid sha256:6f0d5dcd806540ba8061e3d70ed17eac539e560ae0932f04679f706898126588size 255324
1234git+https://github.com/facebookresearch/pytorch3d.gittorchpytorch3dplotly123torchplotlytrimesh
app.py+46-13
model_scripted.pt+1-1
requirements.txt+2-3
sphere.obj