File size: 13,516 Bytes
bc49ed6 f615fc2 bc49ed6 db62905 bc49ed6 306bda8 bc49ed6 1e6ce87 db62905 bc49ed6 306bda8 bc49ed6 db62905 5f34f32 bc49ed6 306bda8 bc49ed6 db62905 5f34f32 bc49ed6 8f7a765 bc49ed6 8f7a765 bc49ed6 8f7a765 bc49ed6 8f7a765 bc49ed6 8f7a765 bc49ed6 8f7a765 bc49ed6 8f7a765 bc49ed6 306bda8 bc49ed6 db62905 5f34f32 bc49ed6 |
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 |
import torch
import numpy as np
from PIL import Image
from einops import rearrange
from mmengine.config import Config
from xtuner.registry import BUILDER
from torch.nn.utils.rnn import pad_sequence
import os
import json
from mmengine.logging import print_log
import spaces
def crop2square(pil_img):
width, height = pil_img.width, pil_img.height
short = min(width, height)
left = (width - short) // 2
upper = (height - short) // 2
return pil_img.crop((left, upper, left + short, upper + short))
def preprocess_image(image: Image.Image, image_size: int, dtype: torch.dtype):
"""将 PIL Image 缩放(使用邻近插值)、归一化并返回 [1, C, H, W] Tensor。"""
# if image.width != image_size or image.height != image_size:
# # 1) 裁剪中央正方
# img = crop2square(image)
# img = img.resize((image_size, image_size))
# else:
# img = image
img = crop2square(image)
img = img.resize((image_size, image_size))
arr = np.asarray(img).astype(np.float32) / 255.0
arr = 2 * arr - 1
tensor = torch.from_numpy(arr).to(dtype=dtype)
return rearrange(tensor, "h w c -> 1 c h w")
def expand2square(pil_img, target_size=1024, background_color=(127, 127, 127)):
"""
Resize an image to fit within a square of size target_size x target_size,
padding with background_color to make it exactly square.
Args:
pil_img (PIL.Image.Image): The input image.
target_size (int): The desired square resolution.
background_color (tuple): RGB color to pad with.
Returns:
PIL.Image.Image: The resized and padded square image.
"""
original_width, original_height = pil_img.size
scale = min(target_size / original_width, target_size / original_height)
new_width = int(original_width * scale)
new_height = int(original_height * scale)
# Resize image
resized_img = pil_img.resize((new_width, new_height), resample=Image.Resampling.BICUBIC)
# Create new square background
new_img = Image.new(pil_img.mode, (target_size, target_size), background_color)
paste_position = ((target_size - new_width) // 2, (target_size - new_height) // 2)
new_img.paste(resized_img, paste_position)
return new_img
def _print_load_result(module_name, missing, unexpected):
print_log(
f"[INFO] Loaded {module_name}. missing={len(missing)}, unexpected={len(unexpected)}"
)
class Inferencer:
def __init__(
self, config_file, model_path, image_size=1024, cfg_prompt="Generate an image."
):
self.config_file = config_file
self.cfg = Config.fromfile(self.config_file)
self.model_path = model_path
self.device = "cuda"
self.image_size = image_size
self.image_shape = (image_size // 16, image_size // 16)
self.cfg_prompt = cfg_prompt
self.model = None
def init_model(self):
# config = Config.fromfile(self.config_file)
# model = BUILDER.build(config.model)
model = BUILDER.build(self.cfg.model)
if os.path.isdir(self.model_path):
index_path = os.path.join(self.model_path, "pytorch_model.bin.index.json")
print_log(
f"[INFO] Loading sharded Harmon checkpoint from: {self.model_path}"
)
state_dict = {}
with open(index_path, "r") as f:
index = json.load(f)
for shard in sorted(set(index["weight_map"].values())):
shard_path = os.path.join(self.model_path, shard)
print_log(f"[INFO] Loading shard: {shard_path}")
state_dict.update(torch.load(shard_path, map_location=self.device))
else:
print_log(f"[INFO] Loading full Harmon checkpoint from: {self.model_path}")
state_dict = torch.load(self.model_path, map_location=self.device)
m, u = model.load_state_dict(state_dict, strict=False)
_print_load_result("Harmon", m, u)
# 载入siglip2 weight
# siglip_proj_path = "/mnt/data_vlm/wangxiaokun/Unified/Harmon_Siglip/Model/400w/stage1/9000/pytorch_model.bin"
# sl_state = torch.load(
# siglip_proj_path, map_location=self.device, weights_only=False
# )
# if isinstance(sl_state, dict) and "model" in sl_state:
# sl_state = sl_state["model"]
# m, u = model.siglip2_proj.load_state_dict(sl_state, strict=False)
# _print_load_result("SigLIP2", m, u)
model = model.to(self.device, dtype=model.dtype)
model.eval()
return model
@spaces.GPU(duration=120)
def gen_image(
self,
raw_prompt,
images_to_generate=1,
cfg=3.0,
num_iter=64,
cfg_schedule="constant",
temperature=1.0,
):
if not self.model:
self.model = self.init_model()
prompt = self.model.prompt_template["INSTRUCTION"].format(
input=f"Generate an image: {raw_prompt.strip()}."
)
prompts = [prompt] * images_to_generate
if cfg != 1.0:
prompts += [
self.model.prompt_template["INSTRUCTION"].format(input=self.cfg_prompt)
] * images_to_generate
inputs = self.model.tokenizer(
prompts, add_special_tokens=True, return_tensors="pt", padding=True
).to(self.device)
print(prompts)
images = self.model.sample(
**inputs,
num_iter=num_iter,
cfg=cfg,
cfg_schedule=cfg_schedule,
temperature=temperature,
progress=False,
image_shape=self.image_shape,
)
images = rearrange(images, "(n b) c h w -> b n h w c", n=images_to_generate)
images = (
torch.clamp(127.5 * images + 128.0, 0, 255)
.to("cpu", dtype=torch.uint8)
.numpy()
)
return [Image.fromarray(img) for img in images[0]]
@spaces.GPU(duration=120)
def query_image(self, img: Image.Image, prompt=""):
model = self.model
if not model:
model = self.init_model()
tokenizer = model.tokenizer
special_tokens_dict = {"additional_special_tokens": ["<image>"]}
tokenizer.add_special_tokens(special_tokens_dict)
image_token_idx = tokenizer.encode("<image>", add_special_tokens=False)[-1]
# preprocess image
image = img.convert("RGB")
image = expand2square(image)
image = torch.from_numpy(np.array(image)).to(
dtype=model.dtype, device=self.device
)
image = rearrange(image, "h w c -> c h w")[None]
image = 2 * (image / 255) - 1
# prepare prompt
full_prompt = model.prompt_template["INSTRUCTION"].format(
input="<image>\n" + prompt
)
image_length = (self.image_size // 16) ** 2 + 64
full_prompt = full_prompt.replace("<image>", "<image>" * image_length)
input_ids = tokenizer.encode(
full_prompt, add_special_tokens=True, return_tensors="pt"
).to(self.device)
# extract image embedding
with torch.no_grad():
_, z_enc = model.extract_visual_feature(model.encode(image))
inputs_embeds = z_enc.new_zeros(*input_ids.shape, model.llm.config.hidden_size)
inputs_embeds[input_ids == image_token_idx] = z_enc.flatten(0, 1)
inputs_embeds[input_ids != image_token_idx] = model.llm.get_input_embeddings()(
input_ids[input_ids != image_token_idx]
)
# generate text
with torch.no_grad():
output = model.llm.generate(
inputs_embeds=inputs_embeds,
use_cache=True,
do_sample=False,
max_new_tokens=4096,
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
)
# print(tokenizer.decode(output[0]))
return tokenizer.decode(output[0])
@spaces.GPU(duration=120)
def edit_image(
self,
source_image: Image.Image,
prompt: str,
num_iter: int = 48,
cfg: float = 3.0,
cfg_prompt: str = "Repeat this image.",
cfg_schedule: str = "constant",
temperature: float = 0.85,
grid_size: int = 1
) -> Image.Image:
"""Edit single image based on prompt."""
model = self.model
if not model:
model = self.init_model()
tokenizer = model.tokenizer
special_tokens_dict = {"additional_special_tokens": ["<image>"]}
tokenizer.add_special_tokens(special_tokens_dict)
image_token_idx = tokenizer.encode("<image>", add_special_tokens=False)[-1]
device = "cuda"
# 1) Preprocess source image
img_tensor = preprocess_image(source_image, self.image_size, model.dtype).to(device)
# 2) Encode image and extract features
with torch.no_grad():
x_enc = model.encode(img_tensor)
x_con, z_enc = model.extract_visual_feature(x_enc)
# 3) Prepare text prompts
m = n = self.image_size // 16
image_length = m * n + 64
if hasattr(self.cfg.model, 'prompt_template'):
prompt_str = self.cfg.model.prompt_template['INSTRUCTION'].format(
input="<image>\n" + prompt.strip()
)
cfg_prompt_str = self.cfg.model.prompt_template['INSTRUCTION'].format(
input="<image>\n" + cfg_prompt.strip()
)
else:
prompt_str = f"<image>\n{prompt.strip()}"
cfg_prompt_str = f"<image>\n{cfg_prompt.strip()}"
# Replace <image> token with multiple tokens
prompt_str = prompt_str.replace('<image>', '<image>' * image_length)
cfg_prompt_str = cfg_prompt_str.replace('<image>', '<image>' * image_length)
# 4) Tokenize and prepare inputs
input_ids = model.tokenizer.encode(
prompt_str, add_special_tokens=True, return_tensors='pt')[0].cuda()
if cfg != 1.0:
null_input_ids = model.tokenizer.encode(
cfg_prompt_str, add_special_tokens=True, return_tensors='pt')[0].cuda()
attention_mask = pad_sequence(
[torch.ones_like(input_ids), torch.ones_like(null_input_ids)],
batch_first=True, padding_value=0).to(torch.bool)
input_ids = pad_sequence(
[input_ids, null_input_ids],
batch_first=True, padding_value=model.tokenizer.eos_token_id)
else:
input_ids = input_ids[None]
attention_mask = torch.ones_like(input_ids).to(torch.bool)
# 5) Prepare embeddings
if cfg != 1.0:
z_enc = torch.cat([z_enc, z_enc], dim=0)
x_con = torch.cat([x_con, x_con], dim=0)
inputs_embeds = z_enc.new_zeros(*input_ids.shape, model.llm.config.hidden_size)
#debug:目前这里报错
inputs_embeds[input_ids == image_token_idx] = z_enc.flatten(0, 1)
inputs_embeds[input_ids != image_token_idx] = model.llm.get_input_embeddings()(
input_ids[input_ids != image_token_idx]
)
# 6) Repeat for grid sampling
bsz = grid_size ** 2
x_con = torch.cat([x_con] * bsz)
if cfg != 1.0:
inputs_embeds = torch.cat([
inputs_embeds[:1].expand(bsz, -1, -1),
inputs_embeds[1:].expand(bsz, -1, -1),
])
attention_mask = torch.cat([
attention_mask[:1].expand(bsz, -1),
attention_mask[1:].expand(bsz, -1),
])
else:
inputs_embeds = inputs_embeds.expand(bsz, -1, -1)
attention_mask = attention_mask.expand(bsz, -1)
# 7) Sampling
samples = model.sample(
inputs_embeds=inputs_embeds,
attention_mask=attention_mask,
num_iter=num_iter,
cfg=cfg,
cfg_schedule=cfg_schedule,
temperature=temperature,
progress=False,
image_shape=(m, n),
x_con=x_con
)
# 9) Convert to PIL Image
samples = rearrange(samples, '(m n) c h w -> (m h) (n w) c', m=grid_size, n=grid_size)
samples = torch.clamp(127.5 * samples + 128.0, 0, 255)
out = samples.to("cpu", torch.uint8).numpy()
return [ Image.fromarray(out) ]
@spaces.GPU(duration=120)
def query_text(self, prompt=""):
model = self.model
if not model:
model = self.init_model()
tokenizer = model.tokenizer
# 构造文本 prompt
full_prompt = model.prompt_template["INSTRUCTION"].format(input=prompt)
input_ids = tokenizer.encode(
full_prompt, add_special_tokens=True, return_tensors="pt"
).to(self.device)
# 生成回复
with torch.no_grad():
output = model.llm.generate(
input_ids=input_ids,
use_cache=True,
do_sample=True,
max_new_tokens=1024,
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id,
)
res = tokenizer.decode(output[0], skip_special_tokens=True)
# print(f"Query Text Output: {res}")
return res
|