-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathinference_edit.py
More file actions
180 lines (163 loc) · 5.49 KB
/
inference_edit.py
File metadata and controls
180 lines (163 loc) · 5.49 KB
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
import torch
try:
import torch_npu
from torch_npu.contrib import transfer_to_npu
import importlib
import transformers.utils
import transformers.models
origin_utils = transformers.utils
origin_models = transformers.models
import flash_attn
flash_attn.hack_transformers_flash_attn_2_available_check()
importlib.reload(transformers.utils)
importlib.reload(transformers.models)
origin_func = torch.nn.functional.interpolate
def new_func(input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None, antialias=False):
if mode == "bilinear":
dtype = input.dtype
res = origin_func(input.to(torch.bfloat16), size, scale_factor, mode, align_corners, recompute_scale_factor, antialias)
return res.to(dtype)
else:
return origin_func(input, size, scale_factor, mode, align_corners, recompute_scale_factor, antialias)
torch.nn.functional.interpolate = new_func
from utils import patch_npu_record_stream
from utils import patch_npu_diffusers_get_1d_rotary_pos_embed
patch_npu_record_stream()
patch_npu_diffusers_get_1d_rotary_pos_embed()
USE_NPU = True
except:
USE_NPU = False
from dreamomni2.pipeline_dreamomni2 import DreamOmni2Pipeline
from diffusers.utils import load_image
from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
# from qwen_vl_utils import process_vision_info
from utils.vprocess import process_vision_info, resizeinput
import os
import argparse
from tqdm import tqdm
import json
from PIL import Image
import re
import argparse
if USE_NPU:
device = "npu"
else:
device = "cuda"
def extract_gen_content(text):
text = text[6:-7]
return text
def parse_args():
"""Parses command-line arguments for model paths and server configuration."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--vlm_path",
type=str,
default="./models/vlm-model",
help="Path to the VLM model directory."
)
parser.add_argument(
"--edit_lora_path",
type=str,
default="./models/edit_lora",
help="Path to the FLUX.1-Kontext editing LoRA weights directory."
)
parser.add_argument(
"--base_model_path",
type=str,
default="black-forest-labs/FLUX.1-Kontext-dev",
help="Path to the FLUX.1-Kontext editing."
)
parser.add_argument(
"--input_img_path",
type=str,
nargs='+', # Accept one or more input paths
default=["example_input/edit_tests/src.jpg", "example_input/edit_tests/ref.jpg"],
help="List of input image paths (e.g., src and ref images)."
)
# Argument for the input instruction
parser.add_argument(
"--input_instruction",
type=str,
default="Make the woman from the second image stand on the road in the first image.",
help="Instruction for image editing."
)
# Argument for the output image path
parser.add_argument(
"--output_path",
type=str,
default="example_input/edit_tests/edi_res.png",
help="Path to save the output image."
)
args = parser.parse_args()
return args
ARGS = parse_args()
vlm_path = ARGS.vlm_path
edit_lora_path = ARGS.edit_lora_path
base_model = ARGS.base_model_path
pipe = DreamOmni2Pipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
pipe.to(device)
pipe.load_lora_weights(
edit_lora_path,
adapter_name="edit"
)
pipe.set_adapters(["edit"], adapter_weights=[1])
vlm_model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
vlm_path, torch_dtype="bfloat16", device_map="cuda"
)
processor = AutoProcessor.from_pretrained(vlm_path)
def infer_vlm(input_img_path,input_instruction,prefix):
tp=[]
for path in input_img_path:
tp.append({"type": "image", "image": path})
tp.append({"type": "text", "text": input_instruction+prefix})
messages = [
{
"role": "user",
"content": tp,
}
]
# Preparation for inference
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
)
inputs = inputs.to("cuda")
# Inference
generated_ids = vlm_model.generate(**inputs, do_sample=False, max_new_tokens=4096)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
return output_text[0]
def infer(source_imgs,prompt):
image = pipe(
images=source_imgs,
height=source_imgs[0].height,
width=source_imgs[0].width,
prompt=prompt,
num_inference_steps=30,
guidance_scale=3.5,
).images[0]
return image
input_img_path=ARGS.input_img_path
input_instruction=ARGS.input_instruction
prefix=" It is editing task."
source_imgs = []
for path in input_img_path:
img = load_image(path)
# source_imgs.append(img)
source_imgs.append(resizeinput(img))
prompt=infer_vlm(input_img_path,input_instruction,prefix)
prompt = extract_gen_content(prompt)
image=infer(source_imgs,prompt)
output_path = ARGS.output_path
image.save(output_path)