Spaces:
Sleeping
Sleeping
xco2
commited on
Commit
·
84bd5f3
1
Parent(s):
eee3139
分开两个tag,增加examples
Browse files- app.py +82 -18
- example.jpg +0 -0
- requirements.txt +2 -1
app.py
CHANGED
@@ -419,7 +419,10 @@ def merge_images(images: np.ndarray):
|
|
419 |
|
420 |
def get_models(device):
|
421 |
def modelLoad(model, model_path, data_parallel=False):
|
422 |
-
|
|
|
|
|
|
|
423 |
|
424 |
if data_parallel:
|
425 |
model = torch.nn.DataParallel(model)
|
@@ -461,10 +464,11 @@ def get_models(device):
|
|
461 |
|
462 |
def init_webui(unet, vae, normal_t):
|
463 |
# 定义回调函数
|
464 |
-
def process_image(input_image_value, noise_step, step_value, batch_size, sampler_name, img_size,
|
465 |
progress=gr.Progress()):
|
466 |
progress(0, desc="开始...")
|
467 |
|
|
|
468 |
noise_step = float(noise_step)
|
469 |
step_value = int(step_value)
|
470 |
batch_size = int(batch_size)
|
@@ -481,7 +485,8 @@ def init_webui(unet, vae, normal_t):
|
|
481 |
if input_image_value is None:
|
482 |
looper = sampler.sample_loop(unet, vae.middle_c, batch_size, step_value, shape=img_size, eta=1.)
|
483 |
else:
|
484 |
-
input_image_value = Image.fromarray(input_image_value).resize(img_size,
|
|
|
485 |
input_image_value = np.array(input_image_value, dtype=np.float32) / 255.
|
486 |
input_image_value = np.transpose(input_image_value, (2, 0, 1))
|
487 |
input_image_value = torch.Tensor([input_image_value]).to(device)
|
@@ -504,34 +509,93 @@ def init_webui(unet, vae, normal_t):
|
|
504 |
|
505 |
return output
|
506 |
|
507 |
-
|
508 |
-
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
|
|
|
|
|
|
516 |
# 选择sampler
|
517 |
-
|
518 |
# 创建滑动条组件
|
519 |
-
|
520 |
-
|
521 |
-
|
|
|
522 |
# 创建开始按钮组件
|
523 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
524 |
# 创建输出组件
|
525 |
output_images = gr.Gallery(show_label=False, height=400, columns=5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
526 |
|
527 |
-
start_button.click(process_image,
|
|
|
528 |
[output_images])
|
|
|
|
|
529 |
|
530 |
return iface
|
531 |
|
532 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
533 |
if __name__ == '__main__':
|
534 |
device = torch.device('cpu')
|
|
|
535 |
unet, vae, normal_t = get_models(device)
|
536 |
|
537 |
|
|
|
419 |
|
420 |
def get_models(device):
|
421 |
def modelLoad(model, model_path, data_parallel=False):
|
422 |
+
if str(device) == "cuda":
|
423 |
+
model.load_state_dict(torch.load(model_path), strict=True)
|
424 |
+
else:
|
425 |
+
model.load_state_dict(torch.load(model_path, map_location='cpu'), strict=True)
|
426 |
|
427 |
if data_parallel:
|
428 |
model = torch.nn.DataParallel(model)
|
|
|
464 |
|
465 |
def init_webui(unet, vae, normal_t):
|
466 |
# 定义回调函数
|
467 |
+
def process_image(input_image_value, noise_step, step_value, batch_size, sampler_name, img_size, random_seed,
|
468 |
progress=gr.Progress()):
|
469 |
progress(0, desc="开始...")
|
470 |
|
471 |
+
setup_seed(int(random_seed))
|
472 |
noise_step = float(noise_step)
|
473 |
step_value = int(step_value)
|
474 |
batch_size = int(batch_size)
|
|
|
485 |
if input_image_value is None:
|
486 |
looper = sampler.sample_loop(unet, vae.middle_c, batch_size, step_value, shape=img_size, eta=1.)
|
487 |
else:
|
488 |
+
input_image_value = Image.fromarray(input_image_value).resize((img_size[0] * 8, img_size[1] * 8),
|
489 |
+
Image.ANTIALIAS)
|
490 |
input_image_value = np.array(input_image_value, dtype=np.float32) / 255.
|
491 |
input_image_value = np.transpose(input_image_value, (2, 0, 1))
|
492 |
input_image_value = torch.Tensor([input_image_value]).to(device)
|
|
|
509 |
|
510 |
return output
|
511 |
|
512 |
+
def process_image_u(step_value, batch_size, sampler_name, img_size, random_seed,
|
513 |
+
progress=gr.Progress()):
|
514 |
+
return process_image(None, 0, step_value, batch_size, sampler_name, img_size, random_seed,
|
515 |
+
progress)
|
516 |
+
|
517 |
+
with gr.Blocks() as iface:
|
518 |
+
gr.Markdown(
|
519 |
+
"This is a diffusion model for generating second-dimensional avatars, which can be used for unconditional generation or image-to-image generation.")
|
520 |
+
|
521 |
+
with gr.Tab(label="unconditional generation"):
|
522 |
+
with gr.Column():
|
523 |
+
with gr.Row():
|
524 |
# 选择sampler
|
525 |
+
sampler_name_u = gr.Dropdown(["DDIM"], label="sampler", value="DDIM") # , "euler a", "dpmpp 2m"
|
526 |
# 创建滑动条组件
|
527 |
+
step_u = gr.Slider(minimum=1, maximum=1000, value=40, label="steps", step=1)
|
528 |
+
batch_size_u = gr.Slider(minimum=1, maximum=4, label="batch size", step=1)
|
529 |
+
img_size_u = gr.Slider(minimum=256, maximum=512, value=256, label="img size", step=64)
|
530 |
+
ramdom_seed_u = gr.Number(value=-1, label="ramdom seed(-1 as random number)")
|
531 |
# 创建开始按钮组件
|
532 |
+
start_button_u = gr.Button(label="Run")
|
533 |
+
# 创建输出组件
|
534 |
+
output_images_u = gr.Gallery(show_label=False, height=400, columns=5)
|
535 |
+
gr.Examples(
|
536 |
+
examples=[["DDIM", 40, 2, 256, 255395]],
|
537 |
+
inputs=[sampler_name_u, step_u, batch_size_u, img_size_u, ramdom_seed_u],
|
538 |
+
outputs=output_images_u,
|
539 |
+
fn=process_image_u,
|
540 |
+
# cache_examples=True,
|
541 |
+
)
|
542 |
+
with gr.Tab(label="image to image"):
|
543 |
+
with gr.Column():
|
544 |
+
with gr.Row():
|
545 |
+
with gr.Column():
|
546 |
+
# 创建输入组件
|
547 |
+
input_image = gr.Image(label="image to image")
|
548 |
+
# 加噪程度
|
549 |
+
noise_step = gr.Slider(minimum=0.05, maximum=1, value=0.6, label="加噪程度", step=0.01)
|
550 |
+
with gr.Column():
|
551 |
+
# 选择sampler
|
552 |
+
sampler_name = gr.Dropdown(["DDIM"], label="sampler", value="DDIM") # , "euler a", "dpmpp 2m"
|
553 |
+
# 创建滑动条组件
|
554 |
+
step = gr.Slider(minimum=1, maximum=1000, value=40, label="steps", step=1)
|
555 |
+
batch_size = gr.Slider(minimum=1, maximum=4, label="batch size", step=1)
|
556 |
+
img_size = gr.Slider(minimum=256, maximum=512, value=256, label="img size", step=64)
|
557 |
+
ramdom_seed = gr.Number(value=-1, label="ramdom seed(-1 as random number)")
|
558 |
+
# 创建开始按钮组件
|
559 |
+
start_button = gr.Button(label="Run")
|
560 |
+
|
561 |
# 创建输出组件
|
562 |
output_images = gr.Gallery(show_label=False, height=400, columns=5)
|
563 |
+
gr.Examples(
|
564 |
+
examples=[["./example.jpg", 0.4, "DDIM", 60, 4, 320, 231324]], # 224477,378754
|
565 |
+
inputs=[input_image, noise_step, sampler_name, step, batch_size, img_size, ramdom_seed],
|
566 |
+
outputs=output_images,
|
567 |
+
fn=process_image,
|
568 |
+
# cache_examples=True,
|
569 |
+
)
|
570 |
|
571 |
+
start_button.click(process_image,
|
572 |
+
[input_image, noise_step, step, batch_size, sampler_name, img_size, ramdom_seed],
|
573 |
[output_images])
|
574 |
+
start_button_u.click(process_image_u, [step_u, batch_size_u, sampler_name_u, img_size_u, ramdom_seed_u],
|
575 |
+
[output_images_u])
|
576 |
|
577 |
return iface
|
578 |
|
579 |
|
580 |
+
def setup_seed(seed=0):
|
581 |
+
import random
|
582 |
+
if seed == -1:
|
583 |
+
seed = random.randint(0, 1000000)
|
584 |
+
print(seed)
|
585 |
+
torch.manual_seed(seed) # 为CPU设置随机种子
|
586 |
+
np.random.seed(seed) # Numpy module.
|
587 |
+
random.seed(seed) # Python random module.
|
588 |
+
if torch.cuda.is_available():
|
589 |
+
# torch.backends.cudnn.benchmark = False
|
590 |
+
torch.backends.cudnn.deterministic = True
|
591 |
+
torch.cuda.manual_seed(seed) # 为当前GPU设置随机种子
|
592 |
+
torch.cuda.manual_seed_all(seed) # 为所有GPU设置随机种子
|
593 |
+
# os.environ['PYTHONHASHSEED'] = str(seed)
|
594 |
+
|
595 |
+
|
596 |
if __name__ == '__main__':
|
597 |
device = torch.device('cpu')
|
598 |
+
# device = torch.device('cuda')
|
599 |
unet, vae, normal_t = get_models(device)
|
600 |
|
601 |
|
example.jpg
ADDED
![]() |
requirements.txt
CHANGED
@@ -5,4 +5,5 @@ numpy==1.23.4
|
|
5 |
torch
|
6 |
tqdm
|
7 |
diffusers==0.20.1
|
8 |
-
accelerate
|
|
|
|
5 |
torch
|
6 |
tqdm
|
7 |
diffusers==0.20.1
|
8 |
+
accelerate
|
9 |
+
Pillow==9.2.0
|