-
-
Save AlexeyLugovoy/3b7d147adb1e0b8633ed22df0bb059c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from transformers import DPTImageProcessor, DPTForDepthEstimation | |
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL | |
processor = DPTImageProcessor.from_pretrained("Intel/dpt-beit-large-512", cache_dir=CACHE_DIR) | |
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-beit-large-512", cache_dir=CACHE_DIR)#.to(DEVICE) | |
# prepare image for the model | |
inputs = processor(images=img_pil, return_tensors="pt") | |
with torch.no_grad(): | |
outputs = model(**inputs) | |
predicted_depth = outputs.predicted_depth | |
# interpolate to original size | |
prediction = torch.nn.functional.interpolate( | |
predicted_depth.unsqueeze(1), | |
size=img_pil.size[::-1], | |
mode="bicubic", | |
align_corners=False, | |
) | |
# visualize the prediction | |
output = prediction.squeeze().cpu().numpy() | |
formatted = (output * 255 / np.max(output)).astype("uint8") | |
img_depth = Image.fromarray(formatted) | |
controlnet = ControlNetModel.from_pretrained( | |
"diffusers/controlnet-depth-sdxl-1.0", | |
variant="fp16", | |
use_safetensors=True, | |
torch_dtype=torch.float16, | |
cache_dir=CACHE_DIR | |
).to(DEVICE) | |
pipe = StableDiffusionXLControlNetPipeline.from_pretrained( | |
"stabilityai/stable-diffusion-xl-base-1.0", | |
controlnet=controlnet, | |
variant="fp16", | |
use_safetensors=True, | |
torch_dtype=torch.float16, | |
cache_dir=CACHE_DIR | |
).to(DEVICE) | |
prompt = ["New modern style of livingroom, warm color palette, detailed, 8k"] | |
negative_prompt = ["low quality, bad quality, sketches"] | |
img_gen = pipe( | |
prompt=prompt, | |
negative_prompt=negative_prompt, | |
image=img_depth, | |
guidance_scale=13, | |
num_images_per_prompt=1, | |
num_inference_steps=100, | |
controlnet_conditioning_scale=0.5, | |
generator = torch.Generator(DEVICE).manual_seed(8) | |
) | |
img_gen.images[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment