IOPaint/lama_cleaner/schema.py

225 lines
7.1 KiB
Python
Raw Normal View History

2023-12-11 15:28:07 +01:00
from typing import Optional, List
2022-04-15 18:11:51 +02:00
from enum import Enum
2022-12-10 15:06:15 +01:00
from PIL.Image import Image
2023-12-11 15:28:07 +01:00
from pydantic import BaseModel, computed_field
from lama_cleaner.const import (
SDXL_CONTROLNET_CHOICES,
SD2_CONTROLNET_CHOICES,
SD_CONTROLNET_CHOICES,
)
2022-04-15 18:11:51 +02:00
2023-12-01 03:15:35 +01:00
DIFFUSERS_SD_CLASS_NAME = "StableDiffusionPipeline"
DIFFUSERS_SD_INPAINT_CLASS_NAME = "StableDiffusionInpaintPipeline"
DIFFUSERS_SDXL_CLASS_NAME = "StableDiffusionXLPipeline"
DIFFUSERS_SDXL_INPAINT_CLASS_NAME = "StableDiffusionXLInpaintPipeline"
class ModelType(str, Enum):
INPAINT = "inpaint" # LaMa, MAT...
DIFFUSERS_SD = "diffusers_sd"
DIFFUSERS_SD_INPAINT = "diffusers_sd_inpaint"
DIFFUSERS_SDXL = "diffusers_sdxl"
DIFFUSERS_SDXL_INPAINT = "diffusers_sdxl_inpaint"
DIFFUSERS_OTHER = "diffusers_other"
FREEU_DEFAULT_CONFIGS = {
ModelType.DIFFUSERS_SD: dict(s1=0.9, s2=0.2, b1=1.2, b2=1.4),
ModelType.DIFFUSERS_SDXL: dict(s1=0.6, s2=0.4, b1=1.1, b2=1.2),
}
class ModelInfo(BaseModel):
name: str
path: str
model_type: ModelType
is_single_file_diffusers: bool = False
2023-12-11 15:28:07 +01:00
@computed_field
@property
def need_prompt(self) -> bool:
return self.model_type in [
ModelType.DIFFUSERS_SD,
ModelType.DIFFUSERS_SDXL,
ModelType.DIFFUSERS_SD_INPAINT,
ModelType.DIFFUSERS_SDXL_INPAINT,
] or self.name in [
"timbrooks/instruct-pix2pix",
"kandinsky-community/kandinsky-2-2-decoder-inpaint",
]
@computed_field
@property
def controlnets(self) -> List[str]:
if self.model_type in [
ModelType.DIFFUSERS_SDXL,
ModelType.DIFFUSERS_SDXL_INPAINT,
]:
return SDXL_CONTROLNET_CHOICES
if self.model_type in [ModelType.DIFFUSERS_SD, ModelType.DIFFUSERS_SD_INPAINT]:
if self.name in ["stabilityai/stable-diffusion-2-inpainting"]:
return SD2_CONTROLNET_CHOICES
else:
return SD_CONTROLNET_CHOICES
return []
@computed_field
@property
2023-12-01 03:15:35 +01:00
def support_lcm_lora(self) -> bool:
return self.model_type in [
ModelType.DIFFUSERS_SD,
ModelType.DIFFUSERS_SDXL,
ModelType.DIFFUSERS_SD_INPAINT,
ModelType.DIFFUSERS_SDXL_INPAINT,
]
2023-12-11 15:28:07 +01:00
@computed_field
@property
2023-12-01 03:15:35 +01:00
def support_controlnet(self) -> bool:
return self.model_type in [
ModelType.DIFFUSERS_SD,
ModelType.DIFFUSERS_SDXL,
ModelType.DIFFUSERS_SD_INPAINT,
ModelType.DIFFUSERS_SDXL_INPAINT,
]
2023-12-11 15:28:07 +01:00
@computed_field
@property
2023-12-01 03:15:35 +01:00
def support_freeu(self) -> bool:
return (
self.model_type
in [
ModelType.DIFFUSERS_SD,
ModelType.DIFFUSERS_SDXL,
ModelType.DIFFUSERS_SD_INPAINT,
ModelType.DIFFUSERS_SDXL_INPAINT,
]
2023-12-11 15:28:07 +01:00
or "timbrooks/instruct-pix2pix" in self.name
2023-12-01 03:15:35 +01:00
)
2022-04-15 18:11:51 +02:00
class HDStrategy(str, Enum):
2022-11-24 02:29:13 +01:00
# Use original image size
2022-09-22 15:50:41 +02:00
ORIGINAL = "Original"
2022-11-24 02:29:13 +01:00
# Resize the longer side of the image to a specific size(hd_strategy_resize_limit),
# then do inpainting on the resized image. Finally, resize the inpainting result to the original size.
# The area outside the mask will not lose quality.
2022-09-22 15:50:41 +02:00
RESIZE = "Resize"
2022-11-24 02:29:13 +01:00
# Crop masking area(with a margin controlled by hd_strategy_crop_margin) from the original image to do inpainting
2022-09-22 15:50:41 +02:00
CROP = "Crop"
2022-04-15 18:11:51 +02:00
2022-06-12 07:14:17 +02:00
class LDMSampler(str, Enum):
2022-09-22 15:50:41 +02:00
ddim = "ddim"
plms = "plms"
2022-06-12 07:14:17 +02:00
2022-09-22 06:38:32 +02:00
class SDSampler(str, Enum):
2022-09-22 15:50:41 +02:00
ddim = "ddim"
pndm = "pndm"
2022-10-15 16:32:25 +02:00
k_lms = "k_lms"
2023-03-19 15:40:23 +01:00
k_euler = "k_euler"
k_euler_a = "k_euler_a"
dpm_plus_plus = "dpm++"
uni_pc = "uni_pc"
2022-09-22 06:38:32 +02:00
2023-11-15 01:50:35 +01:00
lcm = "lcm"
2022-09-22 06:38:32 +02:00
2023-11-14 15:04:16 +01:00
class FREEUConfig(BaseModel):
s1: float = 1.0
s2: float = 1.0
b1: float = 1.0
b2: float = 1.0
2022-04-15 18:11:51 +02:00
class Config(BaseModel):
2022-12-10 15:06:15 +01:00
class Config:
arbitrary_types_allowed = True
2022-11-24 02:29:13 +01:00
# Configs for ldm model
2022-04-15 18:11:51 +02:00
ldm_steps: int
2022-07-14 10:49:03 +02:00
ldm_sampler: str = LDMSampler.plms
2022-11-24 02:29:13 +01:00
# Configs for zits model
2022-07-14 10:49:03 +02:00
zits_wireframe: bool = True
2022-11-24 02:29:13 +01:00
# Configs for High Resolution Strategy(different way to preprocess image)
hd_strategy: str # See HDStrategy Enum
2022-04-15 18:11:51 +02:00
hd_strategy_crop_margin: int
2022-11-24 02:29:13 +01:00
# If the longer side of the image is larger than this value, use crop strategy
2022-04-15 18:11:51 +02:00
hd_strategy_crop_trigger_size: int
hd_strategy_resize_limit: int
2022-09-15 16:21:27 +02:00
2022-11-24 02:29:13 +01:00
# Configs for Stable Diffusion 1.5
2022-09-22 15:50:41 +02:00
prompt: str = ""
2022-11-08 14:58:48 +01:00
negative_prompt: str = ""
2022-11-24 02:29:13 +01:00
# Crop image to this size before doing sd inpainting
# The value is always on the original image scale
2022-09-15 16:21:27 +02:00
use_croper: bool = False
2023-08-30 07:28:31 +02:00
croper_is_outpainting: bool = False
2022-09-15 16:21:27 +02:00
croper_x: int = None
croper_y: int = None
croper_height: int = None
croper_width: int = None
2023-01-05 15:07:39 +01:00
# Resize the image before doing sd inpainting, the area outside the mask will not lose quality.
# Used by sd models and paint_by_example model
sd_scale: float = 1.0
2022-11-24 02:29:13 +01:00
# Blur the edge of mask area. The higher the number the smoother blend with the original image
2022-09-22 15:50:41 +02:00
sd_mask_blur: int = 0
2023-11-14 07:19:56 +01:00
# Indicates extent to transform the reference `image`. Must be between 0 and 1. `image` is used as a
# starting point and more noise is added the higher the `strength`. The number of denoising steps depends
# on the amount of noise initially added. When `strength` is 1, added noise is maximum and the denoising
# process runs for the full number of iterations specified in `num_inference_steps`. A value of 1
# essentially ignores `image`.
sd_strength: float = 1.0
2022-11-24 02:29:13 +01:00
# The number of denoising steps. More denoising steps usually lead to a
# higher quality image at the expense of slower inference.
2022-09-15 16:21:27 +02:00
sd_steps: int = 50
2022-11-24 02:29:13 +01:00
# Higher guidance scale encourages to generate images that are closely linked
# to the text prompt, usually at the expense of lower image quality.
2022-09-15 16:21:27 +02:00
sd_guidance_scale: float = 7.5
2023-03-19 15:40:23 +01:00
sd_sampler: str = SDSampler.uni_pc
2022-09-15 16:21:27 +02:00
# -1 mean random seed
sd_seed: int = 42
sd_match_histograms: bool = False
2022-10-09 15:32:13 +02:00
2023-08-30 07:28:31 +02:00
# out-painting
2023-11-14 07:19:56 +01:00
sd_outpainting_softness: float = 20.0
sd_outpainting_space: float = 20.0
2023-08-30 07:28:31 +02:00
2023-11-14 15:04:16 +01:00
# freeu
sd_freeu: bool = False
sd_freeu_config: FREEUConfig = FREEUConfig()
2023-11-15 01:50:35 +01:00
# lcm-lora
sd_lcm_lora: bool = False
2023-11-15 02:10:13 +01:00
# preserving the unmasked area at the expense of some more unnatural transitions between the masked and unmasked areas.
sd_prevent_unmasked_area: bool = True
2022-11-24 02:29:13 +01:00
# Configs for opencv inpainting
# opencv document https://docs.opencv.org/4.6.0/d7/d8b/group__photo__inpaint.html#gga8002a65f5a3328fbf15df81b842d3c3ca05e763003a805e6c11c673a9f4ba7d07
2023-03-19 15:40:23 +01:00
cv2_flag: str = "INPAINT_NS"
2022-10-09 15:32:13 +02:00
cv2_radius: int = 4
2022-12-10 15:06:15 +01:00
# Paint by Example
paint_by_example_steps: int = 50
paint_by_example_guidance_scale: float = 7.5
paint_by_example_mask_blur: int = 0
paint_by_example_seed: int = 42
paint_by_example_match_histograms: bool = False
paint_by_example_example_image: Optional[Image] = None
2023-01-28 14:13:21 +01:00
# InstructPix2Pix
p2p_steps: int = 50
p2p_image_guidance_scale: float = 1.5
p2p_guidance_scale: float = 7.5
2023-03-19 15:40:23 +01:00
# ControlNet
2023-05-18 06:29:45 +02:00
controlnet_conditioning_scale: float = 0.4
2023-05-13 07:45:27 +02:00
controlnet_method: str = "control_v11p_sd15_canny"