mirror of
https://github.com/vladmandic/automatic
synced 2026-09-20 01:31:13 +02:00
refactor all control processors to support unload and offload
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import os
|
||||
import warnings
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import torch
|
||||
from huggingface_hub import hf_hub_download
|
||||
from PIL import Image
|
||||
|
||||
from modules import devices
|
||||
from modules.shared import opts
|
||||
from modules.control.util import HWC3, resize_image
|
||||
from .models.mbv2_mlsd_large import MobileV2_MLSD_Large
|
||||
from .utils import pred_lines
|
||||
@@ -22,16 +21,13 @@ class MLSDdetector:
|
||||
filename = filename or "annotator/ckpts/mlsd_large_512_fp32.pth"
|
||||
else:
|
||||
filename = filename or "mlsd_large_512_fp32.pth"
|
||||
|
||||
if os.path.isdir(pretrained_model_or_path):
|
||||
model_path = os.path.join(pretrained_model_or_path, filename)
|
||||
else:
|
||||
model_path = hf_hub_download(pretrained_model_or_path, filename, cache_dir=cache_dir)
|
||||
|
||||
model = MobileV2_MLSD_Large()
|
||||
model.load_state_dict(torch.load(model_path), strict=True)
|
||||
model.eval()
|
||||
|
||||
return cls(model)
|
||||
|
||||
def to(self, device):
|
||||
@@ -39,20 +35,11 @@ class MLSDdetector:
|
||||
return self
|
||||
|
||||
def __call__(self, input_image, thr_v=0.1, thr_d=0.1, detect_resolution=512, image_resolution=512, output_type="pil", **kwargs):
|
||||
if "return_pil" in kwargs:
|
||||
warnings.warn("return_pil is deprecated. Use output_type instead.", DeprecationWarning)
|
||||
output_type = "pil" if kwargs["return_pil"] else "np"
|
||||
if type(output_type) is bool:
|
||||
warnings.warn("Passing `True` or `False` to `output_type` is deprecated and will raise an error in future versions")
|
||||
if output_type:
|
||||
output_type = "pil"
|
||||
|
||||
self.model.to(devices.device)
|
||||
if not isinstance(input_image, np.ndarray):
|
||||
input_image = np.array(input_image, dtype=np.uint8)
|
||||
|
||||
input_image = HWC3(input_image)
|
||||
input_image = resize_image(input_image, detect_resolution)
|
||||
|
||||
assert input_image.ndim == 3
|
||||
img = input_image
|
||||
img_output = np.zeros_like(img)
|
||||
@@ -63,16 +50,13 @@ class MLSDdetector:
|
||||
cv2.line(img_output, (x_start, y_start), (x_end, y_end), [255, 255, 255], 1)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
detected_map = img_output[:, :, 0]
|
||||
detected_map = HWC3(detected_map)
|
||||
|
||||
img = resize_image(input_image, image_resolution)
|
||||
H, W, C = img.shape
|
||||
|
||||
H, W, _C = img.shape
|
||||
detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)
|
||||
|
||||
if output_type == "pil":
|
||||
detected_map = Image.fromarray(detected_map)
|
||||
|
||||
if opts.control_move_processor:
|
||||
self.model.to('cpu')
|
||||
return detected_map
|
||||
|
||||
Reference in New Issue
Block a user