dylanebert
commited on
Commit
•
b788c50
1
Parent(s):
f16d4f3
no texture option
Browse files- README.md +1 -0
- lrm/lrm.py +1 -1
- pipeline.py +61 -94
README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
---
|
2 |
library_name: diffusers
|
3 |
pipeline_tag: image-to-3d
|
|
|
4 |
---
|
5 |
|
6 |
Rehosted weights for [InstantMesh](https://huggingface.co/TencentARC/InstantMesh) using the diffusers pipeline.
|
|
|
1 |
---
|
2 |
library_name: diffusers
|
3 |
pipeline_tag: image-to-3d
|
4 |
+
license: mit
|
5 |
---
|
6 |
|
7 |
Rehosted weights for [InstantMesh](https://huggingface.co/TencentARC/InstantMesh) using the diffusers pipeline.
|
lrm/lrm.py
CHANGED
@@ -3358,7 +3358,7 @@ class LRM(ModelMixin, ConfigMixin):
|
|
3358 |
)
|
3359 |
vertices_colors = (vertices_colors * 255).astype(np.uint8)
|
3360 |
|
3361 |
-
return vertices
|
3362 |
|
3363 |
uvs, mesh_tex_idx, gb_pos, tex_hard_mask = xatlas_uvmap(
|
3364 |
self.geometry.renderer.ctx, vertices, faces, resolution=texture_resolution
|
|
|
3358 |
)
|
3359 |
vertices_colors = (vertices_colors * 255).astype(np.uint8)
|
3360 |
|
3361 |
+
return vertices, faces, vertices_colors
|
3362 |
|
3363 |
uvs, mesh_tex_idx, gb_pos, tex_hard_mask = xatlas_uvmap(
|
3364 |
self.geometry.renderer.ctx, vertices, faces, resolution=texture_resolution
|
pipeline.py
CHANGED
@@ -97,100 +97,67 @@ class InstantMeshPipeline(DiffusionPipeline):
|
|
97 |
self.register_modules(lrm=self.lrm)
|
98 |
|
99 |
@torch.no_grad()
|
100 |
-
def __call__(self, images: torch.Tensor):
|
101 |
-
"""if remove_bg:
|
102 |
-
image = rembg.remove(image)
|
103 |
-
|
104 |
-
image = np.array(image)
|
105 |
-
alpha = np.where(image[..., 3] > 0)
|
106 |
-
y1, y2, x1, x2 = (
|
107 |
-
alpha[0].min(),
|
108 |
-
alpha[0].max(),
|
109 |
-
alpha[1].min(),
|
110 |
-
alpha[1].max(),
|
111 |
-
)
|
112 |
-
fg = image[y1:y2, x1:x2]
|
113 |
-
size = max(fg.shape[0], fg.shape[1])
|
114 |
-
ph0, pw0 = (size - fg.shape[0]) // 2, (size - fg.shape[1]) // 2
|
115 |
-
ph1, pw1 = size - fg.shape[0] - ph0, size - fg.shape[1] - pw0
|
116 |
-
image = np.pad(
|
117 |
-
fg,
|
118 |
-
((ph0, ph1), (pw0, pw1), (0, 0)),
|
119 |
-
mode="constant",
|
120 |
-
constant_values=((0, 0), (0, 0), (0, 0)),
|
121 |
-
)
|
122 |
-
|
123 |
-
new_size = int(image.shape[0] / 0.85)
|
124 |
-
ph0, pw0 = (new_size - size) // 2, (new_size - size) // 2
|
125 |
-
ph1, pw1 = new_size - size - ph0, new_size - size - pw0
|
126 |
-
image = np.pad(
|
127 |
-
image,
|
128 |
-
((ph0, ph1), (pw0, pw1), (0, 0)),
|
129 |
-
mode="constant",
|
130 |
-
constant_values=((0, 0), (0, 0), (0, 0)),
|
131 |
-
)
|
132 |
-
image = Image.fromarray(image)
|
133 |
-
|
134 |
-
self.multi_view_diffusion = self.multi_view_diffusion.to(self._execution_device)
|
135 |
-
images = self.multi_view_diffusion(image).images[0]
|
136 |
-
|
137 |
-
images = np.asarray(images, dtype=np.float32) / 255.0
|
138 |
-
images = torch.from_numpy(images).permute(2, 0, 1).contiguous().float()
|
139 |
-
|
140 |
-
n, m = 3, 2
|
141 |
-
c, h, w = images.shape
|
142 |
-
images = (
|
143 |
-
images.view(c, n, h // n, m, w // m).permute(1, 3, 0, 2, 4).contiguous()
|
144 |
-
)
|
145 |
-
images = images.view(n * m, c, h // n, w // m)
|
146 |
-
|
147 |
-
images = images.unsqueeze(0)
|
148 |
-
images = v2.functional.resize(
|
149 |
-
images, 320, interpolation=3, antialias=True
|
150 |
-
).clamp(0, 1)"""
|
151 |
-
|
152 |
self.lrm.init_flexicubes_geometry(self._execution_device, fovy=30.0)
|
153 |
cameras = get_zero123plus_input_cameras().to(self._execution_device)
|
154 |
planes = self.lrm.forward_planes(images, cameras)
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
vertex_uv_pairs
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
self.register_modules(lrm=self.lrm)
|
98 |
|
99 |
@torch.no_grad()
|
100 |
+
def __call__(self, images: torch.Tensor, generate_texture: bool = False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
self.lrm.init_flexicubes_geometry(self._execution_device, fovy=30.0)
|
102 |
cameras = get_zero123plus_input_cameras().to(self._execution_device)
|
103 |
planes = self.lrm.forward_planes(images, cameras)
|
104 |
+
|
105 |
+
if generate_texture:
|
106 |
+
mesh_out = self.lrm.extract_mesh(
|
107 |
+
planes,
|
108 |
+
use_texture_map=True,
|
109 |
+
texture_resolution=1024,
|
110 |
+
)
|
111 |
+
vertices, vertex_indices, uvs, uv_indices, texture = mesh_out
|
112 |
+
|
113 |
+
vertices = vertices.cpu().numpy()
|
114 |
+
vertex_indices = vertex_indices.cpu().numpy()
|
115 |
+
uvs = uvs.cpu().numpy()
|
116 |
+
uv_indices = uv_indices.cpu().numpy()
|
117 |
+
texture = texture.permute(1, 2, 0).cpu().numpy()
|
118 |
+
|
119 |
+
vertex_indices_flat = vertex_indices.reshape(-1)
|
120 |
+
uv_indices_flat = uv_indices.reshape(-1)
|
121 |
+
vertex_uv_pairs = np.stack([vertex_indices_flat, uv_indices_flat], axis=1)
|
122 |
+
unique_pairs, unique_indices = np.unique(
|
123 |
+
vertex_uv_pairs, axis=0, return_inverse=True
|
124 |
+
)
|
125 |
+
|
126 |
+
vertices = vertices[unique_pairs[:, 0]]
|
127 |
+
uvs = uvs[unique_pairs[:, 1]]
|
128 |
+
faces = unique_indices.reshape(-1, 3)
|
129 |
+
|
130 |
+
lo, hi = 0, 1
|
131 |
+
img = np.asarray(texture, dtype=np.float32)
|
132 |
+
img = (img - lo) * (255 / (hi - lo))
|
133 |
+
img = img.clip(0, 255)
|
134 |
+
mask = np.sum(img.astype(np.float32), axis=-1, keepdims=True)
|
135 |
+
mask = (mask <= 3.0).astype(np.float32)
|
136 |
+
kernel = np.ones((3, 3), "uint8")
|
137 |
+
dilate_img = cv2.dilate(img, kernel, iterations=1)
|
138 |
+
img = img * (1 - mask) + dilate_img * mask
|
139 |
+
img = img.clip(0, 255).astype(np.uint8)
|
140 |
+
texture = np.ascontiguousarray(img[::-1, :, :])
|
141 |
+
|
142 |
+
return InstantMeshPipelineOutput(
|
143 |
+
vertices=vertices,
|
144 |
+
faces=faces,
|
145 |
+
uvs=uvs,
|
146 |
+
texture=texture,
|
147 |
+
)
|
148 |
+
else:
|
149 |
+
mesh_out = self.lrm.extract_mesh(
|
150 |
+
planes,
|
151 |
+
use_texture_map=False,
|
152 |
+
)
|
153 |
+
vertices, faces, _ = mesh_out
|
154 |
+
|
155 |
+
vertices = vertices.cpu().numpy()
|
156 |
+
faces = faces.cpu().numpy()
|
157 |
+
|
158 |
+
return InstantMeshPipelineOutput(
|
159 |
+
vertices=vertices,
|
160 |
+
faces=faces,
|
161 |
+
uvs=None,
|
162 |
+
texture=None,
|
163 |
+
)
|