This commit is contained in:
abyz22
2024-02-04 05:09:52 +09:00
parent 6f06232483
commit ced7862086
8 changed files with 7458 additions and 6404 deletions
+6
View File
@@ -25,9 +25,12 @@ NODE_CLASS_MAPPINGS = {
"abyz22_bypass":abyz22_bypass,
"abyz22_smallhead":abyz22_smallhead,
"abyz22_lamaPreprocessor":abyz22_lamaPreprocessor,
"abyz22_lamaInpaint":abyz22_lamaInpaint,
"abyz22_makecircles":abyz22_makecircles,
"abyz22_Topipe":abyz22_Topipe,
"abyz22_Frompipe":abyz22_Frompipe,
"abyz22_Editpipe":abyz22_Editpipe,
"abyz22_Convertpipe":abyz22_Convertpipe,
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
@@ -48,7 +51,10 @@ NODE_DISPLAY_NAME_MAPPINGS = {
"abyz22_bypass":"abyz22_bypass",
"abyz22_smallhead":"abyz22_smallhead",
"abyz22_lamaPreprocessor":"abyz22_lamaPreprocessor",
"abyz22_lamaInpaint":"abyz22_lamaInpaint",
"abyz22_makecircles":"abyz22_makecircles",
"abyz22_Topipe":"abyz22_Topipe",
"abyz22_Frompipe":"abyz22_Frompipe",
"abyz22_Editpipe":"abyz22_Editpipe",
"abyz22_Convertpipe":"abyz22_Convertpipe",
}
+2 -2
View File
@@ -65,9 +65,9 @@ class LamaInpainting:
image_feed = torch.cat([color, mask], dim=2)
image_feed = rearrange(image_feed, 'h w c -> 1 c h w')
print('before : ',image_feed.shape)
# print('before : ',image_feed.shape)
result = self.model(image_feed)[0]
print('after : ', result.shape)
# print('after : ', result.shape)
result = rearrange(result, 'c h w -> h w c')
result = result * mask + color * (1 - mask)
result *= 255.0
+7263 -6382
View File
File diff suppressed because it is too large Load Diff
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.4 MiB

After

Width:  |  Height:  |  Size: 10 MiB

+1 -1
View File
@@ -21,7 +21,7 @@ from PIL import ImageDraw
# CATEGORY = "abyz22"
# def run(sefl,*args,**kwargs):
# def run(self,*args,**kwargs):
# return None
+92 -6
View File
@@ -14,6 +14,7 @@ from PIL import Image
from numpy import dtype
from scipy import ndimage
from torch import device
import nodes
# Get the absolute path of various directories
my_dir = os.path.dirname(os.path.abspath(__file__))
@@ -358,18 +359,18 @@ class abyz22_lamaPreprocessor:
): # pixel= 1,768,512,3
# 모드 설정
# print("☆★ " * 20)
if Width==0 and Height==0:
if Width == 0 and Height == 0:
encoded_image = self._encode_image(vae, pixels.to("cuda" if torch.cuda.is_available() else "cpu"))
encoded_image_dict = {"samples": encoded_image.cpu()}
mask_with_1 = torch.ones_like(pixels[:, :, :, 0])
encoded_image_dict = SetLatentNoiseMask().set_mask(encoded_image_dict, mask_with_1)[0]
return (pixels.to("cuda" if torch.cuda.is_available() else "cpu"),encoded_image_dict)
return (pixels.to("cuda" if torch.cuda.is_available() else "cpu"), encoded_image_dict)
np.random.seed(seed)
random.seed(seed)
model_lama = LamaInpainting()
image,imgs, masks = None, None,None
image, imgs, masks = None, None, None
if Width_Ratio_min > Width_Ratio_max:
Width_Ratio_min, Width_Ratio_max = Width_Ratio_max, Width_Ratio_min
if Height_Ratio_min > Height_Ratio_max:
@@ -382,7 +383,7 @@ class abyz22_lamaPreprocessor:
Down, Right = int(Height - Up), int(Width - Left)
copy_img = pixels.clone()[i, :, :, :]
final_img_with_alpha = copy_img*255.0
final_img_with_alpha = copy_img * 255.0
if Up + Down > 0:
image = torch.rand((pixels.shape[1] + Up + Down, pixels.shape[2], pixels.shape[3]))
image[Up : Up + pixels.shape[1], :, :] = copy_img
@@ -480,3 +481,88 @@ class abyz22_lamaPreprocessor:
)
FUNCTION = "preprocess"
CATEGORY = "abyz22"
class abyz22_lamaInpaint:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"pixels": ("IMAGE",), # N,H,W,C
"vae": ("VAE",),
},
"optional": {
"mask": ("MASK",), # N,H,W
"mask_(img)": ("IMAGE",),
},
}
RETURN_TYPES = (
"IMAGE",
"LATENT",
"IMAGE",
)
RETURN_NAMES = (
"LaMa Preprocessed Image",
"LaMa Preprocessed Latent",
"LaMa Image",
)
FUNCTION = "preprocess"
CATEGORY = "abyz22"
FUNCTION = "run"
def _encode_image(self, vae, image):
# encoder = VAEEncode()
wrapper = VAEWrapper(vae)
image = image[:, :, :, 0:3]
image_without_alpha = image.to(wrapper.vae.vae_dtype) * 2.0 - 1.0
image = image_without_alpha.permute(0, 3, 1, 2)
# image = rearrange(image_without_alpha, "1 w h c -> 1 c w h")
encoded_image = wrapper.encode_first_stage_(image)
if torch.all(torch.isnan(encoded_image.mean())):
print("All values produced are NANs, automatically upcasting dtype to float32")
wrapper.to(torch.float32)
encoded_image = wrapper.encode_first_stage_(image)
vae_output = wrapper.get_first_stage_encoding_(encoded_image)
return vae_output
def run(self, *args, **kwargs):
pixels, vae = kwargs["pixels"], kwargs["vae"]
if kwargs.get("mask") is not None:
mask = kwargs.get("mask")
elif kwargs.get("mask_(img)") is not None:
# print(len(kwargs.get("mask_(img)")))
# print(kwargs.get("mask_(img)")[0].shape)
mask = kwargs.get("mask_(img)")[0][:, :, :, 0]
else:
mask = torch.zeros_like(pixels)[:, :, :, 0]
print("mask shape : ", mask.shape, mask.min(), mask.max())
# 이미지 크기/값 lama에 맞추기 (0.0~1.0 -> 0~255)
for i in range(pixels.shape[0]):
image_with_alpha = (torch.cat([pixels[i], mask[i].unsqueeze(-1)], -1).numpy() * 255).astype(np.uint8) # 알파채널 합침
model_lama = LamaInpainting()
final_img = model_lama(image_with_alpha)
final_img = torch.from_numpy(final_img) / 255.0
print(final_img.shape)
final_img = torchvision.transforms.Resize((final_img.shape[0:2]))(final_img.permute(2, 0, 1)).permute(1, 2, 0)
final_img = final_img.unsqueeze(0)
lama_image=final_img.clone().to("cuda" if torch.cuda.is_available() else "cpu")
encoded_image = self._encode_image(vae, final_img.to("cuda" if torch.cuda.is_available() else "cpu"))
encoded_image_dict = {"samples": encoded_image.cpu()}
# mask = torch.ones_like(pixels[:, :, :, 0])
latent = SetLatentNoiseMask().set_mask(encoded_image_dict, mask)[0]
latent = nodes.VAEEncode().encode(vae, final_img)[0]
# latent = nodes.VAEEncodeForInpaint().encode(vae, final_img, mask, grow_mask_by=6)[0]
print('before concat : ',final_img.shape,mask.unsqueeze(-1).shape)
final_img = torch.cat([final_img, mask.unsqueeze(-1)], -1)
print(final_img.shape, final_img.min(), final_img.max())
c = final_img[:, :, :, 0:3]
m = final_img[:, :, :, 3:4]
m = (m > 0.5).float()
final_img = c * (1 - m) - m
print(final_img.shape, final_img.min(), final_img.max())
return (final_img.to("cuda" if torch.cuda.is_available() else "cpu"), latent,lama_image)
+3 -3
View File
@@ -79,7 +79,7 @@ class abyz22_Pad_Image:
pose_image = obj.estimate_pose(
image, "disable", "enable", "disable", resolution=resolution, bbox_detector="yolox_s.onnx", pose_estimator="dw-ss_ucoco.onnx"
)["result"][0]
padded_image,padded_pose_image=None,None
padded_image, padded_pose_image = None, None
# Resize_by = random.uniform(Ratio_min, Ratio_max)
for i, Resize_by in enumerate(Resize_bys):
@@ -93,7 +93,7 @@ class abyz22_Pad_Image:
# resized_image = 3,768,512
dx, dy = abs(image.shape[2] - resized_image.shape[2]), abs(image.shape[1] - resized_image.shape[1])
rdx, rdy = random.randint(0, dx), random.randint(0, dy)
if Resize_by < 1:
if Resize_by <= 1:
mode_list = {
"Top-Left": (0, dx, 0, dy),
"Top": (int(round(dx / 2)), int(round(dx / 2)) + 1, 0, dy),
@@ -162,7 +162,7 @@ class abyz22_Pad_Image:
final_image = padded_image
final_pose_image = padded_pose_image
else:
final_image = torch.cat((final_image, padded_image))
final_image = torch.cat((final_image, padded_image)) # TODO 여기서 문제 발생? expected Tensor as element 0 in argument 0, but got NoneType
final_pose_image = torch.cat((final_pose_image, padded_pose_image))
latent = nodes.VAEEncode().encode(vae, final_image)[0]
+91 -10
View File
@@ -39,15 +39,15 @@ class abyz22_Topipe:
CATEGORY = "abyz22"
def run(sefl, *args, **kwargs):
pipe={}
pipe['MODEL']=kwargs.get('MODEL')
pipe['CLIP']=kwargs.get('CLIP')
pipe['VAE']=kwargs.get('VAE')
pipe['POSITIVE']=kwargs.get('Positive')
pipe['NEGATIVE']=kwargs.get('Negative')
pipe['IMAGE']=kwargs.get('IMAGE')
pipe['latent_image']=kwargs.get('latent_Image')
pipe['latent']=kwargs.get('latent')
pipe = {}
pipe["MODEL"] = kwargs.get("MODEL")
pipe["CLIP"] = kwargs.get("CLIP")
pipe["VAE"] = kwargs.get("VAE")
pipe["POSITIVE"] = kwargs.get("Positive")
pipe["NEGATIVE"] = kwargs.get("Negative")
pipe["IMAGE"] = kwargs.get("IMAGE")
pipe["latent_image"] = kwargs.get("latent_Image")
pipe["latent"] = kwargs.get("latent")
return (pipe,)
@@ -62,6 +62,7 @@ class abyz22_Frompipe:
}
RETURN_TYPES = (
"PIPE",
"MODEL",
"CLIP",
"VAE",
@@ -72,6 +73,7 @@ class abyz22_Frompipe:
"LATENT",
)
RETURN_NAMES = (
"pipe",
"MODEL",
"CLIP",
"VAE",
@@ -96,7 +98,86 @@ class abyz22_Frompipe:
image = pipe.get("IMAGE")
latent_image = pipe.get("latent_image")
latent = pipe.get("latent")
return (model, clip, vae, positive, negative, image, latent_image, latent,)
return (pipe, model, clip, vae, positive, negative, image, latent_image, latent)
class abyz22_Editpipe:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"pipe": ("PIPE",),
},
"optional": {
"MODEL": ("MODEL",),
"CLIP": ("CLIP",),
"VAE": ("VAE",),
"Positive": ("CONDITIONING",),
"Negative": ("CONDITIONING",),
"IMAGE": ("IMAGE",),
"latent_Image": ("LATENT",),
"latent": ("LATENT",),
},
}
RETURN_TYPES = ("PIPE",)
RETURN_NAMES = ("pipe",)
FUNCTION = "run"
CATEGORY = "abyz22"
def run(sefl, *args, **kwargs):
pipe = kwargs["pipe"]
if kwargs.get("MODEL") is not None:
pipe["MODEL"] = kwargs["MODEL"]
if kwargs.get("CLIP") is not None:
pipe["CLIP"] = kwargs["CLIP"]
if kwargs.get("VAE") is not None:
pipe["VAE"] = kwargs["VAE"]
if kwargs.get("Positive") is not None:
pipe["POSITIVE"] = kwargs["Positive"]
if kwargs.get("Negative") is not None:
pipe["NEGATIVE"] = kwargs["Negative"]
if kwargs.get("IMAGE") is not None:
pipe["IMAGE"] = kwargs["IMAGE"]
if kwargs.get("latent_Image") is not None:
pipe["latent_image"] = kwargs["latent_Image"]
return (pipe,)
class abyz22_Convertpipe:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"pipe": ("PIPE",),
},
}
RETURN_TYPES = ("BASIC_PIPE",)
RETURN_NAMES = ("basic_pipe",)
FUNCTION = "run"
CATEGORY = "abyz22"
def run(sefl, *args, **kwargs):
pipe = kwargs["pipe"]
model=pipe['MODEL']
clip=pipe['CLIP']
vae=pipe['VAE']
positive=pipe['POSITIVE']
negative=pipe['NEGATIVE']
basic_pipe=(model,clip,vae,positive,negative)
return (basic_pipe,)
def tensor_convert_rgba(image, prefer_copy=True):