-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinfer.py
126 lines (106 loc) · 4.82 KB
/
infer.py
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
import torch
import numpy as np
from PIL import Image
import argparse
import os
from diffusers import FluxTransformer2DModel, FluxPipeline
from transformers import T5EncoderModel, CLIPTextModel
from diffusers import FluxInpaintPipeline, AutoencoderKL
from src.pipeline_tryon import FluxTryonPipeline, crop_to_multiple_of_16, resize_and_pad_to_size, resize_by_height
def load_models(model_path, device="cuda", torch_dtype=torch.bfloat16):
text_encoder = CLIPTextModel.from_pretrained(model_path, subfolder="text_encoder", torch_dtype=torch_dtype)
text_encoder_2 = T5EncoderModel.from_pretrained(model_path, subfolder="text_encoder_2", torch_dtype=torch_dtype)
transformer = FluxTransformer2DModel.from_pretrained(model_path, subfolder="transformer", torch_dtype=torch_dtype)
vae = AutoencoderKL.from_pretrained(model_path, subfolder="vae")
pipe = FluxTryonPipeline.from_pretrained(
model_path,
transformer=transformer,
text_encoder=text_encoder,
text_encoder_2=text_encoder_2,
vae=vae,
torch_dtype=torch_dtype,
).to(device=device, dtype=torch_dtype)
pipe.enable_attention_slicing()
pipe.vae.enable_slicing()
pipe.vae.enable_tiling()
pipe.load_lora_weights(
"loooooong/Any2anyTryon",
weight_name="dev_lora_any2any_tryon.safetensors",
adapter_name="tryon",
)
return pipe
@torch.no_grad()
def generate_image(pipe, model_image_path, garment_image_path, prompt="", height=512, width=384,
seed=0, guidance_scale=3.5, num_inference_steps=30):
height, width = int(height), int(width)
width = width - (width % 16)
height = height - (height % 16)
concat_image_list = []
has_model_image = model_image_path is not None
has_garment_image = garment_image_path is not None
if has_model_image:
model_image = Image.open(model_image_path)
if has_garment_image:
input_height, input_width = model_image.size[1], model_image.size[0]
model_image, lp, tp, rp, bp = resize_and_pad_to_size(model_image, width, height)
else:
model_image = resize_by_height(model_image, height)
concat_image_list.append(model_image)
if has_garment_image:
garment_image = Image.open(garment_image_path)
garment_image = resize_by_height(garment_image, height)
concat_image_list.append(garment_image)
concat_image_list.append(Image.fromarray(np.zeros((height, width, 3), dtype=np.uint8)))
image = Image.fromarray(np.concatenate([np.array(img) for img in concat_image_list], axis=1))
mask = np.zeros_like(np.array(image))
mask[:,-width:] = 255
mask_image = Image.fromarray(mask)
image = pipe(
prompt,
image=image,
mask_image=mask_image,
strength=1.,
height=height,
width=image.width,
target_width=width,
tryon=has_model_image and has_garment_image,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
max_sequence_length=512,
generator=torch.Generator("cpu").manual_seed(seed),
output_type="pil",
).images[0]
if has_model_image and has_garment_image:
image = image.crop((lp, tp, image.width-rp, image.height-bp)).resize((input_width, input_height))
return image
def main():
parser = argparse.ArgumentParser(description='Virtual Try-on Image Generation')
parser.add_argument('--model_path', type=str, default="black-forest-labs/FLUX.1-dev", help='Path to the model')
parser.add_argument('--model_image', type=str, help='Path to the model image')
parser.add_argument('--garment_image', type=str, help='Path to the garment image')
parser.add_argument('--prompt', type=str, default="")
parser.add_argument('--height', type=int, default=768)
parser.add_argument('--width', type=int, default=576)
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--guidance_scale', type=float, default=3.5)
parser.add_argument('--num_inference_steps', type=int, default=30)
parser.add_argument('--output_path', type=str, default='./results/output.png')
parser.add_argument('--device', type=str, default='cuda')
args = parser.parse_args()
pipe = load_models(args.model_path, device=args.device)
output_image = generate_image(
pipe=pipe,
model_image_path=args.model_image,
garment_image_path=args.garment_image,
prompt=args.prompt,
height=args.height,
width=args.width,
seed=args.seed,
guidance_scale=args.guidance_scale,
num_inference_steps=args.num_inference_steps
)
os.makedirs(os.path.dirname(args.output_path), exist_ok=True)
output_image.save(args.output_path)
print(f"Generated image saved to {args.output_path}")
if __name__ == "__main__":
main()