IOPaint/iopaint/model/lama.py

58 lines
1.6 KiB
Python
Raw Normal View History

2022-04-15 18:11:51 +02:00
import os
import cv2
import numpy as np
import torch
2024-01-05 08:19:23 +01:00
from iopaint.helper import (
2023-02-14 02:08:56 +01:00
norm_img,
get_cache_path_by_url,
load_jit_model,
2023-11-16 14:12:06 +01:00
download_model,
2023-02-14 02:08:56 +01:00
)
2024-01-05 08:19:23 +01:00
from iopaint.schema import InpaintRequest
2024-01-05 09:40:06 +01:00
from .base import InpaintModel
2022-04-15 18:11:51 +02:00
LAMA_MODEL_URL = os.environ.get(
"LAMA_MODEL_URL",
"https://github.com/Sanster/models/releases/download/add_big_lama/big-lama.pt",
)
2023-02-26 02:19:48 +01:00
LAMA_MODEL_MD5 = os.environ.get("LAMA_MODEL_MD5", "e3aa4aaa15225a33ec84f9f4bc47e500")
2022-04-15 18:11:51 +02:00
class LaMa(InpaintModel):
2023-02-11 06:30:09 +01:00
name = "lama"
2022-04-15 18:11:51 +02:00
pad_mod = 8
2023-12-01 03:15:35 +01:00
is_erase_model = True
2022-04-15 18:11:51 +02:00
2023-11-16 14:12:06 +01:00
@staticmethod
def download():
download_model(LAMA_MODEL_URL, LAMA_MODEL_MD5)
2022-09-15 16:21:27 +02:00
def init_model(self, device, **kwargs):
2023-02-26 02:19:48 +01:00
self.model = load_jit_model(LAMA_MODEL_URL, device, LAMA_MODEL_MD5).eval()
2022-04-17 17:31:12 +02:00
@staticmethod
def is_downloaded() -> bool:
return os.path.exists(get_cache_path_by_url(LAMA_MODEL_URL))
2022-04-15 18:11:51 +02:00
2023-12-30 16:36:44 +01:00
def forward(self, image, mask, config: InpaintRequest):
2022-04-15 18:11:51 +02:00
"""Input image and output image have same size
image: [H, W, C] RGB
mask: [H, W]
return: BGR IMAGE
"""
image = norm_img(image)
mask = norm_img(mask)
mask = (mask > 0) * 1
image = torch.from_numpy(image).unsqueeze(0).to(self.device)
mask = torch.from_numpy(mask).unsqueeze(0).to(self.device)
inpainted_image = self.model(image, mask)
cur_res = inpainted_image[0].permute(1, 2, 0).detach().cpu().numpy()
cur_res = np.clip(cur_res * 255, 0, 255).astype("uint8")
cur_res = cv2.cvtColor(cur_res, cv2.COLOR_RGB2BGR)
return cur_res