Spaces:
Running on Zero
Running on Zero
Fix: Remove global gamma, bake BiRefNet model, optimize Docker for startup
Browse files- Dockerfile +5 -2
- download_models.py +22 -0
- trellis/utils/postprocessing_utils.py +4 -10
Dockerfile
CHANGED
|
@@ -62,8 +62,11 @@ RUN wget -P assets/example_image https://raw.githubusercontent.com/trellis3d/tre
|
|
| 62 |
&& wget -P assets/example_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_image/cherries_1.png \
|
| 63 |
&& wget -P assets/example_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_image/dog_1.png \
|
| 64 |
&& wget -P assets/example_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_image/hotdog_1.png \
|
| 65 |
-
&& wget -P assets/example_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_image/mushroom_1.png \
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
RUN wget -P assets/example_multi_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_multi_image/chair_1.png \
|
| 69 |
&& wget -P assets/example_multi_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_multi_image/chair_2.png \
|
|
|
|
| 62 |
&& wget -P assets/example_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_image/cherries_1.png \
|
| 63 |
&& wget -P assets/example_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_image/dog_1.png \
|
| 64 |
&& wget -P assets/example_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_image/hotdog_1.png \
|
| 65 |
+
&& wget -P assets/example_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_image/mushroom_1.png \ && wget -P assets/example_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_image/soccer_1.png
|
| 66 |
+
|
| 67 |
+
# Bake BiRefNet model into the image (prevents slow runtime download)
|
| 68 |
+
COPY download_models.py .
|
| 69 |
+
RUN python3 download_models.py
|
| 70 |
|
| 71 |
RUN wget -P assets/example_multi_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_multi_image/chair_1.png \
|
| 72 |
&& wget -P assets/example_multi_image https://raw.githubusercontent.com/trellis3d/trellis/main/assets/example_multi_image/chair_2.png \
|
download_models.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModelForImageSegmentation
|
| 5 |
+
|
| 6 |
+
def download_birefnet():
|
| 7 |
+
print("⬇️ Downloading BiRefNet model for baking...")
|
| 8 |
+
try:
|
| 9 |
+
# This will download the model to the huggingface cache directory
|
| 10 |
+
# which is persisted in the Docker image if done during build
|
| 11 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 12 |
+
'ZhengPeng7/BiRefNet',
|
| 13 |
+
trust_remote_code=True
|
| 14 |
+
)
|
| 15 |
+
print("✅ BiRefNet model downloaded successfully.")
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"❌ Failed to download BiRefNet model: {e}")
|
| 18 |
+
# Investigate if we need a workaround or specific cache dir
|
| 19 |
+
raise e
|
| 20 |
+
|
| 21 |
+
if __name__ == "__main__":
|
| 22 |
+
download_birefnet()
|
trellis/utils/postprocessing_utils.py
CHANGED
|
@@ -337,11 +337,8 @@ def bake_texture(
|
|
| 337 |
mask = texture_weights > 0
|
| 338 |
texture[mask] /= texture_weights[mask][:, None]
|
| 339 |
|
| 340 |
-
# 🔧
|
| 341 |
-
|
| 342 |
-
texture_linear = texture.reshape(texture_size, texture_size, 3).cpu().numpy()
|
| 343 |
-
texture_srgb = np.power(np.clip(texture_linear, 0, 1), 1.0/2.2) # Gamma 2.2 correction
|
| 344 |
-
texture = np.clip(texture_srgb * 255, 0, 255).astype(np.uint8)
|
| 345 |
|
| 346 |
# inpaint
|
| 347 |
mask = (texture_weights == 0).cpu().numpy().astype(np.uint8).reshape(texture_size, texture_size)
|
|
@@ -391,11 +388,8 @@ def bake_texture(
|
|
| 391 |
pbar.set_postfix({'loss': loss.item()})
|
| 392 |
pbar.update()
|
| 393 |
|
| 394 |
-
# 🔧
|
| 395 |
-
|
| 396 |
-
texture_linear = texture[0].flip(0).detach().cpu().numpy()
|
| 397 |
-
texture_srgb = np.power(np.clip(texture_linear, 0, 1), 1.0/2.2) # Gamma 2.2 correction
|
| 398 |
-
texture = np.clip(texture_srgb * 255, 0, 255).astype(np.uint8)
|
| 399 |
mask = 1 - utils3d.torch.rasterize_triangle_faces(
|
| 400 |
rastctx, (uvs * 2 - 1)[None], faces, texture_size, texture_size
|
| 401 |
)['mask'][0].detach().cpu().numpy().astype(np.uint8)
|
|
|
|
| 337 |
mask = texture_weights > 0
|
| 338 |
texture[mask] /= texture_weights[mask][:, None]
|
| 339 |
|
| 340 |
+
# 🔧 Gamma correction REMOVED as per user request (relying on SH boost instead)
|
| 341 |
+
texture = np.clip(texture.reshape(texture_size, texture_size, 3).cpu().numpy() * 255, 0, 255).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
# inpaint
|
| 344 |
mask = (texture_weights == 0).cpu().numpy().astype(np.uint8).reshape(texture_size, texture_size)
|
|
|
|
| 388 |
pbar.set_postfix({'loss': loss.item()})
|
| 389 |
pbar.update()
|
| 390 |
|
| 391 |
+
# 🔧 Gamma correction REMOVED as per user request (relying on SH boost instead)
|
| 392 |
+
texture = np.clip(texture[0].flip(0).detach().cpu().numpy() * 255, 0, 255).astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
| 393 |
mask = 1 - utils3d.torch.rasterize_triangle_faces(
|
| 394 |
rastctx, (uvs * 2 - 1)[None], faces, texture_size, texture_size
|
| 395 |
)['mask'][0].detach().cpu().numpy().astype(np.uint8)
|