mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
78377469db
* Bump diffusers to 0.21.2. * Add T2IAdapterInvocation boilerplate. * Add T2I-Adapter model to model-management. * (minor) Tidy prepare_control_image(...). * Add logic to run the T2I-Adapter models at the start of the DenoiseLatentsInvocation. * Add logic for applying T2I-Adapter weights and accumulating. * Add T2IAdapter to MODEL_CLASSES map. * yarn typegen * Add model probes for T2I-Adapter models. * Add all of the frontend boilerplate required to use T2I-Adapter in the nodes editor. * Add T2IAdapterModel.convert_if_required(...). * Fix errors in T2I-Adapter input image sizing logic. * Fix bug with handling of multiple T2I-Adapters. * black / flake8 * Fix typo * yarn build * Add num_channels param to prepare_control_image(...). * Link to upstream diffusers bugfix PR that currently requires a workaround. * feat: Add Color Map Preprocessor Needed for the color T2I Adapter * feat: Add Color Map Preprocessor to Linear UI * Revert "feat: Add Color Map Preprocessor" This reverts commita1119a00bf
. * Revert "feat: Add Color Map Preprocessor to Linear UI" This reverts commitbd8a9b82d8
. * Fix T2I-Adapter field rendering in workflow editor. * yarn build, yarn typegen --------- Co-authored-by: blessedcoolant <54517381+blessedcoolant@users.noreply.github.com> Co-authored-by: psychedelicious <4822129+psychedelicious@users.noreply.github.com>
103 lines
3.2 KiB
Python
103 lines
3.2 KiB
Python
import os
|
|
from enum import Enum
|
|
from typing import Literal, Optional
|
|
|
|
import torch
|
|
from diffusers import T2IAdapter
|
|
|
|
from invokeai.backend.model_management.models.base import (
|
|
BaseModelType,
|
|
EmptyConfigLoader,
|
|
InvalidModelException,
|
|
ModelBase,
|
|
ModelConfigBase,
|
|
ModelNotFoundException,
|
|
ModelType,
|
|
SubModelType,
|
|
calc_model_size_by_data,
|
|
calc_model_size_by_fs,
|
|
classproperty,
|
|
)
|
|
|
|
|
|
class T2IAdapterModelFormat(str, Enum):
|
|
Diffusers = "diffusers"
|
|
|
|
|
|
class T2IAdapterModel(ModelBase):
|
|
class DiffusersConfig(ModelConfigBase):
|
|
model_format: Literal[T2IAdapterModelFormat.Diffusers]
|
|
|
|
def __init__(self, model_path: str, base_model: BaseModelType, model_type: ModelType):
|
|
assert model_type == ModelType.T2IAdapter
|
|
super().__init__(model_path, base_model, model_type)
|
|
|
|
config = EmptyConfigLoader.load_config(self.model_path, config_name="config.json")
|
|
|
|
model_class_name = config.get("_class_name", None)
|
|
if model_class_name not in {"T2IAdapter"}:
|
|
raise InvalidModelException(f"Invalid T2I-Adapter model. Unknown _class_name: '{model_class_name}'.")
|
|
|
|
self.model_class = self._hf_definition_to_type(["diffusers", model_class_name])
|
|
self.model_size = calc_model_size_by_fs(self.model_path)
|
|
|
|
def get_size(self, child_type: Optional[SubModelType] = None):
|
|
if child_type is not None:
|
|
raise ValueError(f"T2I-Adapters do not have child models. Invalid child type: '{child_type}'.")
|
|
return self.model_size
|
|
|
|
def get_model(
|
|
self,
|
|
torch_dtype: Optional[torch.dtype],
|
|
child_type: Optional[SubModelType] = None,
|
|
) -> T2IAdapter:
|
|
if child_type is not None:
|
|
raise ValueError(f"T2I-Adapters do not have child models. Invalid child type: '{child_type}'.")
|
|
|
|
model = None
|
|
for variant in ["fp16", None]:
|
|
try:
|
|
model = self.model_class.from_pretrained(
|
|
self.model_path,
|
|
torch_dtype=torch_dtype,
|
|
variant=variant,
|
|
)
|
|
break
|
|
except Exception:
|
|
pass
|
|
if not model:
|
|
raise ModelNotFoundException()
|
|
|
|
# Calculate a more accurate size after loading the model into memory.
|
|
self.model_size = calc_model_size_by_data(model)
|
|
return model
|
|
|
|
@classproperty
|
|
def save_to_config(cls) -> bool:
|
|
return False
|
|
|
|
@classmethod
|
|
def detect_format(cls, path: str):
|
|
if not os.path.exists(path):
|
|
raise ModelNotFoundException(f"Model not found at '{path}'.")
|
|
|
|
if os.path.isdir(path):
|
|
if os.path.exists(os.path.join(path, "config.json")):
|
|
return T2IAdapterModelFormat.Diffusers
|
|
|
|
raise InvalidModelException(f"Unsupported T2I-Adapter format: '{path}'.")
|
|
|
|
@classmethod
|
|
def convert_if_required(
|
|
cls,
|
|
model_path: str,
|
|
output_path: str,
|
|
config: ModelConfigBase,
|
|
base_model: BaseModelType,
|
|
) -> str:
|
|
format = cls.detect_format(model_path)
|
|
if format == T2IAdapterModelFormat.Diffusers:
|
|
return model_path
|
|
else:
|
|
raise ValueError(f"Unsupported format: '{format}'.")
|