feat: add model patching

automatically unload model when not needed anymore
This commit is contained in:
Manuel Schmid
2024-01-26 02:05:38 +01:00
parent 860ac91b16
commit 90be73a6df
2 changed files with 106 additions and 18 deletions
+2 -18
View File
@@ -2,31 +2,15 @@ from PIL import Image
import numpy as np
import torch
from rembg import remove, new_session
from groundingdino.util.inference import Model as GroundingDinoModel
from extras.GroundingDINO.util.inference import default_groundingdino
from modules.model_loader import load_file_from_url
from modules.config import path_inpaint
config_file = 'extras/GroundingDINO/config/GroundingDINO_SwinT_OGC.py'
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
groundingdino_model = None
def run_grounded_sam(input_image, text_prompt, box_threshold, text_threshold):
global groundingdino_model
if groundingdino_model is None:
filename = load_file_from_url(
url="https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth",
model_dir=path_inpaint)
groundingdino_model = GroundingDinoModel(model_config_path=config_file, model_checkpoint_path=filename, device=device)
# run grounding dino model
boxes, _ = groundingdino_model.predict_with_caption(
boxes, _ = default_groundingdino(
image=np.array(input_image),
caption=text_prompt,
box_threshold=box_threshold,