Spaces:
Running
on
L40S
Running
on
L40S
Delete folder svrm/.ipynb_checkpoints with huggingface_hub
Browse files
svrm/.ipynb_checkpoints/predictor-checkpoint.py
DELETED
@@ -1,152 +0,0 @@
|
|
1 |
-
# Open Source Model Licensed under the Apache License Version 2.0
|
2 |
-
# and Other Licenses of the Third-Party Components therein:
|
3 |
-
# The below Model in this distribution may have been modified by THL A29 Limited
|
4 |
-
# ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
|
5 |
-
|
6 |
-
# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
|
7 |
-
# The below software and/or models in this distribution may have been
|
8 |
-
# modified by THL A29 Limited ("Tencent Modifications").
|
9 |
-
# All Tencent Modifications are Copyright (C) THL A29 Limited.
|
10 |
-
|
11 |
-
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
|
12 |
-
# except for the third-party components listed below.
|
13 |
-
# Hunyuan 3D does not impose any additional limitations beyond what is outlined
|
14 |
-
# in the repsective licenses of these third-party components.
|
15 |
-
# Users must comply with all terms and conditions of original licenses of these third-party
|
16 |
-
# components and must ensure that the usage of the third party components adheres to
|
17 |
-
# all relevant laws and regulations.
|
18 |
-
|
19 |
-
# For avoidance of doubts, Hunyuan 3D means the large language models and
|
20 |
-
# their software and algorithms, including trained model weights, parameters (including
|
21 |
-
# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,
|
22 |
-
# fine-tuning enabling code and other elements of the foregoing made publicly available
|
23 |
-
# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
|
24 |
-
|
25 |
-
import os
|
26 |
-
import math
|
27 |
-
import time
|
28 |
-
import torch
|
29 |
-
import numpy as np
|
30 |
-
from tqdm import tqdm
|
31 |
-
from PIL import Image, ImageSequence
|
32 |
-
from omegaconf import OmegaConf
|
33 |
-
from torchvision import transforms
|
34 |
-
from safetensors.torch import save_file, load_file
|
35 |
-
from .ldm.util import instantiate_from_config
|
36 |
-
from .ldm.vis_util import render
|
37 |
-
|
38 |
-
class MV23DPredictor(object):
|
39 |
-
def __init__(self, ckpt_path, cfg_path, elevation=15, number_view=60,
|
40 |
-
render_size=256, device="cuda:0") -> None:
|
41 |
-
self.device = device
|
42 |
-
self.elevation = elevation
|
43 |
-
self.number_view = number_view
|
44 |
-
self.render_size = render_size
|
45 |
-
|
46 |
-
self.elevation_list = [0, 0, 0, 0, 0, 0, 0]
|
47 |
-
self.azimuth_list = [0, 60, 120, 180, 240, 300, 0]
|
48 |
-
|
49 |
-
st = time.time()
|
50 |
-
self.model = self.init_model(ckpt_path, cfg_path)
|
51 |
-
print(f"=====> mv23d model init time: {time.time() - st}")
|
52 |
-
|
53 |
-
self.input_view_transform = transforms.Compose([
|
54 |
-
transforms.Resize(504, interpolation=Image.BICUBIC),
|
55 |
-
transforms.ToTensor(),
|
56 |
-
])
|
57 |
-
self.final_input_view_transform = transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
|
58 |
-
|
59 |
-
def init_model(self, ckpt_path, cfg_path):
|
60 |
-
config = OmegaConf.load(cfg_path)
|
61 |
-
model = instantiate_from_config(config.model)
|
62 |
-
|
63 |
-
weights = load_file("./weights/svrm/svrm.safetensors")
|
64 |
-
model.load_state_dict(weights)
|
65 |
-
|
66 |
-
model.to(self.device)
|
67 |
-
model = model.eval()
|
68 |
-
model.render.half()
|
69 |
-
print(f'Load model successfully')
|
70 |
-
return model
|
71 |
-
|
72 |
-
def create_camera_to_world_matrix(self, elevation, azimuth, cam_dis=1.5):
|
73 |
-
# elevation azimuth are radians
|
74 |
-
# Convert elevation and azimuth angles to Cartesian coordinates on a unit sphere
|
75 |
-
x = np.cos(elevation) * np.cos(azimuth)
|
76 |
-
y = np.cos(elevation) * np.sin(azimuth)
|
77 |
-
z = np.sin(elevation)
|
78 |
-
|
79 |
-
# Calculate camera position, target, and up vectors
|
80 |
-
camera_pos = np.array([x, y, z]) * cam_dis
|
81 |
-
target = np.array([0, 0, 0])
|
82 |
-
up = np.array([0, 0, 1])
|
83 |
-
|
84 |
-
# Construct view matrix
|
85 |
-
forward = target - camera_pos
|
86 |
-
forward /= np.linalg.norm(forward)
|
87 |
-
right = np.cross(forward, up)
|
88 |
-
right /= np.linalg.norm(right)
|
89 |
-
new_up = np.cross(right, forward)
|
90 |
-
new_up /= np.linalg.norm(new_up)
|
91 |
-
cam2world = np.eye(4)
|
92 |
-
cam2world[:3, :3] = np.array([right, new_up, -forward]).T
|
93 |
-
cam2world[:3, 3] = camera_pos
|
94 |
-
return cam2world
|
95 |
-
|
96 |
-
def refine_mask(self, mask, k=16):
|
97 |
-
mask /= 255.0
|
98 |
-
boder_mask = (mask >= -math.pi / 2.0 / k + 0.5) & (mask <= math.pi / 2.0 / k + 0.5)
|
99 |
-
mask[boder_mask] = 0.5 * np.sin(k * (mask[boder_mask] - 0.5)) + 0.5
|
100 |
-
mask[mask < -math.pi / 2.0 / k + 0.5] = 0.0
|
101 |
-
mask[mask > math.pi / 2.0 / k + 0.5] = 1.0
|
102 |
-
return (mask * 255.0).astype(np.uint8)
|
103 |
-
|
104 |
-
def load_images_and_cameras(self, input_imgs, elevation_list, azimuth_list):
|
105 |
-
input_image_list = []
|
106 |
-
input_cam_list = []
|
107 |
-
for input_view_image, elevation, azimuth in zip(input_imgs, elevation_list, azimuth_list):
|
108 |
-
input_view_image = self.input_view_transform(input_view_image)
|
109 |
-
input_image_list.append(input_view_image)
|
110 |
-
|
111 |
-
input_view_cam_pos = self.create_camera_to_world_matrix(np.radians(elevation), np.radians(azimuth))
|
112 |
-
input_view_cam_intrinsic = np.array([35. / 32, 35. /32, 0.5, 0.5])
|
113 |
-
input_view_cam = torch.from_numpy(
|
114 |
-
np.concatenate([input_view_cam_pos.reshape(-1), input_view_cam_intrinsic], 0)
|
115 |
-
).float()
|
116 |
-
input_cam_list.append(input_view_cam)
|
117 |
-
|
118 |
-
pixels_input = torch.stack(input_image_list, dim=0)
|
119 |
-
input_images = self.final_input_view_transform(pixels_input)
|
120 |
-
input_cams = torch.stack(input_cam_list, dim=0)
|
121 |
-
return input_images, input_cams
|
122 |
-
|
123 |
-
def load_data(self, intput_imgs):
|
124 |
-
assert (6+1) == len(intput_imgs)
|
125 |
-
|
126 |
-
input_images, input_cams = self.load_images_and_cameras(intput_imgs, self.elevation_list, self.azimuth_list)
|
127 |
-
input_cams[-1, :] = 0 # for user input view
|
128 |
-
|
129 |
-
data = {}
|
130 |
-
data["input_view"] = input_images.unsqueeze(0).to(self.device) # 1 4 3 512 512
|
131 |
-
data["input_view_cam"] = input_cams.unsqueeze(0).to(self.device) # 1 4 20
|
132 |
-
return data
|
133 |
-
|
134 |
-
@torch.no_grad()
|
135 |
-
def predict(
|
136 |
-
self,
|
137 |
-
intput_imgs,
|
138 |
-
save_dir = "outputs/",
|
139 |
-
image_input = None,
|
140 |
-
target_face_count = 10000,
|
141 |
-
do_texture_mapping = True,
|
142 |
-
):
|
143 |
-
os.makedirs(save_dir, exist_ok=True)
|
144 |
-
print(save_dir)
|
145 |
-
|
146 |
-
with torch.cuda.amp.autocast():
|
147 |
-
self.model.export_mesh_with_uv(
|
148 |
-
data = self.load_data(intput_imgs),
|
149 |
-
out_dir = save_dir,
|
150 |
-
target_face_count = target_face_count,
|
151 |
-
do_texture_mapping = do_texture_mapping
|
152 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|