Spaces:
Runtime error
Runtime error
File size: 21,348 Bytes
0f95337 |
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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
import numpy
import numpy as np
import torch
import os
import random
import pandas as pd
import os.path as osp
import PIL.Image as Image
from torch.utils.data import Dataset
from pathlib import Path
from imagedream.ldm.util import add_random_background
from imagedream.camera_utils import get_camera_for_index
from libs.base_utils import do_resize_content, add_stroke
import torchvision.transforms as transforms
def to_rgb_image(maybe_rgba: Image.Image):
if maybe_rgba.mode == "RGB":
return maybe_rgba
elif maybe_rgba.mode == "RGBA":
rgba = maybe_rgba
img = numpy.random.randint(
127, 128, size=[rgba.size[1], rgba.size[0], 3], dtype=numpy.uint8
)
img = Image.fromarray(img, "RGB")
img.paste(rgba, mask=rgba.getchannel("A"))
return img
else:
raise ValueError("Unsupported image type.", maybe_rgba.mode)
def axis_rotate_xyz(img: Image.Image, rotate_axis="z", angle=90.0):
img = img.convert("RGB")
img = np.array(img) - 127
img = img.astype(np.float32)
# perform element-wise sin-cos rotation
if rotate_axis == "z":
img = np.stack(
[
img[..., 0] * np.cos(angle) - img[..., 1] * np.sin(angle),
img[..., 0] * np.sin(angle) + img[..., 1] * np.cos(angle),
img[..., 2],
],
-1,
)
elif rotate_axis == "y":
img = np.stack(
[
img[..., 0] * np.cos(angle) + img[..., 2] * np.sin(angle),
img[..., 1],
-img[..., 0] * np.sin(angle) + img[..., 2] * np.cos(angle),
],
-1,
)
elif rotate_axis == "x":
img = np.stack(
[
img[..., 0],
img[..., 1] * np.cos(angle) - img[..., 2] * np.sin(angle),
img[..., 1] * np.sin(angle) + img[..., 2] * np.cos(angle),
],
-1,
)
return Image.fromarray(img.astype(np.uint8) + 127)
class DataHQCRelative(Dataset):
"""
- base_dir
- uid1
- 000.png
- 001.png
- ...
- uid2
- xyz_base
- uid1
- xyz_new_000.png
- xyz_new_001.png
- ...
accepte caption data(in csv format)
"""
def __init__(
self,
base_dir,
caption_csv,
ref_indexs=[0],
ref_position=-1,
xyz_base=None,
camera_views=[3, 6, 9, 12, 15], # camera views are relative views, not abs
split="train",
image_size=256,
random_background=False,
resize_rate=1,
num_frames=5,
repeat=100,
outer_file=None,
debug=False,
eval_size=100,
):
print(__class__)
OPENAI_DATASET_MEAN = (0.48145466, 0.4578275, 0.40821073)
OPENAI_DATASET_STD = (0.26862954, 0.26130258, 0.27577711)
df = pd.read_csv(caption_csv, sep=",", names=["id", "caption"])
id_to_caption = {}
for i in range(len(df.index)):
item = df.iloc[i]
id_to_caption[item["id"]] = item["caption"]
# outer file is txt file, containing each ident per line, specific idents not included in the train process
outer_set = (
set(open(outer_file, "r").read().strip().split("\n"))
if outer_file is not None
else set()
)
xyz_set = set(os.listdir(xyz_base)) if xyz_base is not None else set()
common_keys = set(id_to_caption.keys()) & set(os.listdir(base_dir))
common_keys = common_keys & xyz_set if xyz_base is not None else common_keys
common_keys = common_keys - outer_set
self.common_keys = common_keys
self.id_to_caption = id_to_caption
final_dict = {key: id_to_caption[key] for key in common_keys}
self.image_size = image_size
self.base_dir = Path(base_dir)
self.xyz_base = xyz_base
self.repeat = repeat
self.num_frames = num_frames
self.camera_views = camera_views[:num_frames]
self.split = split
self.ref_indexs = ref_indexs
self.ref_position = ref_position
self.resize_rate = resize_rate
self.random_background = random_background
self.debug = debug
assert split in ["train", "eval"]
clip_size = 224
self.transfrom_clip = transforms.Compose(
[
transforms.Resize(
(clip_size, clip_size),
interpolation=Image.BICUBIC,
antialias="warn",
),
transforms.ToTensor(),
transforms.Normalize(mean=OPENAI_DATASET_MEAN, std=OPENAI_DATASET_STD),
]
)
self.transfrom_vae = transforms.Compose(
[
transforms.Resize((image_size, image_size)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]
)
# 对于第i个视角作为参考时,左边,下面,背面,右边,上面,的图片名称index
import torchvision.transforms.functional as TF
from functools import partial as PA
self.index_mapping = [
# 正 左 下 背 右 上
[0, 1, 2, 3, 4, 5], # 0
[1, 3, 2, 4, 0, 5], # 1
[2, 1, 3, 5, 4, 0], # 2
[3, 4, 2, 0, 1, 5], # 3
[4, 0, 2, 1, 3, 5], # 4
[5, 1, 0, 2, 4, 3], # 5
]
TT = {
"r90": PA(TF.rotate, angle=-90.0), # 顺时针90
"r180": PA(TF.rotate, angle=-180.0), # 顺时针180
"r270": PA(TF.rotate, angle=-270.0), # 顺时针270
"s90": PA(TF.rotate, angle=90.0), # 逆时针90
"s180": PA(TF.rotate, angle=180.0), # 逆时针180
"s270": PA(TF.rotate, angle=270.0), # 逆时针270
}
self.transfroms_mapping = [
# 正 左 下 背 右 上
[None, None, None, None, None, None], # 0
[None, None, TT["r90"], None, None, TT["s90"]], # 1
[None, TT["s90"], TT["s180"], TT["r180"], TT["r90"], None], # 2
[None, None, TT["r180"], None, None, TT["s180"]], # 3
[None, None, TT["s90"], None, None, TT["r90"]], # 4
[None, TT["r90"], None, TT["r180"], TT["s90"], TT["s180"]], # 5
]
XT = { # xyz transforms
"zRrota90": PA(axis_rotate_xyz, rotate_axis="z", angle=np.pi / 2),
"zSrota90": PA(axis_rotate_xyz, rotate_axis="z", angle=-np.pi / 2),
"zrota180": PA(axis_rotate_xyz, rotate_axis="z", angle=-np.pi),
"xRrota90": PA(axis_rotate_xyz, rotate_axis="x", angle=np.pi / 2),
"xSrota90": PA(axis_rotate_xyz, rotate_axis="x", angle=-np.pi / 2),
}
self.xyz_transforms_mapping = [
# 正 左 下 背 右 上
[None,] * 6, # 0
[XT["zRrota90"],] * 6, # 1
[XT["xRrota90"],] * 6, # 2
[XT["zrota180"],] * 6, # 3
[XT["zSrota90"],] * 6, # 4
[XT["xSrota90"],] * 6, # 5
]
total_items = [
{
"path": os.path.join(base_dir, k),
"xyz_path": os.path.join(xyz_base, k) if xyz_base is not None else None,
"caption": v,
}
for k, v in final_dict.items()
]
total_items.sort(key=lambda x: x["path"])
if len(total_items) > eval_size:
if split == "train":
self.items = total_items[eval_size:]
else:
self.items = total_items[:eval_size]
else:
self.items = total_items
print("============= length of dataset %d =============" % len(self.items))
def __len__(self):
return len(self.items) * self.repeat
def __getitem__(self, index):
"""
choose index for target 6 images
select one of them as input image
target_images_vae: batch of `num_frame` images of one object from different views, processed by vae_processor
ref_ip: ref image in piexl space
ref_ip_img:
camera views decide the logical camera pose of images:
000 is front , ev: 0, azimuth: 0
001 is left , ev: 0, azimuth: -90
002 is down , ev: -90, azimuth: 0
003 is back , ev: 0, azimuth: 180
004 is right , ev: 0, azimuth: 90
005 is top , ev: 90, azimuth: 0
ref_index decides which image choose to be input image
for example when camera views = [1, 2, 3, 4, 5, 0], ref_position=5
then dataset return the instance images in order as [left, down, back, right, top, front]
in which view[ref_position] = view[5] = 0, so the refrence image is the front image
as all the faces can be rotated to the front face, so any image can be placed to ref_position as ref image(need some tramsforms)
to do a better control of which image can be placed to ref_position, we can set ref_indexs.
ref_indexs set [0] default, that means only 000 named images will be placed to ref_position.
on the situation of ref_indexs=[0, 1, 3, 4], only 000, 001, 003, 004 named images will be placed to ref_position.
"""
index_mapping = self.index_mapping
transfroms_mapping = self.transfroms_mapping
index = index % len(self.items)
target_dir = self.items[index]["path"]
target_xyz_dir = self.items[index]["xyz_path"]
caption = self.items[index]["caption"]
bg_color = np.random.rand() * 255
target_images = []
target_xyz_images = []
raw_images = []
raw_xyz_images = []
alpha_masks = []
ref_index = random.choice(self.ref_indexs)
cur_index_mapping = index_mapping[ref_index]
cur_transfroms_mapping = transfroms_mapping[ref_index]
cur_xyz_transfroms_mapping = self.xyz_transforms_mapping[ref_index]
for relative_view in self.camera_views:
image_index = cur_index_mapping[relative_view]
trans = cur_transfroms_mapping[relative_view]
trans_xyz = cur_xyz_transfroms_mapping[relative_view]
# open
img = Image.open(
os.path.join(target_dir, f"{image_index:03d}.png")
).convert("RGBA")
if trans is not None:
img = trans(img)
img = do_resize_content(img, self.resize_rate)
alpha_mask = img.getchannel("A")
alpha_masks.append(alpha_mask)
if self.random_background:
img = add_random_background(img, bg_color)
img = img.convert("RGB")
target_images.append(self.transfrom_vae(img))
raw_images.append(img)
if self.xyz_base is not None:
img_xyz = Image.open(
os.path.join(target_xyz_dir, f"xyz_new_{image_index:03d}.png")
).convert("RGBA")
img_xyz = trans_xyz(img_xyz) if trans_xyz is not None else img_xyz
img_xyz = trans(img_xyz) if trans is not None else img_xyz
img_xyz = do_resize_content(img_xyz, self.resize_rate)
img_xyz.putalpha(alpha_mask)
if self.random_background:
img_xyz = add_random_background(img_xyz, bg_color)
img_xyz = img_xyz.convert("RGB")
target_xyz_images.append(self.transfrom_vae(img_xyz))
if self.debug:
raw_xyz_images.append(img_xyz)
cameras = [get_camera_for_index(i).squeeze() for i in self.camera_views]
if self.ref_position is not None:
cameras[self.ref_position] = torch.zeros_like(
cameras[self.ref_position]
) # set ref camera to zero
cameras = torch.stack(cameras)
input_img = Image.open(
os.path.join(target_dir, f"{ref_index:03d}.png")
).convert("RGBA")
input_img = do_resize_content(input_img, self.resize_rate)
if self.random_background:
input_img = add_random_background(input_img, bg_color)
input_img = input_img.convert("RGB")
clip_cond = self.transfrom_clip(input_img)
vae_cond = self.transfrom_vae(input_img)
vae_target = torch.stack(target_images, dim=0)
if self.xyz_base is not None:
xyz_vae_target = torch.stack(target_xyz_images, dim=0)
else:
xyz_vae_target = []
if self.debug:
print(f"debug!!,{bg_color}")
return {
"target_images": raw_images,
"target_images_xyz": raw_xyz_images,
"input_img": input_img,
"cameras": cameras,
"caption": caption,
"item": self.items[index],
"alpha_masks": alpha_masks,
}
if self.split == "train":
return {
"target_images_vae": vae_target,
"target_images_xyz_vae": xyz_vae_target,
"clip_cond": clip_cond,
"vae_cond": vae_cond,
"cameras": cameras,
"caption": caption,
}
else: # eval
path = os.path.join(target_dir, f"{ref_index:03d}.png")
return dict(
path=path,
target_dir=target_dir,
cond_raw_images=raw_images,
cond=input_img,
ref_index=ref_index,
ident=f"{index}-{Path(target_dir).stem}",
)
class DataRelativeStroke(DataHQCRelative):
"""a temp dataset for add sync base using fov data as ref image"""
def __init__(
self,
base_dir,
caption_csv,
ref_indexs=[0],
ref_position=-1,
xyz_base=None,
camera_views=[3, 6, 9, 12, 15], # camera views are relative views, not abs
split="train",
image_size=256,
random_background=False,
resize_rate=1,
num_frames=5,
repeat=100,
outer_file=None,
debug=False,
eval_size=100,
stroke_p=0.3,
resize_range=None,
):
print(__class__)
super().__init__(
base_dir,
caption_csv,
ref_indexs=ref_indexs,
ref_position=ref_position,
xyz_base=xyz_base,
camera_views=camera_views,
split=split,
image_size=image_size,
random_background=random_background,
resize_rate=resize_rate,
num_frames=num_frames,
repeat=repeat,
outer_file=outer_file,
debug=debug,
eval_size=eval_size,
)
self.stroke_p = stroke_p
assert (
resize_range is None or len(resize_range) == 2
), "resize_range should be a tuple of 2 elements"
self.resize_range = resize_range
def __len__(self):
return len(self.items) * self.repeat
def __getitem__(self, index):
index_mapping = self.index_mapping
transfroms_mapping = self.transfroms_mapping
index = index % len(self.items)
target_dir = self.items[index]["path"]
target_xyz_dir = self.items[index]["xyz_path"]
caption = self.items[index]["caption"]
bg_color = np.random.rand() * 255
target_images = []
target_xyz_images = []
raw_images = []
raw_xyz_images = []
alpha_masks = []
ref_index = random.choice(self.ref_indexs)
cur_index_mapping = index_mapping[ref_index]
cur_transfroms_mapping = transfroms_mapping[ref_index]
cur_xyz_transfroms_mapping = self.xyz_transforms_mapping[ref_index]
cur_resize_rate = (
random.uniform(*self.resize_range) * self.resize_rate
if self.resize_range is not None
else self.resize_rate
)
for relative_view in self.camera_views:
image_index = cur_index_mapping[relative_view]
trans = cur_transfroms_mapping[relative_view]
trans_xyz = cur_xyz_transfroms_mapping[relative_view]
# open
img = Image.open(
os.path.join(target_dir, f"{image_index:03d}.png")
).convert("RGBA")
if trans is not None:
img = trans(img)
img = do_resize_content(img, cur_resize_rate)
alpha_mask = img.getchannel("A")
alpha_masks.append(alpha_mask)
if self.random_background:
img = add_random_background(img, bg_color)
img = img.convert("RGB")
target_images.append(self.transfrom_vae(img))
raw_images.append(img)
if self.xyz_base is not None:
img_xyz = Image.open(
os.path.join(target_xyz_dir, f"xyz_new_{image_index:03d}.png")
).convert("RGBA")
img_xyz = trans_xyz(img_xyz) if trans_xyz is not None else img_xyz
img_xyz = trans(img_xyz) if trans is not None else img_xyz
img_xyz = do_resize_content(img_xyz, cur_resize_rate)
img_xyz.putalpha(alpha_mask)
if self.random_background:
img_xyz = add_random_background(img_xyz, bg_color)
img_xyz = img_xyz.convert("RGB")
target_xyz_images.append(self.transfrom_vae(img_xyz))
if self.debug:
raw_xyz_images.append(img_xyz)
cameras = [get_camera_for_index(i).squeeze() for i in self.camera_views]
if self.ref_position is not None:
cameras[self.ref_position] = torch.zeros_like(
cameras[self.ref_position]
) # set ref camera to zero
cameras = torch.stack(cameras)
input_img = Image.open(
os.path.join(target_dir, f"{ref_index:03d}.png")
).convert("RGBA")
input_img = do_resize_content(input_img, cur_resize_rate)
if random.random() < self.stroke_p:
## random rgb color
color = (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255),
)
radius = random.randint(1, 3)
input_img = add_stroke(input_img, color=color, stroke_radius=radius)
if self.random_background:
input_img = add_random_background(input_img, bg_color)
input_img = input_img.convert("RGB")
clip_cond = self.transfrom_clip(input_img)
vae_cond = self.transfrom_vae(input_img)
vae_target = torch.stack(target_images, dim=0)
if self.xyz_base is not None:
xyz_vae_target = torch.stack(target_xyz_images, dim=0)
else:
xyz_vae_target = []
if self.debug:
print(f"debug!!,{bg_color}")
return {
"target_images": raw_images,
"target_images_xyz": raw_xyz_images,
"input_img": input_img,
"cameras": cameras,
"caption": caption,
"item": self.items[index],
"alpha_masks": alpha_masks,
"cur_resize_rate": cur_resize_rate,
}
if self.split == "train":
return {
"target_images_vae": vae_target,
"target_images_xyz_vae": xyz_vae_target,
"clip_cond": clip_cond,
"vae_cond": vae_cond,
"cameras": cameras,
"caption": caption,
}
else: # eval
path = os.path.join(target_dir, f"{ref_index:03d}.png")
return dict(
path=path,
target_dir=target_dir,
cond_raw_images=raw_images,
cond=input_img,
ref_index=ref_index,
ident=f"{index}-{Path(target_dir).stem}",
)
class InTheWildImages(Dataset):
"""
a data set for in the wild images,
receive base floders, image path ls, path files as input
"""
def __init__(self, base_dirs=[], image_paths=[], path_files=[]):
print(__class__)
self.base_dirs = base_dirs
self.image_paths = image_paths
self.path_files = path_files
self.init_item()
def init_item(self):
items = []
for d in self.base_dirs:
items += [osp.join(d, f) for f in os.listdir(d)]
items = items + self.image_paths
for file in self.path_files:
with open(file, "r") as f:
items += [line.strip() for line in f.readlines()]
items.sort()
self.items = items
def __len__(self):
return len(self.items)
def __getitem__(self, index):
item = self.items[index]
img = Image.open(item)
background = Image.new("RGBA", img.size, (0, 0, 0, 0))
cond = Image.alpha_composite(background, img)
return dict(
path=item, ident=f"{index}-{Path(item).stem}", cond=cond.convert("RGB")
)
|