Spaces:
Runtime error
Runtime error
Upload inference.py with huggingface_hub
Browse files- inference.py +91 -99
inference.py
CHANGED
|
@@ -1,99 +1,91 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
-
import torch
|
| 3 |
-
import time
|
| 4 |
-
import nvdiffrast.torch as dr
|
| 5 |
-
from util.utils import get_tri
|
| 6 |
-
import tempfile
|
| 7 |
-
from mesh import Mesh
|
| 8 |
-
import zipfile
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
color_list = []
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
mesh_path_glb = tempfile.NamedTemporaryFile(suffix=f"", delete=False).name
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
#
|
| 80 |
-
#
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
# myzip.write(mesh_path_obj+'.obj', mesh_path_obj.split("/")[-1]+'.obj')
|
| 93 |
-
# myzip.write(mesh_path_obj+'.png', mesh_path_obj.split("/")[-1]+'.png')
|
| 94 |
-
# myzip.write(mesh_path_obj+'.mtl', mesh_path_obj.split("/")[-1]+'.mtl')
|
| 95 |
-
|
| 96 |
-
end_time = time.time()
|
| 97 |
-
elapsed_time = end_time - start_time
|
| 98 |
-
print(f"uv takes {elapsed_time}s")
|
| 99 |
-
return mesh_path_glb+".obj"
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
import time
|
| 4 |
+
import nvdiffrast.torch as dr
|
| 5 |
+
from util.utils import get_tri
|
| 6 |
+
import tempfile
|
| 7 |
+
from mesh import Mesh
|
| 8 |
+
import zipfile
|
| 9 |
+
def generate3d(model, rgb, ccm, device):
|
| 10 |
+
|
| 11 |
+
color_tri = torch.from_numpy(rgb)/255
|
| 12 |
+
xyz_tri = torch.from_numpy(ccm[:,:,(2,1,0)])/255
|
| 13 |
+
color = color_tri.permute(2,0,1)
|
| 14 |
+
xyz = xyz_tri.permute(2,0,1)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def get_imgs(color):
|
| 18 |
+
# color : [C, H, W*6]
|
| 19 |
+
color_list = []
|
| 20 |
+
color_list.append(color[:,:,256*5:256*(1+5)])
|
| 21 |
+
for i in range(0,5):
|
| 22 |
+
color_list.append(color[:,:,256*i:256*(1+i)])
|
| 23 |
+
return torch.stack(color_list, dim=0)# [6, C, H, W]
|
| 24 |
+
|
| 25 |
+
triplane_color = get_imgs(color).permute(0,2,3,1).unsqueeze(0).to(device)# [1, 6, H, W, C]
|
| 26 |
+
|
| 27 |
+
color = get_imgs(color)
|
| 28 |
+
xyz = get_imgs(xyz)
|
| 29 |
+
|
| 30 |
+
color = get_tri(color, dim=0, blender= True, scale = 1).unsqueeze(0)
|
| 31 |
+
xyz = get_tri(xyz, dim=0, blender= True, scale = 1, fix= True).unsqueeze(0)
|
| 32 |
+
|
| 33 |
+
triplane = torch.cat([color,xyz],dim=1).to(device)
|
| 34 |
+
# 3D visualize
|
| 35 |
+
model.eval()
|
| 36 |
+
glctx = dr.RasterizeCudaContext()
|
| 37 |
+
|
| 38 |
+
if model.denoising == True:
|
| 39 |
+
tnew = 20
|
| 40 |
+
tnew = torch.randint(tnew, tnew+1, [triplane.shape[0]], dtype=torch.long, device=triplane.device)
|
| 41 |
+
noise_new = torch.randn_like(triplane) *0.5+0.5
|
| 42 |
+
triplane = model.scheduler.add_noise(triplane, noise_new, tnew)
|
| 43 |
+
start_time = time.time()
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
triplane_feature2 = model.unet2(triplane,tnew)
|
| 46 |
+
end_time = time.time()
|
| 47 |
+
elapsed_time = end_time - start_time
|
| 48 |
+
print(f"unet takes {elapsed_time}s")
|
| 49 |
+
else:
|
| 50 |
+
triplane_feature2 = model.unet2(triplane)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
with torch.no_grad():
|
| 54 |
+
data_config = {
|
| 55 |
+
'resolution': [1024, 1024],
|
| 56 |
+
"triview_color": triplane_color.to(device),
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
verts, faces = model.decode(data_config, triplane_feature2)
|
| 60 |
+
|
| 61 |
+
data_config['verts'] = verts[0]
|
| 62 |
+
data_config['faces'] = faces
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
from kiui.mesh_utils import clean_mesh
|
| 66 |
+
verts, faces = clean_mesh(data_config['verts'].squeeze().cpu().numpy().astype(np.float32), data_config['faces'].squeeze().cpu().numpy().astype(np.int32), repair = False, remesh=False, remesh_size=0.005)
|
| 67 |
+
data_config['verts'] = torch.from_numpy(verts).cuda().contiguous()
|
| 68 |
+
data_config['faces'] = torch.from_numpy(faces).cuda().contiguous()
|
| 69 |
+
|
| 70 |
+
start_time = time.time()
|
| 71 |
+
with torch.no_grad():
|
| 72 |
+
mesh_path_obj = tempfile.NamedTemporaryFile(suffix=f"", delete=False).name
|
| 73 |
+
model.export_mesh_wt_uv(glctx, data_config, mesh_path_obj, "", device, res=(1024,1024), tri_fea_2=triplane_feature2)
|
| 74 |
+
|
| 75 |
+
mesh = Mesh.load(mesh_path_obj+".obj", bound=0.9, front_dir="+z")
|
| 76 |
+
mesh_path_glb = tempfile.NamedTemporaryFile(suffix=f"", delete=False).name
|
| 77 |
+
mesh.write(mesh_path_glb+".glb")
|
| 78 |
+
|
| 79 |
+
# mesh_obj2 = trimesh.load(mesh_path_glb+".glb", file_type='glb')
|
| 80 |
+
# mesh_path_obj2 = tempfile.NamedTemporaryFile(suffix=f"", delete=False).name
|
| 81 |
+
# mesh_obj2.export(mesh_path_obj2+".obj")
|
| 82 |
+
|
| 83 |
+
with zipfile.ZipFile(mesh_path_obj+'.zip', 'w') as myzip:
|
| 84 |
+
myzip.write(mesh_path_obj+'.obj', mesh_path_obj.split("/")[-1]+'.obj')
|
| 85 |
+
myzip.write(mesh_path_obj+'.png', mesh_path_obj.split("/")[-1]+'.png')
|
| 86 |
+
myzip.write(mesh_path_obj+'.mtl', mesh_path_obj.split("/")[-1]+'.mtl')
|
| 87 |
+
|
| 88 |
+
end_time = time.time()
|
| 89 |
+
elapsed_time = end_time - start_time
|
| 90 |
+
print(f"uv takes {elapsed_time}s")
|
| 91 |
+
return mesh_path_glb+".glb", mesh_path_obj+'.zip'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|