Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PuLID #2838

Merged
merged 7 commits into from
May 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Resize img before passing to facexlib
  • Loading branch information
huchenlei committed May 4, 2024
commit a42b991fbe3fd45d99645db7b665785b225522da
19 changes: 17 additions & 2 deletions scripts/preprocessor/pulid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from torchvision.transforms.functional import normalize

from ..supported_preprocessor import Preprocessor, PreprocessorParameter
from scripts.utils import npimg2tensor, tensor2npimg
from scripts.utils import npimg2tensor, tensor2npimg, resize_image_with_pad


def to_gray(img):
Expand Down Expand Up @@ -43,11 +43,13 @@ def load_model(self):
self.model.face_det.to(device=self.device)
return self.model

def unload(self):
def unload(self) -> bool:
"""@Override"""
if self.model is not None:
self.model.face_parse.to(device="cpu")
self.model.face_det.to(device="cpu")
return True
return False

def __call__(
self,
Expand All @@ -66,6 +68,7 @@ def __call__(
"""
self.load_model()
self.model.clean_all()
input_image, _ = resize_image_with_pad(input_image, resolution)
# using facexlib to detect and align face
image_bgr = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
self.model.read_image(image_bgr)
Expand Down Expand Up @@ -107,6 +110,11 @@ def __init__(self):
self.tags = ["IP-Adapter"]
self.slider_resolution = PreprocessorParameter(visible=False)
self.returns_image = False
self.preprocessors_deps = [
"facexlib",
"instant_id_face_embedding",
"EVA02-CLIP-L-14-336",
]

def facexlib_detect(self, input_image: np.ndarray) -> torch.Tensor:
facexlib_preprocessor = Preprocessor.get_preprocessor("facexlib")
Expand All @@ -118,6 +126,13 @@ def insightface_antelopev2_detect(self, input_image: np.ndarray) -> torch.Tensor
)
return antelopev2_preprocessor(input_image)

def unload(self) -> bool:
unloaded = False
for p_name in self.preprocessors_deps:
p = Preprocessor.get_preprocessor(p_name)
unloaded = unloaded or p.unload()
return unloaded

def __call__(
self,
input_image,
Expand Down
Loading