Spaces:
Running
on
Zero
Running
on
Zero
File size: 33,333 Bytes
562fd4c 4a21e26 562fd4c cb6031d 562fd4c 10e02f0 562fd4c 939e89f 10e02f0 939e89f 10e02f0 562fd4c 704db5a 562fd4c 17777da 562fd4c 704db5a 562fd4c a02b691 70926c9 562fd4c a123370 562fd4c 2dc1d37 a02b691 704db5a a02b691 704db5a 90347e4 704db5a 90347e4 704db5a 3c16620 704db5a 90347e4 704db5a 90347e4 704db5a 90347e4 562fd4c 07f2831 3c16620 562fd4c fc8fe55 90347e4 fc8fe55 90347e4 fc8fe55 90347e4 fc8fe55 90347e4 70926c9 90347e4 fc8fe55 10e02f0 704db5a fc8fe55 90347e4 562fd4c ff72a0b 562fd4c cb4d057 562fd4c bfac6cd 416ea46 bfac6cd f00f91b 4191566 3c16620 bfac6cd 22160e6 bfac6cd f00f91b 75c72c3 f00f91b bfac6cd 4d1a7d2 900612e d9bd77d bfac6cd a9d6e82 3c16620 a9d6e82 6b80964 1ccdae3 336b7b8 2c81a56 d9bd77d a9d6e82 336b7b8 a9d6e82 3c16620 6b80964 a9d6e82 336b7b8 6b80964 a9d6e82 91c383d a9d6e82 a00a4d9 a9d6e82 3c16620 a9d6e82 1ccdae3 6b80964 e19267a 6e65e24 a9d6e82 75c72c3 a9d6e82 3c16620 6b80964 336b7b8 6b80964 a9d6e82 2dc1d37 fc8fe55 45ec3c0 fc8fe55 45ec3c0 fc8fe55 3d2230a fc8fe55 10e02f0 fc8fe55 c83d507 fc8fe55 10e02f0 79728e8 10e02f0 3d2230a 10e02f0 79728e8 10e02f0 bc0cf88 10e02f0 3d2230a 10e02f0 fc8fe55 562fd4c bfac6cd 093d444 bfac6cd 093d444 bfac6cd 093d444 bfac6cd 093d444 bfac6cd 093d444 6b95e7c bfac6cd 093d444 bfac6cd 093d444 5463b93 a9d6e82 3c16620 a9d6e82 3c16620 a9d6e82 bfac6cd a9d6e82 3c16620 a9d6e82 3c16620 a9d6e82 562fd4c fc8fe55 10e02f0 72f6757 10e02f0 72f6757 10e02f0 562fd4c 10e02f0 562fd4c 10e02f0 562fd4c bf2146f fc8fe55 10e02f0 562fd4c bf2146f fc8fe55 10e02f0 bf2146f 10e02f0 562fd4c 91c383d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
# Copyright 2024 Guangkai Xu, Zhejiang University. All rights reserved.
#
# Licensed under the CC0-1.0 license;
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://github.com/aim-uofa/GenPercept/blob/main/LICENSE
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# --------------------------------------------------------------------------
# This code is based on Marigold and diffusers codebases
# https://github.com/prs-eth/marigold
# https://github.com/huggingface/diffusers
# --------------------------------------------------------------------------
# If you find this code useful, we kindly ask you to cite our paper in your work.
# Please find bibtex at: https://github.com/aim-uofa/GenPercept#%EF%B8%8F-citation
# More information about the method can be found at https://github.com/aim-uofa/GenPercept
# --------------------------------------------------------------------------
from __future__ import annotations
import functools
import os
import tempfile
import warnings
import gradio as gr
import numpy as np
import spaces
import torch as torch
from PIL import Image
from gradio_imageslider import ImageSlider
from gradio_patches.examples import Examples
from genpercept.genpercept_pipeline import GenPerceptPipeline
from diffusers import (
DiffusionPipeline,
# UNet2DConditionModel,
AutoencoderKL,
)
from genpercept.models.custom_unet import CustomUNet2DConditionModel
from genpercept.customized_modules.ddim import DDIMSchedulerCustomized
warnings.filterwarnings(
"ignore", message=".*LoginButton created outside of a Blocks context.*"
)
default_image_processing_res = 768
default_image_reproducuble = True
def process_image_check(path_input):
if path_input is None:
raise gr.Error(
"Missing image in the first pane: upload a file or use one from the gallery below."
)
def process_depth(
pipe,
path_input,
processing_res=default_image_processing_res,
):
print('line 65', path_input)
name_base, name_ext = os.path.splitext(os.path.basename(path_input))
print(f"Processing image {name_base}{name_ext}")
path_output_dir = tempfile.mkdtemp()
path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_depth_fp32.npy")
path_out_vis = os.path.join(path_output_dir, f"{name_base}_depth_colored.png")
input_image = Image.open(path_input)
pipe_out = pipe(
input_image,
processing_res=processing_res,
batch_size=1 if processing_res == 0 else 0,
show_progress_bar=False,
mode='depth',
color_map='Spectral',
)
depth_pred = pipe_out.pred_np
depth_colored = pipe_out.pred_colored
np.save(path_out_fp32, depth_pred)
depth_colored.save(path_out_vis)
path_out_16bit = os.path.join(path_output_dir, f"{name_base}_depth_16bit.png")
depth_16bit = (depth_pred * 65535.0).astype(np.uint16)
Image.fromarray(depth_16bit).save(path_out_16bit, mode="I;16")
return (
[path_out_16bit, path_out_vis],
[path_out_16bit, path_out_fp32, path_out_vis],
)
def process_normal(
pipe,
path_input,
processing_res=default_image_processing_res,
):
name_base, name_ext = os.path.splitext(os.path.basename(path_input))
print(f"Processing image {name_base}{name_ext}")
path_output_dir = tempfile.mkdtemp()
path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_normal_fp32.npy")
path_out_vis = os.path.join(path_output_dir, f"{name_base}_normal_colored.png")
input_image = Image.open(path_input)
pipe_out = pipe(
input_image,
processing_res=processing_res,
batch_size=1 if processing_res == 0 else 0,
show_progress_bar=False,
mode='normal',
)
normal_pred = pipe_out.pred_np
normal_colored = pipe_out.pred_colored
np.save(path_out_fp32, normal_pred)
normal_colored.save(path_out_vis)
return (
[path_out_vis],
[path_out_fp32, path_out_vis],
)
def process_dis(
pipe,
path_input,
processing_res=default_image_processing_res,
):
name_base, name_ext = os.path.splitext(os.path.basename(path_input))
print(f"Processing image {name_base}{name_ext}")
path_output_dir = tempfile.mkdtemp()
path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_dis_fp32.npy")
path_out_vis = os.path.join(path_output_dir, f"{name_base}_dis_colored.png")
input_image = Image.open(path_input)
pipe_out = pipe(
input_image,
processing_res=processing_res,
batch_size=1 if processing_res == 0 else 0,
show_progress_bar=False,
mode='dis',
)
dis_pred = pipe_out.pred_np
dis_colored = pipe_out.pred_colored
np.save(path_out_fp32, dis_pred)
dis_colored.save(path_out_vis)
return (
[path_out_vis],
[path_out_fp32, path_out_vis],
)
def process_matting(
pipe,
path_input,
processing_res=default_image_processing_res,
):
name_base, name_ext = os.path.splitext(os.path.basename(path_input))
print(f"Processing image {name_base}{name_ext}")
path_output_dir = tempfile.mkdtemp()
path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_matting_fp32.npy")
path_out_vis = os.path.join(path_output_dir, f"{name_base}_matting_colored.png")
input_image = Image.open(path_input)
pipe_out = pipe(
input_image,
processing_res=processing_res,
batch_size=1 if processing_res == 0 else 0,
show_progress_bar=False,
mode='matting',
)
matting_pred = pipe_out.pred_np
matting_colored = pipe_out.pred_colored
np.save(path_out_fp32, matting_pred)
matting_colored.save(path_out_vis)
return (
[path_out_vis],
[path_out_fp32, path_out_vis],
)
def process_seg(
pipe,
path_input,
processing_res=default_image_processing_res,
):
name_base, name_ext = os.path.splitext(os.path.basename(path_input))
print(f"Processing image {name_base}{name_ext}")
path_output_dir = tempfile.mkdtemp()
path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_seg_fp32.npy")
path_out_vis = os.path.join(path_output_dir, f"{name_base}_seg_colored.png")
input_image = Image.open(path_input)
pipe_out = pipe(
input_image,
processing_res=processing_res,
batch_size=1 if processing_res == 0 else 0,
show_progress_bar=False,
mode='seg',
)
seg_pred = pipe_out.pred_np
seg_colored = pipe_out.pred_colored
np.save(path_out_fp32, seg_pred)
seg_colored.save(path_out_vis)
return (
[path_out_vis],
[path_out_fp32, path_out_vis],
)
def process_disparity(
pipe,
path_input,
processing_res=default_image_processing_res,
):
print('line 65', path_input)
name_base, name_ext = os.path.splitext(os.path.basename(path_input))
print(f"Processing image {name_base}{name_ext}")
path_output_dir = tempfile.mkdtemp()
path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_disparity_fp32.npy")
path_out_vis = os.path.join(path_output_dir, f"{name_base}_disparity_colored.png")
input_image = Image.open(path_input)
pipe_out = pipe(
input_image,
processing_res=processing_res,
batch_size=1 if processing_res == 0 else 0,
show_progress_bar=False,
mode='disparity',
color_map='Spectral',
)
disparity_pred = pipe_out.pred_np
disparity_colored = pipe_out.pred_colored
np.save(path_out_fp32, disparity_pred)
disparity_colored.save(path_out_vis)
path_out_16bit = os.path.join(path_output_dir, f"{name_base}_disparity_16bit.png")
disparity_16bit = (disparity_pred * 65535.0).astype(np.uint16)
Image.fromarray(disparity_16bit).save(path_out_16bit, mode="I;16")
return (
[path_out_16bit, path_out_vis],
[path_out_16bit, path_out_fp32, path_out_vis],
)
def run_demo_server(pipe_depth, pipe_normal, pipe_dis, pipe_matting, pipe_seg, pipe_disparity):
process_pipe_depth = spaces.GPU(functools.partial(process_depth, pipe_depth))
process_pipe_normal = spaces.GPU(functools.partial(process_normal, pipe_normal))
process_pipe_dis = spaces.GPU(functools.partial(process_dis, pipe_dis))
process_pipe_matting = spaces.GPU(functools.partial(process_matting, pipe_matting))
process_pipe_seg = spaces.GPU(functools.partial(process_seg, pipe_seg))
process_pipe_disparity = spaces.GPU(functools.partial(process_disparity, pipe_disparity))
gradio_theme = gr.themes.Default()
with gr.Blocks(
theme=gradio_theme,
title="GenPercept",
css="""
#download {
height: 118px;
}
.slider .inner {
width: 5px;
background: #FFF;
}
.viewport {
aspect-ratio: 4/3;
}
.tabs button.selected {
font-size: 20px !important;
color: crimson !important;
}
h1 {
text-align: center;
display: block;
}
h2 {
text-align: center;
display: block;
}
h3 {
text-align: center;
display: block;
}
.md_feedback li {
margin-bottom: 0px !important;
}
""",
head="""
<script async src="https://www.googletagmanager.com/gtag/js?id=G-1FWSVCGZTG"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-1FWSVCGZTG');
</script>
""",
) as demo:
gr.Markdown(
"""
# What Matters When Repurposing Diffusion Models for General Dense Perception Tasks?
# (GenPercept: Diffusion Models Trained with Large Data Are Transferable Visual Models)
<p align="center">
<a title="arXiv" href="https://arxiv.org/abs/2403.06090" target="_blank" rel="noopener noreferrer"
style="display: inline-block;">
<img src="https://www.obukhov.ai/img/badges/badge-pdf.svg">
</a>
<a title="Github" href="https://github.com/aim-uofa/GenPercept" target="_blank" rel="noopener noreferrer"
style="display: inline-block;">
<img src="https://img.shields.io/github/stars/aim-uofa/GenPercept?label=GitHub%20%E2%98%85&logo=github&color=C8C"
alt="badge-github-stars">
</a>
</p>
<p align="justify">
GenPercept is a one-step image perception generalist, which leverages the pretrained prior from stable diffusion models to estimate depth/surface normal/matting/segmentation with impressive details.
It achieves extremely fast inference speed and remarkable generalization capability on these fundamental vision perception tasks.
</p>
"""
)
with gr.Tabs(elem_classes=["tabs"]):
with gr.Tab("Depth"):
with gr.Row():
with gr.Column():
depth_image_input = gr.Image(
label="Input Image",
type="filepath",
# type="pil",
)
with gr.Row():
depth_image_submit_btn = gr.Button(
value="Estimate Depth", variant="primary"
)
depth_image_reset_btn = gr.Button(value="Reset")
with gr.Column():
depth_image_output_slider = ImageSlider(
label="Predicted depth of gray / color (red-near, blue-far)",
type="filepath",
show_download_button=True,
show_share_button=True,
interactive=False,
elem_classes="slider",
position=0.25,
)
depth_image_output_files = gr.Files(
label="Depth outputs",
elem_id="download",
interactive=False,
)
filenames = []
filenames.extend(["depth_anime_%d.jpg" %(i+1) for i in range(7)])
filenames.extend(["depth_line_%d.jpg" %(i+1) for i in range(6)])
filenames.extend(["depth_real_%d.jpg" %(i+1) for i in range(24)])
example_folder = os.path.join(os.path.dirname(__file__), "depth_images")
Examples(
fn=process_pipe_depth,
examples=[
os.path.join(example_folder, name)
for name in filenames
],
inputs=[depth_image_input],
outputs=[depth_image_output_slider, depth_image_output_files],
cache_examples=False,
# directory_name="examples_depth",
# cache_examples=False,
)
with gr.Tab("Normal"):
with gr.Row():
with gr.Column():
normal_image_input = gr.Image(
label="Input Image",
type="filepath",
)
with gr.Row():
normal_image_submit_btn = gr.Button(
value="Estimate Normal", variant="primary"
)
normal_image_reset_btn = gr.Button(value="Reset")
with gr.Column():
# normal_image_output_slider = ImageSlider(
# label="Predicted surface normal",
# type="filepath",
# show_download_button=True,
# show_share_button=True,
# interactive=False,
# elem_classes="slider",
# position=0.25,
# )
normal_image_output = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[1], height='auto')
normal_image_output_files = gr.Files(
label="Normal outputs",
elem_id="download",
interactive=False,
)
filenames = []
filenames.extend(["normal_%d.jpg" %(i+1) for i in range(10)])
# example_folder = "images"
# print(os.path.join(example_folder, '1.jpg'))
# example_folder = os.path.join(os.path.dirname(__file__), "images")
example_folder = os.path.join(os.path.dirname(__file__), "normal_images")
Examples(
fn=process_pipe_normal,
examples=[
os.path.join(example_folder, name)
for name in filenames
],
inputs=[normal_image_input],
outputs=[normal_image_output, normal_image_output_files],
# cache_examples=True,
# directory_name="examples_normal",
directory_name="images_cache",
cache_examples=False,
)
with gr.Tab("Dichotomous Segmentation"):
with gr.Row():
with gr.Column():
dis_image_input = gr.Image(
label="Input Image",
type="filepath",
)
with gr.Row():
dis_image_submit_btn = gr.Button(
value="Estimate Dichotomous Segmentation", variant="primary"
)
dis_image_reset_btn = gr.Button(value="Reset")
with gr.Column():
# dis_image_output_slider = ImageSlider(
# label="Predicted dichotomous image segmentation",
# type="filepath",
# show_download_button=True,
# show_share_button=True,
# interactive=False,
# elem_classes="slider",
# position=0.25,
# )
dis_image_output = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[1], height='auto')
dis_image_output_files = gr.Files(
label="DIS outputs",
elem_id="download",
interactive=False,
)
filenames = []
filenames.extend(["dis_%d.jpg" %(i+1) for i in range(10)])
# example_folder = "images"
# print('line 396', __file__)
example_folder = os.path.join(os.path.dirname(__file__), "dis_images")
# print(example_folder)
Examples(
fn=process_pipe_dis,
examples=[
os.path.join(example_folder, name)
for name in filenames
],
inputs=[dis_image_input],
outputs=[dis_image_output, dis_image_output_files],
# cache_examples=True,
directory_name="images_cache",
cache_examples=False,
)
with gr.Tab("Matting"):
with gr.Row():
with gr.Column():
matting_image_input = gr.Image(
label="Input Image",
type="filepath",
)
with gr.Row():
matting_image_submit_btn = gr.Button(
value="Estimate Matting", variant="primary"
)
matting_image_reset_btn = gr.Button(value="Reset")
with gr.Column():
# matting_image_output_slider = ImageSlider(
# label="Predicted matting image",
# type="filepath",
# show_download_button=True,
# show_share_button=True,
# interactive=False,
# elem_classes="slider",
# position=0.25,
# )
matting_image_output = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[1], height='auto')
matting_image_output_files = gr.Files(
label="Matting outputs",
elem_id="download",
interactive=False,
)
filenames = []
filenames.extend(["matting_%d.jpg" %i for i in range(5)])
example_folder = os.path.join(os.path.dirname(__file__), "matting_images")
# print(example_folder)
Examples(
fn=process_pipe_matting,
examples=[
os.path.join(example_folder, name)
for name in filenames
],
inputs=[matting_image_input],
outputs=[matting_image_output, matting_image_output_files],
# cache_examples=True,
directory_name="images_cache",
cache_examples=False,
)
with gr.Tab("Seg"):
with gr.Row():
with gr.Column():
seg_image_input = gr.Image(
label="Input Image",
type="filepath",
# type="pil",
)
with gr.Row():
seg_image_submit_btn = gr.Button(
value="Estimate Segmentation", variant="primary"
)
seg_image_reset_btn = gr.Button(value="Reset")
with gr.Column():
# seg_image_output_slider = ImageSlider(
# label="Predicted segmentation results",
# type="filepath",
# show_download_button=True,
# show_share_button=True,
# interactive=False,
# elem_classes="slider",
# position=0.25,
# )
seg_image_output = gr.Gallery(label='Output', show_label=False, elem_id="gallery", columns=[1], height='auto')
seg_image_output_files = gr.Files(
label="Seg outputs",
elem_id="download",
interactive=False,
)
filenames = []
filenames.extend(["seg_%d.jpg" %(i+1) for i in range(5)])
example_folder = os.path.join(os.path.dirname(__file__), "seg_images")
Examples(
fn=process_pipe_seg,
examples=[
os.path.join(example_folder, name)
for name in filenames
],
inputs=[seg_image_input],
outputs=[seg_image_output, seg_image_output_files],
cache_examples=False,
# directory_name="examples_depth",
# cache_examples=False,
)
with gr.Tab("Disparity"):
with gr.Row():
with gr.Column():
disparity_image_input = gr.Image(
label="Input Image",
type="filepath",
# type="pil",
)
with gr.Row():
disparity_image_submit_btn = gr.Button(
value="Estimate Disparity", variant="primary"
)
disparity_image_reset_btn = gr.Button(value="Reset")
with gr.Column():
disparity_image_output_slider = ImageSlider(
label="Predicted disparity results",
type="filepath",
show_download_button=True,
show_share_button=True,
interactive=False,
elem_classes="slider",
position=0.25,
)
disparity_image_output_files = gr.Files(
label="Disparity outputs",
elem_id="download",
interactive=False,
)
filenames = []
filenames.extend(["depth_anime_%d.jpg" %(i+1) for i in range(7)])
filenames.extend(["depth_line_%d.jpg" %(i+1) for i in range(6)])
filenames.extend(["depth_real_%d.jpg" %(i+1) for i in range(24)])
example_folder = os.path.join(os.path.dirname(__file__), "depth_images")
Examples(
fn=process_pipe_disparity,
examples=[
os.path.join(example_folder, name)
for name in filenames
],
inputs=[disparity_image_input],
outputs=[disparity_image_output_slider, disparity_image_output_files],
cache_examples=False,
# directory_name="examples_depth",
# cache_examples=False,
)
### Image tab
depth_image_submit_btn.click(
fn=process_image_check,
inputs=depth_image_input,
outputs=None,
preprocess=False,
queue=False,
).success(
fn=process_pipe_depth,
inputs=[
depth_image_input,
],
outputs=[depth_image_output_slider, depth_image_output_files],
concurrency_limit=1,
)
depth_image_reset_btn.click(
fn=lambda: (
None,
None,
None,
),
inputs=[],
outputs=[
depth_image_input,
depth_image_output_slider,
depth_image_output_files,
],
queue=False,
)
normal_image_submit_btn.click(
fn=process_image_check,
inputs=normal_image_input,
outputs=None,
preprocess=False,
queue=False,
).success(
fn=process_pipe_normal,
inputs=[
normal_image_input,
],
outputs=[normal_image_output, normal_image_output_files],
concurrency_limit=1,
)
normal_image_reset_btn.click(
fn=lambda: (
None,
None,
None,
),
inputs=[],
outputs=[
normal_image_input,
normal_image_output,
normal_image_output_files,
],
queue=False,
)
dis_image_submit_btn.click(
fn=process_image_check,
inputs=dis_image_input,
outputs=None,
preprocess=False,
queue=False,
).success(
fn=process_pipe_dis,
inputs=[
dis_image_input,
],
outputs=[dis_image_output, dis_image_output_files],
concurrency_limit=1,
)
dis_image_reset_btn.click(
fn=lambda: (
None,
None,
None,
),
inputs=[],
outputs=[
dis_image_input,
dis_image_output,
dis_image_output_files,
],
queue=False,
)
matting_image_submit_btn.click(
fn=process_image_check,
inputs=matting_image_input,
outputs=None,
preprocess=False,
queue=False,
).success(
fn=process_pipe_dis,
inputs=[
matting_image_input,
],
outputs=[matting_image_output, matting_image_output_files],
concurrency_limit=1,
)
matting_image_reset_btn.click(
fn=lambda: (
None,
None,
None,
),
inputs=[],
outputs=[
matting_image_input,
matting_image_output,
matting_image_output_files,
],
queue=False,
)
seg_image_submit_btn.click(
fn=process_image_check,
inputs=seg_image_input,
outputs=None,
preprocess=False,
queue=False,
).success(
fn=process_pipe_seg,
inputs=[
seg_image_input,
],
outputs=[seg_image_output, seg_image_output_files],
concurrency_limit=1,
)
seg_image_reset_btn.click(
fn=lambda: (
None,
None,
None,
),
inputs=[],
outputs=[
seg_image_input,
seg_image_output,
seg_image_output_files,
],
queue=False,
)
disparity_image_submit_btn.click(
fn=process_image_check,
inputs=disparity_image_input,
outputs=None,
preprocess=False,
queue=False,
).success(
fn=process_pipe_disparity,
inputs=[
disparity_image_input,
],
outputs=[disparity_image_output_slider, disparity_image_output_files],
concurrency_limit=1,
)
disparity_image_reset_btn.click(
fn=lambda: (
None,
None,
None,
),
inputs=[],
outputs=[
disparity_image_input,
disparity_image_output_slider,
disparity_image_output_files,
],
queue=False,
)
### Server launch
demo.queue(
api_open=False,
).launch(
server_name="0.0.0.0",
server_port=7860,
)
def main():
os.system("pip freeze")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# dtype = torch.float16
# variant = "fp16"
dtype = torch.float32
variant = None
unet_depth_v2 = CustomUNet2DConditionModel.from_pretrained('guangkaixu/GenPercept-models', subfolder="unet_depth_v2", use_safetensors=True).to(dtype)
unet_normal_v2 = CustomUNet2DConditionModel.from_pretrained('guangkaixu/GenPercept-models', subfolder="unet_normal_v2", use_safetensors=True).to(dtype)
unet_dis_v2 = CustomUNet2DConditionModel.from_pretrained('guangkaixu/GenPercept-models', subfolder="unet_dis_v2", use_safetensors=True).to(dtype)
unet_matting_v2 = CustomUNet2DConditionModel.from_pretrained('guangkaixu/GenPercept-models', subfolder="unet_matting_v2", use_safetensors=True).to(dtype)
unet_disparity_v2 = CustomUNet2DConditionModel.from_pretrained('guangkaixu/GenPercept-models', subfolder="unet_disparity_v2", use_safetensors=True).to(dtype)
unet_seg_v2 = CustomUNet2DConditionModel.from_pretrained('guangkaixu/GenPercept-models', subfolder="unet_seg_v2", use_safetensors=True).to(dtype)
scheduler = DDIMSchedulerCustomized.from_pretrained("hf_configs/scheduler_beta_1.0_1.0", subfolder='scheduler')
genpercept_pipeline = True
pre_loaded_dict = dict(
scheduler=scheduler,
genpercept_pipeline=genpercept_pipeline,
torch_dtype=dtype,
variant=variant,
)
pipe_depth = GenPerceptPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", unet=unet_depth_v2, **pre_loaded_dict,
)
pipe_normal = GenPerceptPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", unet=unet_normal_v2, **pre_loaded_dict,
)
pipe_dis = GenPerceptPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", unet=unet_dis_v2, **pre_loaded_dict,
)
pipe_matting = GenPerceptPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", unet=unet_matting_v2, **pre_loaded_dict,
)
pipe_seg = GenPerceptPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", unet=unet_seg_v2, **pre_loaded_dict,
)
pipe_disparity = GenPerceptPipeline.from_pretrained(
"stabilityai/stable-diffusion-2-1", unet=unet_disparity_v2, **pre_loaded_dict,
)
try:
import xformers
pipe_depth.enable_xformers_memory_efficient_attention()
pipe_normal.enable_xformers_memory_efficient_attention()
pipe_dis.enable_xformers_memory_efficient_attention()
pipe_matting.enable_xformers_memory_efficient_attention()
pipe_seg.enable_xformers_memory_efficient_attention()
pipe_disparity.enable_xformers_memory_efficient_attention()
except:
pass # run without xformers
pipe_depth = pipe_depth.to(device)
pipe_normal = pipe_normal.to(device)
pipe_dis = pipe_dis.to(device)
pipe_matting = pipe_matting.to(device)
pipe_seg = pipe_seg.to(device)
pipe_disparity = pipe_disparity.to(device)
run_demo_server(pipe_depth, pipe_normal, pipe_dis, pipe_matting, pipe_seg, pipe_disparity)
if __name__ == "__main__":
main() |