Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import matplotlib
|
|
@@ -10,11 +15,9 @@ import tempfile
|
|
| 10 |
from gradio_imageslider import ImageSlider
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
|
| 13 |
-
# from depth_anything_v2.dpt import DepthAnythingV2
|
| 14 |
from Marigold.marigold import MarigoldPipeline
|
| 15 |
from diffusers import AutoencoderKL, DDIMScheduler, UNet2DConditionModel
|
| 16 |
from transformers import CLIPTextModel, CLIPTokenizer
|
| 17 |
-
# import xformers
|
| 18 |
|
| 19 |
css = """
|
| 20 |
#img-display-container {
|
|
@@ -48,39 +51,12 @@ pipe = MarigoldPipeline.from_pretrained(pretrained_model_name_or_path = checkpoi
|
|
| 48 |
variant=variant,
|
| 49 |
torch_dtype=dtype,
|
| 50 |
)
|
| 51 |
-
# try:
|
| 52 |
-
# pipe.enable_xformers_memory_efficient_attention()
|
| 53 |
-
# except ImportError:
|
| 54 |
-
# pass # run without xformers
|
| 55 |
pipe = pipe.to(DEVICE)
|
| 56 |
pipe.unet.eval()
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# 'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
|
| 62 |
-
# 'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
|
| 63 |
-
# }
|
| 64 |
-
# encoder2name = {
|
| 65 |
-
# 'vits': 'Small',
|
| 66 |
-
# 'vitb': 'Base',
|
| 67 |
-
# 'vitl': 'Large',
|
| 68 |
-
# 'vitg': 'Giant', # we are undergoing company review procedures to release our giant model checkpoint
|
| 69 |
-
# }
|
| 70 |
-
# encoder = 'vitl'
|
| 71 |
-
# model_name = encoder2name[encoder]
|
| 72 |
-
# model = DepthAnythingV2(**model_configs[encoder])
|
| 73 |
-
# filepath = hf_hub_download(repo_id=f"depth-anything/Depth-Anything-V2-{model_name}", filename=f"depth_anything_v2_{encoder}.pth", repo_type="model")
|
| 74 |
-
# state_dict = torch.load(filepath, map_location="cpu")
|
| 75 |
-
# model.load_state_dict(state_dict)
|
| 76 |
-
# model = model.to(DEVICE).eval()
|
| 77 |
-
|
| 78 |
-
title = "# ..."
|
| 79 |
-
description = """... **...**"""
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# def predict_depth(image):
|
| 83 |
-
# return model.infer_image(image)
|
| 84 |
|
| 85 |
@spaces.GPU
|
| 86 |
def predict_depth(image, processing_res_choice):
|
|
@@ -112,7 +88,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 112 |
|
| 113 |
gray_depth_file = gr.File(label="Grayscale depth map", elem_id="download",)
|
| 114 |
raw_file = gr.File(label="Raw Depth Data (.npy)", elem_id="download")
|
| 115 |
-
# raw_file = gr.File(label="16-bit raw output (can be considered as disparity)", elem_id="download",)
|
| 116 |
|
| 117 |
cmap = matplotlib.colormaps.get_cmap('Spectral_r')
|
| 118 |
|
|
@@ -140,24 +115,6 @@ with gr.Blocks(css=css) as demo:
|
|
| 140 |
|
| 141 |
return [(image, depth_colored), tmp_gray_depth.name, tmp_npy_depth.name]
|
| 142 |
|
| 143 |
-
# h, w = image.shape[:2]
|
| 144 |
-
|
| 145 |
-
# depth = predict_depth(image[:, :, ::-1])
|
| 146 |
-
|
| 147 |
-
# raw_depth = Image.fromarray(depth.astype('uint16'))
|
| 148 |
-
# tmp_raw_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
| 149 |
-
# raw_depth.save(tmp_raw_depth.name)
|
| 150 |
-
|
| 151 |
-
# depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
| 152 |
-
# depth = depth.astype(np.uint8)
|
| 153 |
-
# colored_depth = (cmap(depth)[:, :, :3] * 255).astype(np.uint8)
|
| 154 |
-
|
| 155 |
-
# gray_depth = Image.fromarray(depth)
|
| 156 |
-
# tmp_gray_depth = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
| 157 |
-
# gray_depth.save(tmp_gray_depth.name)
|
| 158 |
-
|
| 159 |
-
# return [(original_image, colored_depth), tmp_gray_depth.name, tmp_raw_depth.name]
|
| 160 |
-
|
| 161 |
submit.click(on_submit, inputs=[input_image, processing_res_choice], outputs=[depth_image_slider, gray_depth_file, raw_file])
|
| 162 |
|
| 163 |
example_files = os.listdir('assets/examples')
|
|
|
|
| 1 |
+
###########################################################################################
|
| 2 |
+
# Code based on the Hugging Face Space of Depth Anything v2
|
| 3 |
+
# https://huggingface.co/spaces/depth-anything/Depth-Anything-V2/blob/main/app.py
|
| 4 |
+
###########################################################################################
|
| 5 |
+
|
| 6 |
import gradio as gr
|
| 7 |
import cv2
|
| 8 |
import matplotlib
|
|
|
|
| 15 |
from gradio_imageslider import ImageSlider
|
| 16 |
from huggingface_hub import hf_hub_download
|
| 17 |
|
|
|
|
| 18 |
from Marigold.marigold import MarigoldPipeline
|
| 19 |
from diffusers import AutoencoderKL, DDIMScheduler, UNet2DConditionModel
|
| 20 |
from transformers import CLIPTextModel, CLIPTokenizer
|
|
|
|
| 21 |
|
| 22 |
css = """
|
| 23 |
#img-display-container {
|
|
|
|
| 51 |
variant=variant,
|
| 52 |
torch_dtype=dtype,
|
| 53 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
pipe = pipe.to(DEVICE)
|
| 55 |
pipe.unet.eval()
|
| 56 |
|
| 57 |
+
|
| 58 |
+
title = "# End-to-End Fine-Tuned Marigold for Depth Estimation"
|
| 59 |
+
description = """ Please refer to our [paper](https://arxiv.org/abs/2409.11355) and [GitHub](https://vision.rwth-aachen.de/diffusion-e2e-ft) for more details."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
@spaces.GPU
|
| 62 |
def predict_depth(image, processing_res_choice):
|
|
|
|
| 88 |
|
| 89 |
gray_depth_file = gr.File(label="Grayscale depth map", elem_id="download",)
|
| 90 |
raw_file = gr.File(label="Raw Depth Data (.npy)", elem_id="download")
|
|
|
|
| 91 |
|
| 92 |
cmap = matplotlib.colormaps.get_cmap('Spectral_r')
|
| 93 |
|
|
|
|
| 115 |
|
| 116 |
return [(image, depth_colored), tmp_gray_depth.name, tmp_npy_depth.name]
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
submit.click(on_submit, inputs=[input_image, processing_res_choice], outputs=[depth_image_slider, gray_depth_file, raw_file])
|
| 119 |
|
| 120 |
example_files = os.listdir('assets/examples')
|