Spaces:
Configuration error
Configuration error
Commit
·
d7aa376
1
Parent(s):
b4e7a1c
update
Browse files
app.py
CHANGED
|
@@ -4,6 +4,18 @@ from torchvision import transforms
|
|
| 4 |
from SDXL.diff_pipe import StableDiffusionXLDiffImg2ImgPipeline
|
| 5 |
from diffusers import DPMSolverMultistepScheduler
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
NUM_INFERENCE_STEPS = 50
|
| 8 |
dtype = torch.float16
|
| 9 |
if torch.cuda.is_available():
|
|
@@ -15,6 +27,9 @@ else:
|
|
| 15 |
device = "cpu"
|
| 16 |
#device = "cuda"
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
base = StableDiffusionXLDiffImg2ImgPipeline.from_pretrained(
|
| 19 |
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=dtype, variant="fp16", use_safetensors=True
|
| 20 |
)
|
|
@@ -32,6 +47,46 @@ base.scheduler = DPMSolverMultistepScheduler.from_config(base.scheduler.config)
|
|
| 32 |
refiner.scheduler = DPMSolverMultistepScheduler.from_config(base.scheduler.config)
|
| 33 |
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def preprocess_image(image):
|
| 36 |
image = image.convert("RGB")
|
| 37 |
image = transforms.CenterCrop((image.size[1] // 64 * 64, image.size[0] // 64 * 64))(image)
|
|
@@ -78,12 +133,12 @@ def validate_inputs(image, map):
|
|
| 78 |
raise gr.Error("Missing map")
|
| 79 |
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
with gr.Blocks() as demo:
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Column():
|
|
@@ -98,9 +153,11 @@ with gr.Blocks() as demo:
|
|
| 98 |
run_btn = gr.Button("Run",variant="primary")
|
| 99 |
|
| 100 |
output = gr.Image(label="Output Image")
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
| 104 |
clr_btn.add(output)
|
| 105 |
if __name__ == "__main__":
|
| 106 |
demo.launch()
|
|
|
|
| 4 |
from SDXL.diff_pipe import StableDiffusionXLDiffImg2ImgPipeline
|
| 5 |
from diffusers import DPMSolverMultistepScheduler
|
| 6 |
|
| 7 |
+
# DepthAnything
|
| 8 |
+
import cv2
|
| 9 |
+
import numpy as np
|
| 10 |
+
import os
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import torch.nn.functional as F
|
| 13 |
+
from torchvision.transforms import Compose
|
| 14 |
+
import tempfile
|
| 15 |
+
from gradio_imageslider import ImageSlider
|
| 16 |
+
from depth_anything.dpt import DepthAnything
|
| 17 |
+
from depth_anything.util.transform import Resize, NormalizeImage, PrepareForNet
|
| 18 |
+
|
| 19 |
NUM_INFERENCE_STEPS = 50
|
| 20 |
dtype = torch.float16
|
| 21 |
if torch.cuda.is_available():
|
|
|
|
| 27 |
device = "cpu"
|
| 28 |
#device = "cuda"
|
| 29 |
|
| 30 |
+
encoder = 'vitl' # can also be 'vitb' or 'vitl'
|
| 31 |
+
model = DepthAnything.from_pretrained(f"LiheYoung/depth_anything_{encoder}14").to(DEVICE).eval()
|
| 32 |
+
|
| 33 |
base = StableDiffusionXLDiffImg2ImgPipeline.from_pretrained(
|
| 34 |
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=dtype, variant="fp16", use_safetensors=True
|
| 35 |
)
|
|
|
|
| 47 |
refiner.scheduler = DPMSolverMultistepScheduler.from_config(base.scheduler.config)
|
| 48 |
|
| 49 |
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# DepthAnything
|
| 63 |
+
@torch.no_grad()
|
| 64 |
+
def predict_depth(model, image):
|
| 65 |
+
return model(image)
|
| 66 |
+
|
| 67 |
+
def depthify(image):
|
| 68 |
+
original_image = image.copy()
|
| 69 |
+
h, w = image.shape[:2]
|
| 70 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) / 255.0
|
| 71 |
+
image = transform({'image': image})['image']
|
| 72 |
+
image = torch.from_numpy(image).unsqueeze(0).to(DEVICE)
|
| 73 |
+
depth = predict_depth(model, image)
|
| 74 |
+
depth = F.interpolate(depth[None], (h, w), mode='bilinear', align_corners=False)[0, 0]
|
| 75 |
+
raw_depth = Image.fromarray(depth.cpu().numpy().astype('uint8'))
|
| 76 |
+
tmp = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
|
| 77 |
+
raw_depth.save(tmp.name)
|
| 78 |
+
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255.0
|
| 79 |
+
depth = depth.cpu().numpy().astype(np.uint8)
|
| 80 |
+
colored_depth = cv2.applyColorMap(depth, cv2.COLORMAP_INFERNO)[:, :, ::-1]
|
| 81 |
+
return [(original_image, colored_depth), tmp.name, raw_depth]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# DifferentialDiffusion
|
| 89 |
+
|
| 90 |
def preprocess_image(image):
|
| 91 |
image = image.convert("RGB")
|
| 92 |
image = transforms.CenterCrop((image.size[1] // 64 * 64, image.size[0] // 64 * 64))(image)
|
|
|
|
| 133 |
raise gr.Error("Missing map")
|
| 134 |
|
| 135 |
|
| 136 |
+
def run(image, gs, prompt, neg_prompt):
|
| 137 |
+
# first run
|
| 138 |
+
[(original_image, colored_depth), name, raw_depth] = depthify(image)
|
| 139 |
+
print(f"original_image={original_image} colored_depth={colored_depth}, name={name}, raw_depth={raw_depth}")
|
| 140 |
+
return inference(original_image, raw_depth, gs, prompt, neg_prompt)
|
| 141 |
+
|
| 142 |
with gr.Blocks() as demo:
|
| 143 |
with gr.Row():
|
| 144 |
with gr.Column():
|
|
|
|
| 153 |
run_btn = gr.Button("Run",variant="primary")
|
| 154 |
|
| 155 |
output = gr.Image(label="Output Image")
|
| 156 |
+
run_btn.click(
|
| 157 |
+
inference,
|
| 158 |
+
inputs=[input_image, change_map, gs, prompt, neg_prompt],
|
| 159 |
+
outputs=output
|
| 160 |
+
)
|
| 161 |
clr_btn.add(output)
|
| 162 |
if __name__ == "__main__":
|
| 163 |
demo.launch()
|