50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
|
|
import numpy as np
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
# 1) util func
|
|
# -------------------------------------------------------------------------
|
|
def c_crop(image):
|
|
width, height = image.size
|
|
new_size = min(width, height)
|
|
left = (width - new_size) / 2
|
|
top = (height - new_size) / 2
|
|
right = (width + new_size) / 2
|
|
bottom = (height + new_size) / 2
|
|
return image.crop((left, top, right, bottom))
|
|
|
|
|
|
def pad64(x):
|
|
return int(np.ceil(float(x) / 64.0) * 64 - x)
|
|
|
|
|
|
def HWC3(x):
|
|
assert x.dtype == np.uint8
|
|
if x.ndim == 2:
|
|
x = x[:, :, None]
|
|
assert x.ndim == 3
|
|
H, W, C = x.shape
|
|
assert C == 1 or C == 3 or C == 4
|
|
if C == 3:
|
|
return x
|
|
if C == 1:
|
|
return np.concatenate([x, x, x], axis=2)
|
|
if C == 4:
|
|
color = x[:, :, 0:3].astype(np.float32)
|
|
alpha = x[:, :, 3:4].astype(np.float32) / 255.0
|
|
y = color * alpha + 255.0 * (1.0 - alpha)
|
|
y = y.clip(0, 255).astype(np.uint8)
|
|
return y
|
|
|
|
|
|
def print_load_warning(missing: list = None, unexpected: list = None) -> None:
|
|
if missing is not None and unexpected is not None and len(missing) > 0 and len(unexpected) > 0:
|
|
print(f"Got {len(missing)} missing keys:\n\t" + "\n\t".join(missing))
|
|
print("\n" + "-" * 79 + "\n")
|
|
print(f"Got {len(unexpected)} unexpected keys:\n\t" + "\n\t".join(unexpected))
|
|
elif missing is not None and len(missing) > 0:
|
|
print(f"Got {len(missing)} missing keys:\n\t" + "\n\t".join(missing))
|
|
elif unexpected is not None and len(unexpected) > 0:
|
|
print(f"Got {len(unexpected)} unexpected keys:\n\t" + "\n\t".join(unexpected))
|