Spaces:
Build error
Build error
Commit ·
16c2627
1
Parent(s): bafa7b2
update gradio app
Browse filesFiles changed (3)
123452 unmodified lines89101112136 unmodified lines202122232425262728293031323338 unmodified lines72737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114import numpy as npfrom PIL import Imagefrom leffa.transform import LeffaTransformfrom leffa.model import LeffaModelfrom leffa.inference import LeffaInference2 unmodified linesimport gradio as grdef leffa_predict(src_image_path, ref_image_path, control_type):assert control_type in [6 unmodified lines# Maskif control_type == "virtual_tryon":automasker = AutoMasker()src_image = src_image.convert("RGB")mask = automasker(src_image, "upper")["mask"]elif control_type == "pose_transfer":mask = Image.fromarray(np.ones_like(src_image_array) * 255)# DensePosedensepose_predictor = DensePosePredictor()src_image_iuv_array = densepose_predictor.predict_iuv(src_image_array)src_image_seg_array = densepose_predictor.predict_seg(src_image_array)src_image_iuv = Image.fromarray(src_image_iuv_array)38 unmodified lines# control_type = sys.argv[3]# leffa_predict(src_image_path, ref_image_path, control_type)# Launch a gr.Interfacegr_demo = gr.Interface(fn=leffa_predict,inputs=[gr.Image(sources=["upload", "webcam", "clipboard"],type="filepath",label="Source Person Image",width=768,height=1024,),gr.Image(sources=["upload", "webcam", "clipboard"],type="filepath",label="Reference Image",width=768,height=1024,),gr.Radio(["virtual_tryon", "pose_transfer"],label="Control Type",default="virtual_tryon",),],outputs=[gr.Image(label="Generated Person Image",width=768,height=1024,)],title="Leffa",description="Leffa is a unified framework for controllable person image generation that enables precise manipulation of both appearance (i.e., virtual try-on) and pose (i.e., pose transfer).",article="Controllable person image generation aims to generate a person image conditioned on reference images, allowing precise control over the person’s appearance or pose. However, prior methods often distort fine-grained textural details from the reference image, despite achieving high overall image quality. We attribute these distortions to inadequate attention to corresponding regions in the reference image. To address this, we thereby propose \textbf{learning flow fields in attention} (\textbf{\ours{}}), which explicitly guides the target query to attend to the correct reference key in the attention layer during training. Specifically, it is realized via a regularization loss on top of the attention map within a diffusion-based baseline. Our extensive experiments show that Leffa achieves state-of-the-art performance in controlling appearance (virtual try-on) and pose (pose transfer), significantly reducing fine-grained detail distortion while maintaining high image quality. Additionally, we show that our loss is model-agnostic and can be used to improve the performance of other diffusion models.",examples=[["./examples/14092_00_person.jpg", "./examples/04181_00_garment.jpg", "virtual_tryon"],["./examples/14092_00_person.jpg", "./examples/14684_00_person.jpg", "pose_transfer"],],# cache_examples=True,examples_per_page=10,allow_flagging=False,theme=gr.themes.Default(),)gr_demo.launch(share=True, server_port=7860)1234562 unmodified lines910111213141516176 unmodified lines242526272829303132333435363738394041424338 unmodified lines828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137import numpy as npfrom PIL import Imagefrom huggingface_hub import snapshot_downloadfrom leffa.transform import LeffaTransformfrom leffa.model import LeffaModelfrom leffa.inference import LeffaInference2 unmodified linesimport gradio as gr# Download checkpointssnapshot_download(repo_id="franciszzj/Leffa", local_dir="./")def leffa_predict(src_image_path, ref_image_path, control_type):assert control_type in [6 unmodified lines# Maskif control_type == "virtual_tryon":automasker = AutoMasker(densepose_path="./ckpts/densepose",schp_path="./ckpts/schp",)src_image = src_image.convert("RGB")mask = automasker(src_image, "upper")["mask"]elif control_type == "pose_transfer":mask = Image.fromarray(np.ones_like(src_image_array) * 255)# DensePosedensepose_predictor = DensePosePredictor(config_path="./ckpts/densepose/densepose_rcnn_R_50_FPN_s1x.yaml",weights_path="./ckpts/densepose/model_final_162be9.pkl",)src_image_iuv_array = densepose_predictor.predict_iuv(src_image_array)src_image_seg_array = densepose_predictor.predict_seg(src_image_array)src_image_iuv = Image.fromarray(src_image_iuv_array)38 unmodified lines# control_type = sys.argv[3]# leffa_predict(src_image_path, ref_image_path, control_type)with gr.Blocks().queue() as demo:gr.Markdown("## Leffa: Learning Flow Fields in Attention for Controllable Person Image Generation")gr.Markdown("Leffa is a unified framework for controllable person image generation that enables precise manipulation of both appearance (i.e., virtual try-on) and pose (i.e., pose transfer).")with gr.Row():with gr.Column():src_image = gr.Image(sources=["upload"],type="filepath",label="Source Person Image",width=384,height=512,)with gr.Row():control_type = gr.Dropdown(["virtual_tryon", "pose_transfer"], label="Control Type")example = gr.Examples(inputs=src_image,examples_per_page=10,examples=["./examples/14684_00_person.jpg","./examples/14092_00_person.jpg"],)with gr.Column():ref_image = gr.Image(sources=["upload"],type="filepath",label="Reference Image",width=384,height=512,)with gr.Row():gen_button = gr.Button("Generate")example = gr.Examples(inputs=ref_image,examples_per_page=10,examples=["./examples/04181_00_garment.jpg","./examples/14684_00_person.jpg"],)with gr.Column():gen_image = gr.Image(label="Generated Person Image",width=384,height=512,)gen_button.click(fn=leffa_predict, inputs=[src_image, ref_image, control_type], outputs=[gen_image])demo.launch(share=True, server_port=7860)
9 unmodified lines101112131415161718192021229 unmodified linesclass DensePosePredictor(object):def __init__(self):cfg = get_cfg()add_densepose_config(cfg)cfg.merge_from_file("ckpts/densepose/densepose_rcnn_R_50_FPN_s1x.yaml") # Use the path to the config file from denseposecfg.MODEL.WEIGHTS = "ckpts/densepose/model_final_162be9.pkl" # Use the path to the pre-trained model weightscfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # Adjust as neededself.predictor = DefaultPredictor(cfg)9 unmodified lines1011121314151617181920212223249 unmodified linesclass DensePosePredictor(object):def __init__(self,config_path="./ckpts/densepose/densepose_rcnn_R_50_FPN_s1x.yaml",weights_path="./ckpts/densepose/model_final_162be9.pkl"):cfg = get_cfg()add_densepose_config(cfg)cfg.merge_from_file(config_path) # Use the path to the config file from denseposecfg.MODEL.WEIGHTS = weights_path # Use the path to the pre-trained model weightscfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # Adjust as neededself.predictor = DefaultPredictor(cfg)
199 unmodified lines200201202203204205206207208209210211212213214215216217218219220199 unmodified linesclass AutoMasker:def __init__(self,device="cuda",):densepose_ckpt = "./ckpts/densepose"schp_ckpt = "./ckpts/schp"np.random.seed(0)torch.manual_seed(0)torch.cuda.manual_seed(0)self.densepose_processor = DensePose(densepose_ckpt, device)self.schp_processor_atr = SCHP(ckpt_path=os.path.join(schp_ckpt, "exp-schp-201908301523-atr.pth"),device=device,)self.schp_processor_lip = SCHP(ckpt_path=os.path.join(schp_ckpt, "exp-schp-201908261155-lip.pth"),device=device,)199 unmodified lines200201202203204205206207208209210211212213214215216217218219220199 unmodified linesclass AutoMasker:def __init__(self,densepose_path: str = "./ckpts/densepose",schp_path: str = "./ckpts/schp",device="cuda",):np.random.seed(0)torch.manual_seed(0)torch.cuda.manual_seed(0)self.densepose_processor = DensePose(densepose_path, device)self.schp_processor_atr = SCHP(ckpt_path=os.path.join(schp_path, "exp-schp-201908301523-atr.pth"),device=device,)self.schp_processor_lip = SCHP(ckpt_path=os.path.join(schp_path, "exp-schp-201908261155-lip.pth"),device=device,)
app.py+65-42
utils/densepose_predictor.py+6-4
utils/garment_agnostic_mask_predictor.py+5-5