From 60a98cacef241a7671ed3a7f33395cc26799387e Mon Sep 17 00:00:00 2001 From: Lincoln Stein Date: Fri, 3 Mar 2023 01:02:00 -0500 Subject: [PATCH] all vestiges of ldm.invoke removed --- invokeai/app/api/dependencies.py | 55 +- invokeai/app/api/events.py | 28 +- invokeai/app/api/routers/images.py | 59 +- invokeai/app/api/routers/sessions.py | 269 ++-- invokeai/app/api/sockets.py | 32 +- invokeai/app/api_app.py | 105 +- invokeai/app/cli_app.py | 248 ++-- invokeai/app/invocations/__init__.py | 6 +- invokeai/app/invocations/baseinvocation.py | 2 + invokeai/app/invocations/cv.py | 31 +- invokeai/app/invocations/generate.py | 186 ++- invokeai/app/invocations/image.py | 186 ++- invokeai/app/invocations/prompt.py | 6 +- invokeai/app/invocations/reconstruct.py | 38 +- invokeai/app/invocations/upscale.py | 35 +- invokeai/app/services/events.py | 92 +- invokeai/app/services/generate_initializer.py | 164 ++- invokeai/app/services/graph.py | 680 +++++++--- invokeai/app/services/image_storage.py | 33 +- invokeai/app/services/invocation_queue.py | 23 +- invokeai/app/services/invocation_services.py | 26 +- invokeai/app/services/invoker.py | 47 +- invokeai/app/services/item_storage.py | 25 +- invokeai/app/services/processor.py | 72 +- invokeai/app/services/sqlite.py | 73 +- invokeai/backend/__init__.py | 9 +- invokeai/backend/args.py | 1123 +++++++++-------- invokeai/backend/config/invokeai_configure.py | 40 +- .../backend/config/model_install_backend.py | 79 +- invokeai/backend/generate.py | 38 +- invokeai/backend/generator/__init__.py | 4 +- invokeai/backend/generator/base.py | 309 +++-- invokeai/backend/generator/embiggen.py | 325 +++-- invokeai/backend/generator/img2img.py | 88 +- invokeai/backend/generator/inpaint.py | 289 +++-- invokeai/backend/generator/omnibus.py | 173 --- invokeai/backend/generator/txt2img.py | 71 +- invokeai/backend/generator/txt2img2img.py | 135 +- invokeai/backend/globals.py | 64 +- invokeai/backend/image_util/__init__.py | 15 +- invokeai/backend/image_util/patchmatch.py | 29 +- invokeai/backend/image_util/pngwriter.py | 92 +- invokeai/backend/image_util/seamless.py | 46 +- invokeai/backend/image_util/txt2mask.py | 113 +- invokeai/backend/image_util/util.py | 47 +- invokeai/backend/model_management/__init__.py | 12 +- .../convert_ckpt_to_diffusers.py | 625 ++++++--- .../backend/model_management/model_manager.py | 30 +- invokeai/backend/prompting/__init__.py | 15 +- invokeai/backend/prompting/conditioning.py | 223 ++-- invokeai/backend/restoration/__init__.py | 6 +- invokeai/backend/restoration/base.py | 21 +- invokeai/backend/restoration/codeformer.py | 75 +- .../backend/restoration/codeformer_arch.py | 156 ++- invokeai/backend/restoration/gfpgan.py | 48 +- invokeai/backend/restoration/outcrop.py | 123 +- invokeai/backend/restoration/outpaint.py | 72 +- invokeai/backend/restoration/realesrgan.py | 59 +- invokeai/backend/restoration/vqgan_arch.py | 267 ++-- invokeai/backend/stable_diffusion/__init__.py | 18 +- .../backend/stable_diffusion/attention.py | 181 +-- .../backend/stable_diffusion/autoencoder.py | 169 +-- .../backend/stable_diffusion/concepts_lib.py | 197 +-- .../backend/stable_diffusion/data/base.py | 12 +- .../backend/stable_diffusion/data/imagenet.py | 229 ++-- .../backend/stable_diffusion/data/lsun.py | 70 +- .../stable_diffusion/data/personalized.py | 203 ++- .../data/personalized_style.py | 157 +-- .../stable_diffusion/diffusers_pipeline.py | 543 +++++--- .../stable_diffusion/diffusion/__init__.py | 6 +- .../stable_diffusion/diffusion/__init__.py~ | 4 - .../stable_diffusion/diffusion/classifier.py | 161 +-- .../diffusion/cross_attention_control.py | 422 ++++--- .../diffusion/cross_attention_map_saving.py | 47 +- .../stable_diffusion/diffusion/ddim.py | 92 +- .../stable_diffusion/diffusion/ddpm.py | 732 +++++------ .../stable_diffusion/diffusion/ksampler.py | 229 ++-- .../stable_diffusion/diffusion/plms.py | 103 +- .../stable_diffusion/diffusion/sampler.py | 286 ++--- .../diffusion/shared_invokeai_diffusion.py | 436 ++++--- .../diffusionmodules/model.py | 803 +++++++----- .../diffusionmodules/openaimodel.py | 99 +- .../stable_diffusion/diffusionmodules/util.py | 69 +- .../distributions/distributions.py | 8 +- invokeai/backend/stable_diffusion/ema.py | 20 +- .../stable_diffusion/encoders/modules.py | 337 ++--- .../image_degradation/bsrgan.py | 122 +- .../image_degradation/bsrgan_light.py | 112 +- .../image_degradation/utils_image.py | 123 +- .../stable_diffusion/losses/contperceptual.py | 58 +- .../stable_diffusion/losses/vqperceptual.py | 88 +- .../backend/stable_diffusion/offloading.py | 42 +- .../textual_inversion_manager.py | 2 +- .../backend/stable_diffusion/x_transformer.py | 147 +-- .../training/{__init.py__ => __init__.py} | 4 +- .../training/textual_inversion_training.py | 14 +- invokeai/backend/util/__init__.py | 33 +- invokeai/backend/util/devices.py | 31 +- invokeai/backend/util/log.py | 11 +- invokeai/backend/util/util.py | 29 +- invokeai/backend/web/__init__.py | 4 +- invokeai/backend/web/invoke_ai_web_server.py | 295 ++--- .../backend/web/modules/create_cmd_parser.py | 11 +- .../web/modules/get_canvas_generation_mode.py | 12 +- invokeai/backend/web/modules/parameters.py | 3 +- .../configs/stable-diffusion/v1-finetune.yaml | 14 +- .../stable-diffusion/v1-finetune_style.yaml | 12 +- .../stable-diffusion/v1-inference.yaml | 10 +- .../v1-inpainting-inference.yaml | 10 +- .../stable-diffusion/v1-m1-finetune.yaml | 12 +- .../stable-diffusion/v2-inference-v.yaml | 6 +- invokeai/frontend/CLI/CLI.py | 58 +- invokeai/frontend/CLI/__init__.py | 4 +- invokeai/frontend/CLI/readline.py | 424 ++++--- .../frontend/{config => install}/__init__.py | 7 +- .../{config => install}/invokeai_configure.py | 4 +- .../{config => install}/invokeai_update.py | 63 +- .../{config => install}/model_install.py | 65 +- .../frontend/{config => install}/widgets.py | 148 ++- invokeai/frontend/merge/__init__.py | 4 +- invokeai/frontend/merge/merge_diffusers.py | 26 +- invokeai/frontend/training/__init__.py | 5 +- .../frontend/training/textual_inversion.py | 10 +- invokeai/version/__init__.py | 22 +- invokeai/version/invokeai_version.py | 2 +- pyproject.toml | 12 +- 126 files changed, 8514 insertions(+), 6520 deletions(-) delete mode 100644 invokeai/backend/generator/omnibus.py delete mode 100644 invokeai/backend/stable_diffusion/diffusion/__init__.py~ rename invokeai/backend/training/{__init.py__ => __init__.py} (94%) rename invokeai/frontend/{config => install}/__init__.py (96%) rename invokeai/frontend/{config => install}/invokeai_configure.py (93%) rename invokeai/frontend/{config => install}/invokeai_update.py (56%) rename invokeai/frontend/{config => install}/model_install.py (93%) rename invokeai/frontend/{config => install}/widgets.py (61%) diff --git a/invokeai/app/api/dependencies.py b/invokeai/app/api/dependencies.py index 08f362133e..5ff8f9eef5 100644 --- a/invokeai/app/api/dependencies.py +++ b/invokeai/app/api/dependencies.py @@ -1,33 +1,31 @@ # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) -from argparse import Namespace import os - -from ..services.processor import DefaultInvocationProcessor - -from ..services.graph import GraphExecutionState -from ..services.sqlite import SqliteItemStorage +from argparse import Namespace from ...globals import Globals - +from ..services.generate_initializer import get_generate +from ..services.graph import GraphExecutionState from ..services.image_storage import DiskImageStorage from ..services.invocation_queue import MemoryInvocationQueue from ..services.invocation_services import InvocationServices from ..services.invoker import Invoker -from ..services.generate_initializer import get_generate +from ..services.processor import DefaultInvocationProcessor +from ..services.sqlite import SqliteItemStorage from .events import FastAPIEventService # TODO: is there a better way to achieve this? -def check_internet()->bool: - ''' +def check_internet() -> bool: + """ Return true if the internet is reachable. It does this by pinging huggingface.co. - ''' + """ import urllib.request - host = 'http://huggingface.co' + + host = "http://huggingface.co" try: - urllib.request.urlopen(host,timeout=1) + urllib.request.urlopen(host, timeout=1) return True except: return False @@ -35,14 +33,11 @@ def check_internet()->bool: class ApiDependencies: """Contains and initializes all dependencies for the API""" + invoker: Invoker = None @staticmethod - def initialize( - args, - config, - event_handler_id: int - ): + def initialize(args, config, event_handler_id: int): Globals.try_patchmatch = args.patchmatch Globals.always_use_cpu = args.always_use_cpu Globals.internet_available = args.internet_available and check_internet() @@ -50,30 +45,34 @@ class ApiDependencies: Globals.ckpt_convert = args.ckpt_convert # TODO: Use a logger - print(f'>> Internet connectivity is {Globals.internet_available}') + print(f">> Internet connectivity is {Globals.internet_available}") generate = get_generate(args, config) events = FastAPIEventService(event_handler_id) - output_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../../outputs')) + output_folder = os.path.abspath( + os.path.join(os.path.dirname(__file__), "../../../../outputs") + ) images = DiskImageStorage(output_folder) # TODO: build a file/path manager? - db_location = os.path.join(output_folder, 'invokeai.db') + db_location = os.path.join(output_folder, "invokeai.db") services = InvocationServices( - generate = generate, - events = events, - images = images, - queue = MemoryInvocationQueue(), - graph_execution_manager = SqliteItemStorage[GraphExecutionState](filename = db_location, table_name = 'graph_executions'), - processor = DefaultInvocationProcessor() + generate=generate, + events=events, + images=images, + queue=MemoryInvocationQueue(), + graph_execution_manager=SqliteItemStorage[GraphExecutionState]( + filename=db_location, table_name="graph_executions" + ), + processor=DefaultInvocationProcessor(), ) ApiDependencies.invoker = Invoker(services) - + @staticmethod def shutdown(): if ApiDependencies.invoker: diff --git a/invokeai/app/api/events.py b/invokeai/app/api/events.py index 701b48a316..bcfaad35a1 100644 --- a/invokeai/app/api/events.py +++ b/invokeai/app/api/events.py @@ -1,11 +1,14 @@ # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) import asyncio +import threading from queue import Empty, Queue from typing import Any + from fastapi_events.dispatcher import dispatch + from ..services.events import EventServiceBase -import threading + class FastAPIEventService(EventServiceBase): event_handler_id: int @@ -16,39 +19,34 @@ class FastAPIEventService(EventServiceBase): self.event_handler_id = event_handler_id self.__queue = Queue() self.__stop_event = threading.Event() - asyncio.create_task(self.__dispatch_from_queue(stop_event = self.__stop_event)) + asyncio.create_task(self.__dispatch_from_queue(stop_event=self.__stop_event)) super().__init__() - def stop(self, *args, **kwargs): self.__stop_event.set() self.__queue.put(None) - def dispatch(self, event_name: str, payload: Any) -> None: - self.__queue.put(dict( - event_name = event_name, - payload = payload - )) - + self.__queue.put(dict(event_name=event_name, payload=payload)) async def __dispatch_from_queue(self, stop_event: threading.Event): """Get events on from the queue and dispatch them, from the correct thread""" while not stop_event.is_set(): try: - event = self.__queue.get(block = False) - if not event: # Probably stopping + event = self.__queue.get(block=False) + if not event: # Probably stopping continue dispatch( - event.get('event_name'), - payload = event.get('payload'), - middleware_id = self.event_handler_id) + event.get("event_name"), + payload=event.get("payload"), + middleware_id=self.event_handler_id, + ) except Empty: await asyncio.sleep(0.001) pass except asyncio.CancelledError as e: - raise e # Raise a proper error + raise e # Raise a proper error diff --git a/invokeai/app/api/routers/images.py b/invokeai/app/api/routers/images.py index 1ae116e49d..55f1a2f036 100644 --- a/invokeai/app/api/routers/images.py +++ b/invokeai/app/api/routers/images.py @@ -1,57 +1,56 @@ # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) from datetime import datetime, timezone -from fastapi import Path, UploadFile, Request -from fastapi.routing import APIRouter + +from fastapi import Path, Request, UploadFile from fastapi.responses import FileResponse, Response +from fastapi.routing import APIRouter from PIL import Image + from ...services.image_storage import ImageType from ..dependencies import ApiDependencies -images_router = APIRouter( - prefix = '/v1/images', - tags = ['images'] -) +images_router = APIRouter(prefix="/v1/images", tags=["images"]) -@images_router.get('/{image_type}/{image_name}', - operation_id = 'get_image' - ) +@images_router.get("/{image_type}/{image_name}", operation_id="get_image") async def get_image( - image_type: ImageType = Path(description = "The type of image to get"), - image_name: str = Path(description = "The name of the image to get") + image_type: ImageType = Path(description="The type of image to get"), + image_name: str = Path(description="The name of the image to get"), ): """Gets a result""" # TODO: This is not really secure at all. At least make sure only output results are served filename = ApiDependencies.invoker.services.images.get_path(image_type, image_name) return FileResponse(filename) -@images_router.post('/uploads/', - operation_id = 'upload_image', - responses = { - 201: {'description': 'The image was uploaded successfully'}, - 404: {'description': 'Session not found'} - }) -async def upload_image( - file: UploadFile, - request: Request -): - if not file.content_type.startswith('image'): - return Response(status_code = 415) - + +@images_router.post( + "/uploads/", + operation_id="upload_image", + responses={ + 201: {"description": "The image was uploaded successfully"}, + 404: {"description": "Session not found"}, + }, +) +async def upload_image(file: UploadFile, request: Request): + if not file.content_type.startswith("image"): + return Response(status_code=415) + contents = await file.read() try: im = Image.open(contents) except: # Error opening the image - return Response(status_code = 415) - - filename = f'{str(int(datetime.now(timezone.utc).timestamp()))}.png' + return Response(status_code=415) + + filename = f"{str(int(datetime.now(timezone.utc).timestamp()))}.png" ApiDependencies.invoker.services.images.save(ImageType.UPLOAD, filename, im) return Response( status_code=201, - headers = { - 'Location': request.url_for('get_image', image_type=ImageType.UPLOAD, image_name=filename) - } + headers={ + "Location": request.url_for( + "get_image", image_type=ImageType.UPLOAD, image_name=filename + ) + }, ) diff --git a/invokeai/app/api/routers/sessions.py b/invokeai/app/api/routers/sessions.py index beb13736c6..713b212294 100644 --- a/invokeai/app/api/routers/sessions.py +++ b/invokeai/app/api/routers/sessions.py @@ -1,232 +1,271 @@ # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) -from typing import List, Optional, Union, Annotated -from fastapi import Query, Path, Body -from fastapi.routing import APIRouter +from typing import Annotated, List, Optional, Union + +from fastapi import Body, Path, Query from fastapi.responses import Response +from fastapi.routing import APIRouter from pydantic.fields import Field +from ...invocations import * +from ...invocations.baseinvocation import BaseInvocation +from ...services.graph import ( + EdgeConnection, + Graph, + GraphExecutionState, + NodeAlreadyExecutedError, +) from ...services.item_storage import PaginatedResults from ..dependencies import ApiDependencies -from ...invocations.baseinvocation import BaseInvocation -from ...services.graph import EdgeConnection, Graph, GraphExecutionState, NodeAlreadyExecutedError -from ...invocations import * -session_router = APIRouter( - prefix = '/v1/sessions', - tags = ['sessions'] -) +session_router = APIRouter(prefix="/v1/sessions", tags=["sessions"]) -@session_router.post('/', - operation_id = 'create_session', - responses = { +@session_router.post( + "/", + operation_id="create_session", + responses={ 200: {"model": GraphExecutionState}, - 400: {'description': 'Invalid json'} - }) + 400: {"description": "Invalid json"}, + }, +) async def create_session( - graph: Optional[Graph] = Body(default = None, description = "The graph to initialize the session with") + graph: Optional[Graph] = Body( + default=None, description="The graph to initialize the session with" + ) ) -> GraphExecutionState: """Creates a new session, optionally initializing it with an invocation graph""" session = ApiDependencies.invoker.create_execution_state(graph) return session -@session_router.get('/', - operation_id = 'list_sessions', - responses = { - 200: {"model": PaginatedResults[GraphExecutionState]} - }) +@session_router.get( + "/", + operation_id="list_sessions", + responses={200: {"model": PaginatedResults[GraphExecutionState]}}, +) async def list_sessions( - page: int = Query(default = 0, description = "The page of results to get"), - per_page: int = Query(default = 10, description = "The number of results per page"), - query: str = Query(default = '', description = "The query string to search for") + page: int = Query(default=0, description="The page of results to get"), + per_page: int = Query(default=10, description="The number of results per page"), + query: str = Query(default="", description="The query string to search for"), ) -> PaginatedResults[GraphExecutionState]: """Gets a list of sessions, optionally searching""" - if filter == '': - result = ApiDependencies.invoker.services.graph_execution_manager.list(page, per_page) + if filter == "": + result = ApiDependencies.invoker.services.graph_execution_manager.list( + page, per_page + ) else: - result = ApiDependencies.invoker.services.graph_execution_manager.search(query, page, per_page) + result = ApiDependencies.invoker.services.graph_execution_manager.search( + query, page, per_page + ) return result -@session_router.get('/{session_id}', - operation_id = 'get_session', - responses = { +@session_router.get( + "/{session_id}", + operation_id="get_session", + responses={ 200: {"model": GraphExecutionState}, - 404: {'description': 'Session not found'} - }) + 404: {"description": "Session not found"}, + }, +) async def get_session( - session_id: str = Path(description = "The id of the session to get") + session_id: str = Path(description="The id of the session to get"), ) -> GraphExecutionState: """Gets a session""" session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id) if session is None: - return Response(status_code = 404) + return Response(status_code=404) else: return session -@session_router.post('/{session_id}/nodes', - operation_id = 'add_node', - responses = { +@session_router.post( + "/{session_id}/nodes", + operation_id="add_node", + responses={ 200: {"model": str}, - 400: {'description': 'Invalid node or link'}, - 404: {'description': 'Session not found'} - } + 400: {"description": "Invalid node or link"}, + 404: {"description": "Session not found"}, + }, ) async def add_node( - session_id: str = Path(description = "The id of the session"), - node: Annotated[Union[BaseInvocation.get_invocations()], Field(discriminator="type")] = Body(description = "The node to add") + session_id: str = Path(description="The id of the session"), + node: Annotated[ + Union[BaseInvocation.get_invocations()], Field(discriminator="type") + ] = Body(description="The node to add"), ) -> str: """Adds a node to the graph""" session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id) if session is None: - return Response(status_code = 404) + return Response(status_code=404) try: session.add_node(node) - ApiDependencies.invoker.services.graph_execution_manager.set(session) # TODO: can this be done automatically, or add node through an API? + ApiDependencies.invoker.services.graph_execution_manager.set( + session + ) # TODO: can this be done automatically, or add node through an API? return session.id except NodeAlreadyExecutedError: - return Response(status_code = 400) + return Response(status_code=400) except IndexError: - return Response(status_code = 400) - + return Response(status_code=400) -@session_router.put('/{session_id}/nodes/{node_path}', - operation_id = 'update_node', - responses = { + +@session_router.put( + "/{session_id}/nodes/{node_path}", + operation_id="update_node", + responses={ 200: {"model": GraphExecutionState}, - 400: {'description': 'Invalid node or link'}, - 404: {'description': 'Session not found'} - } + 400: {"description": "Invalid node or link"}, + 404: {"description": "Session not found"}, + }, ) async def update_node( - session_id: str = Path(description = "The id of the session"), - node_path: str = Path(description = "The path to the node in the graph"), - node: Annotated[Union[BaseInvocation.get_invocations()], Field(discriminator="type")] = Body(description = "The new node") + session_id: str = Path(description="The id of the session"), + node_path: str = Path(description="The path to the node in the graph"), + node: Annotated[ + Union[BaseInvocation.get_invocations()], Field(discriminator="type") + ] = Body(description="The new node"), ) -> GraphExecutionState: """Updates a node in the graph and removes all linked edges""" session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id) if session is None: - return Response(status_code = 404) + return Response(status_code=404) try: session.update_node(node_path, node) - ApiDependencies.invoker.services.graph_execution_manager.set(session) # TODO: can this be done automatically, or add node through an API? + ApiDependencies.invoker.services.graph_execution_manager.set( + session + ) # TODO: can this be done automatically, or add node through an API? return session except NodeAlreadyExecutedError: - return Response(status_code = 400) + return Response(status_code=400) except IndexError: - return Response(status_code = 400) + return Response(status_code=400) -@session_router.delete('/{session_id}/nodes/{node_path}', - operation_id = 'delete_node', - responses = { +@session_router.delete( + "/{session_id}/nodes/{node_path}", + operation_id="delete_node", + responses={ 200: {"model": GraphExecutionState}, - 400: {'description': 'Invalid node or link'}, - 404: {'description': 'Session not found'} - } + 400: {"description": "Invalid node or link"}, + 404: {"description": "Session not found"}, + }, ) async def delete_node( - session_id: str = Path(description = "The id of the session"), - node_path: str = Path(description = "The path to the node to delete") + session_id: str = Path(description="The id of the session"), + node_path: str = Path(description="The path to the node to delete"), ) -> GraphExecutionState: """Deletes a node in the graph and removes all linked edges""" session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id) if session is None: - return Response(status_code = 404) + return Response(status_code=404) try: session.delete_node(node_path) - ApiDependencies.invoker.services.graph_execution_manager.set(session) # TODO: can this be done automatically, or add node through an API? + ApiDependencies.invoker.services.graph_execution_manager.set( + session + ) # TODO: can this be done automatically, or add node through an API? return session except NodeAlreadyExecutedError: - return Response(status_code = 400) + return Response(status_code=400) except IndexError: - return Response(status_code = 400) + return Response(status_code=400) -@session_router.post('/{session_id}/edges', - operation_id = 'add_edge', - responses = { +@session_router.post( + "/{session_id}/edges", + operation_id="add_edge", + responses={ 200: {"model": GraphExecutionState}, - 400: {'description': 'Invalid node or link'}, - 404: {'description': 'Session not found'} - } + 400: {"description": "Invalid node or link"}, + 404: {"description": "Session not found"}, + }, ) async def add_edge( - session_id: str = Path(description = "The id of the session"), - edge: tuple[EdgeConnection, EdgeConnection] = Body(description = "The edge to add") + session_id: str = Path(description="The id of the session"), + edge: tuple[EdgeConnection, EdgeConnection] = Body(description="The edge to add"), ) -> GraphExecutionState: """Adds an edge to the graph""" session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id) if session is None: - return Response(status_code = 404) + return Response(status_code=404) try: session.add_edge(edge) - ApiDependencies.invoker.services.graph_execution_manager.set(session) # TODO: can this be done automatically, or add node through an API? + ApiDependencies.invoker.services.graph_execution_manager.set( + session + ) # TODO: can this be done automatically, or add node through an API? return session except NodeAlreadyExecutedError: - return Response(status_code = 400) + return Response(status_code=400) except IndexError: - return Response(status_code = 400) + return Response(status_code=400) # TODO: the edge being in the path here is really ugly, find a better solution -@session_router.delete('/{session_id}/edges/{from_node_id}/{from_field}/{to_node_id}/{to_field}', - operation_id = 'delete_edge', - responses = { +@session_router.delete( + "/{session_id}/edges/{from_node_id}/{from_field}/{to_node_id}/{to_field}", + operation_id="delete_edge", + responses={ 200: {"model": GraphExecutionState}, - 400: {'description': 'Invalid node or link'}, - 404: {'description': 'Session not found'} - } + 400: {"description": "Invalid node or link"}, + 404: {"description": "Session not found"}, + }, ) async def delete_edge( - session_id: str = Path(description = "The id of the session"), - from_node_id: str = Path(description = "The id of the node the edge is coming from"), - from_field: str = Path(description = "The field of the node the edge is coming from"), - to_node_id: str = Path(description = "The id of the node the edge is going to"), - to_field: str = Path(description = "The field of the node the edge is going to") + session_id: str = Path(description="The id of the session"), + from_node_id: str = Path(description="The id of the node the edge is coming from"), + from_field: str = Path(description="The field of the node the edge is coming from"), + to_node_id: str = Path(description="The id of the node the edge is going to"), + to_field: str = Path(description="The field of the node the edge is going to"), ) -> GraphExecutionState: """Deletes an edge from the graph""" session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id) if session is None: - return Response(status_code = 404) + return Response(status_code=404) try: - edge = (EdgeConnection(node_id = from_node_id, field = from_field), EdgeConnection(node_id = to_node_id, field = to_field)) + edge = ( + EdgeConnection(node_id=from_node_id, field=from_field), + EdgeConnection(node_id=to_node_id, field=to_field), + ) session.delete_edge(edge) - ApiDependencies.invoker.services.graph_execution_manager.set(session) # TODO: can this be done automatically, or add node through an API? + ApiDependencies.invoker.services.graph_execution_manager.set( + session + ) # TODO: can this be done automatically, or add node through an API? return session except NodeAlreadyExecutedError: - return Response(status_code = 400) + return Response(status_code=400) except IndexError: - return Response(status_code = 400) - + return Response(status_code=400) -@session_router.put('/{session_id}/invoke', - operation_id = 'invoke_session', - responses = { + +@session_router.put( + "/{session_id}/invoke", + operation_id="invoke_session", + responses={ 200: {"model": None}, - 202: {'description': 'The invocation is queued'}, - 400: {'description': 'The session has no invocations ready to invoke'}, - 404: {'description': 'Session not found'} - }) + 202: {"description": "The invocation is queued"}, + 400: {"description": "The session has no invocations ready to invoke"}, + 404: {"description": "Session not found"}, + }, +) async def invoke_session( - session_id: str = Path(description = "The id of the session to invoke"), - all: bool = Query(default = False, description = "Whether or not to invoke all remaining invocations") + session_id: str = Path(description="The id of the session to invoke"), + all: bool = Query( + default=False, description="Whether or not to invoke all remaining invocations" + ), ) -> None: """Invokes a session""" session = ApiDependencies.invoker.services.graph_execution_manager.get(session_id) if session is None: - return Response(status_code = 404) - + return Response(status_code=404) + if session.is_complete(): - return Response(status_code = 400) - - ApiDependencies.invoker.invoke(session, invoke_all = all) + return Response(status_code=400) + + ApiDependencies.invoker.invoke(session, invoke_all=all) return Response(status_code=202) diff --git a/invokeai/app/api/sockets.py b/invokeai/app/api/sockets.py index eb4d5403c0..f70d7a6609 100644 --- a/invokeai/app/api/sockets.py +++ b/invokeai/app/api/sockets.py @@ -1,36 +1,38 @@ # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) from fastapi import FastAPI -from fastapi_socketio import SocketManager from fastapi_events.handlers.local import local_handler from fastapi_events.typing import Event +from fastapi_socketio import SocketManager + from ..services.events import EventServiceBase + class SocketIO: __sio: SocketManager def __init__(self, app: FastAPI): - self.__sio = SocketManager(app = app) - self.__sio.on('subscribe', handler=self._handle_sub) - self.__sio.on('unsubscribe', handler=self._handle_unsub) - + self.__sio = SocketManager(app=app) + self.__sio.on("subscribe", handler=self._handle_sub) + self.__sio.on("unsubscribe", handler=self._handle_unsub) + local_handler.register( - event_name = EventServiceBase.session_event, - _func=self._handle_session_event + event_name=EventServiceBase.session_event, _func=self._handle_session_event ) async def _handle_session_event(self, event: Event): await self.__sio.emit( - event = event[1]['event'], - data = event[1]['data'], - room = event[1]['data']['graph_execution_state_id'] + event=event[1]["event"], + data=event[1]["data"], + room=event[1]["data"]["graph_execution_state_id"], ) async def _handle_sub(self, sid, data, *args, **kwargs): - if 'session' in data: - self.__sio.enter_room(sid, data['session']) - + if "session" in data: + self.__sio.enter_room(sid, data["session"]) + # @app.sio.on('unsubscribe') + async def _handle_unsub(self, sid, data, *args, **kwargs): - if 'session' in data: - self.__sio.leave_room(sid, data['session']) + if "session" in data: + self.__sio.leave_room(sid, data["session"]) diff --git a/invokeai/app/api_app.py b/invokeai/app/api_app.py index db79b0d7e8..79ba126f8e 100644 --- a/invokeai/app/api_app.py +++ b/invokeai/app/api_app.py @@ -2,36 +2,37 @@ import asyncio from inspect import signature -from fastapi import FastAPI -from fastapi.openapi.utils import get_openapi -from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html -from fastapi.staticfiles import StaticFiles -from fastapi_events.middleware import EventHandlerASGIMiddleware -from fastapi_events.handlers.local import local_handler -from fastapi.middleware.cors import CORSMiddleware -from pydantic.schema import schema + import uvicorn +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware +from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html +from fastapi.openapi.utils import get_openapi +from fastapi.staticfiles import StaticFiles +from fastapi_events.handlers.local import local_handler +from fastapi_events.middleware import EventHandlerASGIMiddleware +from pydantic.schema import schema + +from ..args import Args +from .api.dependencies import ApiDependencies +from .api.routers import images, sessions from .api.sockets import SocketIO from .invocations import * from .invocations.baseinvocation import BaseInvocation -from .api.routers import images, sessions -from .api.dependencies import ApiDependencies -from ..args import Args # Create the app # TODO: create this all in a method so configuration/etc. can be passed in? -app = FastAPI( - title = "Invoke AI", - docs_url = None, - redoc_url = None -) +app = FastAPI(title="Invoke AI", docs_url=None, redoc_url=None) # Add event handler event_handler_id: int = id(app) app.add_middleware( EventHandlerASGIMiddleware, - handlers = [local_handler], # TODO: consider doing this in services to support different configurations - middleware_id = event_handler_id) + handlers=[ + local_handler + ], # TODO: consider doing this in services to support different configurations + middleware_id=event_handler_id, +) # Add CORS # TODO: use configuration for this @@ -48,38 +49,34 @@ socket_io = SocketIO(app) config = {} + # Add startup event to load dependencies -@app.on_event('startup') +@app.on_event("startup") async def startup_event(): args = Args() config = args.parse_args() ApiDependencies.initialize( - args = args, - config = config, - event_handler_id = event_handler_id + args=args, config=config, event_handler_id=event_handler_id ) + # Shut down threads -@app.on_event('shutdown') +@app.on_event("shutdown") async def shutdown_event(): ApiDependencies.shutdown() + # Include all routers # TODO: REMOVE # app.include_router( # invocation.invocation_router, # prefix = '/api') -app.include_router( - sessions.session_router, - prefix = '/api' -) +app.include_router(sessions.session_router, prefix="/api") + +app.include_router(images.images_router, prefix="/api") -app.include_router( - images.images_router, - prefix = '/api' -) # Build a custom OpenAPI to include all outputs # TODO: can outputs be included on metadata of invocation schemas somehow? @@ -87,10 +84,10 @@ def custom_openapi(): if app.openapi_schema: return app.openapi_schema openapi_schema = get_openapi( - title = app.title, - description = "An API for invoking AI image operations", - version = "1.0.0", - routes = app.routes + title=app.title, + description="An API for invoking AI image operations", + version="1.0.0", + routes=app.routes, ) # Add all outputs @@ -102,12 +99,12 @@ def custom_openapi(): output_types.add(output_type) output_schemas = schema(output_types, ref_prefix="#/components/schemas/") - for schema_key, output_schema in output_schemas['definitions'].items(): + for schema_key, output_schema in output_schemas["definitions"].items(): openapi_schema["components"]["schemas"][schema_key] = output_schema # TODO: note that we assume the schema_key here is the TYPE.__name__ # This could break in some cases, figure out a better way to do it - output_type_titles[schema_key] = output_schema['title'] + output_type_titles[schema_key] = output_schema["title"] # Add a reference to the output type to additionalProperties of the invoker schema for invoker in all_invocations: @@ -115,47 +112,47 @@ def custom_openapi(): output_type = signature(invoker.invoke).return_annotation output_type_title = output_type_titles[output_type.__name__] invoker_schema = openapi_schema["components"]["schemas"][invoker_name] - outputs_ref = { '$ref': f'#/components/schemas/{output_type_title}' } - if 'additionalProperties' not in invoker_schema: - invoker_schema['additionalProperties'] = {} + outputs_ref = {"$ref": f"#/components/schemas/{output_type_title}"} + if "additionalProperties" not in invoker_schema: + invoker_schema["additionalProperties"] = {} + + invoker_schema["additionalProperties"]["outputs"] = outputs_ref - invoker_schema['additionalProperties']['outputs'] = outputs_ref - app.openapi_schema = openapi_schema return app.openapi_schema + app.openapi = custom_openapi # Override API doc favicons -app.mount('/static', StaticFiles(directory='static/dream_web'), name='static') +app.mount("/static", StaticFiles(directory="static/dream_web"), name="static") + @app.get("/docs", include_in_schema=False) def overridden_swagger(): - return get_swagger_ui_html( + return get_swagger_ui_html( openapi_url=app.openapi_url, title=app.title, - swagger_favicon_url="/static/favicon.ico" + swagger_favicon_url="/static/favicon.ico", ) + @app.get("/redoc", include_in_schema=False) def overridden_redoc(): - return get_redoc_html( + return get_redoc_html( openapi_url=app.openapi_url, title=app.title, - redoc_favicon_url="/static/favicon.ico" + redoc_favicon_url="/static/favicon.ico", ) + def invoke_api(): # Start our own event loop for eventing usage # TODO: determine if there's a better way to do this loop = asyncio.new_event_loop() - config = uvicorn.Config( - app = app, - host = "0.0.0.0", - port = 9090, - loop = loop) - # Use access_log to turn off logging - + config = uvicorn.Config(app=app, host="0.0.0.0", port=9090, loop=loop) + # Use access_log to turn off logging + server = uvicorn.Server(config) loop.run_until_complete(server.serve()) diff --git a/invokeai/app/cli_app.py b/invokeai/app/cli_app.py index 51ccb9d41e..178efc0274 100644 --- a/invokeai/app/cli_app.py +++ b/invokeai/app/cli_app.py @@ -1,33 +1,40 @@ # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) import argparse -import shlex import os +import shlex import time -from typing import Any, Dict, Iterable, Literal, Union, get_args, get_origin, get_type_hints +from typing import ( + Any, + Dict, + Iterable, + Literal, + Union, + get_args, + get_origin, + get_type_hints, +) + from pydantic import BaseModel from pydantic.fields import Field -from .services.processor import DefaultInvocationProcessor - -from .services.graph import EdgeConnection, GraphExecutionState - -from .services.sqlite import SqliteItemStorage - +from ..args import Args +from .invocations import * +from .invocations.baseinvocation import BaseInvocation from .invocations.image import ImageField +from .services.events import EventServiceBase from .services.generate_initializer import get_generate +from .services.graph import EdgeConnection, GraphExecutionState from .services.image_storage import DiskImageStorage from .services.invocation_queue import MemoryInvocationQueue -from .invocations.baseinvocation import BaseInvocation from .services.invocation_services import InvocationServices -from .services.invoker import Invoker -from .invocations import * -from ..args import Args -from .services.events import EventServiceBase +from .services.invoker import Invoker +from .services.processor import DefaultInvocationProcessor +from .services.sqlite import SqliteItemStorage class InvocationCommand(BaseModel): - invocation: Union[BaseInvocation.get_invocations()] = Field(discriminator="type") # type: ignore + invocation: Union[BaseInvocation.get_invocations()] = Field(discriminator="type") # type: ignore class InvalidArgs(Exception): @@ -35,72 +42,94 @@ class InvalidArgs(Exception): def get_invocation_parser() -> argparse.ArgumentParser: - # Create invocation parser parser = argparse.ArgumentParser() + def exit(*args, **kwargs): raise InvalidArgs + parser.exit = exit - subparsers = parser.add_subparsers(dest='type') + subparsers = parser.add_subparsers(dest="type") invocation_parsers = dict() # Add history parser - history_parser = subparsers.add_parser('history', help="Shows the invocation history") - history_parser.add_argument('count', nargs='?', default=5, type=int, help="The number of history entries to show") + history_parser = subparsers.add_parser( + "history", help="Shows the invocation history" + ) + history_parser.add_argument( + "count", + nargs="?", + default=5, + type=int, + help="The number of history entries to show", + ) # Add default parser - default_parser = subparsers.add_parser('default', help="Define a default value for all inputs with a specified name") - default_parser.add_argument('input', type=str, help="The input field") - default_parser.add_argument('value', help="The default value") - - default_parser = subparsers.add_parser('reset_default', help="Resets a default value") - default_parser.add_argument('input', type=str, help="The input field") + default_parser = subparsers.add_parser( + "default", help="Define a default value for all inputs with a specified name" + ) + default_parser.add_argument("input", type=str, help="The input field") + default_parser.add_argument("value", help="The default value") + + default_parser = subparsers.add_parser( + "reset_default", help="Resets a default value" + ) + default_parser.add_argument("input", type=str, help="The input field") # Create subparsers for each invocation invocations = BaseInvocation.get_all_subclasses() for invocation in invocations: hints = get_type_hints(invocation) - cmd_name = get_args(hints['type'])[0] + cmd_name = get_args(hints["type"])[0] command_parser = subparsers.add_parser(cmd_name, help=invocation.__doc__) invocation_parsers[cmd_name] = command_parser # Add linking capability - command_parser.add_argument('--link', '-l', action='append', nargs=3, - help="A link in the format 'dest_field source_node source_field'. source_node can be relative to history (e.g. -1)") + command_parser.add_argument( + "--link", + "-l", + action="append", + nargs=3, + help="A link in the format 'dest_field source_node source_field'. source_node can be relative to history (e.g. -1)", + ) - command_parser.add_argument('--link_node', '-ln', action='append', - help="A link from all fields in the specified node. Node can be relative to history (e.g. -1)") + command_parser.add_argument( + "--link_node", + "-ln", + action="append", + help="A link from all fields in the specified node. Node can be relative to history (e.g. -1)", + ) # Convert all fields to arguments fields = invocation.__fields__ for name, field in fields.items(): - if name in ['id', 'type']: + if name in ["id", "type"]: continue - + if get_origin(field.type_) == Literal: allowed_values = get_args(field.type_) allowed_types = set() for val in allowed_values: allowed_types.add(type(val)) allowed_types_list = list(allowed_types) - field_type = allowed_types_list[0] if len(allowed_types) == 1 else Union[allowed_types_list] # type: ignore + field_type = allowed_types_list[0] if len(allowed_types) == 1 else Union[allowed_types_list] # type: ignore command_parser.add_argument( f"--{name}", dest=name, type=field_type, default=field.default, - choices = allowed_values, - help=field.field_info.description + choices=allowed_values, + help=field.field_info.description, ) else: command_parser.add_argument( f"--{name}", dest=name, type=field.type_, - default=field.default, - help=field.field_info.description + default=field.default, + help=field.field_info.description, ) return parser @@ -110,8 +139,8 @@ def get_invocation_command(invocation) -> str: fields = invocation.__fields__.items() type_hints = get_type_hints(type(invocation)) command = [invocation.type] - for name,field in fields: - if name in ['id', 'type']: + for name, field in fields: + if name in ["id", "type"]: continue # TODO: add links @@ -127,17 +156,25 @@ def get_invocation_command(invocation) -> str: if type_hint is str or str in get_args(type_hint): command.append(f'--{name} "{field_value}"') else: - command.append(f'--{name} {field_value}') - - return ' '.join(command) + command.append(f"--{name} {field_value}") + + return " ".join(command) -def get_graph_execution_history(graph_execution_state: GraphExecutionState) -> Iterable[str]: +def get_graph_execution_history( + graph_execution_state: GraphExecutionState, +) -> Iterable[str]: """Gets the history of fully-executed invocations for a graph execution""" - return (n for n in reversed(graph_execution_state.executed_history) if n in graph_execution_state.graph.nodes) + return ( + n + for n in reversed(graph_execution_state.executed_history) + if n in graph_execution_state.graph.nodes + ) -def generate_matching_edges(a: BaseInvocation, b: BaseInvocation) -> list[tuple[EdgeConnection, EdgeConnection]]: +def generate_matching_edges( + a: BaseInvocation, b: BaseInvocation +) -> list[tuple[EdgeConnection, EdgeConnection]]: """Generates all possible edges between two invocations""" atype = type(a) btype = type(b) @@ -148,12 +185,18 @@ def generate_matching_edges(a: BaseInvocation, b: BaseInvocation) -> list[tuple[ bfields = get_type_hints(btype) matching_fields = set(afields.keys()).intersection(bfields.keys()) - + # Remove invalid fields - invalid_fields = set(['type', 'id']) + invalid_fields = set(["type", "id"]) matching_fields = matching_fields.difference(invalid_fields) - edges = [(EdgeConnection(node_id = a.id, field = field), EdgeConnection(node_id = b.id, field = field)) for field in matching_fields] + edges = [ + ( + EdgeConnection(node_id=a.id, field=field), + EdgeConnection(node_id=b.id, field=field), + ) + for field in matching_fields + ] return edges @@ -165,27 +208,31 @@ def invoke_cli(): # NOTE: load model on first use, uncomment to load at startup # TODO: Make this a config option? - #generate.load_model() + # generate.load_model() events = EventServiceBase() - output_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../outputs')) + output_folder = os.path.abspath( + os.path.join(os.path.dirname(__file__), "../../../outputs") + ) # TODO: build a file/path manager? - db_location = os.path.join(output_folder, 'invokeai.db') + db_location = os.path.join(output_folder, "invokeai.db") services = InvocationServices( - generate = generate, - events = events, - images = DiskImageStorage(output_folder), - queue = MemoryInvocationQueue(), - graph_execution_manager = SqliteItemStorage[GraphExecutionState](filename = db_location, table_name = 'graph_executions'), - processor = DefaultInvocationProcessor() + generate=generate, + events=events, + images=DiskImageStorage(output_folder), + queue=MemoryInvocationQueue(), + graph_execution_manager=SqliteItemStorage[GraphExecutionState]( + filename=db_location, table_name="graph_executions" + ), + processor=DefaultInvocationProcessor(), ) invoker = Invoker(services) session: GraphExecutionState = invoker.create_execution_state() - + parser = get_invocation_parser() # Uncomment to print out previous sessions at startup @@ -201,10 +248,10 @@ def invoke_cli(): # Ctrl-c exits break - if cmd_input in ['exit','q']: - break; + if cmd_input in ["exit", "q"]: + break - if cmd_input in ['--help','help','h','?']: + if cmd_input in ["--help", "help", "h", "?"]: parser.print_help() continue @@ -214,65 +261,82 @@ def invoke_cli(): history = list(get_graph_execution_history(session)) # Split the command for piping - cmds = cmd_input.split('|') + cmds = cmd_input.split("|") start_id = len(history) current_id = start_id new_invocations = list() for cmd in cmds: - if cmd is None or cmd.strip() == '': - raise InvalidArgs('Empty command') + if cmd is None or cmd.strip() == "": + raise InvalidArgs("Empty command") # Parse args to create invocation args = vars(parser.parse_args(shlex.split(cmd.strip()))) # Check for special commands # TODO: These might be better as Pydantic models, similar to the invocations - if args['type'] == 'history': - history_count = args['count'] or 5 + if args["type"] == "history": + history_count = args["count"] or 5 for i in range(min(history_count, len(history))): entry_id = history[-1 - i] entry = session.graph.get_node(entry_id) - print(f'{entry_id}: {get_invocation_command(entry.invocation)}') + print(f"{entry_id}: {get_invocation_command(entry.invocation)}") continue - if args['type'] == 'reset_default': - if args['input'] in defaults: - del defaults[args['input']] + if args["type"] == "reset_default": + if args["input"] in defaults: + del defaults[args["input"]] continue - if args['type'] == 'default': - field = args['input'] - field_value = args['value'] + if args["type"] == "default": + field = args["input"] + field_value = args["value"] defaults[field] = field_value continue # Override defaults - for field_name,field_default in defaults.items(): + for field_name, field_default in defaults.items(): if field_name in args: args[field_name] = field_default # Parse invocation - args['id'] = current_id - command = InvocationCommand(invocation = args) + args["id"] = current_id + command = InvocationCommand(invocation=args) # Pipe previous command output (if there was a previous command) edges = [] if len(history) > 0 or current_id != start_id: - from_id = history[0] if current_id == start_id else str(current_id - 1) - from_node = next(filter(lambda n: n[0].id == from_id, new_invocations))[0] if current_id != start_id else session.graph.get_node(from_id) - matching_edges = generate_matching_edges(from_node, command.invocation) + from_id = ( + history[0] if current_id == start_id else str(current_id - 1) + ) + from_node = ( + next(filter(lambda n: n[0].id == from_id, new_invocations))[0] + if current_id != start_id + else session.graph.get_node(from_id) + ) + matching_edges = generate_matching_edges( + from_node, command.invocation + ) edges.extend(matching_edges) - + # Parse provided links - if 'link_node' in args and args['link_node']: - for link in args['link_node']: + if "link_node" in args and args["link_node"]: + for link in args["link_node"]: link_node = session.graph.get_node(link) - matching_edges = generate_matching_edges(link_node, command.invocation) + matching_edges = generate_matching_edges( + link_node, command.invocation + ) edges.extend(matching_edges) - - if 'link' in args and args['link']: - for link in args['link']: - edges.append((EdgeConnection(node_id = link[1], field = link[0]), EdgeConnection(node_id = command.invocation.id, field = link[2]))) + + if "link" in args and args["link"]: + for link in args["link"]: + edges.append( + ( + EdgeConnection(node_id=link[1], field=link[0]), + EdgeConnection( + node_id=command.invocation.id, field=link[2] + ), + ) + ) new_invocations.append((command.invocation, edges)) @@ -286,17 +350,19 @@ def invoke_cli(): session.add_edge(edge) # Execute all available invocations - invoker.invoke(session, invoke_all = True) + invoker.invoke(session, invoke_all=True) while not session.is_complete(): # Wait some time session = invoker.services.graph_execution_manager.get(session.id) time.sleep(0.1) - + # Print any errors if session.has_error(): for n in session.errors: - print(f'Error in node {n} (source node {session.prepared_source_mapping[n]}): {session.errors[n]}') - + print( + f"Error in node {n} (source node {session.prepared_source_mapping[n]}): {session.errors[n]}" + ) + # Start a new session print("Creating a new session") session = invoker.create_execution_state() @@ -307,7 +373,7 @@ def invoke_cli(): except SystemExit: continue - + invoker.stop() diff --git a/invokeai/app/invocations/__init__.py b/invokeai/app/invocations/__init__.py index 6407a1cdee..0a451ff618 100644 --- a/invokeai/app/invocations/__init__.py +++ b/invokeai/app/invocations/__init__.py @@ -4,5 +4,9 @@ __all__ = [] dirname = os.path.dirname(os.path.abspath(__file__)) for f in os.listdir(dirname): - if f != "__init__.py" and os.path.isfile("%s/%s" % (dirname, f)) and f[-3:] == ".py": + if ( + f != "__init__.py" + and os.path.isfile("%s/%s" % (dirname, f)) + and f[-3:] == ".py" + ): __all__.append(f[:-3]) diff --git a/invokeai/app/invocations/baseinvocation.py b/invokeai/app/invocations/baseinvocation.py index 1ad2d99112..82b6743c27 100644 --- a/invokeai/app/invocations/baseinvocation.py +++ b/invokeai/app/invocations/baseinvocation.py @@ -3,7 +3,9 @@ from abc import ABC, abstractmethod from inspect import signature from typing import get_args, get_type_hints + from pydantic import BaseModel, Field + from ..services.invocation_services import InvocationServices diff --git a/invokeai/app/invocations/cv.py b/invokeai/app/invocations/cv.py index f950669736..ea39a8d8cd 100644 --- a/invokeai/app/invocations/cv.py +++ b/invokeai/app/invocations/cv.py @@ -1,30 +1,37 @@ # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) from typing import Literal -import numpy -from pydantic import Field -from PIL import Image, ImageOps + import cv2 as cv -from .image import ImageField, ImageOutput -from .baseinvocation import BaseInvocation, InvocationContext +import numpy +from PIL import Image, ImageOps +from pydantic import Field + from ..services.image_storage import ImageType +from .baseinvocation import BaseInvocation, InvocationContext +from .image import ImageField, ImageOutput class CvInpaintInvocation(BaseInvocation): """Simple inpaint using opencv.""" - type: Literal['cv_inpaint'] = 'cv_inpaint' + + type: Literal["cv_inpaint"] = "cv_inpaint" # Inputs image: ImageField = Field(default=None, description="The image to inpaint") - mask: ImageField = Field(default=None, description="The mask to use when inpainting") + mask: ImageField = Field( + default=None, description="The mask to use when inpainting" + ) def invoke(self, context: InvocationContext) -> ImageOutput: - image = context.services.images.get(self.image.image_type, self.image.image_name) + image = context.services.images.get( + self.image.image_type, self.image.image_name + ) mask = context.services.images.get(self.mask.image_type, self.mask.image_name) # Convert to cv image/mask # TODO: consider making these utility functions - cv_image = cv.cvtColor(numpy.array(image.convert('RGB')), cv.COLOR_RGB2BGR) + cv_image = cv.cvtColor(numpy.array(image.convert("RGB")), cv.COLOR_RGB2BGR) cv_mask = numpy.array(ImageOps.invert(mask)) # Inpaint @@ -35,8 +42,10 @@ class CvInpaintInvocation(BaseInvocation): image_inpainted = Image.fromarray(cv.cvtColor(cv_inpainted, cv.COLOR_BGR2RGB)) image_type = ImageType.INTERMEDIATE - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, image_inpainted) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) diff --git a/invokeai/app/invocations/generate.py b/invokeai/app/invocations/generate.py index 60b656bf0c..83ad09a3f7 100644 --- a/invokeai/app/invocations/generate.py +++ b/invokeai/app/invocations/generate.py @@ -2,104 +2,160 @@ from datetime import datetime, timezone from typing import Any, Literal, Optional, Union + import numpy as np -from pydantic import Field from PIL import Image +from pydantic import Field from skimage.exposure.histogram_matching import match_histograms -from .image import ImageField, ImageOutput -from .baseinvocation import BaseInvocation, InvocationContext + from ..services.image_storage import ImageType from ..services.invocation_services import InvocationServices +from .baseinvocation import BaseInvocation, InvocationContext +from .image import ImageField, ImageOutput +SAMPLER_NAME_VALUES = Literal[ + "ddim", "plms", "k_lms", "k_dpm_2", "k_dpm_2_a", "k_euler", "k_euler_a", "k_heun" +] -SAMPLER_NAME_VALUES = Literal["ddim","plms","k_lms","k_dpm_2","k_dpm_2_a","k_euler","k_euler_a","k_heun"] # Text to image class TextToImageInvocation(BaseInvocation): """Generates an image using text2img.""" - type: Literal['txt2img'] = 'txt2img' + + type: Literal["txt2img"] = "txt2img" # Inputs # TODO: consider making prompt optional to enable providing prompt through a link - prompt: Optional[str] = Field(description="The prompt to generate an image from") - seed: int = Field(default=-1, ge=-1, le=np.iinfo(np.uint32).max, description="The seed to use (-1 for a random seed)") - steps: int = Field(default=10, gt=0, description="The number of steps to use to generate the image") - width: int = Field(default=512, multiple_of=64, gt=0, description="The width of the resulting image") - height: int = Field(default=512, multiple_of=64, gt=0, description="The height of the resulting image") - cfg_scale: float = Field(default=7.5, gt=0, description="The Classifier-Free Guidance, higher values may result in a result closer to the prompt") - sampler_name: SAMPLER_NAME_VALUES = Field(default="k_lms", description="The sampler to use") - seamless: bool = Field(default=False, description="Whether or not to generate an image that can tile without seams") - model: str = Field(default='', description="The model to use (currently ignored)") - progress_images: bool = Field(default=False, description="Whether or not to produce progress images during generation") + prompt: Optional[str] = Field(description="The prompt to generate an image from") + seed: int = Field( + default=-1, + ge=-1, + le=np.iinfo(np.uint32).max, + description="The seed to use (-1 for a random seed)", + ) + steps: int = Field( + default=10, gt=0, description="The number of steps to use to generate the image" + ) + width: int = Field( + default=512, + multiple_of=64, + gt=0, + description="The width of the resulting image", + ) + height: int = Field( + default=512, + multiple_of=64, + gt=0, + description="The height of the resulting image", + ) + cfg_scale: float = Field( + default=7.5, + gt=0, + description="The Classifier-Free Guidance, higher values may result in a result closer to the prompt", + ) + sampler_name: SAMPLER_NAME_VALUES = Field( + default="k_lms", description="The sampler to use" + ) + seamless: bool = Field( + default=False, + description="Whether or not to generate an image that can tile without seams", + ) + model: str = Field(default="", description="The model to use (currently ignored)") + progress_images: bool = Field( + default=False, + description="Whether or not to produce progress images during generation", + ) # TODO: pass this an emitter method or something? or a session for dispatching? - def dispatch_progress(self, context: InvocationContext, sample: Any = None, step: int = 0) -> None: + def dispatch_progress( + self, context: InvocationContext, sample: Any = None, step: int = 0 + ) -> None: context.services.events.emit_generator_progress( - context.graph_execution_state_id, self.id, step, float(step) / float(self.steps) + context.graph_execution_state_id, + self.id, + step, + float(step) / float(self.steps), ) def invoke(self, context: InvocationContext) -> ImageOutput: - - def step_callback(sample, step = 0): + def step_callback(sample, step=0): self.dispatch_progress(context, sample, step) # Handle invalid model parameter # TODO: figure out if this can be done via a validator that uses the model_cache # TODO: How to get the default model name now? - if self.model is None or self.model == '': + if self.model is None or self.model == "": self.model = context.services.generate.model_name # Set the model (if already cached, this does nothing) context.services.generate.set_model(self.model) results = context.services.generate.prompt2image( - prompt = self.prompt, - step_callback = step_callback, - **self.dict(exclude = {'prompt'}) # Shorthand for passing all of the parameters above manually + prompt=self.prompt, + step_callback=step_callback, + **self.dict( + exclude={"prompt"} + ), # Shorthand for passing all of the parameters above manually ) # Results are image and seed, unwrap for now and ignore the seed # TODO: pre-seed? # TODO: can this return multiple results? Should it? image_type = ImageType.RESULT - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, results[0][0]) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) class ImageToImageInvocation(TextToImageInvocation): """Generates an image using img2img.""" - type: Literal['img2img'] = 'img2img' + + type: Literal["img2img"] = "img2img" # Inputs - image: Union[ImageField,None] = Field(description="The input image") - strength: float = Field(default=0.75, gt=0, le=1, description="The strength of the original image") - fit: bool = Field(default=True, description="Whether or not the result should be fit to the aspect ratio of the input image") + image: Union[ImageField, None] = Field(description="The input image") + strength: float = Field( + default=0.75, gt=0, le=1, description="The strength of the original image" + ) + fit: bool = Field( + default=True, + description="Whether or not the result should be fit to the aspect ratio of the input image", + ) def invoke(self, context: InvocationContext) -> ImageOutput: - image = None if self.image is None else context.services.images.get(self.image.image_type, self.image.image_name) - mask = None + image = ( + None + if self.image is None + else context.services.images.get( + self.image.image_type, self.image.image_name + ) + ) + mask = None - def step_callback(sample, step = 0): + def step_callback(sample, step=0): self.dispatch_progress(context, sample, step) # Handle invalid model parameter # TODO: figure out if this can be done via a validator that uses the model_cache # TODO: How to get the default model name now? - if self.model is None or self.model == '': + if self.model is None or self.model == "": self.model = context.services.generate.model_name # Set the model (if already cached, this does nothing) context.services.generate.set_model(self.model) results = context.services.generate.prompt2image( - prompt = self.prompt, - init_img = image, - init_mask = mask, - step_callback = step_callback, - **self.dict(exclude = {'prompt','image','mask'}) # Shorthand for passing all of the parameters above manually + prompt=self.prompt, + init_img=image, + init_mask=mask, + step_callback=step_callback, + **self.dict( + exclude={"prompt", "image", "mask"} + ), # Shorthand for passing all of the parameters above manually ) result_image = results[0][0] @@ -108,43 +164,63 @@ class ImageToImageInvocation(TextToImageInvocation): # TODO: pre-seed? # TODO: can this return multiple results? Should it? image_type = ImageType.RESULT - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, result_image) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) class InpaintInvocation(ImageToImageInvocation): """Generates an image using inpaint.""" - type: Literal['inpaint'] = 'inpaint' + + type: Literal["inpaint"] = "inpaint" # Inputs - mask: Union[ImageField,None] = Field(description="The mask") - inpaint_replace: float = Field(default=0.0, ge=0.0, le=1.0, description="The amount by which to replace masked areas with latent noise") + mask: Union[ImageField, None] = Field(description="The mask") + inpaint_replace: float = Field( + default=0.0, + ge=0.0, + le=1.0, + description="The amount by which to replace masked areas with latent noise", + ) def invoke(self, context: InvocationContext) -> ImageOutput: - image = None if self.image is None else context.services.images.get(self.image.image_type, self.image.image_name) - mask = None if self.mask is None else context.services.images.get(self.mask.image_type, self.mask.image_name) + image = ( + None + if self.image is None + else context.services.images.get( + self.image.image_type, self.image.image_name + ) + ) + mask = ( + None + if self.mask is None + else context.services.images.get(self.mask.image_type, self.mask.image_name) + ) - def step_callback(sample, step = 0): + def step_callback(sample, step=0): self.dispatch_progress(context, sample, step) # Handle invalid model parameter # TODO: figure out if this can be done via a validator that uses the model_cache # TODO: How to get the default model name now? - if self.model is None or self.model == '': + if self.model is None or self.model == "": self.model = context.services.generate.model_name # Set the model (if already cached, this does nothing) context.services.generate.set_model(self.model) results = context.services.generate.prompt2image( - prompt = self.prompt, - init_img = image, - init_mask = mask, - step_callback = step_callback, - **self.dict(exclude = {'prompt','image','mask'}) # Shorthand for passing all of the parameters above manually + prompt=self.prompt, + init_img=image, + init_mask=mask, + step_callback=step_callback, + **self.dict( + exclude={"prompt", "image", "mask"} + ), # Shorthand for passing all of the parameters above manually ) result_image = results[0][0] @@ -153,8 +229,10 @@ class InpaintInvocation(ImageToImageInvocation): # TODO: pre-seed? # TODO: can this return multiple results? Should it? image_type = ImageType.RESULT - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, result_image) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) diff --git a/invokeai/app/invocations/image.py b/invokeai/app/invocations/image.py index cb326b1bb7..e0a302c24c 100644 --- a/invokeai/app/invocations/image.py +++ b/invokeai/app/invocations/image.py @@ -2,30 +2,37 @@ from datetime import datetime, timezone from typing import Literal, Optional + import numpy -from pydantic import Field, BaseModel -from PIL import Image, ImageOps, ImageFilter -from .baseinvocation import BaseInvocation, BaseInvocationOutput, InvocationContext +from PIL import Image, ImageFilter, ImageOps +from pydantic import BaseModel, Field + from ..services.image_storage import ImageType from ..services.invocation_services import InvocationServices +from .baseinvocation import BaseInvocation, BaseInvocationOutput, InvocationContext class ImageField(BaseModel): """An image field used for passing image objects between invocations""" - image_type: str = Field(default=ImageType.RESULT, description="The type of the image") + + image_type: str = Field( + default=ImageType.RESULT, description="The type of the image" + ) image_name: Optional[str] = Field(default=None, description="The name of the image") class ImageOutput(BaseInvocationOutput): """Base class for invocations that output an image""" - type: Literal['image'] = 'image' + + type: Literal["image"] = "image" image: ImageField = Field(default=None, description="The output image") class MaskOutput(BaseInvocationOutput): """Base class for invocations that output a mask""" - type: Literal['mask'] = 'mask' + + type: Literal["mask"] = "mask" mask: ImageField = Field(default=None, description="The output mask") @@ -33,7 +40,8 @@ class MaskOutput(BaseInvocationOutput): # TODO: this isn't really necessary anymore class LoadImageInvocation(BaseInvocation): """Load an image from a filename and provide it as output.""" - type: Literal['load_image'] = 'load_image' + + type: Literal["load_image"] = "load_image" # Inputs image_type: ImageType = Field(description="The type of the image") @@ -41,69 +49,100 @@ class LoadImageInvocation(BaseInvocation): def invoke(self, context: InvocationContext) -> ImageOutput: return ImageOutput( - image = ImageField(image_type = self.image_type, image_name = self.image_name) + image=ImageField(image_type=self.image_type, image_name=self.image_name) ) class ShowImageInvocation(BaseInvocation): """Displays a provided image, and passes it forward in the pipeline.""" - type: Literal['show_image'] = 'show_image' + + type: Literal["show_image"] = "show_image" # Inputs image: ImageField = Field(default=None, description="The image to show") def invoke(self, context: InvocationContext) -> ImageOutput: - image = context.services.images.get(self.image.image_type, self.image.image_name) + image = context.services.images.get( + self.image.image_type, self.image.image_name + ) if image: image.show() # TODO: how to handle failure? return ImageOutput( - image = ImageField(image_type = self.image.image_type, image_name = self.image.image_name) + image=ImageField( + image_type=self.image.image_type, image_name=self.image.image_name + ) ) class CropImageInvocation(BaseInvocation): """Crops an image to a specified box. The box can be outside of the image.""" - type: Literal['crop'] = 'crop' + + type: Literal["crop"] = "crop" # Inputs image: ImageField = Field(default=None, description="The image to crop") - x: int = Field(default=0, description="The left x coordinate of the crop rectangle") - y: int = Field(default=0, description="The top y coordinate of the crop rectangle") - width: int = Field(default=512, gt=0, description="The width of the crop rectangle") - height: int = Field(default=512, gt=0, description="The height of the crop rectangle") + x: int = Field(default=0, description="The left x coordinate of the crop rectangle") + y: int = Field(default=0, description="The top y coordinate of the crop rectangle") + width: int = Field(default=512, gt=0, description="The width of the crop rectangle") + height: int = Field( + default=512, gt=0, description="The height of the crop rectangle" + ) def invoke(self, context: InvocationContext) -> ImageOutput: - image = context.services.images.get(self.image.image_type, self.image.image_name) + image = context.services.images.get( + self.image.image_type, self.image.image_name + ) - image_crop = Image.new(mode = 'RGBA', size = (self.width, self.height), color = (0, 0, 0, 0)) + image_crop = Image.new( + mode="RGBA", size=(self.width, self.height), color=(0, 0, 0, 0) + ) image_crop.paste(image, (-self.x, -self.y)) image_type = ImageType.INTERMEDIATE - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, image_crop) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) class PasteImageInvocation(BaseInvocation): """Pastes an image into another image.""" - type: Literal['paste'] = 'paste' + + type: Literal["paste"] = "paste" # Inputs - base_image: ImageField = Field(default=None, description="The base image") - image: ImageField = Field(default=None, description="The image to paste") - mask: Optional[ImageField] = Field(default=None, description="The mask to use when pasting") - x: int = Field(default=0, description="The left x coordinate at which to paste the image") - y: int = Field(default=0, description="The top y coordinate at which to paste the image") + base_image: ImageField = Field(default=None, description="The base image") + image: ImageField = Field(default=None, description="The image to paste") + mask: Optional[ImageField] = Field( + default=None, description="The mask to use when pasting" + ) + x: int = Field( + default=0, description="The left x coordinate at which to paste the image" + ) + y: int = Field( + default=0, description="The top y coordinate at which to paste the image" + ) def invoke(self, context: InvocationContext) -> ImageOutput: - base_image = context.services.images.get(self.base_image.image_type, self.base_image.image_name) - image = context.services.images.get(self.image.image_type, self.image.image_name) - mask = None if self.mask is None else ImageOps.invert(services.images.get(self.mask.image_type, self.mask.image_name)) + base_image = context.services.images.get( + self.base_image.image_type, self.base_image.image_name + ) + image = context.services.images.get( + self.image.image_type, self.image.image_name + ) + mask = ( + None + if self.mask is None + else ImageOps.invert( + services.images.get(self.mask.image_type, self.mask.image_name) + ) + ) # TODO: probably shouldn't invert mask here... should user be required to do it? min_x = min(0, self.x) @@ -111,67 +150,88 @@ class PasteImageInvocation(BaseInvocation): max_x = max(base_image.width, image.width + self.x) max_y = max(base_image.height, image.height + self.y) - new_image = Image.new(mode = 'RGBA', size = (max_x - min_x, max_y - min_y), color = (0, 0, 0, 0)) + new_image = Image.new( + mode="RGBA", size=(max_x - min_x, max_y - min_y), color=(0, 0, 0, 0) + ) new_image.paste(base_image, (abs(min_x), abs(min_y))) - new_image.paste(image, (max(0, self.x), max(0, self.y)), mask = mask) + new_image.paste(image, (max(0, self.x), max(0, self.y)), mask=mask) image_type = ImageType.RESULT - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, new_image) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) class MaskFromAlphaInvocation(BaseInvocation): """Extracts the alpha channel of an image as a mask.""" - type: Literal['tomask'] = 'tomask' + + type: Literal["tomask"] = "tomask" # Inputs - image: ImageField = Field(default=None, description="The image to create the mask from") + image: ImageField = Field( + default=None, description="The image to create the mask from" + ) invert: bool = Field(default=False, description="Whether or not to invert the mask") def invoke(self, context: InvocationContext) -> MaskOutput: - image = context.services.images.get(self.image.image_type, self.image.image_name) + image = context.services.images.get( + self.image.image_type, self.image.image_name + ) image_mask = image.split()[-1] if self.invert: image_mask = ImageOps.invert(image_mask) image_type = ImageType.INTERMEDIATE - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) - context.services.images.save(image_type, image_name, image_mask) - return MaskOutput( - mask = ImageField(image_type = image_type, image_name = image_name) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id ) + context.services.images.save(image_type, image_name, image_mask) + return MaskOutput(mask=ImageField(image_type=image_type, image_name=image_name)) class BlurInvocation(BaseInvocation): """Blurs an image""" - type: Literal['blur'] = 'blur' + + type: Literal["blur"] = "blur" # Inputs image: ImageField = Field(default=None, description="The image to blur") - radius: float = Field(default=8.0, ge=0, description="The blur radius") - blur_type: Literal['gaussian', 'box'] = Field(default='gaussian', description="The type of blur") + radius: float = Field(default=8.0, ge=0, description="The blur radius") + blur_type: Literal["gaussian", "box"] = Field( + default="gaussian", description="The type of blur" + ) def invoke(self, context: InvocationContext) -> ImageOutput: - image = context.services.images.get(self.image.image_type, self.image.image_name) + image = context.services.images.get( + self.image.image_type, self.image.image_name + ) - blur = ImageFilter.GaussianBlur(self.radius) if self.blur_type == 'gaussian' else ImageFilter.BoxBlur(self.radius) + blur = ( + ImageFilter.GaussianBlur(self.radius) + if self.blur_type == "gaussian" + else ImageFilter.BoxBlur(self.radius) + ) blur_image = image.filter(blur) image_type = ImageType.INTERMEDIATE - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, blur_image) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) class LerpInvocation(BaseInvocation): """Linear interpolation of all pixels of an image""" - type: Literal['lerp'] = 'lerp' + + type: Literal["lerp"] = "lerp" # Inputs image: ImageField = Field(default=None, description="The image to lerp") @@ -179,7 +239,9 @@ class LerpInvocation(BaseInvocation): max: int = Field(default=255, ge=0, le=255, description="The maximum output value") def invoke(self, context: InvocationContext) -> ImageOutput: - image = context.services.images.get(self.image.image_type, self.image.image_name) + image = context.services.images.get( + self.image.image_type, self.image.image_name + ) image_arr = numpy.asarray(image, dtype=numpy.float32) / 255 image_arr = image_arr * (self.max - self.min) + self.max @@ -187,16 +249,19 @@ class LerpInvocation(BaseInvocation): lerp_image = Image.fromarray(numpy.uint8(image_arr)) image_type = ImageType.INTERMEDIATE - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, lerp_image) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) class InverseLerpInvocation(BaseInvocation): """Inverse linear interpolation of all pixels of an image""" - type: Literal['ilerp'] = 'ilerp' + + type: Literal["ilerp"] = "ilerp" # Inputs image: ImageField = Field(default=None, description="The image to lerp") @@ -204,16 +269,25 @@ class InverseLerpInvocation(BaseInvocation): max: int = Field(default=255, ge=0, le=255, description="The maximum input value") def invoke(self, context: InvocationContext) -> ImageOutput: - image = context.services.images.get(self.image.image_type, self.image.image_name) + image = context.services.images.get( + self.image.image_type, self.image.image_name + ) image_arr = numpy.asarray(image, dtype=numpy.float32) - image_arr = numpy.minimum(numpy.maximum(image_arr - self.min, 0) / float(self.max - self.min), 1) * 255 + image_arr = ( + numpy.minimum( + numpy.maximum(image_arr - self.min, 0) / float(self.max - self.min), 1 + ) + * 255 + ) ilerp_image = Image.fromarray(numpy.uint8(image_arr)) image_type = ImageType.INTERMEDIATE - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, ilerp_image) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) diff --git a/invokeai/app/invocations/prompt.py b/invokeai/app/invocations/prompt.py index 029cad9660..2c8a1c4989 100644 --- a/invokeai/app/invocations/prompt.py +++ b/invokeai/app/invocations/prompt.py @@ -1,9 +1,13 @@ from typing import Literal + from pydantic.fields import Field + from .baseinvocation import BaseInvocationOutput + class PromptOutput(BaseInvocationOutput): """Base class for invocations that output a prompt""" - type: Literal['prompt'] = 'prompt' + + type: Literal["prompt"] = "prompt" prompt: str = Field(default=None, description="The output prompt") diff --git a/invokeai/app/invocations/reconstruct.py b/invokeai/app/invocations/reconstruct.py index 98201ce837..71a15c57e9 100644 --- a/invokeai/app/invocations/reconstruct.py +++ b/invokeai/app/invocations/reconstruct.py @@ -1,36 +1,44 @@ from datetime import datetime, timezone from typing import Literal, Union + from pydantic import Field -from .image import ImageField, ImageOutput -from .baseinvocation import BaseInvocation, InvocationContext + from ..services.image_storage import ImageType from ..services.invocation_services import InvocationServices +from .baseinvocation import BaseInvocation, InvocationContext +from .image import ImageField, ImageOutput class RestoreFaceInvocation(BaseInvocation): """Restores faces in an image.""" - type: Literal['restore_face'] = 'restore_face' + + type: Literal["restore_face"] = "restore_face" # Inputs - image: Union[ImageField,None] = Field(description="The input image") - strength: float = Field(default=0.75, gt=0, le=1, description="The strength of the restoration") + image: Union[ImageField, None] = Field(description="The input image") + strength: float = Field( + default=0.75, gt=0, le=1, description="The strength of the restoration" + ) - - def invoke(self, context: InvocationContext) -> ImageOutput: - image = context.services.images.get(self.image.image_type, self.image.image_name) + def invoke(self, context: InvocationContext) -> ImageOutput: + image = context.services.images.get( + self.image.image_type, self.image.image_name + ) results = context.services.generate.upscale_and_reconstruct( - image_list = [[image, 0]], - upscale = None, - strength = self.strength, # GFPGAN strength - save_original = False, - image_callback = None, + image_list=[[image, 0]], + upscale=None, + strength=self.strength, # GFPGAN strength + save_original=False, + image_callback=None, ) # Results are image and seed, unwrap for now # TODO: can this return multiple results? image_type = ImageType.RESULT - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, results[0][0]) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) diff --git a/invokeai/app/invocations/upscale.py b/invokeai/app/invocations/upscale.py index 1df8c44ea8..d1d8e4e2d4 100644 --- a/invokeai/app/invocations/upscale.py +++ b/invokeai/app/invocations/upscale.py @@ -2,37 +2,44 @@ from datetime import datetime, timezone from typing import Literal, Union + from pydantic import Field -from .image import ImageField, ImageOutput -from .baseinvocation import BaseInvocation, InvocationContext + from ..services.image_storage import ImageType from ..services.invocation_services import InvocationServices +from .baseinvocation import BaseInvocation, InvocationContext +from .image import ImageField, ImageOutput class UpscaleInvocation(BaseInvocation): """Upscales an image.""" - type: Literal['upscale'] = 'upscale' + + type: Literal["upscale"] = "upscale" # Inputs - image: Union[ImageField,None] = Field(description="The input image", default=None) - strength: float = Field(default=0.75, gt=0, le=1, description="The strength") - level: Literal[2,4] = Field(default=2, description = "The upscale level") + image: Union[ImageField, None] = Field(description="The input image", default=None) + strength: float = Field(default=0.75, gt=0, le=1, description="The strength") + level: Literal[2, 4] = Field(default=2, description="The upscale level") def invoke(self, context: InvocationContext) -> ImageOutput: - image = context.services.images.get(self.image.image_type, self.image.image_name) + image = context.services.images.get( + self.image.image_type, self.image.image_name + ) results = context.services.generate.upscale_and_reconstruct( - image_list = [[image, 0]], - upscale = (self.level, self.strength), - strength = 0.0, # GFPGAN strength - save_original = False, - image_callback = None, + image_list=[[image, 0]], + upscale=(self.level, self.strength), + strength=0.0, # GFPGAN strength + save_original=False, + image_callback=None, ) # Results are image and seed, unwrap for now # TODO: can this return multiple results? image_type = ImageType.RESULT - image_name = context.services.images.create_name(context.graph_execution_state_id, self.id) + image_name = context.services.images.create_name( + context.graph_execution_state_id, self.id + ) context.services.images.save(image_type, image_name, results[0][0]) return ImageOutput( - image = ImageField(image_type = image_type, image_name = image_name) + image=ImageField(image_type=image_type, image_name=image_name) ) diff --git a/invokeai/app/services/events.py b/invokeai/app/services/events.py index c7b6367d68..e2ab4e61e3 100644 --- a/invokeai/app/services/events.py +++ b/invokeai/app/services/events.py @@ -4,90 +4,80 @@ from typing import Any, Dict class EventServiceBase: - session_event: str = 'session_event' + session_event: str = "session_event" """Basic event bus, to have an empty stand-in when not needed""" + def dispatch(self, event_name: str, payload: Any) -> None: pass - def __emit_session_event(self, - event_name: str, - payload: Dict) -> None: + def __emit_session_event(self, event_name: str, payload: Dict) -> None: self.dispatch( - event_name = EventServiceBase.session_event, - payload = dict( - event = event_name, - data = payload - ) + event_name=EventServiceBase.session_event, + payload=dict(event=event_name, data=payload), ) # Define events here for every event in the system. # This will make them easier to integrate until we find a schema generator. - def emit_generator_progress(self, + def emit_generator_progress( + self, graph_execution_state_id: str, invocation_id: str, step: int, - percent: float + percent: float, ) -> None: """Emitted when there is generation progress""" self.__emit_session_event( - event_name = 'generator_progress', - payload = dict( - graph_execution_state_id = graph_execution_state_id, - invocation_id = invocation_id, - step = step, - percent = percent - ) + event_name="generator_progress", + payload=dict( + graph_execution_state_id=graph_execution_state_id, + invocation_id=invocation_id, + step=step, + percent=percent, + ), ) - def emit_invocation_complete(self, - graph_execution_state_id: str, - invocation_id: str, - result: Dict + def emit_invocation_complete( + self, graph_execution_state_id: str, invocation_id: str, result: Dict ) -> None: """Emitted when an invocation has completed""" self.__emit_session_event( - event_name = 'invocation_complete', - payload = dict( - graph_execution_state_id = graph_execution_state_id, - invocation_id = invocation_id, - result = result - ) + event_name="invocation_complete", + payload=dict( + graph_execution_state_id=graph_execution_state_id, + invocation_id=invocation_id, + result=result, + ), ) - - def emit_invocation_error(self, - graph_execution_state_id: str, - invocation_id: str, - error: str + + def emit_invocation_error( + self, graph_execution_state_id: str, invocation_id: str, error: str ) -> None: """Emitted when an invocation has completed""" self.__emit_session_event( - event_name = 'invocation_error', - payload = dict( - graph_execution_state_id = graph_execution_state_id, - invocation_id = invocation_id, - error = error - ) + event_name="invocation_error", + payload=dict( + graph_execution_state_id=graph_execution_state_id, + invocation_id=invocation_id, + error=error, + ), ) - def emit_invocation_started(self, - graph_execution_state_id: str, - invocation_id: str + def emit_invocation_started( + self, graph_execution_state_id: str, invocation_id: str ) -> None: """Emitted when an invocation has started""" self.__emit_session_event( - event_name = 'invocation_started', - payload = dict( - graph_execution_state_id = graph_execution_state_id, - invocation_id = invocation_id - ) + event_name="invocation_started", + payload=dict( + graph_execution_state_id=graph_execution_state_id, + invocation_id=invocation_id, + ), ) def emit_graph_execution_complete(self, graph_execution_state_id: str) -> None: """Emitted when a session has completed all invocations""" self.__emit_session_event( - event_name = 'graph_execution_state_complete', - payload = dict( - graph_execution_state_id = graph_execution_state_id - ) + event_name="graph_execution_state_complete", + payload=dict(graph_execution_state_id=graph_execution_state_id), ) diff --git a/invokeai/app/services/generate_initializer.py b/invokeai/app/services/generate_initializer.py index c6e81a2403..4a1ce3ea16 100644 --- a/invokeai/app/services/generate_initializer.py +++ b/invokeai/app/services/generate_initializer.py @@ -1,40 +1,47 @@ -from argparse import Namespace import os import sys import traceback +from argparse import Namespace -from invokeai.backend import ModelManager, Generate +import invokeai.version +from invokeai.backend import Generate, ModelManager from ...globals import Globals -import invokeai.version + # TODO: most of this code should be split into individual services as the Generate.py code is deprecated def get_generate(args, config) -> Generate: if not args.conf: - config_file = os.path.join(Globals.root,'configs','models.yaml') + config_file = os.path.join(Globals.root, "configs", "models.yaml") if not os.path.exists(config_file): - report_model_error(args, FileNotFoundError(f"The file {config_file} could not be found.")) + report_model_error( + args, FileNotFoundError(f"The file {config_file} could not be found.") + ) - print(f'>> {invokeai.version.__app_name__}, version {invokeai.version.__version__}') + print(f">> {invokeai.version.__app_name__}, version {invokeai.version.__version__}") print(f'>> InvokeAI runtime directory is "{Globals.root}"') # these two lines prevent a horrible warning message from appearing # when the frozen CLIP tokenizer is imported import transformers # type: ignore + transformers.logging.set_verbosity_error() import diffusers + diffusers.logging.set_verbosity_error() # Loading Face Restoration and ESRGAN Modules - gfpgan,codeformer,esrgan = load_face_restoration(args) + gfpgan, codeformer, esrgan = load_face_restoration(args) # normalize the config directory relative to root if not os.path.isabs(args.conf): - args.conf = os.path.normpath(os.path.join(Globals.root,args.conf)) + args.conf = os.path.normpath(os.path.join(Globals.root, args.conf)) if args.embeddings: if not os.path.isabs(args.embedding_path): - embedding_path = os.path.normpath(os.path.join(Globals.root,args.embedding_path)) + embedding_path = os.path.normpath( + os.path.join(Globals.root, args.embedding_path) + ) else: embedding_path = args.embedding_path else: @@ -47,35 +54,35 @@ def get_generate(args, config) -> Generate: if args.infile: try: if os.path.isfile(args.infile): - infile = open(args.infile, 'r', encoding='utf-8') - elif args.infile == '-': # stdin + infile = open(args.infile, "r", encoding="utf-8") + elif args.infile == "-": # stdin infile = sys.stdin else: - raise FileNotFoundError(f'{args.infile} not found.') + raise FileNotFoundError(f"{args.infile} not found.") except (FileNotFoundError, IOError) as e: - print(f'{e}. Aborting.') + print(f"{e}. Aborting.") sys.exit(-1) # creating a Generate object: try: gen = Generate( - conf = args.conf, - model = args.model, - sampler_name = args.sampler_name, - embedding_path = embedding_path, - full_precision = args.full_precision, - precision = args.precision, - gfpgan = gfpgan, - codeformer = codeformer, - esrgan = esrgan, - free_gpu_mem = args.free_gpu_mem, - safety_checker = args.safety_checker, - max_loaded_models = args.max_loaded_models, - ) + conf=args.conf, + model=args.model, + sampler_name=args.sampler_name, + embedding_path=embedding_path, + full_precision=args.full_precision, + precision=args.precision, + gfpgan=gfpgan, + codeformer=codeformer, + esrgan=esrgan, + free_gpu_mem=args.free_gpu_mem, + safety_checker=args.safety_checker, + max_loaded_models=args.max_loaded_models, + ) except (FileNotFoundError, TypeError, AssertionError) as e: - report_model_error(opt,e) + report_model_error(opt, e) except (IOError, KeyError) as e: - print(f'{e}. Aborting.') + print(f"{e}. Aborting.") sys.exit(-1) if args.seamless: @@ -96,7 +103,7 @@ def get_generate(args, config) -> Generate: conf_path=args.conf, weights_directory=path, ) - + return gen @@ -104,51 +111,61 @@ def load_face_restoration(opt): try: gfpgan, codeformer, esrgan = None, None, None if opt.restore or opt.esrgan: - from ldm.invoke.restoration import Restoration + from invokeai.backend.restoration import Restoration + restoration = Restoration() if opt.restore: - gfpgan, codeformer = restoration.load_face_restore_models(opt.gfpgan_model_path) + gfpgan, codeformer = restoration.load_face_restore_models( + opt.gfpgan_model_path + ) else: - print('>> Face restoration disabled') + print(">> Face restoration disabled") if opt.esrgan: esrgan = restoration.load_esrgan(opt.esrgan_bg_tile) else: - print('>> Upscaling disabled') + print(">> Upscaling disabled") else: - print('>> Face restoration and upscaling disabled') + print(">> Face restoration and upscaling disabled") except (ModuleNotFoundError, ImportError): print(traceback.format_exc(), file=sys.stderr) - print('>> You may need to install the ESRGAN and/or GFPGAN modules') - return gfpgan,codeformer,esrgan + print(">> You may need to install the ESRGAN and/or GFPGAN modules") + return gfpgan, codeformer, esrgan -def report_model_error(opt:Namespace, e:Exception): +def report_model_error(opt: Namespace, e: Exception): print(f'** An error occurred while attempting to initialize the model: "{str(e)}"') - print('** This can be caused by a missing or corrupted models file, and can sometimes be fixed by (re)installing the models.') - yes_to_all = os.environ.get('INVOKE_MODEL_RECONFIGURE') + print( + "** This can be caused by a missing or corrupted models file, and can sometimes be fixed by (re)installing the models." + ) + yes_to_all = os.environ.get("INVOKE_MODEL_RECONFIGURE") if yes_to_all: - print('** Reconfiguration is being forced by environment variable INVOKE_MODEL_RECONFIGURE') + print( + "** Reconfiguration is being forced by environment variable INVOKE_MODEL_RECONFIGURE" + ) else: - response = input('Do you want to run invokeai-configure script to select and/or reinstall models? [y] ') - if response.startswith(('n', 'N')): + response = input( + "Do you want to run invokeai-configure script to select and/or reinstall models? [y] " + ) + if response.startswith(("n", "N")): return - print('invokeai-configure is launching....\n') + print("invokeai-configure is launching....\n") # Match arguments that were set on the CLI # only the arguments accepted by the configuration script are parsed root_dir = ["--root", opt.root_dir] if opt.root_dir is not None else [] config = ["--config", opt.conf] if opt.conf is not None else [] previous_args = sys.argv - sys.argv = [ 'invokeai-configure' ] + sys.argv = ["invokeai-configure"] sys.argv.extend(root_dir) sys.argv.extend(config) if yes_to_all is not None: for arg in yes_to_all.split(): sys.argv.append(arg) - from ldm.invoke.config import invokeai_configure - invokeai_configure.main() + from invokeai.frontend.install import invokeai_configure + + invokeai_configure() # TODO: Figure out how to restart # print('** InvokeAI will now restart') # sys.argv = previous_args @@ -162,14 +179,17 @@ def old_get_generate(args, config) -> Generate: from invokeai.backend.globals import Globals # alert - setting globals here - Globals.root = os.path.expanduser(args.root_dir or os.environ.get('INVOKEAI_ROOT') or os.path.abspath('.')) + Globals.root = os.path.expanduser( + args.root_dir or os.environ.get("INVOKEAI_ROOT") or os.path.abspath(".") + ) Globals.try_patchmatch = args.patchmatch - + print(f'>> InvokeAI runtime directory is "{Globals.root}"') # these two lines prevent a horrible warning message from appearing # when the frozen CLIP tokenizer is imported import transformers + transformers.logging.set_verbosity_error() # Loading Face Restoration and ESRGAN Modules @@ -177,53 +197,57 @@ def old_get_generate(args, config) -> Generate: try: if config.restore or config.esrgan: from ldm.invoke.restoration import Restoration + restoration = Restoration() if config.restore: - gfpgan, codeformer = restoration.load_face_restore_models(config.gfpgan_model_path) + gfpgan, codeformer = restoration.load_face_restore_models( + config.gfpgan_model_path + ) else: - print('>> Face restoration disabled') + print(">> Face restoration disabled") if config.esrgan: esrgan = restoration.load_esrgan(config.esrgan_bg_tile) else: - print('>> Upscaling disabled') + print(">> Upscaling disabled") else: - print('>> Face restoration and upscaling disabled') + print(">> Face restoration and upscaling disabled") except (ModuleNotFoundError, ImportError): print(traceback.format_exc(), file=sys.stderr) - print('>> You may need to install the ESRGAN and/or GFPGAN modules') + print(">> You may need to install the ESRGAN and/or GFPGAN modules") # normalize the config directory relative to root if not os.path.isabs(config.conf): - config.conf = os.path.normpath(os.path.join(Globals.root,config.conf)) + config.conf = os.path.normpath(os.path.join(Globals.root, config.conf)) if config.embeddings: if not os.path.isabs(config.embedding_path): - embedding_path = os.path.normpath(os.path.join(Globals.root,config.embedding_path)) + embedding_path = os.path.normpath( + os.path.join(Globals.root, config.embedding_path) + ) else: embedding_path = None - # TODO: lazy-initialize this by wrapping it try: generate = Generate( - conf = config.conf, - model = config.model, - sampler_name = config.sampler_name, - embedding_path = embedding_path, - full_precision = config.full_precision, - precision = config.precision, - gfpgan = gfpgan, - codeformer = codeformer, - esrgan = esrgan, - free_gpu_mem = config.free_gpu_mem, - safety_checker = config.safety_checker, - max_loaded_models = config.max_loaded_models, + conf=config.conf, + model=config.model, + sampler_name=config.sampler_name, + embedding_path=embedding_path, + full_precision=config.full_precision, + precision=config.precision, + gfpgan=gfpgan, + codeformer=codeformer, + esrgan=esrgan, + free_gpu_mem=config.free_gpu_mem, + safety_checker=config.safety_checker, + max_loaded_models=config.max_loaded_models, ) except (FileNotFoundError, TypeError, AssertionError): - #emergency_model_reconfigure() # TODO? + # emergency_model_reconfigure() # TODO? sys.exit(-1) except (IOError, KeyError) as e: - print(f'{e}. Aborting.') + print(f"{e}. Aborting.") sys.exit(-1) generate.free_gpu_mem = config.free_gpu_mem diff --git a/invokeai/app/services/graph.py b/invokeai/app/services/graph.py index 059ebca2d4..aa462ab170 100644 --- a/invokeai/app/services/graph.py +++ b/invokeai/app/services/graph.py @@ -3,29 +3,45 @@ import copy import itertools import traceback -from types import NoneType import uuid +from types import NoneType +from typing import ( + Annotated, + Any, + Literal, + Optional, + Union, + get_args, + get_origin, + get_type_hints, +) + import networkx as nx from pydantic import BaseModel, validator from pydantic.fields import Field -from typing import Any, Literal, Optional, Union, get_args, get_origin, get_type_hints, Annotated -from .invocation_services import InvocationServices -from ..invocations.baseinvocation import BaseInvocation, BaseInvocationOutput, InvocationContext from ..invocations import * +from ..invocations.baseinvocation import ( + BaseInvocation, + BaseInvocationOutput, + InvocationContext, +) +from .invocation_services import InvocationServices class EdgeConnection(BaseModel): - node_id: str = Field(description="The id of the node for this edge connection") + node_id: str = Field(description="The id of the node for this edge connection") field: str = Field(description="The field for this connection") def __eq__(self, other): - return (isinstance(other, self.__class__) and - getattr(other, 'node_id', None) == self.node_id and - getattr(other, 'field', None) == self.field) - + return ( + isinstance(other, self.__class__) + and getattr(other, "node_id", None) == self.node_id + and getattr(other, "field", None) == self.field + ) + def __hash__(self): - return hash(f'{self.node_id}.{self.field}') + return hash(f"{self.node_id}.{self.field}") def get_output_field(node: BaseInvocation, field: str) -> Any: @@ -34,7 +50,7 @@ def get_output_field(node: BaseInvocation, field: str) -> Any: node_output_field = node_outputs.get(field) or None return node_output_field - + def get_input_field(node: BaseInvocation, field: str) -> Any: node_type = type(node) node_inputs = get_type_hints(node_type) @@ -47,15 +63,17 @@ def are_connection_types_compatible(from_type: Any, to_type: Any) -> bool: return False if not to_type: return False - + # TODO: this is pretty forgiving on generic types. Clean that up (need to handle optionals and such) if from_type and to_type: # Ports are compatible - if (from_type == to_type or - from_type == Any or - to_type == Any or - Any in get_args(from_type) or - Any in get_args(to_type)): + if ( + from_type == to_type + or from_type == Any + or to_type == Any + or Any in get_args(from_type) + or Any in get_args(to_type) + ): return True if from_type in get_args(to_type): @@ -68,15 +86,13 @@ def are_connection_types_compatible(from_type: Any, to_type: Any) -> bool: return False else: return False - + return True def are_connections_compatible( - from_node: BaseInvocation, - from_field: str, - to_node: BaseInvocation, - to_field: str) -> bool: + from_node: BaseInvocation, from_field: str, to_node: BaseInvocation, to_field: str +) -> bool: """Determines if a connection between fields of two nodes is compatible.""" # TODO: handle iterators and collectors @@ -93,24 +109,26 @@ class NodeAlreadyInGraphError(Exception): class InvalidEdgeError(Exception): pass + class NodeNotFoundError(Exception): pass + class NodeAlreadyExecutedError(Exception): pass # TODO: Create and use an Empty output? class GraphInvocationOutput(BaseInvocationOutput): - type: Literal['graph_output'] = 'graph_output' + type: Literal["graph_output"] = "graph_output" # TODO: Fill this out and move to invocations class GraphInvocation(BaseInvocation): - type: Literal['graph'] = 'graph' + type: Literal["graph"] = "graph" # TODO: figure out how to create a default here - graph: 'Graph' = Field(description="The graph to run", default=None) + graph: "Graph" = Field(description="The graph to run", default=None) def invoke(self, context: InvocationContext) -> GraphInvocationOutput: """Invoke with provided services and return outputs.""" @@ -119,54 +137,71 @@ class GraphInvocation(BaseInvocation): class IterateInvocationOutput(BaseInvocationOutput): """Used to connect iteration outputs. Will be expanded to a specific output.""" - type: Literal['iterate_output'] = 'iterate_output' + + type: Literal["iterate_output"] = "iterate_output" item: Any = Field(description="The item being iterated over") # TODO: Fill this out and move to invocations class IterateInvocation(BaseInvocation): - type: Literal['iterate'] = 'iterate' + type: Literal["iterate"] = "iterate" - collection: list[Any] = Field(description="The list of items to iterate over", default_factory=list) - index: int = Field(description="The index, will be provided on executed iterators", default=0) + collection: list[Any] = Field( + description="The list of items to iterate over", default_factory=list + ) + index: int = Field( + description="The index, will be provided on executed iterators", default=0 + ) def invoke(self, context: InvocationContext) -> IterateInvocationOutput: """Produces the outputs as values""" - return IterateInvocationOutput(item = self.collection[self.index]) + return IterateInvocationOutput(item=self.collection[self.index]) class CollectInvocationOutput(BaseInvocationOutput): - type: Literal['collect_output'] = 'collect_output' + type: Literal["collect_output"] = "collect_output" collection: list[Any] = Field(description="The collection of input items") class CollectInvocation(BaseInvocation): """Collects values into a collection""" - type: Literal['collect'] = 'collect' - item: Any = Field(description="The item to collect (all inputs must be of the same type)", default=None) - collection: list[Any] = Field(description="The collection, will be provided on execution", default_factory=list) + type: Literal["collect"] = "collect" + + item: Any = Field( + description="The item to collect (all inputs must be of the same type)", + default=None, + ) + collection: list[Any] = Field( + description="The collection, will be provided on execution", + default_factory=list, + ) def invoke(self, context: InvocationContext) -> CollectInvocationOutput: """Invoke with provided services and return outputs.""" - return CollectInvocationOutput(collection = copy.copy(self.collection)) + return CollectInvocationOutput(collection=copy.copy(self.collection)) -InvocationsUnion = Union[BaseInvocation.get_invocations()] # type: ignore -InvocationOutputsUnion = Union[BaseInvocationOutput.get_all_subclasses_tuple()] # type: ignore +InvocationsUnion = Union[BaseInvocation.get_invocations()] # type: ignore +InvocationOutputsUnion = Union[BaseInvocationOutput.get_all_subclasses_tuple()] # type: ignore class Graph(BaseModel): id: str = Field(description="The id of this graph", default_factory=uuid.uuid4) # TODO: use a list (and never use dict in a BaseModel) because pydantic/fastapi hates me - nodes: dict[str, Annotated[InvocationsUnion, Field(discriminator="type")]] = Field(description="The nodes in this graph", default_factory=dict) - edges: list[tuple[EdgeConnection,EdgeConnection]] = Field(description="The connections between nodes and their fields in this graph", default_factory=list) + nodes: dict[str, Annotated[InvocationsUnion, Field(discriminator="type")]] = Field( + description="The nodes in this graph", default_factory=dict + ) + edges: list[tuple[EdgeConnection, EdgeConnection]] = Field( + description="The connections between nodes and their fields in this graph", + default_factory=list, + ) def add_node(self, node: BaseInvocation) -> None: """Adds a node to a graph - + :raises NodeAlreadyInGraphError: the node is already present in the graph. """ @@ -174,26 +209,26 @@ class Graph(BaseModel): raise NodeAlreadyInGraphError() self.nodes[node.id] = node - - def _get_graph_and_node(self, node_path: str) -> tuple['Graph', str]: + def _get_graph_and_node(self, node_path: str) -> tuple["Graph", str]: """Returns the graph and node id for a node path.""" # Materialized graphs may have nodes at the top level if node_path in self.nodes: return (self, node_path) - node_id = node_path if '.' not in node_path else node_path[:node_path.index('.')] + node_id = ( + node_path if "." not in node_path else node_path[: node_path.index(".")] + ) if node_id not in self.nodes: - raise NodeNotFoundError(f'Node {node_path} not found in graph') + raise NodeNotFoundError(f"Node {node_path} not found in graph") node = self.nodes[node_id] if not isinstance(node, GraphInvocation): # There's more node path left but this isn't a graph - failure - raise NodeNotFoundError('Node path terminated early at a non-graph node') - - return node.graph._get_graph_and_node(node_path[node_path.index('.')+1:]) + raise NodeNotFoundError("Node path terminated early at a non-graph node") + return node.graph._get_graph_and_node(node_path[node_path.index(".") + 1 :]) def delete_node(self, node_path: str) -> None: """Deletes a node from a graph""" @@ -205,21 +240,20 @@ class Graph(BaseModel): input_edges = self._get_input_edges_and_graphs(node_path) output_edges = self._get_output_edges_and_graphs(node_path) - for edge_graph,_,edge in input_edges: + for edge_graph, _, edge in input_edges: edge_graph.delete_edge(edge) - for edge_graph,_,edge in output_edges: + for edge_graph, _, edge in output_edges: edge_graph.delete_edge(edge) del graph.nodes[node_id] except NodeNotFoundError: - pass # Ignore, not doesn't exist (should this throw?) - + pass # Ignore, not doesn't exist (should this throw?) def add_edge(self, edge: tuple[EdgeConnection, EdgeConnection]) -> None: """Adds an edge to a graph - + :raises InvalidEdgeError: the provided edge is invalid. """ @@ -227,8 +261,7 @@ class Graph(BaseModel): self.edges.append(edge) else: raise InvalidEdgeError() - - + def delete_edge(self, edge: tuple[EdgeConnection, EdgeConnection]) -> None: """Deletes an edge from a graph""" @@ -237,7 +270,6 @@ class Graph(BaseModel): except KeyError: pass - def is_valid(self) -> bool: """Validates the graph.""" @@ -247,7 +279,9 @@ class Graph(BaseModel): return False # Validate all edges reference nodes in the graph - node_ids = set([e[0].node_id for e in self.edges]+[e[1].node_id for e in self.edges]) + node_ids = set( + [e[0].node_id for e in self.edges] + [e[1].node_id for e in self.edges] + ) if not all((self.has_node(node_id) for node_id in node_ids)): return False @@ -255,26 +289,45 @@ class Graph(BaseModel): g = self.nx_graph_flat() if not nx.is_directed_acyclic_graph(g): return False - + # Validate all edge connections are valid - if not all((are_connections_compatible( - self.get_node(e[0].node_id), e[0].field, - self.get_node(e[1].node_id), e[1].field - ) for e in self.edges)): + if not all( + ( + are_connections_compatible( + self.get_node(e[0].node_id), + e[0].field, + self.get_node(e[1].node_id), + e[1].field, + ) + for e in self.edges + ) + ): return False - + # Validate all iterators # TODO: may need to validate all iterators in subgraphs so edge connections in parent graphs will be available - if not all((self._is_iterator_connection_valid(n.id) for n in self.nodes.values() if isinstance(n, IterateInvocation))): + if not all( + ( + self._is_iterator_connection_valid(n.id) + for n in self.nodes.values() + if isinstance(n, IterateInvocation) + ) + ): return False # Validate all collectors # TODO: may need to validate all collectors in subgraphs so edge connections in parent graphs will be available - if not all((self._is_collector_connection_valid(n.id) for n in self.nodes.values() if isinstance(n, CollectInvocation))): + if not all( + ( + self._is_collector_connection_valid(n.id) + for n in self.nodes.values() + if isinstance(n, CollectInvocation) + ) + ): return False - + return True - + def _is_edge_valid(self, edge: tuple[EdgeConnection, EdgeConnection]) -> bool: """Validates that a new edge doesn't create a cycle in the graph""" @@ -295,29 +348,39 @@ class Graph(BaseModel): g.add_edge(edge[0].node_id, edge[1].node_id) if not nx.is_directed_acyclic_graph(g): return False - + # Validate that the field types are compatible - if not are_connections_compatible(from_node, edge[0].field, to_node, edge[1].field): + if not are_connections_compatible( + from_node, edge[0].field, to_node, edge[1].field + ): return False # Validate if iterator output type matches iterator input type (if this edge results in both being set) - if isinstance(to_node, IterateInvocation) and edge[1].field == 'collection': - if not self._is_iterator_connection_valid(edge[1].node_id, new_input = edge[0]): + if isinstance(to_node, IterateInvocation) and edge[1].field == "collection": + if not self._is_iterator_connection_valid( + edge[1].node_id, new_input=edge[0] + ): return False # Validate if iterator input type matches output type (if this edge results in both being set) - if isinstance(from_node, IterateInvocation) and edge[0].field == 'item': - if not self._is_iterator_connection_valid(edge[0].node_id, new_output = edge[1]): + if isinstance(from_node, IterateInvocation) and edge[0].field == "item": + if not self._is_iterator_connection_valid( + edge[0].node_id, new_output=edge[1] + ): return False # Validate if collector input type matches output type (if this edge results in both being set) - if isinstance(to_node, CollectInvocation) and edge[1].field == 'item': - if not self._is_collector_connection_valid(edge[1].node_id, new_input = edge[0]): + if isinstance(to_node, CollectInvocation) and edge[1].field == "item": + if not self._is_collector_connection_valid( + edge[1].node_id, new_input=edge[0] + ): return False # Validate if collector output type matches input type (if this edge results in both being set) - if isinstance(from_node, CollectInvocation) and edge[0].field == 'collection': - if not self._is_collector_connection_valid(edge[0].node_id, new_output = edge[1]): + if isinstance(from_node, CollectInvocation) and edge[0].field == "collection": + if not self._is_collector_connection_valid( + edge[0].node_id, new_output=edge[1] + ): return False return True @@ -339,25 +402,27 @@ class Graph(BaseModel): graph, node_id = self._get_graph_and_node(node_path) return graph.nodes[node_id] - def _get_node_path(self, node_id: str, prefix: Optional[str] = None) -> str: - return node_id if prefix is None or prefix == '' else f'{prefix}.{node_id}' - + return node_id if prefix is None or prefix == "" else f"{prefix}.{node_id}" def update_node(self, node_path: str, new_node: BaseInvocation) -> None: """Updates a node in the graph.""" graph, node_id = self._get_graph_and_node(node_path) node = graph.nodes[node_id] - + # Ensure the node type matches the new node if type(node) != type(new_node): - raise TypeError(f'Node {node_path} is type {type(node)} but new node is type {type(new_node)}') + raise TypeError( + f"Node {node_path} is type {type(node)} but new node is type {type(new_node)}" + ) # Ensure the new id is either the same or is not in the graph - prefix = None if '.' not in node_path else node_path[:node_path.rindex('.')] - new_path = self._get_node_path(new_node.id, prefix = prefix) + prefix = None if "." not in node_path else node_path[: node_path.rindex(".")] + new_path = self._get_node_path(new_node.id, prefix=prefix) if new_node.id != node.id and self.has_node(new_path): - raise NodeAlreadyInGraphError('Node with id {new_node.id} already exists in graph') + raise NodeAlreadyInGraphError( + "Node with id {new_node.id} already exists in graph" + ) # Set the new node in the graph graph.nodes[new_node.id] = new_node @@ -369,136 +434,247 @@ class Graph(BaseModel): graph.delete_node(node_path) # Create new edges for each input and output - for graph,_,edge in input_edges: + for graph, _, edge in input_edges: # Remove the graph prefix from the node path - new_graph_node_path = new_node.id if '.' not in edge[1].node_id else f'{edge[1].node_id[edge[1].node_id.rindex("."):]}.{new_node.id}' - graph.add_edge((edge[0], EdgeConnection(node_id = new_graph_node_path, field = edge[1].field))) + new_graph_node_path = ( + new_node.id + if "." not in edge[1].node_id + else f'{edge[1].node_id[edge[1].node_id.rindex("."):]}.{new_node.id}' + ) + graph.add_edge( + ( + edge[0], + EdgeConnection( + node_id=new_graph_node_path, field=edge[1].field + ), + ) + ) - for graph,_,edge in output_edges: + for graph, _, edge in output_edges: # Remove the graph prefix from the node path - new_graph_node_path = new_node.id if '.' not in edge[0].node_id else f'{edge[0].node_id[edge[0].node_id.rindex("."):]}.{new_node.id}' - graph.add_edge((EdgeConnection(node_id = new_graph_node_path, field = edge[0].field), edge[1])) - + new_graph_node_path = ( + new_node.id + if "." not in edge[0].node_id + else f'{edge[0].node_id[edge[0].node_id.rindex("."):]}.{new_node.id}' + ) + graph.add_edge( + ( + EdgeConnection( + node_id=new_graph_node_path, field=edge[0].field + ), + edge[1], + ) + ) - def _get_input_edges(self, node_path: str, field: Optional[str] = None) -> list[tuple[EdgeConnection,EdgeConnection]]: + def _get_input_edges( + self, node_path: str, field: Optional[str] = None + ) -> list[tuple[EdgeConnection, EdgeConnection]]: """Gets all input edges for a node""" edges = self._get_input_edges_and_graphs(node_path) - + # Filter to edges that match the field filtered_edges = (e for e in edges if field is None or e[2][1].field == field) # Create full node paths for each edge - return [(EdgeConnection(node_id = self._get_node_path(e[0].node_id, prefix = prefix), field=e[0].field), EdgeConnection(node_id = self._get_node_path(e[1].node_id, prefix = prefix), field=e[1].field)) for _,prefix,e in filtered_edges] + return [ + ( + EdgeConnection( + node_id=self._get_node_path(e[0].node_id, prefix=prefix), + field=e[0].field, + ), + EdgeConnection( + node_id=self._get_node_path(e[1].node_id, prefix=prefix), + field=e[1].field, + ), + ) + for _, prefix, e in filtered_edges + ] - - def _get_input_edges_and_graphs(self, node_path: str, prefix: Optional[str] = None) -> list[tuple['Graph', str, tuple[EdgeConnection,EdgeConnection]]]: + def _get_input_edges_and_graphs( + self, node_path: str, prefix: Optional[str] = None + ) -> list[tuple["Graph", str, tuple[EdgeConnection, EdgeConnection]]]: """Gets all input edges for a node along with the graph they are in and the graph's path""" edges = list() # Return any input edges that appear in this graph - edges.extend([(self, prefix, e) for e in self.edges if e[1].node_id == node_path]) + edges.extend( + [(self, prefix, e) for e in self.edges if e[1].node_id == node_path] + ) - node_id = node_path if '.' not in node_path else node_path[:node_path.index('.')] + node_id = ( + node_path if "." not in node_path else node_path[: node_path.index(".")] + ) node = self.nodes[node_id] if isinstance(node, GraphInvocation): graph = node.graph - graph_path = node.id if prefix is None or prefix == '' else self._get_node_path(node.id, prefix = prefix) - graph_edges = graph._get_input_edges_and_graphs(node_path[(len(node_id)+1):], prefix=graph_path) + graph_path = ( + node.id + if prefix is None or prefix == "" + else self._get_node_path(node.id, prefix=prefix) + ) + graph_edges = graph._get_input_edges_and_graphs( + node_path[(len(node_id) + 1) :], prefix=graph_path + ) edges.extend(graph_edges) - + return edges - - def _get_output_edges(self, node_path: str, field: str) -> list[tuple[EdgeConnection,EdgeConnection]]: + def _get_output_edges( + self, node_path: str, field: str + ) -> list[tuple[EdgeConnection, EdgeConnection]]: """Gets all output edges for a node""" edges = self._get_output_edges_and_graphs(node_path) - + # Filter to edges that match the field filtered_edges = (e for e in edges if e[2][0].field == field) # Create full node paths for each edge - return [(EdgeConnection(node_id = self._get_node_path(e[0].node_id, prefix = prefix), field=e[0].field), EdgeConnection(node_id = self._get_node_path(e[1].node_id, prefix = prefix), field=e[1].field)) for _,prefix,e in filtered_edges] + return [ + ( + EdgeConnection( + node_id=self._get_node_path(e[0].node_id, prefix=prefix), + field=e[0].field, + ), + EdgeConnection( + node_id=self._get_node_path(e[1].node_id, prefix=prefix), + field=e[1].field, + ), + ) + for _, prefix, e in filtered_edges + ] - - def _get_output_edges_and_graphs(self, node_path: str, prefix: Optional[str] = None) -> list[tuple['Graph', str, tuple[EdgeConnection,EdgeConnection]]]: + def _get_output_edges_and_graphs( + self, node_path: str, prefix: Optional[str] = None + ) -> list[tuple["Graph", str, tuple[EdgeConnection, EdgeConnection]]]: """Gets all output edges for a node along with the graph they are in and the graph's path""" edges = list() # Return any input edges that appear in this graph - edges.extend([(self, prefix, e) for e in self.edges if e[0].node_id == node_path]) + edges.extend( + [(self, prefix, e) for e in self.edges if e[0].node_id == node_path] + ) - node_id = node_path if '.' not in node_path else node_path[:node_path.index('.')] + node_id = ( + node_path if "." not in node_path else node_path[: node_path.index(".")] + ) node = self.nodes[node_id] if isinstance(node, GraphInvocation): graph = node.graph - graph_path = node.id if prefix is None or prefix == '' else self._get_node_path(node.id, prefix = prefix) - graph_edges = graph._get_output_edges_and_graphs(node_path[(len(node_id)+1):], prefix=graph_path) + graph_path = ( + node.id + if prefix is None or prefix == "" + else self._get_node_path(node.id, prefix=prefix) + ) + graph_edges = graph._get_output_edges_and_graphs( + node_path[(len(node_id) + 1) :], prefix=graph_path + ) edges.extend(graph_edges) - + return edges - - def _is_iterator_connection_valid(self, node_path: str, new_input: Optional[EdgeConnection] = None, new_output: Optional[EdgeConnection] = None) -> bool: - inputs = list([e[0] for e in self._get_input_edges(node_path, 'collection')]) - outputs = list([e[1] for e in self._get_output_edges(node_path, 'item')]) + def _is_iterator_connection_valid( + self, + node_path: str, + new_input: Optional[EdgeConnection] = None, + new_output: Optional[EdgeConnection] = None, + ) -> bool: + inputs = list([e[0] for e in self._get_input_edges(node_path, "collection")]) + outputs = list([e[1] for e in self._get_output_edges(node_path, "item")]) if new_input is not None: inputs.append(new_input) if new_output is not None: outputs.append(new_output) - + # Only one input is allowed for iterators if len(inputs) > 1: return False # Get input and output fields (the fields linked to the iterator's input/output) - input_field = get_output_field(self.get_node(inputs[0].node_id), inputs[0].field) - output_fields = list([get_input_field(self.get_node(e.node_id), e.field) for e in outputs]) + input_field = get_output_field( + self.get_node(inputs[0].node_id), inputs[0].field + ) + output_fields = list( + [get_input_field(self.get_node(e.node_id), e.field) for e in outputs] + ) # Input type must be a list if get_origin(input_field) != list: return False - + # Validate that all outputs match the input type input_field_item_type = get_args(input_field)[0] - if not all((are_connection_types_compatible(input_field_item_type, f) for f in output_fields)): + if not all( + ( + are_connection_types_compatible(input_field_item_type, f) + for f in output_fields + ) + ): return False return True - def _is_collector_connection_valid(self, node_path: str, new_input: Optional[EdgeConnection] = None, new_output: Optional[EdgeConnection] = None) -> bool: - inputs = list([e[0] for e in self._get_input_edges(node_path, 'item')]) - outputs = list([e[1] for e in self._get_output_edges(node_path, 'collection')]) + def _is_collector_connection_valid( + self, + node_path: str, + new_input: Optional[EdgeConnection] = None, + new_output: Optional[EdgeConnection] = None, + ) -> bool: + inputs = list([e[0] for e in self._get_input_edges(node_path, "item")]) + outputs = list([e[1] for e in self._get_output_edges(node_path, "collection")]) if new_input is not None: inputs.append(new_input) if new_output is not None: outputs.append(new_output) - + # Get input and output fields (the fields linked to the iterator's input/output) - input_fields = list([get_output_field(self.get_node(e.node_id), e.field) for e in inputs]) - output_fields = list([get_input_field(self.get_node(e.node_id), e.field) for e in outputs]) - + input_fields = list( + [get_output_field(self.get_node(e.node_id), e.field) for e in inputs] + ) + output_fields = list( + [get_input_field(self.get_node(e.node_id), e.field) for e in outputs] + ) + # Validate that all inputs are derived from or match a single type - input_field_types = set([t for input_field in input_fields for t in ([input_field] if get_origin(input_field) == None else get_args(input_field)) if t != NoneType]) # Get unique types + input_field_types = set( + [ + t + for input_field in input_fields + for t in ( + [input_field] + if get_origin(input_field) == None + else get_args(input_field) + ) + if t != NoneType + ] + ) # Get unique types type_tree = nx.DiGraph() type_tree.add_nodes_from(input_field_types) - type_tree.add_edges_from([e for e in itertools.permutations(input_field_types, 2) if issubclass(e[1], e[0])]) + type_tree.add_edges_from( + [ + e + for e in itertools.permutations(input_field_types, 2) + if issubclass(e[1], e[0]) + ] + ) type_degrees = type_tree.in_degree(type_tree.nodes) - if sum((t[1] == 0 for t in type_degrees)) != 1: # type: ignore - return False # There is more than one root type + if sum((t[1] == 0 for t in type_degrees)) != 1: # type: ignore + return False # There is more than one root type # Get the input root type - input_root_type = next(t[0] for t in type_degrees if t[1] == 0) # type: ignore + input_root_type = next(t[0] for t in type_degrees if t[1] == 0) # type: ignore # Verify that all outputs are lists if not all((get_origin(f) == list for f in output_fields)): return False # Verify that all outputs match the input type (are a base class or the same class) - if not all((issubclass(input_root_type, get_args(f)[0]) for f in output_fields)): + if not all( + (issubclass(input_root_type, get_args(f)[0]) for f in output_fields) + ): return False return True @@ -511,49 +687,86 @@ class Graph(BaseModel): g.add_edges_from(set([(e[0].node_id, e[1].node_id) for e in self.edges])) return g - def nx_graph_flat(self, nx_graph: Optional[nx.DiGraph] = None, prefix: Optional[str] = None) -> nx.DiGraph: + def nx_graph_flat( + self, nx_graph: Optional[nx.DiGraph] = None, prefix: Optional[str] = None + ) -> nx.DiGraph: """Returns a flattened NetworkX DiGraph, including all subgraphs (but not with iterations expanded)""" g = nx_graph or nx.DiGraph() # Add all nodes from this graph except graph/iteration nodes - g.add_nodes_from([self._get_node_path(n.id, prefix) for n in self.nodes.values() if not isinstance(n, GraphInvocation) and not isinstance(n, IterateInvocation)]) + g.add_nodes_from( + [ + self._get_node_path(n.id, prefix) + for n in self.nodes.values() + if not isinstance(n, GraphInvocation) + and not isinstance(n, IterateInvocation) + ] + ) # Expand graph nodes - for sgn in (gn for gn in self.nodes.values() if isinstance(gn, GraphInvocation)): + for sgn in ( + gn for gn in self.nodes.values() if isinstance(gn, GraphInvocation) + ): sgn.graph.nx_graph_flat(g, self._get_node_path(sgn.id, prefix)) # TODO: figure out if iteration nodes need to be expanded unique_edges = set([(e[0].node_id, e[1].node_id) for e in self.edges]) - g.add_edges_from([(self._get_node_path(e[0], prefix), self._get_node_path(e[1], prefix)) for e in unique_edges]) + g.add_edges_from( + [ + (self._get_node_path(e[0], prefix), self._get_node_path(e[1], prefix)) + for e in unique_edges + ] + ) return g class GraphExecutionState(BaseModel): """Tracks the state of a graph execution""" - id: str = Field(description="The id of the execution state", default_factory=uuid.uuid4) + + id: str = Field( + description="The id of the execution state", default_factory=uuid.uuid4 + ) # TODO: Store a reference to the graph instead of the actual graph? graph: Graph = Field(description="The graph being executed") # The graph of materialized nodes - execution_graph: Graph = Field(description="The expanded graph of activated and executed nodes", default_factory=Graph) + execution_graph: Graph = Field( + description="The expanded graph of activated and executed nodes", + default_factory=Graph, + ) # Nodes that have been executed - executed: set[str] = Field(description="The set of node ids that have been executed", default_factory=set) - executed_history: list[str] = Field(description="The list of node ids that have been executed, in order of execution", default_factory=list) + executed: set[str] = Field( + description="The set of node ids that have been executed", default_factory=set + ) + executed_history: list[str] = Field( + description="The list of node ids that have been executed, in order of execution", + default_factory=list, + ) # The results of executed nodes - results: dict[str, Annotated[InvocationOutputsUnion, Field(discriminator="type")]] = Field(description="The results of node executions", default_factory=dict) + results: dict[ + str, Annotated[InvocationOutputsUnion, Field(discriminator="type")] + ] = Field(description="The results of node executions", default_factory=dict) # Errors raised when executing nodes - errors: dict[str, str] = Field(description="Errors raised when executing nodes", default_factory=dict) + errors: dict[str, str] = Field( + description="Errors raised when executing nodes", default_factory=dict + ) # Map of prepared/executed nodes to their original nodes - prepared_source_mapping: dict[str, str] = Field(description="The map of prepared nodes to original graph nodes", default_factory=dict) + prepared_source_mapping: dict[str, str] = Field( + description="The map of prepared nodes to original graph nodes", + default_factory=dict, + ) # Map of original nodes to prepared nodes - source_prepared_mapping: dict[str, set[str]] = Field(description="The map of original graph nodes to prepared nodes", default_factory=dict) + source_prepared_mapping: dict[str, set[str]] = Field( + description="The map of original graph nodes to prepared nodes", + default_factory=dict, + ) def next(self) -> BaseInvocation | None: """Gets the next node ready to execute.""" @@ -569,10 +782,10 @@ class GraphExecutionState(BaseModel): # TODO: prepare multiple nodes at once? # while prepared_id is not None and not isinstance(self.graph.nodes[prepared_id], IterateInvocation): # prepared_id = self._prepare() - + if prepared_id is not None: next_node = self._get_next_node() - + # Get values from edges if next_node is not None: self._prepare_inputs(next_node) @@ -584,7 +797,7 @@ class GraphExecutionState(BaseModel): """Marks a node as complete""" if node_id not in self.execution_graph.nodes: - return # TODO: log error? + return # TODO: log error? # Mark node as executed self.executed.add(node_id) @@ -597,7 +810,7 @@ class GraphExecutionState(BaseModel): if all([n in self.executed for n in prepared_nodes]): self.executed.add(source_node) self.executed_history.append(source_node) - + def set_node_error(self, node_id: str, error: str): """Marks a node as errored""" self.errors[node_id] = error @@ -605,12 +818,14 @@ class GraphExecutionState(BaseModel): def is_complete(self) -> bool: """Returns true if the graph is complete""" return self.has_error() or all((k in self.executed for k in self.graph.nodes)) - + def has_error(self) -> bool: """Returns true if the graph has any errors""" return len(self.errors) > 0 - def _create_execution_node(self, node_path: str, iteration_node_map: list[tuple[str, str]]) -> list[str]: + def _create_execution_node( + self, node_path: str, iteration_node_map: list[tuple[str, str]] + ) -> list[str]: """Prepares an iteration node and connects all edges, returning the new node id""" node = self.graph.get_node(node_path) @@ -620,10 +835,20 @@ class GraphExecutionState(BaseModel): # If this is an iterator node, we must create a copy for each iteration if isinstance(node, IterateInvocation): # Get input collection edge (should error if there are no inputs) - input_collection_edge = next(iter(self.graph._get_input_edges(node_path, 'collection'))) - input_collection_prepared_node_id = next(n[1] for n in iteration_node_map if n[0] == input_collection_edge[0].node_id) - input_collection_prepared_node_output = self.results[input_collection_prepared_node_id] - input_collection = getattr(input_collection_prepared_node_output, input_collection_edge[0].field) + input_collection_edge = next( + iter(self.graph._get_input_edges(node_path, "collection")) + ) + input_collection_prepared_node_id = next( + n[1] + for n in iteration_node_map + if n[0] == input_collection_edge[0].node_id + ) + input_collection_prepared_node_output = self.results[ + input_collection_prepared_node_id + ] + input_collection = getattr( + input_collection_prepared_node_output, input_collection_edge[0].field + ) self_iteration_count = len(input_collection) new_nodes = list() @@ -638,12 +863,17 @@ class GraphExecutionState(BaseModel): # For collect nodes, this may contain multiple inputs to the same field new_edges = list() for edge in input_edges: - for input_node_id in (n[1] for n in iteration_node_map if n[0] == edge[0].node_id): - new_edge = (EdgeConnection(node_id = input_node_id, field = edge[0].field), EdgeConnection(node_id = '', field = edge[1].field)) + for input_node_id in ( + n[1] for n in iteration_node_map if n[0] == edge[0].node_id + ): + new_edge = ( + EdgeConnection(node_id=input_node_id, field=edge[0].field), + EdgeConnection(node_id="", field=edge[1].field), + ) new_edges.append(new_edge) - + # Create a new node (or one for each iteration of this iterator) - for i in (range(self_iteration_count) if self_iteration_count > 0 else [-1]): + for i in range(self_iteration_count) if self_iteration_count > 0 else [-1]: # Create a new node new_node = copy.deepcopy(node) @@ -663,7 +893,10 @@ class GraphExecutionState(BaseModel): # Add new edges to execution graph for edge in new_edges: - new_edge = (edge[0], EdgeConnection(node_id = new_node.id, field = edge[1].field)) + new_edge = ( + edge[0], + EdgeConnection(node_id=new_node.id, field=edge[1].field), + ) self.execution_graph.add_edge(new_edge) new_nodes.append(new_node.id) @@ -673,26 +906,40 @@ class GraphExecutionState(BaseModel): def _iterator_graph(self) -> nx.DiGraph: """Gets a DiGraph with edges to collectors removed so an ancestor search produces all active iterators for any node""" g = self.graph.nx_graph() - collectors = (n for n in self.graph.nodes if isinstance(self.graph.nodes[n], CollectInvocation)) + collectors = ( + n + for n in self.graph.nodes + if isinstance(self.graph.nodes[n], CollectInvocation) + ) for c in collectors: g.remove_edges_from(list(g.in_edges(c))) return g - def _get_node_iterators(self, node_id: str) -> list[str]: """Gets iterators for a node""" g = self._iterator_graph() - iterators = [n for n in nx.ancestors(g, node_id) if isinstance(self.graph.nodes[n], IterateInvocation)] + iterators = [ + n + for n in nx.ancestors(g, node_id) + if isinstance(self.graph.nodes[n], IterateInvocation) + ] return iterators - def _prepare(self) -> Optional[str]: # Get flattened source graph g = self.graph.nx_graph_flat() # Find next unprepared node where all source nodes are executed sorted_nodes = nx.topological_sort(g) - next_node_id = next((n for n in sorted_nodes if n not in self.source_prepared_mapping and all((e[0] in self.executed for e in g.in_edges(n)))), None) + next_node_id = next( + ( + n + for n in sorted_nodes + if n not in self.source_prepared_mapping + and all((e[0] in self.executed for e in g.in_edges(n))) + ), + None, + ) if next_node_id == None: return None @@ -705,48 +952,86 @@ class GraphExecutionState(BaseModel): new_node_ids = list() if isinstance(next_node, CollectInvocation): # Collapse all iterator input mappings and create a single execution node for the collect invocation - all_iteration_mappings = list(itertools.chain(*(((s,p) for p in self.source_prepared_mapping[s]) for s in next_node_parents))) - #all_iteration_mappings = list(set(itertools.chain(*prepared_parent_mappings))) - create_results = self._create_execution_node(next_node_id, all_iteration_mappings) + all_iteration_mappings = list( + itertools.chain( + *( + ((s, p) for p in self.source_prepared_mapping[s]) + for s in next_node_parents + ) + ) + ) + # all_iteration_mappings = list(set(itertools.chain(*prepared_parent_mappings))) + create_results = self._create_execution_node( + next_node_id, all_iteration_mappings + ) if create_results is not None: new_node_ids.extend(create_results) - else: # Iterators or normal nodes + else: # Iterators or normal nodes # Get all iterator combinations for this node # Will produce a list of lists of prepared iterator nodes, from which results can be iterated iterator_nodes = self._get_node_iterators(next_node_id) - iterator_nodes_prepared = [list(self.source_prepared_mapping[n]) for n in iterator_nodes] - iterator_node_prepared_combinations = list(itertools.product(*iterator_nodes_prepared)) + iterator_nodes_prepared = [ + list(self.source_prepared_mapping[n]) for n in iterator_nodes + ] + iterator_node_prepared_combinations = list( + itertools.product(*iterator_nodes_prepared) + ) # Select the correct prepared parents for each iteration # For every iterator, the parent must either not be a child of that iterator, or must match the prepared iteration for that iterator # TODO: Handle a node mapping to none eg = self.execution_graph.nx_graph_flat() - prepared_parent_mappings = [[(n,self._get_iteration_node(n, g, eg, it)) for n in next_node_parents] for it in iterator_node_prepared_combinations] # type: ignore - + prepared_parent_mappings = [[(n, self._get_iteration_node(n, g, eg, it)) for n in next_node_parents] for it in iterator_node_prepared_combinations] # type: ignore + # Create execution node for each iteration for iteration_mappings in prepared_parent_mappings: - create_results = self._create_execution_node(next_node_id, iteration_mappings) # type: ignore + create_results = self._create_execution_node(next_node_id, iteration_mappings) # type: ignore if create_results is not None: new_node_ids.extend(create_results) return next(iter(new_node_ids), None) - - def _get_iteration_node(self, source_node_path: str, graph: nx.DiGraph, execution_graph: nx.DiGraph, prepared_iterator_nodes: list[str]) -> Optional[str]: + + def _get_iteration_node( + self, + source_node_path: str, + graph: nx.DiGraph, + execution_graph: nx.DiGraph, + prepared_iterator_nodes: list[str], + ) -> Optional[str]: """Gets the prepared version of the specified source node that matches every iteration specified""" prepared_nodes = self.source_prepared_mapping[source_node_path] if len(prepared_nodes) == 1: return next(iter(prepared_nodes)) - + # Check if the requested node is an iterator - prepared_iterator = next((n for n in prepared_nodes if n in prepared_iterator_nodes), None) + prepared_iterator = next( + (n for n in prepared_nodes if n in prepared_iterator_nodes), None + ) if prepared_iterator is not None: return prepared_iterator # Filter to only iterator nodes that are a parent of the specified node, in tuple format (prepared, source) - iterator_source_node_mapping = [(n, self.prepared_source_mapping[n]) for n in prepared_iterator_nodes] - parent_iterators = [itn for itn in iterator_source_node_mapping if nx.has_path(graph, itn[1], source_node_path)] + iterator_source_node_mapping = [ + (n, self.prepared_source_mapping[n]) for n in prepared_iterator_nodes + ] + parent_iterators = [ + itn + for itn in iterator_source_node_mapping + if nx.has_path(graph, itn[1], source_node_path) + ] - return next((n for n in prepared_nodes if all(pit for pit in parent_iterators if nx.has_path(execution_graph, pit[0], n))), None) + return next( + ( + n + for n in prepared_nodes + if all( + pit + for pit in parent_iterators + if nx.has_path(execution_graph, pit[0], n) + ) + ), + None, + ) def _get_next_node(self) -> Optional[BaseInvocation]: g = self.execution_graph.nx_graph() @@ -760,8 +1045,12 @@ class GraphExecutionState(BaseModel): def _prepare_inputs(self, node: BaseInvocation): input_edges = [e for e in self.execution_graph.edges if e[1].node_id == node.id] if isinstance(node, CollectInvocation): - output_collection = [getattr(self.results[edge[0].node_id], edge[0].field) for edge in input_edges if edge[1].field == 'item'] - setattr(node, 'collection', output_collection) + output_collection = [ + getattr(self.results[edge[0].node_id], edge[0].field) + for edge in input_edges + if edge[1].field == "item" + ] + setattr(node, "collection", output_collection) else: for edge in input_edges: output_value = getattr(self.results[edge[0].node_id], edge[0].field) @@ -771,7 +1060,7 @@ class GraphExecutionState(BaseModel): def _is_edge_valid(self, edge: tuple[EdgeConnection, EdgeConnection]) -> bool: if not self._is_edge_valid(edge): return False - + # Invalid if destination has already been prepared or executed if edge[1].node_id in self.source_prepared_mapping: return False @@ -785,25 +1074,34 @@ class GraphExecutionState(BaseModel): def add_node(self, node: BaseInvocation) -> None: self.graph.add_node(node) - + def update_node(self, node_path: str, new_node: BaseInvocation) -> None: if not self._is_node_updatable(node_path): - raise NodeAlreadyExecutedError(f'Node {node_path} has already been prepared or executed and cannot be updated') + raise NodeAlreadyExecutedError( + f"Node {node_path} has already been prepared or executed and cannot be updated" + ) self.graph.update_node(node_path, new_node) def delete_node(self, node_path: str) -> None: if not self._is_node_updatable(node_path): - raise NodeAlreadyExecutedError(f'Node {node_path} has already been prepared or executed and cannot be deleted') + raise NodeAlreadyExecutedError( + f"Node {node_path} has already been prepared or executed and cannot be deleted" + ) self.graph.delete_node(node_path) def add_edge(self, edge: tuple[EdgeConnection, EdgeConnection]) -> None: if not self._is_node_updatable(edge[1].node_id): - raise NodeAlreadyExecutedError(f'Destination node {edge[1].node_id} has already been prepared or executed and cannot be linked to') + raise NodeAlreadyExecutedError( + f"Destination node {edge[1].node_id} has already been prepared or executed and cannot be linked to" + ) self.graph.add_edge(edge) - + def delete_edge(self, edge: tuple[EdgeConnection, EdgeConnection]) -> None: if not self._is_node_updatable(edge[1].node_id): - raise NodeAlreadyExecutedError(f'Destination node {edge[1].node_id} has already been prepared or executed and cannot have a source edge deleted') + raise NodeAlreadyExecutedError( + f"Destination node {edge[1].node_id} has already been prepared or executed and cannot have a source edge deleted" + ) self.graph.delete_edge(edge) + GraphInvocation.update_forward_refs() diff --git a/invokeai/app/services/image_storage.py b/invokeai/app/services/image_storage.py index fa6a85dfbe..ad0ff23f14 100644 --- a/invokeai/app/services/image_storage.py +++ b/invokeai/app/services/image_storage.py @@ -1,20 +1,22 @@ # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) -from abc import ABC, abstractmethod -from enum import Enum import datetime import os +from abc import ABC, abstractmethod +from enum import Enum from pathlib import Path from queue import Queue from typing import Dict + from PIL.Image import Image + from invokeai.backend.image_util import PngWriter class ImageType(str, Enum): - RESULT = 'results' - INTERMEDIATE = 'intermediates' - UPLOAD = 'uploads' + RESULT = "results" + INTERMEDIATE = "intermediates" + UPLOAD = "uploads" class ImageStorageBase(ABC): @@ -38,14 +40,15 @@ class ImageStorageBase(ABC): pass def create_name(self, context_id: str, node_id: str) -> str: - return f'{context_id}_{node_id}_{str(int(datetime.datetime.now(datetime.timezone.utc).timestamp()))}.png' + return f"{context_id}_{node_id}_{str(int(datetime.datetime.now(datetime.timezone.utc).timestamp()))}.png" class DiskImageStorage(ImageStorageBase): """Stores images on disk""" + __output_folder: str __pngWriter: PngWriter - __cache_ids: Queue # TODO: this is an incredibly naive cache + __cache_ids: Queue # TODO: this is an incredibly naive cache __cache: Dict[str, Image] __max_cache_size: int @@ -54,13 +57,15 @@ class DiskImageStorage(ImageStorageBase): self.__pngWriter = PngWriter(output_folder) self.__cache = dict() self.__cache_ids = Queue() - self.__max_cache_size = 10 # TODO: get this from config + self.__max_cache_size = 10 # TODO: get this from config Path(output_folder).mkdir(parents=True, exist_ok=True) # TODO: don't hard-code. get/save/delete should maybe take subpath? for image_type in ImageType: - Path(os.path.join(output_folder, image_type)).mkdir(parents=True, exist_ok=True) + Path(os.path.join(output_folder, image_type)).mkdir( + parents=True, exist_ok=True + ) def get(self, image_type: ImageType, image_name: str) -> Image: image_path = self.get_path(image_type, image_name) @@ -79,7 +84,9 @@ class DiskImageStorage(ImageStorageBase): def save(self, image_type: ImageType, image_name: str, image: Image) -> None: image_subpath = os.path.join(image_type, image_name) - self.__pngWriter.save_image_and_prompt_to_png(image, "", image_subpath, None) # TODO: just pass full path to png writer + self.__pngWriter.save_image_and_prompt_to_png( + image, "", image_subpath, None + ) # TODO: just pass full path to png writer image_path = self.get_path(image_type, image_name) self.__set_cache(image_path, image) @@ -88,7 +95,7 @@ class DiskImageStorage(ImageStorageBase): image_path = self.get_path(image_type, image_name) if os.path.exists(image_path): os.remove(image_path) - + if image_path in self.__cache: del self.__cache[image_path] @@ -98,7 +105,9 @@ class DiskImageStorage(ImageStorageBase): def __set_cache(self, image_name: str, image: Image): if not image_name in self.__cache: self.__cache[image_name] = image - self.__cache_ids.put(image_name) # TODO: this should refresh position for LRU cache + self.__cache_ids.put( + image_name + ) # TODO: this should refresh position for LRU cache if len(self.__cache) > self.__max_cache_size: cache_id = self.__cache_ids.get() del self.__cache[cache_id] diff --git a/invokeai/app/services/invocation_queue.py b/invokeai/app/services/invocation_queue.py index 0a5b5ae3bb..88a4f8708d 100644 --- a/invokeai/app/services/invocation_queue.py +++ b/invokeai/app/services/invocation_queue.py @@ -6,17 +6,19 @@ from queue import Queue # TODO: make this serializable class InvocationQueueItem: - #session_id: str + # session_id: str graph_execution_state_id: str invocation_id: str invoke_all: bool - def __init__(self, - #session_id: str, + def __init__( + self, + # session_id: str, graph_execution_state_id: str, invocation_id: str, - invoke_all: bool = False): - #self.session_id = session_id + invoke_all: bool = False, + ): + # self.session_id = session_id self.graph_execution_state_id = graph_execution_state_id self.invocation_id = invocation_id self.invoke_all = invoke_all @@ -24,12 +26,13 @@ class InvocationQueueItem: class InvocationQueueABC(ABC): """Abstract base class for all invocation queues""" + @abstractmethod def get(self) -> InvocationQueueItem: pass - + @abstractmethod - def put(self, item: InvocationQueueItem|None) -> None: + def put(self, item: InvocationQueueItem | None) -> None: pass @@ -38,9 +41,9 @@ class MemoryInvocationQueue(InvocationQueueABC): def __init__(self): self.__queue = Queue() - + def get(self) -> InvocationQueueItem: return self.__queue.get() - - def put(self, item: InvocationQueueItem|None) -> None: + + def put(self, item: InvocationQueueItem | None) -> None: self.__queue.put(item) diff --git a/invokeai/app/services/invocation_services.py b/invokeai/app/services/invocation_services.py index 93d60bb230..42cbd6c271 100644 --- a/invokeai/app/services/invocation_services.py +++ b/invokeai/app/services/invocation_services.py @@ -1,28 +1,32 @@ # Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654) -from .invocation_queue import InvocationQueueABC -from .item_storage import ItemStorageABC -from .image_storage import ImageStorageBase -from .events import EventServiceBase from invokeai.backend import Generate -class InvocationServices(): +from .events import EventServiceBase +from .image_storage import ImageStorageBase +from .invocation_queue import InvocationQueueABC +from .item_storage import ItemStorageABC + + +class InvocationServices: """Services that can be used by invocations""" - generate: Generate # TODO: wrap Generate, or split it up from model? + + generate: Generate # TODO: wrap Generate, or split it up from model? events: EventServiceBase images: ImageStorageBase queue: InvocationQueueABC # NOTE: we must forward-declare any types that include invocations, since invocations can use services - graph_execution_manager: ItemStorageABC['GraphExecutionState'] - processor: 'InvocationProcessorABC' + graph_execution_manager: ItemStorageABC["GraphExecutionState"] + processor: "InvocationProcessorABC" - def __init__(self, + def __init__( + self, generate: Generate, events: EventServiceBase, images: ImageStorageBase, queue: InvocationQueueABC, - graph_execution_manager: ItemStorageABC['GraphExecutionState'], - processor: 'InvocationProcessorABC' + graph_execution_manager: ItemStorageABC["GraphExecutionState"], + processor: "InvocationProcessorABC", ): self.generate = generate self.events = events diff --git a/invokeai/app/services/invoker.py b/invokeai/app/services/invoker.py index 4397a75021..f234cd827b 100644 --- a/invokeai/app/services/invoker.py +++ b/invokeai/app/services/invoker.py @@ -2,11 +2,12 @@ from abc import ABC from threading import Event, Thread -from .graph import Graph, GraphExecutionState -from .item_storage import ItemStorageABC + from ..invocations.baseinvocation import InvocationContext -from .invocation_services import InvocationServices +from .graph import Graph, GraphExecutionState from .invocation_queue import InvocationQueueABC, InvocationQueueItem +from .invocation_services import InvocationServices +from .item_storage import ItemStorageABC class Invoker: @@ -14,14 +15,13 @@ class Invoker: services: InvocationServices - def __init__(self, - services: InvocationServices - ): + def __init__(self, services: InvocationServices): self.services = services self._start() - - def invoke(self, graph_execution_state: GraphExecutionState, invoke_all: bool = False) -> str|None: + def invoke( + self, graph_execution_state: GraphExecutionState, invoke_all: bool = False + ) -> str | None: """Determines the next node to invoke and returns the id of the invoked node, or None if there are no nodes to execute""" # Get the next invocation @@ -33,38 +33,36 @@ class Invoker: self.services.graph_execution_manager.set(graph_execution_state) # Queue the invocation - print(f'queueing item {invocation.id}') - self.services.queue.put(InvocationQueueItem( - #session_id = session.id, - graph_execution_state_id = graph_execution_state.id, - invocation_id = invocation.id, - invoke_all = invoke_all - )) + print(f"queueing item {invocation.id}") + self.services.queue.put( + InvocationQueueItem( + # session_id = session.id, + graph_execution_state_id=graph_execution_state.id, + invocation_id=invocation.id, + invoke_all=invoke_all, + ) + ) return invocation.id - - def create_execution_state(self, graph: Graph|None = None) -> GraphExecutionState: + def create_execution_state(self, graph: Graph | None = None) -> GraphExecutionState: """Creates a new execution state for the given graph""" - new_state = GraphExecutionState(graph = Graph() if graph is None else graph) + new_state = GraphExecutionState(graph=Graph() if graph is None else graph) self.services.graph_execution_manager.set(new_state) return new_state - def __start_service(self, service) -> None: # Call start() method on any services that have it - start_op = getattr(service, 'start', None) + start_op = getattr(service, "start", None) if callable(start_op): start_op(self) - def __stop_service(self, service) -> None: # Call stop() method on any services that have it - stop_op = getattr(service, 'stop', None) + stop_op = getattr(service, "stop", None) if callable(stop_op): stop_op(self) - def _start(self) -> None: """Starts the invoker. This is called automatically when the invoker is created.""" for service in vars(self.services): @@ -73,7 +71,6 @@ class Invoker: for service in vars(self.services): self.__start_service(getattr(self.services, service)) - def stop(self) -> None: """Stops the invoker. A new invoker will have to be created to execute further.""" # First stop all services @@ -87,4 +84,4 @@ class Invoker: class InvocationProcessorABC(ABC): - pass \ No newline at end of file + pass diff --git a/invokeai/app/services/item_storage.py b/invokeai/app/services/item_storage.py index 738f06cb7e..83044e5a52 100644 --- a/invokeai/app/services/item_storage.py +++ b/invokeai/app/services/item_storage.py @@ -1,18 +1,20 @@ +from abc import ABC, abstractmethod +from typing import Callable, Generic, TypeVar -from typing import Callable, TypeVar, Generic from pydantic import BaseModel, Field from pydantic.generics import GenericModel -from abc import ABC, abstractmethod -T = TypeVar('T', bound=BaseModel) +T = TypeVar("T", bound=BaseModel) + class PaginatedResults(GenericModel, Generic[T]): """Paginated results""" - items: list[T] = Field(description = "Items") - page: int = Field(description = "Current Page") - pages: int = Field(description = "Total number of pages") - per_page: int = Field(description = "Number of items per page") - total: int = Field(description = "Total number of items in result") + + items: list[T] = Field(description="Items") + page: int = Field(description="Current Page") + pages: int = Field(description="Total number of pages") + per_page: int = Field(description="Number of items per page") + total: int = Field(description="Total number of items in result") class ItemStorageABC(ABC, Generic[T]): @@ -24,6 +26,7 @@ class ItemStorageABC(ABC, Generic[T]): self._on_deleted_callbacks = list() """Base item storage class""" + @abstractmethod def get(self, item_id: str) -> T: pass @@ -37,7 +40,9 @@ class ItemStorageABC(ABC, Generic[T]): pass @abstractmethod - def search(self, query: str, page: int = 0, per_page: int = 10) -> PaginatedResults[T]: + def search( + self, query: str, page: int = 0, per_page: int = 10 + ) -> PaginatedResults[T]: pass def on_changed(self, on_changed: Callable[[T], None]) -> None: @@ -51,7 +56,7 @@ class ItemStorageABC(ABC, Generic[T]): def _on_changed(self, item: T) -> None: for callback in self._on_changed_callbacks: callback(item) - + def _on_deleted(self, item_id: str) -> None: for callback in self._on_deleted_callbacks: callback(item_id) diff --git a/invokeai/app/services/processor.py b/invokeai/app/services/processor.py index 1825d404e5..5baa64503c 100644 --- a/invokeai/app/services/processor.py +++ b/invokeai/app/services/processor.py @@ -1,5 +1,6 @@ -from threading import Event, Thread import traceback +from threading import Event, Thread + from ..invocations.baseinvocation import InvocationContext from .invocation_queue import InvocationQueueItem from .invoker import InvocationProcessorABC, Invoker @@ -14,52 +15,62 @@ class DefaultInvocationProcessor(InvocationProcessorABC): self.__invoker = invoker self.__stop_event = Event() self.__invoker_thread = Thread( - name = "invoker_processor", - target = self.__process, - kwargs = dict(stop_event = self.__stop_event) + name="invoker_processor", + target=self.__process, + kwargs=dict(stop_event=self.__stop_event), + ) + self.__invoker_thread.daemon = ( + True # TODO: probably better to just not use threads? ) - self.__invoker_thread.daemon = True # TODO: probably better to just not use threads? self.__invoker_thread.start() - def stop(self, *args, **kwargs) -> None: self.__stop_event.set() - def __process(self, stop_event: Event): try: while not stop_event.is_set(): queue_item: InvocationQueueItem = self.__invoker.services.queue.get() - if not queue_item: # Probably stopping + if not queue_item: # Probably stopping continue - graph_execution_state = self.__invoker.services.graph_execution_manager.get(queue_item.graph_execution_state_id) - invocation = graph_execution_state.execution_graph.get_node(queue_item.invocation_id) + graph_execution_state = ( + self.__invoker.services.graph_execution_manager.get( + queue_item.graph_execution_state_id + ) + ) + invocation = graph_execution_state.execution_graph.get_node( + queue_item.invocation_id + ) # Send starting event self.__invoker.services.events.emit_invocation_started( - graph_execution_state_id = graph_execution_state.id, - invocation_id = invocation.id + graph_execution_state_id=graph_execution_state.id, + invocation_id=invocation.id, ) # Invoke try: - outputs = invocation.invoke(InvocationContext( - services = self.__invoker.services, - graph_execution_state_id = graph_execution_state.id - )) + outputs = invocation.invoke( + InvocationContext( + services=self.__invoker.services, + graph_execution_state_id=graph_execution_state.id, + ) + ) # Save outputs and history graph_execution_state.complete(invocation.id, outputs) # Save the state changes - self.__invoker.services.graph_execution_manager.set(graph_execution_state) + self.__invoker.services.graph_execution_manager.set( + graph_execution_state + ) # Send complete event self.__invoker.services.events.emit_invocation_complete( - graph_execution_state_id = graph_execution_state.id, - invocation_id = invocation.id, - result = outputs.dict() + graph_execution_state_id=graph_execution_state.id, + invocation_id=invocation.id, + result=outputs.dict(), ) except KeyboardInterrupt: @@ -72,24 +83,27 @@ class DefaultInvocationProcessor(InvocationProcessorABC): graph_execution_state.set_node_error(invocation.id, error) # Save the state changes - self.__invoker.services.graph_execution_manager.set(graph_execution_state) + self.__invoker.services.graph_execution_manager.set( + graph_execution_state + ) # Send error event self.__invoker.services.events.emit_invocation_error( - graph_execution_state_id = graph_execution_state.id, - invocation_id = invocation.id, - error = error - ) + graph_execution_state_id=graph_execution_state.id, + invocation_id=invocation.id, + error=error, + ) pass - # Queue any further commands if invoking all is_complete = graph_execution_state.is_complete() if queue_item.invoke_all and not is_complete: - self.__invoker.invoke(graph_execution_state, invoke_all = True) + self.__invoker.invoke(graph_execution_state, invoke_all=True) elif is_complete: - self.__invoker.services.events.emit_graph_execution_complete(graph_execution_state.id) + self.__invoker.services.events.emit_graph_execution_complete( + graph_execution_state.id + ) except KeyboardInterrupt: - ... # Log something? + ... # Log something? diff --git a/invokeai/app/services/sqlite.py b/invokeai/app/services/sqlite.py index 8858bbd874..e5bba4ad31 100644 --- a/invokeai/app/services/sqlite.py +++ b/invokeai/app/services/sqlite.py @@ -1,12 +1,15 @@ import sqlite3 from threading import Lock from typing import Generic, TypeVar, Union, get_args + from pydantic import BaseModel, parse_raw_as + from .item_storage import ItemStorageABC, PaginatedResults -T = TypeVar('T', bound=BaseModel) +T = TypeVar("T", bound=BaseModel) + +sqlite_memory = ":memory:" -sqlite_memory = ':memory:' class SqliteItemStorage(ItemStorageABC, Generic[T]): _filename: str @@ -16,15 +19,17 @@ class SqliteItemStorage(ItemStorageABC, Generic[T]): _id_field: str _lock: Lock - def __init__(self, filename: str, table_name: str, id_field: str = 'id'): + def __init__(self, filename: str, table_name: str, id_field: str = "id"): super().__init__() self._filename = filename self._table_name = table_name - self._id_field = id_field # TODO: validate that T has this field + self._id_field = id_field # TODO: validate that T has this field self._lock = Lock() - self._conn = sqlite3.connect(self._filename, check_same_thread=False) # TODO: figure out a better threading solution + self._conn = sqlite3.connect( + self._filename, check_same_thread=False + ) # TODO: figure out a better threading solution self._cursor = self._conn.cursor() self._create_table() @@ -32,10 +37,14 @@ class SqliteItemStorage(ItemStorageABC, Generic[T]): def _create_table(self): try: self._lock.acquire() - self._cursor.execute(f'''CREATE TABLE IF NOT EXISTS {self._table_name} ( + self._cursor.execute( + f"""CREATE TABLE IF NOT EXISTS {self._table_name} ( item TEXT, - id TEXT GENERATED ALWAYS AS (json_extract(item, '$.{self._id_field}')) VIRTUAL NOT NULL);''') - self._cursor.execute(f'''CREATE UNIQUE INDEX IF NOT EXISTS {self._table_name}_id ON {self._table_name}(id);''') + id TEXT GENERATED ALWAYS AS (json_extract(item, '$.{self._id_field}')) VIRTUAL NOT NULL);""" + ) + self._cursor.execute( + f"""CREATE UNIQUE INDEX IF NOT EXISTS {self._table_name}_id ON {self._table_name}(id);""" + ) finally: self._lock.release() @@ -46,7 +55,10 @@ class SqliteItemStorage(ItemStorageABC, Generic[T]): def set(self, item: T): try: self._lock.acquire() - self._cursor.execute(f'''INSERT OR REPLACE INTO {self._table_name} (item) VALUES (?);''', (item.json(),)) + self._cursor.execute( + f"""INSERT OR REPLACE INTO {self._table_name} (item) VALUES (?);""", + (item.json(),), + ) finally: self._lock.release() self._on_changed(item) @@ -54,7 +66,9 @@ class SqliteItemStorage(ItemStorageABC, Generic[T]): def get(self, id: str) -> Union[T, None]: try: self._lock.acquire() - self._cursor.execute(f'''SELECT item FROM {self._table_name} WHERE id = ?;''', (str(id),)) + self._cursor.execute( + f"""SELECT item FROM {self._table_name} WHERE id = ?;""", (str(id),) + ) result = self._cursor.fetchone() finally: self._lock.release() @@ -67,7 +81,9 @@ class SqliteItemStorage(ItemStorageABC, Generic[T]): def delete(self, id: str): try: self._lock.acquire() - self._cursor.execute(f'''DELETE FROM {self._table_name} WHERE id = ?;''', (str(id),)) + self._cursor.execute( + f"""DELETE FROM {self._table_name} WHERE id = ?;""", (str(id),) + ) finally: self._lock.release() self._on_deleted(id) @@ -75,12 +91,15 @@ class SqliteItemStorage(ItemStorageABC, Generic[T]): def list(self, page: int = 0, per_page: int = 10) -> PaginatedResults[T]: try: self._lock.acquire() - self._cursor.execute(f'''SELECT item FROM {self._table_name} LIMIT ? OFFSET ?;''', (per_page, page * per_page)) + self._cursor.execute( + f"""SELECT item FROM {self._table_name} LIMIT ? OFFSET ?;""", + (per_page, page * per_page), + ) result = self._cursor.fetchall() items = list(map(lambda r: self._parse_item(r[0]), result)) - self._cursor.execute(f'''SELECT count(*) FROM {self._table_name};''') + self._cursor.execute(f"""SELECT count(*) FROM {self._table_name};""") count = self._cursor.fetchone()[0] finally: self._lock.release() @@ -88,22 +107,26 @@ class SqliteItemStorage(ItemStorageABC, Generic[T]): pageCount = int(count / per_page) + 1 return PaginatedResults[T]( - items = items, - page = page, - pages = pageCount, - per_page = per_page, - total = count + items=items, page=page, pages=pageCount, per_page=per_page, total=count ) - - def search(self, query: str, page: int = 0, per_page: int = 10) -> PaginatedResults[T]: + + def search( + self, query: str, page: int = 0, per_page: int = 10 + ) -> PaginatedResults[T]: try: self._lock.acquire() - self._cursor.execute(f'''SELECT item FROM {self._table_name} WHERE item LIKE ? LIMIT ? OFFSET ?;''', (f'%{query}%', per_page, page * per_page)) + self._cursor.execute( + f"""SELECT item FROM {self._table_name} WHERE item LIKE ? LIMIT ? OFFSET ?;""", + (f"%{query}%", per_page, page * per_page), + ) result = self._cursor.fetchall() items = list(map(lambda r: self._parse_item(r[0]), result)) - self._cursor.execute(f'''SELECT count(*) FROM {self._table_name} WHERE item LIKE ?;''', (f'%{query}%',)) + self._cursor.execute( + f"""SELECT count(*) FROM {self._table_name} WHERE item LIKE ?;""", + (f"%{query}%",), + ) count = self._cursor.fetchone()[0] finally: self._lock.release() @@ -111,9 +134,5 @@ class SqliteItemStorage(ItemStorageABC, Generic[T]): pageCount = int(count / per_page) + 1 return PaginatedResults[T]( - items = items, - page = page, - pages = pageCount, - per_page = per_page, - total = count + items=items, page=page, pages=pageCount, per_page=per_page, total=count ) diff --git a/invokeai/backend/__init__.py b/invokeai/backend/__init__.py index 7bdc6e7270..9062fb7d52 100644 --- a/invokeai/backend/__init__.py +++ b/invokeai/backend/__init__.py @@ -1,8 +1,5 @@ -''' +""" Initialization file for invokeai.backend -''' -from .model_management import ModelManager +""" from .generate import Generate - - - +from .model_management import ModelManager diff --git a/invokeai/backend/args.py b/invokeai/backend/args.py index 7de6b35edb..b961658fde 100644 --- a/invokeai/backend/args.py +++ b/invokeai/backend/args.py @@ -95,8 +95,8 @@ from argparse import Namespace from pathlib import Path from typing import List -from invokeai.backend.image_util import retrieve_metadata import invokeai.version +from invokeai.backend.image_util import retrieve_metadata from .globals import Globals from .prompting import split_weighted_subprompts @@ -106,32 +106,33 @@ APP_NAME = invokeai.version.__app_name__ APP_VERSION = invokeai.version.__version__ SAMPLER_CHOICES = [ - 'ddim', - 'k_dpm_2_a', - 'k_dpm_2', - 'k_dpmpp_2_a', - 'k_dpmpp_2', - 'k_euler_a', - 'k_euler', - 'k_heun', - 'k_lms', - 'plms', + "ddim", + "k_dpm_2_a", + "k_dpm_2", + "k_dpmpp_2_a", + "k_dpmpp_2", + "k_euler_a", + "k_euler", + "k_heun", + "k_lms", + "plms", # diffusers: "pndm", ] PRECISION_CHOICES = [ - 'auto', - 'float32', - 'autocast', - 'float16', + "auto", + "float32", + "autocast", + "float16", ] + class ArgFormatter(argparse.RawTextHelpFormatter): - # use defined argument order to display usage + # use defined argument order to display usage def _format_usage(self, usage, actions, groups, prefix): if prefix is None: - prefix = 'usage: ' + prefix = "usage: " # if usage is specified, use that if usage is not None: @@ -139,109 +140,121 @@ class ArgFormatter(argparse.RawTextHelpFormatter): # if no optionals or positionals are available, usage is just prog elif usage is None and not actions: - usage = 'invoke>' + usage = "invoke>" elif usage is None: - prog='invoke>' + prog = "invoke>" # build full usage string - action_usage = self._format_actions_usage(actions, groups) # NEW - usage = ' '.join([s for s in [prog, action_usage] if s]) + action_usage = self._format_actions_usage(actions, groups) # NEW + usage = " ".join([s for s in [prog, action_usage] if s]) # omit the long line wrapping code # prefix with 'usage:' - return '%s%s\n\n' % (prefix, usage) + return "%s%s\n\n" % (prefix, usage) + class PagingArgumentParser(argparse.ArgumentParser): - ''' + """ A custom ArgumentParser that uses pydoc to page its output. It also supports reading defaults from an init file. - ''' + """ + def print_help(self, file=None): text = self.format_help() pydoc.pager(text) def convert_arg_line_to_args(self, arg_line): - return shlex.split(arg_line,comments=True) + return shlex.split(arg_line, comments=True) + class Args(object): - def __init__(self,arg_parser=None,cmd_parser=None): - ''' + def __init__(self, arg_parser=None, cmd_parser=None): + """ Initialize new Args class. It takes two optional arguments, an argparse parser for switches given on the shell command line, and an argparse parser for switches given on the invoke> CLI line. If one or both are missing, it creates appropriate parsers internally. - ''' - self._arg_parser = arg_parser or self._create_arg_parser() - self._cmd_parser = cmd_parser or self._create_dream_cmd_parser() - self._arg_switches = self.parse_cmd('') # fill in defaults - self._cmd_switches = self.parse_cmd('') # fill in defaults + """ + self._arg_parser = arg_parser or self._create_arg_parser() + self._cmd_parser = cmd_parser or self._create_dream_cmd_parser() + self._arg_switches = self.parse_cmd("") # fill in defaults + self._cmd_switches = self.parse_cmd("") # fill in defaults - def parse_args(self, args: List[str]=None): - '''Parse the shell switches and store.''' + def parse_args(self, args: List[str] = None): + """Parse the shell switches and store.""" sysargs = args if args is not None else sys.argv[1:] try: # pre-parse before we do any initialization to get root directory # and intercept --version request switches = self._arg_parser.parse_args(sysargs) if switches.version: - print(f'{APP_NAME} {APP_VERSION}') + print(f"{APP_NAME} {APP_VERSION}") sys.exit(0) - print('* Initializing, be patient...') + print("* Initializing, be patient...") Globals.root = Path(os.path.abspath(switches.root_dir or Globals.root)) Globals.try_patchmatch = switches.patchmatch # now use root directory to find the init file - initfile = os.path.expanduser(os.path.join(Globals.root,Globals.initfile)) - legacyinit = os.path.expanduser('~/.invokeai') + initfile = os.path.expanduser(os.path.join(Globals.root, Globals.initfile)) + legacyinit = os.path.expanduser("~/.invokeai") if os.path.exists(initfile): - print(f'>> Initialization file {initfile} found. Loading...',file=sys.stderr) - sysargs.insert(0,f'@{initfile}') + print( + f">> Initialization file {initfile} found. Loading...", + file=sys.stderr, + ) + sysargs.insert(0, f"@{initfile}") elif os.path.exists(legacyinit): - print(f'>> WARNING: Old initialization file found at {legacyinit}. This location is deprecated. Please move it to {Globals.root}/invokeai.init.') - sysargs.insert(0,f'@{legacyinit}') - Globals.log_tokenization = self._arg_parser.parse_args(sysargs).log_tokenization + print( + f">> WARNING: Old initialization file found at {legacyinit}. This location is deprecated. Please move it to {Globals.root}/invokeai.init." + ) + sysargs.insert(0, f"@{legacyinit}") + Globals.log_tokenization = self._arg_parser.parse_args( + sysargs + ).log_tokenization self._arg_switches = self._arg_parser.parse_args(sysargs) return self._arg_switches except Exception as e: - print(f'An exception has occurred: {e}') + print(f"An exception has occurred: {e}") return None - def parse_cmd(self,cmd_string): - '''Parse a invoke>-style command string ''' + def parse_cmd(self, cmd_string): + """Parse a invoke>-style command string""" # handle the case in which the first token is a switch - if cmd_string.startswith('-'): - prompt = '' + if cmd_string.startswith("-"): + prompt = "" switches = cmd_string # handle the case in which the prompt is enclosed by quotes elif cmd_string.startswith('"'): - a = shlex.split(cmd_string,comments=True) + a = shlex.split(cmd_string, comments=True) prompt = a[0] switches = shlex.join(a[1:]) else: # no initial quote, so get everything up to the first thing # that looks like a switch - if cmd_string.startswith('-'): - prompt = '' + if cmd_string.startswith("-"): + prompt = "" switches = cmd_string else: - match = re.match('^(.+?)\s(--?[a-zA-Z].+)',cmd_string) + match = re.match("^(.+?)\s(--?[a-zA-Z].+)", cmd_string) if match: - prompt,switches = match.groups() + prompt, switches = match.groups() else: prompt = cmd_string - switches = '' + switches = "" try: - self._cmd_switches = self._cmd_parser.parse_args(shlex.split(switches,comments=True)) - if not getattr(self._cmd_switches,'prompt'): - setattr(self._cmd_switches,'prompt',prompt) + self._cmd_switches = self._cmd_parser.parse_args( + shlex.split(switches, comments=True) + ) + if not getattr(self._cmd_switches, "prompt"): + setattr(self._cmd_switches, "prompt", prompt) return self._cmd_switches except: return None - def json(self,**kwargs): + def json(self, **kwargs): return json.dumps(self.to_dict(**kwargs)) - def to_dict(self,**kwargs): + def to_dict(self, **kwargs): a = vars(self) a.update(kwargs) return a @@ -249,125 +262,129 @@ class Args(object): # Isn't there a more automated way of doing this? # Ideally we get the switch strings out of the argparse objects, # but I don't see a documented API for this. - def dream_prompt_str(self,**kwargs): + def dream_prompt_str(self, **kwargs): """Normalized dream_prompt.""" a = vars(self) a.update(kwargs) switches = list() - prompt = a['prompt'] - prompt.replace('"','\\"') + prompt = a["prompt"] + prompt.replace('"', '\\"') switches.append(prompt) switches.append(f'-s {a["steps"]}') switches.append(f'-S {a["seed"]}') switches.append(f'-W {a["width"]}') switches.append(f'-H {a["height"]}') switches.append(f'-C {a["cfg_scale"]}') - if a['karras_max'] is not None: + if a["karras_max"] is not None: switches.append(f'--karras_max {a["karras_max"]}') - if a['perlin'] > 0: + if a["perlin"] > 0: switches.append(f'--perlin {a["perlin"]}') - if a['threshold'] > 0: + if a["threshold"] > 0: switches.append(f'--threshold {a["threshold"]}') - if a['grid']: - switches.append('--grid') - if a['seamless']: - switches.append('--seamless') - if a['hires_fix']: - switches.append('--hires_fix') - if a['h_symmetry_time_pct']: + if a["grid"]: + switches.append("--grid") + if a["seamless"]: + switches.append("--seamless") + if a["hires_fix"]: + switches.append("--hires_fix") + if a["h_symmetry_time_pct"]: switches.append(f'--h_symmetry_time_pct {a["h_symmetry_time_pct"]}') - if a['v_symmetry_time_pct']: + if a["v_symmetry_time_pct"]: switches.append(f'--v_symmetry_time_pct {a["v_symmetry_time_pct"]}') # img2img generations have parameters relevant only to them and have special handling - if a['init_img'] and len(a['init_img'])>0: + if a["init_img"] and len(a["init_img"]) > 0: switches.append(f'-I {a["init_img"]}') switches.append(f'-A {a["sampler_name"]}') - if a['fit']: - switches.append('--fit') - if a['init_mask'] and len(a['init_mask'])>0: + if a["fit"]: + switches.append("--fit") + if a["init_mask"] and len(a["init_mask"]) > 0: switches.append(f'-M {a["init_mask"]}') - if a['init_color'] and len(a['init_color'])>0: + if a["init_color"] and len(a["init_color"]) > 0: switches.append(f'--init_color {a["init_color"]}') - if a['strength'] and a['strength']>0: + if a["strength"] and a["strength"] > 0: switches.append(f'-f {a["strength"]}') - if a['inpaint_replace']: - switches.append('--inpaint_replace') - if a['text_mask']: + if a["inpaint_replace"]: + switches.append("--inpaint_replace") + if a["text_mask"]: switches.append(f'-tm {" ".join([str(u) for u in a["text_mask"]])}') else: switches.append(f'-A {a["sampler_name"]}') # facetool-specific parameters, only print if running facetool - if a['facetool_strength']: + if a["facetool_strength"]: switches.append(f'-G {a["facetool_strength"]}') switches.append(f'-ft {a["facetool"]}') if a["facetool"] == "codeformer": switches.append(f'-cf {a["codeformer_fidelity"]}') - if a['outcrop']: + if a["outcrop"]: switches.append(f'-c {" ".join([str(u) for u in a["outcrop"]])}') # esrgan-specific parameters - if a['upscale']: + if a["upscale"]: switches.append(f'-U {" ".join([str(u) for u in a["upscale"]])}') # embiggen parameters - if a['embiggen']: + if a["embiggen"]: switches.append(f'--embiggen {" ".join([str(u) for u in a["embiggen"]])}') - if a['embiggen_tiles']: - switches.append(f'--embiggen_tiles {" ".join([str(u) for u in a["embiggen_tiles"]])}') - if a['embiggen_strength']: + if a["embiggen_tiles"]: + switches.append( + f'--embiggen_tiles {" ".join([str(u) for u in a["embiggen_tiles"]])}' + ) + if a["embiggen_strength"]: switches.append(f'--embiggen_strength {a["embiggen_strength"]}') # outpainting parameters - if a['out_direction']: + if a["out_direction"]: switches.append(f'-D {" ".join([str(u) for u in a["out_direction"]])}') # LS: slight semantic drift which needs addressing in the future: # 1. Variations come out of the stored metadata as a packed string with the keyword "variations" # 2. However, they come out of the CLI (and probably web) with the keyword "with_variations" and # in broken-out form. Variation (1) should be changed to comply with (2) - if a['with_variations'] and len(a['with_variations'])>0: - formatted_variations = ','.join(f'{seed}:{weight}' for seed, weight in (a["with_variations"])) - switches.append(f'-V {formatted_variations}') - if 'variations' in a and len(a['variations'])>0: + if a["with_variations"] and len(a["with_variations"]) > 0: + formatted_variations = ",".join( + f"{seed}:{weight}" for seed, weight in (a["with_variations"]) + ) + switches.append(f"-V {formatted_variations}") + if "variations" in a and len(a["variations"]) > 0: switches.append(f'-V {a["variations"]}') - return ' '.join(switches) + return " ".join(switches) - def __getattribute__(self,name): - ''' + def __getattribute__(self, name): + """ Returns union of command-line arguments and dream_prompt arguments, with the latter superseding the former. - ''' + """ cmd_switches = None arg_switches = None try: - cmd_switches = object.__getattribute__(self,'_cmd_switches') - arg_switches = object.__getattribute__(self,'_arg_switches') + cmd_switches = object.__getattribute__(self, "_cmd_switches") + arg_switches = object.__getattribute__(self, "_arg_switches") except AttributeError: pass - if cmd_switches and arg_switches and name=='__dict__': + if cmd_switches and arg_switches and name == "__dict__": return self._merge_dict( arg_switches.__dict__, cmd_switches.__dict__, ) try: - return object.__getattribute__(self,name) + return object.__getattribute__(self, name) except AttributeError: pass - if not hasattr(cmd_switches,name) and not hasattr(arg_switches,name): + if not hasattr(cmd_switches, name) and not hasattr(arg_switches, name): raise AttributeError - value_arg,value_cmd = (None,None) + value_arg, value_cmd = (None, None) try: - value_cmd = getattr(cmd_switches,name) + value_cmd = getattr(cmd_switches, name) except AttributeError: pass try: - value_arg = getattr(arg_switches,name) + value_arg = getattr(arg_switches, name) except AttributeError: pass @@ -375,46 +392,46 @@ class Args(object): # default behavior is to choose the dream_command value over # the arg value. For example, the --grid and --individual options are a little # funny because of their push/pull relationship. This is how to handle it. - if name=='grid': + if name == "grid": if cmd_switches.individual: return False else: return value_cmd or value_arg return value_cmd if value_cmd is not None else value_arg - def __setattr__(self,name,value): - if name.startswith('_'): - object.__setattr__(self,name,value) + def __setattr__(self, name, value): + if name.startswith("_"): + object.__setattr__(self, name, value) else: self._cmd_switches.__dict__[name] = value - def _merge_dict(self,dict1,dict2): - new_dict = {} - for k in set(list(dict1.keys())+list(dict2.keys())): - value1 = dict1.get(k,None) - value2 = dict2.get(k,None) + def _merge_dict(self, dict1, dict2): + new_dict = {} + for k in set(list(dict1.keys()) + list(dict2.keys())): + value1 = dict1.get(k, None) + value2 = dict2.get(k, None) new_dict[k] = value2 if value2 is not None else value1 return new_dict - def _create_init_file(self,initfile:str): - with open(initfile, mode='w', encoding='utf-8') as f: - f.write('''# InvokeAI initialization file + def _create_init_file(self, initfile: str): + with open(initfile, mode="w", encoding="utf-8") as f: + f.write( + """# InvokeAI initialization file # Put frequently-used startup commands here, one or more per line # Examples: # --web --host=0.0.0.0 # --steps 20 # -Ak_euler_a -C10.0 -''' +""" ) def _create_arg_parser(self): - ''' + """ This defines all the arguments used on the command line when you launch the CLI or web backend. - ''' + """ parser = PagingArgumentParser( - description= - """ + description=""" Generate images using Stable Diffusion. Use --web to launch the web interface. Use --from_file to load prompts from a file path or standard input ("-"). @@ -422,298 +439,293 @@ class Args(object): Other command-line arguments are defaults that can usually be overridden prompt the command prompt. """, - fromfile_prefix_chars='@', + fromfile_prefix_chars="@", ) - general_group = parser.add_argument_group('General') - model_group = parser.add_argument_group('Model selection') - file_group = parser.add_argument_group('Input/output') - web_server_group = parser.add_argument_group('Web server') - render_group = parser.add_argument_group('Rendering') - postprocessing_group = parser.add_argument_group('Postprocessing') - deprecated_group = parser.add_argument_group('Deprecated options') + general_group = parser.add_argument_group("General") + model_group = parser.add_argument_group("Model selection") + file_group = parser.add_argument_group("Input/output") + web_server_group = parser.add_argument_group("Web server") + render_group = parser.add_argument_group("Rendering") + postprocessing_group = parser.add_argument_group("Postprocessing") + deprecated_group = parser.add_argument_group("Deprecated options") - deprecated_group.add_argument('--laion400m') - deprecated_group.add_argument('--weights') # deprecated + deprecated_group.add_argument("--laion400m") + deprecated_group.add_argument("--weights") # deprecated deprecated_group.add_argument( - '--ckpt_convert', + "--ckpt_convert", action=argparse.BooleanOptionalAction, - dest='ckpt_convert', + dest="ckpt_convert", default=True, - help='Load legacy ckpt files as diffusers (deprecated; always true now).', + help="Load legacy ckpt files as diffusers (deprecated; always true now).", ) general_group.add_argument( - '--version','-V', - action='store_true', - help='Print InvokeAI version number' + "--version", "-V", action="store_true", help="Print InvokeAI version number" ) model_group.add_argument( - '--root_dir', + "--root_dir", default=None, help='Path to directory containing "models", "outputs" and "configs". If not present will read from environment variable INVOKEAI_ROOT. Defaults to ~/invokeai.', ) model_group.add_argument( - '--config', - '-c', - '-config', - dest='conf', - default='./configs/models.yaml', - help='Path to configuration file for alternate models.', + "--config", + "-c", + "-config", + dest="conf", + default="./configs/models.yaml", + help="Path to configuration file for alternate models.", ) model_group.add_argument( - '--model', + "--model", help='Indicates which diffusion model to load (defaults to "default" stanza in configs/models.yaml)', ) model_group.add_argument( - '--weight_dirs', - nargs='+', + "--weight_dirs", + nargs="+", type=str, - help='List of one or more directories that will be auto-scanned for new model weights to import', + help="List of one or more directories that will be auto-scanned for new model weights to import", ) model_group.add_argument( - '--png_compression','-z', + "--png_compression", + "-z", type=int, default=6, - choices=range(0,9), - dest='png_compression', - help='level of PNG compression, from 0 (none) to 9 (maximum). Default is 6.' + choices=range(0, 9), + dest="png_compression", + help="level of PNG compression, from 0 (none) to 9 (maximum). Default is 6.", ) model_group.add_argument( - '-F', - '--full_precision', - dest='full_precision', - action='store_true', - help='Deprecated way to set --precision=float32', + "-F", + "--full_precision", + dest="full_precision", + action="store_true", + help="Deprecated way to set --precision=float32", ) model_group.add_argument( - '--max_loaded_models', - dest='max_loaded_models', + "--max_loaded_models", + dest="max_loaded_models", type=int, default=2, - help='Maximum number of models to keep in memory for fast switching, including the one in GPU', + help="Maximum number of models to keep in memory for fast switching, including the one in GPU", ) model_group.add_argument( - '--free_gpu_mem', - dest='free_gpu_mem', - action='store_true', - help='Force free gpu memory before final decoding', + "--free_gpu_mem", + dest="free_gpu_mem", + action="store_true", + help="Force free gpu memory before final decoding", ) model_group.add_argument( - '--sequential_guidance', - dest='sequential_guidance', - action='store_true', + "--sequential_guidance", + dest="sequential_guidance", + action="store_true", help="Calculate guidance in serial instead of in parallel, lowering memory requirement " - "at the expense of speed", + "at the expense of speed", ) model_group.add_argument( - '--xformers', + "--xformers", action=argparse.BooleanOptionalAction, default=True, - help='Enable/disable xformers support (default enabled if installed)', + help="Enable/disable xformers support (default enabled if installed)", ) model_group.add_argument( "--always_use_cpu", dest="always_use_cpu", action="store_true", - help="Force use of CPU even if GPU is available" + help="Force use of CPU even if GPU is available", ) model_group.add_argument( - '--precision', - dest='precision', + "--precision", + dest="precision", type=str, choices=PRECISION_CHOICES, - metavar='PRECISION', + metavar="PRECISION", help=f'Set model precision. Defaults to auto selected based on device. Options: {", ".join(PRECISION_CHOICES)}', - default='auto', + default="auto", ) model_group.add_argument( - '--internet', + "--internet", action=argparse.BooleanOptionalAction, - dest='internet_available', + dest="internet_available", default=True, - help='Indicate whether internet is available for just-in-time model downloading (default: probe automatically).', + help="Indicate whether internet is available for just-in-time model downloading (default: probe automatically).", ) model_group.add_argument( - '--nsfw_checker', - '--safety_checker', + "--nsfw_checker", + "--safety_checker", action=argparse.BooleanOptionalAction, - dest='safety_checker', + dest="safety_checker", default=False, - help='Check for and blur potentially NSFW images. Use --no-nsfw_checker to disable.', + help="Check for and blur potentially NSFW images. Use --no-nsfw_checker to disable.", ) model_group.add_argument( - '--autoimport', + "--autoimport", default=None, type=str, - help='Check the indicated directory for .ckpt/.safetensors weights files at startup and import directly', + help="Check the indicated directory for .ckpt/.safetensors weights files at startup and import directly", ) model_group.add_argument( - '--autoconvert', + "--autoconvert", default=None, type=str, - help='Check the indicated directory for .ckpt/.safetensors weights files at startup and import as optimized diffuser models', + help="Check the indicated directory for .ckpt/.safetensors weights files at startup and import as optimized diffuser models", ) model_group.add_argument( - '--patchmatch', + "--patchmatch", action=argparse.BooleanOptionalAction, default=True, - help='Load the patchmatch extension for outpainting. Use --no-patchmatch to disable.', + help="Load the patchmatch extension for outpainting. Use --no-patchmatch to disable.", ) file_group.add_argument( - '--from_file', - dest='infile', + "--from_file", + dest="infile", type=str, - help='If specified, load prompts from this file', + help="If specified, load prompts from this file", ) file_group.add_argument( - '--outdir', - '-o', + "--outdir", + "-o", type=str, - help='Directory to save generated images and a log of prompts and seeds. Default: ROOTDIR/outputs', - default='outputs', + help="Directory to save generated images and a log of prompts and seeds. Default: ROOTDIR/outputs", + default="outputs", ) file_group.add_argument( - '--prompt_as_dir', - '-p', - action='store_true', - help='Place images in subdirectories named after the prompt.', + "--prompt_as_dir", + "-p", + action="store_true", + help="Place images in subdirectories named after the prompt.", ) render_group.add_argument( - '--fnformat', - default='{prefix}.{seed}.png', + "--fnformat", + default="{prefix}.{seed}.png", type=str, - help='Overwrite the filename format. You can use any argument as wildcard enclosed in curly braces. Default is {prefix}.{seed}.png', + help="Overwrite the filename format. You can use any argument as wildcard enclosed in curly braces. Default is {prefix}.{seed}.png", ) render_group.add_argument( - '-s', - '--steps', + "-s", "--steps", type=int, default=50, help="Number of steps" + ) + render_group.add_argument( + "-W", + "--width", type=int, - default=50, - help='Number of steps' + help="Image width, multiple of 64", ) render_group.add_argument( - '-W', - '--width', + "-H", + "--height", type=int, - help='Image width, multiple of 64', + help="Image height, multiple of 64", ) render_group.add_argument( - '-H', - '--height', - type=int, - help='Image height, multiple of 64', - ) - render_group.add_argument( - '-C', - '--cfg_scale', + "-C", + "--cfg_scale", default=7.5, type=float, help='Classifier free guidance (CFG) scale - higher numbers cause generator to "try" harder.', ) render_group.add_argument( - '--sampler', - '-A', - '-m', - dest='sampler_name', + "--sampler", + "-A", + "-m", + dest="sampler_name", type=str, choices=SAMPLER_CHOICES, - metavar='SAMPLER_NAME', + metavar="SAMPLER_NAME", help=f'Set the default sampler. Supported samplers: {", ".join(SAMPLER_CHOICES)}', - default='k_lms', + default="k_lms", ) render_group.add_argument( - '--log_tokenization', - '-t', - action='store_true', - help='shows how the prompt is split into tokens' + "--log_tokenization", + "-t", + action="store_true", + help="shows how the prompt is split into tokens", ) render_group.add_argument( - '-f', - '--strength', + "-f", + "--strength", type=float, - help='img2img strength for noising/unnoising. 0.0 preserves image exactly, 1.0 replaces it completely', + help="img2img strength for noising/unnoising. 0.0 preserves image exactly, 1.0 replaces it completely", ) render_group.add_argument( - '-T', - '-fit', - '--fit', + "-T", + "-fit", + "--fit", action=argparse.BooleanOptionalAction, - help='If specified, will resize the input image to fit within the dimensions of width x height (512x512 default)', + help="If specified, will resize the input image to fit within the dimensions of width x height (512x512 default)", ) render_group.add_argument( - '--grid', - '-g', + "--grid", + "-g", action=argparse.BooleanOptionalAction, - help='generate a grid' + help="generate a grid", ) render_group.add_argument( - '--embedding_directory', - '--embedding_path', - dest='embedding_path', - default='embeddings', + "--embedding_directory", + "--embedding_path", + dest="embedding_path", + default="embeddings", type=str, - help='Path to a directory containing .bin and/or .pt files, or a single .bin/.pt file. You may use subdirectories. (default is ROOTDIR/embeddings)' + help="Path to a directory containing .bin and/or .pt files, or a single .bin/.pt file. You may use subdirectories. (default is ROOTDIR/embeddings)", ) render_group.add_argument( - '--embeddings', + "--embeddings", action=argparse.BooleanOptionalAction, default=True, - help='Enable embedding directory (default). Use --no-embeddings to disable.', + help="Enable embedding directory (default). Use --no-embeddings to disable.", ) render_group.add_argument( - '--enable_image_debugging', - action='store_true', - help='Generates debugging image to display' + "--enable_image_debugging", + action="store_true", + help="Generates debugging image to display", ) render_group.add_argument( - '--karras_max', + "--karras_max", type=int, default=None, - help="control the point at which the K* samplers will shift from using the Karras noise schedule (good for low step counts) to the LatentDiffusion noise schedule (good for high step counts). Set to 0 to use LatentDiffusion for all step values, and to a high value (e.g. 1000) to use Karras for all step values. [29]." + help="control the point at which the K* samplers will shift from using the Karras noise schedule (good for low step counts) to the LatentDiffusion noise schedule (good for high step counts). Set to 0 to use LatentDiffusion for all step values, and to a high value (e.g. 1000) to use Karras for all step values. [29].", ) # Restoration related args postprocessing_group.add_argument( - '--no_restore', - dest='restore', - action='store_false', - help='Disable face restoration with GFPGAN or codeformer', + "--no_restore", + dest="restore", + action="store_false", + help="Disable face restoration with GFPGAN or codeformer", ) postprocessing_group.add_argument( - '--no_upscale', - dest='esrgan', - action='store_false', - help='Disable upscaling with ESRGAN', + "--no_upscale", + dest="esrgan", + action="store_false", + help="Disable upscaling with ESRGAN", ) postprocessing_group.add_argument( - '--esrgan_bg_tile', + "--esrgan_bg_tile", type=int, default=400, - help='Tile size for background sampler, 0 for no tile during testing. Default: 400.', + help="Tile size for background sampler, 0 for no tile during testing. Default: 400.", ) postprocessing_group.add_argument( - '--esrgan_denoise_str', + "--esrgan_denoise_str", type=float, default=0.75, - help='esrgan denoise str. 0 is no denoise, 1 is max denoise. Default: 0.75', + help="esrgan denoise str. 0 is no denoise, 1 is max denoise. Default: 0.75", ) postprocessing_group.add_argument( - '--gfpgan_model_path', + "--gfpgan_model_path", type=str, - default='./models/gfpgan/GFPGANv1.4.pth', - help='Indicates the path to the GFPGAN model', + default="./models/gfpgan/GFPGANv1.4.pth", + help="Indicates the path to the GFPGAN model", ) web_server_group.add_argument( - '--web', - dest='web', - action='store_true', - help='Start in web server mode.', + "--web", + dest="web", + action="store_true", + help="Start in web server mode.", ) web_server_group.add_argument( - '--web_develop', - dest='web_develop', - action='store_true', - help='Start in web server development mode.', + "--web_develop", + dest="web_develop", + action="store_true", + help="Start in web server development mode.", ) web_server_group.add_argument( "--web_verbose", @@ -727,34 +739,31 @@ class Args(object): help="Additional allowed origins, comma-separated", ) web_server_group.add_argument( - '--host', + "--host", type=str, - default='127.0.0.1', - help='Web server: Host or IP to listen on. Set to 0.0.0.0 to accept traffic from other devices on your network.' + default="127.0.0.1", + help="Web server: Host or IP to listen on. Set to 0.0.0.0 to accept traffic from other devices on your network.", ) web_server_group.add_argument( - '--port', - type=int, - default='9090', - help='Web server: Port to listen on' + "--port", type=int, default="9090", help="Web server: Port to listen on" ) web_server_group.add_argument( - '--certfile', + "--certfile", type=str, default=None, - help='Web server: Path to certificate file to use for SSL. Use together with --keyfile' + help="Web server: Path to certificate file to use for SSL. Use together with --keyfile", ) web_server_group.add_argument( - '--keyfile', + "--keyfile", type=str, default=None, - help='Web server: Path to private key file to use for SSL. Use together with --certfile' + help="Web server: Path to private key file to use for SSL. Use together with --certfile", ) web_server_group.add_argument( - '--gui', - dest='gui', - action='store_true', - help='Start InvokeAI GUI', + "--gui", + dest="gui", + action="store_true", + help="Start InvokeAI GUI", ) return parser @@ -762,8 +771,7 @@ class Args(object): def _create_dream_cmd_parser(self): parser = PagingArgumentParser( formatter_class=ArgFormatter, - description= - """ + description=""" *Image generation* invoke> a fantastic alien landscape -W576 -H512 -s60 -n4 @@ -800,517 +808,549 @@ class Args(object): !convert_model /path/to/weights/file.ckpt -- converts a .ckpt file path to a diffusers model !edit_model -- edit a model's description !del_model -- delete a model - """ + """, ) - render_group = parser.add_argument_group('General rendering') - img2img_group = parser.add_argument_group('Image-to-image and inpainting') - inpainting_group = parser.add_argument_group('Inpainting') - outpainting_group = parser.add_argument_group('Outpainting and outcropping') - variation_group = parser.add_argument_group('Creating and combining variations') - postprocessing_group = parser.add_argument_group('Post-processing') - special_effects_group = parser.add_argument_group('Special effects') - deprecated_group = parser.add_argument_group('Deprecated options') + render_group = parser.add_argument_group("General rendering") + img2img_group = parser.add_argument_group("Image-to-image and inpainting") + inpainting_group = parser.add_argument_group("Inpainting") + outpainting_group = parser.add_argument_group("Outpainting and outcropping") + variation_group = parser.add_argument_group("Creating and combining variations") + postprocessing_group = parser.add_argument_group("Post-processing") + special_effects_group = parser.add_argument_group("Special effects") + deprecated_group = parser.add_argument_group("Deprecated options") render_group.add_argument( - '--prompt', - default='', - help='prompt string', + "--prompt", + default="", + help="prompt string", ) + render_group.add_argument("-s", "--steps", type=int, help="Number of steps") render_group.add_argument( - '-s', - '--steps', - type=int, - help='Number of steps' - ) - render_group.add_argument( - '-S', - '--seed', + "-S", + "--seed", type=int, default=None, - help='Image seed; a +ve integer, or use -1 for the previous seed, -2 for the one before that, etc', + help="Image seed; a +ve integer, or use -1 for the previous seed, -2 for the one before that, etc", ) render_group.add_argument( - '-n', - '--iterations', + "-n", + "--iterations", type=int, default=1, - help='Number of samplings to perform (slower, but will provide seeds for individual images)', + help="Number of samplings to perform (slower, but will provide seeds for individual images)", ) render_group.add_argument( - '-W', - '--width', + "-W", + "--width", type=int, - help='Image width, multiple of 64', + help="Image width, multiple of 64", ) render_group.add_argument( - '-H', - '--height', + "-H", + "--height", type=int, - help='Image height, multiple of 64', + help="Image height, multiple of 64", ) render_group.add_argument( - '-C', - '--cfg_scale', + "-C", + "--cfg_scale", type=float, help='Classifier free guidance (CFG) scale - higher numbers cause generator to "try" harder.', ) render_group.add_argument( - '--threshold', + "--threshold", default=0.0, type=float, help='Latent threshold for classifier free guidance (CFG) - prevent generator from "trying" too hard. Use positive values, 0 disables.', ) render_group.add_argument( - '--perlin', + "--perlin", default=0.0, type=float, - help='Perlin noise scale (0.0 - 1.0) - add perlin noise to the initialization instead of the usual gaussian noise.', + help="Perlin noise scale (0.0 - 1.0) - add perlin noise to the initialization instead of the usual gaussian noise.", ) render_group.add_argument( - '--h_symmetry_time_pct', + "--h_symmetry_time_pct", default=None, type=float, - help='Horizontal symmetry point (0.0 - 1.0) - apply horizontal symmetry at this point in image generation.', + help="Horizontal symmetry point (0.0 - 1.0) - apply horizontal symmetry at this point in image generation.", ) render_group.add_argument( - '--v_symmetry_time_pct', + "--v_symmetry_time_pct", default=None, type=float, - help='Vertical symmetry point (0.0 - 1.0) - apply vertical symmetry at this point in image generation.', + help="Vertical symmetry point (0.0 - 1.0) - apply vertical symmetry at this point in image generation.", ) render_group.add_argument( - '--fnformat', - default='{prefix}.{seed}.png', + "--fnformat", + default="{prefix}.{seed}.png", type=str, - help='Overwrite the filename format. You can use any argument as wildcard enclosed in curly braces. Default is {prefix}.{seed}.png', + help="Overwrite the filename format. You can use any argument as wildcard enclosed in curly braces. Default is {prefix}.{seed}.png", ) render_group.add_argument( - '--grid', - '-g', + "--grid", + "-g", action=argparse.BooleanOptionalAction, - help='generate a grid' + help="generate a grid", ) render_group.add_argument( - '-i', - '--individual', - action='store_true', - help='override command-line --grid setting and generate individual images' + "-i", + "--individual", + action="store_true", + help="override command-line --grid setting and generate individual images", ) render_group.add_argument( - '-x', - '--skip_normalize', - action='store_true', - help='Skip subprompt weight normalization', + "-x", + "--skip_normalize", + action="store_true", + help="Skip subprompt weight normalization", ) render_group.add_argument( - '-A', - '-m', - '--sampler', - dest='sampler_name', + "-A", + "-m", + "--sampler", + dest="sampler_name", type=str, choices=SAMPLER_CHOICES, - metavar='SAMPLER_NAME', + metavar="SAMPLER_NAME", help=f'Switch to a different sampler. Supported samplers: {", ".join(SAMPLER_CHOICES)}', ) render_group.add_argument( - '-t', - '--log_tokenization', - action='store_true', - help='shows how the prompt is split into tokens' + "-t", + "--log_tokenization", + action="store_true", + help="shows how the prompt is split into tokens", ) render_group.add_argument( - '--outdir', - '-o', + "--outdir", + "-o", type=str, - help='Directory to save generated images and a log of prompts and seeds', + help="Directory to save generated images and a log of prompts and seeds", ) render_group.add_argument( - '--hires_fix', - action='store_true', - dest='hires_fix', - help='Create hires image using img2img to prevent duplicated objects' + "--hires_fix", + action="store_true", + dest="hires_fix", + help="Create hires image using img2img to prevent duplicated objects", ) render_group.add_argument( - '--save_intermediates', + "--save_intermediates", type=int, default=0, - dest='save_intermediates', - help='Save every nth intermediate image into an "intermediates" directory within the output directory' + dest="save_intermediates", + help='Save every nth intermediate image into an "intermediates" directory within the output directory', ) render_group.add_argument( - '--png_compression','-z', + "--png_compression", + "-z", type=int, default=6, - choices=range(0,10), - dest='png_compression', - help='level of PNG compression, from 0 (none) to 9 (maximum). [6]' + choices=range(0, 10), + dest="png_compression", + help="level of PNG compression, from 0 (none) to 9 (maximum). [6]", ) render_group.add_argument( - '--karras_max', + "--karras_max", type=int, default=None, - help="control the point at which the K* samplers will shift from using the Karras noise schedule (good for low step counts) to the LatentDiffusion noise schedule (good for high step counts). Set to 0 to use LatentDiffusion for all step values, and to a high value (e.g. 1000) to use Karras for all step values. [29]." + help="control the point at which the K* samplers will shift from using the Karras noise schedule (good for low step counts) to the LatentDiffusion noise schedule (good for high step counts). Set to 0 to use LatentDiffusion for all step values, and to a high value (e.g. 1000) to use Karras for all step values. [29].", ) img2img_group.add_argument( - '-I', - '--init_img', + "-I", + "--init_img", type=str, - help='Path to input image for img2img mode (supersedes width and height)', + help="Path to input image for img2img mode (supersedes width and height)", ) img2img_group.add_argument( - '-tm', - '--text_mask', - nargs='+', + "-tm", + "--text_mask", + nargs="+", type=str, help='Use the clipseg classifier to generate the mask area for inpainting. Provide a description of the area to mask ("a mug"), optionally followed by the confidence level threshold (0-1.0; defaults to 0.5).', default=None, ) img2img_group.add_argument( - '--init_color', + "--init_color", type=str, - help='Path to reference image for color correction (used for repeated img2img and inpainting)' + help="Path to reference image for color correction (used for repeated img2img and inpainting)", ) img2img_group.add_argument( - '-T', - '-fit', - '--fit', - action='store_true', - help='If specified, will resize the input image to fit within the dimensions of width x height (512x512 default)', + "-T", + "-fit", + "--fit", + action="store_true", + help="If specified, will resize the input image to fit within the dimensions of width x height (512x512 default)", ) img2img_group.add_argument( - '-f', - '--strength', + "-f", + "--strength", type=float, - help='img2img strength for noising/unnoising. 0.0 preserves image exactly, 1.0 replaces it completely', + help="img2img strength for noising/unnoising. 0.0 preserves image exactly, 1.0 replaces it completely", ) inpainting_group.add_argument( - '-M', - '--init_mask', + "-M", + "--init_mask", type=str, - help='Path to input mask for inpainting mode (supersedes width and height)', + help="Path to input mask for inpainting mode (supersedes width and height)", ) inpainting_group.add_argument( - '--invert_mask', - action='store_true', - help='Invert the mask', + "--invert_mask", + action="store_true", + help="Invert the mask", ) inpainting_group.add_argument( - '-r', - '--inpaint_replace', + "-r", + "--inpaint_replace", type=float, default=0.0, - help='when inpainting, adjust how aggressively to replace the part of the picture under the mask, from 0.0 (a gentle merge) to 1.0 (replace entirely)', + help="when inpainting, adjust how aggressively to replace the part of the picture under the mask, from 0.0 (a gentle merge) to 1.0 (replace entirely)", ) outpainting_group.add_argument( - '-c', - '--outcrop', - nargs='+', + "-c", + "--outcrop", + nargs="+", type=str, - metavar=('direction','pixels'), - help='Outcrop the image with one or more direction/pixel pairs: e.g. -c top 64 bottom 128 left 64 right 64', + metavar=("direction", "pixels"), + help="Outcrop the image with one or more direction/pixel pairs: e.g. -c top 64 bottom 128 left 64 right 64", ) outpainting_group.add_argument( - '--force_outpaint', - action='store_true', + "--force_outpaint", + action="store_true", default=False, - help='Force outpainting if you have no inpainting mask to pass', + help="Force outpainting if you have no inpainting mask to pass", ) outpainting_group.add_argument( - '--seam_size', + "--seam_size", type=int, default=0, - help='When outpainting, size of the mask around the seam between original and outpainted image', + help="When outpainting, size of the mask around the seam between original and outpainted image", ) outpainting_group.add_argument( - '--seam_blur', + "--seam_blur", type=int, default=0, - help='When outpainting, the amount to blur the seam inwards', + help="When outpainting, the amount to blur the seam inwards", ) outpainting_group.add_argument( - '--seam_strength', + "--seam_strength", type=float, default=0.7, - help='When outpainting, the img2img strength to use when filling the seam. Values around 0.7 work well', + help="When outpainting, the img2img strength to use when filling the seam. Values around 0.7 work well", ) outpainting_group.add_argument( - '--seam_steps', + "--seam_steps", type=int, default=10, - help='When outpainting, the number of steps to use to fill the seam. Low values (~10) work well', + help="When outpainting, the number of steps to use to fill the seam. Low values (~10) work well", ) outpainting_group.add_argument( - '--tile_size', + "--tile_size", type=int, default=32, - help='When outpainting, the tile size to use for filling outpaint areas', + help="When outpainting, the tile size to use for filling outpaint areas", ) postprocessing_group.add_argument( - '--new_prompt', + "--new_prompt", type=str, - help='Change the text prompt applied during postprocessing (default, use original generation prompt)', + help="Change the text prompt applied during postprocessing (default, use original generation prompt)", ) postprocessing_group.add_argument( - '-ft', - '--facetool', + "-ft", + "--facetool", type=str, - default='gfpgan', - help='Select the face restoration AI to use: gfpgan, codeformer', + default="gfpgan", + help="Select the face restoration AI to use: gfpgan, codeformer", ) postprocessing_group.add_argument( - '-G', - '--facetool_strength', - '--gfpgan_strength', + "-G", + "--facetool_strength", + "--gfpgan_strength", type=float, - help='The strength at which to apply the face restoration to the result.', + help="The strength at which to apply the face restoration to the result.", default=0.0, ) postprocessing_group.add_argument( - '-cf', - '--codeformer_fidelity', + "-cf", + "--codeformer_fidelity", type=float, - help='Used along with CodeFormer. Takes values between 0 and 1. 0 produces high quality but low accuracy. 1 produces high accuracy but low quality.', - default=0.75 + help="Used along with CodeFormer. Takes values between 0 and 1. 0 produces high quality but low accuracy. 1 produces high accuracy but low quality.", + default=0.75, ) postprocessing_group.add_argument( - '-U', - '--upscale', - nargs='+', + "-U", + "--upscale", + nargs="+", type=float, - help='Scale factor (1, 2, 3, 4, etc..) for upscaling final output followed by upscaling strength (0-1.0). If strength not specified, defaults to 0.75', + help="Scale factor (1, 2, 3, 4, etc..) for upscaling final output followed by upscaling strength (0-1.0). If strength not specified, defaults to 0.75", default=None, ) postprocessing_group.add_argument( - '--save_original', - '-save_orig', - action='store_true', - help='Save original. Use it when upscaling to save both versions.', + "--save_original", + "-save_orig", + action="store_true", + help="Save original. Use it when upscaling to save both versions.", ) postprocessing_group.add_argument( - '--embiggen', - '-embiggen', - nargs='+', + "--embiggen", + "-embiggen", + nargs="+", type=float, - help='Arbitrary upscaling using img2img. Provide scale factor (0.75), optionally followed by strength (0.75) and tile overlap proportion (0.25).', + help="Arbitrary upscaling using img2img. Provide scale factor (0.75), optionally followed by strength (0.75) and tile overlap proportion (0.25).", default=None, ) postprocessing_group.add_argument( - '--embiggen_tiles', - '-embiggen_tiles', - nargs='+', + "--embiggen_tiles", + "-embiggen_tiles", + nargs="+", type=int, - help='For embiggen, provide list of tiles to process and replace onto the image e.g. `1 3 5`.', + help="For embiggen, provide list of tiles to process and replace onto the image e.g. `1 3 5`.", default=None, ) postprocessing_group.add_argument( - '--embiggen_strength', - '-embiggen_strength', + "--embiggen_strength", + "-embiggen_strength", type=float, - help='The strength of the embiggen img2img step, defaults to 0.4', + help="The strength of the embiggen img2img step, defaults to 0.4", default=None, ) special_effects_group.add_argument( - '--seamless', - action='store_true', - help='Change the model to seamless tiling (circular) mode', + "--seamless", + action="store_true", + help="Change the model to seamless tiling (circular) mode", ) special_effects_group.add_argument( - '--seamless_axes', - default=['x', 'y'], + "--seamless_axes", + default=["x", "y"], type=list[str], - help='Specify which axes to use circular convolution on.', + help="Specify which axes to use circular convolution on.", ) variation_group.add_argument( - '-v', - '--variation_amount', + "-v", + "--variation_amount", default=0.0, type=float, - help='If > 0, generates variations on the initial seed instead of random seeds per iteration. Must be between 0 and 1. Higher values will be more different.' + help="If > 0, generates variations on the initial seed instead of random seeds per iteration. Must be between 0 and 1. Higher values will be more different.", ) variation_group.add_argument( - '-V', - '--with_variations', + "-V", + "--with_variations", default=None, type=str, - help='list of variations to apply, in the format `seed:weight,seed:weight,...' + help="list of variations to apply, in the format `seed:weight,seed:weight,...", ) render_group.add_argument( - '--use_mps_noise', - action='store_true', - dest='use_mps_noise', - help='Simulate noise on M1 systems to get the same results' + "--use_mps_noise", + action="store_true", + dest="use_mps_noise", + help="Simulate noise on M1 systems to get the same results", ) deprecated_group.add_argument( - '-D', - '--out_direction', - nargs='+', + "-D", + "--out_direction", + nargs="+", type=str, - metavar=('direction', 'pixels'), - help='Older outcropping system. Direction to extend the given image (left|right|top|bottom). If a distance pixel value is not specified it defaults to half the image size' + metavar=("direction", "pixels"), + help="Older outcropping system. Direction to extend the given image (left|right|top|bottom). If a distance pixel value is not specified it defaults to half the image size", ) return parser + def format_metadata(**kwargs): - print('format_metadata() is deprecated. Please use metadata_dumps()') + print("format_metadata() is deprecated. Please use metadata_dumps()") return metadata_dumps(kwargs) -def metadata_dumps(opt, - seeds=[], - model_hash=None, - postprocessing=None): - ''' + +def metadata_dumps(opt, seeds=[], model_hash=None, postprocessing=None): + """ Given an Args object, returns a dict containing the keys and structure of the proposed stable diffusion metadata standard https://github.com/lstein/stable-diffusion/discussions/392 This is intended to be turned into JSON and stored in the "sd - ''' + """ # top-level metadata minus `image` or `images` metadata = { - 'model' : 'stable diffusion', - 'model_id' : opt.model, - 'model_hash' : model_hash, - 'app_id' : APP_ID, - 'app_version' : APP_VERSION, + "model": "stable diffusion", + "model_id": opt.model, + "model_hash": model_hash, + "app_id": APP_ID, + "app_version": APP_VERSION, } # # add some RFC266 fields that are generated internally, and not as # # user args - image_dict = opt.to_dict( - postprocessing=postprocessing - ) + image_dict = opt.to_dict(postprocessing=postprocessing) # remove any image keys not mentioned in RFC #266 - rfc266_img_fields = ['type','postprocessing','sampler','prompt','seed','variations','steps', - 'cfg_scale','threshold','perlin','step_number','width','height','extra','strength','seamless' - 'init_img','init_mask','facetool','facetool_strength','upscale','h_symmetry_time_pct', - 'v_symmetry_time_pct'] - rfc_dict ={} + rfc266_img_fields = [ + "type", + "postprocessing", + "sampler", + "prompt", + "seed", + "variations", + "steps", + "cfg_scale", + "threshold", + "perlin", + "step_number", + "width", + "height", + "extra", + "strength", + "seamless" "init_img", + "init_mask", + "facetool", + "facetool_strength", + "upscale", + "h_symmetry_time_pct", + "v_symmetry_time_pct", + ] + rfc_dict = {} for item in image_dict.items(): - key,value = item + key, value = item if key in rfc266_img_fields: rfc_dict[key] = value # semantic drift - rfc_dict['sampler'] = image_dict.get('sampler_name',None) + rfc_dict["sampler"] = image_dict.get("sampler_name", None) # display weighted subprompts (liable to change) if opt.prompt: subprompts = split_weighted_subprompts(opt.prompt) - subprompts = [{'prompt':x[0],'weight':x[1]} for x in subprompts] - rfc_dict['prompt'] = subprompts + subprompts = [{"prompt": x[0], "weight": x[1]} for x in subprompts] + rfc_dict["prompt"] = subprompts # 'variations' should always exist and be an array, empty or consisting of {'seed': seed, 'weight': weight} pairs - rfc_dict['variations'] = [{'seed':x[0],'weight':x[1]} for x in opt.with_variations] if opt.with_variations else [] + rfc_dict["variations"] = ( + [{"seed": x[0], "weight": x[1]} for x in opt.with_variations] + if opt.with_variations + else [] + ) # if variations are present then we need to replace 'seed' with 'orig_seed' - if hasattr(opt,'first_seed'): - rfc_dict['seed'] = opt.first_seed + if hasattr(opt, "first_seed"): + rfc_dict["seed"] = opt.first_seed if opt.init_img: - rfc_dict['type'] = 'img2img' - rfc_dict['strength_steps'] = rfc_dict.pop('strength') - rfc_dict['orig_hash'] = calculate_init_img_hash(opt.init_img) - rfc_dict['inpaint_replace'] = opt.inpaint_replace + rfc_dict["type"] = "img2img" + rfc_dict["strength_steps"] = rfc_dict.pop("strength") + rfc_dict["orig_hash"] = calculate_init_img_hash(opt.init_img) + rfc_dict["inpaint_replace"] = opt.inpaint_replace else: - rfc_dict['type'] = 'txt2img' - rfc_dict.pop('strength') + rfc_dict["type"] = "txt2img" + rfc_dict.pop("strength") - if len(seeds)==0 and opt.seed: - seeds=[opt.seed] + if len(seeds) == 0 and opt.seed: + seeds = [opt.seed] if opt.grid: images = [] for seed in seeds: - rfc_dict['seed'] = seed + rfc_dict["seed"] = seed images.append(copy.copy(rfc_dict)) - metadata['images'] = images + metadata["images"] = images else: # there should only ever be a single seed if we did not generate a grid - assert len(seeds) == 1, 'Expected a single seed' - rfc_dict['seed'] = seeds[0] - metadata['image'] = rfc_dict + assert len(seeds) == 1, "Expected a single seed" + rfc_dict["seed"] = seeds[0] + metadata["image"] = rfc_dict return metadata + @functools.lru_cache(maxsize=50) def args_from_png(png_file_path) -> list[Args]: - ''' + """ Given the path to a PNG file created by invoke.py, retrieves a list of Args objects containing the image data. - ''' + """ try: meta = retrieve_metadata(png_file_path) except AttributeError: - return [legacy_metadata_load({},png_file_path)] + return [legacy_metadata_load({}, png_file_path)] try: return metadata_loads(meta) except: - return [legacy_metadata_load(meta,png_file_path)] + return [legacy_metadata_load(meta, png_file_path)] + @functools.lru_cache(maxsize=50) def metadata_from_png(png_file_path) -> Args: - ''' + """ Given the path to a PNG file created by dream.py, retrieves an Args object containing the image metadata. Note that this returns a single Args object, not multiple. - ''' + """ args_list = args_from_png(png_file_path) - return args_list[0] if len(args_list)>0 else Args() # empty args + return args_list[0] if len(args_list) > 0 else Args() # empty args + def dream_cmd_from_png(png_file_path): opt = metadata_from_png(png_file_path) return opt.dream_prompt_str() + def metadata_loads(metadata) -> list: - ''' + """ Takes the dictionary corresponding to RFC266 (https://github.com/lstein/stable-diffusion/issues/266) and returns a series of opt objects for each of the images described in the dictionary. Note that this returns a list, and not a single object. See metadata_from_png() for a more convenient function for files that contain a single image. - ''' + """ results = [] try: - if 'images' in metadata['sd-metadata']: - images = metadata['sd-metadata']['images'] + if "images" in metadata["sd-metadata"]: + images = metadata["sd-metadata"]["images"] else: - images = [metadata['sd-metadata']['image']] + images = [metadata["sd-metadata"]["image"]] for image in images: # repack the prompt and variations - if 'prompt' in image: - image['prompt'] = repack_prompt(image['prompt']) - if 'variations' in image: - image['variations'] = ','.join([':'.join([str(x['seed']),str(x['weight'])]) for x in image['variations']]) + if "prompt" in image: + image["prompt"] = repack_prompt(image["prompt"]) + if "variations" in image: + image["variations"] = ",".join( + [ + ":".join([str(x["seed"]), str(x["weight"])]) + for x in image["variations"] + ] + ) # fix a bit of semantic drift here - image['sampler_name']=image.pop('sampler') + image["sampler_name"] = image.pop("sampler") opt = Args() opt._cmd_switches = Namespace(**image) results.append(opt) except Exception: - import sys, traceback - print('>> could not read metadata',file=sys.stderr) + import sys + import traceback + + print(">> could not read metadata", file=sys.stderr) print(traceback.format_exc(), file=sys.stderr) return results -def repack_prompt(prompt_list:list)->str: + +def repack_prompt(prompt_list: list) -> str: # in the common case of no weighting syntax, just return the prompt as is if len(prompt_list) > 1: - return ','.join([':'.join([x['prompt'], str(x['weight'])]) for x in prompt_list]) + return ",".join( + [":".join([x["prompt"], str(x["weight"])]) for x in prompt_list] + ) else: - return prompt_list[0]['prompt'] + return prompt_list[0]["prompt"] + # image can either be a file path on disk or a base64-encoded # representation of the file's contents def calculate_init_img_hash(image_string): - prefix = 'data:image/png;base64,' - hash = None + prefix = "data:image/png;base64," + hash = None if image_string.startswith(prefix): - imagebase64 = image_string[len(prefix):] - imagedata = base64.b64decode(imagebase64) - with open('outputs/test.png','wb') as file: + imagebase64 = image_string[len(prefix) :] + imagedata = base64.b64decode(imagebase64) + with open("outputs/test.png", "wb") as file: file.write(imagedata) sha = hashlib.sha256() sha.update(imagedata) @@ -1319,10 +1359,11 @@ def calculate_init_img_hash(image_string): hash = sha256(image_string) return hash + # Bah. This should be moved somewhere else... def sha256(path): sha = hashlib.sha256() - with open(path,'rb') as f: + with open(path, "rb") as f: while True: data = f.read(65536) if not data: @@ -1330,18 +1371,18 @@ def sha256(path): sha.update(data) return sha.hexdigest() -def legacy_metadata_load(meta,pathname) -> Args: + +def legacy_metadata_load(meta, pathname) -> Args: opt = Args() - if 'Dream' in meta and len(meta['Dream']) > 0: - dream_prompt = meta['Dream'] + if "Dream" in meta and len(meta["Dream"]) > 0: + dream_prompt = meta["Dream"] opt.parse_cmd(dream_prompt) - else: # if nothing else, we can get the seed - match = re.search('\d+\.(\d+)',pathname) + else: # if nothing else, we can get the seed + match = re.search("\d+\.(\d+)", pathname) if match: seed = match.groups()[0] opt.seed = seed else: - opt.prompt = '' + opt.prompt = "" opt.seed = 0 return opt - diff --git a/invokeai/backend/config/invokeai_configure.py b/invokeai/backend/config/invokeai_configure.py index 9b1cc5affa..6f0a218dc1 100755 --- a/invokeai/backend/config/invokeai_configure.py +++ b/invokeai/backend/config/invokeai_configure.py @@ -17,8 +17,8 @@ import traceback import warnings from argparse import Namespace from pathlib import Path -from urllib import request from shutil import get_terminal_size +from urllib import request import npyscreen import torch @@ -37,17 +37,20 @@ from transformers import ( import invokeai.configs as configs +from ...frontend.install.model_install import addModelsForm, process_and_execute +from ...frontend.install.widgets import ( + CenteredButtonPress, + IntTitleSlider, + set_min_terminal_size, +) from ..args import PRECISION_CHOICES, Args -from ..globals import Globals, global_config_dir, global_config_file, global_cache_dir -from ...frontend.config.model_install import addModelsForm, process_and_execute +from ..globals import Globals, global_cache_dir, global_config_dir, global_config_file from .model_install_backend import ( default_dataset, download_from_hf, - recommended_datasets, hf_download_with_resume, + recommended_datasets, ) -from ...frontend.config.widgets import IntTitleSlider, CenteredButtonPress, set_min_terminal_size - warnings.filterwarnings("ignore") @@ -82,6 +85,7 @@ INIT_FILE_PREAMBLE = """# InvokeAI initialization file # -Ak_euler_a -C10.0 """ + # -------------------------------------------- def postscript(errors: None): if not any(errors): @@ -180,13 +184,11 @@ def download_with_progress_bar(model_url: str, model_dest: str, label: str = "th # --------------------------------------------- # this will preload the Bert tokenizer fles def download_bert(): - print( - "Installing bert tokenizer...", - file=sys.stderr - ) + print("Installing bert tokenizer...", file=sys.stderr) with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) from transformers import BertTokenizerFast + download_from_hf(BertTokenizerFast, "bert-base-uncased") @@ -197,12 +199,14 @@ def download_sd1_clip(): download_from_hf(CLIPTokenizer, version) download_from_hf(CLIPTextModel, version) + # --------------------------------------------- def download_sd2_clip(): - version = 'stabilityai/stable-diffusion-2' + version = "stabilityai/stable-diffusion-2" print("Installing SD2 clip model...", file=sys.stderr) - download_from_hf(CLIPTokenizer, version, subfolder='tokenizer') - download_from_hf(CLIPTextModel, version, subfolder='text_encoder') + download_from_hf(CLIPTokenizer, version, subfolder="tokenizer") + download_from_hf(CLIPTextModel, version, subfolder="text_encoder") + # --------------------------------------------- def download_realesrgan(): @@ -323,13 +327,13 @@ def get_root(root: str = None) -> str: class editOptsForm(npyscreen.FormMultiPage): # for responsive resizing - disabled # FIX_MINIMUM_SIZE_WHEN_CREATED = False - + def create(self): program_opts = self.parentApp.program_opts old_opts = self.parentApp.invokeai_opts first_time = not (Globals.root / Globals.initfile).exists() access_token = HfFolder.get_token() - window_width,window_height = get_terminal_size() + window_width, window_height = get_terminal_size() for i in [ "Configure startup settings. You can come back and change these later.", "Use ctrl-N and ctrl-P to move to the ext and

revious fields.", @@ -681,6 +685,7 @@ def run_console_ui( else: return (editApp.new_opts, editApp.user_selections) + # ------------------------------------- def write_opts(opts: Namespace, init_file: Path): """ @@ -701,8 +706,8 @@ def write_opts(opts: Namespace, init_file: Path): "^--?(o|out|no-xformer|xformer|no-ckpt|ckpt|free|no-nsfw|nsfw|prec|max_load|embed|always|ckpt|free_gpu)" ) # fix windows paths - opts.outdir = opts.outdir.replace('\\','/') - opts.embedding_path = opts.embedding_path.replace('\\','/') + opts.outdir = opts.outdir.replace("\\", "/") + opts.embedding_path = opts.embedding_path.replace("\\", "/") new_file = f"{init_file}.new" try: lines = [x.strip() for x in open(init_file, "r").readlines()] @@ -855,6 +860,7 @@ def main(): except KeyboardInterrupt: print("\nGoodbye! Come back soon.") + # ------------------------------------- if __name__ == "__main__": main() diff --git a/invokeai/backend/config/model_install_backend.py b/invokeai/backend/config/model_install_backend.py index 6157c2b2b4..1180991d06 100644 --- a/invokeai/backend/config/model_install_backend.py +++ b/invokeai/backend/config/model_install_backend.py @@ -8,6 +8,7 @@ import sys import warnings from pathlib import Path from tempfile import TemporaryFile +from typing import List import requests from diffusers import AutoencoderKL @@ -15,12 +16,12 @@ from huggingface_hub import hf_hub_url from omegaconf import OmegaConf from omegaconf.dictconfig import DictConfig from tqdm import tqdm -from typing import List import invokeai.configs as configs -from ..stable_diffusion import StableDiffusionGeneratorPipeline + from ..globals import Globals, global_cache_dir, global_config_dir from ..model_management import ModelManager +from ..stable_diffusion import StableDiffusionGeneratorPipeline warnings.filterwarnings("ignore") @@ -44,45 +45,49 @@ Config_preamble = """ # was trained on. """ + def default_config_file(): return Path(global_config_dir()) / "models.yaml" + def sd_configs(): return Path(global_config_dir()) / "stable-diffusion" + def initial_models(): global Datasets if Datasets: return Datasets return (Datasets := OmegaConf.load(Dataset_path)) + def install_requested_models( - install_initial_models: List[str] = None, - remove_models: List[str] = None, - scan_directory: Path = None, - external_models: List[str] = None, - scan_at_startup: bool = False, - convert_to_diffusers: bool = False, - precision: str = "float16", - purge_deleted: bool = False, - config_file_path: Path = None, + install_initial_models: List[str] = None, + remove_models: List[str] = None, + scan_directory: Path = None, + external_models: List[str] = None, + scan_at_startup: bool = False, + convert_to_diffusers: bool = False, + precision: str = "float16", + purge_deleted: bool = False, + config_file_path: Path = None, ): - ''' + """ Entry point for installing/deleting starter models, or installing external models. - ''' - config_file_path=config_file_path or default_config_file() + """ + config_file_path = config_file_path or default_config_file() if not config_file_path.exists(): - open(config_file_path,'w') - - model_manager= ModelManager(OmegaConf.load(config_file_path),precision=precision) - + open(config_file_path, "w") + + model_manager = ModelManager(OmegaConf.load(config_file_path), precision=precision) + if remove_models and len(remove_models) > 0: print("== DELETING UNCHECKED STARTER MODELS ==") for model in remove_models: - print(f'{model}...') + print(f"{model}...") model_manager.del_model(model, delete_files=purge_deleted) model_manager.commit(config_file_path) - + if install_initial_models and len(install_initial_models) > 0: print("== INSTALLING SELECTED STARTER MODELS ==") successfully_downloaded = download_weight_datasets( @@ -96,20 +101,20 @@ def install_requested_models( # due to above, we have to reload the model manager because conf file # was changed behind its back - model_manager= ModelManager(OmegaConf.load(config_file_path),precision=precision) + model_manager = ModelManager(OmegaConf.load(config_file_path), precision=precision) external_models = external_models or list() if scan_directory: external_models.append(str(scan_directory)) - if len(external_models)>0: + if len(external_models) > 0: print("== INSTALLING EXTERNAL MODELS ==") for path_url_or_repo in external_models: try: model_manager.heuristic_import( path_url_or_repo, convert=convert_to_diffusers, - commit_to_conf=config_file_path + commit_to_conf=config_file_path, ) except KeyboardInterrupt: sys.exit(-1) @@ -117,17 +122,18 @@ def install_requested_models( pass if scan_at_startup and scan_directory.is_dir(): - argument = '--autoconvert' if convert_to_diffusers else '--autoimport' + argument = "--autoconvert" if convert_to_diffusers else "--autoimport" initfile = Path(Globals.root, Globals.initfile) - replacement = Path(Globals.root, f'{Globals.initfile}.new') - directory = str(scan_directory).replace('\\','/') - with open(initfile,'r') as input: - with open(replacement,'w') as output: + replacement = Path(Globals.root, f"{Globals.initfile}.new") + directory = str(scan_directory).replace("\\", "/") + with open(initfile, "r") as input: + with open(replacement, "w") as output: while line := input.readline(): if not line.startswith(argument): output.writelines([line]) - output.writelines([f'{argument} {directory}']) - os.replace(replacement,initfile) + output.writelines([f"{argument} {directory}"]) + os.replace(replacement, initfile) + # ------------------------------------- def yes_or_no(prompt: str, default_yes=True): @@ -183,7 +189,9 @@ def migrate_models_ckpt(): if not os.path.exists(os.path.join(model_path, "model.ckpt")): return new_name = initial_models()["stable-diffusion-1.4"]["file"] - print('The Stable Diffusion v4.1 "model.ckpt" is already installed. The name will be changed to {new_name} to avoid confusion.') + print( + 'The Stable Diffusion v4.1 "model.ckpt" is already installed. The name will be changed to {new_name} to avoid confusion.' + ) print(f"model.ckpt => {new_name}") os.replace( os.path.join(model_path, "model.ckpt"), os.path.join(model_path, new_name) @@ -383,7 +391,8 @@ def update_config_file(successfully_downloaded: dict, config_file: Path): # --------------------------------------------- def new_config_file_contents( - successfully_downloaded: dict, config_file: Path, + successfully_downloaded: dict, + config_file: Path, ) -> str: if config_file.exists(): conf = OmegaConf.load(str(config_file.expanduser().resolve())) @@ -413,7 +422,9 @@ def new_config_file_contents( stanza["weights"] = os.path.relpath( successfully_downloaded[model], start=Globals.root ) - stanza["config"] = os.path.normpath(os.path.join(sd_configs(), mod["config"])) + stanza["config"] = os.path.normpath( + os.path.join(sd_configs(), mod["config"]) + ) if "vae" in mod: if "file" in mod["vae"]: stanza["vae"] = os.path.normpath( @@ -445,7 +456,7 @@ def delete_weights(model_name: str, conf_stanza: dict): print( f"\n** The checkpoint version of {model_name} is superseded by the diffusers version. Deleting the original file {weights}?" ) - + weights = Path(weights) if not weights.is_absolute(): weights = Path(Globals.root) / weights diff --git a/invokeai/backend/generate.py b/invokeai/backend/generate.py index 329e1b50bd..8f2992db0c 100644 --- a/invokeai/backend/generate.py +++ b/invokeai/backend/generate.py @@ -25,18 +25,20 @@ from omegaconf import OmegaConf from PIL import Image, ImageOps from pytorch_lightning import logging, seed_everything -from . import ModelManager -from .prompting import get_uc_and_c_and_ec -from .stable_diffusion import (DDIMSampler, KSampler, PLMSSampler, HuggingFaceConceptsLibrary) -from .generator import infill_methods -from .util import choose_precision, choose_torch_device -from .image_util import (InitImageResizer, - PngWriter, - Txt2Mask, - configure_model_padding) - -from .globals import Globals, global_cache_dir +from .model_management import ModelManager from .args import metadata_from_png +from .generator import infill_methods +from .globals import Globals, global_cache_dir +from .image_util import InitImageResizer, PngWriter, Txt2Mask, configure_model_padding +from .prompting import get_uc_and_c_and_ec +from .stable_diffusion import ( + DDIMSampler, + HuggingFaceConceptsLibrary, + KSampler, + PLMSSampler, +) +from .util import choose_precision, choose_torch_device + def fix_func(orig): if hasattr(torch.backends, "mps") and torch.backends.mps.is_available(): @@ -324,8 +326,8 @@ class Generate: variation_amount=0.0, threshold=0.0, perlin=0.0, - h_symmetry_time_pct = None, - v_symmetry_time_pct = None, + h_symmetry_time_pct=None, + v_symmetry_time_pct=None, karras_max=None, outdir=None, # these are specific to img2img and inpaint @@ -713,7 +715,7 @@ class Generate: prompt, model=self.model, skip_normalize_legacy_blend=opt.skip_normalize, - log_tokens=ldm.invoke.conditioning.log_tokenization, + log_tokens=invokeai.backend.prompting.conditioning.log_tokenization, ) if tool in ("gfpgan", "codeformer", "upscale"): @@ -737,7 +739,7 @@ class Generate: ) elif tool == "outcrop": - from ldm.invoke.restoration.outcrop import Outcrop + from .restoration.outcrop import Outcrop extend_instructions = {} for direction, pixels in _pairwise(opt.outcrop): @@ -790,7 +792,7 @@ class Generate: clear_cuda_cache=self.clear_cuda_cache, ) elif tool == "outpaint": - from ldm.invoke.restoration.outpaint import Outpaint + from .restoration.outpaint import Outpaint restorer = Outpaint(image, self) return restorer.process(opt, args, image_callback=callback, prefix=prefix) @@ -812,7 +814,6 @@ class Generate: hires_fix: bool = False, force_outpaint: bool = False, ): - if hires_fix: return self._make_txt2img2img() @@ -960,7 +961,7 @@ class Generate: seed_everything(random.randrange(0, np.iinfo(np.uint32).max)) if self.embedding_path is not None: - print(f'>> Loading embeddings from {self.embedding_path}') + print(f">> Loading embeddings from {self.embedding_path}") for root, _, files in os.walk(self.embedding_path): for name in files: ti_path = os.path.join(root, name) @@ -1015,7 +1016,6 @@ class Generate: image_callback=None, prefix=None, ): - results = [] for r in image_list: image, seed = r diff --git a/invokeai/backend/generator/__init__.py b/invokeai/backend/generator/__init__.py index 4d61779472..b01e93ad81 100644 --- a/invokeai/backend/generator/__init__.py +++ b/invokeai/backend/generator/__init__.py @@ -1,5 +1,5 @@ -''' +""" Initialization file for the invokeai.generator package -''' +""" from .base import Generator from .inpaint import infill_methods diff --git a/invokeai/backend/generator/base.py b/invokeai/backend/generator/base.py index 767b632103..831c941ff4 100644 --- a/invokeai/backend/generator/base.py +++ b/invokeai/backend/generator/base.py @@ -1,7 +1,7 @@ -''' +""" Base class for invokeai.backend.generator.* including img2img, txt2img, and inpaint -''' +""" from __future__ import annotations import os @@ -9,24 +9,25 @@ import os.path as osp import random import traceback from contextlib import nullcontext +from pathlib import Path import cv2 import numpy as np import torch - -from PIL import Image, ImageFilter, ImageChops from diffusers import DiffusionPipeline from einops import rearrange -from pathlib import Path +from PIL import Image, ImageChops, ImageFilter from pytorch_lightning import seed_everything from tqdm import trange import invokeai.assets.web as web_assets + from ..stable_diffusion.diffusion.ddpm import DiffusionWrapper from ..util.util import rand_perlin_2d downsampling = 8 -CAUTION_IMG = 'caution.png' +CAUTION_IMG = "caution.png" + class Generator: downsampling_factor: int @@ -39,7 +40,7 @@ class Generator: self.precision = precision self.seed = None self.latent_channels = model.channels - self.downsampling_factor = downsampling # BUG: should come from model or config + self.downsampling_factor = downsampling # BUG: should come from model or config self.safety_checker = None self.perlin = 0.0 self.threshold = 0 @@ -50,56 +51,73 @@ class Generator: self.caution_img = None # this is going to be overridden in img2img.py, txt2img.py and inpaint.py - def get_make_image(self,prompt,**kwargs): + def get_make_image(self, prompt, **kwargs): """ Returns a function returning an image derived from the prompt and the initial image Return value depends on the seed at the time you call it """ - raise NotImplementedError("image_iterator() must be implemented in a descendent class") + raise NotImplementedError( + "image_iterator() must be implemented in a descendent class" + ) def set_variation(self, seed, variation_amount, with_variations): - self.seed = seed + self.seed = seed self.variation_amount = variation_amount - self.with_variations = with_variations + self.with_variations = with_variations - def generate(self,prompt,init_image,width,height,sampler, iterations=1,seed=None, - image_callback=None, step_callback=None, threshold=0.0, perlin=0.0, - h_symmetry_time_pct=None, v_symmetry_time_pct=None, - safety_checker:dict=None, - free_gpu_mem: bool=False, - **kwargs): + def generate( + self, + prompt, + init_image, + width, + height, + sampler, + iterations=1, + seed=None, + image_callback=None, + step_callback=None, + threshold=0.0, + perlin=0.0, + h_symmetry_time_pct=None, + v_symmetry_time_pct=None, + safety_checker: dict = None, + free_gpu_mem: bool = False, + **kwargs, + ): scope = nullcontext self.safety_checker = safety_checker self.free_gpu_mem = free_gpu_mem attention_maps_images = [] - attention_maps_callback = lambda saver: attention_maps_images.append(saver.get_stacked_maps_image()) + attention_maps_callback = lambda saver: attention_maps_images.append( + saver.get_stacked_maps_image() + ) make_image = self.get_make_image( prompt, - sampler = sampler, - init_image = init_image, - width = width, - height = height, - step_callback = step_callback, - threshold = threshold, - perlin = perlin, - h_symmetry_time_pct = h_symmetry_time_pct, - v_symmetry_time_pct = v_symmetry_time_pct, - attention_maps_callback = attention_maps_callback, - **kwargs + sampler=sampler, + init_image=init_image, + width=width, + height=height, + step_callback=step_callback, + threshold=threshold, + perlin=perlin, + h_symmetry_time_pct=h_symmetry_time_pct, + v_symmetry_time_pct=v_symmetry_time_pct, + attention_maps_callback=attention_maps_callback, + **kwargs, ) - results = [] - seed = seed if seed is not None and seed >= 0 else self.new_seed() - first_seed = seed + results = [] + seed = seed if seed is not None and seed >= 0 else self.new_seed() + first_seed = seed seed, initial_noise = self.generate_initial_noise(seed, width, height) # There used to be an additional self.model.ema_scope() here, but it breaks # the inpaint-1.5 model. Not sure what it did.... ? with scope(self.model.device.type): - for n in trange(iterations, desc='Generating'): + for n in trange(iterations, desc="Generating"): x_T = None if self.variation_amount > 0: seed_everything(seed) - target_noise = self.get_noise(width,height) + target_noise = self.get_noise(width, height) x_T = self.slerp(self.variation_amount, initial_noise, target_noise) elif initial_noise is not None: # i.e. we specified particular variations @@ -107,9 +125,9 @@ class Generator: else: seed_everything(seed) try: - x_T = self.get_noise(width,height) + x_T = self.get_noise(width, height) except: - print('** An error occurred while getting initial noise **') + print("** An error occurred while getting initial noise **") print(traceback.format_exc()) image = make_image(x_T) @@ -120,19 +138,30 @@ class Generator: results.append([image, seed]) if image_callback is not None: - attention_maps_image = None if len(attention_maps_images)==0 else attention_maps_images[-1] - image_callback(image, seed, first_seed=first_seed, attention_maps_image=attention_maps_image) + attention_maps_image = ( + None + if len(attention_maps_images) == 0 + else attention_maps_images[-1] + ) + image_callback( + image, + seed, + first_seed=first_seed, + attention_maps_image=attention_maps_image, + ) seed = self.new_seed() # Free up memory from the last generation. - clear_cuda_cache = kwargs['clear_cuda_cache'] if 'clear_cuda_cache' in kwargs else None + clear_cuda_cache = ( + kwargs["clear_cuda_cache"] if "clear_cuda_cache" in kwargs else None + ) if clear_cuda_cache is not None: clear_cuda_cache() return results - def sample_to_image(self,samples)->Image.Image: + def sample_to_image(self, samples) -> Image.Image: """ Given samples returned from a sampler, converts it into a PIL Image @@ -141,18 +170,30 @@ class Generator: image = self.model.decode_latents(samples) return self.model.numpy_to_pil(image)[0] - def repaste_and_color_correct(self, result: Image.Image, init_image: Image.Image, init_mask: Image.Image, mask_blur_radius: int = 8) -> Image.Image: + def repaste_and_color_correct( + self, + result: Image.Image, + init_image: Image.Image, + init_mask: Image.Image, + mask_blur_radius: int = 8, + ) -> Image.Image: if init_image is None or init_mask is None: return result # Get the original alpha channel of the mask if there is one. # Otherwise it is some other black/white image format ('1', 'L' or 'RGB') - pil_init_mask = init_mask.getchannel('A') if init_mask.mode == 'RGBA' else init_mask.convert('L') - pil_init_image = init_image.convert('RGBA') # Add an alpha channel if one doesn't exist + pil_init_mask = ( + init_mask.getchannel("A") + if init_mask.mode == "RGBA" + else init_mask.convert("L") + ) + pil_init_image = init_image.convert( + "RGBA" + ) # Add an alpha channel if one doesn't exist # Build an image with only visible pixels from source to use as reference for color-matching. - init_rgb_pixels = np.asarray(init_image.convert('RGB'), dtype=np.uint8) - init_a_pixels = np.asarray(pil_init_image.getchannel('A'), dtype=np.uint8) + init_rgb_pixels = np.asarray(init_image.convert("RGB"), dtype=np.uint8) + init_a_pixels = np.asarray(pil_init_image.getchannel("A"), dtype=np.uint8) init_mask_pixels = np.asarray(pil_init_mask, dtype=np.uint8) # Get numpy version of result @@ -171,44 +212,70 @@ class Generator: # Color correct np_matched_result = np_image.copy() - np_matched_result[:,:,:] = (((np_matched_result[:,:,:].astype(np.float32) - gen_means[None,None,:]) / gen_std[None,None,:]) * init_std[None,None,:] + init_means[None,None,:]).clip(0, 255).astype(np.uint8) - matched_result = Image.fromarray(np_matched_result, mode='RGB') + np_matched_result[:, :, :] = ( + ( + ( + ( + np_matched_result[:, :, :].astype(np.float32) + - gen_means[None, None, :] + ) + / gen_std[None, None, :] + ) + * init_std[None, None, :] + + init_means[None, None, :] + ) + .clip(0, 255) + .astype(np.uint8) + ) + matched_result = Image.fromarray(np_matched_result, mode="RGB") else: - matched_result = Image.fromarray(np_image, mode='RGB') + matched_result = Image.fromarray(np_image, mode="RGB") # Blur the mask out (into init image) by specified amount if mask_blur_radius > 0: nm = np.asarray(pil_init_mask, dtype=np.uint8) - nmd = cv2.erode(nm, kernel=np.ones((3,3), dtype=np.uint8), iterations=int(mask_blur_radius / 2)) - pmd = Image.fromarray(nmd, mode='L') + nmd = cv2.erode( + nm, + kernel=np.ones((3, 3), dtype=np.uint8), + iterations=int(mask_blur_radius / 2), + ) + pmd = Image.fromarray(nmd, mode="L") blurred_init_mask = pmd.filter(ImageFilter.BoxBlur(mask_blur_radius)) else: blurred_init_mask = pil_init_mask - multiplied_blurred_init_mask = ImageChops.multiply(blurred_init_mask, self.pil_image.split()[-1]) + multiplied_blurred_init_mask = ImageChops.multiply( + blurred_init_mask, self.pil_image.split()[-1] + ) # Paste original on color-corrected generation (using blurred mask) - matched_result.paste(init_image, (0,0), mask = multiplied_blurred_init_mask) + matched_result.paste(init_image, (0, 0), mask=multiplied_blurred_init_mask) return matched_result - def sample_to_lowres_estimated_image(self,samples): + def sample_to_lowres_estimated_image(self, samples): # origingally adapted from code by @erucipe and @keturn here: # https://discuss.huggingface.co/t/decoding-latents-to-rgb-without-upscaling/23204/7 # these updated numbers for v1.5 are from @torridgristle - v1_5_latent_rgb_factors = torch.tensor([ - # R G B - [ 0.3444, 0.1385, 0.0670], # L1 - [ 0.1247, 0.4027, 0.1494], # L2 - [-0.3192, 0.2513, 0.2103], # L3 - [-0.1307, -0.1874, -0.7445] # L4 - ], dtype=samples.dtype, device=samples.device) + v1_5_latent_rgb_factors = torch.tensor( + [ + # R G B + [0.3444, 0.1385, 0.0670], # L1 + [0.1247, 0.4027, 0.1494], # L2 + [-0.3192, 0.2513, 0.2103], # L3 + [-0.1307, -0.1874, -0.7445], # L4 + ], + dtype=samples.dtype, + device=samples.device, + ) latent_image = samples[0].permute(1, 2, 0) @ v1_5_latent_rgb_factors - latents_ubyte = (((latent_image + 1) / 2) - .clamp(0, 1) # change scale from -1..1 to 0..1 - .mul(0xFF) # to 0..255 - .byte()).cpu() + latents_ubyte = ( + ((latent_image + 1) / 2) + .clamp(0, 1) # change scale from -1..1 to 0..1 + .mul(0xFF) # to 0..255 + .byte() + ).cpu() return Image.fromarray(latents_ubyte.numpy()) @@ -217,38 +284,45 @@ class Generator: if self.variation_amount > 0 or len(self.with_variations) > 0: # use fixed initial noise plus random noise per iteration seed_everything(seed) - initial_noise = self.get_noise(width,height) + initial_noise = self.get_noise(width, height) for v_seed, v_weight in self.with_variations: seed = v_seed seed_everything(seed) - next_noise = self.get_noise(width,height) + next_noise = self.get_noise(width, height) initial_noise = self.slerp(v_weight, initial_noise, next_noise) if self.variation_amount > 0: - random.seed() # reset RNG to an actually random state, so we can get a random seed for variations - seed = random.randrange(0,np.iinfo(np.uint32).max) + random.seed() # reset RNG to an actually random state, so we can get a random seed for variations + seed = random.randrange(0, np.iinfo(np.uint32).max) return (seed, initial_noise) else: return (seed, None) # returns a tensor filled with random numbers from a normal distribution - def get_noise(self,width,height): + def get_noise(self, width, height): """ Returns a tensor filled with random numbers, either form a normal distribution (txt2img) or from the latent image (img2img, inpaint) """ - raise NotImplementedError("get_noise() must be implemented in a descendent class") + raise NotImplementedError( + "get_noise() must be implemented in a descendent class" + ) - def get_perlin_noise(self,width,height): - fixdevice = 'cpu' if (self.model.device.type == 'mps') else self.model.device + def get_perlin_noise(self, width, height): + fixdevice = "cpu" if (self.model.device.type == "mps") else self.model.device # limit noise to only the diffusion image channels, not the mask channels input_channels = min(self.latent_channels, 4) # round up to the nearest block of 8 temp_width = int((width + 7) / 8) * 8 temp_height = int((height + 7) / 8) * 8 - noise = torch.stack([ - rand_perlin_2d((temp_height, temp_width), - (8, 8), - device = self.model.device).to(fixdevice) for _ in range(input_channels)], dim=0).to(self.model.device) + noise = torch.stack( + [ + rand_perlin_2d( + (temp_height, temp_width), (8, 8), device=self.model.device + ).to(fixdevice) + for _ in range(input_channels) + ], + dim=0, + ).to(self.model.device) return noise[0:4, 0:height, 0:width] def new_seed(self): @@ -256,7 +330,7 @@ class Generator: return self.seed def slerp(self, t, v0, v1, DOT_THRESHOLD=0.9995): - ''' + """ Spherical linear interpolation Args: t (float/np.ndarray): Float value between 0.0 and 1.0 @@ -266,7 +340,7 @@ class Generator: colineal. Not recommended to alter this. Returns: v2 (np.ndarray): Interpolation vector between v0 and v1 - ''' + """ inputs_are_torch = False if not isinstance(v0, np.ndarray): inputs_are_torch = True @@ -292,15 +366,15 @@ class Generator: return v2 - def safety_check(self,image:Image.Image): - ''' + def safety_check(self, image: Image.Image): + """ If the CompViz safety checker flags an NSFW image, we blur it out. - ''' + """ import diffusers - checker = self.safety_checker['checker'] - extractor = self.safety_checker['extractor'] + checker = self.safety_checker["checker"] + extractor = self.safety_checker["extractor"] features = extractor([image], return_tensors="pt") features.to(self.model.device) @@ -309,19 +383,23 @@ class Generator: x_image = x_image[None].transpose(0, 3, 1, 2) diffusers.logging.set_verbosity_error() - checked_image, has_nsfw_concept = checker(images=x_image, clip_input=features.pixel_values) + checked_image, has_nsfw_concept = checker( + images=x_image, clip_input=features.pixel_values + ) if has_nsfw_concept[0]: - print('** An image with potential non-safe content has been detected. A blurred image will be returned. **') + print( + "** An image with potential non-safe content has been detected. A blurred image will be returned. **" + ) return self.blur(image) else: return image - def blur(self,input): + def blur(self, input): blurry = input.filter(filter=ImageFilter.GaussianBlur(radius=32)) try: caution = self.get_caution_img() if caution: - blurry.paste(caution,(0,0),caution) + blurry.paste(caution, (0, 0), caution) except FileNotFoundError: pass return blurry @@ -332,43 +410,52 @@ class Generator: return self.caution_img path = Path(web_assets.__path__[0]) / CAUTION_IMG caution = Image.open(path) - self.caution_img = caution.resize((caution.width // 2, caution.height //2)) + self.caution_img = caution.resize((caution.width // 2, caution.height // 2)) return self.caution_img # this is a handy routine for debugging use. Given a generated sample, # convert it into a PNG image and store it at the indicated path def save_sample(self, sample, filepath): image = self.sample_to_image(sample) - dirname = os.path.dirname(filepath) or '.' + dirname = os.path.dirname(filepath) or "." if not os.path.exists(dirname): - print(f'** creating directory {dirname}') + print(f"** creating directory {dirname}") os.makedirs(dirname, exist_ok=True) - image.save(filepath,'PNG') + image.save(filepath, "PNG") - - def torch_dtype(self)->torch.dtype: - return torch.float16 if self.precision == 'float16' else torch.float32 + def torch_dtype(self) -> torch.dtype: + return torch.float16 if self.precision == "float16" else torch.float32 # returns a tensor filled with random numbers from a normal distribution - def get_noise(self,width,height): - device = self.model.device + def get_noise(self, width, height): + device = self.model.device # limit noise to only the diffusion image channels, not the mask channels input_channels = min(self.latent_channels, 4) - if self.use_mps_noise or device.type == 'mps': - x = torch.randn([1, - input_channels, - height // self.downsampling_factor, - width // self.downsampling_factor], - dtype=self.torch_dtype(), - device='cpu').to(device) + if self.use_mps_noise or device.type == "mps": + x = torch.randn( + [ + 1, + input_channels, + height // self.downsampling_factor, + width // self.downsampling_factor, + ], + dtype=self.torch_dtype(), + device="cpu", + ).to(device) else: - x = torch.randn([1, - input_channels, - height // self.downsampling_factor, - width // self.downsampling_factor], - dtype=self.torch_dtype(), - device=device) + x = torch.randn( + [ + 1, + input_channels, + height // self.downsampling_factor, + width // self.downsampling_factor, + ], + dtype=self.torch_dtype(), + device=device, + ) if self.perlin > 0.0: - perlin_noise = self.get_perlin_noise(width // self.downsampling_factor, height // self.downsampling_factor) - x = (1-self.perlin)*x + self.perlin*perlin_noise + perlin_noise = self.get_perlin_noise( + width // self.downsampling_factor, height // self.downsampling_factor + ) + x = (1 - self.perlin) * x + self.perlin * perlin_noise return x diff --git a/invokeai/backend/generator/embiggen.py b/invokeai/backend/generator/embiggen.py index 23447d59e3..ce9ef4d1b6 100644 --- a/invokeai/backend/generator/embiggen.py +++ b/invokeai/backend/generator/embiggen.py @@ -1,7 +1,7 @@ -''' -invokeai.backend.generator.embiggen descends from ldm.invoke.generator -and generates with invokeai.backend.generator.img2img -''' +""" +invokeai.backend.generator.embiggen descends from .generator +and generates with .generator.img2img +""" import numpy as np import torch @@ -15,23 +15,24 @@ from .img2img import Img2Img class Embiggen(Generator): def __init__(self, model, precision): super().__init__(model, precision) - self.init_latent = None + self.init_latent = None # Replace generate because Embiggen doesn't need/use most of what it does normallly - def generate(self,prompt,iterations=1,seed=None, - image_callback=None, step_callback=None, - **kwargs): - - make_image = self.get_make_image( - prompt, - step_callback = step_callback, - **kwargs - ) - results = [] - seed = seed if seed else self.new_seed() + def generate( + self, + prompt, + iterations=1, + seed=None, + image_callback=None, + step_callback=None, + **kwargs, + ): + make_image = self.get_make_image(prompt, step_callback=step_callback, **kwargs) + results = [] + seed = seed if seed else self.new_seed() # Noise will be generated by the Img2Img generator when called - for _ in trange(iterations, desc='Generating'): + for _ in trange(iterations, desc="Generating"): # make_image will call Img2Img which will do the equivalent of get_noise itself image = make_image() results.append([image, seed]) @@ -56,13 +57,15 @@ class Embiggen(Generator): embiggen, embiggen_tiles, step_callback=None, - **kwargs + **kwargs, ): """ Returns a function returning an image derived from the prompt and multi-stage twice-baked potato layering over the img2img on the initial image Return value depends on the seed at the time you call it """ - assert not sampler.uses_inpainting_model(), "--embiggen is not supported by inpainting models" + assert ( + not sampler.uses_inpainting_model() + ), "--embiggen is not supported by inpainting models" # Construct embiggen arg array, and sanity check arguments if embiggen == None: # embiggen can also be called with just embiggen_tiles @@ -70,48 +73,57 @@ class Embiggen(Generator): elif embiggen[0] < 0: embiggen[0] = 1.0 print( - '>> Embiggen scaling factor cannot be negative, fell back to the default of 1.0 !') + ">> Embiggen scaling factor cannot be negative, fell back to the default of 1.0 !" + ) if len(embiggen) < 2: embiggen.append(0.75) elif embiggen[1] > 1.0 or embiggen[1] < 0: embiggen[1] = 0.75 - print('>> Embiggen upscaling strength for ESRGAN must be between 0 and 1, fell back to the default of 0.75 !') + print( + ">> Embiggen upscaling strength for ESRGAN must be between 0 and 1, fell back to the default of 0.75 !" + ) if len(embiggen) < 3: embiggen.append(0.25) elif embiggen[2] < 0: embiggen[2] = 0.25 - print('>> Overlap size for Embiggen must be a positive ratio between 0 and 1 OR a number of pixels, fell back to the default of 0.25 !') + print( + ">> Overlap size for Embiggen must be a positive ratio between 0 and 1 OR a number of pixels, fell back to the default of 0.25 !" + ) # Convert tiles from their user-freindly count-from-one to count-from-zero, because we need to do modulo math # and then sort them, because... people. if embiggen_tiles: - embiggen_tiles = list(map(lambda n: n-1, embiggen_tiles)) + embiggen_tiles = list(map(lambda n: n - 1, embiggen_tiles)) embiggen_tiles.sort() if strength >= 0.5: - print(f'* WARNING: Embiggen may produce mirror motifs if the strength (-f) is too high (currently {strength}). Try values between 0.35-0.45.') + print( + f"* WARNING: Embiggen may produce mirror motifs if the strength (-f) is too high (currently {strength}). Try values between 0.35-0.45." + ) # Prep img2img generator, since we wrap over it - gen_img2img = Img2Img(self.model,self.precision) + gen_img2img = Img2Img(self.model, self.precision) # Open original init image (not a tensor) to manipulate initsuperimage = Image.open(init_img) with Image.open(init_img) as img: - initsuperimage = img.convert('RGB') + initsuperimage = img.convert("RGB") # Size of the target super init image in pixels initsuperwidth, initsuperheight = initsuperimage.size # Increase by scaling factor if not already resized, using ESRGAN as able if embiggen[0] != 1.0: - initsuperwidth = round(initsuperwidth*embiggen[0]) - initsuperheight = round(initsuperheight*embiggen[0]) + initsuperwidth = round(initsuperwidth * embiggen[0]) + initsuperheight = round(initsuperheight * embiggen[0]) if embiggen[1] > 0: # No point in ESRGAN upscaling if strength is set zero - from ldm.invoke.restoration.realesrgan import ESRGAN + from ..restoration.realesrgan import ESRGAN + esrgan = ESRGAN() print( - f'>> ESRGAN upscaling init image prior to cutting with Embiggen with strength {embiggen[1]}') + f">> ESRGAN upscaling init image prior to cutting with Embiggen with strength {embiggen[1]}" + ) if embiggen[0] > 2: initsuperimage = esrgan.process( initsuperimage, @@ -130,7 +142,8 @@ class Embiggen(Generator): # but from personal experiance it doesn't greatly improve anything after 4x # Resize to target scaling factor resolution initsuperimage = initsuperimage.resize( - (initsuperwidth, initsuperheight), Image.Resampling.LANCZOS) + (initsuperwidth, initsuperheight), Image.Resampling.LANCZOS + ) # Use width and height as tile widths and height # Determine buffer size in pixels @@ -153,23 +166,24 @@ class Embiggen(Generator): emb_tiles_x = 1 emb_tiles_y = 1 if (initsuperwidth - width) > 0: - emb_tiles_x = ceildiv(initsuperwidth - width, - width - overlap_size_x) + 1 + emb_tiles_x = ceildiv(initsuperwidth - width, width - overlap_size_x) + 1 if (initsuperheight - height) > 0: - emb_tiles_y = ceildiv(initsuperheight - height, - height - overlap_size_y) + 1 + emb_tiles_y = ceildiv(initsuperheight - height, height - overlap_size_y) + 1 # Sanity - assert emb_tiles_x > 1 or emb_tiles_y > 1, f'ERROR: Based on the requested dimensions of {initsuperwidth}x{initsuperheight} and tiles of {width}x{height} you don\'t need to Embiggen! Check your arguments.' + assert ( + emb_tiles_x > 1 or emb_tiles_y > 1 + ), f"ERROR: Based on the requested dimensions of {initsuperwidth}x{initsuperheight} and tiles of {width}x{height} you don't need to Embiggen! Check your arguments." # Prep alpha layers -------------- # https://stackoverflow.com/questions/69321734/how-to-create-different-transparency-like-gradient-with-python-pil # agradientL is Left-side transparent - agradientL = Image.linear_gradient('L').rotate( - 90).resize((overlap_size_x, height)) + agradientL = ( + Image.linear_gradient("L").rotate(90).resize((overlap_size_x, height)) + ) # agradientT is Top-side transparent - agradientT = Image.linear_gradient('L').resize((width, overlap_size_y)) + agradientT = Image.linear_gradient("L").resize((width, overlap_size_y)) # radial corner is the left-top corner, made full circle then cut to just the left-top quadrant - agradientC = Image.new('L', (256, 256)) + agradientC = Image.new("L", (256, 256)) for y in range(256): for x in range(256): # Find distance to lower right corner (numpy takes arrays) @@ -177,16 +191,16 @@ class Embiggen(Generator): # Clamp values to max 255 if distanceToLR > 255: distanceToLR = 255 - #Place the pixel as invert of distance + # Place the pixel as invert of distance agradientC.putpixel((x, y), round(255 - distanceToLR)) # Create alternative asymmetric diagonal corner to use on "tailing" intersections to prevent hard edges # Fits for a left-fading gradient on the bottom side and full opacity on the right side. - agradientAsymC = Image.new('L', (256, 256)) + agradientAsymC = Image.new("L", (256, 256)) for y in range(256): for x in range(256): - value = round(max(0, x-(255-y)) * (255 / max(1,y))) - #Clamp values + value = round(max(0, x - (255 - y)) * (255 / max(1, y))) + # Clamp values value = max(0, value) value = min(255, value) agradientAsymC.putpixel((x, y), value) @@ -204,80 +218,91 @@ class Embiggen(Generator): # make masks with an asymmetric upper-right corner so when the curved transparent corner of the next tile # to its right is placed it doesn't reveal a hard trailing semi-transparent edge in the overlapping space alphaLayerTaC = alphaLayerT.copy() - alphaLayerTaC.paste(agradientAsymC.rotate(270).resize((overlap_size_x, overlap_size_y)), (width - overlap_size_x, 0)) + alphaLayerTaC.paste( + agradientAsymC.rotate(270).resize((overlap_size_x, overlap_size_y)), + (width - overlap_size_x, 0), + ) alphaLayerLTaC = alphaLayerLTC.copy() - alphaLayerLTaC.paste(agradientAsymC.rotate(270).resize((overlap_size_x, overlap_size_y)), (width - overlap_size_x, 0)) + alphaLayerLTaC.paste( + agradientAsymC.rotate(270).resize((overlap_size_x, overlap_size_y)), + (width - overlap_size_x, 0), + ) if embiggen_tiles: # Individual unconnected sides alphaLayerR = Image.new("L", (width, height), 255) - alphaLayerR.paste(agradientL.rotate( - 180), (width - overlap_size_x, 0)) + alphaLayerR.paste(agradientL.rotate(180), (width - overlap_size_x, 0)) alphaLayerB = Image.new("L", (width, height), 255) - alphaLayerB.paste(agradientT.rotate( - 180), (0, height - overlap_size_y)) + alphaLayerB.paste(agradientT.rotate(180), (0, height - overlap_size_y)) alphaLayerTB = Image.new("L", (width, height), 255) alphaLayerTB.paste(agradientT, (0, 0)) - alphaLayerTB.paste(agradientT.rotate( - 180), (0, height - overlap_size_y)) + alphaLayerTB.paste(agradientT.rotate(180), (0, height - overlap_size_y)) alphaLayerLR = Image.new("L", (width, height), 255) alphaLayerLR.paste(agradientL, (0, 0)) - alphaLayerLR.paste(agradientL.rotate( - 180), (width - overlap_size_x, 0)) + alphaLayerLR.paste(agradientL.rotate(180), (width - overlap_size_x, 0)) # Sides and corner Layers alphaLayerRBC = Image.new("L", (width, height), 255) - alphaLayerRBC.paste(agradientL.rotate( - 180), (width - overlap_size_x, 0)) - alphaLayerRBC.paste(agradientT.rotate( - 180), (0, height - overlap_size_y)) - alphaLayerRBC.paste(agradientC.rotate(180).resize( - (overlap_size_x, overlap_size_y)), (width - overlap_size_x, height - overlap_size_y)) + alphaLayerRBC.paste(agradientL.rotate(180), (width - overlap_size_x, 0)) + alphaLayerRBC.paste(agradientT.rotate(180), (0, height - overlap_size_y)) + alphaLayerRBC.paste( + agradientC.rotate(180).resize((overlap_size_x, overlap_size_y)), + (width - overlap_size_x, height - overlap_size_y), + ) alphaLayerLBC = Image.new("L", (width, height), 255) alphaLayerLBC.paste(agradientL, (0, 0)) - alphaLayerLBC.paste(agradientT.rotate( - 180), (0, height - overlap_size_y)) - alphaLayerLBC.paste(agradientC.rotate(90).resize( - (overlap_size_x, overlap_size_y)), (0, height - overlap_size_y)) + alphaLayerLBC.paste(agradientT.rotate(180), (0, height - overlap_size_y)) + alphaLayerLBC.paste( + agradientC.rotate(90).resize((overlap_size_x, overlap_size_y)), + (0, height - overlap_size_y), + ) alphaLayerRTC = Image.new("L", (width, height), 255) - alphaLayerRTC.paste(agradientL.rotate( - 180), (width - overlap_size_x, 0)) + alphaLayerRTC.paste(agradientL.rotate(180), (width - overlap_size_x, 0)) alphaLayerRTC.paste(agradientT, (0, 0)) - alphaLayerRTC.paste(agradientC.rotate(270).resize( - (overlap_size_x, overlap_size_y)), (width - overlap_size_x, 0)) + alphaLayerRTC.paste( + agradientC.rotate(270).resize((overlap_size_x, overlap_size_y)), + (width - overlap_size_x, 0), + ) # All but X layers alphaLayerABT = Image.new("L", (width, height), 255) alphaLayerABT.paste(alphaLayerLBC, (0, 0)) - alphaLayerABT.paste(agradientL.rotate( - 180), (width - overlap_size_x, 0)) - alphaLayerABT.paste(agradientC.rotate(180).resize( - (overlap_size_x, overlap_size_y)), (width - overlap_size_x, height - overlap_size_y)) + alphaLayerABT.paste(agradientL.rotate(180), (width - overlap_size_x, 0)) + alphaLayerABT.paste( + agradientC.rotate(180).resize((overlap_size_x, overlap_size_y)), + (width - overlap_size_x, height - overlap_size_y), + ) alphaLayerABL = Image.new("L", (width, height), 255) alphaLayerABL.paste(alphaLayerRTC, (0, 0)) - alphaLayerABL.paste(agradientT.rotate( - 180), (0, height - overlap_size_y)) - alphaLayerABL.paste(agradientC.rotate(180).resize( - (overlap_size_x, overlap_size_y)), (width - overlap_size_x, height - overlap_size_y)) + alphaLayerABL.paste(agradientT.rotate(180), (0, height - overlap_size_y)) + alphaLayerABL.paste( + agradientC.rotate(180).resize((overlap_size_x, overlap_size_y)), + (width - overlap_size_x, height - overlap_size_y), + ) alphaLayerABR = Image.new("L", (width, height), 255) alphaLayerABR.paste(alphaLayerLBC, (0, 0)) alphaLayerABR.paste(agradientT, (0, 0)) - alphaLayerABR.paste(agradientC.resize( - (overlap_size_x, overlap_size_y)), (0, 0)) + alphaLayerABR.paste( + agradientC.resize((overlap_size_x, overlap_size_y)), (0, 0) + ) alphaLayerABB = Image.new("L", (width, height), 255) alphaLayerABB.paste(alphaLayerRTC, (0, 0)) alphaLayerABB.paste(agradientL, (0, 0)) - alphaLayerABB.paste(agradientC.resize( - (overlap_size_x, overlap_size_y)), (0, 0)) + alphaLayerABB.paste( + agradientC.resize((overlap_size_x, overlap_size_y)), (0, 0) + ) # All-around layer alphaLayerAA = Image.new("L", (width, height), 255) alphaLayerAA.paste(alphaLayerABT, (0, 0)) alphaLayerAA.paste(agradientT, (0, 0)) - alphaLayerAA.paste(agradientC.resize( - (overlap_size_x, overlap_size_y)), (0, 0)) - alphaLayerAA.paste(agradientC.rotate(270).resize( - (overlap_size_x, overlap_size_y)), (width - overlap_size_x, 0)) + alphaLayerAA.paste( + agradientC.resize((overlap_size_x, overlap_size_y)), (0, 0) + ) + alphaLayerAA.paste( + agradientC.rotate(270).resize((overlap_size_x, overlap_size_y)), + (width - overlap_size_x, 0), + ) # Clean up temporary gradients del agradientL @@ -287,17 +312,20 @@ class Embiggen(Generator): def make_image(): # Make main tiles ------------------------------------------------- if embiggen_tiles: - print(f'>> Making {len(embiggen_tiles)} Embiggen tiles...') + print(f">> Making {len(embiggen_tiles)} Embiggen tiles...") else: print( - f'>> Making {(emb_tiles_x * emb_tiles_y)} Embiggen tiles ({emb_tiles_x}x{emb_tiles_y})...') + f">> Making {(emb_tiles_x * emb_tiles_y)} Embiggen tiles ({emb_tiles_x}x{emb_tiles_y})..." + ) emb_tile_store = [] # Although we could use the same seed for every tile for determinism, at higher strengths this may # produce duplicated structures for each tile and make the tiling effect more obvious # instead track and iterate a local seed we pass to Img2Img seed = self.seed - seedintlimit = np.iinfo(np.uint32).max - 1 # only retreive this one from numpy + seedintlimit = ( + np.iinfo(np.uint32).max - 1 + ) # only retreive this one from numpy for tile in range(emb_tiles_x * emb_tiles_y): # Don't iterate on first tile @@ -334,37 +362,38 @@ class Embiggen(Generator): if embiggen_tiles: print( - f'Making tile #{tile + 1} ({embiggen_tiles.index(tile) + 1} of {len(embiggen_tiles)} requested)') + f"Making tile #{tile + 1} ({embiggen_tiles.index(tile) + 1} of {len(embiggen_tiles)} requested)" + ) else: - print( - f'Starting {tile + 1} of {(emb_tiles_x * emb_tiles_y)} tiles') + print(f"Starting {tile + 1} of {(emb_tiles_x * emb_tiles_y)} tiles") # create a torch tensor from an Image - newinitimage = np.array( - newinitimage).astype(np.float32) / 255.0 + newinitimage = np.array(newinitimage).astype(np.float32) / 255.0 newinitimage = newinitimage[None].transpose(0, 3, 1, 2) newinitimage = torch.from_numpy(newinitimage) newinitimage = 2.0 * newinitimage - 1.0 newinitimage = newinitimage.to(self.model.device) - clear_cuda_cache = kwargs['clear_cuda_cache'] if 'clear_cuda_cache' in kwargs else None + clear_cuda_cache = ( + kwargs["clear_cuda_cache"] if "clear_cuda_cache" in kwargs else None + ) tile_results = gen_img2img.generate( prompt, - iterations = 1, - seed = seed, - sampler = sampler, - steps = steps, - cfg_scale = cfg_scale, - conditioning = conditioning, - ddim_eta = ddim_eta, - image_callback = None, # called only after the final image is generated - step_callback = step_callback, # called after each intermediate image is generated - width = width, - height = height, - init_image = newinitimage, # notice that init_image is different from init_img - mask_image = None, - strength = strength, - clear_cuda_cache = clear_cuda_cache + iterations=1, + seed=seed, + sampler=sampler, + steps=steps, + cfg_scale=cfg_scale, + conditioning=conditioning, + ddim_eta=ddim_eta, + image_callback=None, # called only after the final image is generated + step_callback=step_callback, # called after each intermediate image is generated + width=width, + height=height, + init_image=newinitimage, # notice that init_image is different from init_img + mask_image=None, + strength=strength, + clear_cuda_cache=clear_cuda_cache, ) emb_tile_store.append(tile_results[0][0]) @@ -373,12 +402,14 @@ class Embiggen(Generator): del newinitimage # Sanity check we have them all - if len(emb_tile_store) == (emb_tiles_x * emb_tiles_y) or (embiggen_tiles != [] and len(emb_tile_store) == len(embiggen_tiles)): - outputsuperimage = Image.new( - "RGBA", (initsuperwidth, initsuperheight)) + if len(emb_tile_store) == (emb_tiles_x * emb_tiles_y) or ( + embiggen_tiles != [] and len(emb_tile_store) == len(embiggen_tiles) + ): + outputsuperimage = Image.new("RGBA", (initsuperwidth, initsuperheight)) if embiggen_tiles: outputsuperimage.alpha_composite( - initsuperimage.convert('RGBA'), (0, 0)) + initsuperimage.convert("RGBA"), (0, 0) + ) for tile in range(emb_tiles_x * emb_tiles_y): if embiggen_tiles: if tile in embiggen_tiles: @@ -387,7 +418,7 @@ class Embiggen(Generator): continue else: intileimage = emb_tile_store[tile] - intileimage = intileimage.convert('RGBA') + intileimage = intileimage.convert("RGBA") # Get row and column entries emb_row_i = tile // emb_tiles_x emb_column_i = tile % emb_tiles_x @@ -399,8 +430,7 @@ class Embiggen(Generator): if emb_column_i + 1 == emb_tiles_x: left = initsuperwidth - width else: - left = round(emb_column_i * - (width - overlap_size_x)) + left = round(emb_column_i * (width - overlap_size_x)) if emb_row_i + 1 == emb_tiles_y: top = initsuperheight - height else: @@ -411,33 +441,43 @@ class Embiggen(Generator): # top of image if emb_row_i == 0: if emb_column_i == 0: - if (tile+1) in embiggen_tiles: # Look-ahead right - if (tile+emb_tiles_x) not in embiggen_tiles: # Look-ahead down + if (tile + 1) in embiggen_tiles: # Look-ahead right + if ( + tile + emb_tiles_x + ) not in embiggen_tiles: # Look-ahead down intileimage.putalpha(alphaLayerB) # Otherwise do nothing on this tile - elif (tile+emb_tiles_x) in embiggen_tiles: # Look-ahead down only + elif ( + tile + emb_tiles_x + ) in embiggen_tiles: # Look-ahead down only intileimage.putalpha(alphaLayerR) else: intileimage.putalpha(alphaLayerRBC) elif emb_column_i == emb_tiles_x - 1: - if (tile+emb_tiles_x) in embiggen_tiles: # Look-ahead down + if ( + tile + emb_tiles_x + ) in embiggen_tiles: # Look-ahead down intileimage.putalpha(alphaLayerL) else: intileimage.putalpha(alphaLayerLBC) else: - if (tile+1) in embiggen_tiles: # Look-ahead right - if (tile+emb_tiles_x) in embiggen_tiles: # Look-ahead down + if (tile + 1) in embiggen_tiles: # Look-ahead right + if ( + tile + emb_tiles_x + ) in embiggen_tiles: # Look-ahead down intileimage.putalpha(alphaLayerL) else: intileimage.putalpha(alphaLayerLBC) - elif (tile+emb_tiles_x) in embiggen_tiles: # Look-ahead down only + elif ( + tile + emb_tiles_x + ) in embiggen_tiles: # Look-ahead down only intileimage.putalpha(alphaLayerLR) else: intileimage.putalpha(alphaLayerABT) # bottom of image elif emb_row_i == emb_tiles_y - 1: if emb_column_i == 0: - if (tile+1) in embiggen_tiles: # Look-ahead right + if (tile + 1) in embiggen_tiles: # Look-ahead right intileimage.putalpha(alphaLayerTaC) else: intileimage.putalpha(alphaLayerRTC) @@ -445,34 +485,44 @@ class Embiggen(Generator): # No tiles to look ahead to intileimage.putalpha(alphaLayerLTC) else: - if (tile+1) in embiggen_tiles: # Look-ahead right + if (tile + 1) in embiggen_tiles: # Look-ahead right intileimage.putalpha(alphaLayerLTaC) else: intileimage.putalpha(alphaLayerABB) # vertical middle of image else: if emb_column_i == 0: - if (tile+1) in embiggen_tiles: # Look-ahead right - if (tile+emb_tiles_x) in embiggen_tiles: # Look-ahead down + if (tile + 1) in embiggen_tiles: # Look-ahead right + if ( + tile + emb_tiles_x + ) in embiggen_tiles: # Look-ahead down intileimage.putalpha(alphaLayerTaC) else: intileimage.putalpha(alphaLayerTB) - elif (tile+emb_tiles_x) in embiggen_tiles: # Look-ahead down only + elif ( + tile + emb_tiles_x + ) in embiggen_tiles: # Look-ahead down only intileimage.putalpha(alphaLayerRTC) else: intileimage.putalpha(alphaLayerABL) elif emb_column_i == emb_tiles_x - 1: - if (tile+emb_tiles_x) in embiggen_tiles: # Look-ahead down + if ( + tile + emb_tiles_x + ) in embiggen_tiles: # Look-ahead down intileimage.putalpha(alphaLayerLTC) else: intileimage.putalpha(alphaLayerABR) else: - if (tile+1) in embiggen_tiles: # Look-ahead right - if (tile+emb_tiles_x) in embiggen_tiles: # Look-ahead down + if (tile + 1) in embiggen_tiles: # Look-ahead right + if ( + tile + emb_tiles_x + ) in embiggen_tiles: # Look-ahead down intileimage.putalpha(alphaLayerLTaC) else: intileimage.putalpha(alphaLayerABR) - elif (tile+emb_tiles_x) in embiggen_tiles: # Look-ahead down only + elif ( + tile + emb_tiles_x + ) in embiggen_tiles: # Look-ahead down only intileimage.putalpha(alphaLayerABB) else: intileimage.putalpha(alphaLayerAA) @@ -481,21 +531,28 @@ class Embiggen(Generator): if emb_row_i == 0 and emb_column_i >= 1: intileimage.putalpha(alphaLayerL) elif emb_row_i >= 1 and emb_column_i == 0: - if emb_column_i + 1 == emb_tiles_x: # If we don't have anything that can be placed to the right + if ( + emb_column_i + 1 == emb_tiles_x + ): # If we don't have anything that can be placed to the right intileimage.putalpha(alphaLayerT) else: intileimage.putalpha(alphaLayerTaC) else: - if emb_column_i + 1 == emb_tiles_x: # If we don't have anything that can be placed to the right + if ( + emb_column_i + 1 == emb_tiles_x + ): # If we don't have anything that can be placed to the right intileimage.putalpha(alphaLayerLTC) else: intileimage.putalpha(alphaLayerLTaC) # Layer tile onto final image outputsuperimage.alpha_composite(intileimage, (left, top)) else: - print('Error: could not find all Embiggen output tiles in memory? Something must have gone wrong with img2img generation.') + print( + "Error: could not find all Embiggen output tiles in memory? Something must have gone wrong with img2img generation." + ) # after internal loops and patching up return Embiggen image return outputsuperimage + # end of function declaration return make_image diff --git a/invokeai/backend/generator/img2img.py b/invokeai/backend/generator/img2img.py index edd78d6148..f9692b9cc5 100644 --- a/invokeai/backend/generator/img2img.py +++ b/invokeai/backend/generator/img2img.py @@ -1,25 +1,42 @@ -''' -invokeai.backend.generator.img2img descends from ldm.invoke.generator -''' +""" +invokeai.backend.generator.img2img descends from .generator +""" import torch from diffusers import logging +from ..stable_diffusion import ( + ConditioningData, + PostprocessingSettings, + StableDiffusionGeneratorPipeline, +) from .base import Generator -from ..stable_diffusion import (StableDiffusionGeneratorPipeline, - ConditioningData, - PostprocessingSettings - ) + class Img2Img(Generator): def __init__(self, model, precision): super().__init__(model, precision) - self.init_latent = None # by get_noise() + self.init_latent = None # by get_noise() - def get_make_image(self,prompt,sampler,steps,cfg_scale,ddim_eta, - conditioning,init_image,strength,step_callback=None,threshold=0.0,warmup=0.2,perlin=0.0, - h_symmetry_time_pct=None,v_symmetry_time_pct=None,attention_maps_callback=None, - **kwargs): + def get_make_image( + self, + prompt, + sampler, + steps, + cfg_scale, + ddim_eta, + conditioning, + init_image, + strength, + step_callback=None, + threshold=0.0, + warmup=0.2, + perlin=0.0, + h_symmetry_time_pct=None, + v_symmetry_time_pct=None, + attention_maps_callback=None, + **kwargs, + ): """ Returns a function returning an image derived from the prompt and the initial image Return value depends on the seed at the time you call it. @@ -30,30 +47,37 @@ class Img2Img(Generator): pipeline: StableDiffusionGeneratorPipeline = self.model pipeline.scheduler = sampler - uc, c, extra_conditioning_info = conditioning - conditioning_data = ( - ConditioningData( - uc, c, cfg_scale, extra_conditioning_info, - postprocessing_settings=PostprocessingSettings( - threshold=threshold, - warmup=warmup, - h_symmetry_time_pct=h_symmetry_time_pct, - v_symmetry_time_pct=v_symmetry_time_pct - ) - ).add_scheduler_args_if_applicable(pipeline.scheduler, eta=ddim_eta)) - + uc, c, extra_conditioning_info = conditioning + conditioning_data = ConditioningData( + uc, + c, + cfg_scale, + extra_conditioning_info, + postprocessing_settings=PostprocessingSettings( + threshold=threshold, + warmup=warmup, + h_symmetry_time_pct=h_symmetry_time_pct, + v_symmetry_time_pct=v_symmetry_time_pct, + ), + ).add_scheduler_args_if_applicable(pipeline.scheduler, eta=ddim_eta) def make_image(x_T): # FIXME: use x_T for initial seeded noise # We're not at the moment because the pipeline automatically resizes init_image if # necessary, which the x_T input might not match. - logging.set_verbosity_error() # quench safety check warnings + logging.set_verbosity_error() # quench safety check warnings pipeline_output = pipeline.img2img_from_embeddings( - init_image, strength, steps, conditioning_data, + init_image, + strength, + steps, + conditioning_data, noise_func=self.get_noise_like, - callback=step_callback + callback=step_callback, ) - if pipeline_output.attention_map_saver is not None and attention_maps_callback is not None: + if ( + pipeline_output.attention_map_saver is not None + and attention_maps_callback is not None + ): attention_maps_callback(pipeline_output.attention_map_saver) return pipeline.numpy_to_pil(pipeline_output.images)[0] @@ -61,11 +85,13 @@ class Img2Img(Generator): def get_noise_like(self, like: torch.Tensor): device = like.device - if device.type == 'mps': - x = torch.randn_like(like, device='cpu').to(device) + if device.type == "mps": + x = torch.randn_like(like, device="cpu").to(device) else: x = torch.randn_like(like, device=device) if self.perlin > 0.0: shape = like.shape - x = (1-self.perlin)*x + self.perlin*self.get_perlin_noise(shape[3], shape[2]) + x = (1 - self.perlin) * x + self.perlin * self.get_perlin_noise( + shape[3], shape[2] + ) return x diff --git a/invokeai/backend/generator/inpaint.py b/invokeai/backend/generator/inpaint.py index a6e6fe4387..f7f21b8906 100644 --- a/invokeai/backend/generator/inpaint.py +++ b/invokeai/backend/generator/inpaint.py @@ -1,33 +1,35 @@ -''' -invokeai.backend.generator.inpaint descends from ldm.invoke.generator -''' +""" +invokeai.backend.generator.inpaint descends from .generator +""" from __future__ import annotations import math -import PIL import cv2 import numpy as np +import PIL import torch -from PIL import Image, ImageFilter, ImageOps, ImageChops +from PIL import Image, ImageChops, ImageFilter, ImageOps -from ..stable_diffusion.diffusers_pipeline import (image_resized_to_grid_as_tensor, - StableDiffusionGeneratorPipeline, - ConditioningData - ) -from .img2img import Img2Img from ..image_util import PatchMatch, debug_image +from ..stable_diffusion.diffusers_pipeline import ( + ConditioningData, + StableDiffusionGeneratorPipeline, + image_resized_to_grid_as_tensor, +) +from .img2img import Img2Img -def infill_methods()->list[str]: +def infill_methods() -> list[str]: methods = [ "tile", "solid", ] if PatchMatch.patchmatch_available(): - methods.insert(0, 'patchmatch') + methods.insert(0, "patchmatch") return methods + class Inpaint(Img2Img): def __init__(self, model, precision): self.inpaint_height = 0 @@ -54,11 +56,11 @@ class Inpaint(Img2Img): np.ravel(image), shape=(nrows, ncols, height, width, depth), strides=(height * _strides[0], width * _strides[1], *_strides), - writeable=False + writeable=False, ) def infill_patchmatch(self, im: Image.Image) -> Image: - if im.mode != 'RGBA': + if im.mode != "RGBA": return im # Skip patchmatch if patchmatch isn't available @@ -66,13 +68,17 @@ class Inpaint(Img2Img): return im # Patchmatch (note, we may want to expose patch_size? Increasing it significantly impacts performance though) - im_patched_np = PatchMatch.inpaint(im.convert('RGB'), ImageOps.invert(im.split()[-1]), patch_size = 3) - im_patched = Image.fromarray(im_patched_np, mode = 'RGB') + im_patched_np = PatchMatch.inpaint( + im.convert("RGB"), ImageOps.invert(im.split()[-1]), patch_size=3 + ) + im_patched = Image.fromarray(im_patched_np, mode="RGB") return im_patched - def tile_fill_missing(self, im: Image.Image, tile_size: int = 16, seed: int = None) -> Image: + def tile_fill_missing( + self, im: Image.Image, tile_size: int = 16, seed: int = None + ) -> Image: # Only fill if there's an alpha layer - if im.mode != 'RGBA': + if im.mode != "RGBA": return im a = np.asarray(im, dtype=np.uint8) @@ -80,21 +86,21 @@ class Inpaint(Img2Img): tile_size = (tile_size, tile_size) # Get the image as tiles of a specified size - tiles = self.get_tile_images(a,*tile_size).copy() + tiles = self.get_tile_images(a, *tile_size).copy() # Get the mask as tiles - tiles_mask = tiles[:,:,:,:,3] + tiles_mask = tiles[:, :, :, :, 3] # Find any mask tiles with any fully transparent pixels (we will be replacing these later) tmask_shape = tiles_mask.shape tiles_mask = tiles_mask.reshape(math.prod(tiles_mask.shape)) - n,ny = (math.prod(tmask_shape[0:2])), math.prod(tmask_shape[2:]) - tiles_mask = (tiles_mask > 0) - tiles_mask = tiles_mask.reshape((n,ny)).all(axis = 1) + n, ny = (math.prod(tmask_shape[0:2])), math.prod(tmask_shape[2:]) + tiles_mask = tiles_mask > 0 + tiles_mask = tiles_mask.reshape((n, ny)).all(axis=1) # Get RGB tiles in single array and filter by the mask tshape = tiles.shape - tiles_all = tiles.reshape((math.prod(tiles.shape[0:2]), * tiles.shape[2:])) + tiles_all = tiles.reshape((math.prod(tiles.shape[0:2]), *tiles.shape[2:])) filtered_tiles = tiles_all[tiles_mask] if len(filtered_tiles) == 0: @@ -102,23 +108,32 @@ class Inpaint(Img2Img): # Find all invalid tiles and replace with a random valid tile replace_count = (tiles_mask == False).sum() - rng = np.random.default_rng(seed = seed) - tiles_all[np.logical_not(tiles_mask)] = filtered_tiles[rng.choice(filtered_tiles.shape[0], replace_count),:,:,:] + rng = np.random.default_rng(seed=seed) + tiles_all[np.logical_not(tiles_mask)] = filtered_tiles[ + rng.choice(filtered_tiles.shape[0], replace_count), :, :, : + ] # Convert back to an image tiles_all = tiles_all.reshape(tshape) - tiles_all = tiles_all.swapaxes(1,2) - st = tiles_all.reshape((math.prod(tiles_all.shape[0:2]), math.prod(tiles_all.shape[2:4]), tiles_all.shape[4])) - si = Image.fromarray(st, mode='RGBA') + tiles_all = tiles_all.swapaxes(1, 2) + st = tiles_all.reshape( + ( + math.prod(tiles_all.shape[0:2]), + math.prod(tiles_all.shape[2:4]), + tiles_all.shape[4], + ) + ) + si = Image.fromarray(st, mode="RGBA") return si - def mask_edge(self, mask: Image, edge_size: int, edge_blur: int) -> Image: npimg = np.asarray(mask, dtype=np.uint8) # Detect any partially transparent regions - npgradient = np.uint8(255 * (1.0 - np.floor(np.abs(0.5 - np.float32(npimg) / 255.0) * 2.0))) + npgradient = np.uint8( + 255 * (1.0 - np.floor(np.abs(0.5 - np.float32(npimg) / 255.0) * 2.0)) + ) # Detect hard edges npedge = cv2.Canny(npimg, threshold1=100, threshold2=200) @@ -127,7 +142,9 @@ class Inpaint(Img2Img): npmask = npgradient + npedge # Expand - npmask = cv2.dilate(npmask, np.ones((3,3), np.uint8), iterations = int(edge_size / 2)) + npmask = cv2.dilate( + npmask, np.ones((3, 3), np.uint8), iterations=int(edge_size / 2) + ) new_mask = Image.fromarray(npmask) @@ -136,9 +153,22 @@ class Inpaint(Img2Img): return ImageOps.invert(new_mask) - - def seam_paint(self, im: Image.Image, seam_size: int, seam_blur: int, prompt, sampler, steps, cfg_scale, ddim_eta, - conditioning, strength, noise, infill_method, step_callback) -> Image.Image: + def seam_paint( + self, + im: Image.Image, + seam_size: int, + seam_blur: int, + prompt, + sampler, + steps, + cfg_scale, + ddim_eta, + conditioning, + strength, + noise, + infill_method, + step_callback, + ) -> Image.Image: hard_mask = self.pil_image.split()[-1].copy() mask = self.mask_edge(hard_mask, seam_size, seam_blur) @@ -149,15 +179,15 @@ class Inpaint(Img2Img): cfg_scale, ddim_eta, conditioning, - init_image = im.copy().convert('RGBA'), - mask_image = mask, - strength = strength, - mask_blur_radius = 0, - seam_size = 0, - step_callback = step_callback, - inpaint_width = im.width, - inpaint_height = im.height, - infill_method = infill_method + init_image=im.copy().convert("RGBA"), + mask_image=mask, + strength=strength, + mask_blur_radius=0, + seam_size=0, + step_callback=step_callback, + inpaint_width=im.width, + inpaint_height=im.height, + infill_method=infill_method, ) seam_noise = self.get_noise(im.width, im.height) @@ -166,28 +196,35 @@ class Inpaint(Img2Img): return result - @torch.no_grad() - def get_make_image(self,prompt,sampler,steps,cfg_scale,ddim_eta, - conditioning, - init_image: PIL.Image.Image | torch.FloatTensor, - mask_image: PIL.Image.Image | torch.FloatTensor, - strength: float, - mask_blur_radius: int = 8, - # Seam settings - when 0, doesn't fill seam - seam_size: int = 0, - seam_blur: int = 0, - seam_strength: float = 0.7, - seam_steps: int = 10, - tile_size: int = 32, - step_callback=None, - inpaint_replace=False, enable_image_debugging=False, - infill_method = None, - inpaint_width=None, - inpaint_height=None, - inpaint_fill:tuple(int)=(0x7F, 0x7F, 0x7F, 0xFF), - attention_maps_callback=None, - **kwargs): + def get_make_image( + self, + prompt, + sampler, + steps, + cfg_scale, + ddim_eta, + conditioning, + init_image: PIL.Image.Image | torch.FloatTensor, + mask_image: PIL.Image.Image | torch.FloatTensor, + strength: float, + mask_blur_radius: int = 8, + # Seam settings - when 0, doesn't fill seam + seam_size: int = 0, + seam_blur: int = 0, + seam_strength: float = 0.7, + seam_steps: int = 10, + tile_size: int = 32, + step_callback=None, + inpaint_replace=False, + enable_image_debugging=False, + infill_method=None, + inpaint_width=None, + inpaint_height=None, + inpaint_fill: tuple(int) = (0x7F, 0x7F, 0x7F, 0xFF), + attention_maps_callback=None, + **kwargs, + ): """ Returns a function returning an image derived from the prompt and the initial image + mask. Return value depends on the seed at @@ -205,33 +242,39 @@ class Inpaint(Img2Img): self.pil_image = init_image.copy() # Do infill - if infill_method == 'patchmatch' and PatchMatch.patchmatch_available(): + if infill_method == "patchmatch" and PatchMatch.patchmatch_available(): init_filled = self.infill_patchmatch(self.pil_image.copy()) - elif infill_method == 'tile': + elif infill_method == "tile": init_filled = self.tile_fill_missing( - self.pil_image.copy(), - seed = self.seed, - tile_size = tile_size + self.pil_image.copy(), seed=self.seed, tile_size=tile_size ) - elif infill_method == 'solid': + elif infill_method == "solid": solid_bg = PIL.Image.new("RGBA", init_image.size, inpaint_fill) init_filled = PIL.Image.alpha_composite(solid_bg, init_image) else: - raise ValueError(f"Non-supported infill type {infill_method}", infill_method) - init_filled.paste(init_image, (0,0), init_image.split()[-1]) + raise ValueError( + f"Non-supported infill type {infill_method}", infill_method + ) + init_filled.paste(init_image, (0, 0), init_image.split()[-1]) # Resize if requested for inpainting if inpaint_width and inpaint_height: init_filled = init_filled.resize((inpaint_width, inpaint_height)) - debug_image(init_filled, "init_filled", debug_status=self.enable_image_debugging) + debug_image( + init_filled, "init_filled", debug_status=self.enable_image_debugging + ) # Create init tensor - init_image = image_resized_to_grid_as_tensor(init_filled.convert('RGB')) + init_image = image_resized_to_grid_as_tensor(init_filled.convert("RGB")) if isinstance(mask_image, PIL.Image.Image): self.pil_mask = mask_image.copy() - debug_image(mask_image, "mask_image BEFORE multiply with pil_image", debug_status=self.enable_image_debugging) + debug_image( + mask_image, + "mask_image BEFORE multiply with pil_image", + debug_status=self.enable_image_debugging, + ) init_alpha = self.pil_image.getchannel("A") if mask_image.mode != "L": @@ -244,8 +287,14 @@ class Inpaint(Img2Img): if inpaint_width and inpaint_height: mask_image = mask_image.resize((inpaint_width, inpaint_height)) - debug_image(mask_image, "mask_image AFTER multiply with pil_image", debug_status=self.enable_image_debugging) - mask: torch.FloatTensor = image_resized_to_grid_as_tensor(mask_image, normalize=False) + debug_image( + mask_image, + "mask_image AFTER multiply with pil_image", + debug_status=self.enable_image_debugging, + ) + mask: torch.FloatTensor = image_resized_to_grid_as_tensor( + mask_image, normalize=False + ) else: mask: torch.FloatTensor = mask_image @@ -257,9 +306,9 @@ class Inpaint(Img2Img): # todo: support cross-attention control uc, c, _ = conditioning - conditioning_data = (ConditioningData(uc, c, cfg_scale) - .add_scheduler_args_if_applicable(pipeline.scheduler, eta=ddim_eta)) - + conditioning_data = ConditioningData( + uc, c, cfg_scale + ).add_scheduler_args_if_applicable(pipeline.scheduler, eta=ddim_eta) def make_image(x_T): pipeline_output = pipeline.inpaint_from_embeddings( @@ -272,43 +321,71 @@ class Inpaint(Img2Img): callback=step_callback, ) - if pipeline_output.attention_map_saver is not None and attention_maps_callback is not None: + if ( + pipeline_output.attention_map_saver is not None + and attention_maps_callback is not None + ): attention_maps_callback(pipeline_output.attention_map_saver) - result = self.postprocess_size_and_mask(pipeline.numpy_to_pil(pipeline_output.images)[0]) + result = self.postprocess_size_and_mask( + pipeline.numpy_to_pil(pipeline_output.images)[0] + ) # Seam paint if this is our first pass (seam_size set to 0 during seam painting) if seam_size > 0: old_image = self.pil_image or init_image old_mask = self.pil_mask or mask_image - result = self.seam_paint(result, seam_size, seam_blur, prompt, sampler, seam_steps, cfg_scale, ddim_eta, - conditioning, seam_strength, x_T, infill_method, step_callback) + result = self.seam_paint( + result, + seam_size, + seam_blur, + prompt, + sampler, + seam_steps, + cfg_scale, + ddim_eta, + conditioning, + seam_strength, + x_T, + infill_method, + step_callback, + ) # Restore original settings - self.get_make_image(prompt,sampler,steps,cfg_scale,ddim_eta, - conditioning, - old_image, - old_mask, - strength, - mask_blur_radius, seam_size, seam_blur, seam_strength, - seam_steps, tile_size, step_callback, - inpaint_replace, enable_image_debugging, - inpaint_width = inpaint_width, - inpaint_height = inpaint_height, - infill_method = infill_method, - **kwargs) + self.get_make_image( + prompt, + sampler, + steps, + cfg_scale, + ddim_eta, + conditioning, + old_image, + old_mask, + strength, + mask_blur_radius, + seam_size, + seam_blur, + seam_strength, + seam_steps, + tile_size, + step_callback, + inpaint_replace, + enable_image_debugging, + inpaint_width=inpaint_width, + inpaint_height=inpaint_height, + infill_method=infill_method, + **kwargs, + ) return result return make_image - - def sample_to_image(self, samples)->Image.Image: - gen_result = super().sample_to_image(samples).convert('RGB') + def sample_to_image(self, samples) -> Image.Image: + gen_result = super().sample_to_image(samples).convert("RGB") return self.postprocess_size_and_mask(gen_result) - def postprocess_size_and_mask(self, gen_result: Image.Image) -> Image.Image: debug_image(gen_result, "gen_result", debug_status=self.enable_image_debugging) @@ -319,7 +396,13 @@ class Inpaint(Img2Img): if self.pil_image is None or self.pil_mask is None: return gen_result - corrected_result = self.repaste_and_color_correct(gen_result, self.pil_image, self.pil_mask, self.mask_blur_radius) - debug_image(corrected_result, "corrected_result", debug_status=self.enable_image_debugging) + corrected_result = self.repaste_and_color_correct( + gen_result, self.pil_image, self.pil_mask, self.mask_blur_radius + ) + debug_image( + corrected_result, + "corrected_result", + debug_status=self.enable_image_debugging, + ) return corrected_result diff --git a/invokeai/backend/generator/omnibus.py b/invokeai/backend/generator/omnibus.py deleted file mode 100644 index a6fae3e567..0000000000 --- a/invokeai/backend/generator/omnibus.py +++ /dev/null @@ -1,173 +0,0 @@ -"""omnibus module to be used with the runwayml 9-channel custom inpainting model""" - -import torch -from PIL import Image, ImageOps -from einops import repeat - -from ldm.invoke.devices import choose_autocast -from ldm.invoke.generator.img2img import Img2Img -from ldm.invoke.generator.txt2img import Txt2Img - - -class Omnibus(Img2Img,Txt2Img): - def __init__(self, model, precision): - super().__init__(model, precision) - self.pil_mask = None - self.pil_image = None - - def get_make_image( - self, - prompt, - sampler, - steps, - cfg_scale, - ddim_eta, - conditioning, - width, - height, - init_image = None, - mask_image = None, - strength = None, - step_callback=None, - threshold=0.0, - perlin=0.0, - mask_blur_radius: int = 8, - **kwargs): - """ - Returns a function returning an image derived from the prompt and the initial image - Return value depends on the seed at the time you call it. - """ - self.perlin = perlin - num_samples = 1 - - sampler.make_schedule( - ddim_num_steps=steps, ddim_eta=ddim_eta, verbose=False - ) - - if isinstance(init_image, Image.Image): - self.pil_image = init_image - if init_image.mode != 'RGB': - init_image = init_image.convert('RGB') - init_image = self._image_to_tensor(init_image) - - if isinstance(mask_image, Image.Image): - self.pil_mask = mask_image - - mask_image = ImageChops.multiply(mask_image.convert('L'), self.pil_image.split()[-1]) - mask_image = self._image_to_tensor(ImageOps.invert(mask_image), normalize=False) - - self.mask_blur_radius = mask_blur_radius - - if init_image is not None and mask_image is not None: # inpainting - masked_image = init_image * (1 - mask_image) # masked image is the image masked by mask - masked regions zero - - elif init_image is not None: # img2img - scope = choose_autocast(self.precision) - - with scope(self.model.device.type): - self.init_latent = self.model.get_first_stage_encoding( - self.model.encode_first_stage(init_image) - ) # move to latent space - - # create a completely black mask (1s) - mask_image = torch.ones(1, 1, init_image.shape[2], init_image.shape[3], device=self.model.device) - # and the masked image is just a copy of the original - masked_image = init_image - - else: # txt2img - init_image = torch.zeros(1, 3, height, width, device=self.model.device) - mask_image = torch.ones(1, 1, height, width, device=self.model.device) - masked_image = init_image - - self.init_latent = init_image - height = init_image.shape[2] - width = init_image.shape[3] - model = self.model - - def make_image(x_T): - with torch.no_grad(): - scope = choose_autocast(self.precision) - with scope(self.model.device.type): - - batch = self.make_batch_sd( - init_image, - mask_image, - masked_image, - prompt=prompt, - device=model.device, - num_samples=num_samples, - ) - - c = model.cond_stage_model.encode(batch["txt"]) - c_cat = list() - for ck in model.concat_keys: - cc = batch[ck].float() - if ck != model.masked_image_key: - bchw = [num_samples, 4, height//8, width//8] - cc = torch.nn.functional.interpolate(cc, size=bchw[-2:]) - else: - cc = model.get_first_stage_encoding(model.encode_first_stage(cc)) - c_cat.append(cc) - c_cat = torch.cat(c_cat, dim=1) - - # cond - cond={"c_concat": [c_cat], "c_crossattn": [c]} - - # uncond cond - uc_cross = model.get_unconditional_conditioning(num_samples, "") - uc_full = {"c_concat": [c_cat], "c_crossattn": [uc_cross]} - shape = [model.channels, height//8, width//8] - - samples, _ = sampler.sample( - batch_size = 1, - S = steps, - x_T = x_T, - conditioning = cond, - shape = shape, - verbose = False, - unconditional_guidance_scale = cfg_scale, - unconditional_conditioning = uc_full, - eta = 1.0, - img_callback = step_callback, - threshold = threshold, - ) - if self.free_gpu_mem: - self.model.model.to("cpu") - return self.sample_to_image(samples) - - return make_image - - def make_batch_sd( - self, - image, - mask, - masked_image, - prompt, - device, - num_samples=1): - batch = { - "image": repeat(image.to(device=device), "1 ... -> n ...", n=num_samples), - "txt": num_samples * [prompt], - "mask": repeat(mask.to(device=device), "1 ... -> n ...", n=num_samples), - "masked_image": repeat(masked_image.to(device=device), "1 ... -> n ...", n=num_samples), - } - return batch - - def get_noise(self, width:int, height:int): - if self.init_latent is not None: - height = self.init_latent.shape[2] - width = self.init_latent.shape[3] - return Txt2Img.get_noise(self,width,height) - - - def sample_to_image(self, samples)->Image.Image: - gen_result = super().sample_to_image(samples).convert('RGB') - - if self.pil_image is None or self.pil_mask is None: - return gen_result - if self.pil_image.size != self.pil_mask.size: - return gen_result - - corrected_result = super(Img2Img, self).repaste_and_color_correct(gen_result, self.pil_image, self.pil_mask, self.mask_blur_radius) - - return corrected_result diff --git a/invokeai/backend/generator/txt2img.py b/invokeai/backend/generator/txt2img.py index 40094a3033..a9918f81ce 100644 --- a/invokeai/backend/generator/txt2img.py +++ b/invokeai/backend/generator/txt2img.py @@ -1,24 +1,41 @@ -''' +""" invokeai.backend.generator.txt2img inherits from invokeai.backend.generator -''' +""" import PIL.Image import torch +from ..stable_diffusion import ( + ConditioningData, + PostprocessingSettings, + StableDiffusionGeneratorPipeline, +) from .base import Generator -from ..stable_diffusion import (PostprocessingSettings, - StableDiffusionGeneratorPipeline, - ConditioningData - ) + class Txt2Img(Generator): def __init__(self, model, precision): super().__init__(model, precision) @torch.no_grad() - def get_make_image(self,prompt,sampler,steps,cfg_scale,ddim_eta, - conditioning,width,height,step_callback=None,threshold=0.0,warmup=0.2,perlin=0.0, - h_symmetry_time_pct=None,v_symmetry_time_pct=None,attention_maps_callback=None, - **kwargs): + def get_make_image( + self, + prompt, + sampler, + steps, + cfg_scale, + ddim_eta, + conditioning, + width, + height, + step_callback=None, + threshold=0.0, + warmup=0.2, + perlin=0.0, + h_symmetry_time_pct=None, + v_symmetry_time_pct=None, + attention_maps_callback=None, + **kwargs, + ): """ Returns a function returning an image derived from the prompt and the initial image Return value depends on the seed at the time you call it @@ -30,33 +47,35 @@ class Txt2Img(Generator): pipeline: StableDiffusionGeneratorPipeline = self.model pipeline.scheduler = sampler - uc, c, extra_conditioning_info = conditioning - conditioning_data = ( - ConditioningData( - uc, c, cfg_scale, extra_conditioning_info, - postprocessing_settings=PostprocessingSettings( - threshold=threshold, - warmup=warmup, - h_symmetry_time_pct=h_symmetry_time_pct, - v_symmetry_time_pct=v_symmetry_time_pct - ) - ).add_scheduler_args_if_applicable(pipeline.scheduler, eta=ddim_eta)) + uc, c, extra_conditioning_info = conditioning + conditioning_data = ConditioningData( + uc, + c, + cfg_scale, + extra_conditioning_info, + postprocessing_settings=PostprocessingSettings( + threshold=threshold, + warmup=warmup, + h_symmetry_time_pct=h_symmetry_time_pct, + v_symmetry_time_pct=v_symmetry_time_pct, + ), + ).add_scheduler_args_if_applicable(pipeline.scheduler, eta=ddim_eta) def make_image(x_T) -> PIL.Image.Image: pipeline_output = pipeline.image_from_embeddings( - latents=torch.zeros_like(x_T,dtype=self.torch_dtype()), + latents=torch.zeros_like(x_T, dtype=self.torch_dtype()), noise=x_T, num_inference_steps=steps, conditioning_data=conditioning_data, callback=step_callback, ) - if pipeline_output.attention_map_saver is not None and attention_maps_callback is not None: + if ( + pipeline_output.attention_map_saver is not None + and attention_maps_callback is not None + ): attention_maps_callback(pipeline_output.attention_map_saver) return pipeline.numpy_to_pil(pipeline_output.images)[0] return make_image - - - diff --git a/invokeai/backend/generator/txt2img2img.py b/invokeai/backend/generator/txt2img2img.py index 67de74fecf..76573b689d 100644 --- a/invokeai/backend/generator/txt2img2img.py +++ b/invokeai/backend/generator/txt2img2img.py @@ -1,6 +1,6 @@ -''' +""" invokeai.backend.generator.txt2img inherits from invokeai.backend.generator -''' +""" import math from typing import Callable, Optional @@ -8,21 +8,40 @@ from typing import Callable, Optional import torch from diffusers.utils.logging import get_verbosity, set_verbosity, set_verbosity_error -from .base import Generator -from .diffusers_pipeline import trim_to_multiple_of, StableDiffusionGeneratorPipeline, \ - ConditioningData from ..models import PostprocessingSettings +from .base import Generator +from .diffusers_pipeline import ( + ConditioningData, + StableDiffusionGeneratorPipeline, + trim_to_multiple_of, +) class Txt2Img2Img(Generator): def __init__(self, model, precision): super().__init__(model, precision) - self.init_latent = None # for get_noise() + self.init_latent = None # for get_noise() - def get_make_image(self, prompt:str, sampler, steps:int, cfg_scale:float, ddim_eta, - conditioning, width:int, height:int, strength:float, - step_callback:Optional[Callable]=None, threshold=0.0, warmup=0.2, perlin=0.0, - h_symmetry_time_pct=None, v_symmetry_time_pct=None, attention_maps_callback=None, **kwargs): + def get_make_image( + self, + prompt: str, + sampler, + steps: int, + cfg_scale: float, + ddim_eta, + conditioning, + width: int, + height: int, + strength: float, + step_callback: Optional[Callable] = None, + threshold=0.0, + warmup=0.2, + perlin=0.0, + h_symmetry_time_pct=None, + v_symmetry_time_pct=None, + attention_maps_callback=None, + **kwargs, + ): """ Returns a function returning an image derived from the prompt and the initial image Return value depends on the seed at the time you call it @@ -35,19 +54,20 @@ class Txt2Img2Img(Generator): pipeline.scheduler = sampler uc, c, extra_conditioning_info = conditioning - conditioning_data = ( - ConditioningData( - uc, c, cfg_scale, extra_conditioning_info, - postprocessing_settings = PostprocessingSettings( - threshold=threshold, - warmup=0.2, - h_symmetry_time_pct=h_symmetry_time_pct, - v_symmetry_time_pct=v_symmetry_time_pct - ) - ).add_scheduler_args_if_applicable(pipeline.scheduler, eta=ddim_eta)) + conditioning_data = ConditioningData( + uc, + c, + cfg_scale, + extra_conditioning_info, + postprocessing_settings=PostprocessingSettings( + threshold=threshold, + warmup=0.2, + h_symmetry_time_pct=h_symmetry_time_pct, + v_symmetry_time_pct=v_symmetry_time_pct, + ), + ).add_scheduler_args_if_applicable(pipeline.scheduler, eta=ddim_eta) def make_image(x_T): - first_pass_latent_output, _ = pipeline.latents_from_embeddings( latents=torch.zeros_like(x_T), num_inference_steps=steps, @@ -61,28 +81,40 @@ class Txt2Img2Img(Generator): init_width = first_pass_latent_output.size()[3] * self.downsampling_factor init_height = first_pass_latent_output.size()[2] * self.downsampling_factor print( - f"\n>> Interpolating from {init_width}x{init_height} to {width}x{height} using DDIM sampling" - ) + f"\n>> Interpolating from {init_width}x{init_height} to {width}x{height} using DDIM sampling" + ) # resizing resized_latents = torch.nn.functional.interpolate( first_pass_latent_output, - size=(height // self.downsampling_factor, width // self.downsampling_factor), - mode="bilinear" + size=( + height // self.downsampling_factor, + width // self.downsampling_factor, + ), + mode="bilinear", ) # Free up memory from the last generation. - clear_cuda_cache = kwargs['clear_cuda_cache'] or None + clear_cuda_cache = kwargs["clear_cuda_cache"] or None if clear_cuda_cache is not None: clear_cuda_cache() - second_pass_noise = self.get_noise_like(resized_latents, override_perlin=True) + second_pass_noise = self.get_noise_like( + resized_latents, override_perlin=True + ) # Clear symmetry for the second pass from dataclasses import replace - new_postprocessing_settings = replace(conditioning_data.postprocessing_settings, h_symmetry_time_pct=None) - new_postprocessing_settings = replace(new_postprocessing_settings, v_symmetry_time_pct=None) - new_conditioning_data = replace(conditioning_data, postprocessing_settings=new_postprocessing_settings) + + new_postprocessing_settings = replace( + conditioning_data.postprocessing_settings, h_symmetry_time_pct=None + ) + new_postprocessing_settings = replace( + new_postprocessing_settings, v_symmetry_time_pct=None + ) + new_conditioning_data = replace( + conditioning_data, postprocessing_settings=new_postprocessing_settings + ) verbosity = get_verbosity() set_verbosity_error() @@ -92,15 +124,18 @@ class Txt2Img2Img(Generator): conditioning_data=new_conditioning_data, strength=strength, noise=second_pass_noise, - callback=step_callback) + callback=step_callback, + ) set_verbosity(verbosity) - if pipeline_output.attention_map_saver is not None and attention_maps_callback is not None: + if ( + pipeline_output.attention_map_saver is not None + and attention_maps_callback is not None + ): attention_maps_callback(pipeline_output.attention_map_saver) return pipeline.numpy_to_pil(pipeline_output.images)[0] - # FIXME: do we really need something entirely different for the inpainting model? # in the case of the inpainting model being loaded, the trick of @@ -111,19 +146,23 @@ class Txt2Img2Img(Generator): return make_image - def get_noise_like(self, like: torch.Tensor, override_perlin: bool=False): + def get_noise_like(self, like: torch.Tensor, override_perlin: bool = False): device = like.device - if device.type == 'mps': - x = torch.randn_like(like, device='cpu', dtype=self.torch_dtype()).to(device) + if device.type == "mps": + x = torch.randn_like(like, device="cpu", dtype=self.torch_dtype()).to( + device + ) else: x = torch.randn_like(like, device=device, dtype=self.torch_dtype()) if self.perlin > 0.0 and override_perlin == False: shape = like.shape - x = (1-self.perlin)*x + self.perlin*self.get_perlin_noise(shape[3], shape[2]) + x = (1 - self.perlin) * x + self.perlin * self.get_perlin_noise( + shape[3], shape[2] + ) return x # returns a tensor filled with random numbers from a normal distribution - def get_noise(self,width,height,scale = True): + def get_noise(self, width, height, scale=True): # print(f"Get noise: {width}x{height}") if scale: # Scale the input width and height for the initial generation @@ -133,7 +172,9 @@ class Txt2Img2Img(Generator): aspect = width / height dimension = self.model.unet.config.sample_size * self.model.vae_scale_factor min_dimension = math.floor(dimension * 0.5) - model_area = dimension * dimension # hardcoded for now since all models are trained on square images + model_area = ( + dimension * dimension + ) # hardcoded for now since all models are trained on square images if aspect > 1.0: init_height = max(min_dimension, math.sqrt(model_area / aspect)) @@ -142,7 +183,9 @@ class Txt2Img2Img(Generator): init_width = max(min_dimension, math.sqrt(model_area * aspect)) init_height = init_width / aspect - scaled_width, scaled_height = trim_to_multiple_of(math.floor(init_width), math.floor(init_height)) + scaled_width, scaled_height = trim_to_multiple_of( + math.floor(init_width), math.floor(init_height) + ) else: scaled_width = width @@ -152,10 +195,14 @@ class Txt2Img2Img(Generator): channels = self.latent_channels if channels == 9: channels = 4 # we don't really want noise for all the mask channels - shape = (1, channels, - scaled_height // self.downsampling_factor, scaled_width // self.downsampling_factor) - if self.use_mps_noise or device.type == 'mps': - tensor = torch.empty(size=shape, device='cpu') + shape = ( + 1, + channels, + scaled_height // self.downsampling_factor, + scaled_width // self.downsampling_factor, + ) + if self.use_mps_noise or device.type == "mps": + tensor = torch.empty(size=shape, device="cpu") tensor = self.get_noise_like(like=tensor).to(device) else: tensor = torch.empty(size=shape, device=device) diff --git a/invokeai/backend/globals.py b/invokeai/backend/globals.py index f41160a39b..5ccc3a6a1a 100644 --- a/invokeai/backend/globals.py +++ b/invokeai/backend/globals.py @@ -1,4 +1,4 @@ -''' +""" invokeai.backend.globals defines a small number of global variables that would otherwise have to be passed through long and complex call chains. @@ -9,7 +9,7 @@ the attributes: - initfile - path to the initialization file - try_patchmatch - option to globally disable loading of 'patchmatch' module - always_use_cpu - force use of CPU even if GPU is available -''' +""" import os import os.path as osp @@ -20,12 +20,12 @@ from typing import Union Globals = Namespace() # Where to look for the initialization file and other key components -Globals.initfile = 'invokeai.init' -Globals.models_file = 'models.yaml' -Globals.models_dir = 'models' -Globals.config_dir = 'configs' -Globals.autoscan_dir = 'weights' -Globals.converted_ckpts_dir = 'converted_ckpts' +Globals.initfile = "invokeai.init" +Globals.models_file = "models.yaml" +Globals.models_dir = "models" +Globals.config_dir = "configs" +Globals.autoscan_dir = "weights" +Globals.converted_ckpts_dir = "converted_ckpts" # Set the default root directory. This can be overwritten by explicitly # passing the `--root ` argument on the command line. @@ -34,12 +34,15 @@ Globals.converted_ckpts_dir = 'converted_ckpts' # 2) use VIRTUAL_ENV environment variable, with a check for initfile being there # 3) use ~/invokeai -if os.environ.get('INVOKEAI_ROOT'): - Globals.root = osp.abspath(os.environ.get('INVOKEAI_ROOT')) -elif os.environ.get('VIRTUAL_ENV') and Path(os.environ.get('VIRTUAL_ENV'),'..',Globals.initfile).exists(): - Globals.root = osp.abspath(osp.join(os.environ.get('VIRTUAL_ENV'), '..')) +if os.environ.get("INVOKEAI_ROOT"): + Globals.root = osp.abspath(os.environ.get("INVOKEAI_ROOT")) +elif ( + os.environ.get("VIRTUAL_ENV") + and Path(os.environ.get("VIRTUAL_ENV"), "..", Globals.initfile).exists() +): + Globals.root = osp.abspath(osp.join(os.environ.get("VIRTUAL_ENV"), "..")) else: - Globals.root = osp.abspath(osp.expanduser('~/invokeai')) + Globals.root = osp.abspath(osp.expanduser("~/invokeai")) # Try loading patchmatch Globals.try_patchmatch = True @@ -66,26 +69,33 @@ Globals.ckpt_convert = True # logging tokenization everywhere Globals.log_tokenization = False -def global_config_file()->Path: + +def global_config_file() -> Path: return Path(Globals.root, Globals.config_dir, Globals.models_file) -def global_config_dir()->Path: + +def global_config_dir() -> Path: return Path(Globals.root, Globals.config_dir) -def global_models_dir()->Path: + +def global_models_dir() -> Path: return Path(Globals.root, Globals.models_dir) -def global_autoscan_dir()->Path: + +def global_autoscan_dir() -> Path: return Path(Globals.root, Globals.autoscan_dir) -def global_converted_ckpts_dir()->Path: + +def global_converted_ckpts_dir() -> Path: return Path(global_models_dir(), Globals.converted_ckpts_dir) -def global_set_root(root_dir:Union[str,Path]): + +def global_set_root(root_dir: Union[str, Path]): Globals.root = root_dir -def global_cache_dir(subdir:Union[str,Path]='')->Path: - ''' + +def global_cache_dir(subdir: Union[str, Path] = "") -> Path: + """ Returns Path to the model cache directory. If a subdirectory is provided, it will be appended to the end of the path, allowing for huggingface-style conventions: @@ -98,18 +108,18 @@ def global_cache_dir(subdir:Union[str,Path]='')->Path: One other caveat is that HuggingFace is moving some diffusers models into the "hub" subdirectory as well, so this will need to be revisited from time to time. - ''' - home: str = os.getenv('HF_HOME') + """ + home: str = os.getenv("HF_HOME") if home is None: - home = os.getenv('XDG_CACHE_HOME') + home = os.getenv("XDG_CACHE_HOME") if home is not None: # Set `home` to $XDG_CACHE_HOME/huggingface, which is the default location mentioned in HuggingFace Hub Client Library. # See: https://huggingface.co/docs/huggingface_hub/main/en/package_reference/environment_variables#xdgcachehome - home += os.sep + 'huggingface' + home += os.sep + "huggingface" if home is not None: - return Path(home,subdir) + return Path(home, subdir) else: - return Path(Globals.root,'models',subdir) + return Path(Globals.root, "models", subdir) diff --git a/invokeai/backend/image_util/__init__.py b/invokeai/backend/image_util/__init__.py index 3b55f094d7..410f003f6a 100644 --- a/invokeai/backend/image_util/__init__.py +++ b/invokeai/backend/image_util/__init__.py @@ -1,15 +1,12 @@ -''' +""" Initialization file for invokeai.backend.image_util methods. -''' +""" from .patchmatch import PatchMatch +from .pngwriter import PngWriter, PromptFormatter, retrieve_metadata, write_metadata +from .seamless import configure_model_padding from .txt2mask import Txt2Mask from .util import InitImageResizer, make_grid -from .pngwriter import (PngWriter, - PromptFormatter, - retrieve_metadata, - write_metadata, - ) -from .seamless import configure_model_padding + def debug_image( debug_image, debug_text, debug_show=True, debug_result=False, debug_status=False @@ -25,5 +22,3 @@ def debug_image( if debug_result: return image_copy - - diff --git a/invokeai/backend/image_util/patchmatch.py b/invokeai/backend/image_util/patchmatch.py index 1c27a1d67d..8753298f51 100644 --- a/invokeai/backend/image_util/patchmatch.py +++ b/invokeai/backend/image_util/patchmatch.py @@ -1,20 +1,22 @@ -''' +""" This module defines a singleton object, "patchmatch" that wraps the actual patchmatch object. It respects the global "try_patchmatch" attribute, so that patchmatch loading can be suppressed or deferred -''' +""" +import numpy as np + from invokeai.backend.globals import Globals -import numpy as np + class PatchMatch: - ''' + """ Thin class wrapper around the patchmatch function. - ''' + """ patch_match = None - tried_load:bool = False - + tried_load: bool = False + def __init__(self): super().__init__() @@ -24,21 +26,22 @@ class PatchMatch: return if Globals.try_patchmatch: from patchmatch import patch_match as pm + if pm.patchmatch_available: - print('>> Patchmatch initialized') + print(">> Patchmatch initialized") else: - print('>> Patchmatch not loaded (nonfatal)') + print(">> Patchmatch not loaded (nonfatal)") self.patch_match = pm else: - print('>> Patchmatch loading disabled') + print(">> Patchmatch loading disabled") self.tried_load = True @classmethod - def patchmatch_available(self)->bool: + def patchmatch_available(self) -> bool: self._load_patch_match() return self.patch_match and self.patch_match.patchmatch_available @classmethod - def inpaint(self,*args,**kwargs)->np.ndarray: + def inpaint(self, *args, **kwargs) -> np.ndarray: if self.patchmatch_available(): - return self.patch_match.inpaint(*args,**kwargs) + return self.patch_match.inpaint(*args, **kwargs) diff --git a/invokeai/backend/image_util/pngwriter.py b/invokeai/backend/image_util/pngwriter.py index c022c62870..452bbfc783 100644 --- a/invokeai/backend/image_util/pngwriter.py +++ b/invokeai/backend/image_util/pngwriter.py @@ -6,10 +6,11 @@ PngWriter -- Converts Images generated by T2I into PNGs, finds Exports function retrieve_metadata(path) """ +import json import os import re -import json -from PIL import PngImagePlugin, Image + +from PIL import Image, PngImagePlugin # -------------------image generation utils----- @@ -25,52 +26,57 @@ class PngWriter: dirlist = sorted(os.listdir(self.outdir), reverse=True) # find the first filename that matches our pattern or return 000000.0.png existing_name = next( - (f for f in dirlist if re.match('^(\d+)\..*\.png', f)), - '0000000.0.png', + (f for f in dirlist if re.match("^(\d+)\..*\.png", f)), + "0000000.0.png", ) - basecount = int(existing_name.split('.', 1)[0]) + 1 - return f'{basecount:06}' + basecount = int(existing_name.split(".", 1)[0]) + 1 + return f"{basecount:06}" # saves image named _image_ to outdir/name, writing metadata from prompt # returns full path of output - def save_image_and_prompt_to_png(self, image, dream_prompt, name, metadata=None, compress_level=6): + def save_image_and_prompt_to_png( + self, image, dream_prompt, name, metadata=None, compress_level=6 + ): path = os.path.join(self.outdir, name) info = PngImagePlugin.PngInfo() - info.add_text('Dream', dream_prompt) + info.add_text("Dream", dream_prompt) if metadata: - info.add_text('sd-metadata', json.dumps(metadata)) - image.save(path, 'PNG', pnginfo=info, compress_level=compress_level) + info.add_text("sd-metadata", json.dumps(metadata)) + image.save(path, "PNG", pnginfo=info, compress_level=compress_level) return path - def retrieve_metadata(self,img_basename): - ''' + def retrieve_metadata(self, img_basename): + """ Given a PNG filename stored in outdir, returns the "sd-metadata" metadata stored there, as a dict - ''' - path = os.path.join(self.outdir,img_basename) + """ + path = os.path.join(self.outdir, img_basename) all_metadata = retrieve_metadata(path) - return all_metadata['sd-metadata'] + return all_metadata["sd-metadata"] + def retrieve_metadata(img_path): - ''' + """ Given a path to a PNG image, returns the "sd-metadata" metadata stored there, as a dict - ''' + """ im = Image.open(img_path) - if hasattr(im, 'text'): - md = im.text.get('sd-metadata', '{}') - dream_prompt = im.text.get('Dream', '') + if hasattr(im, "text"): + md = im.text.get("sd-metadata", "{}") + dream_prompt = im.text.get("Dream", "") else: # When trying to retrieve metadata from images without a 'text' payload, such as JPG images. - md = '{}' - dream_prompt = '' - return {'sd-metadata': json.loads(md), 'Dream': dream_prompt} + md = "{}" + dream_prompt = "" + return {"sd-metadata": json.loads(md), "Dream": dream_prompt} -def write_metadata(img_path:str, meta:dict): + +def write_metadata(img_path: str, meta: dict): im = Image.open(img_path) info = PngImagePlugin.PngInfo() - info.add_text('sd-metadata', json.dumps(meta)) - im.save(img_path,'PNG',pnginfo=info) + info.add_text("sd-metadata", json.dumps(meta)) + im.save(img_path, "PNG", pnginfo=info) + class PromptFormatter: def __init__(self, t2i, opt): @@ -86,28 +92,30 @@ class PromptFormatter: switches = list() switches.append(f'"{opt.prompt}"') - switches.append(f'-s{opt.steps or t2i.steps}') - switches.append(f'-W{opt.width or t2i.width}') - switches.append(f'-H{opt.height or t2i.height}') - switches.append(f'-C{opt.cfg_scale or t2i.cfg_scale}') - switches.append(f'-A{opt.sampler_name or t2i.sampler_name}') -# to do: put model name into the t2i object -# switches.append(f'--model{t2i.model_name}') + switches.append(f"-s{opt.steps or t2i.steps}") + switches.append(f"-W{opt.width or t2i.width}") + switches.append(f"-H{opt.height or t2i.height}") + switches.append(f"-C{opt.cfg_scale or t2i.cfg_scale}") + switches.append(f"-A{opt.sampler_name or t2i.sampler_name}") + # to do: put model name into the t2i object + # switches.append(f'--model{t2i.model_name}') if opt.seamless or t2i.seamless: - switches.append(f'--seamless') + switches.append(f"--seamless") if opt.init_img: - switches.append(f'-I{opt.init_img}') + switches.append(f"-I{opt.init_img}") if opt.fit: - switches.append(f'--fit') + switches.append(f"--fit") if opt.strength and opt.init_img is not None: - switches.append(f'-f{opt.strength or t2i.strength}') + switches.append(f"-f{opt.strength or t2i.strength}") if opt.gfpgan_strength: - switches.append(f'-G{opt.gfpgan_strength}') + switches.append(f"-G{opt.gfpgan_strength}") if opt.upscale: switches.append(f'-U {" ".join([str(u) for u in opt.upscale])}') if opt.variation_amount > 0: - switches.append(f'-v{opt.variation_amount}') + switches.append(f"-v{opt.variation_amount}") if opt.with_variations: - formatted_variations = ','.join(f'{seed}:{weight}' for seed, weight in opt.with_variations) - switches.append(f'-V{formatted_variations}') - return ' '.join(switches) + formatted_variations = ",".join( + f"{seed}:{weight}" for seed, weight in opt.with_variations + ) + switches.append(f"-V{formatted_variations}") + return " ".join(switches) diff --git a/invokeai/backend/image_util/seamless.py b/invokeai/backend/image_util/seamless.py index fda363eb7b..4fbc0cd78e 100644 --- a/invokeai/backend/image_util/seamless.py +++ b/invokeai/backend/image_util/seamless.py @@ -1,12 +1,26 @@ import torch.nn as nn + def _conv_forward_asymmetric(self, input, weight, bias): """ Patch for Conv2d._conv_forward that supports asymmetric padding """ - working = nn.functional.pad(input, self.asymmetric_padding['x'], mode=self.asymmetric_padding_mode['x']) - working = nn.functional.pad(working, self.asymmetric_padding['y'], mode=self.asymmetric_padding_mode['y']) - return nn.functional.conv2d(working, weight, bias, self.stride, nn.modules.utils._pair(0), self.dilation, self.groups) + working = nn.functional.pad( + input, self.asymmetric_padding["x"], mode=self.asymmetric_padding_mode["x"] + ) + working = nn.functional.pad( + working, self.asymmetric_padding["y"], mode=self.asymmetric_padding_mode["y"] + ) + return nn.functional.conv2d( + working, + weight, + bias, + self.stride, + nn.modules.utils._pair(0), + self.dilation, + self.groups, + ) + def configure_model_padding(model, seamless, seamless_axes): """ @@ -18,14 +32,28 @@ def configure_model_padding(model, seamless, seamless_axes): if seamless: m.asymmetric_padding_mode = {} m.asymmetric_padding = {} - m.asymmetric_padding_mode['x'] = 'circular' if ('x' in seamless_axes) else 'constant' - m.asymmetric_padding['x'] = (m._reversed_padding_repeated_twice[0], m._reversed_padding_repeated_twice[1], 0, 0) - m.asymmetric_padding_mode['y'] = 'circular' if ('y' in seamless_axes) else 'constant' - m.asymmetric_padding['y'] = (0, 0, m._reversed_padding_repeated_twice[2], m._reversed_padding_repeated_twice[3]) + m.asymmetric_padding_mode["x"] = ( + "circular" if ("x" in seamless_axes) else "constant" + ) + m.asymmetric_padding["x"] = ( + m._reversed_padding_repeated_twice[0], + m._reversed_padding_repeated_twice[1], + 0, + 0, + ) + m.asymmetric_padding_mode["y"] = ( + "circular" if ("y" in seamless_axes) else "constant" + ) + m.asymmetric_padding["y"] = ( + 0, + 0, + m._reversed_padding_repeated_twice[2], + m._reversed_padding_repeated_twice[3], + ) m._conv_forward = _conv_forward_asymmetric.__get__(m, nn.Conv2d) else: m._conv_forward = nn.Conv2d._conv_forward.__get__(m, nn.Conv2d) - if hasattr(m, 'asymmetric_padding_mode'): + if hasattr(m, "asymmetric_padding_mode"): del m.asymmetric_padding_mode - if hasattr(m, 'asymmetric_padding'): + if hasattr(m, "asymmetric_padding"): del m.asymmetric_padding diff --git a/invokeai/backend/image_util/txt2mask.py b/invokeai/backend/image_util/txt2mask.py index 6e64df3f31..bc7e56d397 100644 --- a/invokeai/backend/image_util/txt2mask.py +++ b/invokeai/backend/image_util/txt2mask.py @@ -1,9 +1,9 @@ -'''Makes available the Txt2Mask class, which assists in the automatic +"""Makes available the Txt2Mask class, which assists in the automatic assignment of masks via text prompt using clipseg. Here is typical usage: - from ldm.invoke.txt2mask import Txt2Mask, SegmentedGrayscale + from invokeai.backend.image_util.txt2mask import Txt2Mask, SegmentedGrayscale from PIL import Image txt2mask = Txt2Mask(self.device) @@ -25,31 +25,39 @@ the mask that exceed the indicated confidence threshold. Values range from 0.0 to 1.0. The higher the threshold, the more confident the algorithm is. In limited testing, I have found that values around 0.5 work fine. -''' +""" +import numpy as np import torch -import numpy as np -from transformers import AutoProcessor, CLIPSegForImageSegmentation from PIL import Image, ImageOps from torchvision import transforms +from transformers import AutoProcessor, CLIPSegForImageSegmentation + from invokeai.backend.globals import global_cache_dir -CLIPSEG_MODEL = 'CIDAS/clipseg-rd64-refined' +CLIPSEG_MODEL = "CIDAS/clipseg-rd64-refined" CLIPSEG_SIZE = 352 + class SegmentedGrayscale(object): - def __init__(self, image:Image, heatmap:torch.Tensor): + def __init__(self, image: Image, heatmap: torch.Tensor): self.heatmap = heatmap self.image = image - def to_grayscale(self,invert:bool=False)->Image: - return self._rescale(Image.fromarray(np.uint8(255 - self.heatmap * 255 if invert else self.heatmap * 255))) + def to_grayscale(self, invert: bool = False) -> Image: + return self._rescale( + Image.fromarray( + np.uint8(255 - self.heatmap * 255 if invert else self.heatmap * 255) + ) + ) - def to_mask(self,threshold:float=0.5)->Image: + def to_mask(self, threshold: float = 0.5) -> Image: discrete_heatmap = self.heatmap.lt(threshold).int() - return self._rescale(Image.fromarray(np.uint8(discrete_heatmap*255),mode='L')) + return self._rescale( + Image.fromarray(np.uint8(discrete_heatmap * 255), mode="L") + ) - def to_transparent(self,invert:bool=False)->Image: + def to_transparent(self, invert: bool = False) -> Image: transparent_image = self.image.copy() # For img2img, we want the selected regions to be transparent, # but to_grayscale() returns the opposite. Thus invert. @@ -58,70 +66,77 @@ class SegmentedGrayscale(object): return transparent_image # unscales and uncrops the 352x352 heatmap so that it matches the image again - def _rescale(self, heatmap:Image)->Image: - size = self.image.width if (self.image.width > self.image.height) else self.image.height - resized_image = heatmap.resize( - (size,size), - resample=Image.Resampling.LANCZOS + def _rescale(self, heatmap: Image) -> Image: + size = ( + self.image.width + if (self.image.width > self.image.height) + else self.image.height ) - return resized_image.crop((0,0,self.image.width,self.image.height)) + resized_image = heatmap.resize((size, size), resample=Image.Resampling.LANCZOS) + return resized_image.crop((0, 0, self.image.width, self.image.height)) + class Txt2Mask(object): - ''' + """ Create new Txt2Mask object. The optional device argument can be one of 'cuda', 'mps' or 'cpu'. - ''' - def __init__(self,device='cpu',refined=False): - print('>> Initializing clipseg model for text to mask inference') + """ + + def __init__(self, device="cpu", refined=False): + print(">> Initializing clipseg model for text to mask inference") # BUG: we are not doing anything with the device option at this time self.device = device - self.processor = AutoProcessor.from_pretrained(CLIPSEG_MODEL, - cache_dir=global_cache_dir('hub') - ) - self.model = CLIPSegForImageSegmentation.from_pretrained(CLIPSEG_MODEL, - cache_dir=global_cache_dir('hub') - ) + self.processor = AutoProcessor.from_pretrained( + CLIPSEG_MODEL, cache_dir=global_cache_dir("hub") + ) + self.model = CLIPSegForImageSegmentation.from_pretrained( + CLIPSEG_MODEL, cache_dir=global_cache_dir("hub") + ) @torch.no_grad() - def segment(self, image, prompt:str) -> SegmentedGrayscale: - ''' + def segment(self, image, prompt: str) -> SegmentedGrayscale: + """ Given a prompt string such as "a bagel", tries to identify the object in the provided image and returns a SegmentedGrayscale object in which the brighter pixels indicate where the object is inferred to be. - ''' - transform = transforms.Compose([ - transforms.ToTensor(), - transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), - transforms.Resize((CLIPSEG_SIZE, CLIPSEG_SIZE)), # must be multiple of 64... - ]) + """ + transform = transforms.Compose( + [ + transforms.ToTensor(), + transforms.Normalize( + mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] + ), + transforms.Resize( + (CLIPSEG_SIZE, CLIPSEG_SIZE) + ), # must be multiple of 64... + ] + ) if type(image) is str: - image = Image.open(image).convert('RGB') + image = Image.open(image).convert("RGB") image = ImageOps.exif_transpose(image) img = self._scale_and_crop(image) - inputs = self.processor(text=[prompt], - images=[img], - padding=True, - return_tensors='pt') + inputs = self.processor( + text=[prompt], images=[img], padding=True, return_tensors="pt" + ) outputs = self.model(**inputs) heatmap = torch.sigmoid(outputs.logits) return SegmentedGrayscale(image, heatmap) - def _scale_and_crop(self, image:Image)->Image: - scaled_image = Image.new('RGB',(CLIPSEG_SIZE,CLIPSEG_SIZE)) - if image.width > image.height: # width is constraint + def _scale_and_crop(self, image: Image) -> Image: + scaled_image = Image.new("RGB", (CLIPSEG_SIZE, CLIPSEG_SIZE)) + if image.width > image.height: # width is constraint scale = CLIPSEG_SIZE / image.width else: scale = CLIPSEG_SIZE / image.height scaled_image.paste( image.resize( - (int(scale * image.width), - int(scale * image.height) - ), - resample=Image.Resampling.LANCZOS - ),box=(0,0) + (int(scale * image.width), int(scale * image.height)), + resample=Image.Resampling.LANCZOS, + ), + box=(0, 0), ) return scaled_image diff --git a/invokeai/backend/image_util/util.py b/invokeai/backend/image_util/util.py index 3fb509b3cd..bc7fa01e3b 100644 --- a/invokeai/backend/image_util/util.py +++ b/invokeai/backend/image_util/util.py @@ -1,12 +1,15 @@ -from math import sqrt, floor, ceil +from math import ceil, floor, sqrt + from PIL import Image -class InitImageResizer(): + +class InitImageResizer: """Simple class to create resized copies of an Image while preserving the aspect ratio.""" - def __init__(self,Image): + + def __init__(self, Image): self.image = Image - def resize(self,width=None,height=None) -> Image: + def resize(self, width=None, height=None) -> Image: """ Return a copy of the image resized to fit within a box width x height. The aspect ratio is @@ -18,37 +21,36 @@ class InitImageResizer(): Everything is floored to the nearest multiple of 64 so that it can be passed to img2img() """ - im = self.image + im = self.image - ar = im.width/float(im.height) + ar = im.width / float(im.height) # Infer missing values from aspect ratio - if not(width or height): # both missing - width = im.width + if not (width or height): # both missing + width = im.width height = im.height - elif not height: # height missing - height = int(width/ar) - elif not width: # width missing - width = int(height*ar) + elif not height: # height missing + height = int(width / ar) + elif not width: # width missing + width = int(height * ar) - w_scale = width/im.width - h_scale = height/im.height - scale = min(w_scale,h_scale) - (rw,rh) = (int(scale*im.width),int(scale*im.height)) + w_scale = width / im.width + h_scale = height / im.height + scale = min(w_scale, h_scale) + (rw, rh) = (int(scale * im.width), int(scale * im.height)) - #round everything to multiples of 64 - width,height,rw,rh = map( - lambda x: x-x%64, (width,height,rw,rh) - ) + # round everything to multiples of 64 + width, height, rw, rh = map(lambda x: x - x % 64, (width, height, rw, rh)) # no resize necessary, but return a copy if im.width == width and im.height == height: return im.copy() # otherwise resize the original image so that it fits inside the bounding box - resized_image = self.image.resize((rw,rh),resample=Image.Resampling.LANCZOS) + resized_image = self.image.resize((rw, rh), resample=Image.Resampling.LANCZOS) return resized_image + def make_grid(image_list, rows=None, cols=None): image_cnt = len(image_list) if None in (rows, cols): @@ -57,7 +59,7 @@ def make_grid(image_list, rows=None, cols=None): width = image_list[0].width height = image_list[0].height - grid_img = Image.new('RGB', (width * cols, height * rows)) + grid_img = Image.new("RGB", (width * cols, height * rows)) i = 0 for r in range(0, rows): for c in range(0, cols): @@ -67,4 +69,3 @@ def make_grid(image_list, rows=None, cols=None): i = i + 1 return grid_img - diff --git a/invokeai/backend/model_management/__init__.py b/invokeai/backend/model_management/__init__.py index e83527123f..39411a853a 100644 --- a/invokeai/backend/model_management/__init__.py +++ b/invokeai/backend/model_management/__init__.py @@ -1,8 +1,8 @@ -''' +""" Initialization file for invokeai.backend.model_management -''' +""" +from .convert_ckpt_to_diffusers import ( + convert_ckpt_to_diffusers, + load_pipeline_from_original_stable_diffusion_ckpt, +) from .model_manager import ModelManager -from .convert_ckpt_to_diffusers import (load_pipeline_from_original_stable_diffusion_ckpt, - convert_ckpt_to_diffusers) -from ...frontend.merge.merge_diffusers import (merge_diffusion_models, - merge_diffusion_models_and_commit) diff --git a/invokeai/backend/model_management/convert_ckpt_to_diffusers.py b/invokeai/backend/model_management/convert_ckpt_to_diffusers.py index 395432c1e6..c0c1cca3c4 100644 --- a/invokeai/backend/model_management/convert_ckpt_to_diffusers.py +++ b/invokeai/backend/model_management/convert_ckpt_to_diffusers.py @@ -18,17 +18,17 @@ """ Conversion script for the LDM checkpoints. """ import re -import torch import warnings from pathlib import Path -from invokeai.backend.globals import ( - global_cache_dir, - global_config_dir, - ) -from .model_manager import ModelManager, SDLegacyType -from safetensors.torch import load_file from typing import Union +import torch +from safetensors.torch import load_file + +from invokeai.backend.globals import global_cache_dir, global_config_dir + +from .model_manager import ModelManager, SDLegacyType + try: from omegaconf import OmegaConf except ImportError: @@ -48,16 +48,31 @@ from diffusers import ( PNDMScheduler, StableDiffusionPipeline, UNet2DConditionModel, - logging as dlogging, ) -from diffusers.pipelines.latent_diffusion.pipeline_latent_diffusion import LDMBertConfig, LDMBertModel -from diffusers.pipelines.paint_by_example import PaintByExampleImageEncoder, PaintByExamplePipeline -from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker +from diffusers import logging as dlogging +from diffusers.pipelines.latent_diffusion.pipeline_latent_diffusion import ( + LDMBertConfig, + LDMBertModel, +) +from diffusers.pipelines.paint_by_example import ( + PaintByExampleImageEncoder, + PaintByExamplePipeline, +) +from diffusers.pipelines.stable_diffusion.safety_checker import ( + StableDiffusionSafetyChecker, +) from diffusers.utils import is_safetensors_available -from transformers import AutoFeatureExtractor, BertTokenizerFast, CLIPTextModel, CLIPTokenizer, CLIPVisionConfig +from transformers import ( + AutoFeatureExtractor, + BertTokenizerFast, + CLIPTextModel, + CLIPTokenizer, + CLIPVisionConfig, +) from ..stable_diffusion import StableDiffusionGeneratorPipeline + def shave_segments(path, n_shave_prefix_segments=1): """ Removes segments. Positive values shave the first segments, negative shave the last segments. @@ -83,7 +98,9 @@ def renew_resnet_paths(old_list, n_shave_prefix_segments=0): new_item = new_item.replace("emb_layers.1", "time_emb_proj") new_item = new_item.replace("skip_connection", "conv_shortcut") - new_item = shave_segments(new_item, n_shave_prefix_segments=n_shave_prefix_segments) + new_item = shave_segments( + new_item, n_shave_prefix_segments=n_shave_prefix_segments + ) mapping.append({"old": old_item, "new": new_item}) @@ -99,7 +116,9 @@ def renew_vae_resnet_paths(old_list, n_shave_prefix_segments=0): new_item = old_item new_item = new_item.replace("nin_shortcut", "conv_shortcut") - new_item = shave_segments(new_item, n_shave_prefix_segments=n_shave_prefix_segments) + new_item = shave_segments( + new_item, n_shave_prefix_segments=n_shave_prefix_segments + ) mapping.append({"old": old_item, "new": new_item}) @@ -150,7 +169,9 @@ def renew_vae_attention_paths(old_list, n_shave_prefix_segments=0): new_item = new_item.replace("proj_out.weight", "proj_attn.weight") new_item = new_item.replace("proj_out.bias", "proj_attn.bias") - new_item = shave_segments(new_item, n_shave_prefix_segments=n_shave_prefix_segments) + new_item = shave_segments( + new_item, n_shave_prefix_segments=n_shave_prefix_segments + ) mapping.append({"old": old_item, "new": new_item}) @@ -158,7 +179,12 @@ def renew_vae_attention_paths(old_list, n_shave_prefix_segments=0): def assign_to_checkpoint( - paths, checkpoint, old_checkpoint, attention_paths_to_split=None, additional_replacements=None, config=None + paths, + checkpoint, + old_checkpoint, + attention_paths_to_split=None, + additional_replacements=None, + config=None, ): """ This does the final conversion step: take locally converted weights and apply a global renaming @@ -167,7 +193,9 @@ def assign_to_checkpoint( Assigns the weights to the new checkpoint. """ - assert isinstance(paths, list), "Paths should be a list of dicts containing 'old' and 'new' keys." + assert isinstance( + paths, list + ), "Paths should be a list of dicts containing 'old' and 'new' keys." # Splits the attention layers into three variables. if attention_paths_to_split is not None: @@ -179,7 +207,9 @@ def assign_to_checkpoint( num_heads = old_tensor.shape[0] // config["num_head_channels"] // 3 - old_tensor = old_tensor.reshape((num_heads, 3 * channels // num_heads) + old_tensor.shape[1:]) + old_tensor = old_tensor.reshape( + (num_heads, 3 * channels // num_heads) + old_tensor.shape[1:] + ) query, key, value = old_tensor.split(channels // num_heads, dim=1) checkpoint[path_map["query"]] = query.reshape(target_shape) @@ -190,7 +220,10 @@ def assign_to_checkpoint( new_path = path["new"] # These have already been assigned - if attention_paths_to_split is not None and new_path in attention_paths_to_split: + if ( + attention_paths_to_split is not None + and new_path in attention_paths_to_split + ): continue # Global renaming happens here @@ -228,19 +261,29 @@ def create_unet_diffusers_config(original_config, image_size: int): unet_params = original_config.model.params.unet_config.params vae_params = original_config.model.params.first_stage_config.params.ddconfig - block_out_channels = [unet_params.model_channels * mult for mult in unet_params.channel_mult] + block_out_channels = [ + unet_params.model_channels * mult for mult in unet_params.channel_mult + ] down_block_types = [] resolution = 1 for i in range(len(block_out_channels)): - block_type = "CrossAttnDownBlock2D" if resolution in unet_params.attention_resolutions else "DownBlock2D" + block_type = ( + "CrossAttnDownBlock2D" + if resolution in unet_params.attention_resolutions + else "DownBlock2D" + ) down_block_types.append(block_type) if i != len(block_out_channels) - 1: resolution *= 2 up_block_types = [] for i in range(len(block_out_channels)): - block_type = "CrossAttnUpBlock2D" if resolution in unet_params.attention_resolutions else "UpBlock2D" + block_type = ( + "CrossAttnUpBlock2D" + if resolution in unet_params.attention_resolutions + else "UpBlock2D" + ) up_block_types.append(block_type) resolution //= 2 @@ -248,7 +291,9 @@ def create_unet_diffusers_config(original_config, image_size: int): head_dim = unet_params.num_heads if "num_heads" in unet_params else None use_linear_projection = ( - unet_params.use_linear_in_transformer if "use_linear_in_transformer" in unet_params else False + unet_params.use_linear_in_transformer + if "use_linear_in_transformer" in unet_params + else False ) if use_linear_projection: # stable diffusion 2-base-512 and 2-768 @@ -329,16 +374,16 @@ def convert_ldm_unet_checkpoint(checkpoint, config, path=None, extract_ema=False if sum(k.startswith("model_ema") for k in keys) > 100: print(f" | Checkpoint {path} has both EMA and non-EMA weights.") if extract_ema: - print( - ' | Extracting EMA weights (usually better for inference)' - ) + print(" | Extracting EMA weights (usually better for inference)") for key in keys: if key.startswith("model.diffusion_model"): flat_ema_key = "model_ema." + "".join(key.split(".")[1:]) - unet_state_dict[key.replace(unet_key, "")] = checkpoint.pop(flat_ema_key) + unet_state_dict[key.replace(unet_key, "")] = checkpoint.pop( + flat_ema_key + ) else: print( - ' | Extracting only the non-EMA weights (usually better for fine-tuning)' + " | Extracting only the non-EMA weights (usually better for fine-tuning)" ) for key in keys: @@ -347,10 +392,18 @@ def convert_ldm_unet_checkpoint(checkpoint, config, path=None, extract_ema=False new_checkpoint = {} - new_checkpoint["time_embedding.linear_1.weight"] = unet_state_dict["time_embed.0.weight"] - new_checkpoint["time_embedding.linear_1.bias"] = unet_state_dict["time_embed.0.bias"] - new_checkpoint["time_embedding.linear_2.weight"] = unet_state_dict["time_embed.2.weight"] - new_checkpoint["time_embedding.linear_2.bias"] = unet_state_dict["time_embed.2.bias"] + new_checkpoint["time_embedding.linear_1.weight"] = unet_state_dict[ + "time_embed.0.weight" + ] + new_checkpoint["time_embedding.linear_1.bias"] = unet_state_dict[ + "time_embed.0.bias" + ] + new_checkpoint["time_embedding.linear_2.weight"] = unet_state_dict[ + "time_embed.2.weight" + ] + new_checkpoint["time_embedding.linear_2.bias"] = unet_state_dict[ + "time_embed.2.bias" + ] new_checkpoint["conv_in.weight"] = unet_state_dict["input_blocks.0.0.weight"] new_checkpoint["conv_in.bias"] = unet_state_dict["input_blocks.0.0.bias"] @@ -361,21 +414,39 @@ def convert_ldm_unet_checkpoint(checkpoint, config, path=None, extract_ema=False new_checkpoint["conv_out.bias"] = unet_state_dict["out.2.bias"] # Retrieves the keys for the input blocks only - num_input_blocks = len({".".join(layer.split(".")[:2]) for layer in unet_state_dict if "input_blocks" in layer}) + num_input_blocks = len( + { + ".".join(layer.split(".")[:2]) + for layer in unet_state_dict + if "input_blocks" in layer + } + ) input_blocks = { layer_id: [key for key in unet_state_dict if f"input_blocks.{layer_id}" in key] for layer_id in range(num_input_blocks) } # Retrieves the keys for the middle blocks only - num_middle_blocks = len({".".join(layer.split(".")[:2]) for layer in unet_state_dict if "middle_block" in layer}) + num_middle_blocks = len( + { + ".".join(layer.split(".")[:2]) + for layer in unet_state_dict + if "middle_block" in layer + } + ) middle_blocks = { layer_id: [key for key in unet_state_dict if f"middle_block.{layer_id}" in key] for layer_id in range(num_middle_blocks) } # Retrieves the keys for the output blocks only - num_output_blocks = len({".".join(layer.split(".")[:2]) for layer in unet_state_dict if "output_blocks" in layer}) + num_output_blocks = len( + { + ".".join(layer.split(".")[:2]) + for layer in unet_state_dict + if "output_blocks" in layer + } + ) output_blocks = { layer_id: [key for key in unet_state_dict if f"output_blocks.{layer_id}" in key] for layer_id in range(num_output_blocks) @@ -386,29 +457,45 @@ def convert_ldm_unet_checkpoint(checkpoint, config, path=None, extract_ema=False layer_in_block_id = (i - 1) % (config["layers_per_block"] + 1) resnets = [ - key for key in input_blocks[i] if f"input_blocks.{i}.0" in key and f"input_blocks.{i}.0.op" not in key + key + for key in input_blocks[i] + if f"input_blocks.{i}.0" in key and f"input_blocks.{i}.0.op" not in key ] attentions = [key for key in input_blocks[i] if f"input_blocks.{i}.1" in key] if f"input_blocks.{i}.0.op.weight" in unet_state_dict: - new_checkpoint[f"down_blocks.{block_id}.downsamplers.0.conv.weight"] = unet_state_dict.pop( - f"input_blocks.{i}.0.op.weight" - ) - new_checkpoint[f"down_blocks.{block_id}.downsamplers.0.conv.bias"] = unet_state_dict.pop( - f"input_blocks.{i}.0.op.bias" - ) + new_checkpoint[ + f"down_blocks.{block_id}.downsamplers.0.conv.weight" + ] = unet_state_dict.pop(f"input_blocks.{i}.0.op.weight") + new_checkpoint[ + f"down_blocks.{block_id}.downsamplers.0.conv.bias" + ] = unet_state_dict.pop(f"input_blocks.{i}.0.op.bias") paths = renew_resnet_paths(resnets) - meta_path = {"old": f"input_blocks.{i}.0", "new": f"down_blocks.{block_id}.resnets.{layer_in_block_id}"} + meta_path = { + "old": f"input_blocks.{i}.0", + "new": f"down_blocks.{block_id}.resnets.{layer_in_block_id}", + } assign_to_checkpoint( - paths, new_checkpoint, unet_state_dict, additional_replacements=[meta_path], config=config + paths, + new_checkpoint, + unet_state_dict, + additional_replacements=[meta_path], + config=config, ) if len(attentions): paths = renew_attention_paths(attentions) - meta_path = {"old": f"input_blocks.{i}.1", "new": f"down_blocks.{block_id}.attentions.{layer_in_block_id}"} + meta_path = { + "old": f"input_blocks.{i}.1", + "new": f"down_blocks.{block_id}.attentions.{layer_in_block_id}", + } assign_to_checkpoint( - paths, new_checkpoint, unet_state_dict, additional_replacements=[meta_path], config=config + paths, + new_checkpoint, + unet_state_dict, + additional_replacements=[meta_path], + config=config, ) resnet_0 = middle_blocks[0] @@ -424,7 +511,11 @@ def convert_ldm_unet_checkpoint(checkpoint, config, path=None, extract_ema=False attentions_paths = renew_attention_paths(attentions) meta_path = {"old": "middle_block.1", "new": "mid_block.attentions.0"} assign_to_checkpoint( - attentions_paths, new_checkpoint, unet_state_dict, additional_replacements=[meta_path], config=config + attentions_paths, + new_checkpoint, + unet_state_dict, + additional_replacements=[meta_path], + config=config, ) for i in range(num_output_blocks): @@ -442,25 +533,36 @@ def convert_ldm_unet_checkpoint(checkpoint, config, path=None, extract_ema=False if len(output_block_list) > 1: resnets = [key for key in output_blocks[i] if f"output_blocks.{i}.0" in key] - attentions = [key for key in output_blocks[i] if f"output_blocks.{i}.1" in key] + attentions = [ + key for key in output_blocks[i] if f"output_blocks.{i}.1" in key + ] resnet_0_paths = renew_resnet_paths(resnets) paths = renew_resnet_paths(resnets) - meta_path = {"old": f"output_blocks.{i}.0", "new": f"up_blocks.{block_id}.resnets.{layer_in_block_id}"} + meta_path = { + "old": f"output_blocks.{i}.0", + "new": f"up_blocks.{block_id}.resnets.{layer_in_block_id}", + } assign_to_checkpoint( - paths, new_checkpoint, unet_state_dict, additional_replacements=[meta_path], config=config + paths, + new_checkpoint, + unet_state_dict, + additional_replacements=[meta_path], + config=config, ) output_block_list = {k: sorted(v) for k, v in output_block_list.items()} if ["conv.bias", "conv.weight"] in output_block_list.values(): - index = list(output_block_list.values()).index(["conv.bias", "conv.weight"]) - new_checkpoint[f"up_blocks.{block_id}.upsamplers.0.conv.weight"] = unet_state_dict[ - f"output_blocks.{i}.{index}.conv.weight" - ] - new_checkpoint[f"up_blocks.{block_id}.upsamplers.0.conv.bias"] = unet_state_dict[ - f"output_blocks.{i}.{index}.conv.bias" - ] + index = list(output_block_list.values()).index( + ["conv.bias", "conv.weight"] + ) + new_checkpoint[ + f"up_blocks.{block_id}.upsamplers.0.conv.weight" + ] = unet_state_dict[f"output_blocks.{i}.{index}.conv.weight"] + new_checkpoint[ + f"up_blocks.{block_id}.upsamplers.0.conv.bias" + ] = unet_state_dict[f"output_blocks.{i}.{index}.conv.bias"] # Clear attentions as they have been attributed above. if len(attentions) == 2: @@ -473,13 +575,27 @@ def convert_ldm_unet_checkpoint(checkpoint, config, path=None, extract_ema=False "new": f"up_blocks.{block_id}.attentions.{layer_in_block_id}", } assign_to_checkpoint( - paths, new_checkpoint, unet_state_dict, additional_replacements=[meta_path], config=config + paths, + new_checkpoint, + unet_state_dict, + additional_replacements=[meta_path], + config=config, ) else: - resnet_0_paths = renew_resnet_paths(output_block_layers, n_shave_prefix_segments=1) + resnet_0_paths = renew_resnet_paths( + output_block_layers, n_shave_prefix_segments=1 + ) for path in resnet_0_paths: old_path = ".".join(["output_blocks", str(i), path["old"]]) - new_path = ".".join(["up_blocks", str(block_id), "resnets", str(layer_in_block_id), path["new"]]) + new_path = ".".join( + [ + "up_blocks", + str(block_id), + "resnets", + str(layer_in_block_id), + path["new"], + ] + ) new_checkpoint[new_path] = unet_state_dict[old_path] @@ -499,17 +615,29 @@ def convert_ldm_vae_checkpoint(checkpoint, config): new_checkpoint["encoder.conv_in.weight"] = vae_state_dict["encoder.conv_in.weight"] new_checkpoint["encoder.conv_in.bias"] = vae_state_dict["encoder.conv_in.bias"] - new_checkpoint["encoder.conv_out.weight"] = vae_state_dict["encoder.conv_out.weight"] + new_checkpoint["encoder.conv_out.weight"] = vae_state_dict[ + "encoder.conv_out.weight" + ] new_checkpoint["encoder.conv_out.bias"] = vae_state_dict["encoder.conv_out.bias"] - new_checkpoint["encoder.conv_norm_out.weight"] = vae_state_dict["encoder.norm_out.weight"] - new_checkpoint["encoder.conv_norm_out.bias"] = vae_state_dict["encoder.norm_out.bias"] + new_checkpoint["encoder.conv_norm_out.weight"] = vae_state_dict[ + "encoder.norm_out.weight" + ] + new_checkpoint["encoder.conv_norm_out.bias"] = vae_state_dict[ + "encoder.norm_out.bias" + ] new_checkpoint["decoder.conv_in.weight"] = vae_state_dict["decoder.conv_in.weight"] new_checkpoint["decoder.conv_in.bias"] = vae_state_dict["decoder.conv_in.bias"] - new_checkpoint["decoder.conv_out.weight"] = vae_state_dict["decoder.conv_out.weight"] + new_checkpoint["decoder.conv_out.weight"] = vae_state_dict[ + "decoder.conv_out.weight" + ] new_checkpoint["decoder.conv_out.bias"] = vae_state_dict["decoder.conv_out.bias"] - new_checkpoint["decoder.conv_norm_out.weight"] = vae_state_dict["decoder.norm_out.weight"] - new_checkpoint["decoder.conv_norm_out.bias"] = vae_state_dict["decoder.norm_out.bias"] + new_checkpoint["decoder.conv_norm_out.weight"] = vae_state_dict[ + "decoder.norm_out.weight" + ] + new_checkpoint["decoder.conv_norm_out.bias"] = vae_state_dict[ + "decoder.norm_out.bias" + ] new_checkpoint["quant_conv.weight"] = vae_state_dict["quant_conv.weight"] new_checkpoint["quant_conv.bias"] = vae_state_dict["quant_conv.bias"] @@ -517,31 +645,55 @@ def convert_ldm_vae_checkpoint(checkpoint, config): new_checkpoint["post_quant_conv.bias"] = vae_state_dict["post_quant_conv.bias"] # Retrieves the keys for the encoder down blocks only - num_down_blocks = len({".".join(layer.split(".")[:3]) for layer in vae_state_dict if "encoder.down" in layer}) + num_down_blocks = len( + { + ".".join(layer.split(".")[:3]) + for layer in vae_state_dict + if "encoder.down" in layer + } + ) down_blocks = { - layer_id: [key for key in vae_state_dict if f"down.{layer_id}" in key] for layer_id in range(num_down_blocks) + layer_id: [key for key in vae_state_dict if f"down.{layer_id}" in key] + for layer_id in range(num_down_blocks) } # Retrieves the keys for the decoder up blocks only - num_up_blocks = len({".".join(layer.split(".")[:3]) for layer in vae_state_dict if "decoder.up" in layer}) + num_up_blocks = len( + { + ".".join(layer.split(".")[:3]) + for layer in vae_state_dict + if "decoder.up" in layer + } + ) up_blocks = { - layer_id: [key for key in vae_state_dict if f"up.{layer_id}" in key] for layer_id in range(num_up_blocks) + layer_id: [key for key in vae_state_dict if f"up.{layer_id}" in key] + for layer_id in range(num_up_blocks) } for i in range(num_down_blocks): - resnets = [key for key in down_blocks[i] if f"down.{i}" in key and f"down.{i}.downsample" not in key] + resnets = [ + key + for key in down_blocks[i] + if f"down.{i}" in key and f"down.{i}.downsample" not in key + ] if f"encoder.down.{i}.downsample.conv.weight" in vae_state_dict: - new_checkpoint[f"encoder.down_blocks.{i}.downsamplers.0.conv.weight"] = vae_state_dict.pop( - f"encoder.down.{i}.downsample.conv.weight" - ) - new_checkpoint[f"encoder.down_blocks.{i}.downsamplers.0.conv.bias"] = vae_state_dict.pop( - f"encoder.down.{i}.downsample.conv.bias" - ) + new_checkpoint[ + f"encoder.down_blocks.{i}.downsamplers.0.conv.weight" + ] = vae_state_dict.pop(f"encoder.down.{i}.downsample.conv.weight") + new_checkpoint[ + f"encoder.down_blocks.{i}.downsamplers.0.conv.bias" + ] = vae_state_dict.pop(f"encoder.down.{i}.downsample.conv.bias") paths = renew_vae_resnet_paths(resnets) meta_path = {"old": f"down.{i}.block", "new": f"down_blocks.{i}.resnets"} - assign_to_checkpoint(paths, new_checkpoint, vae_state_dict, additional_replacements=[meta_path], config=config) + assign_to_checkpoint( + paths, + new_checkpoint, + vae_state_dict, + additional_replacements=[meta_path], + config=config, + ) mid_resnets = [key for key in vae_state_dict if "encoder.mid.block" in key] num_mid_res_blocks = 2 @@ -550,31 +702,51 @@ def convert_ldm_vae_checkpoint(checkpoint, config): paths = renew_vae_resnet_paths(resnets) meta_path = {"old": f"mid.block_{i}", "new": f"mid_block.resnets.{i - 1}"} - assign_to_checkpoint(paths, new_checkpoint, vae_state_dict, additional_replacements=[meta_path], config=config) + assign_to_checkpoint( + paths, + new_checkpoint, + vae_state_dict, + additional_replacements=[meta_path], + config=config, + ) mid_attentions = [key for key in vae_state_dict if "encoder.mid.attn" in key] paths = renew_vae_attention_paths(mid_attentions) meta_path = {"old": "mid.attn_1", "new": "mid_block.attentions.0"} - assign_to_checkpoint(paths, new_checkpoint, vae_state_dict, additional_replacements=[meta_path], config=config) + assign_to_checkpoint( + paths, + new_checkpoint, + vae_state_dict, + additional_replacements=[meta_path], + config=config, + ) conv_attn_to_linear(new_checkpoint) for i in range(num_up_blocks): block_id = num_up_blocks - 1 - i resnets = [ - key for key in up_blocks[block_id] if f"up.{block_id}" in key and f"up.{block_id}.upsample" not in key + key + for key in up_blocks[block_id] + if f"up.{block_id}" in key and f"up.{block_id}.upsample" not in key ] if f"decoder.up.{block_id}.upsample.conv.weight" in vae_state_dict: - new_checkpoint[f"decoder.up_blocks.{i}.upsamplers.0.conv.weight"] = vae_state_dict[ - f"decoder.up.{block_id}.upsample.conv.weight" - ] - new_checkpoint[f"decoder.up_blocks.{i}.upsamplers.0.conv.bias"] = vae_state_dict[ - f"decoder.up.{block_id}.upsample.conv.bias" - ] + new_checkpoint[ + f"decoder.up_blocks.{i}.upsamplers.0.conv.weight" + ] = vae_state_dict[f"decoder.up.{block_id}.upsample.conv.weight"] + new_checkpoint[ + f"decoder.up_blocks.{i}.upsamplers.0.conv.bias" + ] = vae_state_dict[f"decoder.up.{block_id}.upsample.conv.bias"] paths = renew_vae_resnet_paths(resnets) meta_path = {"old": f"up.{block_id}.block", "new": f"up_blocks.{i}.resnets"} - assign_to_checkpoint(paths, new_checkpoint, vae_state_dict, additional_replacements=[meta_path], config=config) + assign_to_checkpoint( + paths, + new_checkpoint, + vae_state_dict, + additional_replacements=[meta_path], + config=config, + ) mid_resnets = [key for key in vae_state_dict if "decoder.mid.block" in key] num_mid_res_blocks = 2 @@ -583,12 +755,24 @@ def convert_ldm_vae_checkpoint(checkpoint, config): paths = renew_vae_resnet_paths(resnets) meta_path = {"old": f"mid.block_{i}", "new": f"mid_block.resnets.{i - 1}"} - assign_to_checkpoint(paths, new_checkpoint, vae_state_dict, additional_replacements=[meta_path], config=config) + assign_to_checkpoint( + paths, + new_checkpoint, + vae_state_dict, + additional_replacements=[meta_path], + config=config, + ) mid_attentions = [key for key in vae_state_dict if "decoder.mid.attn" in key] paths = renew_vae_attention_paths(mid_attentions) meta_path = {"old": "mid.attn_1", "new": "mid_block.attentions.0"} - assign_to_checkpoint(paths, new_checkpoint, vae_state_dict, additional_replacements=[meta_path], config=config) + assign_to_checkpoint( + paths, + new_checkpoint, + vae_state_dict, + additional_replacements=[meta_path], + config=config, + ) conv_attn_to_linear(new_checkpoint) return new_checkpoint @@ -630,7 +814,9 @@ def convert_ldm_bert_checkpoint(checkpoint, config): # copy embeds hf_model.model.embed_tokens.weight = checkpoint.transformer.token_emb.weight - hf_model.model.embed_positions.weight.data = checkpoint.transformer.pos_emb.emb.weight + hf_model.model.embed_positions.weight.data = ( + checkpoint.transformer.pos_emb.emb.weight + ) # copy layer norm _copy_linear(hf_model.model.layer_norm, checkpoint.transformer.norm) @@ -644,7 +830,9 @@ def convert_ldm_bert_checkpoint(checkpoint, config): def convert_ldm_clip_checkpoint(checkpoint): - text_model = CLIPTextModel.from_pretrained("openai/clip-vit-large-patch14",cache_dir=global_cache_dir('hub')) + text_model = CLIPTextModel.from_pretrained( + "openai/clip-vit-large-patch14", cache_dir=global_cache_dir("hub") + ) keys = list(checkpoint.keys()) @@ -652,7 +840,9 @@ def convert_ldm_clip_checkpoint(checkpoint): for key in keys: if key.startswith("cond_stage_model.transformer"): - text_model_dict[key[len("cond_stage_model.transformer.") :]] = checkpoint[key] + text_model_dict[key[len("cond_stage_model.transformer.") :]] = checkpoint[ + key + ] text_model.load_state_dict(text_model_dict) @@ -660,8 +850,14 @@ def convert_ldm_clip_checkpoint(checkpoint): textenc_conversion_lst = [ - ("cond_stage_model.model.positional_embedding", "text_model.embeddings.position_embedding.weight"), - ("cond_stage_model.model.token_embedding.weight", "text_model.embeddings.token_embedding.weight"), + ( + "cond_stage_model.model.positional_embedding", + "text_model.embeddings.position_embedding.weight", + ), + ( + "cond_stage_model.model.token_embedding.weight", + "text_model.embeddings.token_embedding.weight", + ), ("cond_stage_model.model.ln_final.weight", "text_model.final_layer_norm.weight"), ("cond_stage_model.model.ln_final.bias", "text_model.final_layer_norm.bias"), ] @@ -676,16 +872,24 @@ textenc_transformer_conversion_lst = [ (".c_proj.", ".fc2."), (".attn", ".self_attn"), ("ln_final.", "transformer.text_model.final_layer_norm."), - ("token_embedding.weight", "transformer.text_model.embeddings.token_embedding.weight"), - ("positional_embedding", "transformer.text_model.embeddings.position_embedding.weight"), + ( + "token_embedding.weight", + "transformer.text_model.embeddings.token_embedding.weight", + ), + ( + "positional_embedding", + "transformer.text_model.embeddings.position_embedding.weight", + ), ] protected = {re.escape(x[0]): x[1] for x in textenc_transformer_conversion_lst} textenc_pattern = re.compile("|".join(protected.keys())) def convert_paint_by_example_checkpoint(checkpoint): - cache_dir = global_cache_dir('hub') - config = CLIPVisionConfig.from_pretrained("openai/clip-vit-large-patch14",cache_dir=cache_dir) + cache_dir = global_cache_dir("hub") + config = CLIPVisionConfig.from_pretrained( + "openai/clip-vit-large-patch14", cache_dir=cache_dir + ) model = PaintByExampleImageEncoder(config) keys = list(checkpoint.keys()) @@ -694,7 +898,9 @@ def convert_paint_by_example_checkpoint(checkpoint): for key in keys: if key.startswith("cond_stage_model.transformer"): - text_model_dict[key[len("cond_stage_model.transformer.") :]] = checkpoint[key] + text_model_dict[key[len("cond_stage_model.transformer.") :]] = checkpoint[ + key + ] # load clip vision model.model.load_state_dict(text_model_dict) @@ -752,24 +958,32 @@ def convert_paint_by_example_checkpoint(checkpoint): def convert_open_clip_checkpoint(checkpoint): - cache_dir=global_cache_dir('hub') - text_model = CLIPTextModel.from_pretrained("stabilityai/stable-diffusion-2", subfolder="text_encoder", cache_dir=cache_dir) + cache_dir = global_cache_dir("hub") + text_model = CLIPTextModel.from_pretrained( + "stabilityai/stable-diffusion-2", subfolder="text_encoder", cache_dir=cache_dir + ) keys = list(checkpoint.keys()) text_model_dict = {} - if 'cond_stage_model.model.text_projection' in keys: + if "cond_stage_model.model.text_projection" in keys: d_model = int(checkpoint["cond_stage_model.model.text_projection"].shape[0]) - elif 'cond_stage_model.model.ln_final.bias' in keys: - d_model = int(checkpoint['cond_stage_model.model.ln_final.bias'].shape[0]) + elif "cond_stage_model.model.ln_final.bias" in keys: + d_model = int(checkpoint["cond_stage_model.model.ln_final.bias"].shape[0]) else: - raise KeyError('Expected key "cond_stage_model.model.text_projection" not found in model') + raise KeyError( + 'Expected key "cond_stage_model.model.text_projection" not found in model' + ) - text_model_dict["text_model.embeddings.position_ids"] = text_model.text_model.embeddings.get_buffer("position_ids") + text_model_dict[ + "text_model.embeddings.position_ids" + ] = text_model.text_model.embeddings.get_buffer("position_ids") for key in keys: - if "resblocks.23" in key: # Diffusers drops the final layer and only uses the penultimate layer + if ( + "resblocks.23" in key + ): # Diffusers drops the final layer and only uses the penultimate layer continue if key in textenc_conversion_map: text_model_dict[textenc_conversion_map[key]] = checkpoint[key] @@ -777,18 +991,34 @@ def convert_open_clip_checkpoint(checkpoint): new_key = key[len("cond_stage_model.model.transformer.") :] if new_key.endswith(".in_proj_weight"): new_key = new_key[: -len(".in_proj_weight")] - new_key = textenc_pattern.sub(lambda m: protected[re.escape(m.group(0))], new_key) - text_model_dict[new_key + ".q_proj.weight"] = checkpoint[key][:d_model, :] - text_model_dict[new_key + ".k_proj.weight"] = checkpoint[key][d_model : d_model * 2, :] - text_model_dict[new_key + ".v_proj.weight"] = checkpoint[key][d_model * 2 :, :] + new_key = textenc_pattern.sub( + lambda m: protected[re.escape(m.group(0))], new_key + ) + text_model_dict[new_key + ".q_proj.weight"] = checkpoint[key][ + :d_model, : + ] + text_model_dict[new_key + ".k_proj.weight"] = checkpoint[key][ + d_model : d_model * 2, : + ] + text_model_dict[new_key + ".v_proj.weight"] = checkpoint[key][ + d_model * 2 :, : + ] elif new_key.endswith(".in_proj_bias"): new_key = new_key[: -len(".in_proj_bias")] - new_key = textenc_pattern.sub(lambda m: protected[re.escape(m.group(0))], new_key) + new_key = textenc_pattern.sub( + lambda m: protected[re.escape(m.group(0))], new_key + ) text_model_dict[new_key + ".q_proj.bias"] = checkpoint[key][:d_model] - text_model_dict[new_key + ".k_proj.bias"] = checkpoint[key][d_model : d_model * 2] - text_model_dict[new_key + ".v_proj.bias"] = checkpoint[key][d_model * 2 :] + text_model_dict[new_key + ".k_proj.bias"] = checkpoint[key][ + d_model : d_model * 2 + ] + text_model_dict[new_key + ".v_proj.bias"] = checkpoint[key][ + d_model * 2 : + ] else: - new_key = textenc_pattern.sub(lambda m: protected[re.escape(m.group(0))], new_key) + new_key = textenc_pattern.sub( + lambda m: protected[re.escape(m.group(0))], new_key + ) text_model_dict[new_key] = checkpoint[key] @@ -796,21 +1026,22 @@ def convert_open_clip_checkpoint(checkpoint): return text_model + def load_pipeline_from_original_stable_diffusion_ckpt( - checkpoint_path:str, - original_config_file:str=None, - num_in_channels:int=None, - scheduler_type:str='pndm', - pipeline_type:str=None, - image_size:int=None, - prediction_type:str=None, - extract_ema:bool=True, - upcast_attn:bool=False, - vae:AutoencoderKL=None, - precision:torch.dtype=torch.float32, - return_generator_pipeline:bool=False, -)->Union[StableDiffusionPipeline,StableDiffusionGeneratorPipeline]: - ''' + checkpoint_path: str, + original_config_file: str = None, + num_in_channels: int = None, + scheduler_type: str = "pndm", + pipeline_type: str = None, + image_size: int = None, + prediction_type: str = None, + extract_ema: bool = True, + upcast_attn: bool = False, + vae: AutoencoderKL = None, + precision: torch.dtype = torch.float32, + return_generator_pipeline: bool = False, +) -> Union[StableDiffusionPipeline, StableDiffusionGeneratorPipeline]: + """ Load a Stable Diffusion pipeline object from a CompVis-style `.ckpt`/`.safetensors` file and (ideally) a `.yaml` config file. @@ -818,15 +1049,15 @@ def load_pipeline_from_original_stable_diffusion_ckpt( global step count, which will likely fail for models that have undergone further fine-tuning. Therefore, it is recommended that you override the default values and/or supply an `original_config_file` wherever possible. - :param checkpoint_path: Path to `.ckpt` file. - :param original_config_file: Path to `.yaml` config file corresponding to the original architecture. + :param checkpoint_path: Path to `.ckpt` file. + :param original_config_file: Path to `.yaml` config file corresponding to the original architecture. If `None`, will be automatically inferred by looking for a key that only exists in SD2.0 models. :param image_size: The image size that the model was trained on. Use 512 for Stable Diffusion v1.X and Stable Diffusion v2 Base. Use 768 for Stable Diffusion v2. :param prediction_type: The prediction type that the model was trained on. Use `'epsilon'` for Stable Diffusion v1.X and Stable Diffusion v2 Base. Use `'v-prediction'` for Stable Diffusion v2. :param num_in_channels: The number of input channels. If `None` number of input channels will be automatically - inferred. + inferred. :param scheduler_type: Type of scheduler to use. Should be one of `["pndm", "lms", "heun", "euler", "euler-ancestral", "dpm", "ddim"]`. :param model_type: The pipeline type. `None` to automatically infer, or one of `["FrozenOpenCLIPEmbedder", "FrozenCLIPEmbedder", "PaintByExample"]`. :param extract_ema: Only relevant for @@ -836,16 +1067,24 @@ def load_pipeline_from_original_stable_diffusion_ckpt( :param precision: precision to use - torch.float16, torch.float32 or torch.autocast :param upcast_attention: Whether the attention computation should always be upcasted. This is necessary when running stable diffusion 2.1. - ''' + """ with warnings.catch_warnings(): - warnings.simplefilter('ignore') + warnings.simplefilter("ignore") verbosity = dlogging.get_verbosity() dlogging.set_verbosity_error() - checkpoint = load_file(checkpoint_path) if Path(checkpoint_path).suffix == '.safetensors' else torch.load(checkpoint_path) - cache_dir = global_cache_dir('hub') - pipeline_class = StableDiffusionGeneratorPipeline if return_generator_pipeline else StableDiffusionPipeline + checkpoint = ( + load_file(checkpoint_path) + if Path(checkpoint_path).suffix == ".safetensors" + else torch.load(checkpoint_path) + ) + cache_dir = global_cache_dir("hub") + pipeline_class = ( + StableDiffusionGeneratorPipeline + if return_generator_pipeline + else StableDiffusionPipeline + ) # Sometimes models don't have the global_step item if "global_step" in checkpoint: @@ -855,32 +1094,42 @@ def load_pipeline_from_original_stable_diffusion_ckpt( global_step = None # sometimes there is a state_dict key and sometimes not - if 'state_dict' in checkpoint: + if "state_dict" in checkpoint: checkpoint = checkpoint["state_dict"] upcast_attention = False if original_config_file is None: model_type = ModelManager.probe_model_type(checkpoint) - + if model_type == SDLegacyType.V2: - original_config_file = global_config_dir() / 'stable-diffusion' / 'v2-inference-v.yaml' + original_config_file = ( + global_config_dir() / "stable-diffusion" / "v2-inference-v.yaml" + ) if global_step == 110000: # v2.1 needs to upcast attention upcast_attention = True - + elif model_type == SDLegacyType.V1_INPAINT: - original_config_file = global_config_dir() / 'stable-diffusion' / 'v1-inpainting-inference.yaml' - + original_config_file = ( + global_config_dir() + / "stable-diffusion" + / "v1-inpainting-inference.yaml" + ) + elif model_type == SDLegacyType.V1: - original_config_file = global_config_dir() / 'stable-diffusion' / 'v1-inference.yaml' + original_config_file = ( + global_config_dir() / "stable-diffusion" / "v1-inference.yaml" + ) else: - raise Exception('Unknown checkpoint type') + raise Exception("Unknown checkpoint type") original_config = OmegaConf.load(original_config_file) if num_in_channels is not None: - original_config["model"]["params"]["unet_config"]["params"]["in_channels"] = num_in_channels + original_config["model"]["params"]["unet_config"]["params"][ + "in_channels" + ] = num_in_channels if ( "parameterization" in original_config["model"]["params"] @@ -937,7 +1186,9 @@ def load_pipeline_from_original_stable_diffusion_ckpt( raise ValueError(f"Scheduler of type {scheduler_type} doesn't exist!") # Convert the UNet2DConditionModel model. - unet_config = create_unet_diffusers_config(original_config, image_size=image_size) + unet_config = create_unet_diffusers_config( + original_config, image_size=image_size + ) unet_config["upcast_attention"] = upcast_attention unet = UNet2DConditionModel(**unet_config) @@ -949,26 +1200,33 @@ def load_pipeline_from_original_stable_diffusion_ckpt( # Convert the VAE model, or use the one passed if not vae: - print(' | Using checkpoint model\'s original VAE') - vae_config = create_vae_diffusers_config(original_config, image_size=image_size) - converted_vae_checkpoint = convert_ldm_vae_checkpoint(checkpoint, vae_config) + print(" | Using checkpoint model's original VAE") + vae_config = create_vae_diffusers_config( + original_config, image_size=image_size + ) + converted_vae_checkpoint = convert_ldm_vae_checkpoint( + checkpoint, vae_config + ) vae = AutoencoderKL(**vae_config) vae.load_state_dict(converted_vae_checkpoint) else: - print(' | Using external VAE specified in config') + print(" | Using external VAE specified in config") # Convert the text model. model_type = pipeline_type if model_type is None: - model_type = original_config.model.params.cond_stage_config.target.split(".")[-1] + model_type = original_config.model.params.cond_stage_config.target.split( + "." + )[-1] if model_type == "FrozenOpenCLIPEmbedder": text_model = convert_open_clip_checkpoint(checkpoint) - tokenizer = CLIPTokenizer.from_pretrained("stabilityai/stable-diffusion-2", - subfolder="tokenizer", - cache_dir=cache_dir, - ) + tokenizer = CLIPTokenizer.from_pretrained( + "stabilityai/stable-diffusion-2", + subfolder="tokenizer", + cache_dir=cache_dir, + ) pipe = pipeline_class( vae=vae, text_encoder=text_model, @@ -981,8 +1239,12 @@ def load_pipeline_from_original_stable_diffusion_ckpt( ) elif model_type == "PaintByExample": vision_model = convert_paint_by_example_checkpoint(checkpoint) - tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-large-patch14",cache_dir=cache_dir) - feature_extractor = AutoFeatureExtractor.from_pretrained("CompVis/stable-diffusion-safety-checker",cache_dir=cache_dir) + tokenizer = CLIPTokenizer.from_pretrained( + "openai/clip-vit-large-patch14", cache_dir=cache_dir + ) + feature_extractor = AutoFeatureExtractor.from_pretrained( + "CompVis/stable-diffusion-safety-checker", cache_dir=cache_dir + ) pipe = PaintByExamplePipeline( vae=vae, image_encoder=vision_model, @@ -991,11 +1253,18 @@ def load_pipeline_from_original_stable_diffusion_ckpt( safety_checker=None, feature_extractor=feature_extractor, ) - elif model_type in ['FrozenCLIPEmbedder','WeightedFrozenCLIPEmbedder']: + elif model_type in ["FrozenCLIPEmbedder", "WeightedFrozenCLIPEmbedder"]: text_model = convert_ldm_clip_checkpoint(checkpoint) - tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-large-patch14",cache_dir=cache_dir) - safety_checker = StableDiffusionSafetyChecker.from_pretrained('CompVis/stable-diffusion-safety-checker',cache_dir=global_cache_dir("hub")) - feature_extractor = AutoFeatureExtractor.from_pretrained("CompVis/stable-diffusion-safety-checker",cache_dir=cache_dir) + tokenizer = CLIPTokenizer.from_pretrained( + "openai/clip-vit-large-patch14", cache_dir=cache_dir + ) + safety_checker = StableDiffusionSafetyChecker.from_pretrained( + "CompVis/stable-diffusion-safety-checker", + cache_dir=global_cache_dir("hub"), + ) + feature_extractor = AutoFeatureExtractor.from_pretrained( + "CompVis/stable-diffusion-safety-checker", cache_dir=cache_dir + ) pipe = pipeline_class( vae=vae.to(precision), text_encoder=text_model.to(precision), @@ -1008,27 +1277,33 @@ def load_pipeline_from_original_stable_diffusion_ckpt( else: text_config = create_ldm_bert_config(original_config) text_model = convert_ldm_bert_checkpoint(checkpoint, text_config) - tokenizer = BertTokenizerFast.from_pretrained("bert-base-uncased",cache_dir=cache_dir) - pipe = LDMTextToImagePipeline(vqvae=vae, bert=text_model, tokenizer=tokenizer, unet=unet, scheduler=scheduler) + tokenizer = BertTokenizerFast.from_pretrained( + "bert-base-uncased", cache_dir=cache_dir + ) + pipe = LDMTextToImagePipeline( + vqvae=vae, + bert=text_model, + tokenizer=tokenizer, + unet=unet, + scheduler=scheduler, + ) dlogging.set_verbosity(verbosity) return pipe + def convert_ckpt_to_diffusers( - checkpoint_path:Union[str,Path], - dump_path:Union[str,Path], - **kwargs, + checkpoint_path: Union[str, Path], + dump_path: Union[str, Path], + **kwargs, ): - ''' + """ Takes all the arguments of load_pipeline_from_original_stable_diffusion_ckpt(), and in addition a path-like object indicating the location of the desired diffusers model to be written. - ''' - pipe = load_pipeline_from_original_stable_diffusion_ckpt( - checkpoint_path, - **kwargs - ) - + """ + pipe = load_pipeline_from_original_stable_diffusion_ckpt(checkpoint_path, **kwargs) + pipe.save_pretrained( dump_path, safe_serialization=is_safetensors_available(), diff --git a/invokeai/backend/model_management/model_manager.py b/invokeai/backend/model_management/model_manager.py index 34a10969cd..450043d369 100644 --- a/invokeai/backend/model_management/model_manager.py +++ b/invokeai/backend/model_management/model_manager.py @@ -31,13 +31,11 @@ from omegaconf import OmegaConf from omegaconf.dictconfig import DictConfig from picklescan.scanner import scan_file_path -from ..util import CPU_DEVICE from invokeai.backend.globals import Globals, global_cache_dir -from ..util import ( - ask_user, - download_with_resume, -) + from ..stable_diffusion import StableDiffusionGeneratorPipeline +from ..util import CPU_DEVICE, ask_user, download_with_resume + class SDLegacyType(Enum): V1 = 1 @@ -45,11 +43,13 @@ class SDLegacyType(Enum): V2 = 3 UNKNOWN = 99 + DEFAULT_MAX_MODELS = 2 VAE_TO_REPO_ID = { # hack, see note in convert_and_import() "vae-ft-mse-840000-ema-pruned": "stabilityai/sd-vae-ft-mse", } + class ModelManager(object): def __init__( self, @@ -428,11 +428,9 @@ class ModelManager(object): weights = os.path.normpath(os.path.join(Globals.root, weights)) # Convert to diffusers and return a diffusers pipeline - print( - f">> Converting legacy checkpoint {model_name} into a diffusers model..." - ) - - from . import load_pipeline_from_original_stable_diffusion_ckpt + print(f">> Converting legacy checkpoint {model_name} into a diffusers model...") + + from . import load_pipeline_from_original_stable_diffusion_ckpt self.offload_model(self.current_model) if vae_config := self._choose_diffusers_vae(model_name): @@ -444,9 +442,7 @@ class ModelManager(object): original_config_file=config, vae=vae, return_generator_pipeline=True, - precision=torch.float16 - if self.precision == "float16" - else torch.float32, + precision=torch.float16 if self.precision == "float16" else torch.float32, ) if self.sequential_offload: pipeline.enable_offload_submodels(self.device) @@ -547,7 +543,9 @@ class ModelManager(object): models.yaml file. """ model_name = model_name or Path(repo_or_path).stem - model_description = model_description or f"Imported diffusers model {model_name}" + model_description = ( + model_description or f"Imported diffusers model {model_name}" + ) new_config = dict( description=model_description, vae=vae, @@ -729,7 +727,7 @@ class ModelManager(object): f"** {thing} is a legacy checkpoint file but not in a known Stable Diffusion model. Skipping import" ) return - + diffuser_path = Path( Globals.root, "models", Globals.converted_ckpts_dir, model_path.stem ) @@ -781,7 +779,7 @@ class ModelManager(object): # By passing the specified VAE to the conversion function, the autoencoder # will be built into the model rather than tacked on afterward via the config file vae_model = self._load_vae(vae) if vae else None - convert_ckpt_to_diffusers ( + convert_ckpt_to_diffusers( ckpt_path, diffusers_path, extract_ema=True, diff --git a/invokeai/backend/prompting/__init__.py b/invokeai/backend/prompting/__init__.py index 29694c6538..152edf646b 100644 --- a/invokeai/backend/prompting/__init__.py +++ b/invokeai/backend/prompting/__init__.py @@ -1,7 +1,10 @@ -''' +""" Initialization file for invokeai.backend.prompting -''' -from .conditioning import (get_uc_and_c_and_ec, - split_weighted_subprompts, - get_tokens_for_prompt_object, - get_prompt_structure, get_tokenizer) +""" +from .conditioning import ( + get_prompt_structure, + get_tokenizer, + get_tokens_for_prompt_object, + get_uc_and_c_and_ec, + split_weighted_subprompts, +) diff --git a/invokeai/backend/prompting/conditioning.py b/invokeai/backend/prompting/conditioning.py index b9378cf5ed..04051084c0 100644 --- a/invokeai/backend/prompting/conditioning.py +++ b/invokeai/backend/prompting/conditioning.py @@ -1,31 +1,46 @@ -''' +""" This module handles the generation of the conditioning tensors. Useful function exports: get_uc_and_c_and_ec() get the conditioned and unconditioned latent, and edited conditioning if we're doing cross-attention control -''' +""" import re -from typing import Union, Optional, Any - -from transformers import CLIPTokenizer, CLIPTextModel +from typing import Any, Optional, Union from compel import Compel -from compel.prompt_parser import FlattenedPrompt, Blend, Fragment, CrossAttentionControlSubstitute, PromptParser -from ..util import torch_dtype -from ..stable_diffusion import InvokeAIDiffuserComponent +from compel.prompt_parser import ( + Blend, + CrossAttentionControlSubstitute, + FlattenedPrompt, + Fragment, + PromptParser, +) +from transformers import CLIPTextModel, CLIPTokenizer + from invokeai.backend.globals import Globals +from ..stable_diffusion import InvokeAIDiffuserComponent +from ..util import torch_dtype + + def get_tokenizer(model) -> CLIPTokenizer: # TODO remove legacy ckpt fallback handling - return (getattr(model, 'tokenizer', None) # diffusers - or model.cond_stage_model.tokenizer) # ldm + return ( + getattr(model, "tokenizer", None) # diffusers + or model.cond_stage_model.tokenizer + ) # ldm + def get_text_encoder(model) -> Any: # TODO remove legacy ckpt fallback handling - return (getattr(model, 'text_encoder', None) # diffusers - or UnsqueezingLDMTransformer(model.cond_stage_model.transformer)) # ldm + return getattr( + model, "text_encoder", None + ) or UnsqueezingLDMTransformer( # diffusers + model.cond_stage_model.transformer + ) # ldm + class UnsqueezingLDMTransformer: def __init__(self, ldm_transformer): @@ -40,28 +55,41 @@ class UnsqueezingLDMTransformer: return insufficiently_unsqueezed_tensor.unsqueeze(0) -def get_uc_and_c_and_ec(prompt_string, model, log_tokens=False, skip_normalize_legacy_blend=False): +def get_uc_and_c_and_ec( + prompt_string, model, log_tokens=False, skip_normalize_legacy_blend=False +): # lazy-load any deferred textual inversions. # this might take a couple of seconds the first time a textual inversion is used. - model.textual_inversion_manager.create_deferred_token_ids_for_any_trigger_terms(prompt_string) + model.textual_inversion_manager.create_deferred_token_ids_for_any_trigger_terms( + prompt_string + ) tokenizer = get_tokenizer(model) text_encoder = get_text_encoder(model) - compel = Compel(tokenizer=tokenizer, - text_encoder=text_encoder, - textual_inversion_manager=model.textual_inversion_manager, - dtype_for_device_getter=torch_dtype) + compel = Compel( + tokenizer=tokenizer, + text_encoder=text_encoder, + textual_inversion_manager=model.textual_inversion_manager, + dtype_for_device_getter=torch_dtype, + ) # get rid of any newline characters prompt_string = prompt_string.replace("\n", " ") - positive_prompt_string, negative_prompt_string = split_prompt_to_positive_and_negative(prompt_string) - legacy_blend = try_parse_legacy_blend(positive_prompt_string, skip_normalize_legacy_blend) - positive_prompt: FlattenedPrompt|Blend + ( + positive_prompt_string, + negative_prompt_string, + ) = split_prompt_to_positive_and_negative(prompt_string) + legacy_blend = try_parse_legacy_blend( + positive_prompt_string, skip_normalize_legacy_blend + ) + positive_prompt: FlattenedPrompt | Blend if legacy_blend is not None: positive_prompt = legacy_blend else: positive_prompt = Compel.parse_prompt_string(positive_prompt_string) - negative_prompt: FlattenedPrompt|Blend = Compel.parse_prompt_string(negative_prompt_string) + negative_prompt: FlattenedPrompt | Blend = Compel.parse_prompt_string( + negative_prompt_string + ) if log_tokens or getattr(Globals, "log_tokenization", False): log_tokenization(positive_prompt, negative_prompt, tokenizer=tokenizer) @@ -71,42 +99,70 @@ def get_uc_and_c_and_ec(prompt_string, model, log_tokens=False, skip_normalize_l tokens_count = get_max_token_count(tokenizer, positive_prompt) - ec = InvokeAIDiffuserComponent.ExtraConditioningInfo(tokens_count_including_eos_bos=tokens_count, - cross_attention_control_args=options.get( - 'cross_attention_control', None)) + ec = InvokeAIDiffuserComponent.ExtraConditioningInfo( + tokens_count_including_eos_bos=tokens_count, + cross_attention_control_args=options.get("cross_attention_control", None), + ) return uc, c, ec -def get_prompt_structure(prompt_string, skip_normalize_legacy_blend: bool = False) -> ( - Union[FlattenedPrompt, Blend], FlattenedPrompt): - positive_prompt_string, negative_prompt_string = split_prompt_to_positive_and_negative(prompt_string) - legacy_blend = try_parse_legacy_blend(positive_prompt_string, skip_normalize_legacy_blend) - positive_prompt: FlattenedPrompt|Blend +def get_prompt_structure( + prompt_string, skip_normalize_legacy_blend: bool = False +) -> (Union[FlattenedPrompt, Blend], FlattenedPrompt): + ( + positive_prompt_string, + negative_prompt_string, + ) = split_prompt_to_positive_and_negative(prompt_string) + legacy_blend = try_parse_legacy_blend( + positive_prompt_string, skip_normalize_legacy_blend + ) + positive_prompt: FlattenedPrompt | Blend if legacy_blend is not None: positive_prompt = legacy_blend else: positive_prompt = Compel.parse_prompt_string(positive_prompt_string) - negative_prompt: FlattenedPrompt|Blend = Compel.parse_prompt_string(negative_prompt_string) + negative_prompt: FlattenedPrompt | Blend = Compel.parse_prompt_string( + negative_prompt_string + ) return positive_prompt, negative_prompt -def get_max_token_count(tokenizer, prompt: Union[FlattenedPrompt, Blend], truncate_if_too_long=True) -> int: + +def get_max_token_count( + tokenizer, prompt: Union[FlattenedPrompt, Blend], truncate_if_too_long=True +) -> int: if type(prompt) is Blend: blend: Blend = prompt - return max([get_max_token_count(tokenizer, c, truncate_if_too_long) for c in blend.prompts]) + return max( + [ + get_max_token_count(tokenizer, c, truncate_if_too_long) + for c in blend.prompts + ] + ) else: - return len(get_tokens_for_prompt_object(tokenizer, prompt, truncate_if_too_long)) + return len( + get_tokens_for_prompt_object(tokenizer, prompt, truncate_if_too_long) + ) -def get_tokens_for_prompt_object(tokenizer, parsed_prompt: FlattenedPrompt, truncate_if_too_long=True) -> [str]: - +def get_tokens_for_prompt_object( + tokenizer, parsed_prompt: FlattenedPrompt, truncate_if_too_long=True +) -> [str]: if type(parsed_prompt) is Blend: - raise ValueError("Blend is not supported here - you need to get tokens for each of its .children") + raise ValueError( + "Blend is not supported here - you need to get tokens for each of its .children" + ) - text_fragments = [x.text if type(x) is Fragment else - (" ".join([f.text for f in x.original]) if type(x) is CrossAttentionControlSubstitute else - str(x)) - for x in parsed_prompt.children] + text_fragments = [ + x.text + if type(x) is Fragment + else ( + " ".join([f.text for f in x.original]) + if type(x) is CrossAttentionControlSubstitute + else str(x) + ) + for x in parsed_prompt.children + ] text = " ".join(text_fragments) tokens = tokenizer.tokenize(text) if truncate_if_too_long: @@ -116,39 +172,47 @@ def get_tokens_for_prompt_object(tokenizer, parsed_prompt: FlattenedPrompt, trun def split_prompt_to_positive_and_negative(prompt_string_uncleaned: str): - unconditioned_words = '' - unconditional_regex = r'\[(.*?)\]' + unconditioned_words = "" + unconditional_regex = r"\[(.*?)\]" unconditionals = re.findall(unconditional_regex, prompt_string_uncleaned) if len(unconditionals) > 0: - unconditioned_words = ' '.join(unconditionals) + unconditioned_words = " ".join(unconditionals) # Remove Unconditioned Words From Prompt unconditional_regex_compile = re.compile(unconditional_regex) - clean_prompt = unconditional_regex_compile.sub(' ', prompt_string_uncleaned) - prompt_string_cleaned = re.sub(' +', ' ', clean_prompt) + clean_prompt = unconditional_regex_compile.sub(" ", prompt_string_uncleaned) + prompt_string_cleaned = re.sub(" +", " ", clean_prompt) else: prompt_string_cleaned = prompt_string_uncleaned return prompt_string_cleaned, unconditioned_words -def log_tokenization(positive_prompt: Union[Blend, FlattenedPrompt], - negative_prompt: Union[Blend, FlattenedPrompt], - tokenizer): +def log_tokenization( + positive_prompt: Union[Blend, FlattenedPrompt], + negative_prompt: Union[Blend, FlattenedPrompt], + tokenizer, +): print(f"\n>> [TOKENLOG] Parsed Prompt: {positive_prompt}") print(f"\n>> [TOKENLOG] Parsed Negative Prompt: {negative_prompt}") log_tokenization_for_prompt_object(positive_prompt, tokenizer) - log_tokenization_for_prompt_object(negative_prompt, tokenizer, display_label_prefix="(negative prompt)") + log_tokenization_for_prompt_object( + negative_prompt, tokenizer, display_label_prefix="(negative prompt)" + ) -def log_tokenization_for_prompt_object(p: Union[Blend, FlattenedPrompt], tokenizer, display_label_prefix=None): +def log_tokenization_for_prompt_object( + p: Union[Blend, FlattenedPrompt], tokenizer, display_label_prefix=None +): display_label_prefix = display_label_prefix or "" if type(p) is Blend: blend: Blend = p for i, c in enumerate(blend.prompts): log_tokenization_for_prompt_object( - c, tokenizer, - display_label_prefix=f"{display_label_prefix}(blend part {i + 1}, weight={blend.weights[i]})") + c, + tokenizer, + display_label_prefix=f"{display_label_prefix}(blend part {i + 1}, weight={blend.weights[i]})", + ) elif type(p) is FlattenedPrompt: flattened_prompt: FlattenedPrompt = p if flattened_prompt.wants_cross_attention_control: @@ -163,18 +227,26 @@ def log_tokenization_for_prompt_object(p: Union[Blend, FlattenedPrompt], tokeniz edited_fragments.append(f) original_text = " ".join([x.text for x in original_fragments]) - log_tokenization_for_text(original_text, tokenizer, - display_label=f"{display_label_prefix}(.swap originals)") + log_tokenization_for_text( + original_text, + tokenizer, + display_label=f"{display_label_prefix}(.swap originals)", + ) edited_text = " ".join([x.text for x in edited_fragments]) - log_tokenization_for_text(edited_text, tokenizer, - display_label=f"{display_label_prefix}(.swap replacements)") + log_tokenization_for_text( + edited_text, + tokenizer, + display_label=f"{display_label_prefix}(.swap replacements)", + ) else: text = " ".join([x.text for x in flattened_prompt.children]) - log_tokenization_for_text(text, tokenizer, display_label=display_label_prefix) + log_tokenization_for_text( + text, tokenizer, display_label=display_label_prefix + ) def log_tokenization_for_text(text, tokenizer, display_label=None): - """ shows how the prompt is tokenized + """shows how the prompt is tokenized # usually tokens have '' to indicate end-of-word, # but for readability it has been replaced with ' ' """ @@ -185,7 +257,7 @@ def log_tokenization_for_text(text, tokenizer, display_label=None): totalTokens = len(tokens) for i in range(0, totalTokens): - token = tokens[i].replace('', ' ') + token = tokens[i].replace("", " ") # alternate color s = (usedTokens % 6) + 1 if i < tokenizer.model_max_length: @@ -196,14 +268,14 @@ def log_tokenization_for_text(text, tokenizer, display_label=None): if usedTokens > 0: print(f'\n>> [TOKENLOG] Tokens {display_label or ""} ({usedTokens}):') - print(f'{tokenized}\x1b[0m') + print(f"{tokenized}\x1b[0m") if discarded != "": - print(f'\n>> [TOKENLOG] Tokens Discarded ({totalTokens - usedTokens}):') - print(f'{discarded}\x1b[0m') + print(f"\n>> [TOKENLOG] Tokens Discarded ({totalTokens - usedTokens}):") + print(f"{discarded}\x1b[0m") -def try_parse_legacy_blend(text: str, skip_normalize: bool=False) -> Optional[Blend]: +def try_parse_legacy_blend(text: str, skip_normalize: bool = False) -> Optional[Blend]: weighted_subprompts = split_weighted_subprompts(text, skip_normalize=skip_normalize) if len(weighted_subprompts) <= 1: return None @@ -214,10 +286,12 @@ def try_parse_legacy_blend(text: str, skip_normalize: bool=False) -> Optional[Bl parsed_conjunctions = [pp.parse_conjunction(x) for x in strings] flattened_prompts = [x.prompts[0] for x in parsed_conjunctions] - return Blend(prompts=flattened_prompts, weights=weights, normalize_weights=not skip_normalize) + return Blend( + prompts=flattened_prompts, weights=weights, normalize_weights=not skip_normalize + ) -def split_weighted_subprompts(text, skip_normalize=False)->list: +def split_weighted_subprompts(text, skip_normalize=False) -> list: """ Legacy blend parsing. @@ -226,7 +300,8 @@ def split_weighted_subprompts(text, skip_normalize=False)->list: if ':' has no value defined, defaults to 1.0 repeats until no text remaining """ - prompt_parser = re.compile(""" + prompt_parser = re.compile( + """ (?P # capture group for 'prompt' (?:\\\:|[^:])+ # match one or more non ':' characters or escaped colons '\:' ) # end 'prompt' @@ -239,16 +314,20 @@ def split_weighted_subprompts(text, skip_normalize=False)->list: | # OR $ # else, if no ':' then match end of line ) # end non-capture group - """, re.VERBOSE) - parsed_prompts = [(match.group("prompt").replace("\\:", ":"), float( - match.group("weight") or 1)) for match in re.finditer(prompt_parser, text)] + """, + re.VERBOSE, + ) + parsed_prompts = [ + (match.group("prompt").replace("\\:", ":"), float(match.group("weight") or 1)) + for match in re.finditer(prompt_parser, text) + ] if skip_normalize: return parsed_prompts weight_sum = sum(map(lambda x: x[1], parsed_prompts)) if weight_sum == 0: print( - "* Warning: Subprompt weights add up to zero. Discarding and using even weights instead.") + "* Warning: Subprompt weights add up to zero. Discarding and using even weights instead." + ) equal_weight = 1 / max(len(parsed_prompts), 1) return [(x[0], equal_weight) for x in parsed_prompts] return [(x[0], x[1] / weight_sum) for x in parsed_prompts] - diff --git a/invokeai/backend/restoration/__init__.py b/invokeai/backend/restoration/__init__.py index e784ea52b8..165ef080b3 100644 --- a/invokeai/backend/restoration/__init__.py +++ b/invokeai/backend/restoration/__init__.py @@ -1,4 +1,4 @@ -''' -Initialization file for the ldm.invoke.restoration package -''' +""" +Initialization file for the invokeai.backend.restoration package +""" from .base import Restoration diff --git a/invokeai/backend/restoration/base.py b/invokeai/backend/restoration/base.py index 036d56a271..0957811fc3 100644 --- a/invokeai/backend/restoration/base.py +++ b/invokeai/backend/restoration/base.py @@ -1,22 +1,24 @@ -class Restoration(): +class Restoration: def __init__(self) -> None: pass - def load_face_restore_models(self, gfpgan_model_path='./models/gfpgan/GFPGANv1.4.pth'): + def load_face_restore_models( + self, gfpgan_model_path="./models/gfpgan/GFPGANv1.4.pth" + ): # Load GFPGAN gfpgan = self.load_gfpgan(gfpgan_model_path) if gfpgan.gfpgan_model_exists: - print('>> GFPGAN Initialized') + print(">> GFPGAN Initialized") else: - print('>> GFPGAN Disabled') + print(">> GFPGAN Disabled") gfpgan = None # Load CodeFormer codeformer = self.load_codeformer() if codeformer.codeformer_model_exists: - print('>> CodeFormer Initialized') + print(">> CodeFormer Initialized") else: - print('>> CodeFormer Disabled') + print(">> CodeFormer Disabled") codeformer = None return gfpgan, codeformer @@ -24,15 +26,18 @@ class Restoration(): # Face Restore Models def load_gfpgan(self, gfpgan_model_path): from .gfpgan import GFPGAN + return GFPGAN(gfpgan_model_path) def load_codeformer(self): from .codeformer import CodeFormerRestoration + return CodeFormerRestoration() # Upscale Models def load_esrgan(self, esrgan_bg_tile=400): from .realesrgan import ESRGAN + esrgan = ESRGAN(esrgan_bg_tile) - print('>> ESRGAN Initialized') - return esrgan; + print(">> ESRGAN Initialized") + return esrgan diff --git a/invokeai/backend/restoration/codeformer.py b/invokeai/backend/restoration/codeformer.py index ab4ce82376..94add72b00 100644 --- a/invokeai/backend/restoration/codeformer.py +++ b/invokeai/backend/restoration/codeformer.py @@ -1,17 +1,21 @@ import os -import torch -import numpy as np -import warnings import sys -from invokeai.backend.globals import Globals +import warnings -pretrained_model_url = 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth' +import numpy as np +import torch -class CodeFormerRestoration(): - def __init__(self, - codeformer_dir='models/codeformer', - codeformer_model_path='codeformer.pth') -> None: +from ..globals import Globals +pretrained_model_url = ( + "https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth" +) + + +class CodeFormerRestoration: + def __init__( + self, codeformer_dir="models/codeformer", codeformer_model_path="codeformer.pth" + ) -> None: if not os.path.isabs(codeformer_dir): codeformer_dir = os.path.join(Globals.root, codeformer_dir) @@ -19,22 +23,23 @@ class CodeFormerRestoration(): self.codeformer_model_exists = os.path.isfile(self.model_path) if not self.codeformer_model_exists: - print('## NOT FOUND: CodeFormer model not found at ' + self.model_path) + print("## NOT FOUND: CodeFormer model not found at " + self.model_path) sys.path.append(os.path.abspath(codeformer_dir)) def process(self, image, strength, device, seed=None, fidelity=0.75): if seed is not None: - print(f'>> CodeFormer - Restoring Faces for image seed:{seed}') + print(f">> CodeFormer - Restoring Faces for image seed:{seed}") with warnings.catch_warnings(): - warnings.filterwarnings('ignore', category=DeprecationWarning) - warnings.filterwarnings('ignore', category=UserWarning) + warnings.filterwarnings("ignore", category=DeprecationWarning) + warnings.filterwarnings("ignore", category=UserWarning) - from basicsr.utils.download_util import load_file_from_url from basicsr.utils import img2tensor, tensor2img + from basicsr.utils.download_util import load_file_from_url from facexlib.utils.face_restoration_helper import FaceRestoreHelper - from ldm.invoke.restoration.codeformer_arch import CodeFormer - from torchvision.transforms.functional import normalize from PIL import Image + from torchvision.transforms.functional import normalize + + from .codeformer_arch import CodeFormer cf_class = CodeFormer @@ -43,28 +48,31 @@ class CodeFormerRestoration(): codebook_size=1024, n_head=8, n_layers=9, - connect_list=['32', '64', '128', '256'] + connect_list=["32", "64", "128", "256"], ).to(device) # note that this file should already be downloaded and cached at # this point - checkpoint_path = load_file_from_url(url=pretrained_model_url, - model_dir=os.path.abspath(os.path.dirname(self.model_path)), - progress=True + checkpoint_path = load_file_from_url( + url=pretrained_model_url, + model_dir=os.path.abspath(os.path.dirname(self.model_path)), + progress=True, ) - checkpoint = torch.load(checkpoint_path)['params_ema'] + checkpoint = torch.load(checkpoint_path)["params_ema"] cf.load_state_dict(checkpoint) cf.eval() - image = image.convert('RGB') + image = image.convert("RGB") # Codeformer expects a BGR np array; make array and flip channels - bgr_image_array = np.array(image, dtype=np.uint8)[...,::-1] + bgr_image_array = np.array(image, dtype=np.uint8)[..., ::-1] face_helper = FaceRestoreHelper( upscale_factor=1, use_parse=True, device=device, - model_rootpath=os.path.join(Globals.root,'models','gfpgan','weights'), + model_rootpath=os.path.join( + Globals.root, "models", "gfpgan", "weights" + ), ) face_helper.clean_all() face_helper.read_image(bgr_image_array) @@ -72,30 +80,35 @@ class CodeFormerRestoration(): face_helper.align_warp_face() for idx, cropped_face in enumerate(face_helper.cropped_faces): - cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True) - normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True) + cropped_face_t = img2tensor( + cropped_face / 255.0, bgr2rgb=True, float32=True + ) + normalize( + cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True + ) cropped_face_t = cropped_face_t.unsqueeze(0).to(device) try: with torch.no_grad(): output = cf(cropped_face_t, w=fidelity, adain=True)[0] - restored_face = tensor2img(output.squeeze(0), rgb2bgr=True, min_max=(-1, 1)) + restored_face = tensor2img( + output.squeeze(0), rgb2bgr=True, min_max=(-1, 1) + ) del output torch.cuda.empty_cache() except RuntimeError as error: - print(f'\tFailed inference for CodeFormer: {error}.') + print(f"\tFailed inference for CodeFormer: {error}.") restored_face = cropped_face - restored_face = restored_face.astype('uint8') + restored_face = restored_face.astype("uint8") face_helper.add_restored_face(restored_face) - face_helper.get_inverse_affine(None) restored_img = face_helper.paste_faces_to_input_image() # Flip the channels back to RGB - res = Image.fromarray(restored_img[...,::-1]) + res = Image.fromarray(restored_img[..., ::-1]) if strength < 1.0: # Resize the image to the new image if the sizes have changed diff --git a/invokeai/backend/restoration/codeformer_arch.py b/invokeai/backend/restoration/codeformer_arch.py index dc71c86b33..0f6b881020 100644 --- a/invokeai/backend/restoration/codeformer_arch.py +++ b/invokeai/backend/restoration/codeformer_arch.py @@ -1,13 +1,15 @@ import math +from typing import List, Optional + import numpy as np import torch -from torch import nn, Tensor import torch.nn.functional as F -from typing import Optional, List - -from .vqgan_arch import * from basicsr.utils import get_root_logger from basicsr.utils.registry import ARCH_REGISTRY +from torch import Tensor, nn + +from .vqgan_arch import * + def calc_mean_std(feat, eps=1e-5): """Calculate mean and std for adaptive_instance_normalization. @@ -18,13 +20,14 @@ def calc_mean_std(feat, eps=1e-5): divide-by-zero. Default: 1e-5. """ size = feat.size() - assert len(size) == 4, 'The input feature should be 4D tensor.' + assert len(size) == 4, "The input feature should be 4D tensor." b, c = size[:2] feat_var = feat.view(b, c, -1).var(dim=2) + eps feat_std = feat_var.sqrt().view(b, c, 1, 1) feat_mean = feat.view(b, c, -1).mean(dim=2).view(b, c, 1, 1) return feat_mean, feat_std + def adaptive_instance_normalization(content_feat, style_feat): """Adaptive instance normalization. @@ -38,7 +41,9 @@ def adaptive_instance_normalization(content_feat, style_feat): size = content_feat.size() style_mean, style_std = calc_mean_std(style_feat) content_mean, content_std = calc_mean_std(content_feat) - normalized_feat = (content_feat - content_mean.expand(size)) / content_std.expand(size) + normalized_feat = (content_feat - content_mean.expand(size)) / content_std.expand( + size + ) return normalized_feat * style_std.expand(size) + style_mean.expand(size) @@ -48,7 +53,9 @@ class PositionEmbeddingSine(nn.Module): used by the Attention is all you need paper, generalized to work on images. """ - def __init__(self, num_pos_feats=64, temperature=10000, normalize=False, scale=None): + def __init__( + self, num_pos_feats=64, temperature=10000, normalize=False, scale=None + ): super().__init__() self.num_pos_feats = num_pos_feats self.temperature = temperature @@ -61,7 +68,9 @@ class PositionEmbeddingSine(nn.Module): def forward(self, x, mask=None): if mask is None: - mask = torch.zeros((x.size(0), x.size(2), x.size(3)), device=x.device, dtype=torch.bool) + mask = torch.zeros( + (x.size(0), x.size(2), x.size(3)), device=x.device, dtype=torch.bool + ) not_mask = ~mask y_embed = not_mask.cumsum(1, dtype=torch.float32) x_embed = not_mask.cumsum(2, dtype=torch.float32) @@ -84,6 +93,7 @@ class PositionEmbeddingSine(nn.Module): pos = torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2) return pos + def _get_activation_fn(activation): """Return an activation function given a string""" if activation == "relu": @@ -92,11 +102,13 @@ def _get_activation_fn(activation): return F.gelu if activation == "glu": return F.glu - raise RuntimeError(F"activation should be relu/gelu, not {activation}.") + raise RuntimeError(f"activation should be relu/gelu, not {activation}.") class TransformerSALayer(nn.Module): - def __init__(self, embed_dim, nhead=8, dim_mlp=2048, dropout=0.0, activation="gelu"): + def __init__( + self, embed_dim, nhead=8, dim_mlp=2048, dropout=0.0, activation="gelu" + ): super().__init__() self.self_attn = nn.MultiheadAttention(embed_dim, nhead, dropout=dropout) # Implementation of Feedforward model - MLP @@ -114,16 +126,19 @@ class TransformerSALayer(nn.Module): def with_pos_embed(self, tensor, pos: Optional[Tensor]): return tensor if pos is None else tensor + pos - def forward(self, tgt, - tgt_mask: Optional[Tensor] = None, - tgt_key_padding_mask: Optional[Tensor] = None, - query_pos: Optional[Tensor] = None): - + def forward( + self, + tgt, + tgt_mask: Optional[Tensor] = None, + tgt_key_padding_mask: Optional[Tensor] = None, + query_pos: Optional[Tensor] = None, + ): # self attention tgt2 = self.norm1(tgt) q = k = self.with_pos_embed(tgt2, query_pos) - tgt2 = self.self_attn(q, k, value=tgt2, attn_mask=tgt_mask, - key_padding_mask=tgt_key_padding_mask)[0] + tgt2 = self.self_attn( + q, k, value=tgt2, attn_mask=tgt_mask, key_padding_mask=tgt_key_padding_mask + )[0] tgt = tgt + self.dropout1(tgt2) # ffn @@ -132,20 +147,23 @@ class TransformerSALayer(nn.Module): tgt = tgt + self.dropout2(tgt2) return tgt + class Fuse_sft_block(nn.Module): def __init__(self, in_ch, out_ch): super().__init__() - self.encode_enc = ResBlock(2*in_ch, out_ch) + self.encode_enc = ResBlock(2 * in_ch, out_ch) self.scale = nn.Sequential( - nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1), - nn.LeakyReLU(0.2, True), - nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1)) + nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1), + nn.LeakyReLU(0.2, True), + nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1), + ) self.shift = nn.Sequential( - nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1), - nn.LeakyReLU(0.2, True), - nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1)) + nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1), + nn.LeakyReLU(0.2, True), + nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1), + ) def forward(self, enc_feat, dec_feat, w=1): enc_feat = self.encode_enc(torch.cat([enc_feat, dec_feat], dim=1)) @@ -158,11 +176,19 @@ class Fuse_sft_block(nn.Module): @ARCH_REGISTRY.register() class CodeFormer(VQAutoEncoder): - def __init__(self, dim_embd=512, n_head=8, n_layers=9, - codebook_size=1024, latent_size=256, - connect_list=['32', '64', '128', '256'], - fix_modules=['quantize','generator']): - super(CodeFormer, self).__init__(512, 64, [1, 2, 2, 4, 4, 8], 'nearest',2, [16], codebook_size) + def __init__( + self, + dim_embd=512, + n_head=8, + n_layers=9, + codebook_size=1024, + latent_size=256, + connect_list=["32", "64", "128", "256"], + fix_modules=["quantize", "generator"], + ): + super(CodeFormer, self).__init__( + 512, 64, [1, 2, 2, 4, 4, 8], "nearest", 2, [16], codebook_size + ) if fix_modules is not None: for module in fix_modules: @@ -172,33 +198,53 @@ class CodeFormer(VQAutoEncoder): self.connect_list = connect_list self.n_layers = n_layers self.dim_embd = dim_embd - self.dim_mlp = dim_embd*2 + self.dim_mlp = dim_embd * 2 self.position_emb = nn.Parameter(torch.zeros(latent_size, self.dim_embd)) self.feat_emb = nn.Linear(256, self.dim_embd) # transformer - self.ft_layers = nn.Sequential(*[TransformerSALayer(embed_dim=dim_embd, nhead=n_head, dim_mlp=self.dim_mlp, dropout=0.0) - for _ in range(self.n_layers)]) + self.ft_layers = nn.Sequential( + *[ + TransformerSALayer( + embed_dim=dim_embd, nhead=n_head, dim_mlp=self.dim_mlp, dropout=0.0 + ) + for _ in range(self.n_layers) + ] + ) # logits_predict head self.idx_pred_layer = nn.Sequential( - nn.LayerNorm(dim_embd), - nn.Linear(dim_embd, codebook_size, bias=False)) + nn.LayerNorm(dim_embd), nn.Linear(dim_embd, codebook_size, bias=False) + ) self.channels = { - '16': 512, - '32': 256, - '64': 256, - '128': 128, - '256': 128, - '512': 64, + "16": 512, + "32": 256, + "64": 256, + "128": 128, + "256": 128, + "512": 64, } # after second residual block for > 16, before attn layer for ==16 - self.fuse_encoder_block = {'512':2, '256':5, '128':8, '64':11, '32':14, '16':18} + self.fuse_encoder_block = { + "512": 2, + "256": 5, + "128": 8, + "64": 11, + "32": 14, + "16": 18, + } # after first residual block for > 16, before attn layer for ==16 - self.fuse_generator_block = {'16':6, '32': 9, '64':12, '128':15, '256':18, '512':21} + self.fuse_generator_block = { + "16": 6, + "32": 9, + "64": 12, + "128": 15, + "256": 18, + "512": 21, + } # fuse_convs_dict self.fuse_convs_dict = nn.ModuleDict() @@ -227,20 +273,20 @@ class CodeFormer(VQAutoEncoder): lq_feat = x # ################# Transformer ################### # quant_feat, codebook_loss, quant_stats = self.quantize(lq_feat) - pos_emb = self.position_emb.unsqueeze(1).repeat(1,x.shape[0],1) + pos_emb = self.position_emb.unsqueeze(1).repeat(1, x.shape[0], 1) # BCHW -> BC(HW) -> (HW)BC - feat_emb = self.feat_emb(lq_feat.flatten(2).permute(2,0,1)) + feat_emb = self.feat_emb(lq_feat.flatten(2).permute(2, 0, 1)) query_emb = feat_emb # Transformer encoder for layer in self.ft_layers: query_emb = layer(query_emb, query_pos=pos_emb) # output logits - logits = self.idx_pred_layer(query_emb) # (hw)bn - logits = logits.permute(1,0,2) # (hw)bn -> b(hw)n + logits = self.idx_pred_layer(query_emb) # (hw)bn + logits = logits.permute(1, 0, 2) # (hw)bn -> b(hw)n - if code_only: # for training stage II - # logits doesn't need softmax before cross_entropy loss + if code_only: # for training stage II + # logits doesn't need softmax before cross_entropy loss return logits, lq_feat # ################# Quantization ################### @@ -251,12 +297,14 @@ class CodeFormer(VQAutoEncoder): # ------------ soft_one_hot = F.softmax(logits, dim=2) _, top_idx = torch.topk(soft_one_hot, 1, dim=2) - quant_feat = self.quantize.get_codebook_feat(top_idx, shape=[x.shape[0],16,16,256]) + quant_feat = self.quantize.get_codebook_feat( + top_idx, shape=[x.shape[0], 16, 16, 256] + ) # preserve gradients # quant_feat = lq_feat + (quant_feat - lq_feat).detach() if detach_16: - quant_feat = quant_feat.detach() # for training stage III + quant_feat = quant_feat.detach() # for training stage III if adain: quant_feat = adaptive_instance_normalization(quant_feat, lq_feat) @@ -266,10 +314,12 @@ class CodeFormer(VQAutoEncoder): for i, block in enumerate(self.generator.blocks): x = block(x) - if i in fuse_list: # fuse after i-th block + if i in fuse_list: # fuse after i-th block f_size = str(x.shape[-1]) - if w>0: - x = self.fuse_convs_dict[f_size](enc_feat_dict[f_size].detach(), x, w) + if w > 0: + x = self.fuse_convs_dict[f_size]( + enc_feat_dict[f_size].detach(), x, w + ) out = x # logits doesn't need softmax before cross_entropy loss return out, logits, lq_feat diff --git a/invokeai/backend/restoration/gfpgan.py b/invokeai/backend/restoration/gfpgan.py index 5fff52583f..d13745d0c6 100644 --- a/invokeai/backend/restoration/gfpgan.py +++ b/invokeai/backend/restoration/gfpgan.py @@ -1,26 +1,25 @@ -import torch -import warnings import os import sys -import numpy as np -from invokeai.backend.globals import Globals +import warnings +import numpy as np +import torch from PIL import Image +from invokeai.backend.globals import Globals -class GFPGAN(): - def __init__( - self, - gfpgan_model_path='models/gfpgan/GFPGANv1.4.pth' - ) -> None: +class GFPGAN: + def __init__(self, gfpgan_model_path="models/gfpgan/GFPGANv1.4.pth") -> None: if not os.path.isabs(gfpgan_model_path): - gfpgan_model_path=os.path.abspath(os.path.join(Globals.root,gfpgan_model_path)) + gfpgan_model_path = os.path.abspath( + os.path.join(Globals.root, gfpgan_model_path) + ) self.model_path = gfpgan_model_path self.gfpgan_model_exists = os.path.isfile(self.model_path) if not self.gfpgan_model_exists: - print('## NOT FOUND: GFPGAN model not found at ' + self.model_path) + print("## NOT FOUND: GFPGAN model not found at " + self.model_path) return None def model_exists(self): @@ -28,40 +27,40 @@ class GFPGAN(): def process(self, image, strength: float, seed: str = None): if seed is not None: - print(f'>> GFPGAN - Restoring Faces for image seed:{seed}') + print(f">> GFPGAN - Restoring Faces for image seed:{seed}") with warnings.catch_warnings(): - warnings.filterwarnings('ignore', category=DeprecationWarning) - warnings.filterwarnings('ignore', category=UserWarning) + warnings.filterwarnings("ignore", category=DeprecationWarning) + warnings.filterwarnings("ignore", category=UserWarning) cwd = os.getcwd() - os.chdir(os.path.join(Globals.root,'models')) + os.chdir(os.path.join(Globals.root, "models")) try: from gfpgan import GFPGANer + self.gfpgan = GFPGANer( model_path=self.model_path, upscale=1, - arch='clean', + arch="clean", channel_multiplier=2, bg_upsampler=None, ) except Exception: import traceback - print('>> Error loading GFPGAN:', file=sys.stderr) + + print(">> Error loading GFPGAN:", file=sys.stderr) print(traceback.format_exc(), file=sys.stderr) os.chdir(cwd) if self.gfpgan is None: + print(f">> WARNING: GFPGAN not initialized.") print( - f'>> WARNING: GFPGAN not initialized.' - ) - print( - f'>> Download https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth to {self.model_path}' + f">> Download https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth to {self.model_path}" ) - image = image.convert('RGB') + image = image.convert("RGB") # GFPGAN expects a BGR np array; make array and flip channels - bgr_image_array = np.array(image, dtype=np.uint8)[...,::-1] + bgr_image_array = np.array(image, dtype=np.uint8)[..., ::-1] _, _, restored_img = self.gfpgan.enhance( bgr_image_array, @@ -71,7 +70,7 @@ class GFPGAN(): ) # Flip the channels back to RGB - res = Image.fromarray(restored_img[...,::-1]) + res = Image.fromarray(restored_img[..., ::-1]) if strength < 1.0: # Resize the image to the new image if the sizes have changed @@ -79,7 +78,6 @@ class GFPGAN(): image = image.resize(res.size) res = Image.blend(image, res, strength) - if torch.cuda.is_available(): torch.cuda.empty_cache() self.gfpgan = None diff --git a/invokeai/backend/restoration/outcrop.py b/invokeai/backend/restoration/outcrop.py index 60bedea98c..0778d7cc8f 100644 --- a/invokeai/backend/restoration/outcrop.py +++ b/invokeai/backend/restoration/outcrop.py @@ -1,23 +1,25 @@ -import warnings import math +import warnings + from PIL import Image, ImageFilter + class Outcrop(object): def __init__( - self, - image, - generate, # current generate object + self, + image, + generate, # current generate object ): - self.image = image - self.generate = generate + self.image = image + self.generate = generate - def process ( - self, - extents:dict, - opt, # current options - orig_opt, # ones originally used to generate the image - image_callback = None, - prefix = None + def process( + self, + extents: dict, + opt, # current options + orig_opt, # ones originally used to generate the image + image_callback=None, + prefix=None, ): # grow and mask the image extended_image = self._extend_all(extents) @@ -27,29 +29,33 @@ class Outcrop(object): self.generate.sampler_name = opt.sampler_name self.generate._set_sampler() - def wrapped_callback(img,seed,**kwargs): - preferred_seed = orig_opt.seed if orig_opt.seed is not None and orig_opt.seed >= 0 else seed - image_callback(img,preferred_seed,use_prefix=prefix,**kwargs) + def wrapped_callback(img, seed, **kwargs): + preferred_seed = ( + orig_opt.seed + if orig_opt.seed is not None and orig_opt.seed >= 0 + else seed + ) + image_callback(img, preferred_seed, use_prefix=prefix, **kwargs) - result= self.generate.prompt2image( + result = self.generate.prompt2image( opt.prompt, - seed = opt.seed or orig_opt.seed, - sampler = self.generate.sampler, - steps = opt.steps, - cfg_scale = opt.cfg_scale, - ddim_eta = self.generate.ddim_eta, - width = extended_image.width, - height = extended_image.height, - init_img = extended_image, - strength = 0.90, - image_callback = wrapped_callback if image_callback else None, - seam_size = opt.seam_size or 96, - seam_blur = opt.seam_blur or 16, - seam_strength = opt.seam_strength or 0.7, - seam_steps = 20, - tile_size = 32, - color_match = True, - force_outpaint = True, # this just stops the warning about erased regions + seed=opt.seed or orig_opt.seed, + sampler=self.generate.sampler, + steps=opt.steps, + cfg_scale=opt.cfg_scale, + ddim_eta=self.generate.ddim_eta, + width=extended_image.width, + height=extended_image.height, + init_img=extended_image, + strength=0.90, + image_callback=wrapped_callback if image_callback else None, + seam_size=opt.seam_size or 96, + seam_blur=opt.seam_blur or 16, + seam_strength=opt.seam_strength or 0.7, + seam_steps=20, + tile_size=32, + color_match=True, + force_outpaint=True, # this just stops the warning about erased regions ) # swap sampler back @@ -57,52 +63,57 @@ class Outcrop(object): return result def _extend_all( - self, - extents:dict, + self, + extents: dict, ) -> Image: - ''' + """ Extend the image in direction ('top','bottom','left','right') by the indicated value. The image canvas is extended, and the empty rectangular section will be filled with a blurred copy of the adjacent image. - ''' + """ image = self.image for direction in extents: - assert direction in ['top', 'left', 'bottom', 'right'],'Direction must be one of "top", "left", "bottom", "right"' + assert direction in [ + "top", + "left", + "bottom", + "right", + ], 'Direction must be one of "top", "left", "bottom", "right"' pixels = extents[direction] # round pixels up to the nearest 64 - pixels = math.ceil(pixels/64) * 64 - print(f'>> extending image {direction}ward by {pixels} pixels') - image = self._rotate(image,direction) - image = self._extend(image,pixels) - image = self._rotate(image,direction,reverse=True) + pixels = math.ceil(pixels / 64) * 64 + print(f">> extending image {direction}ward by {pixels} pixels") + image = self._rotate(image, direction) + image = self._extend(image, pixels) + image = self._rotate(image, direction, reverse=True) return image - def _rotate(self,image:Image,direction:str,reverse=False) -> Image: - ''' + def _rotate(self, image: Image, direction: str, reverse=False) -> Image: + """ Rotates image so that the area to extend is always at the top top. Simplifies logic later. The reverse argument, if true, will undo the previous transpose. - ''' + """ transposes = { - 'right': ['ROTATE_90','ROTATE_270'], - 'bottom': ['ROTATE_180','ROTATE_180'], - 'left': ['ROTATE_270','ROTATE_90'] + "right": ["ROTATE_90", "ROTATE_270"], + "bottom": ["ROTATE_180", "ROTATE_180"], + "left": ["ROTATE_270", "ROTATE_90"], } if direction not in transposes: return image transpose = transposes[direction][1 if reverse else 0] return image.transpose(Image.Transpose.__dict__[transpose]) - def _extend(self,image:Image,pixels:int)-> Image: - extended_img = Image.new('RGBA',(image.width,image.height+pixels)) + def _extend(self, image: Image, pixels: int) -> Image: + extended_img = Image.new("RGBA", (image.width, image.height + pixels)) - extended_img.paste((0,0,0),[0,0,image.width,image.height+pixels]) - extended_img.paste(image,box=(0,pixels)) + extended_img.paste((0, 0, 0), [0, 0, image.width, image.height + pixels]) + extended_img.paste(image, box=(0, pixels)) # now make the top part transparent to use as a mask - alpha = extended_img.getchannel('A') - alpha.paste(0,(0,0,extended_img.width,pixels)) + alpha = extended_img.getchannel("A") + alpha.paste(0, (0, 0, extended_img.width, pixels)) extended_img.putalpha(alpha) return extended_img diff --git a/invokeai/backend/restoration/outpaint.py b/invokeai/backend/restoration/outpaint.py index 06b69188a5..184db10fa3 100644 --- a/invokeai/backend/restoration/outpaint.py +++ b/invokeai/backend/restoration/outpaint.py @@ -1,39 +1,43 @@ -import warnings import math +import warnings + from PIL import Image, ImageFilter + class Outpaint(object): def __init__(self, image, generate): - self.image = image - self.generate = generate + self.image = image + self.generate = generate - def process(self, opt, old_opt, image_callback = None, prefix = None): + def process(self, opt, old_opt, image_callback=None, prefix=None): image = self._create_outpaint_image(self.image, opt.out_direction) - seed = old_opt.seed + seed = old_opt.seed prompt = old_opt.prompt - def wrapped_callback(img,seed,**kwargs): - image_callback(img,seed,use_prefix=prefix,**kwargs) - + def wrapped_callback(img, seed, **kwargs): + image_callback(img, seed, use_prefix=prefix, **kwargs) return self.generate.prompt2image( prompt, - seed = seed, - sampler = self.generate.sampler, - steps = opt.steps, - cfg_scale = opt.cfg_scale, - ddim_eta = self.generate.ddim_eta, - width = opt.width, - height = opt.height, - init_img = image, - strength = 0.83, - image_callback = wrapped_callback, - prefix = prefix, + seed=seed, + sampler=self.generate.sampler, + steps=opt.steps, + cfg_scale=opt.cfg_scale, + ddim_eta=self.generate.ddim_eta, + width=opt.width, + height=opt.height, + init_img=image, + strength=0.83, + image_callback=wrapped_callback, + prefix=prefix, ) def _create_outpaint_image(self, image, direction_args): - assert len(direction_args) in [1, 2], 'Direction (-D) must have exactly one or two arguments.' + assert len(direction_args) in [ + 1, + 2, + ], "Direction (-D) must have exactly one or two arguments." if len(direction_args) == 1: direction = direction_args[0] @@ -42,19 +46,26 @@ class Outpaint(object): direction = direction_args[0] pixels = int(direction_args[1]) - assert direction in ['top', 'left', 'bottom', 'right'], 'Direction (-D) must be one of "top", "left", "bottom", "right"' + assert direction in [ + "top", + "left", + "bottom", + "right", + ], 'Direction (-D) must be one of "top", "left", "bottom", "right"' image = image.convert("RGBA") # we always extend top, but rotate to extend along the requested side - if direction == 'left': + if direction == "left": image = image.transpose(Image.Transpose.ROTATE_270) - elif direction == 'bottom': + elif direction == "bottom": image = image.transpose(Image.Transpose.ROTATE_180) - elif direction == 'right': + elif direction == "right": image = image.transpose(Image.Transpose.ROTATE_90) - pixels = image.height//2 if pixels is None else int(pixels) - assert 0 < pixels < image.height, 'Direction (-D) pixels length must be in the range 0 - image.size' + pixels = image.height // 2 if pixels is None else int(pixels) + assert ( + 0 < pixels < image.height + ), "Direction (-D) pixels length must be in the range 0 - image.size" # the top part of the image is taken from the source image mirrored # coordinates (0,0) are the upper left corner of an image @@ -74,19 +85,18 @@ class Outpaint(object): new_img.paste(bottom, (0, pixels)) # create a 10% dither in the middle - dither = min(image.height//10, pixels) + dither = min(image.height // 10, pixels) for x in range(0, image.width, 2): for y in range(pixels - dither, pixels + dither): (r, g, b, a) = new_img.getpixel((x, y)) new_img.putpixel((x, y), (r, g, b, 0)) # let's rotate back again - if direction == 'left': + if direction == "left": new_img = new_img.transpose(Image.Transpose.ROTATE_90) - elif direction == 'bottom': + elif direction == "bottom": new_img = new_img.transpose(Image.Transpose.ROTATE_180) - elif direction == 'right': + elif direction == "right": new_img = new_img.transpose(Image.Transpose.ROTATE_270) return new_img - diff --git a/invokeai/backend/restoration/realesrgan.py b/invokeai/backend/restoration/realesrgan.py index 0b99f8bbb6..ad6ad556f1 100644 --- a/invokeai/backend/restoration/realesrgan.py +++ b/invokeai/backend/restoration/realesrgan.py @@ -1,13 +1,15 @@ -import torch -import warnings -import numpy as np import os +import warnings -from invokeai.backend.globals import Globals +import numpy as np +import torch from PIL import Image from PIL.Image import Image as ImageType -class ESRGAN(): +from invokeai.backend.globals import Globals + + +class ESRGAN: def __init__(self, bg_tile_size=400) -> None: self.bg_tile_size = bg_tile_size @@ -22,12 +24,23 @@ class ESRGAN(): else: use_half_precision = True - from realesrgan.archs.srvgg_arch import SRVGGNetCompact from realesrgan import RealESRGANer + from realesrgan.archs.srvgg_arch import SRVGGNetCompact - model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu') - model_path = os.path.join(Globals.root, 'models/realesrgan/realesr-general-x4v3.pth') - wdn_model_path = os.path.join(Globals.root, 'models/realesrgan/realesr-general-wdn-x4v3.pth') + model = SRVGGNetCompact( + num_in_ch=3, + num_out_ch=3, + num_feat=64, + num_conv=32, + upscale=4, + act_type="prelu", + ) + model_path = os.path.join( + Globals.root, "models/realesrgan/realesr-general-x4v3.pth" + ) + wdn_model_path = os.path.join( + Globals.root, "models/realesrgan/realesr-general-wdn-x4v3.pth" + ) scale = 4 bg_upsampler = RealESRGANer( @@ -43,41 +56,49 @@ class ESRGAN(): return bg_upsampler - def process(self, image: ImageType, strength: float, seed: str = None, upsampler_scale: int = 2, denoise_str: float = 0.75): + def process( + self, + image: ImageType, + strength: float, + seed: str = None, + upsampler_scale: int = 2, + denoise_str: float = 0.75, + ): with warnings.catch_warnings(): - warnings.filterwarnings('ignore', category=DeprecationWarning) - warnings.filterwarnings('ignore', category=UserWarning) + warnings.filterwarnings("ignore", category=DeprecationWarning) + warnings.filterwarnings("ignore", category=UserWarning) try: upsampler = self.load_esrgan_bg_upsampler(denoise_str) except Exception: - import traceback import sys - print('>> Error loading Real-ESRGAN:', file=sys.stderr) + import traceback + + print(">> Error loading Real-ESRGAN:", file=sys.stderr) print(traceback.format_exc(), file=sys.stderr) if upsampler_scale == 0: - print('>> Real-ESRGAN: Invalid scaling option. Image not upscaled.') + print(">> Real-ESRGAN: Invalid scaling option. Image not upscaled.") return image if seed is not None: print( - f'>> Real-ESRGAN Upscaling seed:{seed}, scale:{upsampler_scale}x, tile:{self.bg_tile_size}, denoise:{denoise_str}' + f">> Real-ESRGAN Upscaling seed:{seed}, scale:{upsampler_scale}x, tile:{self.bg_tile_size}, denoise:{denoise_str}" ) # ESRGAN outputs images with partial transparency if given RGBA images; convert to RGB image = image.convert("RGB") # REALSRGAN expects a BGR np array; make array and flip channels - bgr_image_array = np.array(image, dtype=np.uint8)[...,::-1] + bgr_image_array = np.array(image, dtype=np.uint8)[..., ::-1] output, _ = upsampler.enhance( bgr_image_array, outscale=upsampler_scale, - alpha_upsampler='realesrgan', + alpha_upsampler="realesrgan", ) # Flip the channels back to RGB - res = Image.fromarray(output[...,::-1]) + res = Image.fromarray(output[..., ::-1]) if strength < 1.0: # Resize the image to the new image if the sizes have changed diff --git a/invokeai/backend/restoration/vqgan_arch.py b/invokeai/backend/restoration/vqgan_arch.py index e78f90815d..96d5f04eee 100644 --- a/invokeai/backend/restoration/vqgan_arch.py +++ b/invokeai/backend/restoration/vqgan_arch.py @@ -1,23 +1,27 @@ -''' +""" VQGAN code, adapted from the original created by the Unleashing Transformers authors: https://github.com/samb-t/unleashing-transformers/blob/master/models/vqgan.py -''' +""" +import copy + import numpy as np import torch import torch.nn as nn import torch.nn.functional as F -import copy from basicsr.utils import get_root_logger from basicsr.utils.registry import ARCH_REGISTRY + def normalize(in_channels): - return torch.nn.GroupNorm(num_groups=32, num_channels=in_channels, eps=1e-6, affine=True) + return torch.nn.GroupNorm( + num_groups=32, num_channels=in_channels, eps=1e-6, affine=True + ) @torch.jit.script def swish(x): - return x*torch.sigmoid(x) + return x * torch.sigmoid(x) # Define VQVAE classes @@ -28,7 +32,9 @@ class VectorQuantizer(nn.Module): self.emb_dim = emb_dim # dimension of embedding self.beta = beta # commitment cost used in loss term, beta * ||z_e(x)-sg[e]||^2 self.embedding = nn.Embedding(self.codebook_size, self.emb_dim) - self.embedding.weight.data.uniform_(-1.0 / self.codebook_size, 1.0 / self.codebook_size) + self.embedding.weight.data.uniform_( + -1.0 / self.codebook_size, 1.0 / self.codebook_size + ) def forward(self, z): # reshape z -> (batch, height, width, channel) and flatten @@ -36,23 +42,32 @@ class VectorQuantizer(nn.Module): z_flattened = z.view(-1, self.emb_dim) # distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z - d = (z_flattened ** 2).sum(dim=1, keepdim=True) + (self.embedding.weight**2).sum(1) - \ - 2 * torch.matmul(z_flattened, self.embedding.weight.t()) + d = ( + (z_flattened**2).sum(dim=1, keepdim=True) + + (self.embedding.weight**2).sum(1) + - 2 * torch.matmul(z_flattened, self.embedding.weight.t()) + ) mean_distance = torch.mean(d) # find closest encodings # min_encoding_indices = torch.argmin(d, dim=1).unsqueeze(1) - min_encoding_scores, min_encoding_indices = torch.topk(d, 1, dim=1, largest=False) + min_encoding_scores, min_encoding_indices = torch.topk( + d, 1, dim=1, largest=False + ) # [0-1], higher score, higher confidence - min_encoding_scores = torch.exp(-min_encoding_scores/10) + min_encoding_scores = torch.exp(-min_encoding_scores / 10) - min_encodings = torch.zeros(min_encoding_indices.shape[0], self.codebook_size).to(z) + min_encodings = torch.zeros( + min_encoding_indices.shape[0], self.codebook_size + ).to(z) min_encodings.scatter_(1, min_encoding_indices, 1) # get quantized latent vectors z_q = torch.matmul(min_encodings, self.embedding.weight).view(z.shape) # compute loss for embedding - loss = torch.mean((z_q.detach()-z)**2) + self.beta * torch.mean((z_q - z.detach()) ** 2) + loss = torch.mean((z_q.detach() - z) ** 2) + self.beta * torch.mean( + (z_q - z.detach()) ** 2 + ) # preserve gradients z_q = z + (z_q - z).detach() @@ -62,18 +77,22 @@ class VectorQuantizer(nn.Module): # reshape back to match original input shape z_q = z_q.permute(0, 3, 1, 2).contiguous() - return z_q, loss, { - "perplexity": perplexity, - "min_encodings": min_encodings, - "min_encoding_indices": min_encoding_indices, - "min_encoding_scores": min_encoding_scores, - "mean_distance": mean_distance - } + return ( + z_q, + loss, + { + "perplexity": perplexity, + "min_encodings": min_encodings, + "min_encoding_indices": min_encoding_indices, + "min_encoding_scores": min_encoding_scores, + "mean_distance": mean_distance, + }, + ) def get_codebook_feat(self, indices, shape): # input indices: batch*token_num -> (batch*token_num)*1 # shape: batch, height, width, channel - indices = indices.view(-1,1) + indices = indices.view(-1, 1) min_encodings = torch.zeros(indices.shape[0], self.codebook_size).to(indices) min_encodings.scatter_(1, indices, 1) # get quantized latent vectors @@ -86,14 +105,24 @@ class VectorQuantizer(nn.Module): class GumbelQuantizer(nn.Module): - def __init__(self, codebook_size, emb_dim, num_hiddens, straight_through=False, kl_weight=5e-4, temp_init=1.0): + def __init__( + self, + codebook_size, + emb_dim, + num_hiddens, + straight_through=False, + kl_weight=5e-4, + temp_init=1.0, + ): super().__init__() self.codebook_size = codebook_size # number of embeddings self.emb_dim = emb_dim # dimension of embedding self.straight_through = straight_through self.temperature = temp_init self.kl_weight = kl_weight - self.proj = nn.Conv2d(num_hiddens, codebook_size, 1) # projects last encoder layer to quantized logits + self.proj = nn.Conv2d( + num_hiddens, codebook_size, 1 + ) # projects last encoder layer to quantized logits self.embed = nn.Embedding(codebook_size, emb_dim) def forward(self, z): @@ -107,18 +136,21 @@ class GumbelQuantizer(nn.Module): # + kl divergence to the prior loss qy = F.softmax(logits, dim=1) - diff = self.kl_weight * torch.sum(qy * torch.log(qy * self.codebook_size + 1e-10), dim=1).mean() + diff = ( + self.kl_weight + * torch.sum(qy * torch.log(qy * self.codebook_size + 1e-10), dim=1).mean() + ) min_encoding_indices = soft_one_hot.argmax(dim=1) - return z_q, diff, { - "min_encoding_indices": min_encoding_indices - } + return z_q, diff, {"min_encoding_indices": min_encoding_indices} class Downsample(nn.Module): def __init__(self, in_channels): super().__init__() - self.conv = torch.nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=2, padding=0) + self.conv = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=3, stride=2, padding=0 + ) def forward(self, x): pad = (0, 1, 0, 1) @@ -130,7 +162,9 @@ class Downsample(nn.Module): class Upsample(nn.Module): def __init__(self, in_channels): super().__init__() - self.conv = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1) + self.conv = nn.Conv2d( + in_channels, in_channels, kernel_size=3, stride=1, padding=1 + ) def forward(self, x): x = F.interpolate(x, scale_factor=2.0, mode="nearest") @@ -145,11 +179,17 @@ class ResBlock(nn.Module): self.in_channels = in_channels self.out_channels = in_channels if out_channels is None else out_channels self.norm1 = normalize(in_channels) - self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) + self.conv1 = nn.Conv2d( + in_channels, out_channels, kernel_size=3, stride=1, padding=1 + ) self.norm2 = normalize(out_channels) - self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1) + self.conv2 = nn.Conv2d( + out_channels, out_channels, kernel_size=3, stride=1, padding=1 + ) if self.in_channels != self.out_channels: - self.conv_out = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0) + self.conv_out = nn.Conv2d( + in_channels, out_channels, kernel_size=1, stride=1, padding=0 + ) def forward(self, x_in): x = x_in @@ -172,32 +212,16 @@ class AttnBlock(nn.Module): self.norm = normalize(in_channels) self.q = torch.nn.Conv2d( - in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0 + in_channels, in_channels, kernel_size=1, stride=1, padding=0 ) self.k = torch.nn.Conv2d( - in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0 + in_channels, in_channels, kernel_size=1, stride=1, padding=0 ) self.v = torch.nn.Conv2d( - in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0 + in_channels, in_channels, kernel_size=1, stride=1, padding=0 ) self.proj_out = torch.nn.Conv2d( - in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0 + in_channels, in_channels, kernel_size=1, stride=1, padding=0 ) def forward(self, x): @@ -209,26 +233,35 @@ class AttnBlock(nn.Module): # compute attention b, c, h, w = q.shape - q = q.reshape(b, c, h*w) + q = q.reshape(b, c, h * w) q = q.permute(0, 2, 1) - k = k.reshape(b, c, h*w) + k = k.reshape(b, c, h * w) w_ = torch.bmm(q, k) - w_ = w_ * (int(c)**(-0.5)) + w_ = w_ * (int(c) ** (-0.5)) w_ = F.softmax(w_, dim=2) # attend to values - v = v.reshape(b, c, h*w) + v = v.reshape(b, c, h * w) w_ = w_.permute(0, 2, 1) h_ = torch.bmm(v, w_) h_ = h_.reshape(b, c, h, w) h_ = self.proj_out(h_) - return x+h_ + return x + h_ class Encoder(nn.Module): - def __init__(self, in_channels, nf, emb_dim, ch_mult, num_res_blocks, resolution, attn_resolutions): + def __init__( + self, + in_channels, + nf, + emb_dim, + ch_mult, + num_res_blocks, + resolution, + attn_resolutions, + ): super().__init__() self.nf = nf self.num_resolutions = len(ch_mult) @@ -237,7 +270,7 @@ class Encoder(nn.Module): self.attn_resolutions = attn_resolutions curr_res = self.resolution - in_ch_mult = (1,)+tuple(ch_mult) + in_ch_mult = (1,) + tuple(ch_mult) blocks = [] # initial convultion @@ -264,7 +297,9 @@ class Encoder(nn.Module): # normalise and convert to latent size blocks.append(normalize(block_in_ch)) - blocks.append(nn.Conv2d(block_in_ch, emb_dim, kernel_size=3, stride=1, padding=1)) + blocks.append( + nn.Conv2d(block_in_ch, emb_dim, kernel_size=3, stride=1, padding=1) + ) self.blocks = nn.ModuleList(blocks) def forward(self, x): @@ -286,11 +321,13 @@ class Generator(nn.Module): self.in_channels = emb_dim self.out_channels = 3 block_in_ch = self.nf * self.ch_mult[-1] - curr_res = self.resolution // 2 ** (self.num_resolutions-1) + curr_res = self.resolution // 2 ** (self.num_resolutions - 1) blocks = [] # initial conv - blocks.append(nn.Conv2d(self.in_channels, block_in_ch, kernel_size=3, stride=1, padding=1)) + blocks.append( + nn.Conv2d(self.in_channels, block_in_ch, kernel_size=3, stride=1, padding=1) + ) # non-local attention block blocks.append(ResBlock(block_in_ch, block_in_ch)) @@ -312,11 +349,14 @@ class Generator(nn.Module): curr_res = curr_res * 2 blocks.append(normalize(block_in_ch)) - blocks.append(nn.Conv2d(block_in_ch, self.out_channels, kernel_size=3, stride=1, padding=1)) + blocks.append( + nn.Conv2d( + block_in_ch, self.out_channels, kernel_size=3, stride=1, padding=1 + ) + ) self.blocks = nn.ModuleList(blocks) - def forward(self, x): for block in self.blocks: x = block(x) @@ -326,8 +366,21 @@ class Generator(nn.Module): @ARCH_REGISTRY.register() class VQAutoEncoder(nn.Module): - def __init__(self, img_size, nf, ch_mult, quantizer="nearest", res_blocks=2, attn_resolutions=[16], codebook_size=1024, emb_dim=256, - beta=0.25, gumbel_straight_through=False, gumbel_kl_weight=1e-8, model_path=None): + def __init__( + self, + img_size, + nf, + ch_mult, + quantizer="nearest", + res_blocks=2, + attn_resolutions=[16], + codebook_size=1024, + emb_dim=256, + beta=0.25, + gumbel_straight_through=False, + gumbel_kl_weight=1e-8, + model_path=None, + ): super().__init__() logger = get_root_logger() self.in_channels = 3 @@ -346,11 +399,13 @@ class VQAutoEncoder(nn.Module): self.ch_mult, self.n_blocks, self.resolution, - self.attn_resolutions + self.attn_resolutions, ) if self.quantizer_type == "nearest": - self.beta = beta #0.25 - self.quantize = VectorQuantizer(self.codebook_size, self.embed_dim, self.beta) + self.beta = beta # 0.25 + self.quantize = VectorQuantizer( + self.codebook_size, self.embed_dim, self.beta + ) elif self.quantizer_type == "gumbel": self.gumbel_num_hiddens = emb_dim self.straight_through = gumbel_straight_through @@ -360,7 +415,7 @@ class VQAutoEncoder(nn.Module): self.embed_dim, self.gumbel_num_hiddens, self.straight_through, - self.kl_weight + self.kl_weight, ) self.generator = Generator( self.nf, @@ -368,20 +423,23 @@ class VQAutoEncoder(nn.Module): self.ch_mult, self.n_blocks, self.resolution, - self.attn_resolutions + self.attn_resolutions, ) if model_path is not None: - chkpt = torch.load(model_path, map_location='cpu') - if 'params_ema' in chkpt: - self.load_state_dict(torch.load(model_path, map_location='cpu')['params_ema']) - logger.info(f'vqgan is loaded from: {model_path} [params_ema]') - elif 'params' in chkpt: - self.load_state_dict(torch.load(model_path, map_location='cpu')['params']) - logger.info(f'vqgan is loaded from: {model_path} [params]') + chkpt = torch.load(model_path, map_location="cpu") + if "params_ema" in chkpt: + self.load_state_dict( + torch.load(model_path, map_location="cpu")["params_ema"] + ) + logger.info(f"vqgan is loaded from: {model_path} [params_ema]") + elif "params" in chkpt: + self.load_state_dict( + torch.load(model_path, map_location="cpu")["params"] + ) + logger.info(f"vqgan is loaded from: {model_path} [params]") else: - raise ValueError(f'Wrong params!') - + raise ValueError(f"Wrong params!") def forward(self, x): x = self.encoder(x) @@ -390,46 +448,67 @@ class VQAutoEncoder(nn.Module): return x, codebook_loss, quant_stats - # patch based discriminator @ARCH_REGISTRY.register() class VQGANDiscriminator(nn.Module): def __init__(self, nc=3, ndf=64, n_layers=4, model_path=None): super().__init__() - layers = [nn.Conv2d(nc, ndf, kernel_size=4, stride=2, padding=1), nn.LeakyReLU(0.2, True)] + layers = [ + nn.Conv2d(nc, ndf, kernel_size=4, stride=2, padding=1), + nn.LeakyReLU(0.2, True), + ] ndf_mult = 1 ndf_mult_prev = 1 for n in range(1, n_layers): # gradually increase the number of filters ndf_mult_prev = ndf_mult - ndf_mult = min(2 ** n, 8) + ndf_mult = min(2**n, 8) layers += [ - nn.Conv2d(ndf * ndf_mult_prev, ndf * ndf_mult, kernel_size=4, stride=2, padding=1, bias=False), + nn.Conv2d( + ndf * ndf_mult_prev, + ndf * ndf_mult, + kernel_size=4, + stride=2, + padding=1, + bias=False, + ), nn.BatchNorm2d(ndf * ndf_mult), - nn.LeakyReLU(0.2, True) + nn.LeakyReLU(0.2, True), ] ndf_mult_prev = ndf_mult - ndf_mult = min(2 ** n_layers, 8) + ndf_mult = min(2**n_layers, 8) layers += [ - nn.Conv2d(ndf * ndf_mult_prev, ndf * ndf_mult, kernel_size=4, stride=1, padding=1, bias=False), + nn.Conv2d( + ndf * ndf_mult_prev, + ndf * ndf_mult, + kernel_size=4, + stride=1, + padding=1, + bias=False, + ), nn.BatchNorm2d(ndf * ndf_mult), - nn.LeakyReLU(0.2, True) + nn.LeakyReLU(0.2, True), ] layers += [ - nn.Conv2d(ndf * ndf_mult, 1, kernel_size=4, stride=1, padding=1)] # output 1 channel prediction map + nn.Conv2d(ndf * ndf_mult, 1, kernel_size=4, stride=1, padding=1) + ] # output 1 channel prediction map self.main = nn.Sequential(*layers) if model_path is not None: - chkpt = torch.load(model_path, map_location='cpu') - if 'params_d' in chkpt: - self.load_state_dict(torch.load(model_path, map_location='cpu')['params_d']) - elif 'params' in chkpt: - self.load_state_dict(torch.load(model_path, map_location='cpu')['params']) + chkpt = torch.load(model_path, map_location="cpu") + if "params_d" in chkpt: + self.load_state_dict( + torch.load(model_path, map_location="cpu")["params_d"] + ) + elif "params" in chkpt: + self.load_state_dict( + torch.load(model_path, map_location="cpu")["params"] + ) else: - raise ValueError(f'Wrong params!') + raise ValueError(f"Wrong params!") def forward(self, x): return self.main(x) diff --git a/invokeai/backend/stable_diffusion/__init__.py b/invokeai/backend/stable_diffusion/__init__.py index 13e28ce22d..05886f7b10 100644 --- a/invokeai/backend/stable_diffusion/__init__.py +++ b/invokeai/backend/stable_diffusion/__init__.py @@ -1,16 +1,16 @@ -''' +""" Initialization file for the invokeai.backend.stable_diffusion package -''' +""" +from .concepts_lib import HuggingFaceConceptsLibrary +from .diffusers_pipeline import ( + ConditioningData, + PipelineIntermediateState, + StableDiffusionGeneratorPipeline, +) from .diffusion import InvokeAIDiffuserComponent +from .diffusion.cross_attention_map_saving import AttentionMapSaver from .diffusion.ddim import DDIMSampler from .diffusion.ksampler import KSampler from .diffusion.plms import PLMSSampler -from .diffusion.cross_attention_map_saving import AttentionMapSaver from .diffusion.shared_invokeai_diffusion import PostprocessingSettings from .textual_inversion_manager import TextualInversionManager -from .concepts_lib import HuggingFaceConceptsLibrary -from .diffusers_pipeline import (StableDiffusionGeneratorPipeline, - ConditioningData, - PipelineIntermediateState, - StableDiffusionGeneratorPipeline - ) diff --git a/invokeai/backend/stable_diffusion/attention.py b/invokeai/backend/stable_diffusion/attention.py index 27fc5cf4bd..484b42c0bd 100644 --- a/invokeai/backend/stable_diffusion/attention.py +++ b/invokeai/backend/stable_diffusion/attention.py @@ -1,21 +1,22 @@ -from inspect import isfunction import math +from inspect import isfunction from typing import Callable, Optional import torch import torch.nn.functional as F -from torch import nn, einsum from einops import rearrange, repeat +from torch import einsum, nn from .diffusion import InvokeAICrossAttentionMixin from .diffusionmodules.util import checkpoint + def exists(val): return val is not None def uniq(arr): - return{el: True for el in arr}.keys() + return {el: True for el in arr}.keys() def default(val, d): @@ -47,19 +48,18 @@ class GEGLU(nn.Module): class FeedForward(nn.Module): - def __init__(self, dim, dim_out=None, mult=4, glu=False, dropout=0.): + def __init__(self, dim, dim_out=None, mult=4, glu=False, dropout=0.0): super().__init__() inner_dim = int(dim * mult) dim_out = default(dim_out, dim) - project_in = nn.Sequential( - nn.Linear(dim, inner_dim), - nn.GELU() - ) if not glu else GEGLU(dim, inner_dim) + project_in = ( + nn.Sequential(nn.Linear(dim, inner_dim), nn.GELU()) + if not glu + else GEGLU(dim, inner_dim) + ) self.net = nn.Sequential( - project_in, - nn.Dropout(dropout), - nn.Linear(inner_dim, dim_out) + project_in, nn.Dropout(dropout), nn.Linear(inner_dim, dim_out) ) def forward(self, x): @@ -76,7 +76,9 @@ def zero_module(module): def Normalize(in_channels): - return torch.nn.GroupNorm(num_groups=32, num_channels=in_channels, eps=1e-6, affine=True) + return torch.nn.GroupNorm( + num_groups=32, num_channels=in_channels, eps=1e-6, affine=True + ) class LinearAttention(nn.Module): @@ -84,17 +86,21 @@ class LinearAttention(nn.Module): super().__init__() self.heads = heads hidden_dim = dim_head * heads - self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias = False) + self.to_qkv = nn.Conv2d(dim, hidden_dim * 3, 1, bias=False) self.to_out = nn.Conv2d(hidden_dim, dim, 1) def forward(self, x): b, c, h, w = x.shape qkv = self.to_qkv(x) - q, k, v = rearrange(qkv, 'b (qkv heads c) h w -> qkv b heads c (h w)', heads = self.heads, qkv=3) + q, k, v = rearrange( + qkv, "b (qkv heads c) h w -> qkv b heads c (h w)", heads=self.heads, qkv=3 + ) k = k.softmax(dim=-1) - context = torch.einsum('bhdn,bhen->bhde', k, v) - out = torch.einsum('bhde,bhdn->bhen', context, q) - out = rearrange(out, 'b heads c (h w) -> b (heads c) h w', heads=self.heads, h=h, w=w) + context = torch.einsum("bhdn,bhen->bhde", k, v) + out = torch.einsum("bhde,bhdn->bhen", context, q) + out = rearrange( + out, "b heads c (h w) -> b (heads c) h w", heads=self.heads, h=h, w=w + ) return self.to_out(out) @@ -104,26 +110,18 @@ class SpatialSelfAttention(nn.Module): self.in_channels = in_channels self.norm = Normalize(in_channels) - self.q = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0) - self.k = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0) - self.v = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0) - self.proj_out = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0) + self.q = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=1, stride=1, padding=0 + ) + self.k = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=1, stride=1, padding=0 + ) + self.v = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=1, stride=1, padding=0 + ) + self.proj_out = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=1, stride=1, padding=0 + ) def forward(self, x): h_ = x @@ -133,43 +131,45 @@ class SpatialSelfAttention(nn.Module): v = self.v(h_) # compute attention - b,c,h,w = q.shape - q = rearrange(q, 'b c h w -> b (h w) c') - k = rearrange(k, 'b c h w -> b c (h w)') - w_ = torch.einsum('bij,bjk->bik', q, k) + b, c, h, w = q.shape + q = rearrange(q, "b c h w -> b (h w) c") + k = rearrange(k, "b c h w -> b c (h w)") + w_ = torch.einsum("bij,bjk->bik", q, k) - w_ = w_ * (int(c)**(-0.5)) + w_ = w_ * (int(c) ** (-0.5)) w_ = torch.nn.functional.softmax(w_, dim=2) # attend to values - v = rearrange(v, 'b c h w -> b c (h w)') - w_ = rearrange(w_, 'b i j -> b j i') - h_ = torch.einsum('bij,bjk->bik', v, w_) - h_ = rearrange(h_, 'b c (h w) -> b c h w', h=h) + v = rearrange(v, "b c h w -> b c (h w)") + w_ = rearrange(w_, "b i j -> b j i") + h_ = torch.einsum("bij,bjk->bik", v, w_) + h_ = rearrange(h_, "b c (h w) -> b c h w", h=h) h_ = self.proj_out(h_) - return x+h_ + return x + h_ + def get_mem_free_total(device): - #only on cuda + # only on cuda if not torch.cuda.is_available(): return None stats = torch.cuda.memory_stats(device) - mem_active = stats['active_bytes.all.current'] - mem_reserved = stats['reserved_bytes.all.current'] + mem_active = stats["active_bytes.all.current"] + mem_reserved = stats["reserved_bytes.all.current"] mem_free_cuda, _ = torch.cuda.mem_get_info(device) mem_free_torch = mem_reserved - mem_active mem_free_total = mem_free_cuda + mem_free_torch return mem_free_total + class CrossAttention(nn.Module, InvokeAICrossAttentionMixin): - def __init__(self, query_dim, context_dim=None, heads=8, dim_head=64, dropout=0.): + def __init__(self, query_dim, context_dim=None, heads=8, dim_head=64, dropout=0.0): super().__init__() InvokeAICrossAttentionMixin.__init__(self) inner_dim = dim_head * heads context_dim = default(context_dim, query_dim) - self.scale = dim_head ** -0.5 + self.scale = dim_head**-0.5 self.heads = heads self.to_q = nn.Linear(query_dim, inner_dim, bias=False) @@ -177,8 +177,7 @@ class CrossAttention(nn.Module, InvokeAICrossAttentionMixin): self.to_v = nn.Linear(context_dim, inner_dim, bias=False) self.to_out = nn.Sequential( - nn.Linear(inner_dim, query_dim), - nn.Dropout(dropout) + nn.Linear(inner_dim, query_dim), nn.Dropout(dropout) ) def forward(self, x, context=None, mask=None): @@ -190,7 +189,7 @@ class CrossAttention(nn.Module, InvokeAICrossAttentionMixin): v = self.to_v(context) del context, x - q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v)) + q, k, v = map(lambda t: rearrange(t, "b n (h d) -> (b h) n d", h=h), (q, k, v)) # don't apply scale twice cached_scale = self.scale @@ -198,29 +197,45 @@ class CrossAttention(nn.Module, InvokeAICrossAttentionMixin): r = self.get_invokeai_attention_mem_efficient(q, k, v) self.scale = cached_scale - hidden_states = rearrange(r, '(b h) n d -> b n (h d)', h=h) + hidden_states = rearrange(r, "(b h) n d -> b n (h d)", h=h) return self.to_out(hidden_states) - - class BasicTransformerBlock(nn.Module): - def __init__(self, dim, n_heads, d_head, dropout=0., context_dim=None, gated_ff=True, checkpoint=True): + def __init__( + self, + dim, + n_heads, + d_head, + dropout=0.0, + context_dim=None, + gated_ff=True, + checkpoint=True, + ): super().__init__() - self.attn1 = CrossAttention(query_dim=dim, heads=n_heads, dim_head=d_head, dropout=dropout) # is a self-attention + self.attn1 = CrossAttention( + query_dim=dim, heads=n_heads, dim_head=d_head, dropout=dropout + ) # is a self-attention self.ff = FeedForward(dim, dropout=dropout, glu=gated_ff) - self.attn2 = CrossAttention(query_dim=dim, context_dim=context_dim, - heads=n_heads, dim_head=d_head, dropout=dropout) # is self-attn if context is none + self.attn2 = CrossAttention( + query_dim=dim, + context_dim=context_dim, + heads=n_heads, + dim_head=d_head, + dropout=dropout, + ) # is self-attn if context is none self.norm1 = nn.LayerNorm(dim) self.norm2 = nn.LayerNorm(dim) self.norm3 = nn.LayerNorm(dim) self.checkpoint = checkpoint def forward(self, x, context=None): - return checkpoint(self._forward, (x, context), self.parameters(), self.checkpoint) + return checkpoint( + self._forward, (x, context), self.parameters(), self.checkpoint + ) def _forward(self, x, context=None): - x = x.contiguous() if x.device.type == 'mps' else x + x = x.contiguous() if x.device.type == "mps" else x x += self.attn1(self.norm1(x.clone())) x += self.attn2(self.norm2(x.clone()), context=context) x += self.ff(self.norm3(x.clone())) @@ -235,29 +250,31 @@ class SpatialTransformer(nn.Module): Then apply standard transformer action. Finally, reshape to image """ - def __init__(self, in_channels, n_heads, d_head, - depth=1, dropout=0., context_dim=None): + + def __init__( + self, in_channels, n_heads, d_head, depth=1, dropout=0.0, context_dim=None + ): super().__init__() self.in_channels = in_channels inner_dim = n_heads * d_head self.norm = Normalize(in_channels) - self.proj_in = nn.Conv2d(in_channels, - inner_dim, - kernel_size=1, - stride=1, - padding=0) - - self.transformer_blocks = nn.ModuleList( - [BasicTransformerBlock(inner_dim, n_heads, d_head, dropout=dropout, context_dim=context_dim) - for d in range(depth)] + self.proj_in = nn.Conv2d( + in_channels, inner_dim, kernel_size=1, stride=1, padding=0 ) - self.proj_out = zero_module(nn.Conv2d(inner_dim, - in_channels, - kernel_size=1, - stride=1, - padding=0)) + self.transformer_blocks = nn.ModuleList( + [ + BasicTransformerBlock( + inner_dim, n_heads, d_head, dropout=dropout, context_dim=context_dim + ) + for d in range(depth) + ] + ) + + self.proj_out = zero_module( + nn.Conv2d(inner_dim, in_channels, kernel_size=1, stride=1, padding=0) + ) def forward(self, x, context=None): # note: if no context is given, cross-attention defaults to self-attention @@ -265,9 +282,9 @@ class SpatialTransformer(nn.Module): x_in = x x = self.norm(x) x = self.proj_in(x) - x = rearrange(x, 'b c h w -> b (h w) c') + x = rearrange(x, "b c h w -> b (h w) c") for block in self.transformer_blocks: x = block(x, context=context) - x = rearrange(x, 'b (h w) c -> b c h w', h=h, w=w) + x = rearrange(x, "b (h w) c -> b c h w", h=h, w=w) x = self.proj_out(x) return x + x_in diff --git a/invokeai/backend/stable_diffusion/autoencoder.py b/invokeai/backend/stable_diffusion/autoencoder.py index ba081bab28..2bc7fa84f6 100644 --- a/invokeai/backend/stable_diffusion/autoencoder.py +++ b/invokeai/backend/stable_diffusion/autoencoder.py @@ -1,16 +1,13 @@ -import torch -import pytorch_lightning as pl -import torch.nn.functional as F from contextlib import contextmanager +import pytorch_lightning as pl +import torch +import torch.nn.functional as F from taming.modules.vqvae.quantize import VectorQuantizer2 as VectorQuantizer -from .diffusionmodules.model import Encoder, Decoder -from .distributions.distributions import ( - DiagonalGaussianDistribution, -) - from ..util import instantiate_from_config +from .diffusionmodules.model import Decoder, Encoder +from .distributions.distributions import DiagonalGaussianDistribution class VQModel(pl.LightningModule): @@ -22,7 +19,7 @@ class VQModel(pl.LightningModule): embed_dim, ckpt_path=None, ignore_keys=[], - image_key='image', + image_key="image", colorize_nlabels=None, monitor=None, batch_resize_range=None, @@ -46,27 +43,23 @@ class VQModel(pl.LightningModule): remap=remap, sane_index_shape=sane_index_shape, ) - self.quant_conv = torch.nn.Conv2d(ddconfig['z_channels'], embed_dim, 1) - self.post_quant_conv = torch.nn.Conv2d( - embed_dim, ddconfig['z_channels'], 1 - ) + self.quant_conv = torch.nn.Conv2d(ddconfig["z_channels"], embed_dim, 1) + self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1) if colorize_nlabels is not None: assert type(colorize_nlabels) == int - self.register_buffer( - 'colorize', torch.randn(3, colorize_nlabels, 1, 1) - ) + self.register_buffer("colorize", torch.randn(3, colorize_nlabels, 1, 1)) if monitor is not None: self.monitor = monitor self.batch_resize_range = batch_resize_range if self.batch_resize_range is not None: print( - f'{self.__class__.__name__}: Using per-batch resizing in range {batch_resize_range}.' + f"{self.__class__.__name__}: Using per-batch resizing in range {batch_resize_range}." ) self.use_ema = use_ema if self.use_ema: self.model_ema = LitEma(self) - print(f'>> Keeping EMAs of {len(list(self.model_ema.buffers()))}.') + print(f">> Keeping EMAs of {len(list(self.model_ema.buffers()))}.") if ckpt_path is not None: self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys) @@ -79,30 +72,30 @@ class VQModel(pl.LightningModule): self.model_ema.store(self.parameters()) self.model_ema.copy_to(self) if context is not None: - print(f'{context}: Switched to EMA weights') + print(f"{context}: Switched to EMA weights") try: yield None finally: if self.use_ema: self.model_ema.restore(self.parameters()) if context is not None: - print(f'{context}: Restored training weights') + print(f"{context}: Restored training weights") def init_from_ckpt(self, path, ignore_keys=list()): - sd = torch.load(path, map_location='cpu')['state_dict'] + sd = torch.load(path, map_location="cpu")["state_dict"] keys = list(sd.keys()) for k in keys: for ik in ignore_keys: if k.startswith(ik): - print('Deleting key {} from state_dict.'.format(k)) + print("Deleting key {} from state_dict.".format(k)) del sd[k] missing, unexpected = self.load_state_dict(sd, strict=False) print( - f'Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys' + f"Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys" ) if len(missing) > 0: - print(f'Missing Keys: {missing}') - print(f'Unexpected Keys: {unexpected}') + print(f"Missing Keys: {missing}") + print(f"Unexpected Keys: {unexpected}") def on_train_batch_end(self, *args, **kwargs): if self.use_ema: @@ -140,11 +133,7 @@ class VQModel(pl.LightningModule): x = batch[k] if len(x.shape) == 3: x = x[..., None] - x = ( - x.permute(0, 3, 1, 2) - .to(memory_format=torch.contiguous_format) - .float() - ) + x = x.permute(0, 3, 1, 2).to(memory_format=torch.contiguous_format).float() if self.batch_resize_range is not None: lower_size = self.batch_resize_range[0] upper_size = self.batch_resize_range[1] @@ -156,7 +145,7 @@ class VQModel(pl.LightningModule): np.arange(lower_size, upper_size + 16, 16) ) if new_resize != x.shape[2]: - x = F.interpolate(x, size=new_resize, mode='bicubic') + x = F.interpolate(x, size=new_resize, mode="bicubic") x = x.detach() return x @@ -175,7 +164,7 @@ class VQModel(pl.LightningModule): optimizer_idx, self.global_step, last_layer=self.get_last_layer(), - split='train', + split="train", predicted_indices=ind, ) @@ -197,7 +186,7 @@ class VQModel(pl.LightningModule): optimizer_idx, self.global_step, last_layer=self.get_last_layer(), - split='train', + split="train", ) self.log_dict( log_dict_disc, @@ -211,12 +200,10 @@ class VQModel(pl.LightningModule): def validation_step(self, batch, batch_idx): log_dict = self._validation_step(batch, batch_idx) with self.ema_scope(): - log_dict_ema = self._validation_step( - batch, batch_idx, suffix='_ema' - ) + log_dict_ema = self._validation_step(batch, batch_idx, suffix="_ema") return log_dict - def _validation_step(self, batch, batch_idx, suffix=''): + def _validation_step(self, batch, batch_idx, suffix=""): x = self.get_input(batch, self.image_key) xrec, qloss, ind = self(x, return_pred_indices=True) aeloss, log_dict_ae = self.loss( @@ -226,7 +213,7 @@ class VQModel(pl.LightningModule): 0, self.global_step, last_layer=self.get_last_layer(), - split='val' + suffix, + split="val" + suffix, predicted_indices=ind, ) @@ -237,12 +224,12 @@ class VQModel(pl.LightningModule): 1, self.global_step, last_layer=self.get_last_layer(), - split='val' + suffix, + split="val" + suffix, predicted_indices=ind, ) - rec_loss = log_dict_ae[f'val{suffix}/rec_loss'] + rec_loss = log_dict_ae[f"val{suffix}/rec_loss"] self.log( - f'val{suffix}/rec_loss', + f"val{suffix}/rec_loss", rec_loss, prog_bar=True, logger=True, @@ -251,7 +238,7 @@ class VQModel(pl.LightningModule): sync_dist=True, ) self.log( - f'val{suffix}/aeloss', + f"val{suffix}/aeloss", aeloss, prog_bar=True, logger=True, @@ -259,8 +246,8 @@ class VQModel(pl.LightningModule): on_epoch=True, sync_dist=True, ) - if version.parse(pl.__version__) >= version.parse('1.4.0'): - del log_dict_ae[f'val{suffix}/rec_loss'] + if version.parse(pl.__version__) >= version.parse("1.4.0"): + del log_dict_ae[f"val{suffix}/rec_loss"] self.log_dict(log_dict_ae) self.log_dict(log_dict_disc) return self.log_dict @@ -268,8 +255,8 @@ class VQModel(pl.LightningModule): def configure_optimizers(self): lr_d = self.learning_rate lr_g = self.lr_g_factor * self.learning_rate - print('lr_d', lr_d) - print('lr_g', lr_g) + print("lr_d", lr_d) + print("lr_g", lr_g) opt_ae = torch.optim.Adam( list(self.encoder.parameters()) + list(self.decoder.parameters()) @@ -286,21 +273,17 @@ class VQModel(pl.LightningModule): if self.scheduler_config is not None: scheduler = instantiate_from_config(self.scheduler_config) - print('Setting up LambdaLR scheduler...') + print("Setting up LambdaLR scheduler...") scheduler = [ { - 'scheduler': LambdaLR( - opt_ae, lr_lambda=scheduler.schedule - ), - 'interval': 'step', - 'frequency': 1, + "scheduler": LambdaLR(opt_ae, lr_lambda=scheduler.schedule), + "interval": "step", + "frequency": 1, }, { - 'scheduler': LambdaLR( - opt_disc, lr_lambda=scheduler.schedule - ), - 'interval': 'step', - 'frequency': 1, + "scheduler": LambdaLR(opt_disc, lr_lambda=scheduler.schedule), + "interval": "step", + "frequency": 1, }, ] return [opt_ae, opt_disc], scheduler @@ -314,7 +297,7 @@ class VQModel(pl.LightningModule): x = self.get_input(batch, self.image_key) x = x.to(self.device) if only_inputs: - log['inputs'] = x + log["inputs"] = x return log xrec, _ = self(x) if x.shape[1] > 3: @@ -322,22 +305,20 @@ class VQModel(pl.LightningModule): assert xrec.shape[1] > 3 x = self.to_rgb(x) xrec = self.to_rgb(xrec) - log['inputs'] = x - log['reconstructions'] = xrec + log["inputs"] = x + log["reconstructions"] = xrec if plot_ema: with self.ema_scope(): xrec_ema, _ = self(x) if x.shape[1] > 3: xrec_ema = self.to_rgb(xrec_ema) - log['reconstructions_ema'] = xrec_ema + log["reconstructions_ema"] = xrec_ema return log def to_rgb(self, x): - assert self.image_key == 'segmentation' - if not hasattr(self, 'colorize'): - self.register_buffer( - 'colorize', torch.randn(3, x.shape[1], 1, 1).to(x) - ) + assert self.image_key == "segmentation" + if not hasattr(self, "colorize"): + self.register_buffer("colorize", torch.randn(3, x.shape[1], 1, 1).to(x)) x = F.conv2d(x, weight=self.colorize) x = 2.0 * (x - x.min()) / (x.max() - x.min()) - 1.0 return x @@ -372,7 +353,7 @@ class AutoencoderKL(pl.LightningModule): embed_dim, ckpt_path=None, ignore_keys=[], - image_key='image', + image_key="image", colorize_nlabels=None, monitor=None, ): @@ -381,34 +362,28 @@ class AutoencoderKL(pl.LightningModule): self.encoder = Encoder(**ddconfig) self.decoder = Decoder(**ddconfig) self.loss = instantiate_from_config(lossconfig) - assert ddconfig['double_z'] - self.quant_conv = torch.nn.Conv2d( - 2 * ddconfig['z_channels'], 2 * embed_dim, 1 - ) - self.post_quant_conv = torch.nn.Conv2d( - embed_dim, ddconfig['z_channels'], 1 - ) + assert ddconfig["double_z"] + self.quant_conv = torch.nn.Conv2d(2 * ddconfig["z_channels"], 2 * embed_dim, 1) + self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1) self.embed_dim = embed_dim if colorize_nlabels is not None: assert type(colorize_nlabels) == int - self.register_buffer( - 'colorize', torch.randn(3, colorize_nlabels, 1, 1) - ) + self.register_buffer("colorize", torch.randn(3, colorize_nlabels, 1, 1)) if monitor is not None: self.monitor = monitor if ckpt_path is not None: self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys) def init_from_ckpt(self, path, ignore_keys=list()): - sd = torch.load(path, map_location='cpu')['state_dict'] + sd = torch.load(path, map_location="cpu")["state_dict"] keys = list(sd.keys()) for k in keys: for ik in ignore_keys: if k.startswith(ik): - print('Deleting key {} from state_dict.'.format(k)) + print("Deleting key {} from state_dict.".format(k)) del sd[k] self.load_state_dict(sd, strict=False) - print(f'Restored from {path}') + print(f"Restored from {path}") def encode(self, x): h = self.encoder(x) @@ -434,11 +409,7 @@ class AutoencoderKL(pl.LightningModule): x = batch[k] if len(x.shape) == 3: x = x[..., None] - x = ( - x.permute(0, 3, 1, 2) - .to(memory_format=torch.contiguous_format) - .float() - ) + x = x.permute(0, 3, 1, 2).to(memory_format=torch.contiguous_format).float() return x def training_step(self, batch, batch_idx, optimizer_idx): @@ -454,10 +425,10 @@ class AutoencoderKL(pl.LightningModule): optimizer_idx, self.global_step, last_layer=self.get_last_layer(), - split='train', + split="train", ) self.log( - 'aeloss', + "aeloss", aeloss, prog_bar=True, logger=True, @@ -482,11 +453,11 @@ class AutoencoderKL(pl.LightningModule): optimizer_idx, self.global_step, last_layer=self.get_last_layer(), - split='train', + split="train", ) self.log( - 'discloss', + "discloss", discloss, prog_bar=True, logger=True, @@ -512,7 +483,7 @@ class AutoencoderKL(pl.LightningModule): 0, self.global_step, last_layer=self.get_last_layer(), - split='val', + split="val", ) discloss, log_dict_disc = self.loss( @@ -522,10 +493,10 @@ class AutoencoderKL(pl.LightningModule): 1, self.global_step, last_layer=self.get_last_layer(), - split='val', + split="val", ) - self.log('val/rec_loss', log_dict_ae['val/rec_loss']) + self.log("val/rec_loss", log_dict_ae["val/rec_loss"]) self.log_dict(log_dict_ae) self.log_dict(log_dict_disc) return self.log_dict @@ -560,17 +531,15 @@ class AutoencoderKL(pl.LightningModule): assert xrec.shape[1] > 3 x = self.to_rgb(x) xrec = self.to_rgb(xrec) - log['samples'] = self.decode(torch.randn_like(posterior.sample())) - log['reconstructions'] = xrec - log['inputs'] = x + log["samples"] = self.decode(torch.randn_like(posterior.sample())) + log["reconstructions"] = xrec + log["inputs"] = x return log def to_rgb(self, x): - assert self.image_key == 'segmentation' - if not hasattr(self, 'colorize'): - self.register_buffer( - 'colorize', torch.randn(3, x.shape[1], 1, 1).to(x) - ) + assert self.image_key == "segmentation" + if not hasattr(self, "colorize"): + self.register_buffer("colorize", torch.randn(3, x.shape[1], 1, 1).to(x)) x = F.conv2d(x, weight=self.colorize) x = 2.0 * (x - x.min()) / (x.max() - x.min()) - 1.0 return x diff --git a/invokeai/backend/stable_diffusion/concepts_lib.py b/invokeai/backend/stable_diffusion/concepts_lib.py index 63d0a660a0..50ff6c16c9 100644 --- a/invokeai/backend/stable_diffusion/concepts_lib.py +++ b/invokeai/backend/stable_diffusion/concepts_lib.py @@ -8,32 +8,50 @@ import os import re import traceback from typing import Callable -from urllib import request, error as ul_error -from huggingface_hub import HfFolder, hf_hub_url, ModelSearchArguments, ModelFilter, HfApi +from urllib import error as ul_error +from urllib import request + +from huggingface_hub import ( + HfApi, + HfFolder, + ModelFilter, + ModelSearchArguments, + hf_hub_url, +) + from invokeai.backend.globals import Globals + class HuggingFaceConceptsLibrary(object): def __init__(self, root=None): - ''' + """ Initialize the Concepts object. May optionally pass a root directory. - ''' + """ self.root = root or Globals.root self.hf_api = HfApi() self.local_concepts = dict() self.concept_list = None self.concepts_loaded = dict() - self.triggers = dict() # concept name to trigger phrase - self.concept_names = dict() # trigger phrase to concept name - self.match_trigger = re.compile('(<[\w\- >]+>)') # trigger is slightly less restrictive than HF concept name - self.match_concept = re.compile('<([\w\-]+)>') # HF concept name can only contain A-Za-z0-9_- + self.triggers = dict() # concept name to trigger phrase + self.concept_names = dict() # trigger phrase to concept name + self.match_trigger = re.compile( + "(<[\w\- >]+>)" + ) # trigger is slightly less restrictive than HF concept name + self.match_concept = re.compile( + "<([\w\-]+)>" + ) # HF concept name can only contain A-Za-z0-9_- - def list_concepts(self)->list: - ''' + def list_concepts(self) -> list: + """ Return a list of all the concepts by name, without the 'sd-concepts-library' part. Also adds local concepts in invokeai/embeddings folder. - ''' - local_concepts_now = self.get_local_concepts(os.path.join(self.root, 'embeddings')) - local_concepts_to_add = set(local_concepts_now).difference(set(self.local_concepts)) + """ + local_concepts_now = self.get_local_concepts( + os.path.join(self.root, "embeddings") + ) + local_concepts_to_add = set(local_concepts_now).difference( + set(self.local_concepts) + ) self.local_concepts.update(local_concepts_now) if self.concept_list is not None: @@ -43,83 +61,96 @@ class HuggingFaceConceptsLibrary(object): return self.concept_list else: try: - models = self.hf_api.list_models(filter=ModelFilter(model_name='sd-concepts-library/')) - self.concept_list = [a.id.split('/')[1] for a in models] + models = self.hf_api.list_models( + filter=ModelFilter(model_name="sd-concepts-library/") + ) + self.concept_list = [a.id.split("/")[1] for a in models] # when init, add all in dir. when not init, add only concepts added between init and now self.concept_list.extend(list(local_concepts_to_add)) except Exception as e: - print(f' ** WARNING: Hugging Face textual inversion concepts libraries could not be loaded. The error was {str(e)}.') - print(' ** You may load .bin and .pt file(s) manually using the --embedding_directory argument.') + print( + f" ** WARNING: Hugging Face textual inversion concepts libraries could not be loaded. The error was {str(e)}." + ) + print( + " ** You may load .bin and .pt file(s) manually using the --embedding_directory argument." + ) return self.concept_list - def get_concept_model_path(self, concept_name:str)->str: - ''' + def get_concept_model_path(self, concept_name: str) -> str: + """ Returns the path to the 'learned_embeds.bin' file in the named concept. Returns None if invalid or cannot be downloaded. - ''' + """ if not concept_name in self.list_concepts(): - print(f'This concept is not a local embedding trigger, nor is it a HuggingFace concept. Generation will continue without the concept.') + print( + f"This concept is not a local embedding trigger, nor is it a HuggingFace concept. Generation will continue without the concept." + ) return None - return self.get_concept_file(concept_name.lower(),'learned_embeds.bin') + return self.get_concept_file(concept_name.lower(), "learned_embeds.bin") - def concept_to_trigger(self, concept_name:str)->str: - ''' + def concept_to_trigger(self, concept_name: str) -> str: + """ Given a concept name returns its trigger by looking in the "token_identifier.txt" file. - ''' + """ if concept_name in self.triggers: return self.triggers[concept_name] elif self.concept_is_local(concept_name): - trigger = f'<{concept_name}>' + trigger = f"<{concept_name}>" self.triggers[concept_name] = trigger self.concept_names[trigger] = concept_name return trigger - file = self.get_concept_file(concept_name, 'token_identifier.txt', local_only=True) + file = self.get_concept_file( + concept_name, "token_identifier.txt", local_only=True + ) if not file: return None - with open(file,'r') as f: + with open(file, "r") as f: trigger = f.readline() trigger = trigger.strip() self.triggers[concept_name] = trigger self.concept_names[trigger] = concept_name return trigger - def trigger_to_concept(self, trigger:str)->str: - ''' + def trigger_to_concept(self, trigger: str) -> str: + """ Given a trigger phrase, maps it to the concept library name. Only works if concept_to_trigger() has previously been called on this library. There needs to be a persistent database for this. - ''' - concept = self.concept_names.get(trigger,None) - return f'<{concept}>' if concept else f'{trigger}' + """ + concept = self.concept_names.get(trigger, None) + return f"<{concept}>" if concept else f"{trigger}" - def replace_triggers_with_concepts(self, prompt:str)->str: - ''' + def replace_triggers_with_concepts(self, prompt: str) -> str: + """ Given a prompt string that contains tags, replace these tags with the concept name. The reason for this is so that the concept names get stored in the prompt metadata. There is no controlling of colliding triggers in the SD library, so it is better to store the concept name (unique) than the concept trigger (not necessarily unique!) - ''' + """ if not prompt: return prompt triggers = self.match_trigger.findall(prompt) if not triggers: return prompt - def do_replace(match)->str: - return self.trigger_to_concept(match.group(1)) or f'<{match.group(1)}>' + def do_replace(match) -> str: + return self.trigger_to_concept(match.group(1)) or f"<{match.group(1)}>" + return self.match_trigger.sub(do_replace, prompt) - def replace_concepts_with_triggers(self, - prompt:str, - load_concepts_callback: Callable[[list], any], - excluded_tokens:list[str])->str: - ''' + def replace_concepts_with_triggers( + self, + prompt: str, + load_concepts_callback: Callable[[list], any], + excluded_tokens: list[str], + ) -> str: + """ Given a prompt string that contains `` tags, replace these tags with the appropriate trigger. @@ -128,20 +159,30 @@ class HuggingFaceConceptsLibrary(object): `excluded_tokens` are any tokens that should not be replaced, typically because they are trigger tokens from a locally-loaded embedding. - ''' + """ concepts = self.match_concept.findall(prompt) if not concepts: return prompt load_concepts_callback(concepts) - def do_replace(match)->str: - if excluded_tokens and f'<{match.group(1)}>' in excluded_tokens: - return f'<{match.group(1)}>' - return self.concept_to_trigger(match.group(1)) or f'<{match.group(1)}>' + def do_replace(match) -> str: + if excluded_tokens and f"<{match.group(1)}>" in excluded_tokens: + return f"<{match.group(1)}>" + return self.concept_to_trigger(match.group(1)) or f"<{match.group(1)}>" + return self.match_concept.sub(do_replace, prompt) - def get_concept_file(self, concept_name:str, file_name:str='learned_embeds.bin' , local_only:bool=False)->str: - if not (self.concept_is_downloaded(concept_name) or self.concept_is_local(concept_name) or local_only): + def get_concept_file( + self, + concept_name: str, + file_name: str = "learned_embeds.bin", + local_only: bool = False, + ) -> str: + if not ( + self.concept_is_downloaded(concept_name) + or self.concept_is_local(concept_name) + or local_only + ): self.download_concept(concept_name) # get local path in invokeai/embeddings if local concept @@ -153,19 +194,19 @@ class HuggingFaceConceptsLibrary(object): path = os.path.join(concept_path, file_name) return path if os.path.exists(path) else None - def concept_is_local(self, concept_name)->bool: + def concept_is_local(self, concept_name) -> bool: return concept_name in self.local_concepts - def concept_is_downloaded(self, concept_name)->bool: + def concept_is_downloaded(self, concept_name) -> bool: concept_directory = self._concept_path(concept_name) return os.path.exists(concept_directory) - def download_concept(self,concept_name)->bool: + def download_concept(self, concept_name) -> bool: repo_id = self._concept_id(concept_name) dest = self._concept_path(concept_name) access_token = HfFolder.get_token() - header = [("Authorization", f'Bearer {access_token}')] if access_token else [] + header = [("Authorization", f"Bearer {access_token}")] if access_token else [] opener = request.build_opener() opener.addheaders = header request.install_opener(opener) @@ -174,45 +215,59 @@ class HuggingFaceConceptsLibrary(object): succeeded = True bytes = 0 + def tally_download_size(chunk, size, total): nonlocal bytes - if chunk==0: + if chunk == 0: bytes += total - print(f'>> Downloading {repo_id}...',end='') + print(f">> Downloading {repo_id}...", end="") try: - for file in ('README.md','learned_embeds.bin','token_identifier.txt','type_of_concept.txt'): + for file in ( + "README.md", + "learned_embeds.bin", + "token_identifier.txt", + "type_of_concept.txt", + ): url = hf_hub_url(repo_id, file) - request.urlretrieve(url, os.path.join(dest,file),reporthook=tally_download_size) + request.urlretrieve( + url, os.path.join(dest, file), reporthook=tally_download_size + ) except ul_error.HTTPError as e: - if e.code==404: - print(f'This concept is not known to the Hugging Face library. Generation will continue without the concept.') + if e.code == 404: + print( + f"This concept is not known to the Hugging Face library. Generation will continue without the concept." + ) else: - print(f'Failed to download {concept_name}/{file} ({str(e)}. Generation will continue without the concept.)') + print( + f"Failed to download {concept_name}/{file} ({str(e)}. Generation will continue without the concept.)" + ) os.rmdir(dest) return False except ul_error.URLError as e: - print(f'ERROR: {str(e)}. This may reflect a network issue. Generation will continue without the concept.') + print( + f"ERROR: {str(e)}. This may reflect a network issue. Generation will continue without the concept." + ) os.rmdir(dest) return False - print('...{:.2f}Kb'.format(bytes/1024)) + print("...{:.2f}Kb".format(bytes / 1024)) return succeeded - def _concept_id(self, concept_name:str)->str: - return f'sd-concepts-library/{concept_name}' + def _concept_id(self, concept_name: str) -> str: + return f"sd-concepts-library/{concept_name}" - def _concept_path(self, concept_name:str)->str: - return os.path.join(self.root,'models','sd-concepts-library',concept_name) + def _concept_path(self, concept_name: str) -> str: + return os.path.join(self.root, "models", "sd-concepts-library", concept_name) - def _concept_local_path(self, concept_name:str)->str: + def _concept_local_path(self, concept_name: str) -> str: filename = self.local_concepts[concept_name] - return os.path.join(self.root,'embeddings',filename) + return os.path.join(self.root, "embeddings", filename) - def get_local_concepts(self, loc_dir:str): + def get_local_concepts(self, loc_dir: str): locs_dic = dict() if os.path.isdir(loc_dir): for file in os.listdir(loc_dir): f = os.path.splitext(file) - if f[1] == '.bin' or f[1] == '.pt': + if f[1] == ".bin" or f[1] == ".pt": locs_dic[f[0]] = file return locs_dic diff --git a/invokeai/backend/stable_diffusion/data/base.py b/invokeai/backend/stable_diffusion/data/base.py index de9493fc1e..1b6a138bf7 100644 --- a/invokeai/backend/stable_diffusion/data/base.py +++ b/invokeai/backend/stable_diffusion/data/base.py @@ -1,10 +1,6 @@ from abc import abstractmethod -from torch.utils.data import ( - Dataset, - ConcatDataset, - ChainDataset, - IterableDataset, -) + +from torch.utils.data import ChainDataset, ConcatDataset, Dataset, IterableDataset class Txt2ImgIterableBaseDataset(IterableDataset): @@ -19,9 +15,7 @@ class Txt2ImgIterableBaseDataset(IterableDataset): self.sample_ids = valid_ids self.size = size - print( - f'{self.__class__.__name__} dataset contains {self.__len__()} examples.' - ) + print(f"{self.__class__.__name__} dataset contains {self.__len__()} examples.") def __len__(self): return self.num_records diff --git a/invokeai/backend/stable_diffusion/data/imagenet.py b/invokeai/backend/stable_diffusion/data/imagenet.py index d155f6d6ae..84bad27590 100644 --- a/invokeai/backend/stable_diffusion/data/imagenet.py +++ b/invokeai/backend/stable_diffusion/data/imagenet.py @@ -1,31 +1,32 @@ -import os, yaml, pickle, shutil, tarfile, glob -import cv2 -import albumentations -import PIL -import numpy as np -import torchvision.transforms.functional as TF -from omegaconf import OmegaConf +import glob +import os +import pickle +import shutil +import tarfile from functools import partial -from PIL import Image -from tqdm import tqdm -from torch.utils.data import Dataset, Subset +import albumentations +import cv2 +import numpy as np +import PIL import taming.data.utils as tdu +import torchvision.transforms.functional as TF +import yaml +from ldm.modules.image_degradation import degradation_fn_bsr, degradation_fn_bsr_light +from omegaconf import OmegaConf +from PIL import Image from taming.data.imagenet import ( - str_to_indices, - give_synsets_from_indices, + ImagePaths, download, + give_synsets_from_indices, retrieve, + str_to_indices, ) -from taming.data.imagenet import ImagePaths - -from ldm.modules.image_degradation import ( - degradation_fn_bsr, - degradation_fn_bsr_light, -) +from torch.utils.data import Dataset, Subset +from tqdm import tqdm -def synset2idx(path_to_yaml='data/index_synset.yaml'): +def synset2idx(path_to_yaml="data/index_synset.yaml"): with open(path_to_yaml) as f: di2s = yaml.load(f) return dict((v, k) for k, v in di2s.items()) @@ -36,9 +37,7 @@ class ImageNetBase(Dataset): self.config = config or OmegaConf.create() if not type(self.config) == dict: self.config = OmegaConf.to_container(self.config) - self.keep_orig_class_label = self.config.get( - 'keep_orig_class_label', False - ) + self.keep_orig_class_label = self.config.get("keep_orig_class_label", False) self.process_images = True # if False we skip loading & processing images and self.data contains filepaths self._prepare() self._prepare_synset_to_human() @@ -58,21 +57,19 @@ class ImageNetBase(Dataset): def _filter_relpaths(self, relpaths): ignore = set( [ - 'n06596364_9591.JPEG', + "n06596364_9591.JPEG", ] ) - relpaths = [ - rpath for rpath in relpaths if not rpath.split('/')[-1] in ignore - ] - if 'sub_indices' in self.config: - indices = str_to_indices(self.config['sub_indices']) + relpaths = [rpath for rpath in relpaths if not rpath.split("/")[-1] in ignore] + if "sub_indices" in self.config: + indices = str_to_indices(self.config["sub_indices"]) synsets = give_synsets_from_indices( indices, path_to_yaml=self.idx2syn ) # returns a list of strings self.synset2idx = synset2idx(path_to_yaml=self.idx2syn) files = [] for rpath in relpaths: - syn = rpath.split('/')[0] + syn = rpath.split("/")[0] if syn in synsets: files.append(rpath) return files @@ -81,8 +78,8 @@ class ImageNetBase(Dataset): def _prepare_synset_to_human(self): SIZE = 2655750 - URL = 'https://heibox.uni-heidelberg.de/f/9f28e956cd304264bb82/?dl=1' - self.human_dict = os.path.join(self.root, 'synset_human.txt') + URL = "https://heibox.uni-heidelberg.de/f/9f28e956cd304264bb82/?dl=1" + self.human_dict = os.path.join(self.root, "synset_human.txt") if ( not os.path.exists(self.human_dict) or not os.path.getsize(self.human_dict) == SIZE @@ -90,64 +87,62 @@ class ImageNetBase(Dataset): download(URL, self.human_dict) def _prepare_idx_to_synset(self): - URL = 'https://heibox.uni-heidelberg.de/f/d835d5b6ceda4d3aa910/?dl=1' - self.idx2syn = os.path.join(self.root, 'index_synset.yaml') + URL = "https://heibox.uni-heidelberg.de/f/d835d5b6ceda4d3aa910/?dl=1" + self.idx2syn = os.path.join(self.root, "index_synset.yaml") if not os.path.exists(self.idx2syn): download(URL, self.idx2syn) def _prepare_human_to_integer_label(self): - URL = 'https://heibox.uni-heidelberg.de/f/2362b797d5be43b883f6/?dl=1' + URL = "https://heibox.uni-heidelberg.de/f/2362b797d5be43b883f6/?dl=1" self.human2integer = os.path.join( - self.root, 'imagenet1000_clsidx_to_labels.txt' + self.root, "imagenet1000_clsidx_to_labels.txt" ) if not os.path.exists(self.human2integer): download(URL, self.human2integer) - with open(self.human2integer, 'r') as f: + with open(self.human2integer, "r") as f: lines = f.read().splitlines() assert len(lines) == 1000 self.human2integer_dict = dict() for line in lines: - value, key = line.split(':') + value, key = line.split(":") self.human2integer_dict[key] = int(value) def _load(self): - with open(self.txt_filelist, 'r') as f: + with open(self.txt_filelist, "r") as f: self.relpaths = f.read().splitlines() l1 = len(self.relpaths) self.relpaths = self._filter_relpaths(self.relpaths) print( - 'Removed {} files from filelist during filtering.'.format( + "Removed {} files from filelist during filtering.".format( l1 - len(self.relpaths) ) ) - self.synsets = [p.split('/')[0] for p in self.relpaths] + self.synsets = [p.split("/")[0] for p in self.relpaths] self.abspaths = [os.path.join(self.datadir, p) for p in self.relpaths] unique_synsets = np.unique(self.synsets) - class_dict = dict( - (synset, i) for i, synset in enumerate(unique_synsets) - ) + class_dict = dict((synset, i) for i, synset in enumerate(unique_synsets)) if not self.keep_orig_class_label: self.class_labels = [class_dict[s] for s in self.synsets] else: self.class_labels = [self.synset2idx[s] for s in self.synsets] - with open(self.human_dict, 'r') as f: + with open(self.human_dict, "r") as f: human_dict = f.read().splitlines() human_dict = dict(line.split(maxsplit=1) for line in human_dict) self.human_labels = [human_dict[s] for s in self.synsets] labels = { - 'relpath': np.array(self.relpaths), - 'synsets': np.array(self.synsets), - 'class_label': np.array(self.class_labels), - 'human_label': np.array(self.human_labels), + "relpath": np.array(self.relpaths), + "synsets": np.array(self.synsets), + "class_label": np.array(self.class_labels), + "human_label": np.array(self.human_labels), } if self.process_images: - self.size = retrieve(self.config, 'size', default=256) + self.size = retrieve(self.config, "size", default=256) self.data = ImagePaths( self.abspaths, labels=labels, @@ -159,11 +154,11 @@ class ImageNetBase(Dataset): class ImageNetTrain(ImageNetBase): - NAME = 'ILSVRC2012_train' - URL = 'http://www.image-net.org/challenges/LSVRC/2012/' - AT_HASH = 'a306397ccf9c2ead27155983c254227c0fd938e2' + NAME = "ILSVRC2012_train" + URL = "http://www.image-net.org/challenges/LSVRC/2012/" + AT_HASH = "a306397ccf9c2ead27155983c254227c0fd938e2" FILES = [ - 'ILSVRC2012_img_train.tar', + "ILSVRC2012_img_train.tar", ] SIZES = [ 147897477120, @@ -178,20 +173,18 @@ class ImageNetTrain(ImageNetBase): if self.data_root: self.root = os.path.join(self.data_root, self.NAME) else: - cachedir = os.environ.get( - 'XDG_CACHE_HOME', os.path.expanduser('~/.cache') - ) - self.root = os.path.join(cachedir, 'autoencoders/data', self.NAME) + cachedir = os.environ.get("XDG_CACHE_HOME", os.path.expanduser("~/.cache")) + self.root = os.path.join(cachedir, "autoencoders/data", self.NAME) - self.datadir = os.path.join(self.root, 'data') - self.txt_filelist = os.path.join(self.root, 'filelist.txt') + self.datadir = os.path.join(self.root, "data") + self.txt_filelist = os.path.join(self.root, "filelist.txt") self.expected_length = 1281167 self.random_crop = retrieve( - self.config, 'ImageNetTrain/random_crop', default=True + self.config, "ImageNetTrain/random_crop", default=True ) if not tdu.is_prepared(self.root): # prep - print('Preparing dataset {} in {}'.format(self.NAME, self.root)) + print("Preparing dataset {} in {}".format(self.NAME, self.root)) datadir = self.datadir if not os.path.exists(datadir): @@ -205,37 +198,37 @@ class ImageNetTrain(ImageNetBase): atpath = at.get(self.AT_HASH, datastore=self.root) assert atpath == path - print('Extracting {} to {}'.format(path, datadir)) + print("Extracting {} to {}".format(path, datadir)) os.makedirs(datadir, exist_ok=True) - with tarfile.open(path, 'r:') as tar: + with tarfile.open(path, "r:") as tar: tar.extractall(path=datadir) - print('Extracting sub-tars.') - subpaths = sorted(glob.glob(os.path.join(datadir, '*.tar'))) + print("Extracting sub-tars.") + subpaths = sorted(glob.glob(os.path.join(datadir, "*.tar"))) for subpath in tqdm(subpaths): - subdir = subpath[: -len('.tar')] + subdir = subpath[: -len(".tar")] os.makedirs(subdir, exist_ok=True) - with tarfile.open(subpath, 'r:') as tar: + with tarfile.open(subpath, "r:") as tar: tar.extractall(path=subdir) - filelist = glob.glob(os.path.join(datadir, '**', '*.JPEG')) + filelist = glob.glob(os.path.join(datadir, "**", "*.JPEG")) filelist = [os.path.relpath(p, start=datadir) for p in filelist] filelist = sorted(filelist) - filelist = '\n'.join(filelist) + '\n' - with open(self.txt_filelist, 'w') as f: + filelist = "\n".join(filelist) + "\n" + with open(self.txt_filelist, "w") as f: f.write(filelist) tdu.mark_prepared(self.root) class ImageNetValidation(ImageNetBase): - NAME = 'ILSVRC2012_validation' - URL = 'http://www.image-net.org/challenges/LSVRC/2012/' - AT_HASH = '5d6d0df7ed81efd49ca99ea4737e0ae5e3a5f2e5' - VS_URL = 'https://heibox.uni-heidelberg.de/f/3e0f6e9c624e45f2bd73/?dl=1' + NAME = "ILSVRC2012_validation" + URL = "http://www.image-net.org/challenges/LSVRC/2012/" + AT_HASH = "5d6d0df7ed81efd49ca99ea4737e0ae5e3a5f2e5" + VS_URL = "https://heibox.uni-heidelberg.de/f/3e0f6e9c624e45f2bd73/?dl=1" FILES = [ - 'ILSVRC2012_img_val.tar', - 'validation_synset.txt', + "ILSVRC2012_img_val.tar", + "validation_synset.txt", ] SIZES = [ 6744924160, @@ -251,19 +244,17 @@ class ImageNetValidation(ImageNetBase): if self.data_root: self.root = os.path.join(self.data_root, self.NAME) else: - cachedir = os.environ.get( - 'XDG_CACHE_HOME', os.path.expanduser('~/.cache') - ) - self.root = os.path.join(cachedir, 'autoencoders/data', self.NAME) - self.datadir = os.path.join(self.root, 'data') - self.txt_filelist = os.path.join(self.root, 'filelist.txt') + cachedir = os.environ.get("XDG_CACHE_HOME", os.path.expanduser("~/.cache")) + self.root = os.path.join(cachedir, "autoencoders/data", self.NAME) + self.datadir = os.path.join(self.root, "data") + self.txt_filelist = os.path.join(self.root, "filelist.txt") self.expected_length = 50000 self.random_crop = retrieve( - self.config, 'ImageNetValidation/random_crop', default=False + self.config, "ImageNetValidation/random_crop", default=False ) if not tdu.is_prepared(self.root): # prep - print('Preparing dataset {} in {}'.format(self.NAME, self.root)) + print("Preparing dataset {} in {}".format(self.NAME, self.root)) datadir = self.datadir if not os.path.exists(datadir): @@ -277,9 +268,9 @@ class ImageNetValidation(ImageNetBase): atpath = at.get(self.AT_HASH, datastore=self.root) assert atpath == path - print('Extracting {} to {}'.format(path, datadir)) + print("Extracting {} to {}".format(path, datadir)) os.makedirs(datadir, exist_ok=True) - with tarfile.open(path, 'r:') as tar: + with tarfile.open(path, "r:") as tar: tar.extractall(path=datadir) vspath = os.path.join(self.root, self.FILES[1]) @@ -289,11 +280,11 @@ class ImageNetValidation(ImageNetBase): ): download(self.VS_URL, vspath) - with open(vspath, 'r') as f: + with open(vspath, "r") as f: synset_dict = f.read().splitlines() synset_dict = dict(line.split() for line in synset_dict) - print('Reorganizing into synset folders') + print("Reorganizing into synset folders") synsets = np.unique(list(synset_dict.values())) for s in synsets: os.makedirs(os.path.join(datadir, s), exist_ok=True) @@ -302,11 +293,11 @@ class ImageNetValidation(ImageNetBase): dst = os.path.join(datadir, v) shutil.move(src, dst) - filelist = glob.glob(os.path.join(datadir, '**', '*.JPEG')) + filelist = glob.glob(os.path.join(datadir, "**", "*.JPEG")) filelist = [os.path.relpath(p, start=datadir) for p in filelist] filelist = sorted(filelist) - filelist = '\n'.join(filelist) + '\n' - with open(self.txt_filelist, 'w') as f: + filelist = "\n".join(filelist) + "\n" + with open(self.txt_filelist, "w") as f: f.write(filelist) tdu.mark_prepared(self.root) @@ -356,32 +347,28 @@ class ImageNetSR(Dataset): False # gets reset later if incase interp_op is from pillow ) - if degradation == 'bsrgan': - self.degradation_process = partial( - degradation_fn_bsr, sf=downscale_f - ) + if degradation == "bsrgan": + self.degradation_process = partial(degradation_fn_bsr, sf=downscale_f) - elif degradation == 'bsrgan_light': - self.degradation_process = partial( - degradation_fn_bsr_light, sf=downscale_f - ) + elif degradation == "bsrgan_light": + self.degradation_process = partial(degradation_fn_bsr_light, sf=downscale_f) else: interpolation_fn = { - 'cv_nearest': cv2.INTER_NEAREST, - 'cv_bilinear': cv2.INTER_LINEAR, - 'cv_bicubic': cv2.INTER_CUBIC, - 'cv_area': cv2.INTER_AREA, - 'cv_lanczos': cv2.INTER_LANCZOS4, - 'pil_nearest': PIL.Image.NEAREST, - 'pil_bilinear': PIL.Image.BILINEAR, - 'pil_bicubic': PIL.Image.BICUBIC, - 'pil_box': PIL.Image.BOX, - 'pil_hamming': PIL.Image.HAMMING, - 'pil_lanczos': PIL.Image.LANCZOS, + "cv_nearest": cv2.INTER_NEAREST, + "cv_bilinear": cv2.INTER_LINEAR, + "cv_bicubic": cv2.INTER_CUBIC, + "cv_area": cv2.INTER_AREA, + "cv_lanczos": cv2.INTER_LANCZOS4, + "pil_nearest": PIL.Image.NEAREST, + "pil_bilinear": PIL.Image.BILINEAR, + "pil_bicubic": PIL.Image.BICUBIC, + "pil_box": PIL.Image.BOX, + "pil_hamming": PIL.Image.HAMMING, + "pil_lanczos": PIL.Image.LANCZOS, }[degradation] - self.pil_interpolation = degradation.startswith('pil_') + self.pil_interpolation = degradation.startswith("pil_") if self.pil_interpolation: self.degradation_process = partial( @@ -400,10 +387,10 @@ class ImageNetSR(Dataset): def __getitem__(self, i): example = self.base[i] - image = Image.open(example['file_path_']) + image = Image.open(example["file_path_"]) - if not image.mode == 'RGB': - image = image.convert('RGB') + if not image.mode == "RGB": + image = image.convert("RGB") image = np.array(image).astype(np.uint8) @@ -423,8 +410,8 @@ class ImageNetSR(Dataset): height=crop_side_len, width=crop_side_len ) - image = self.cropper(image=image)['image'] - image = self.image_rescaler(image=image)['image'] + image = self.cropper(image=image)["image"] + image = self.image_rescaler(image=image)["image"] if self.pil_interpolation: image_pil = PIL.Image.fromarray(image) @@ -432,10 +419,10 @@ class ImageNetSR(Dataset): LR_image = np.array(LR_image).astype(np.uint8) else: - LR_image = self.degradation_process(image=image)['image'] + LR_image = self.degradation_process(image=image)["image"] - example['image'] = (image / 127.5 - 1.0).astype(np.float32) - example['LR_image'] = (LR_image / 127.5 - 1.0).astype(np.float32) + example["image"] = (image / 127.5 - 1.0).astype(np.float32) + example["LR_image"] = (LR_image / 127.5 - 1.0).astype(np.float32) return example @@ -445,7 +432,7 @@ class ImageNetSRTrain(ImageNetSR): super().__init__(**kwargs) def get_base(self): - with open('data/imagenet_train_hr_indices.p', 'rb') as f: + with open("data/imagenet_train_hr_indices.p", "rb") as f: indices = pickle.load(f) dset = ImageNetTrain( process_images=False, @@ -458,7 +445,7 @@ class ImageNetSRValidation(ImageNetSR): super().__init__(**kwargs) def get_base(self): - with open('data/imagenet_val_hr_indices.p', 'rb') as f: + with open("data/imagenet_val_hr_indices.p", "rb") as f: indices = pickle.load(f) dset = ImageNetValidation( process_images=False, diff --git a/invokeai/backend/stable_diffusion/data/lsun.py b/invokeai/backend/stable_diffusion/data/lsun.py index 4a7ecb147e..e9c2543f10 100644 --- a/invokeai/backend/stable_diffusion/data/lsun.py +++ b/invokeai/backend/stable_diffusion/data/lsun.py @@ -1,4 +1,5 @@ import os + import numpy as np import PIL from PIL import Image @@ -12,27 +13,25 @@ class LSUNBase(Dataset): txt_file, data_root, size=None, - interpolation='bicubic', + interpolation="bicubic", flip_p=0.5, ): self.data_paths = txt_file self.data_root = data_root - with open(self.data_paths, 'r') as f: + with open(self.data_paths, "r") as f: self.image_paths = f.read().splitlines() self._length = len(self.image_paths) self.labels = { - 'relative_file_path_': [l for l in self.image_paths], - 'file_path_': [ - os.path.join(self.data_root, l) for l in self.image_paths - ], + "relative_file_path_": [l for l in self.image_paths], + "file_path_": [os.path.join(self.data_root, l) for l in self.image_paths], } self.size = size self.interpolation = { - 'linear': PIL.Image.LINEAR, - 'bilinear': PIL.Image.BILINEAR, - 'bicubic': PIL.Image.BICUBIC, - 'lanczos': PIL.Image.LANCZOS, + "linear": PIL.Image.LINEAR, + "bilinear": PIL.Image.BILINEAR, + "bicubic": PIL.Image.BICUBIC, + "lanczos": PIL.Image.LANCZOS, }[interpolation] self.flip = transforms.RandomHorizontalFlip(p=flip_p) @@ -41,14 +40,17 @@ class LSUNBase(Dataset): def __getitem__(self, i): example = dict((k, self.labels[k][i]) for k in self.labels) - image = Image.open(example['file_path_']) - if not image.mode == 'RGB': - image = image.convert('RGB') + image = Image.open(example["file_path_"]) + if not image.mode == "RGB": + image = image.convert("RGB") # default to score-sde preprocessing img = np.array(image).astype(np.uint8) crop = min(img.shape[0], img.shape[1]) - h, w, = ( + ( + h, + w, + ) = ( img.shape[0], img.shape[1], ) @@ -59,68 +61,64 @@ class LSUNBase(Dataset): image = Image.fromarray(img) if self.size is not None: - image = image.resize( - (self.size, self.size), resample=self.interpolation - ) + image = image.resize((self.size, self.size), resample=self.interpolation) image = self.flip(image) image = np.array(image).astype(np.uint8) - example['image'] = (image / 127.5 - 1.0).astype(np.float32) + example["image"] = (image / 127.5 - 1.0).astype(np.float32) return example class LSUNChurchesTrain(LSUNBase): def __init__(self, **kwargs): super().__init__( - txt_file='data/lsun/church_outdoor_train.txt', - data_root='data/lsun/churches', - **kwargs + txt_file="data/lsun/church_outdoor_train.txt", + data_root="data/lsun/churches", + **kwargs, ) class LSUNChurchesValidation(LSUNBase): def __init__(self, flip_p=0.0, **kwargs): super().__init__( - txt_file='data/lsun/church_outdoor_val.txt', - data_root='data/lsun/churches', + txt_file="data/lsun/church_outdoor_val.txt", + data_root="data/lsun/churches", flip_p=flip_p, - **kwargs + **kwargs, ) class LSUNBedroomsTrain(LSUNBase): def __init__(self, **kwargs): super().__init__( - txt_file='data/lsun/bedrooms_train.txt', - data_root='data/lsun/bedrooms', - **kwargs + txt_file="data/lsun/bedrooms_train.txt", + data_root="data/lsun/bedrooms", + **kwargs, ) class LSUNBedroomsValidation(LSUNBase): def __init__(self, flip_p=0.0, **kwargs): super().__init__( - txt_file='data/lsun/bedrooms_val.txt', - data_root='data/lsun/bedrooms', + txt_file="data/lsun/bedrooms_val.txt", + data_root="data/lsun/bedrooms", flip_p=flip_p, - **kwargs + **kwargs, ) class LSUNCatsTrain(LSUNBase): def __init__(self, **kwargs): super().__init__( - txt_file='data/lsun/cat_train.txt', - data_root='data/lsun/cats', - **kwargs + txt_file="data/lsun/cat_train.txt", data_root="data/lsun/cats", **kwargs ) class LSUNCatsValidation(LSUNBase): def __init__(self, flip_p=0.0, **kwargs): super().__init__( - txt_file='data/lsun/cat_val.txt', - data_root='data/lsun/cats', + txt_file="data/lsun/cat_val.txt", + data_root="data/lsun/cats", flip_p=flip_p, - **kwargs + **kwargs, ) diff --git a/invokeai/backend/stable_diffusion/data/personalized.py b/invokeai/backend/stable_diffusion/data/personalized.py index 8d9573fbc6..fc8297a68a 100644 --- a/invokeai/backend/stable_diffusion/data/personalized.py +++ b/invokeai/backend/stable_diffusion/data/personalized.py @@ -1,99 +1,99 @@ import os +import random + import numpy as np import PIL from PIL import Image from torch.utils.data import Dataset from torchvision import transforms -import random - imagenet_templates_smallest = [ - 'a photo of a {}', + "a photo of a {}", ] imagenet_templates_small = [ - 'a photo of a {}', - 'a rendering of a {}', - 'a cropped photo of the {}', - 'the photo of a {}', - 'a photo of a clean {}', - 'a photo of a dirty {}', - 'a dark photo of the {}', - 'a photo of my {}', - 'a photo of the cool {}', - 'a close-up photo of a {}', - 'a bright photo of the {}', - 'a cropped photo of a {}', - 'a photo of the {}', - 'a good photo of the {}', - 'a photo of one {}', - 'a close-up photo of the {}', - 'a rendition of the {}', - 'a photo of the clean {}', - 'a rendition of a {}', - 'a photo of a nice {}', - 'a good photo of a {}', - 'a photo of the nice {}', - 'a photo of the small {}', - 'a photo of the weird {}', - 'a photo of the large {}', - 'a photo of a cool {}', - 'a photo of a small {}', + "a photo of a {}", + "a rendering of a {}", + "a cropped photo of the {}", + "the photo of a {}", + "a photo of a clean {}", + "a photo of a dirty {}", + "a dark photo of the {}", + "a photo of my {}", + "a photo of the cool {}", + "a close-up photo of a {}", + "a bright photo of the {}", + "a cropped photo of a {}", + "a photo of the {}", + "a good photo of the {}", + "a photo of one {}", + "a close-up photo of the {}", + "a rendition of the {}", + "a photo of the clean {}", + "a rendition of a {}", + "a photo of a nice {}", + "a good photo of a {}", + "a photo of the nice {}", + "a photo of the small {}", + "a photo of the weird {}", + "a photo of the large {}", + "a photo of a cool {}", + "a photo of a small {}", ] imagenet_dual_templates_small = [ - 'a photo of a {} with {}', - 'a rendering of a {} with {}', - 'a cropped photo of the {} with {}', - 'the photo of a {} with {}', - 'a photo of a clean {} with {}', - 'a photo of a dirty {} with {}', - 'a dark photo of the {} with {}', - 'a photo of my {} with {}', - 'a photo of the cool {} with {}', - 'a close-up photo of a {} with {}', - 'a bright photo of the {} with {}', - 'a cropped photo of a {} with {}', - 'a photo of the {} with {}', - 'a good photo of the {} with {}', - 'a photo of one {} with {}', - 'a close-up photo of the {} with {}', - 'a rendition of the {} with {}', - 'a photo of the clean {} with {}', - 'a rendition of a {} with {}', - 'a photo of a nice {} with {}', - 'a good photo of a {} with {}', - 'a photo of the nice {} with {}', - 'a photo of the small {} with {}', - 'a photo of the weird {} with {}', - 'a photo of the large {} with {}', - 'a photo of a cool {} with {}', - 'a photo of a small {} with {}', + "a photo of a {} with {}", + "a rendering of a {} with {}", + "a cropped photo of the {} with {}", + "the photo of a {} with {}", + "a photo of a clean {} with {}", + "a photo of a dirty {} with {}", + "a dark photo of the {} with {}", + "a photo of my {} with {}", + "a photo of the cool {} with {}", + "a close-up photo of a {} with {}", + "a bright photo of the {} with {}", + "a cropped photo of a {} with {}", + "a photo of the {} with {}", + "a good photo of the {} with {}", + "a photo of one {} with {}", + "a close-up photo of the {} with {}", + "a rendition of the {} with {}", + "a photo of the clean {} with {}", + "a rendition of a {} with {}", + "a photo of a nice {} with {}", + "a good photo of a {} with {}", + "a photo of the nice {} with {}", + "a photo of the small {} with {}", + "a photo of the weird {} with {}", + "a photo of the large {} with {}", + "a photo of a cool {} with {}", + "a photo of a small {} with {}", ] per_img_token_list = [ - 'א', - 'ב', - 'ג', - 'ד', - 'ה', - 'ו', - 'ז', - 'ח', - 'ט', - 'י', - 'כ', - 'ל', - 'מ', - 'נ', - 'ס', - 'ע', - 'פ', - 'צ', - 'ק', - 'ר', - 'ש', - 'ת', + "א", + "ב", + "ג", + "ד", + "ה", + "ו", + "ז", + "ח", + "ט", + "י", + "כ", + "ל", + "מ", + "נ", + "ס", + "ע", + "פ", + "צ", + "ק", + "ר", + "ש", + "ת", ] @@ -103,21 +103,21 @@ class PersonalizedBase(Dataset): data_root, size=None, repeats=100, - interpolation='bicubic', + interpolation="bicubic", flip_p=0.5, - set='train', - placeholder_token='*', + set="train", + placeholder_token="*", per_image_tokens=False, center_crop=False, mixing_prob=0.25, coarse_class_text=None, ): - self.data_root = data_root self.image_paths = [ os.path.join(self.data_root, file_path) - for file_path in os.listdir(self.data_root) if file_path != ".DS_Store" + for file_path in os.listdir(self.data_root) + if file_path != ".DS_Store" ] # self._length = len(self.image_paths) @@ -137,15 +137,15 @@ class PersonalizedBase(Dataset): per_img_token_list ), f"Can't use per-image tokens when the training set contains more than {len(per_img_token_list)} tokens. To enable larger sets, add more tokens to 'per_img_token_list'." - if set == 'train': + if set == "train": self._length = self.num_images * repeats self.size = size self.interpolation = { - 'linear': PIL.Image.LINEAR, - 'bilinear': PIL.Image.BILINEAR, - 'bicubic': PIL.Image.BICUBIC, - 'lanczos': PIL.Image.LANCZOS, + "linear": PIL.Image.LINEAR, + "bilinear": PIL.Image.BILINEAR, + "bicubic": PIL.Image.BICUBIC, + "lanczos": PIL.Image.LANCZOS, }[interpolation] self.flip = transforms.RandomHorizontalFlip(p=flip_p) @@ -156,32 +156,31 @@ class PersonalizedBase(Dataset): example = {} image = Image.open(self.image_paths[i % self.num_images]) - if not image.mode == 'RGB': - image = image.convert('RGB') + if not image.mode == "RGB": + image = image.convert("RGB") placeholder_string = self.placeholder_token if self.coarse_class_text: - placeholder_string = ( - f'{self.coarse_class_text} {placeholder_string}' - ) + placeholder_string = f"{self.coarse_class_text} {placeholder_string}" if self.per_image_tokens and np.random.uniform() < self.mixing_prob: text = random.choice(imagenet_dual_templates_small).format( placeholder_string, per_img_token_list[i % self.num_images] ) else: - text = random.choice(imagenet_templates_small).format( - placeholder_string - ) + text = random.choice(imagenet_templates_small).format(placeholder_string) - example['caption'] = text + example["caption"] = text # default to score-sde preprocessing img = np.array(image).astype(np.uint8) if self.center_crop: crop = min(img.shape[0], img.shape[1]) - h, w, = ( + ( + h, + w, + ) = ( img.shape[0], img.shape[1], ) @@ -192,11 +191,9 @@ class PersonalizedBase(Dataset): image = Image.fromarray(img) if self.size is not None: - image = image.resize( - (self.size, self.size), resample=self.interpolation - ) + image = image.resize((self.size, self.size), resample=self.interpolation) image = self.flip(image) image = np.array(image).astype(np.uint8) - example['image'] = (image / 127.5 - 1.0).astype(np.float32) + example["image"] = (image / 127.5 - 1.0).astype(np.float32) return example diff --git a/invokeai/backend/stable_diffusion/data/personalized_style.py b/invokeai/backend/stable_diffusion/data/personalized_style.py index 118d5be991..246c25e930 100644 --- a/invokeai/backend/stable_diffusion/data/personalized_style.py +++ b/invokeai/backend/stable_diffusion/data/personalized_style.py @@ -1,77 +1,77 @@ import os +import random + import numpy as np import PIL from PIL import Image from torch.utils.data import Dataset from torchvision import transforms -import random - imagenet_templates_small = [ - 'a painting in the style of {}', - 'a rendering in the style of {}', - 'a cropped painting in the style of {}', - 'the painting in the style of {}', - 'a clean painting in the style of {}', - 'a dirty painting in the style of {}', - 'a dark painting in the style of {}', - 'a picture in the style of {}', - 'a cool painting in the style of {}', - 'a close-up painting in the style of {}', - 'a bright painting in the style of {}', - 'a cropped painting in the style of {}', - 'a good painting in the style of {}', - 'a close-up painting in the style of {}', - 'a rendition in the style of {}', - 'a nice painting in the style of {}', - 'a small painting in the style of {}', - 'a weird painting in the style of {}', - 'a large painting in the style of {}', + "a painting in the style of {}", + "a rendering in the style of {}", + "a cropped painting in the style of {}", + "the painting in the style of {}", + "a clean painting in the style of {}", + "a dirty painting in the style of {}", + "a dark painting in the style of {}", + "a picture in the style of {}", + "a cool painting in the style of {}", + "a close-up painting in the style of {}", + "a bright painting in the style of {}", + "a cropped painting in the style of {}", + "a good painting in the style of {}", + "a close-up painting in the style of {}", + "a rendition in the style of {}", + "a nice painting in the style of {}", + "a small painting in the style of {}", + "a weird painting in the style of {}", + "a large painting in the style of {}", ] imagenet_dual_templates_small = [ - 'a painting in the style of {} with {}', - 'a rendering in the style of {} with {}', - 'a cropped painting in the style of {} with {}', - 'the painting in the style of {} with {}', - 'a clean painting in the style of {} with {}', - 'a dirty painting in the style of {} with {}', - 'a dark painting in the style of {} with {}', - 'a cool painting in the style of {} with {}', - 'a close-up painting in the style of {} with {}', - 'a bright painting in the style of {} with {}', - 'a cropped painting in the style of {} with {}', - 'a good painting in the style of {} with {}', - 'a painting of one {} in the style of {}', - 'a nice painting in the style of {} with {}', - 'a small painting in the style of {} with {}', - 'a weird painting in the style of {} with {}', - 'a large painting in the style of {} with {}', + "a painting in the style of {} with {}", + "a rendering in the style of {} with {}", + "a cropped painting in the style of {} with {}", + "the painting in the style of {} with {}", + "a clean painting in the style of {} with {}", + "a dirty painting in the style of {} with {}", + "a dark painting in the style of {} with {}", + "a cool painting in the style of {} with {}", + "a close-up painting in the style of {} with {}", + "a bright painting in the style of {} with {}", + "a cropped painting in the style of {} with {}", + "a good painting in the style of {} with {}", + "a painting of one {} in the style of {}", + "a nice painting in the style of {} with {}", + "a small painting in the style of {} with {}", + "a weird painting in the style of {} with {}", + "a large painting in the style of {} with {}", ] per_img_token_list = [ - 'א', - 'ב', - 'ג', - 'ד', - 'ה', - 'ו', - 'ז', - 'ח', - 'ט', - 'י', - 'כ', - 'ל', - 'מ', - 'נ', - 'ס', - 'ע', - 'פ', - 'צ', - 'ק', - 'ר', - 'ש', - 'ת', + "א", + "ב", + "ג", + "ד", + "ה", + "ו", + "ז", + "ח", + "ט", + "י", + "כ", + "ל", + "מ", + "נ", + "ס", + "ע", + "פ", + "צ", + "ק", + "ר", + "ש", + "ת", ] @@ -81,19 +81,19 @@ class PersonalizedBase(Dataset): data_root, size=None, repeats=100, - interpolation='bicubic', + interpolation="bicubic", flip_p=0.5, - set='train', - placeholder_token='*', + set="train", + placeholder_token="*", per_image_tokens=False, center_crop=False, ): - self.data_root = data_root self.image_paths = [ os.path.join(self.data_root, file_path) - for file_path in os.listdir(self.data_root) if file_path != ".DS_Store" + for file_path in os.listdir(self.data_root) + if file_path != ".DS_Store" ] # self._length = len(self.image_paths) @@ -110,15 +110,15 @@ class PersonalizedBase(Dataset): per_img_token_list ), f"Can't use per-image tokens when the training set contains more than {len(per_img_token_list)} tokens. To enable larger sets, add more tokens to 'per_img_token_list'." - if set == 'train': + if set == "train": self._length = self.num_images * repeats self.size = size self.interpolation = { - 'linear': PIL.Image.LINEAR, - 'bilinear': PIL.Image.BILINEAR, - 'bicubic': PIL.Image.BICUBIC, - 'lanczos': PIL.Image.LANCZOS, + "linear": PIL.Image.LINEAR, + "bilinear": PIL.Image.BILINEAR, + "bicubic": PIL.Image.BICUBIC, + "lanczos": PIL.Image.LANCZOS, }[interpolation] self.flip = transforms.RandomHorizontalFlip(p=flip_p) @@ -129,8 +129,8 @@ class PersonalizedBase(Dataset): example = {} image = Image.open(self.image_paths[i % self.num_images]) - if not image.mode == 'RGB': - image = image.convert('RGB') + if not image.mode == "RGB": + image = image.convert("RGB") if self.per_image_tokens and np.random.uniform() < 0.25: text = random.choice(imagenet_dual_templates_small).format( @@ -141,14 +141,17 @@ class PersonalizedBase(Dataset): self.placeholder_token ) - example['caption'] = text + example["caption"] = text # default to score-sde preprocessing img = np.array(image).astype(np.uint8) if self.center_crop: crop = min(img.shape[0], img.shape[1]) - h, w, = ( + ( + h, + w, + ) = ( img.shape[0], img.shape[1], ) @@ -159,11 +162,9 @@ class PersonalizedBase(Dataset): image = Image.fromarray(img) if self.size is not None: - image = image.resize( - (self.size, self.size), resample=self.interpolation - ) + image = image.resize((self.size, self.size), resample=self.interpolation) image = self.flip(image) image = np.array(image).astype(np.uint8) - example['image'] = (image / 127.5 - 1.0).astype(np.float32) + example["image"] = (image / 127.5 - 1.0).astype(np.float32) return example diff --git a/invokeai/backend/stable_diffusion/diffusers_pipeline.py b/invokeai/backend/stable_diffusion/diffusers_pipeline.py index c4ac77aaf6..f1622f86be 100644 --- a/invokeai/backend/stable_diffusion/diffusers_pipeline.py +++ b/invokeai/backend/stable_diffusion/diffusers_pipeline.py @@ -2,22 +2,28 @@ from __future__ import annotations import dataclasses import inspect -import psutil import secrets from collections.abc import Sequence from dataclasses import dataclass, field -from typing import List, Optional, Union, Callable, Type, TypeVar, Generic, Any +from typing import Any, Callable, Generic, List, Optional, Type, TypeVar, Union -import PIL.Image import einops +import PIL.Image import psutil import torch import torchvision.transforms as T +from compel import EmbeddingsProvider from diffusers.models import AutoencoderKL, UNet2DConditionModel from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput -from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipeline -from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img import StableDiffusionImg2ImgPipeline -from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker +from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import ( + StableDiffusionPipeline, +) +from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img import ( + StableDiffusionImg2ImgPipeline, +) +from diffusers.pipelines.stable_diffusion.safety_checker import ( + StableDiffusionSafetyChecker, +) from diffusers.schedulers import KarrasDiffusionSchedulers from diffusers.schedulers.scheduling_utils import SchedulerMixin, SchedulerOutput from diffusers.utils.import_utils import is_xformers_available @@ -27,11 +33,16 @@ from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer from typing_extensions import ParamSpec from invokeai.backend.globals import Globals -from .diffusion import InvokeAIDiffuserComponent, PostprocessingSettings, AttentionMapSaver + +from ..util import CPU_DEVICE, normalize_device +from .diffusion import ( + AttentionMapSaver, + InvokeAIDiffuserComponent, + PostprocessingSettings, +) +from .offloading import FullyLoadedModelGroup, LazilyLoadedModelGroup, ModelGroup from .textual_inversion_manager import TextualInversionManager -from .offloading import LazilyLoadedModelGroup, FullyLoadedModelGroup, ModelGroup -from ..util import normalize_device, CPU_DEVICE -from compel import EmbeddingsProvider + @dataclass class PipelineIntermediateState: @@ -49,7 +60,7 @@ _default_personalization_config_params = dict( initializer_wods=["sculpture"], per_image_tokens=False, num_vectors_per_token=1, - progressive_words=False + progressive_words=False, ) @@ -62,29 +73,34 @@ class AddsMaskLatents: This class assumes the same mask and base image should apply to all items in the batch. """ + forward: Callable[[torch.Tensor, torch.Tensor, torch.Tensor], torch.Tensor] mask: torch.Tensor initial_image_latents: torch.Tensor - def __call__(self, latents: torch.Tensor, t: torch.Tensor, text_embeddings: torch.Tensor) -> torch.Tensor: + def __call__( + self, latents: torch.Tensor, t: torch.Tensor, text_embeddings: torch.Tensor + ) -> torch.Tensor: model_input = self.add_mask_channels(latents) return self.forward(model_input, t, text_embeddings) def add_mask_channels(self, latents): batch_size = latents.size(0) # duplicate mask and latents for each batch - mask = einops.repeat(self.mask, 'b c h w -> (repeat b) c h w', repeat=batch_size) - image_latents = einops.repeat(self.initial_image_latents, 'b c h w -> (repeat b) c h w', repeat=batch_size) + mask = einops.repeat( + self.mask, "b c h w -> (repeat b) c h w", repeat=batch_size + ) + image_latents = einops.repeat( + self.initial_image_latents, "b c h w -> (repeat b) c h w", repeat=batch_size + ) # add mask and image as additional channels - model_input, _ = einops.pack([latents, mask, image_latents], 'b * h w') + model_input, _ = einops.pack([latents, mask, image_latents], "b * h w") return model_input def are_like_tensors(a: torch.Tensor, b: object) -> bool: - return ( - isinstance(b, torch.Tensor) - and (a.size() == b.size()) - ) + return isinstance(b, torch.Tensor) and (a.size() == b.size()) + @dataclass class AddsMaskGuidance: @@ -94,7 +110,9 @@ class AddsMaskGuidance: noise: torch.Tensor _debug: Optional[Callable] = None - def __call__(self, step_output: BaseOutput | SchedulerOutput, t: torch.Tensor, conditioning) -> BaseOutput: + def __call__( + self, step_output: BaseOutput | SchedulerOutput, t: torch.Tensor, conditioning + ) -> BaseOutput: output_class = step_output.__class__ # We'll create a new one with masked data. # The problem with taking SchedulerOutput instead of the model output is that we're less certain what's in it. @@ -104,30 +122,41 @@ class AddsMaskGuidance: prev_sample = step_output[0] # Mask anything that has the same shape as prev_sample, return others as-is. return output_class( - {k: (self.apply_mask(v, self._t_for_field(k, t)) - if are_like_tensors(prev_sample, v) else v) - for k, v in step_output.items()} + { + k: ( + self.apply_mask(v, self._t_for_field(k, t)) + if are_like_tensors(prev_sample, v) + else v + ) + for k, v in step_output.items() + } ) - def _t_for_field(self, field_name:str, t): + def _t_for_field(self, field_name: str, t): if field_name == "pred_original_sample": return torch.zeros_like(t, dtype=t.dtype) # it represents t=0 return t def apply_mask(self, latents: torch.Tensor, t) -> torch.Tensor: batch_size = latents.size(0) - mask = einops.repeat(self.mask, 'b c h w -> (repeat b) c h w', repeat=batch_size) + mask = einops.repeat( + self.mask, "b c h w -> (repeat b) c h w", repeat=batch_size + ) if t.dim() == 0: # some schedulers expect t to be one-dimensional. # TODO: file diffusers bug about inconsistency? - t = einops.repeat(t, '-> batch', batch=batch_size) + t = einops.repeat(t, "-> batch", batch=batch_size) # Noise shouldn't be re-randomized between steps here. The multistep schedulers # get very confused about what is happening from step to step when we do that. mask_latents = self.scheduler.add_noise(self.mask_latents, self.noise, t) # TODO: Do we need to also apply scheduler.scale_model_input? Or is add_noise appropriately scaled already? # mask_latents = self.scheduler.scale_model_input(mask_latents, t) - mask_latents = einops.repeat(mask_latents, 'b c h w -> (repeat b) c h w', repeat=batch_size) - masked_input = torch.lerp(mask_latents.to(dtype=latents.dtype), latents, mask.to(dtype=latents.dtype)) + mask_latents = einops.repeat( + mask_latents, "b c h w -> (repeat b) c h w", repeat=batch_size + ) + masked_input = torch.lerp( + mask_latents.to(dtype=latents.dtype), latents, mask.to(dtype=latents.dtype) + ) if self._debug: self._debug(masked_input, f"t={t} lerped") return masked_input @@ -137,7 +166,9 @@ def trim_to_multiple_of(*args, multiple_of=8): return tuple((x - x % multiple_of) for x in args) -def image_resized_to_grid_as_tensor(image: PIL.Image.Image, normalize: bool=True, multiple_of=8) -> torch.FloatTensor: +def image_resized_to_grid_as_tensor( + image: PIL.Image.Image, normalize: bool = True, multiple_of=8 +) -> torch.FloatTensor: """ :param image: input image @@ -145,10 +176,12 @@ def image_resized_to_grid_as_tensor(image: PIL.Image.Image, normalize: bool=True :param multiple_of: resize the input so both dimensions are a multiple of this """ w, h = trim_to_multiple_of(*image.size) - transformation = T.Compose([ - T.Resize((h, w), T.InterpolationMode.LANCZOS), - T.ToTensor(), - ]) + transformation = T.Compose( + [ + T.Resize((h, w), T.InterpolationMode.LANCZOS), + T.ToTensor(), + ] + ) tensor = transformation(image) if normalize: tensor = tensor * 2.0 - 1.0 @@ -158,9 +191,11 @@ def image_resized_to_grid_as_tensor(image: PIL.Image.Image, normalize: bool=True def is_inpainting_model(unet: UNet2DConditionModel): return unet.conv_in.in_channels == 9 -CallbackType = TypeVar('CallbackType') -ReturnType = TypeVar('ReturnType') -ParamType = ParamSpec('ParamType') + +CallbackType = TypeVar("CallbackType") +ReturnType = TypeVar("ReturnType") +ParamType = ParamSpec("ParamType") + @dataclass(frozen=True) class GeneratorToCallbackinator(Generic[ParamType, ReturnType, CallbackType]): @@ -169,9 +204,12 @@ class GeneratorToCallbackinator(Generic[ParamType, ReturnType, CallbackType]): generator_method: Callable[ParamType, ReturnType] callback_arg_type: Type[CallbackType] - def __call__(self, *args: ParamType.args, - callback:Callable[[CallbackType], Any]=None, - **kwargs: ParamType.kwargs) -> ReturnType: + def __call__( + self, + *args: ParamType.args, + callback: Callable[[CallbackType], Any] = None, + **kwargs: ParamType.kwargs, + ) -> ReturnType: result = None for result in self.generator_method(*args, **kwargs): if callback is not None and isinstance(result, self.callback_arg_type): @@ -216,6 +254,7 @@ class ConditioningData: scheduler_args[name] = value return dataclasses.replace(self, scheduler_args=scheduler_args) + @dataclass class InvokeAIStableDiffusionPipelineOutput(StableDiffusionPipelineOutput): r""" @@ -273,10 +312,18 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): safety_checker: Optional[StableDiffusionSafetyChecker], feature_extractor: Optional[CLIPFeatureExtractor], requires_safety_checker: bool = False, - precision: str = 'float32', + precision: str = "float32", ): - super().__init__(vae, text_encoder, tokenizer, unet, scheduler, - safety_checker, feature_extractor, requires_safety_checker) + super().__init__( + vae, + text_encoder, + tokenizer, + unet, + scheduler, + safety_checker, + feature_extractor, + requires_safety_checker, + ) self.register_modules( vae=vae, @@ -287,27 +334,34 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): safety_checker=safety_checker, feature_extractor=feature_extractor, ) - self.invokeai_diffuser = InvokeAIDiffuserComponent(self.unet, self._unet_forward, is_running_diffusers=True) - use_full_precision = (precision == 'float32' or precision == 'autocast') - self.textual_inversion_manager = TextualInversionManager(tokenizer=self.tokenizer, - text_encoder=self.text_encoder, - full_precision=use_full_precision) + self.invokeai_diffuser = InvokeAIDiffuserComponent( + self.unet, self._unet_forward, is_running_diffusers=True + ) + use_full_precision = precision == "float32" or precision == "autocast" + self.textual_inversion_manager = TextualInversionManager( + tokenizer=self.tokenizer, + text_encoder=self.text_encoder, + full_precision=use_full_precision, + ) # InvokeAI's interface for text embeddings and whatnot self.embeddings_provider = EmbeddingsProvider( tokenizer=self.tokenizer, text_encoder=self.text_encoder, - textual_inversion_manager=self.textual_inversion_manager + textual_inversion_manager=self.textual_inversion_manager, ) self._model_group = FullyLoadedModelGroup(self.unet.device) self._model_group.install(*self._submodels) - def _adjust_memory_efficient_attention(self, latents: torch.Tensor): """ if xformers is available, use it, otherwise use sliced attention. """ - if torch.cuda.is_available() and is_xformers_available() and not Globals.disable_xformers: + if ( + torch.cuda.is_available() + and is_xformers_available() + and not Globals.disable_xformers + ): self.enable_xformers_memory_efficient_attention() else: if torch.backends.mps.is_available(): @@ -316,25 +370,32 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): # fix is in https://github.com/kulinseth/pytorch/pull/222 but no idea when it will get merged to pytorch mainline. pass else: - if self.device.type == 'cpu' or self.device.type == 'mps': + if self.device.type == "cpu" or self.device.type == "mps": mem_free = psutil.virtual_memory().free - elif self.device.type == 'cuda': + elif self.device.type == "cuda": mem_free, _ = torch.cuda.mem_get_info(normalize_device(self.device)) else: raise ValueError(f"unrecognized device {self.device}") # input tensor of [1, 4, h/8, w/8] # output tensor of [16, (h/8 * w/8), (h/8 * w/8)] - bytes_per_element_needed_for_baddbmm_duplication = latents.element_size() + 4 - max_size_required_for_baddbmm = \ - 16 * \ - latents.size(dim=2) * latents.size(dim=3) * latents.size(dim=2) * latents.size(dim=3) * \ - bytes_per_element_needed_for_baddbmm_duplication - if max_size_required_for_baddbmm > (mem_free * 3.0 / 4.0): # 3.3 / 4.0 is from old Invoke code - self.enable_attention_slicing(slice_size='max') + bytes_per_element_needed_for_baddbmm_duplication = ( + latents.element_size() + 4 + ) + max_size_required_for_baddbmm = ( + 16 + * latents.size(dim=2) + * latents.size(dim=3) + * latents.size(dim=2) + * latents.size(dim=3) + * bytes_per_element_needed_for_baddbmm_duplication + ) + if max_size_required_for_baddbmm > ( + mem_free * 3.0 / 4.0 + ): # 3.3 / 4.0 is from old Invoke code + self.enable_attention_slicing(slice_size="max") else: self.disable_attention_slicing() - def enable_offload_submodels(self, device: torch.device): """ Offload each submodel when it's not in use. @@ -396,12 +457,16 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): values = [getattr(self, name) for name in module_names.keys()] return [m for m in values if isinstance(m, torch.nn.Module)] - def image_from_embeddings(self, latents: torch.Tensor, num_inference_steps: int, - conditioning_data: ConditioningData, - *, - noise: torch.Tensor, - callback: Callable[[PipelineIntermediateState], None]=None, - run_id=None) -> InvokeAIStableDiffusionPipelineOutput: + def image_from_embeddings( + self, + latents: torch.Tensor, + num_inference_steps: int, + conditioning_data: ConditioningData, + *, + noise: torch.Tensor, + callback: Callable[[PipelineIntermediateState], None] = None, + run_id=None, + ) -> InvokeAIStableDiffusionPipelineOutput: r""" Function invoked when calling the pipeline for generation. @@ -415,71 +480,104 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): :param run_id: """ result_latents, result_attention_map_saver = self.latents_from_embeddings( - latents, num_inference_steps, + latents, + num_inference_steps, conditioning_data, noise=noise, run_id=run_id, - callback=callback) + callback=callback, + ) # https://discuss.huggingface.co/t/memory-usage-by-later-pipeline-stages/23699 torch.cuda.empty_cache() with torch.inference_mode(): image = self.decode_latents(result_latents) - output = InvokeAIStableDiffusionPipelineOutput(images=image, nsfw_content_detected=[], attention_map_saver=result_attention_map_saver) + output = InvokeAIStableDiffusionPipelineOutput( + images=image, + nsfw_content_detected=[], + attention_map_saver=result_attention_map_saver, + ) return self.check_for_safety(output, dtype=conditioning_data.dtype) - def latents_from_embeddings(self, latents: torch.Tensor, num_inference_steps: int, - conditioning_data: ConditioningData, - *, - noise: torch.Tensor, - timesteps=None, - additional_guidance: List[Callable] = None, run_id=None, - callback: Callable[[PipelineIntermediateState], None] = None - ) -> tuple[torch.Tensor, Optional[AttentionMapSaver]]: + def latents_from_embeddings( + self, + latents: torch.Tensor, + num_inference_steps: int, + conditioning_data: ConditioningData, + *, + noise: torch.Tensor, + timesteps=None, + additional_guidance: List[Callable] = None, + run_id=None, + callback: Callable[[PipelineIntermediateState], None] = None, + ) -> tuple[torch.Tensor, Optional[AttentionMapSaver]]: if timesteps is None: - self.scheduler.set_timesteps(num_inference_steps, device=self._model_group.device_for(self.unet)) + self.scheduler.set_timesteps( + num_inference_steps, device=self._model_group.device_for(self.unet) + ) timesteps = self.scheduler.timesteps - infer_latents_from_embeddings = GeneratorToCallbackinator(self.generate_latents_from_embeddings, PipelineIntermediateState) + infer_latents_from_embeddings = GeneratorToCallbackinator( + self.generate_latents_from_embeddings, PipelineIntermediateState + ) result: PipelineIntermediateState = infer_latents_from_embeddings( - latents, timesteps, conditioning_data, + latents, + timesteps, + conditioning_data, noise=noise, additional_guidance=additional_guidance, run_id=run_id, - callback=callback) + callback=callback, + ) return result.latents, result.attention_map_saver - def generate_latents_from_embeddings(self, latents: torch.Tensor, timesteps, - conditioning_data: ConditioningData, - *, - noise: torch.Tensor, - run_id: str = None, - additional_guidance: List[Callable] = None): + def generate_latents_from_embeddings( + self, + latents: torch.Tensor, + timesteps, + conditioning_data: ConditioningData, + *, + noise: torch.Tensor, + run_id: str = None, + additional_guidance: List[Callable] = None, + ): self._adjust_memory_efficient_attention(latents) if run_id is None: run_id = secrets.token_urlsafe(self.ID_LENGTH) if additional_guidance is None: additional_guidance = [] extra_conditioning_info = conditioning_data.extra - with self.invokeai_diffuser.custom_attention_context(extra_conditioning_info=extra_conditioning_info, - step_count=len(self.scheduler.timesteps) - ): - - yield PipelineIntermediateState(run_id=run_id, step=-1, timestep=self.scheduler.num_train_timesteps, - latents=latents) + with self.invokeai_diffuser.custom_attention_context( + extra_conditioning_info=extra_conditioning_info, + step_count=len(self.scheduler.timesteps), + ): + yield PipelineIntermediateState( + run_id=run_id, + step=-1, + timestep=self.scheduler.num_train_timesteps, + latents=latents, + ) batch_size = latents.shape[0] - batched_t = torch.full((batch_size,), timesteps[0], - dtype=timesteps.dtype, device=self._model_group.device_for(self.unet)) + batched_t = torch.full( + (batch_size,), + timesteps[0], + dtype=timesteps.dtype, + device=self._model_group.device_for(self.unet), + ) latents = self.scheduler.add_noise(latents, noise, batched_t) attention_map_saver: Optional[AttentionMapSaver] = None for i, t in enumerate(self.progress_bar(timesteps)): batched_t.fill_(t) - step_output = self.step(batched_t, latents, conditioning_data, - step_index=i, - total_step_count=len(timesteps), - additional_guidance=additional_guidance) + step_output = self.step( + batched_t, + latents, + conditioning_data, + step_index=i, + total_step_count=len(timesteps), + additional_guidance=additional_guidance, + ) latents = step_output.prev_sample latents = self.invokeai_diffuser.do_latent_postprocessing( @@ -487,28 +585,39 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): latents=latents, sigma=batched_t, step_index=i, - total_step_count=len(timesteps) + total_step_count=len(timesteps), ) - predicted_original = getattr(step_output, 'pred_original_sample', None) + predicted_original = getattr(step_output, "pred_original_sample", None) # TODO resuscitate attention map saving - #if i == len(timesteps)-1 and extra_conditioning_info is not None: + # if i == len(timesteps)-1 and extra_conditioning_info is not None: # eos_token_index = extra_conditioning_info.tokens_count_including_eos_bos - 1 # attention_map_token_ids = range(1, eos_token_index) # attention_map_saver = AttentionMapSaver(token_ids=attention_map_token_ids, latents_shape=latents.shape[-2:]) # self.invokeai_diffuser.setup_attention_map_saving(attention_map_saver) - yield PipelineIntermediateState(run_id=run_id, step=i, timestep=int(t), latents=latents, - predicted_original=predicted_original, attention_map_saver=attention_map_saver) + yield PipelineIntermediateState( + run_id=run_id, + step=i, + timestep=int(t), + latents=latents, + predicted_original=predicted_original, + attention_map_saver=attention_map_saver, + ) return latents, attention_map_saver @torch.inference_mode() - def step(self, t: torch.Tensor, latents: torch.Tensor, - conditioning_data: ConditioningData, - step_index:int, total_step_count:int, - additional_guidance: List[Callable] = None): + def step( + self, + t: torch.Tensor, + latents: torch.Tensor, + conditioning_data: ConditioningData, + step_index: int, + total_step_count: int, + additional_guidance: List[Callable] = None, + ): # invokeai_diffuser has batched timesteps, but diffusers schedulers expect a single value timestep = t[0] @@ -521,16 +630,19 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): # predict the noise residual noise_pred = self.invokeai_diffuser.do_diffusion_step( - latent_model_input, t, - conditioning_data.unconditioned_embeddings, conditioning_data.text_embeddings, + latent_model_input, + t, + conditioning_data.unconditioned_embeddings, + conditioning_data.text_embeddings, conditioning_data.guidance_scale, step_index=step_index, total_step_count=total_step_count, ) # compute the previous noisy sample x_t -> x_t-1 - step_output = self.scheduler.step(noise_pred, timestep, latents, - **conditioning_data.scheduler_args) + step_output = self.scheduler.step( + noise_pred, timestep, latents, **conditioning_data.scheduler_args + ) # TODO: this additional_guidance extension point feels redundant with InvokeAIDiffusionComponent. # But the way things are now, scheduler runs _after_ that, so there was @@ -540,7 +652,13 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): return step_output - def _unet_forward(self, latents, t, text_embeddings, cross_attention_kwargs: Optional[dict[str,Any]] = None): + def _unet_forward( + self, + latents, + t, + text_embeddings, + cross_attention_kwargs: Optional[dict[str, Any]] = None, + ): """predict the noise residual""" if is_inpainting_model(self.unet) and latents.size(1) == 4: # Pad out normal non-inpainting inputs for an inpainting model. @@ -549,67 +667,100 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): # use of AddsMaskLatents. latents = AddsMaskLatents( self._unet_forward, - mask=torch.ones_like(latents[:1, :1], device=latents.device, dtype=latents.dtype), - initial_image_latents=torch.zeros_like(latents[:1], device=latents.device, dtype=latents.dtype) + mask=torch.ones_like( + latents[:1, :1], device=latents.device, dtype=latents.dtype + ), + initial_image_latents=torch.zeros_like( + latents[:1], device=latents.device, dtype=latents.dtype + ), ).add_mask_channels(latents) # First three args should be positional, not keywords, so torch hooks can see them. - return self.unet(latents, t, text_embeddings, - cross_attention_kwargs=cross_attention_kwargs).sample + return self.unet( + latents, t, text_embeddings, cross_attention_kwargs=cross_attention_kwargs + ).sample - def img2img_from_embeddings(self, - init_image: Union[torch.FloatTensor, PIL.Image.Image], - strength: float, - num_inference_steps: int, - conditioning_data: ConditioningData, - *, callback: Callable[[PipelineIntermediateState], None] = None, - run_id=None, - noise_func=None - ) -> InvokeAIStableDiffusionPipelineOutput: + def img2img_from_embeddings( + self, + init_image: Union[torch.FloatTensor, PIL.Image.Image], + strength: float, + num_inference_steps: int, + conditioning_data: ConditioningData, + *, + callback: Callable[[PipelineIntermediateState], None] = None, + run_id=None, + noise_func=None, + ) -> InvokeAIStableDiffusionPipelineOutput: if isinstance(init_image, PIL.Image.Image): - init_image = image_resized_to_grid_as_tensor(init_image.convert('RGB')) + init_image = image_resized_to_grid_as_tensor(init_image.convert("RGB")) if init_image.dim() == 3: - init_image = einops.rearrange(init_image, 'c h w -> 1 c h w') + init_image = einops.rearrange(init_image, "c h w -> 1 c h w") # 6. Prepare latent variables initial_latents = self.non_noised_latents_from_image( - init_image, device=self._model_group.device_for(self.unet), - dtype=self.unet.dtype) + init_image, + device=self._model_group.device_for(self.unet), + dtype=self.unet.dtype, + ) noise = noise_func(initial_latents) - return self.img2img_from_latents_and_embeddings(initial_latents, num_inference_steps, - conditioning_data, - strength, - noise, run_id, callback) + return self.img2img_from_latents_and_embeddings( + initial_latents, + num_inference_steps, + conditioning_data, + strength, + noise, + run_id, + callback, + ) - def img2img_from_latents_and_embeddings(self, initial_latents, num_inference_steps, - conditioning_data: ConditioningData, - strength, - noise: torch.Tensor, run_id=None, callback=None - ) -> InvokeAIStableDiffusionPipelineOutput: - timesteps, _ = self.get_img2img_timesteps(num_inference_steps, strength, - device=self._model_group.device_for(self.unet)) + def img2img_from_latents_and_embeddings( + self, + initial_latents, + num_inference_steps, + conditioning_data: ConditioningData, + strength, + noise: torch.Tensor, + run_id=None, + callback=None, + ) -> InvokeAIStableDiffusionPipelineOutput: + timesteps, _ = self.get_img2img_timesteps( + num_inference_steps, + strength, + device=self._model_group.device_for(self.unet), + ) result_latents, result_attention_maps = self.latents_from_embeddings( - initial_latents, num_inference_steps, conditioning_data, + initial_latents, + num_inference_steps, + conditioning_data, timesteps=timesteps, noise=noise, run_id=run_id, - callback=callback) + callback=callback, + ) # https://discuss.huggingface.co/t/memory-usage-by-later-pipeline-stages/23699 torch.cuda.empty_cache() with torch.inference_mode(): image = self.decode_latents(result_latents) - output = InvokeAIStableDiffusionPipelineOutput(images=image, nsfw_content_detected=[], attention_map_saver=result_attention_maps) + output = InvokeAIStableDiffusionPipelineOutput( + images=image, + nsfw_content_detected=[], + attention_map_saver=result_attention_maps, + ) return self.check_for_safety(output, dtype=conditioning_data.dtype) - def get_img2img_timesteps(self, num_inference_steps: int, strength: float, device) -> (torch.Tensor, int): + def get_img2img_timesteps( + self, num_inference_steps: int, strength: float, device + ) -> (torch.Tensor, int): img2img_pipeline = StableDiffusionImg2ImgPipeline(**self.components) assert img2img_pipeline.scheduler is self.scheduler img2img_pipeline.scheduler.set_timesteps(num_inference_steps, device=device) - timesteps, adjusted_steps = img2img_pipeline.get_timesteps(num_inference_steps, strength, device=device) + timesteps, adjusted_steps = img2img_pipeline.get_timesteps( + num_inference_steps, strength, device=device + ) # Workaround for low strength resulting in zero timesteps. # TODO: submit upstream fix for zero-step img2img if timesteps.numel() == 0: @@ -618,21 +769,22 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): return timesteps, adjusted_steps def inpaint_from_embeddings( - self, - init_image: torch.FloatTensor, - mask: torch.FloatTensor, - strength: float, - num_inference_steps: int, - conditioning_data: ConditioningData, - *, callback: Callable[[PipelineIntermediateState], None] = None, - run_id=None, - noise_func=None, - ) -> InvokeAIStableDiffusionPipelineOutput: + self, + init_image: torch.FloatTensor, + mask: torch.FloatTensor, + strength: float, + num_inference_steps: int, + conditioning_data: ConditioningData, + *, + callback: Callable[[PipelineIntermediateState], None] = None, + run_id=None, + noise_func=None, + ) -> InvokeAIStableDiffusionPipelineOutput: device = self._model_group.device_for(self.unet) latents_dtype = self.unet.dtype if isinstance(init_image, PIL.Image.Image): - init_image = image_resized_to_grid_as_tensor(init_image.convert('RGB')) + init_image = image_resized_to_grid_as_tensor(init_image.convert("RGB")) init_image = init_image.to(device=device, dtype=latents_dtype) mask = mask.to(device=device, dtype=latents_dtype) @@ -640,18 +792,23 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): if init_image.dim() == 3: init_image = init_image.unsqueeze(0) - timesteps, _ = self.get_img2img_timesteps(num_inference_steps, strength, device=device) + timesteps, _ = self.get_img2img_timesteps( + num_inference_steps, strength, device=device + ) # 6. Prepare latent variables # can't quite use upstream StableDiffusionImg2ImgPipeline.prepare_latents # because we have our own noise function - init_image_latents = self.non_noised_latents_from_image(init_image, device=device, dtype=latents_dtype) + init_image_latents = self.non_noised_latents_from_image( + init_image, device=device, dtype=latents_dtype + ) noise = noise_func(init_image_latents) if mask.dim() == 3: mask = mask.unsqueeze(0) - latent_mask = tv_resize(mask, init_image_latents.shape[-2:], T.InterpolationMode.BILINEAR) \ - .to(device=device, dtype=latents_dtype) + latent_mask = tv_resize( + mask, init_image_latents.shape[-2:], T.InterpolationMode.BILINEAR + ).to(device=device, dtype=latents_dtype) guidance: List[Callable] = [] @@ -659,20 +816,30 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): # You'd think the inpainting model wouldn't be paying attention to the area it is going to repaint # (that's why there's a mask!) but it seems to really want that blanked out. masked_init_image = init_image * torch.where(mask < 0.5, 1, 0) - masked_latents = self.non_noised_latents_from_image(masked_init_image, device=device, dtype=latents_dtype) + masked_latents = self.non_noised_latents_from_image( + masked_init_image, device=device, dtype=latents_dtype + ) # TODO: we should probably pass this in so we don't have to try/finally around setting it. - self.invokeai_diffuser.model_forward_callback = \ - AddsMaskLatents(self._unet_forward, latent_mask, masked_latents) + self.invokeai_diffuser.model_forward_callback = AddsMaskLatents( + self._unet_forward, latent_mask, masked_latents + ) else: - guidance.append(AddsMaskGuidance(latent_mask, init_image_latents, self.scheduler, noise)) + guidance.append( + AddsMaskGuidance(latent_mask, init_image_latents, self.scheduler, noise) + ) try: result_latents, result_attention_maps = self.latents_from_embeddings( - init_image_latents, num_inference_steps, - conditioning_data, noise=noise, timesteps=timesteps, + init_image_latents, + num_inference_steps, + conditioning_data, + noise=noise, + timesteps=timesteps, additional_guidance=guidance, - run_id=run_id, callback=callback) + run_id=run_id, + callback=callback, + ) finally: self.invokeai_diffuser.model_forward_callback = self._unet_forward @@ -681,13 +848,17 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): with torch.inference_mode(): image = self.decode_latents(result_latents) - output = InvokeAIStableDiffusionPipelineOutput(images=image, nsfw_content_detected=[], attention_map_saver=result_attention_maps) + output = InvokeAIStableDiffusionPipelineOutput( + images=image, + nsfw_content_detected=[], + attention_map_saver=result_attention_maps, + ) return self.check_for_safety(output, dtype=conditioning_data.dtype) def non_noised_latents_from_image(self, init_image, *, device: torch.device, dtype): init_image = init_image.to(device=device, dtype=dtype) with torch.inference_mode(): - if device.type == 'mps': + if device.type == "mps": # workaround for torch MPS bug that has been fixed in https://github.com/kulinseth/pytorch/pull/222 # TODO remove this workaround once kulinseth#222 is merged to pytorch mainline self.vae.to(CPU_DEVICE) @@ -695,8 +866,10 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): else: self._model_group.load(self.vae) init_latent_dist = self.vae.encode(init_image).latent_dist - init_latents = init_latent_dist.sample().to(dtype=dtype) # FIXME: uses torch.randn. make reproducible! - if device.type == 'mps': + init_latents = init_latent_dist.sample().to( + dtype=dtype + ) # FIXME: uses torch.randn. make reproducible! + if device.type == "mps": self.vae.to(device) init_latents = init_latents.to(device) @@ -705,14 +878,18 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): def check_for_safety(self, output, dtype): with torch.inference_mode(): - screened_images, has_nsfw_concept = self.run_safety_checker(output.images, dtype=dtype) + screened_images, has_nsfw_concept = self.run_safety_checker( + output.images, dtype=dtype + ) screened_attention_map_saver = None if has_nsfw_concept is None or not has_nsfw_concept: screened_attention_map_saver = output.attention_map_saver - return InvokeAIStableDiffusionPipelineOutput(screened_images, - has_nsfw_concept, - # block the attention maps if NSFW content is detected - attention_map_saver=screened_attention_map_saver) + return InvokeAIStableDiffusionPipelineOutput( + screened_images, + has_nsfw_concept, + # block the attention maps if NSFW content is detected + attention_map_saver=screened_attention_map_saver, + ) def run_safety_checker(self, image, device=None, dtype=None): # overriding to use the model group for device info instead of requiring the caller to know. @@ -721,7 +898,9 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): return super().run_safety_checker(image, device, dtype) @torch.inference_mode() - def get_learned_conditioning(self, c: List[List[str]], *, return_tokens=True, fragment_weights=None): + def get_learned_conditioning( + self, c: List[List[str]], *, return_tokens=True, fragment_weights=None + ): """ Compatibility function for invokeai.models.diffusion.ddpm.LatentDiffusion. """ @@ -729,7 +908,8 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): text_batch=c, fragment_weights_batch=fragment_weights, should_return_tokens=return_tokens, - device=self._model_group.device_for(self.unet)) + device=self._model_group.device_for(self.unet), + ) @property def cond_stage_model(self): @@ -758,6 +938,9 @@ class StableDiffusionGeneratorPipeline(StableDiffusionPipeline): def debug_latents(self, latents, msg): with torch.inference_mode(): from ldm.util import debug_image + decoded = self.numpy_to_pil(self.decode_latents(latents)) for i, img in enumerate(decoded): - debug_image(img, f"latents {msg} {i+1}/{len(decoded)}", debug_status=True) + debug_image( + img, f"latents {msg} {i+1}/{len(decoded)}", debug_status=True + ) diff --git a/invokeai/backend/stable_diffusion/diffusion/__init__.py b/invokeai/backend/stable_diffusion/diffusion/__init__.py index 569c22c429..6dd2817f29 100644 --- a/invokeai/backend/stable_diffusion/diffusion/__init__.py +++ b/invokeai/backend/stable_diffusion/diffusion/__init__.py @@ -1,6 +1,6 @@ -''' +""" Initialization file for invokeai.models.diffusion -''' -from .shared_invokeai_diffusion import InvokeAIDiffuserComponent, PostprocessingSettings +""" from .cross_attention_control import InvokeAICrossAttentionMixin from .cross_attention_map_saving import AttentionMapSaver +from .shared_invokeai_diffusion import InvokeAIDiffuserComponent, PostprocessingSettings diff --git a/invokeai/backend/stable_diffusion/diffusion/__init__.py~ b/invokeai/backend/stable_diffusion/diffusion/__init__.py~ deleted file mode 100644 index d7706c27eb..0000000000 --- a/invokeai/backend/stable_diffusion/diffusion/__init__.py~ +++ /dev/null @@ -1,4 +0,0 @@ -''' -Initialization file for invokeai.models.diffusion -''' -from shared_invokeai_diffusion import InvokeAIDiffuserComponent diff --git a/invokeai/backend/stable_diffusion/diffusion/classifier.py b/invokeai/backend/stable_diffusion/diffusion/classifier.py index be0d8c1919..89aba16ee9 100644 --- a/invokeai/backend/stable_diffusion/diffusion/classifier.py +++ b/invokeai/backend/stable_diffusion/diffusion/classifier.py @@ -1,22 +1,19 @@ import os -import torch +from copy import deepcopy +from glob import glob + import pytorch_lightning as pl +import torch +from einops import rearrange +from ldm.modules.diffusionmodules.openaimodel import EncoderUNetModel, UNetModel +from ldm.util import default, instantiate_from_config, ismap, log_txt_as_img +from natsort import natsorted from omegaconf import OmegaConf from torch.nn import functional as F from torch.optim import AdamW from torch.optim.lr_scheduler import LambdaLR -from copy import deepcopy -from einops import rearrange -from glob import glob -from natsort import natsorted -from ldm.modules.diffusionmodules.openaimodel import ( - EncoderUNetModel, - UNetModel, -) -from ldm.util import log_txt_as_img, default, ismap, instantiate_from_config - -__models__ = {'class_label': EncoderUNetModel, 'segmentation': UNetModel} +__models__ = {"class_label": EncoderUNetModel, "segmentation": UNetModel} def disabled_train(self, mode=True): @@ -31,13 +28,13 @@ class NoisyLatentImageClassifier(pl.LightningModule): diffusion_path, num_classes, ckpt_path=None, - pool='attention', + pool="attention", label_key=None, diffusion_ckpt_path=None, scheduler_config=None, weight_decay=1.0e-2, log_steps=10, - monitor='val/loss', + monitor="val/loss", *args, **kwargs, ): @@ -45,30 +42,26 @@ class NoisyLatentImageClassifier(pl.LightningModule): self.num_classes = num_classes # get latest config of diffusion model diffusion_config = natsorted( - glob(os.path.join(diffusion_path, 'configs', '*-project.yaml')) + glob(os.path.join(diffusion_path, "configs", "*-project.yaml")) )[-1] self.diffusion_config = OmegaConf.load(diffusion_config).model self.diffusion_config.params.ckpt_path = diffusion_ckpt_path self.load_diffusion() self.monitor = monitor - self.numd = ( - self.diffusion_model.first_stage_model.encoder.num_resolutions - 1 - ) - self.log_time_interval = ( - self.diffusion_model.num_timesteps // log_steps - ) + self.numd = self.diffusion_model.first_stage_model.encoder.num_resolutions - 1 + self.log_time_interval = self.diffusion_model.num_timesteps // log_steps self.log_steps = log_steps self.label_key = ( label_key - if not hasattr(self.diffusion_model, 'cond_stage_key') + if not hasattr(self.diffusion_model, "cond_stage_key") else self.diffusion_model.cond_stage_key ) assert ( self.label_key is not None - ), 'label_key neither in diffusion model nor in model.params' + ), "label_key neither in diffusion model nor in model.params" if self.label_key not in __models__: raise NotImplementedError() @@ -80,14 +73,14 @@ class NoisyLatentImageClassifier(pl.LightningModule): self.weight_decay = weight_decay def init_from_ckpt(self, path, ignore_keys=list(), only_model=False): - sd = torch.load(path, map_location='cpu') - if 'state_dict' in list(sd.keys()): - sd = sd['state_dict'] + sd = torch.load(path, map_location="cpu") + if "state_dict" in list(sd.keys()): + sd = sd["state_dict"] keys = list(sd.keys()) for k in keys: for ik in ignore_keys: if k.startswith(ik): - print('Deleting key {} from state_dict.'.format(k)) + print("Deleting key {} from state_dict.".format(k)) del sd[k] missing, unexpected = ( self.load_state_dict(sd, strict=False) @@ -95,12 +88,12 @@ class NoisyLatentImageClassifier(pl.LightningModule): else self.model.load_state_dict(sd, strict=False) ) print( - f'Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys' + f"Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys" ) if len(missing) > 0: - print(f'Missing Keys: {missing}') + print(f"Missing Keys: {missing}") if len(unexpected) > 0: - print(f'Unexpected Keys: {unexpected}') + print(f"Unexpected Keys: {unexpected}") def load_diffusion(self): model = instantiate_from_config(self.diffusion_config) @@ -110,24 +103,22 @@ class NoisyLatentImageClassifier(pl.LightningModule): param.requires_grad = False def load_classifier(self, ckpt_path, pool): - model_config = deepcopy( - self.diffusion_config.params.unet_config.params - ) + model_config = deepcopy(self.diffusion_config.params.unet_config.params) model_config.in_channels = ( self.diffusion_config.params.unet_config.params.out_channels ) model_config.out_channels = self.num_classes - if self.label_key == 'class_label': + if self.label_key == "class_label": model_config.pool = pool self.model = __models__[self.label_key](**model_config) if ckpt_path is not None: print( - '#####################################################################' + "#####################################################################" ) print(f'load from ckpt "{ckpt_path}"') print( - '#####################################################################' + "#####################################################################" ) self.init_from_ckpt(ckpt_path) @@ -137,9 +128,7 @@ class NoisyLatentImageClassifier(pl.LightningModule): continuous_sqrt_alpha_cumprod = None if self.diffusion_model.use_continuous_noise: continuous_sqrt_alpha_cumprod = ( - self.diffusion_model.sample_continuous_noise_level( - x.shape[0], t + 1 - ) + self.diffusion_model.sample_continuous_noise_level(x.shape[0], t + 1) ) # todo: make sure t+1 is correct here @@ -158,7 +147,7 @@ class NoisyLatentImageClassifier(pl.LightningModule): x = batch[k] if len(x.shape) == 3: x = x[..., None] - x = rearrange(x, 'b h w c -> b c h w') + x = rearrange(x, "b h w c -> b c h w") x = x.to(memory_format=torch.contiguous_format).float() return x @@ -166,45 +155,41 @@ class NoisyLatentImageClassifier(pl.LightningModule): def get_conditioning(self, batch, k=None): if k is None: k = self.label_key - assert k is not None, 'Needs to provide label key' + assert k is not None, "Needs to provide label key" targets = batch[k].to(self.device) - if self.label_key == 'segmentation': - targets = rearrange(targets, 'b h w c -> b c h w') + if self.label_key == "segmentation": + targets = rearrange(targets, "b h w c -> b c h w") for down in range(self.numd): h, w = targets.shape[-2:] - targets = F.interpolate( - targets, size=(h // 2, w // 2), mode='nearest' - ) + targets = F.interpolate(targets, size=(h // 2, w // 2), mode="nearest") # targets = rearrange(targets,'b c h w -> b h w c') return targets - def compute_top_k(self, logits, labels, k, reduction='mean'): + def compute_top_k(self, logits, labels, k, reduction="mean"): _, top_ks = torch.topk(logits, k, dim=1) - if reduction == 'mean': - return ( - (top_ks == labels[:, None]).float().sum(dim=-1).mean().item() - ) - elif reduction == 'none': + if reduction == "mean": + return (top_ks == labels[:, None]).float().sum(dim=-1).mean().item() + elif reduction == "none": return (top_ks == labels[:, None]).float().sum(dim=-1) def on_train_epoch_start(self): # save some memory - self.diffusion_model.model.to('cpu') + self.diffusion_model.model.to("cpu") @torch.no_grad() def write_logs(self, loss, logits, targets): - log_prefix = 'train' if self.training else 'val' + log_prefix = "train" if self.training else "val" log = {} - log[f'{log_prefix}/loss'] = loss.mean() - log[f'{log_prefix}/acc@1'] = self.compute_top_k( - logits, targets, k=1, reduction='mean' + log[f"{log_prefix}/loss"] = loss.mean() + log[f"{log_prefix}/acc@1"] = self.compute_top_k( + logits, targets, k=1, reduction="mean" ) - log[f'{log_prefix}/acc@5'] = self.compute_top_k( - logits, targets, k=5, reduction='mean' + log[f"{log_prefix}/acc@5"] = self.compute_top_k( + logits, targets, k=5, reduction="mean" ) self.log_dict( @@ -214,19 +199,17 @@ class NoisyLatentImageClassifier(pl.LightningModule): on_step=self.training, on_epoch=True, ) + self.log("loss", log[f"{log_prefix}/loss"], prog_bar=True, logger=False) self.log( - 'loss', log[f'{log_prefix}/loss'], prog_bar=True, logger=False - ) - self.log( - 'global_step', + "global_step", self.global_step, logger=False, on_epoch=False, prog_bar=True, ) - lr = self.optimizers().param_groups[0]['lr'] + lr = self.optimizers().param_groups[0]["lr"] self.log( - 'lr_abs', + "lr_abs", lr, on_step=True, logger=True, @@ -249,13 +232,11 @@ class NoisyLatentImageClassifier(pl.LightningModule): device=self.device, ).long() else: - t = torch.full( - size=(x.shape[0],), fill_value=t, device=self.device - ).long() + t = torch.full(size=(x.shape[0],), fill_value=t, device=self.device).long() x_noisy = self.get_x_noisy(x, t) logits = self(x_noisy, t) - loss = F.cross_entropy(logits, targets, reduction='none') + loss = F.cross_entropy(logits, targets, reduction="none") self.write_logs(loss.detach(), logits.detach(), targets.detach()) @@ -268,7 +249,7 @@ class NoisyLatentImageClassifier(pl.LightningModule): def reset_noise_accs(self): self.noisy_acc = { - t: {'acc@1': [], 'acc@5': []} + t: {"acc@1": [], "acc@5": []} for t in range( 0, self.diffusion_model.num_timesteps, @@ -285,11 +266,11 @@ class NoisyLatentImageClassifier(pl.LightningModule): for t in self.noisy_acc: _, logits, _, targets = self.shared_step(batch, t) - self.noisy_acc[t]['acc@1'].append( - self.compute_top_k(logits, targets, k=1, reduction='mean') + self.noisy_acc[t]["acc@1"].append( + self.compute_top_k(logits, targets, k=1, reduction="mean") ) - self.noisy_acc[t]['acc@5'].append( - self.compute_top_k(logits, targets, k=5, reduction='mean') + self.noisy_acc[t]["acc@5"].append( + self.compute_top_k(logits, targets, k=5, reduction="mean") ) return loss @@ -304,14 +285,12 @@ class NoisyLatentImageClassifier(pl.LightningModule): if self.use_scheduler: scheduler = instantiate_from_config(self.scheduler_config) - print('Setting up LambdaLR scheduler...') + print("Setting up LambdaLR scheduler...") scheduler = [ { - 'scheduler': LambdaLR( - optimizer, lr_lambda=scheduler.schedule - ), - 'interval': 'step', - 'frequency': 1, + "scheduler": LambdaLR(optimizer, lr_lambda=scheduler.schedule), + "interval": "step", + "frequency": 1, } ] return [optimizer], scheduler @@ -322,32 +301,28 @@ class NoisyLatentImageClassifier(pl.LightningModule): def log_images(self, batch, N=8, *args, **kwargs): log = dict() x = self.get_input(batch, self.diffusion_model.first_stage_key) - log['inputs'] = x + log["inputs"] = x y = self.get_conditioning(batch) - if self.label_key == 'class_label': - y = log_txt_as_img((x.shape[2], x.shape[3]), batch['human_label']) - log['labels'] = y + if self.label_key == "class_label": + y = log_txt_as_img((x.shape[2], x.shape[3]), batch["human_label"]) + log["labels"] = y if ismap(y): - log['labels'] = self.diffusion_model.to_rgb(y) + log["labels"] = self.diffusion_model.to_rgb(y) for step in range(self.log_steps): current_time = step * self.log_time_interval _, logits, x_noisy, _ = self.shared_step(batch, t=current_time) - log[f'inputs@t{current_time}'] = x_noisy + log[f"inputs@t{current_time}"] = x_noisy - pred = F.one_hot( - logits.argmax(dim=1), num_classes=self.num_classes - ) - pred = rearrange(pred, 'b h w c -> b c h w') + pred = F.one_hot(logits.argmax(dim=1), num_classes=self.num_classes) + pred = rearrange(pred, "b h w c -> b c h w") - log[f'pred@t{current_time}'] = self.diffusion_model.to_rgb( - pred - ) + log[f"pred@t{current_time}"] = self.diffusion_model.to_rgb(pred) for key in log: log[key] = log[key][:N] diff --git a/invokeai/backend/stable_diffusion/diffusion/cross_attention_control.py b/invokeai/backend/stable_diffusion/diffusion/cross_attention_control.py index 3373bf0e61..f933a11a6f 100644 --- a/invokeai/backend/stable_diffusion/diffusion/cross_attention_control.py +++ b/invokeai/backend/stable_diffusion/diffusion/cross_attention_control.py @@ -1,20 +1,19 @@ - # adapted from bloc97's CrossAttentionControl colab # https://github.com/bloc97/CrossAttentionControl import enum import math -from typing import Optional, Callable +from typing import Callable, Optional +import diffusers import psutil import torch -import diffusers +from compel.cross_attention_control import Arguments +from diffusers.models.cross_attention import AttnProcessor +from diffusers.models.unet_2d_condition import UNet2DConditionModel from torch import nn -from compel.cross_attention_control import Arguments -from diffusers.models.unet_2d_condition import UNet2DConditionModel -from diffusers.models.cross_attention import AttnProcessor from ...util import torch_dtype @@ -24,13 +23,12 @@ class CrossAttentionType(enum.Enum): class Context: - cross_attention_mask: Optional[torch.Tensor] cross_attention_index_map: Optional[torch.Tensor] class Action(enum.Enum): NONE = 0 - SAVE = 1, + SAVE = (1,) APPLY = 2 def __init__(self, arguments: Arguments, step_count: int): @@ -53,11 +51,13 @@ class Context: self.clear_requests(cleanup=True) def register_cross_attention_modules(self, model): - for name,module in get_cross_attention_modules(model, CrossAttentionType.SELF): + for name, module in get_cross_attention_modules(model, CrossAttentionType.SELF): if name in self.self_cross_attention_module_identifiers: assert False, f"name {name} cannot appear more than once" self.self_cross_attention_module_identifiers.append(name) - for name,module in get_cross_attention_modules(model, CrossAttentionType.TOKENS): + for name, module in get_cross_attention_modules( + model, CrossAttentionType.TOKENS + ): if name in self.tokens_cross_attention_module_identifiers: assert False, f"name {name} cannot appear more than once" self.tokens_cross_attention_module_identifiers.append(name) @@ -68,7 +68,9 @@ class Context: else: self.tokens_cross_attention_action = Context.Action.SAVE - def request_apply_saved_attention_maps(self, cross_attention_type: CrossAttentionType): + def request_apply_saved_attention_maps( + self, cross_attention_type: CrossAttentionType + ): if cross_attention_type == CrossAttentionType.SELF: self.self_cross_attention_action = Context.Action.APPLY else: @@ -91,8 +93,9 @@ class Context: return self.tokens_cross_attention_action == Context.Action.APPLY return False - def get_active_cross_attention_control_types_for_step(self, percent_through:float=None)\ - -> list[CrossAttentionType]: + def get_active_cross_attention_control_types_for_step( + self, percent_through: float = None + ) -> list[CrossAttentionType]: """ Should cross-attention control be applied on the given step? :param percent_through: How far through the step sequence are we (0.0=pure noise, 1.0=completely denoised image). Expected range 0.0..<1.0. @@ -103,50 +106,73 @@ class Context: opts = self.arguments.edit_options to_control = [] - if opts['s_start'] <= percent_through < opts['s_end']: + if opts["s_start"] <= percent_through < opts["s_end"]: to_control.append(CrossAttentionType.SELF) - if opts['t_start'] <= percent_through < opts['t_end']: + if opts["t_start"] <= percent_through < opts["t_end"]: to_control.append(CrossAttentionType.TOKENS) return to_control - def save_slice(self, identifier: str, slice: torch.Tensor, dim: Optional[int], offset: int, - slice_size: Optional[int]): + def save_slice( + self, + identifier: str, + slice: torch.Tensor, + dim: Optional[int], + offset: int, + slice_size: Optional[int], + ): if identifier not in self.saved_cross_attention_maps: self.saved_cross_attention_maps[identifier] = { - 'dim': dim, - 'slice_size': slice_size, - 'slices': {offset or 0: slice} + "dim": dim, + "slice_size": slice_size, + "slices": {offset or 0: slice}, } else: - self.saved_cross_attention_maps[identifier]['slices'][offset or 0] = slice + self.saved_cross_attention_maps[identifier]["slices"][offset or 0] = slice - def get_slice(self, identifier: str, requested_dim: Optional[int], requested_offset: int, slice_size: int): + def get_slice( + self, + identifier: str, + requested_dim: Optional[int], + requested_offset: int, + slice_size: int, + ): saved_attention_dict = self.saved_cross_attention_maps[identifier] if requested_dim is None: - if saved_attention_dict['dim'] is not None: - raise RuntimeError(f"dim mismatch: expected dim=None, have {saved_attention_dict['dim']}") - return saved_attention_dict['slices'][0] - - if saved_attention_dict['dim'] == requested_dim: - if slice_size != saved_attention_dict['slice_size']: + if saved_attention_dict["dim"] is not None: raise RuntimeError( - f"slice_size mismatch: expected slice_size={slice_size}, have {saved_attention_dict['slice_size']}") - return saved_attention_dict['slices'][requested_offset] + f"dim mismatch: expected dim=None, have {saved_attention_dict['dim']}" + ) + return saved_attention_dict["slices"][0] - if saved_attention_dict['dim'] is None: - whole_saved_attention = saved_attention_dict['slices'][0] + if saved_attention_dict["dim"] == requested_dim: + if slice_size != saved_attention_dict["slice_size"]: + raise RuntimeError( + f"slice_size mismatch: expected slice_size={slice_size}, have {saved_attention_dict['slice_size']}" + ) + return saved_attention_dict["slices"][requested_offset] + + if saved_attention_dict["dim"] is None: + whole_saved_attention = saved_attention_dict["slices"][0] if requested_dim == 0: - return whole_saved_attention[requested_offset:requested_offset + slice_size] + return whole_saved_attention[ + requested_offset : requested_offset + slice_size + ] elif requested_dim == 1: - return whole_saved_attention[:, requested_offset:requested_offset + slice_size] + return whole_saved_attention[ + :, requested_offset : requested_offset + slice_size + ] - raise RuntimeError(f"Cannot convert dim {saved_attention_dict['dim']} to requested dim {requested_dim}") + raise RuntimeError( + f"Cannot convert dim {saved_attention_dict['dim']} to requested dim {requested_dim}" + ) - def get_slicing_strategy(self, identifier: str) -> tuple[Optional[int], Optional[int]]: + def get_slicing_strategy( + self, identifier: str + ) -> tuple[Optional[int], Optional[int]]: saved_attention = self.saved_cross_attention_maps.get(identifier, None) if saved_attention is None: return None, None - return saved_attention['dim'], saved_attention['slice_size'] + return saved_attention["dim"], saved_attention["slice_size"] def clear_requests(self, cleanup=True): self.tokens_cross_attention_action = Context.Action.NONE @@ -156,9 +182,8 @@ class Context: def offload_saved_attention_slices_to_cpu(self): for key, map_dict in self.saved_cross_attention_maps.items(): - for offset, slice in map_dict['slices'].items(): - map_dict[offset] = slice.to('cpu') - + for offset, slice in map_dict["slices"].items(): + map_dict[offset] = slice.to("cpu") class InvokeAICrossAttentionMixin: @@ -167,14 +192,20 @@ class InvokeAICrossAttentionMixin: through both to an attention_slice_wrangler and a slicing_strategy_getter for custom attention map wrangling and dymamic slicing strategy selection. """ + def __init__(self): self.mem_total_gb = psutil.virtual_memory().total // (1 << 30) self.attention_slice_wrangler = None self.slicing_strategy_getter = None self.attention_slice_calculated_callback = None - def set_attention_slice_wrangler(self, wrangler: Optional[Callable[[nn.Module, torch.Tensor, int, int, int], torch.Tensor]]): - ''' + def set_attention_slice_wrangler( + self, + wrangler: Optional[ + Callable[[nn.Module, torch.Tensor, int, int, int], torch.Tensor] + ], + ): + """ Set custom attention calculator to be called when attention is calculated :param wrangler: Callback, with args (module, suggested_attention_slice, dim, offset, slice_size), which returns either the suggested_attention_slice or an adjusted equivalent. @@ -185,20 +216,30 @@ class InvokeAICrossAttentionMixin: Pass None to use the default attention calculation. :return: - ''' + """ self.attention_slice_wrangler = wrangler - def set_slicing_strategy_getter(self, getter: Optional[Callable[[nn.Module], tuple[int,int]]]): + def set_slicing_strategy_getter( + self, getter: Optional[Callable[[nn.Module], tuple[int, int]]] + ): self.slicing_strategy_getter = getter - def set_attention_slice_calculated_callback(self, callback: Optional[Callable[[torch.Tensor], None]]): + def set_attention_slice_calculated_callback( + self, callback: Optional[Callable[[torch.Tensor], None]] + ): self.attention_slice_calculated_callback = callback def einsum_lowest_level(self, query, key, value, dim, offset, slice_size): # calculate attention scores - #attention_scores = torch.einsum('b i d, b j d -> b i j', q, k) + # attention_scores = torch.einsum('b i d, b j d -> b i j', q, k) attention_scores = torch.baddbmm( - torch.empty(query.shape[0], query.shape[1], key.shape[1], dtype=query.dtype, device=query.device), + torch.empty( + query.shape[0], + query.shape[1], + key.shape[1], + dtype=query.dtype, + device=query.device, + ), query, key.transpose(-1, -2), beta=0, @@ -206,35 +247,49 @@ class InvokeAICrossAttentionMixin: ) # calculate attention slice by taking the best scores for each latent pixel - default_attention_slice = attention_scores.softmax(dim=-1, dtype=attention_scores.dtype) + default_attention_slice = attention_scores.softmax( + dim=-1, dtype=attention_scores.dtype + ) attention_slice_wrangler = self.attention_slice_wrangler if attention_slice_wrangler is not None: - attention_slice = attention_slice_wrangler(self, default_attention_slice, dim, offset, slice_size) + attention_slice = attention_slice_wrangler( + self, default_attention_slice, dim, offset, slice_size + ) else: attention_slice = default_attention_slice if self.attention_slice_calculated_callback is not None: - self.attention_slice_calculated_callback(attention_slice, dim, offset, slice_size) + self.attention_slice_calculated_callback( + attention_slice, dim, offset, slice_size + ) hidden_states = torch.bmm(attention_slice, value) return hidden_states def einsum_op_slice_dim0(self, q, k, v, slice_size): - r = torch.zeros(q.shape[0], q.shape[1], v.shape[2], device=q.device, dtype=q.dtype) + r = torch.zeros( + q.shape[0], q.shape[1], v.shape[2], device=q.device, dtype=q.dtype + ) for i in range(0, q.shape[0], slice_size): end = i + slice_size - r[i:end] = self.einsum_lowest_level(q[i:end], k[i:end], v[i:end], dim=0, offset=i, slice_size=slice_size) + r[i:end] = self.einsum_lowest_level( + q[i:end], k[i:end], v[i:end], dim=0, offset=i, slice_size=slice_size + ) return r def einsum_op_slice_dim1(self, q, k, v, slice_size): - r = torch.zeros(q.shape[0], q.shape[1], v.shape[2], device=q.device, dtype=q.dtype) + r = torch.zeros( + q.shape[0], q.shape[1], v.shape[2], device=q.device, dtype=q.dtype + ) for i in range(0, q.shape[1], slice_size): end = i + slice_size - r[:, i:end] = self.einsum_lowest_level(q[:, i:end], k, v, dim=1, offset=i, slice_size=slice_size) + r[:, i:end] = self.einsum_lowest_level( + q[:, i:end], k, v, dim=1, offset=i, slice_size=slice_size + ) return r def einsum_op_mps_v1(self, q, k, v): - if q.shape[1] <= 4096: # (512x512) max q.shape[1]: 4096 + if q.shape[1] <= 4096: # (512x512) max q.shape[1]: 4096 return self.einsum_lowest_level(q, k, v, None, None, None) else: slice_size = math.floor(2**30 / (q.shape[0] * q.shape[1])) @@ -272,13 +327,12 @@ class InvokeAICrossAttentionMixin: # Divide factor of safety as there's copying and fragmentation return self.einsum_op_tensor_mem(q, k, v, mem_free_total / 3.3 / (1 << 20)) - def get_invokeai_attention_mem_efficient(self, q, k, v): - if q.device.type == 'cuda': - #print("in get_attention_mem_efficient with q shape", q.shape, ", k shape", k.shape, ", free memory is", get_mem_free_total(q.device)) + if q.device.type == "cuda": + # print("in get_attention_mem_efficient with q shape", q.shape, ", k shape", k.shape, ", free memory is", get_mem_free_total(q.device)) return self.einsum_op_cuda(q, k, v) - if q.device.type == 'mps' or q.device.type == 'cpu': + if q.device.type == "mps" or q.device.type == "cpu": if self.mem_total_gb >= 32: return self.einsum_op_mps_v1(q, k, v) return self.einsum_op_mps_v2(q, k, v) @@ -288,8 +342,11 @@ class InvokeAICrossAttentionMixin: return self.einsum_op_tensor_mem(q, k, v, 32) - -def restore_default_cross_attention(model, is_running_diffusers: bool, restore_attention_processor: Optional[AttnProcessor]=None): +def restore_default_cross_attention( + model, + is_running_diffusers: bool, + restore_attention_processor: Optional[AttnProcessor] = None, +): if is_running_diffusers: unet = model unet.set_attn_processor(restore_attention_processor or CrossAttnProcessor()) @@ -297,7 +354,7 @@ def restore_default_cross_attention(model, is_running_diffusers: bool, restore_a remove_attention_function(model) -def override_cross_attention(model, context: Context, is_running_diffusers = False): +def override_cross_attention(model, context: Context, is_running_diffusers=False): """ Inject attention parameters and functions into the passed in model to enable cross attention editing. @@ -316,7 +373,7 @@ def override_cross_attention(model, context: Context, is_running_diffusers = Fal indices = torch.arange(max_length, dtype=torch.long) for name, a0, a1, b0, b1 in context.arguments.edit_opcodes: if b0 < max_length: - if name == "equal":# or (name == "replace" and a1 - a0 == b1 - b0): + if name == "equal": # or (name == "replace" and a1 - a0 == b1 - b0): # these tokens have not been edited indices[b0:b1] = indices_target[a0:a1] mask[b0:b1] = 1 @@ -332,7 +389,14 @@ def override_cross_attention(model, context: Context, is_running_diffusers = Fal else: # try to re-use an existing slice size default_slice_size = 4 - slice_size = next((p.slice_size for p in old_attn_processors.values() if type(p) is SlicedAttnProcessor), default_slice_size) + slice_size = next( + ( + p.slice_size + for p in old_attn_processors.values() + if type(p) is SlicedAttnProcessor + ), + default_slice_size, + ) unet.set_attn_processor(SlicedSwapCrossAttnProcesser(slice_size=slice_size)) return old_attn_processors else: @@ -341,65 +405,96 @@ def override_cross_attention(model, context: Context, is_running_diffusers = Fal return None +def get_cross_attention_modules( + model, which: CrossAttentionType +) -> list[tuple[str, InvokeAICrossAttentionMixin]]: + from ldm.modules.attention import CrossAttention # avoid circular import - -def get_cross_attention_modules(model, which: CrossAttentionType) -> list[tuple[str, InvokeAICrossAttentionMixin]]: - from ldm.modules.attention import CrossAttention # avoid circular import - cross_attention_class: type = InvokeAIDiffusersCrossAttention if isinstance(model,UNet2DConditionModel) else CrossAttention + cross_attention_class: type = ( + InvokeAIDiffusersCrossAttention + if isinstance(model, UNet2DConditionModel) + else CrossAttention + ) which_attn = "attn1" if which is CrossAttentionType.SELF else "attn2" - attention_module_tuples = [(name,module) for name, module in model.named_modules() if - isinstance(module, cross_attention_class) and which_attn in name] + attention_module_tuples = [ + (name, module) + for name, module in model.named_modules() + if isinstance(module, cross_attention_class) and which_attn in name + ] cross_attention_modules_in_model_count = len(attention_module_tuples) expected_count = 16 if cross_attention_modules_in_model_count != expected_count: # non-fatal error but .swap() won't work. - print(f"Error! CrossAttentionControl found an unexpected number of {cross_attention_class} modules in the model " + - f"(expected {expected_count}, found {cross_attention_modules_in_model_count}). Either monkey-patching failed " + - f"or some assumption has changed about the structure of the model itself. Please fix the monkey-patching, " + - f"and/or update the {expected_count} above to an appropriate number, and/or find and inform someone who knows " + - f"what it means. This error is non-fatal, but it is likely that .swap() and attention map display will not " + - f"work properly until it is fixed.") + print( + f"Error! CrossAttentionControl found an unexpected number of {cross_attention_class} modules in the model " + + f"(expected {expected_count}, found {cross_attention_modules_in_model_count}). Either monkey-patching failed " + + f"or some assumption has changed about the structure of the model itself. Please fix the monkey-patching, " + + f"and/or update the {expected_count} above to an appropriate number, and/or find and inform someone who knows " + + f"what it means. This error is non-fatal, but it is likely that .swap() and attention map display will not " + + f"work properly until it is fixed." + ) return attention_module_tuples def inject_attention_function(unet, context: Context): # ORIGINAL SOURCE CODE: https://github.com/huggingface/diffusers/blob/91ddd2a25b848df0fa1262d4f1cd98c7ccb87750/src/diffusers/models/attention.py#L276 - def attention_slice_wrangler(module, suggested_attention_slice:torch.Tensor, dim, offset, slice_size): - - #memory_usage = suggested_attention_slice.element_size() * suggested_attention_slice.nelement() + def attention_slice_wrangler( + module, suggested_attention_slice: torch.Tensor, dim, offset, slice_size + ): + # memory_usage = suggested_attention_slice.element_size() * suggested_attention_slice.nelement() attention_slice = suggested_attention_slice if context.get_should_save_maps(module.identifier): - #print(module.identifier, "saving suggested_attention_slice of shape", + # print(module.identifier, "saving suggested_attention_slice of shape", # suggested_attention_slice.shape, "dim", dim, "offset", offset) - slice_to_save = attention_slice.to('cpu') if dim is not None else attention_slice - context.save_slice(module.identifier, slice_to_save, dim=dim, offset=offset, slice_size=slice_size) + slice_to_save = ( + attention_slice.to("cpu") if dim is not None else attention_slice + ) + context.save_slice( + module.identifier, + slice_to_save, + dim=dim, + offset=offset, + slice_size=slice_size, + ) elif context.get_should_apply_saved_maps(module.identifier): - #print(module.identifier, "applying saved attention slice for dim", dim, "offset", offset) - saved_attention_slice = context.get_slice(module.identifier, dim, offset, slice_size) + # print(module.identifier, "applying saved attention slice for dim", dim, "offset", offset) + saved_attention_slice = context.get_slice( + module.identifier, dim, offset, slice_size + ) # slice may have been offloaded to CPU - saved_attention_slice = saved_attention_slice.to(suggested_attention_slice.device) + saved_attention_slice = saved_attention_slice.to( + suggested_attention_slice.device + ) if context.is_tokens_cross_attention(module.identifier): index_map = context.cross_attention_index_map - remapped_saved_attention_slice = torch.index_select(saved_attention_slice, -1, index_map) + remapped_saved_attention_slice = torch.index_select( + saved_attention_slice, -1, index_map + ) this_attention_slice = suggested_attention_slice - mask = context.cross_attention_mask.to(torch_dtype(suggested_attention_slice.device)) + mask = context.cross_attention_mask.to( + torch_dtype(suggested_attention_slice.device) + ) saved_mask = mask this_mask = 1 - mask - attention_slice = remapped_saved_attention_slice * saved_mask + \ - this_attention_slice * this_mask + attention_slice = ( + remapped_saved_attention_slice * saved_mask + + this_attention_slice * this_mask + ) else: # just use everything attention_slice = saved_attention_slice return attention_slice - cross_attention_modules = get_cross_attention_modules(unet, CrossAttentionType.TOKENS) + get_cross_attention_modules(unet, CrossAttentionType.SELF) + cross_attention_modules = get_cross_attention_modules( + unet, CrossAttentionType.TOKENS + ) + get_cross_attention_modules(unet, CrossAttentionType.SELF) for identifier, module in cross_attention_modules: module.identifier = identifier try: @@ -408,56 +503,61 @@ def inject_attention_function(unet, context: Context): lambda module: context.get_slicing_strategy(identifier) ) except AttributeError as e: - if is_attribute_error_about(e, 'set_attention_slice_wrangler'): - print(f"TODO: implement set_attention_slice_wrangler for {type(module)}") # TODO + if is_attribute_error_about(e, "set_attention_slice_wrangler"): + print( + f"TODO: implement set_attention_slice_wrangler for {type(module)}" + ) # TODO else: raise def remove_attention_function(unet): - cross_attention_modules = get_cross_attention_modules(unet, CrossAttentionType.TOKENS) + get_cross_attention_modules(unet, CrossAttentionType.SELF) + cross_attention_modules = get_cross_attention_modules( + unet, CrossAttentionType.TOKENS + ) + get_cross_attention_modules(unet, CrossAttentionType.SELF) for identifier, module in cross_attention_modules: try: # clear wrangler callback module.set_attention_slice_wrangler(None) module.set_slicing_strategy_getter(None) except AttributeError as e: - if is_attribute_error_about(e, 'set_attention_slice_wrangler'): - print(f"TODO: implement set_attention_slice_wrangler for {type(module)}") + if is_attribute_error_about(e, "set_attention_slice_wrangler"): + print( + f"TODO: implement set_attention_slice_wrangler for {type(module)}" + ) else: raise def is_attribute_error_about(error: AttributeError, attribute: str): - if hasattr(error, 'name'): # Python 3.10 + if hasattr(error, "name"): # Python 3.10 return error.name == attribute else: # Python 3.9 return attribute in str(error) - def get_mem_free_total(device): - #only on cuda + # only on cuda if not torch.cuda.is_available(): return None stats = torch.cuda.memory_stats(device) - mem_active = stats['active_bytes.all.current'] - mem_reserved = stats['reserved_bytes.all.current'] + mem_active = stats["active_bytes.all.current"] + mem_reserved = stats["reserved_bytes.all.current"] mem_free_cuda, _ = torch.cuda.mem_get_info(device) mem_free_torch = mem_reserved - mem_active mem_free_total = mem_free_cuda + mem_free_torch return mem_free_total - -class InvokeAIDiffusersCrossAttention(diffusers.models.attention.CrossAttention, InvokeAICrossAttentionMixin): - +class InvokeAIDiffusersCrossAttention( + diffusers.models.attention.CrossAttention, InvokeAICrossAttentionMixin +): def __init__(self, **kwargs): super().__init__(**kwargs) InvokeAICrossAttentionMixin.__init__(self) def _attention(self, query, key, value, attention_mask=None): - #default_result = super()._attention(query, key, value) + # default_result = super()._attention(query, key, value) if attention_mask is not None: print(f"{type(self).__name__} ignoring passed-in attention_mask") attention_result = self.get_invokeai_attention_mem_efficient(query, key, value) @@ -466,9 +566,6 @@ class InvokeAIDiffusersCrossAttention(diffusers.models.attention.CrossAttention, return hidden_states - - - ## 🧨diffusers implementation follows @@ -501,25 +598,30 @@ class CrossAttnProcessor: return hidden_states """ -from dataclasses import field, dataclass +from dataclasses import dataclass, field import torch - -from diffusers.models.cross_attention import CrossAttention, CrossAttnProcessor, SlicedAttnProcessor +from diffusers.models.cross_attention import ( + CrossAttention, + CrossAttnProcessor, + SlicedAttnProcessor, +) @dataclass class SwapCrossAttnContext: modified_text_embeddings: torch.Tensor - index_map: torch.Tensor # maps from original prompt token indices to the equivalent tokens in the modified prompt - mask: torch.Tensor # in the target space of the index_map + index_map: torch.Tensor # maps from original prompt token indices to the equivalent tokens in the modified prompt + mask: torch.Tensor # in the target space of the index_map cross_attention_types_to_do: list[CrossAttentionType] = field(default_factory=list) - def __int__(self, - cac_types_to_do: [CrossAttentionType], - modified_text_embeddings: torch.Tensor, - index_map: torch.Tensor, - mask: torch.Tensor): + def __int__( + self, + cac_types_to_do: [CrossAttentionType], + modified_text_embeddings: torch.Tensor, + index_map: torch.Tensor, + mask: torch.Tensor, + ): self.cross_attention_types_to_do = cac_types_to_do self.modified_text_embeddings = modified_text_embeddings self.index_map = index_map @@ -529,9 +631,9 @@ class SwapCrossAttnContext: return attn_type in self.cross_attention_types_to_do @classmethod - def make_mask_and_index_map(cls, edit_opcodes: list[tuple[str, int, int, int, int]], max_length: int) \ - -> tuple[torch.Tensor, torch.Tensor]: - + def make_mask_and_index_map( + cls, edit_opcodes: list[tuple[str, int, int, int, int]], max_length: int + ) -> tuple[torch.Tensor, torch.Tensor]: # mask=1 means use original prompt attention, mask=0 means use modified prompt attention mask = torch.zeros(max_length) indices_target = torch.arange(max_length, dtype=torch.long) @@ -547,28 +649,42 @@ class SwapCrossAttnContext: class SlicedSwapCrossAttnProcesser(SlicedAttnProcessor): - # TODO: dynamically pick slice size based on memory conditions - def __call__(self, attn: CrossAttention, hidden_states, encoder_hidden_states=None, attention_mask=None, - # kwargs - swap_cross_attn_context: SwapCrossAttnContext=None): - - attention_type = CrossAttentionType.SELF if encoder_hidden_states is None else CrossAttentionType.TOKENS + def __call__( + self, + attn: CrossAttention, + hidden_states, + encoder_hidden_states=None, + attention_mask=None, + # kwargs + swap_cross_attn_context: SwapCrossAttnContext = None, + ): + attention_type = ( + CrossAttentionType.SELF + if encoder_hidden_states is None + else CrossAttentionType.TOKENS + ) # if cross-attention control is not in play, just call through to the base implementation. - if attention_type is CrossAttentionType.SELF or \ - swap_cross_attn_context is None or \ - not swap_cross_attn_context.wants_cross_attention_control(attention_type): - #print(f"SwapCrossAttnContext for {attention_type} not active - passing request to superclass") - return super().__call__(attn, hidden_states, encoder_hidden_states, attention_mask) - #else: + if ( + attention_type is CrossAttentionType.SELF + or swap_cross_attn_context is None + or not swap_cross_attn_context.wants_cross_attention_control(attention_type) + ): + # print(f"SwapCrossAttnContext for {attention_type} not active - passing request to superclass") + return super().__call__( + attn, hidden_states, encoder_hidden_states, attention_mask + ) + # else: # print(f"SwapCrossAttnContext for {attention_type} active") batch_size, sequence_length, _ = hidden_states.shape attention_mask = attn.prepare_attention_mask( - attention_mask=attention_mask, target_length=sequence_length, - batch_size=batch_size) + attention_mask=attention_mask, + target_length=sequence_length, + batch_size=batch_size, + ) query = attn.to_q(hidden_states) dim = query.shape[-1] @@ -589,41 +705,51 @@ class SlicedSwapCrossAttnProcesser(SlicedAttnProcessor): # compute slices and prepare output tensor batch_size_attention = query.shape[0] hidden_states = torch.zeros( - (batch_size_attention, sequence_length, dim // attn.heads), device=query.device, dtype=query.dtype + (batch_size_attention, sequence_length, dim // attn.heads), + device=query.device, + dtype=query.dtype, ) # do slices - for i in range(max(1,hidden_states.shape[0] // self.slice_size)): + for i in range(max(1, hidden_states.shape[0] // self.slice_size)): start_idx = i * self.slice_size end_idx = (i + 1) * self.slice_size query_slice = query[start_idx:end_idx] original_key_slice = original_text_key[start_idx:end_idx] modified_key_slice = modified_text_key[start_idx:end_idx] - attn_mask_slice = attention_mask[start_idx:end_idx] if attention_mask is not None else None + attn_mask_slice = ( + attention_mask[start_idx:end_idx] + if attention_mask is not None + else None + ) - original_attn_slice = attn.get_attention_scores(query_slice, original_key_slice, attn_mask_slice) - modified_attn_slice = attn.get_attention_scores(query_slice, modified_key_slice, attn_mask_slice) + original_attn_slice = attn.get_attention_scores( + query_slice, original_key_slice, attn_mask_slice + ) + modified_attn_slice = attn.get_attention_scores( + query_slice, modified_key_slice, attn_mask_slice + ) # because the prompt modifications may result in token sequences shifted forwards or backwards, # the original attention probabilities must be remapped to account for token index changes in the # modified prompt - remapped_original_attn_slice = torch.index_select(original_attn_slice, -1, - swap_cross_attn_context.index_map) + remapped_original_attn_slice = torch.index_select( + original_attn_slice, -1, swap_cross_attn_context.index_map + ) # only some tokens taken from the original attention probabilities. this is controlled by the mask. mask = swap_cross_attn_context.mask inverse_mask = 1 - mask - attn_slice = \ - remapped_original_attn_slice * mask + \ - modified_attn_slice * inverse_mask + attn_slice = ( + remapped_original_attn_slice * mask + modified_attn_slice * inverse_mask + ) del remapped_original_attn_slice, modified_attn_slice attn_slice = torch.bmm(attn_slice, modified_value[start_idx:end_idx]) hidden_states[start_idx:end_idx] = attn_slice - # done hidden_states = attn.batch_to_head_dim(hidden_states) @@ -636,7 +762,7 @@ class SlicedSwapCrossAttnProcesser(SlicedAttnProcessor): class SwapCrossAttnProcessor(SlicedSwapCrossAttnProcesser): - def __init__(self): - super(SwapCrossAttnProcessor, self).__init__(slice_size=int(1e9)) # massive slice size = don't slice - + super(SwapCrossAttnProcessor, self).__init__( + slice_size=int(1e9) + ) # massive slice size = don't slice diff --git a/invokeai/backend/stable_diffusion/diffusion/cross_attention_map_saving.py b/invokeai/backend/stable_diffusion/diffusion/cross_attention_map_saving.py index eede431d33..c489c2f0a9 100644 --- a/invokeai/backend/stable_diffusion/diffusion/cross_attention_map_saving.py +++ b/invokeai/backend/stable_diffusion/diffusion/cross_attention_map_saving.py @@ -2,17 +2,17 @@ import math import PIL import torch -from torchvision.transforms.functional import resize as tv_resize, InterpolationMode +from torchvision.transforms.functional import InterpolationMode +from torchvision.transforms.functional import resize as tv_resize -from .cross_attention_control import get_cross_attention_modules, CrossAttentionType +from .cross_attention_control import CrossAttentionType, get_cross_attention_modules -class AttentionMapSaver(): - +class AttentionMapSaver: def __init__(self, token_ids: range, latents_shape: torch.Size): self.token_ids = token_ids self.latents_shape = latents_shape - #self.collated_maps = #torch.zeros([len(token_ids), latents_shape[0], latents_shape[1]]) + # self.collated_maps = #torch.zeros([len(token_ids), latents_shape[0], latents_shape[1]]) self.collated_maps = {} def clear_maps(self): @@ -25,7 +25,7 @@ class AttentionMapSaver(): :param key: Storage key. If a map already exists for this key it will be summed with the incoming data. In this case the maps sizes (H and W) should match. :return: None """ - key_and_size = f'{key}_{maps.shape[1]}' + key_and_size = f"{key}_{maps.shape[1]}" # extract desired tokens maps = maps[:, :, self.token_ids] @@ -35,12 +35,12 @@ class AttentionMapSaver(): # store if key_and_size not in self.collated_maps: - self.collated_maps[key_and_size] = torch.zeros_like(maps, device='cpu') + self.collated_maps[key_and_size] = torch.zeros_like(maps, device="cpu") self.collated_maps[key_and_size] += maps.cpu() def write_maps_to_disk(self, path: str): pil_image = self.get_stacked_maps_image() - pil_image.save(path, 'PNG') + pil_image.save(path, "PNG") def get_stacked_maps_image(self) -> PIL.Image: """ @@ -57,39 +57,50 @@ class AttentionMapSaver(): merged = None for key, maps in self.collated_maps.items(): - # maps has shape [(H*W), N] for N tokens # but we want [N, H, W] - this_scale_factor = math.sqrt(maps.shape[0] / (latents_width * latents_height)) + this_scale_factor = math.sqrt( + maps.shape[0] / (latents_width * latents_height) + ) this_maps_height = int(float(latents_height) * this_scale_factor) this_maps_width = int(float(latents_width) * this_scale_factor) # and we need to do some dimension juggling - maps = torch.reshape(torch.swapdims(maps, 0, 1), [num_tokens, this_maps_height, this_maps_width]) + maps = torch.reshape( + torch.swapdims(maps, 0, 1), + [num_tokens, this_maps_height, this_maps_width], + ) # scale to output size if necessary if this_scale_factor != 1: - maps = tv_resize(maps, [latents_height, latents_width], InterpolationMode.BICUBIC) + maps = tv_resize( + maps, [latents_height, latents_width], InterpolationMode.BICUBIC + ) # normalize maps_min = torch.min(maps) maps_range = torch.max(maps) - maps_min - #print(f"map {key} size {[this_maps_width, this_maps_height]} range {[maps_min, maps_min + maps_range]}") + # print(f"map {key} size {[this_maps_width, this_maps_height]} range {[maps_min, maps_min + maps_range]}") maps_normalized = (maps - maps_min) / maps_range # expand to (-0.1, 1.1) and clamp maps_normalized_expanded = maps_normalized * 1.1 - 0.05 - maps_normalized_expanded_clamped = torch.clamp(maps_normalized_expanded, 0, 1) + maps_normalized_expanded_clamped = torch.clamp( + maps_normalized_expanded, 0, 1 + ) # merge together, producing a vertical stack - maps_stacked = torch.reshape(maps_normalized_expanded_clamped, [num_tokens * latents_height, latents_width]) + maps_stacked = torch.reshape( + maps_normalized_expanded_clamped, + [num_tokens * latents_height, latents_width], + ) if merged is None: merged = maps_stacked else: # screen blend - merged = 1 - (1 - maps_stacked)*(1 - merged) + merged = 1 - (1 - maps_stacked) * (1 - merged) if merged is None: return None - merged_bytes = merged.mul(0xff).byte() - return PIL.Image.fromarray(merged_bytes.numpy(), mode='L') + merged_bytes = merged.mul(0xFF).byte() + return PIL.Image.fromarray(merged_bytes.numpy(), mode="L") diff --git a/invokeai/backend/stable_diffusion/diffusion/ddim.py b/invokeai/backend/stable_diffusion/diffusion/ddim.py index 41d6249fc5..87f6f2166b 100644 --- a/invokeai/backend/stable_diffusion/diffusion/ddim.py +++ b/invokeai/backend/stable_diffusion/diffusion/ddim.py @@ -1,77 +1,82 @@ """SAMPLING ONLY.""" import torch -from .shared_invokeai_diffusion import InvokeAIDiffuserComponent + +from ..diffusionmodules.util import noise_like from .sampler import Sampler -from ..diffusionmodules.util import noise_like +from .shared_invokeai_diffusion import InvokeAIDiffuserComponent + class DDIMSampler(Sampler): - def __init__(self, model, schedule='linear', device=None, **kwargs): - super().__init__(model,schedule,model.num_timesteps,device) + def __init__(self, model, schedule="linear", device=None, **kwargs): + super().__init__(model, schedule, model.num_timesteps, device) - self.invokeai_diffuser = InvokeAIDiffuserComponent(self.model, - model_forward_callback = lambda x, sigma, cond: self.model.apply_model(x, sigma, cond)) + self.invokeai_diffuser = InvokeAIDiffuserComponent( + self.model, + model_forward_callback=lambda x, sigma, cond: self.model.apply_model( + x, sigma, cond + ), + ) def prepare_to_sample(self, t_enc, **kwargs): super().prepare_to_sample(t_enc, **kwargs) - extra_conditioning_info = kwargs.get('extra_conditioning_info', None) - all_timesteps_count = kwargs.get('all_timesteps_count', t_enc) + extra_conditioning_info = kwargs.get("extra_conditioning_info", None) + all_timesteps_count = kwargs.get("all_timesteps_count", t_enc) - if extra_conditioning_info is not None and extra_conditioning_info.wants_cross_attention_control: - self.invokeai_diffuser.override_cross_attention(extra_conditioning_info, step_count = all_timesteps_count) + if ( + extra_conditioning_info is not None + and extra_conditioning_info.wants_cross_attention_control + ): + self.invokeai_diffuser.override_cross_attention( + extra_conditioning_info, step_count=all_timesteps_count + ) else: self.invokeai_diffuser.restore_default_cross_attention() - # This is the central routine @torch.no_grad() def p_sample( - self, - x, - c, - t, - index, - repeat_noise=False, - use_original_steps=False, - quantize_denoised=False, - temperature=1.0, - noise_dropout=0.0, - score_corrector=None, - corrector_kwargs=None, - unconditional_guidance_scale=1.0, - unconditional_conditioning=None, - step_count:int=1000, # total number of steps - **kwargs, + self, + x, + c, + t, + index, + repeat_noise=False, + use_original_steps=False, + quantize_denoised=False, + temperature=1.0, + noise_dropout=0.0, + score_corrector=None, + corrector_kwargs=None, + unconditional_guidance_scale=1.0, + unconditional_conditioning=None, + step_count: int = 1000, # total number of steps + **kwargs, ): b, *_, device = *x.shape, x.device - if ( - unconditional_conditioning is None - or unconditional_guidance_scale == 1.0 - ): + if unconditional_conditioning is None or unconditional_guidance_scale == 1.0: # damian0815 would like to know when/if this code path is used e_t = self.model.apply_model(x, t, c) else: # step_index counts in the opposite direction to index - step_index = step_count-(index+1) + step_index = step_count - (index + 1) e_t = self.invokeai_diffuser.do_diffusion_step( - x, t, - unconditional_conditioning, c, + x, + t, + unconditional_conditioning, + c, unconditional_guidance_scale, - step_index=step_index + step_index=step_index, ) if score_corrector is not None: - assert self.model.parameterization == 'eps' + assert self.model.parameterization == "eps" e_t = score_corrector.modify_score( self.model, e_t, x, t, c, **corrector_kwargs ) - alphas = ( - self.model.alphas_cumprod - if use_original_steps - else self.ddim_alphas - ) + alphas = self.model.alphas_cumprod if use_original_steps else self.ddim_alphas alphas_prev = ( self.model.alphas_cumprod_prev if use_original_steps @@ -101,11 +106,8 @@ class DDIMSampler(Sampler): pred_x0, _, *_ = self.model.first_stage_model.quantize(pred_x0) # direction pointing to x_t dir_xt = (1.0 - a_prev - sigma_t**2).sqrt() * e_t - noise = ( - sigma_t * noise_like(x.shape, device, repeat_noise) * temperature - ) + noise = sigma_t * noise_like(x.shape, device, repeat_noise) * temperature if noise_dropout > 0.0: noise = torch.nn.functional.dropout(noise, p=noise_dropout) x_prev = a_prev.sqrt() * pred_x0 + dir_xt + noise return x_prev, pred_x0, None - diff --git a/invokeai/backend/stable_diffusion/diffusion/ddpm.py b/invokeai/backend/stable_diffusion/diffusion/ddpm.py index 36251a0940..6741498303 100644 --- a/invokeai/backend/stable_diffusion/diffusion/ddpm.py +++ b/invokeai/backend/stable_diffusion/diffusion/ddpm.py @@ -6,55 +6,43 @@ https://github.com/CompVis/taming-transformers -- merci """ -import torch - -import torch.nn as nn import os -import numpy as np -import pytorch_lightning as pl -from torch.optim.lr_scheduler import LambdaLR -from einops import rearrange, repeat +import urllib from contextlib import contextmanager from functools import partial -from tqdm import tqdm -from torchvision.utils import make_grid -from pytorch_lightning.utilities.distributed import rank_zero_only -from omegaconf import ListConfig -import urllib -from ..textual_inversion_manager import TextualInversionManager +import numpy as np +import pytorch_lightning as pl +import torch +import torch.nn as nn +from einops import rearrange, repeat +from omegaconf import ListConfig +from pytorch_lightning.utilities.distributed import rank_zero_only +from torch.optim.lr_scheduler import LambdaLR +from torchvision.utils import make_grid +from tqdm import tqdm + from ...util.util import ( - log_txt_as_img, - exists, - default, - ismap, - isimage, - mean_flat, count_params, + default, + exists, instantiate_from_config, + isimage, + ismap, + log_txt_as_img, + mean_flat, ) +from ..autoencoder import AutoencoderKL, IdentityFirstStage, VQModelInterface +from ..diffusionmodules.util import extract_into_tensor, make_beta_schedule, noise_like +from ..distributions.distributions import DiagonalGaussianDistribution, normal_kl from ..ema import LitEma -from ..distributions.distributions import ( - normal_kl, - DiagonalGaussianDistribution, -) -from ..autoencoder import ( - VQModelInterface, - IdentityFirstStage, - AutoencoderKL, -) -from ..diffusionmodules.util import ( - make_beta_schedule, - extract_into_tensor, - noise_like, -) +from ..textual_inversion_manager import TextualInversionManager from .ddim import DDIMSampler - __conditioning_keys__ = { - 'concat': 'c_concat', - 'crossattn': 'c_crossattn', - 'adm': 'y', + "concat": "c_concat", + "crossattn": "c_crossattn", + "adm": "y", } @@ -74,14 +62,14 @@ class DDPM(pl.LightningModule): self, unet_config, timesteps=1000, - beta_schedule='linear', - loss_type='l2', + beta_schedule="linear", + loss_type="l2", ckpt_path=None, ignore_keys=[], load_only_unet=False, - monitor='val/loss', + monitor="val/loss", use_ema=True, - first_stage_key='image', + first_stage_key="image", image_size=256, channels=3, log_every_t=100, @@ -95,7 +83,7 @@ class DDPM(pl.LightningModule): v_posterior=0.0, # weight for choosing posterior variance as sigma = (1-v) * beta_tilde + v * beta l_simple_weight=1.0, conditioning_key=None, - parameterization='eps', # all assuming fixed variance schedules + parameterization="eps", # all assuming fixed variance schedules scheduler_config=None, use_positional_encodings=False, learn_logvar=False, @@ -103,12 +91,12 @@ class DDPM(pl.LightningModule): ): super().__init__() assert parameterization in [ - 'eps', - 'x0', + "eps", + "x0", ], 'currently only supporting "eps" and "x0"' self.parameterization = parameterization print( - f' | {self.__class__.__name__}: Running in {self.parameterization}-prediction mode' + f" | {self.__class__.__name__}: Running in {self.parameterization}-prediction mode" ) self.cond_stage_model = None self.clip_denoised = clip_denoised @@ -122,7 +110,7 @@ class DDPM(pl.LightningModule): self.use_ema = use_ema if self.use_ema: self.model_ema = LitEma(self.model) - print(f' | Keeping EMAs of {len(list(self.model_ema.buffers()))}.') + print(f" | Keeping EMAs of {len(list(self.model_ema.buffers()))}.") self.use_scheduler = scheduler_config is not None if self.use_scheduler: @@ -152,16 +140,14 @@ class DDPM(pl.LightningModule): self.loss_type = loss_type self.learn_logvar = learn_logvar - self.logvar = torch.full( - fill_value=logvar_init, size=(self.num_timesteps,) - ) + self.logvar = torch.full(fill_value=logvar_init, size=(self.num_timesteps,)) if self.learn_logvar: self.logvar = nn.Parameter(self.logvar, requires_grad=True) def register_schedule( self, given_betas=None, - beta_schedule='linear', + beta_schedule="linear", timesteps=1000, linear_start=1e-4, linear_end=2e-2, @@ -187,34 +173,30 @@ class DDPM(pl.LightningModule): self.linear_end = linear_end assert ( alphas_cumprod.shape[0] == self.num_timesteps - ), 'alphas have to be defined for each timestep' + ), "alphas have to be defined for each timestep" to_torch = partial(torch.tensor, dtype=torch.float32) - self.register_buffer('betas', to_torch(betas)) - self.register_buffer('alphas_cumprod', to_torch(alphas_cumprod)) - self.register_buffer( - 'alphas_cumprod_prev', to_torch(alphas_cumprod_prev) - ) + self.register_buffer("betas", to_torch(betas)) + self.register_buffer("alphas_cumprod", to_torch(alphas_cumprod)) + self.register_buffer("alphas_cumprod_prev", to_torch(alphas_cumprod_prev)) # calculations for diffusion q(x_t | x_{t-1}) and others + self.register_buffer("sqrt_alphas_cumprod", to_torch(np.sqrt(alphas_cumprod))) self.register_buffer( - 'sqrt_alphas_cumprod', to_torch(np.sqrt(alphas_cumprod)) - ) - self.register_buffer( - 'sqrt_one_minus_alphas_cumprod', + "sqrt_one_minus_alphas_cumprod", to_torch(np.sqrt(1.0 - alphas_cumprod)), ) self.register_buffer( - 'log_one_minus_alphas_cumprod', + "log_one_minus_alphas_cumprod", to_torch(np.log(1.0 - alphas_cumprod)), ) self.register_buffer( - 'sqrt_recip_alphas_cumprod', + "sqrt_recip_alphas_cumprod", to_torch(np.sqrt(1.0 / alphas_cumprod)), ) self.register_buffer( - 'sqrt_recipm1_alphas_cumprod', + "sqrt_recipm1_alphas_cumprod", to_torch(np.sqrt(1.0 / alphas_cumprod - 1)), ) @@ -223,47 +205,41 @@ class DDPM(pl.LightningModule): 1.0 - alphas_cumprod_prev ) / (1.0 - alphas_cumprod) + self.v_posterior * betas # above: equal to 1. / (1. / (1. - alpha_cumprod_tm1) + alpha_t / beta_t) - self.register_buffer( - 'posterior_variance', to_torch(posterior_variance) - ) + self.register_buffer("posterior_variance", to_torch(posterior_variance)) # below: log calculation clipped because the posterior variance is 0 at the beginning of the diffusion chain self.register_buffer( - 'posterior_log_variance_clipped', + "posterior_log_variance_clipped", to_torch(np.log(np.maximum(posterior_variance, 1e-20))), ) self.register_buffer( - 'posterior_mean_coef1', - to_torch( - betas * np.sqrt(alphas_cumprod_prev) / (1.0 - alphas_cumprod) - ), + "posterior_mean_coef1", + to_torch(betas * np.sqrt(alphas_cumprod_prev) / (1.0 - alphas_cumprod)), ) self.register_buffer( - 'posterior_mean_coef2', + "posterior_mean_coef2", to_torch( - (1.0 - alphas_cumprod_prev) - * np.sqrt(alphas) - / (1.0 - alphas_cumprod) + (1.0 - alphas_cumprod_prev) * np.sqrt(alphas) / (1.0 - alphas_cumprod) ), ) - if self.parameterization == 'eps': + if self.parameterization == "eps": lvlb_weights = self.betas**2 / ( 2 * self.posterior_variance * to_torch(alphas) * (1 - self.alphas_cumprod) ) - elif self.parameterization == 'x0': + elif self.parameterization == "x0": lvlb_weights = ( 0.5 * np.sqrt(torch.Tensor(alphas_cumprod)) / (2.0 * 1 - torch.Tensor(alphas_cumprod)) ) else: - raise NotImplementedError('mu not supported') + raise NotImplementedError("mu not supported") # TODO how to choose this term lvlb_weights[0] = lvlb_weights[1] - self.register_buffer('lvlb_weights', lvlb_weights, persistent=False) + self.register_buffer("lvlb_weights", lvlb_weights, persistent=False) assert not torch.isnan(self.lvlb_weights).all() @contextmanager @@ -272,24 +248,24 @@ class DDPM(pl.LightningModule): self.model_ema.store(self.model.parameters()) self.model_ema.copy_to(self.model) if context is not None: - print(f'{context}: Switched to EMA weights') + print(f"{context}: Switched to EMA weights") try: yield None finally: if self.use_ema: self.model_ema.restore(self.model.parameters()) if context is not None: - print(f'{context}: Restored training weights') + print(f"{context}: Restored training weights") def init_from_ckpt(self, path, ignore_keys=list(), only_model=False): - sd = torch.load(path, map_location='cpu') - if 'state_dict' in list(sd.keys()): - sd = sd['state_dict'] + sd = torch.load(path, map_location="cpu") + if "state_dict" in list(sd.keys()): + sd = sd["state_dict"] keys = list(sd.keys()) for k in keys: for ik in ignore_keys: if k.startswith(ik): - print('Deleting key {} from state_dict.'.format(k)) + print("Deleting key {} from state_dict.".format(k)) del sd[k] missing, unexpected = ( self.load_state_dict(sd, strict=False) @@ -297,12 +273,12 @@ class DDPM(pl.LightningModule): else self.model.load_state_dict(sd, strict=False) ) print( - f'Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys' + f"Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys" ) if len(missing) > 0: - print(f'Missing Keys: {missing}') + print(f"Missing Keys: {missing}") if len(unexpected) > 0: - print(f'Unexpected Keys: {unexpected}') + print(f"Unexpected Keys: {unexpected}") def q_mean_variance(self, x_start, t): """ @@ -311,13 +287,8 @@ class DDPM(pl.LightningModule): :param t: the number of diffusion steps (minus 1). Here, 0 means one step. :return: A tuple (mean, variance, log_variance), all of x_start's shape. """ - mean = ( - extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) - * x_start - ) - variance = extract_into_tensor( - 1.0 - self.alphas_cumprod, t, x_start.shape - ) + mean = extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start + variance = extract_into_tensor(1.0 - self.alphas_cumprod, t, x_start.shape) log_variance = extract_into_tensor( self.log_one_minus_alphas_cumprod, t, x_start.shape ) @@ -325,24 +296,17 @@ class DDPM(pl.LightningModule): def predict_start_from_noise(self, x_t, t, noise): return ( - extract_into_tensor(self.sqrt_recip_alphas_cumprod, t, x_t.shape) - * x_t - - extract_into_tensor( - self.sqrt_recipm1_alphas_cumprod, t, x_t.shape - ) + extract_into_tensor(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t + - extract_into_tensor(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape) * noise ) def q_posterior(self, x_start, x_t, t): posterior_mean = ( - extract_into_tensor(self.posterior_mean_coef1, t, x_t.shape) - * x_start - + extract_into_tensor(self.posterior_mean_coef2, t, x_t.shape) - * x_t - ) - posterior_variance = extract_into_tensor( - self.posterior_variance, t, x_t.shape + extract_into_tensor(self.posterior_mean_coef1, t, x_t.shape) * x_start + + extract_into_tensor(self.posterior_mean_coef2, t, x_t.shape) * x_t ) + posterior_variance = extract_into_tensor(self.posterior_variance, t, x_t.shape) posterior_log_variance_clipped = extract_into_tensor( self.posterior_log_variance_clipped, t, x_t.shape ) @@ -354,9 +318,9 @@ class DDPM(pl.LightningModule): def p_mean_variance(self, x, t, clip_denoised: bool): model_out = self.model(x, t) - if self.parameterization == 'eps': + if self.parameterization == "eps": x_recon = self.predict_start_from_noise(x, t=t, noise=model_out) - elif self.parameterization == 'x0': + elif self.parameterization == "x0": x_recon = model_out if clip_denoised: x_recon.clamp_(-1.0, 1.0) @@ -376,13 +340,8 @@ class DDPM(pl.LightningModule): ) noise = noise_like(x.shape, device, repeat_noise) # no noise when t == 0 - nonzero_mask = (1 - (t == 0).float()).reshape( - b, *((1,) * (len(x.shape) - 1)) - ) - return ( - model_mean - + nonzero_mask * (0.5 * model_log_variance).exp() * noise - ) + nonzero_mask = (1 - (t == 0).float()).reshape(b, *((1,) * (len(x.shape) - 1))) + return model_mean + nonzero_mask * (0.5 * model_log_variance).exp() * noise @torch.no_grad() def p_sample_loop(self, shape, return_intermediates=False): @@ -392,7 +351,7 @@ class DDPM(pl.LightningModule): intermediates = [img] for i in tqdm( reversed(range(0, self.num_timesteps)), - desc='Sampling t', + desc="Sampling t", total=self.num_timesteps, dynamic_ncols=True, ): @@ -419,26 +378,21 @@ class DDPM(pl.LightningModule): def q_sample(self, x_start, t, noise=None): noise = default(noise, lambda: torch.randn_like(x_start)) return ( - extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) - * x_start - + extract_into_tensor( - self.sqrt_one_minus_alphas_cumprod, t, x_start.shape - ) + extract_into_tensor(self.sqrt_alphas_cumprod, t, x_start.shape) * x_start + + extract_into_tensor(self.sqrt_one_minus_alphas_cumprod, t, x_start.shape) * noise ) def get_loss(self, pred, target, mean=True): - if self.loss_type == 'l1': + if self.loss_type == "l1": loss = (target - pred).abs() if mean: loss = loss.mean() - elif self.loss_type == 'l2': + elif self.loss_type == "l2": if mean: loss = torch.nn.functional.mse_loss(target, pred) else: - loss = torch.nn.functional.mse_loss( - target, pred, reduction='none' - ) + loss = torch.nn.functional.mse_loss(target, pred, reduction="none") else: raise NotImplementedError("unknown loss type '{loss_type}'") @@ -450,28 +404,28 @@ class DDPM(pl.LightningModule): model_out = self.model(x_noisy, t) loss_dict = {} - if self.parameterization == 'eps': + if self.parameterization == "eps": target = noise - elif self.parameterization == 'x0': + elif self.parameterization == "x0": target = x_start else: raise NotImplementedError( - f'Paramterization {self.parameterization} not yet supported' + f"Paramterization {self.parameterization} not yet supported" ) loss = self.get_loss(model_out, target, mean=False).mean(dim=[1, 2, 3]) - log_prefix = 'train' if self.training else 'val' + log_prefix = "train" if self.training else "val" - loss_dict.update({f'{log_prefix}/loss_simple': loss.mean()}) + loss_dict.update({f"{log_prefix}/loss_simple": loss.mean()}) loss_simple = loss.mean() * self.l_simple_weight loss_vlb = (self.lvlb_weights[t] * loss).mean() - loss_dict.update({f'{log_prefix}/loss_vlb': loss_vlb}) + loss_dict.update({f"{log_prefix}/loss_vlb": loss_vlb}) loss = loss_simple + self.original_elbo_weight * loss_vlb - loss_dict.update({f'{log_prefix}/loss': loss}) + loss_dict.update({f"{log_prefix}/loss": loss}) return loss, loss_dict @@ -487,7 +441,7 @@ class DDPM(pl.LightningModule): x = batch[k] if len(x.shape) == 3: x = x[..., None] - x = rearrange(x, 'b h w c -> b c h w') + x = rearrange(x, "b h w c -> b c h w") x = x.to(memory_format=torch.contiguous_format).float() return x @@ -504,7 +458,7 @@ class DDPM(pl.LightningModule): ) self.log( - 'global_step', + "global_step", self.global_step, prog_bar=True, logger=True, @@ -513,9 +467,9 @@ class DDPM(pl.LightningModule): ) if self.use_scheduler: - lr = self.optimizers().param_groups[0]['lr'] + lr = self.optimizers().param_groups[0]["lr"] self.log( - 'lr_abs', + "lr_abs", lr, prog_bar=True, logger=True, @@ -530,9 +484,7 @@ class DDPM(pl.LightningModule): _, loss_dict_no_ema = self.shared_step(batch) with self.ema_scope(): _, loss_dict_ema = self.shared_step(batch) - loss_dict_ema = { - key + '_ema': loss_dict_ema[key] for key in loss_dict_ema - } + loss_dict_ema = {key + "_ema": loss_dict_ema[key] for key in loss_dict_ema} self.log_dict( loss_dict_no_ema, prog_bar=False, @@ -554,21 +506,19 @@ class DDPM(pl.LightningModule): def _get_rows_from_list(self, samples): n_imgs_per_row = len(samples) - denoise_grid = rearrange(samples, 'n b c h w -> b n c h w') - denoise_grid = rearrange(denoise_grid, 'b n c h w -> (b n) c h w') + denoise_grid = rearrange(samples, "n b c h w -> b n c h w") + denoise_grid = rearrange(denoise_grid, "b n c h w -> (b n) c h w") denoise_grid = make_grid(denoise_grid, nrow=n_imgs_per_row) return denoise_grid @torch.no_grad() - def log_images( - self, batch, N=8, n_row=2, sample=True, return_keys=None, **kwargs - ): + def log_images(self, batch, N=8, n_row=2, sample=True, return_keys=None, **kwargs): log = dict() x = self.get_input(batch, self.first_stage_key) N = min(x.shape[0], N) n_row = min(x.shape[0], n_row) x = x.to(self.device)[:N] - log['inputs'] = x + log["inputs"] = x # get diffusion row diffusion_row = list() @@ -576,23 +526,23 @@ class DDPM(pl.LightningModule): for t in range(self.num_timesteps): if t % self.log_every_t == 0 or t == self.num_timesteps - 1: - t = repeat(torch.tensor([t]), '1 -> b', b=n_row) + t = repeat(torch.tensor([t]), "1 -> b", b=n_row) t = t.to(self.device).long() noise = torch.randn_like(x_start) x_noisy = self.q_sample(x_start=x_start, t=t, noise=noise) diffusion_row.append(x_noisy) - log['diffusion_row'] = self._get_rows_from_list(diffusion_row) + log["diffusion_row"] = self._get_rows_from_list(diffusion_row) if sample: # get denoise row - with self.ema_scope('Plotting'): + with self.ema_scope("Plotting"): samples, denoise_row = self.sample( batch_size=N, return_intermediates=True ) - log['samples'] = samples - log['denoise_row'] = self._get_rows_from_list(denoise_row) + log["samples"] = samples + log["denoise_row"] = self._get_rows_from_list(denoise_row) if return_keys: if np.intersect1d(list(log.keys()), return_keys).shape[0] == 0: @@ -619,7 +569,7 @@ class LatentDiffusion(DDPM): cond_stage_config, personalization_config, num_timesteps_cond=None, - cond_stage_key='image', + cond_stage_key="image", cond_stage_trainable=False, concat_mode=True, cond_stage_forward=None, @@ -629,32 +579,29 @@ class LatentDiffusion(DDPM): *args, **kwargs, ): - self.num_timesteps_cond = default(num_timesteps_cond, 1) self.scale_by_std = scale_by_std - assert self.num_timesteps_cond <= kwargs['timesteps'] + assert self.num_timesteps_cond <= kwargs["timesteps"] # for backwards compatibility after implementation of DiffusionWrapper if conditioning_key is None: - conditioning_key = 'concat' if concat_mode else 'crossattn' - if cond_stage_config == '__is_unconditional__': + conditioning_key = "concat" if concat_mode else "crossattn" + if cond_stage_config == "__is_unconditional__": conditioning_key = None - ckpt_path = kwargs.pop('ckpt_path', None) - ignore_keys = kwargs.pop('ignore_keys', []) + ckpt_path = kwargs.pop("ckpt_path", None) + ignore_keys = kwargs.pop("ignore_keys", []) super().__init__(conditioning_key=conditioning_key, *args, **kwargs) self.concat_mode = concat_mode self.cond_stage_trainable = cond_stage_trainable self.cond_stage_key = cond_stage_key try: - self.num_downs = ( - len(first_stage_config.params.ddconfig.ch_mult) - 1 - ) + self.num_downs = len(first_stage_config.params.ddconfig.ch_mult) - 1 except: self.num_downs = 0 if not scale_by_std: self.scale_factor = scale_factor else: - self.register_buffer('scale_factor', torch.tensor(scale_factor)) + self.register_buffer("scale_factor", torch.tensor(scale_factor)) self.instantiate_first_stage(first_stage_config) self.instantiate_cond_stage(cond_stage_config) @@ -680,12 +627,14 @@ class LatentDiffusion(DDPM): personalization_config, self.cond_stage_model ) self.textual_inversion_manager = TextualInversionManager( - tokenizer = self.cond_stage_model.tokenizer, - text_encoder = self.cond_stage_model.transformer, - full_precision = True + tokenizer=self.cond_stage_model.tokenizer, + text_encoder=self.cond_stage_model.transformer, + full_precision=True, ) # this circular component dependency is gross and bad, needs to be rethought - self.cond_stage_model.set_textual_inversion_manager(self.textual_inversion_manager) + self.cond_stage_model.set_textual_inversion_manager( + self.textual_inversion_manager + ) self.emb_ckpt_counter = 0 @@ -721,22 +670,22 @@ class LatentDiffusion(DDPM): ): assert ( self.scale_factor == 1.0 - ), 'rather not use custom rescaling and std-rescaling simultaneously' + ), "rather not use custom rescaling and std-rescaling simultaneously" # set rescale weight to 1./std of encodings - print('### USING STD-RESCALING ###') + print("### USING STD-RESCALING ###") x = super().get_input(batch, self.first_stage_key) x = x.to(self.device) encoder_posterior = self.encode_first_stage(x) z = self.get_first_stage_encoding(encoder_posterior).detach() del self.scale_factor - self.register_buffer('scale_factor', 1.0 / z.flatten().std()) - print(f'setting self.scale_factor to {self.scale_factor}') - print('### USING STD-RESCALING ###') + self.register_buffer("scale_factor", 1.0 / z.flatten().std()) + print(f"setting self.scale_factor to {self.scale_factor}") + print("### USING STD-RESCALING ###") def register_schedule( self, given_betas=None, - beta_schedule='linear', + beta_schedule="linear", timesteps=1000, linear_start=1e-4, linear_end=2e-2, @@ -764,13 +713,11 @@ class LatentDiffusion(DDPM): def instantiate_cond_stage(self, config): if not self.cond_stage_trainable: - if config == '__is_first_stage__': - print('Using first stage also as cond stage.') + if config == "__is_first_stage__": + print("Using first stage also as cond stage.") self.cond_stage_model = self.first_stage_model - elif config == '__is_unconditional__': - print( - f'Training {self.__class__.__name__} as an unconditional model.' - ) + elif config == "__is_unconditional__": + print(f"Training {self.__class__.__name__} as an unconditional model.") self.cond_stage_model = None # self.be_unconditional = True else: @@ -780,8 +727,8 @@ class LatentDiffusion(DDPM): for param in self.cond_stage_model.parameters(): param.requires_grad = False else: - assert config != '__is_first_stage__' - assert config != '__is_unconditional__' + assert config != "__is_first_stage__" + assert config != "__is_unconditional__" try: model = instantiate_from_config(config) except urllib.error.URLError: @@ -794,14 +741,14 @@ class LatentDiffusion(DDPM): model = instantiate_from_config(config, embedder=embedder) if config.params.get( - 'embedding_manager_ckpt', None - ): # do not load if missing OR empty string + "embedding_manager_ckpt", None + ): # do not load if missing OR empty string model.load(config.params.embedding_manager_ckpt) return model def _get_denoise_row_from_list( - self, samples, desc='', force_no_decoder_quantization=False + self, samples, desc="", force_no_decoder_quantization=False ): denoise_row = [] for zd in tqdm(samples, desc=desc): @@ -813,8 +760,8 @@ class LatentDiffusion(DDPM): ) n_imgs_per_row = len(denoise_row) denoise_row = torch.stack(denoise_row) # n_log_step, n_row, C, H, W - denoise_grid = rearrange(denoise_row, 'n b c h w -> b n c h w') - denoise_grid = rearrange(denoise_grid, 'b n c h w -> (b n) c h w') + denoise_grid = rearrange(denoise_row, "n b c h w -> b n c h w") + denoise_grid = rearrange(denoise_grid, "b n c h w -> (b n) c h w") denoise_grid = make_grid(denoise_grid, nrow=n_imgs_per_row) return denoise_grid @@ -831,11 +778,11 @@ class LatentDiffusion(DDPM): def get_learned_conditioning(self, c, **kwargs): if self.cond_stage_forward is None: - if hasattr(self.cond_stage_model, 'encode') and callable( + if hasattr(self.cond_stage_model, "encode") and callable( self.cond_stage_model.encode ): c = self.cond_stage_model.encode( - c, embedding_manager=self.embedding_manager,**kwargs + c, embedding_manager=self.embedding_manager, **kwargs ) if isinstance(c, DiagonalGaussianDistribution): c = c.mode() @@ -873,19 +820,17 @@ class LatentDiffusion(DDPM): weighting = self.delta_border(h, w) weighting = torch.clip( weighting, - self.split_input_params['clip_min_weight'], - self.split_input_params['clip_max_weight'], - ) - weighting = ( - weighting.view(1, h * w, 1).repeat(1, 1, Ly * Lx).to(device) + self.split_input_params["clip_min_weight"], + self.split_input_params["clip_max_weight"], ) + weighting = weighting.view(1, h * w, 1).repeat(1, 1, Ly * Lx).to(device) - if self.split_input_params['tie_braker']: + if self.split_input_params["tie_braker"]: L_weighting = self.delta_border(Ly, Lx) L_weighting = torch.clip( L_weighting, - self.split_input_params['clip_min_tie_weight'], - self.split_input_params['clip_max_tie_weight'], + self.split_input_params["clip_min_tie_weight"], + self.split_input_params["clip_max_tie_weight"], ) L_weighting = L_weighting.view(1, 1, Ly * Lx).to(device) @@ -916,12 +861,8 @@ class LatentDiffusion(DDPM): weighting = self.get_weighting( kernel_size[0], kernel_size[1], Ly, Lx, x.device ).to(x.dtype) - normalization = fold(weighting).view( - 1, 1, h, w - ) # normalizes the overlap - weighting = weighting.view( - (1, 1, kernel_size[0], kernel_size[1], Ly * Lx) - ) + normalization = fold(weighting).view(1, 1, h, w) # normalizes the overlap + weighting = weighting.view((1, 1, kernel_size[0], kernel_size[1], Ly * Lx)) elif uf > 1 and df == 1: fold_params = dict( @@ -1003,9 +944,9 @@ class LatentDiffusion(DDPM): if cond_key is None: cond_key = self.cond_stage_key if cond_key != self.first_stage_key: - if cond_key in ['caption', 'coordinates_bbox']: + if cond_key in ["caption", "coordinates_bbox"]: xc = batch[cond_key] - elif cond_key == 'class_label': + elif cond_key == "class_label": xc = batch else: xc = super().get_input(batch, cond_key).to(self.device) @@ -1025,14 +966,14 @@ class LatentDiffusion(DDPM): if self.use_positional_encodings: pos_x, pos_y = self.compute_latent_shifts(batch) ckey = __conditioning_keys__[self.model.conditioning_key] - c = {ckey: c, 'pos_x': pos_x, 'pos_y': pos_y} + c = {ckey: c, "pos_x": pos_x, "pos_y": pos_y} else: c = None xc = None if self.use_positional_encodings: pos_x, pos_y = self.compute_latent_shifts(batch) - c = {'pos_x': pos_x, 'pos_y': pos_y} + c = {"pos_x": pos_x, "pos_y": pos_y} out = [z, c] if return_first_stage_outputs: xrec = self.decode_first_stage(z) @@ -1042,32 +983,28 @@ class LatentDiffusion(DDPM): return out @torch.no_grad() - def decode_first_stage( - self, z, predict_cids=False, force_not_quantize=False - ): + def decode_first_stage(self, z, predict_cids=False, force_not_quantize=False): if predict_cids: if z.dim() == 4: z = torch.argmax(z.exp(), dim=1).long() - z = self.first_stage_model.quantize.get_codebook_entry( - z, shape=None - ) - z = rearrange(z, 'b h w c -> b c h w').contiguous() + z = self.first_stage_model.quantize.get_codebook_entry(z, shape=None) + z = rearrange(z, "b h w c -> b c h w").contiguous() z = 1.0 / self.scale_factor * z - if hasattr(self, 'split_input_params'): - if self.split_input_params['patch_distributed_vq']: - ks = self.split_input_params['ks'] # eg. (128, 128) - stride = self.split_input_params['stride'] # eg. (64, 64) - uf = self.split_input_params['vqf'] + if hasattr(self, "split_input_params"): + if self.split_input_params["patch_distributed_vq"]: + ks = self.split_input_params["ks"] # eg. (128, 128) + stride = self.split_input_params["stride"] # eg. (64, 64) + uf = self.split_input_params["vqf"] bs, nc, h, w = z.shape if ks[0] > h or ks[1] > w: ks = (min(ks[0], h), min(ks[1], w)) - print('reducing Kernel') + print("reducing Kernel") if stride[0] > h or stride[1] > w: stride = (min(stride[0], h), min(stride[1], w)) - print('reducing stride') + print("reducing stride") fold, unfold, normalization, weighting = self.get_fold_unfold( z, ks, stride, uf=uf @@ -1084,26 +1021,20 @@ class LatentDiffusion(DDPM): output_list = [ self.first_stage_model.decode( z[:, :, :, :, i], - force_not_quantize=predict_cids - or force_not_quantize, + force_not_quantize=predict_cids or force_not_quantize, ) for i in range(z.shape[-1]) ] else: - output_list = [ self.first_stage_model.decode(z[:, :, :, :, i]) for i in range(z.shape[-1]) ] - o = torch.stack( - output_list, axis=-1 - ) # # (bn, nc, ks[0], ks[1], L) + o = torch.stack(output_list, axis=-1) # # (bn, nc, ks[0], ks[1], L) o = o * weighting # Reverse 1. reshape to img shape - o = o.view( - (o.shape[0], -1, o.shape[-1]) - ) # (bn, nc * ks[0] * ks[1], L) + o = o.view((o.shape[0], -1, o.shape[-1])) # (bn, nc * ks[0] * ks[1], L) # stitch crops together decoded = fold(o) decoded = decoded / normalization # norm is shape (1, 1, h, w) @@ -1132,26 +1063,24 @@ class LatentDiffusion(DDPM): if predict_cids: if z.dim() == 4: z = torch.argmax(z.exp(), dim=1).long() - z = self.first_stage_model.quantize.get_codebook_entry( - z, shape=None - ) - z = rearrange(z, 'b h w c -> b c h w').contiguous() + z = self.first_stage_model.quantize.get_codebook_entry(z, shape=None) + z = rearrange(z, "b h w c -> b c h w").contiguous() z = 1.0 / self.scale_factor * z - if hasattr(self, 'split_input_params'): - if self.split_input_params['patch_distributed_vq']: - ks = self.split_input_params['ks'] # eg. (128, 128) - stride = self.split_input_params['stride'] # eg. (64, 64) - uf = self.split_input_params['vqf'] + if hasattr(self, "split_input_params"): + if self.split_input_params["patch_distributed_vq"]: + ks = self.split_input_params["ks"] # eg. (128, 128) + stride = self.split_input_params["stride"] # eg. (64, 64) + uf = self.split_input_params["vqf"] bs, nc, h, w = z.shape if ks[0] > h or ks[1] > w: ks = (min(ks[0], h), min(ks[1], w)) - print('reducing Kernel') + print("reducing Kernel") if stride[0] > h or stride[1] > w: stride = (min(stride[0], h), min(stride[1], w)) - print('reducing stride') + print("reducing stride") fold, unfold, normalization, weighting = self.get_fold_unfold( z, ks, stride, uf=uf @@ -1168,26 +1097,20 @@ class LatentDiffusion(DDPM): output_list = [ self.first_stage_model.decode( z[:, :, :, :, i], - force_not_quantize=predict_cids - or force_not_quantize, + force_not_quantize=predict_cids or force_not_quantize, ) for i in range(z.shape[-1]) ] else: - output_list = [ self.first_stage_model.decode(z[:, :, :, :, i]) for i in range(z.shape[-1]) ] - o = torch.stack( - output_list, axis=-1 - ) # # (bn, nc, ks[0], ks[1], L) + o = torch.stack(output_list, axis=-1) # # (bn, nc, ks[0], ks[1], L) o = o * weighting # Reverse 1. reshape to img shape - o = o.view( - (o.shape[0], -1, o.shape[-1]) - ) # (bn, nc * ks[0] * ks[1], L) + o = o.view((o.shape[0], -1, o.shape[-1])) # (bn, nc * ks[0] * ks[1], L) # stitch crops together decoded = fold(o) decoded = decoded / normalization # norm is shape (1, 1, h, w) @@ -1211,20 +1134,20 @@ class LatentDiffusion(DDPM): @torch.no_grad() def encode_first_stage(self, x): - if hasattr(self, 'split_input_params'): - if self.split_input_params['patch_distributed_vq']: - ks = self.split_input_params['ks'] # eg. (128, 128) - stride = self.split_input_params['stride'] # eg. (64, 64) - df = self.split_input_params['vqf'] - self.split_input_params['original_image_size'] = x.shape[-2:] + if hasattr(self, "split_input_params"): + if self.split_input_params["patch_distributed_vq"]: + ks = self.split_input_params["ks"] # eg. (128, 128) + stride = self.split_input_params["stride"] # eg. (64, 64) + df = self.split_input_params["vqf"] + self.split_input_params["original_image_size"] = x.shape[-2:] bs, nc, h, w = x.shape if ks[0] > h or ks[1] > w: ks = (min(ks[0], h), min(ks[1], w)) - print('reducing Kernel') + print("reducing Kernel") if stride[0] > h or stride[1] > w: stride = (min(stride[0], h), min(stride[1], w)) - print('reducing stride') + print("reducing stride") fold, unfold, normalization, weighting = self.get_fold_unfold( x, ks, stride, df=df @@ -1244,9 +1167,7 @@ class LatentDiffusion(DDPM): o = o * weighting # Reverse reshape to img shape - o = o.view( - (o.shape[0], -1, o.shape[-1]) - ) # (bn, nc * ks[0] * ks[1], L) + o = o.view((o.shape[0], -1, o.shape[-1])) # (bn, nc * ks[0] * ks[1], L) # stitch crops together decoded = fold(o) decoded = decoded / normalization @@ -1272,15 +1193,11 @@ class LatentDiffusion(DDPM): c = self.get_learned_conditioning(c) if self.shorten_cond_schedule: # TODO: drop this option tc = self.cond_ids[t].to(self.device) - c = self.q_sample( - x_start=c, t=tc, noise=torch.randn_like(c.float()) - ) + c = self.q_sample(x_start=c, t=tc, noise=torch.randn_like(c.float())) return self.p_losses(x, c, t, *args, **kwargs) - def _rescale_annotations( - self, bboxes, crop_coordinates - ): # TODO: move to dataset + def _rescale_annotations(self, bboxes, crop_coordinates): # TODO: move to dataset def rescale_bbox(bbox): x0 = clamp((bbox[0] - crop_coordinates[0]) / crop_coordinates[2]) y0 = clamp((bbox[1] - crop_coordinates[1]) / crop_coordinates[3]) @@ -1291,7 +1208,6 @@ class LatentDiffusion(DDPM): return [rescale_bbox(b) for b in bboxes] def apply_model(self, x_noisy, t, cond, return_ids=False): - if isinstance(cond, dict): # hybrid case, cond is exptected to be a dict pass @@ -1299,19 +1215,15 @@ class LatentDiffusion(DDPM): if not isinstance(cond, list): cond = [cond] key = ( - 'c_concat' - if self.model.conditioning_key == 'concat' - else 'c_crossattn' + "c_concat" if self.model.conditioning_key == "concat" else "c_crossattn" ) cond = {key: cond} - if hasattr(self, 'split_input_params'): - assert ( - len(cond) == 1 - ) # todo can only deal with one conditioning atm + if hasattr(self, "split_input_params"): + assert len(cond) == 1 # todo can only deal with one conditioning atm assert not return_ids - ks = self.split_input_params['ks'] # eg. (128, 128) - stride = self.split_input_params['stride'] # eg. (64, 64) + ks = self.split_input_params["ks"] # eg. (128, 128) + stride = self.split_input_params["stride"] # eg. (64, 64) h, w = x_noisy.shape[-2:] @@ -1327,15 +1239,12 @@ class LatentDiffusion(DDPM): z_list = [z[:, :, :, :, i] for i in range(z.shape[-1])] if ( - self.cond_stage_key - in ['image', 'LR_image', 'segmentation', 'bbox_img'] + self.cond_stage_key in ["image", "LR_image", "segmentation", "bbox_img"] and self.model.conditioning_key ): # todo check for completeness c_key = next(iter(cond.keys())) # get key c = next(iter(cond.values())) # get value - assert ( - len(c) == 1 - ) # todo extend to list with more than one elem + assert len(c) == 1 # todo extend to list with more than one elem c = c[0] # get element c = unfold(c) @@ -1343,20 +1252,16 @@ class LatentDiffusion(DDPM): (c.shape[0], -1, ks[0], ks[1], c.shape[-1]) ) # (bn, nc, ks[0], ks[1], L ) - cond_list = [ - {c_key: [c[:, :, :, :, i]]} for i in range(c.shape[-1]) - ] + cond_list = [{c_key: [c[:, :, :, :, i]]} for i in range(c.shape[-1])] - elif self.cond_stage_key == 'coordinates_bbox': + elif self.cond_stage_key == "coordinates_bbox": assert ( - 'original_image_size' in self.split_input_params - ), 'BoudingBoxRescaling is missing original_image_size' + "original_image_size" in self.split_input_params + ), "BoudingBoxRescaling is missing original_image_size" # assuming padding of unfold is always 0 and its dilation is always 1 n_patches_per_row = int((w - ks[0]) / stride[0] + 1) - full_img_h, full_img_w = self.split_input_params[ - 'original_image_size' - ] + full_img_h, full_img_w = self.split_input_params["original_image_size"] # as we are operating on latents, we need the factor from the original image size to the # spatial latent size to properly rescale the crops for regenerating the bbox annotations num_downs = self.first_stage_model.encoder.num_resolutions - 1 @@ -1392,35 +1297,30 @@ class LatentDiffusion(DDPM): # tokenize crop coordinates for the bounding boxes of the respective patches patch_limits_tknzd = [ - torch.LongTensor(self.bbox_tokenizer._crop_encoder(bbox))[ - None - ].to(self.device) + torch.LongTensor(self.bbox_tokenizer._crop_encoder(bbox))[None].to( + self.device + ) for bbox in patch_limits ] # list of length l with tensors of shape (1, 2) print(patch_limits_tknzd[0].shape) # cut tknzd crop position from conditioning - assert isinstance( - cond, dict - ), 'cond must be dict to be fed into model' - cut_cond = cond['c_crossattn'][0][..., :-2].to(self.device) + assert isinstance(cond, dict), "cond must be dict to be fed into model" + cut_cond = cond["c_crossattn"][0][..., :-2].to(self.device) print(cut_cond.shape) adapted_cond = torch.stack( - [ - torch.cat([cut_cond, p], dim=1) - for p in patch_limits_tknzd - ] + [torch.cat([cut_cond, p], dim=1) for p in patch_limits_tknzd] ) - adapted_cond = rearrange(adapted_cond, 'l b n -> (l b) n') + adapted_cond = rearrange(adapted_cond, "l b n -> (l b) n") print(adapted_cond.shape) adapted_cond = self.get_learned_conditioning(adapted_cond) print(adapted_cond.shape) adapted_cond = rearrange( - adapted_cond, '(l b) n d -> l b n d', l=z.shape[-1] + adapted_cond, "(l b) n d -> l b n d", l=z.shape[-1] ) print(adapted_cond.shape) - cond_list = [{'c_crossattn': [e]} for e in adapted_cond] + cond_list = [{"c_crossattn": [e]} for e in adapted_cond] else: cond_list = [ @@ -1429,8 +1329,7 @@ class LatentDiffusion(DDPM): # apply model by loop over crops output_list = [ - self.model(z_list[i], t, **cond_list[i]) - for i in range(z.shape[-1]) + self.model(z_list[i], t, **cond_list[i]) for i in range(z.shape[-1]) ] assert not isinstance( output_list[0], tuple @@ -1439,9 +1338,7 @@ class LatentDiffusion(DDPM): o = torch.stack(output_list, axis=-1) o = o * weighting # Reverse reshape to img shape - o = o.view( - (o.shape[0], -1, o.shape[-1]) - ) # (bn, nc * ks[0] * ks[1], L) + o = o.view((o.shape[0], -1, o.shape[-1])) # (bn, nc * ks[0] * ks[1], L) # stitch crops together x_recon = fold(o) / normalization @@ -1455,8 +1352,7 @@ class LatentDiffusion(DDPM): def _predict_eps_from_xstart(self, x_t, t, pred_xstart): return ( - extract_into_tensor(self.sqrt_recip_alphas_cumprod, t, x_t.shape) - * x_t + extract_into_tensor(self.sqrt_recip_alphas_cumprod, t, x_t.shape) * x_t - pred_xstart ) / extract_into_tensor(self.sqrt_recipm1_alphas_cumprod, t, x_t.shape) @@ -1469,9 +1365,7 @@ class LatentDiffusion(DDPM): :return: a batch of [N] KL values (in bits), one per batch element. """ batch_size = x_start.shape[0] - t = torch.tensor( - [self.num_timesteps - 1] * batch_size, device=x_start.device - ) + t = torch.tensor([self.num_timesteps - 1] * batch_size, device=x_start.device) qt_mean, _, qt_log_variance = self.q_mean_variance(x_start, t) kl_prior = normal_kl( mean1=qt_mean, logvar1=qt_log_variance, mean2=0.0, logvar2=0.0 @@ -1484,46 +1378,42 @@ class LatentDiffusion(DDPM): model_output = self.apply_model(x_noisy, t, cond) loss_dict = {} - prefix = 'train' if self.training else 'val' + prefix = "train" if self.training else "val" - if self.parameterization == 'x0': + if self.parameterization == "x0": target = x_start - elif self.parameterization == 'eps': + elif self.parameterization == "eps": target = noise else: raise NotImplementedError() - loss_simple = self.get_loss(model_output, target, mean=False).mean( - [1, 2, 3] - ) - loss_dict.update({f'{prefix}/loss_simple': loss_simple.mean()}) + loss_simple = self.get_loss(model_output, target, mean=False).mean([1, 2, 3]) + loss_dict.update({f"{prefix}/loss_simple": loss_simple.mean()}) logvar_t = self.logvar[t.item()].to(self.device) loss = loss_simple / torch.exp(logvar_t) + logvar_t # loss = loss_simple / torch.exp(self.logvar) + self.logvar if self.learn_logvar: - loss_dict.update({f'{prefix}/loss_gamma': loss.mean()}) - loss_dict.update({'logvar': self.logvar.data.mean()}) + loss_dict.update({f"{prefix}/loss_gamma": loss.mean()}) + loss_dict.update({"logvar": self.logvar.data.mean()}) loss = self.l_simple_weight * loss.mean() - loss_vlb = self.get_loss(model_output, target, mean=False).mean( - dim=(1, 2, 3) - ) + loss_vlb = self.get_loss(model_output, target, mean=False).mean(dim=(1, 2, 3)) loss_vlb = (self.lvlb_weights[t] * loss_vlb).mean() - loss_dict.update({f'{prefix}/loss_vlb': loss_vlb}) + loss_dict.update({f"{prefix}/loss_vlb": loss_vlb}) loss += self.original_elbo_weight * loss_vlb - loss_dict.update({f'{prefix}/loss': loss}) + loss_dict.update({f"{prefix}/loss": loss}) if self.embedding_reg_weight > 0: loss_embedding_reg = ( self.embedding_manager.embedding_to_coarse_loss().mean() ) - loss_dict.update({f'{prefix}/loss_emb_reg': loss_embedding_reg}) + loss_dict.update({f"{prefix}/loss_emb_reg": loss_embedding_reg}) loss += self.embedding_reg_weight * loss_embedding_reg - loss_dict.update({f'{prefix}/loss': loss}) + loss_dict.update({f"{prefix}/loss": loss}) return loss, loss_dict @@ -1540,12 +1430,10 @@ class LatentDiffusion(DDPM): corrector_kwargs=None, ): t_in = t - model_out = self.apply_model( - x, t_in, c, return_ids=return_codebook_ids - ) + model_out = self.apply_model(x, t_in, c, return_ids=return_codebook_ids) if score_corrector is not None: - assert self.parameterization == 'eps' + assert self.parameterization == "eps" model_out = score_corrector.modify_score( self, model_out, x, t, c, **corrector_kwargs ) @@ -1553,9 +1441,9 @@ class LatentDiffusion(DDPM): if return_codebook_ids: model_out, logits = model_out - if self.parameterization == 'eps': + if self.parameterization == "eps": x_recon = self.predict_start_from_noise(x, t=t, noise=model_out) - elif self.parameterization == 'x0': + elif self.parameterization == "x0": x_recon = model_out else: raise NotImplementedError() @@ -1563,9 +1451,7 @@ class LatentDiffusion(DDPM): if clip_denoised: x_recon.clamp_(-1.0, 1.0) if quantize_denoised: - x_recon, _, [_, _, indices] = self.first_stage_model.quantize( - x_recon - ) + x_recon, _, [_, _, indices] = self.first_stage_model.quantize(x_recon) ( model_mean, posterior_variance, @@ -1617,7 +1503,7 @@ class LatentDiffusion(DDPM): corrector_kwargs=corrector_kwargs, ) if return_codebook_ids: - raise DeprecationWarning('Support dropped.') + raise DeprecationWarning("Support dropped.") model_mean, _, model_log_variance, logits = outputs elif return_x0: model_mean, _, model_log_variance, x0 = outputs @@ -1628,9 +1514,7 @@ class LatentDiffusion(DDPM): if noise_dropout > 0.0: noise = torch.nn.functional.dropout(noise, p=noise_dropout) # no noise when t == 0 - nonzero_mask = (1 - (t == 0).float()).reshape( - b, *((1,) * (len(x.shape) - 1)) - ) + nonzero_mask = (1 - (t == 0).float()).reshape(b, *((1,) * (len(x.shape) - 1))) if return_codebook_ids: return model_mean + nonzero_mask * ( @@ -1638,15 +1522,11 @@ class LatentDiffusion(DDPM): ).exp() * noise, logits.argmax(dim=1) if return_x0: return ( - model_mean - + nonzero_mask * (0.5 * model_log_variance).exp() * noise, + model_mean + nonzero_mask * (0.5 * model_log_variance).exp() * noise, x0, ) else: - return ( - model_mean - + nonzero_mask * (0.5 * model_log_variance).exp() * noise - ) + return model_mean + nonzero_mask * (0.5 * model_log_variance).exp() * noise @torch.no_grad() def progressive_denoising( @@ -1701,7 +1581,7 @@ class LatentDiffusion(DDPM): iterator = ( tqdm( reversed(range(0, timesteps)), - desc='Progressive Generation', + desc="Progressive Generation", total=timesteps, ) if verbose @@ -1713,11 +1593,9 @@ class LatentDiffusion(DDPM): for i in iterator: ts = torch.full((b,), i, device=self.device, dtype=torch.long) if self.shorten_cond_schedule: - assert self.model.conditioning_key != 'hybrid' + assert self.model.conditioning_key != "hybrid" tc = self.cond_ids[ts].to(cond.device) - cond = self.q_sample( - x_start=cond, t=tc, noise=torch.randn_like(cond) - ) + cond = self.q_sample(x_start=cond, t=tc, noise=torch.randn_like(cond)) img, x0_partial = self.p_sample( img, @@ -1761,7 +1639,6 @@ class LatentDiffusion(DDPM): start_T=None, log_every_t=None, ): - if not log_every_t: log_every_t = self.log_every_t device = self.betas.device @@ -1780,7 +1657,7 @@ class LatentDiffusion(DDPM): iterator = ( tqdm( reversed(range(0, timesteps)), - desc='Sampling t', + desc="Sampling t", total=timesteps, ) if verbose @@ -1789,18 +1666,14 @@ class LatentDiffusion(DDPM): if mask is not None: assert x0 is not None - assert ( - x0.shape[2:3] == mask.shape[2:3] - ) # spatial size has to match + assert x0.shape[2:3] == mask.shape[2:3] # spatial size has to match for i in iterator: ts = torch.full((b,), i, device=device, dtype=torch.long) if self.shorten_cond_schedule: - assert self.model.conditioning_key != 'hybrid' + assert self.model.conditioning_key != "hybrid" tc = self.cond_ids[ts].to(cond.device) - cond = self.q_sample( - x_start=cond, t=tc, noise=torch.randn_like(cond) - ) + cond = self.q_sample(x_start=cond, t=tc, noise=torch.randn_like(cond)) img = self.p_sample( img, @@ -1874,7 +1747,6 @@ class LatentDiffusion(DDPM): @torch.no_grad() def sample_log(self, cond, batch_size, ddim, ddim_steps, **kwargs): - if ddim: ddim_sampler = DDIMSampler(self) shape = (self.channels, self.image_size, self.image_size) @@ -1927,7 +1799,6 @@ class LatentDiffusion(DDPM): plot_diffusion_rows=False, **kwargs, ): - use_ddim = ddim_steps is not None log = dict() @@ -1941,24 +1812,22 @@ class LatentDiffusion(DDPM): ) N = min(x.shape[0], N) n_row = min(x.shape[0], n_row) - log['inputs'] = x - log['reconstruction'] = xrec + log["inputs"] = x + log["reconstruction"] = xrec if self.model.conditioning_key is not None: - if hasattr(self.cond_stage_model, 'decode'): + if hasattr(self.cond_stage_model, "decode"): xc = self.cond_stage_model.decode(c) - log['conditioning'] = xc - elif self.cond_stage_key in ['caption']: - xc = log_txt_as_img((x.shape[2], x.shape[3]), batch['caption']) - log['conditioning'] = xc - elif self.cond_stage_key == 'class_label': - xc = log_txt_as_img( - (x.shape[2], x.shape[3]), batch['human_label'] - ) - log['conditioning'] = xc + log["conditioning"] = xc + elif self.cond_stage_key in ["caption"]: + xc = log_txt_as_img((x.shape[2], x.shape[3]), batch["caption"]) + log["conditioning"] = xc + elif self.cond_stage_key == "class_label": + xc = log_txt_as_img((x.shape[2], x.shape[3]), batch["human_label"]) + log["conditioning"] = xc elif isimage(xc): - log['conditioning'] = xc + log["conditioning"] = xc if ismap(xc): - log['original_conditioning'] = self.to_rgb(xc) + log["original_conditioning"] = self.to_rgb(xc) if plot_diffusion_rows: # get diffusion row @@ -1966,27 +1835,21 @@ class LatentDiffusion(DDPM): z_start = z[:n_row] for t in range(self.num_timesteps): if t % self.log_every_t == 0 or t == self.num_timesteps - 1: - t = repeat(torch.tensor([t]), '1 -> b', b=n_row) + t = repeat(torch.tensor([t]), "1 -> b", b=n_row) t = t.to(self.device).long() noise = torch.randn_like(z_start) z_noisy = self.q_sample(x_start=z_start, t=t, noise=noise) diffusion_row.append(self.decode_first_stage(z_noisy)) - diffusion_row = torch.stack( - diffusion_row - ) # n_log_step, n_row, C, H, W - diffusion_grid = rearrange(diffusion_row, 'n b c h w -> b n c h w') - diffusion_grid = rearrange( - diffusion_grid, 'b n c h w -> (b n) c h w' - ) - diffusion_grid = make_grid( - diffusion_grid, nrow=diffusion_row.shape[0] - ) - log['diffusion_row'] = diffusion_grid + diffusion_row = torch.stack(diffusion_row) # n_log_step, n_row, C, H, W + diffusion_grid = rearrange(diffusion_row, "n b c h w -> b n c h w") + diffusion_grid = rearrange(diffusion_grid, "b n c h w -> (b n) c h w") + diffusion_grid = make_grid(diffusion_grid, nrow=diffusion_row.shape[0]) + log["diffusion_row"] = diffusion_grid if sample: # get denoise row - with self.ema_scope('Plotting'): + with self.ema_scope("Plotting"): samples, z_denoise_row = self.sample_log( cond=c, batch_size=N, @@ -1996,12 +1859,12 @@ class LatentDiffusion(DDPM): ) # samples, z_denoise_row = self.sample(cond=c, batch_size=N, return_intermediates=True) x_samples = self.decode_first_stage(samples) - log['samples'] = x_samples + log["samples"] = x_samples if plot_denoise_rows: denoise_grid = self._get_denoise_row_from_list(z_denoise_row) - log['denoise_row'] = denoise_grid + log["denoise_row"] = denoise_grid - uc = self.get_learned_conditioning(len(c) * ['']) + uc = self.get_learned_conditioning(len(c) * [""]) sample_scaled, _ = self.sample_log( cond=c, batch_size=N, @@ -2011,7 +1874,7 @@ class LatentDiffusion(DDPM): unconditional_guidance_scale=5.0, unconditional_conditioning=uc, ) - log['samples_scaled'] = self.decode_first_stage(sample_scaled) + log["samples_scaled"] = self.decode_first_stage(sample_scaled) if ( quantize_denoised @@ -2019,7 +1882,7 @@ class LatentDiffusion(DDPM): and not isinstance(self.first_stage_model, IdentityFirstStage) ): # also display when quantizing x0 while sampling - with self.ema_scope('Plotting Quantized Denoised'): + with self.ema_scope("Plotting Quantized Denoised"): samples, z_denoise_row = self.sample_log( cond=c, batch_size=N, @@ -2031,7 +1894,7 @@ class LatentDiffusion(DDPM): # samples, z_denoise_row = self.sample(cond=c, batch_size=N, return_intermediates=True, # quantize_denoised=True) x_samples = self.decode_first_stage(samples.to(self.device)) - log['samples_x0_quantized'] = x_samples + log["samples_x0_quantized"] = x_samples if inpaint: # make a simple center square @@ -2040,8 +1903,7 @@ class LatentDiffusion(DDPM): # zeros will be filled in mask[:, h // 4 : 3 * h // 4, w // 4 : 3 * w // 4] = 0.0 mask = mask[:, None, ...] - with self.ema_scope('Plotting Inpaint'): - + with self.ema_scope("Plotting Inpaint"): samples, _ = self.sample_log( cond=c, batch_size=N, @@ -2052,11 +1914,11 @@ class LatentDiffusion(DDPM): mask=mask, ) x_samples = self.decode_first_stage(samples.to(self.device)) - log['samples_inpainting'] = x_samples - log['mask'] = mask + log["samples_inpainting"] = x_samples + log["mask"] = mask # outpaint - with self.ema_scope('Plotting Outpaint'): + with self.ema_scope("Plotting Outpaint"): samples, _ = self.sample_log( cond=c, batch_size=N, @@ -2067,19 +1929,19 @@ class LatentDiffusion(DDPM): mask=mask, ) x_samples = self.decode_first_stage(samples.to(self.device)) - log['samples_outpainting'] = x_samples + log["samples_outpainting"] = x_samples if plot_progressive_rows: - with self.ema_scope('Plotting Progressives'): + with self.ema_scope("Plotting Progressives"): img, progressives = self.progressive_denoising( c, shape=(self.channels, self.image_size, self.image_size), batch_size=N, ) prog_row = self._get_denoise_row_from_list( - progressives, desc='Progressive Generation' + progressives, desc="Progressive Generation" ) - log['progressive_row'] = prog_row + log["progressive_row"] = prog_row if return_keys: if np.intersect1d(list(log.keys()), return_keys).shape[0] == 0: @@ -2097,24 +1959,22 @@ class LatentDiffusion(DDPM): else: params = list(self.model.parameters()) if self.cond_stage_trainable: - print( - f'{self.__class__.__name__}: Also optimizing conditioner params!' - ) + print(f"{self.__class__.__name__}: Also optimizing conditioner params!") params = params + list(self.cond_stage_model.parameters()) if self.learn_logvar: - print('Diffusion model optimizing logvar') + print("Diffusion model optimizing logvar") params.append(self.logvar) opt = torch.optim.AdamW(params, lr=lr) if self.use_scheduler: - assert 'target' in self.scheduler_config + assert "target" in self.scheduler_config scheduler = instantiate_from_config(self.scheduler_config) - print('Setting up LambdaLR scheduler...') + print("Setting up LambdaLR scheduler...") scheduler = [ { - 'scheduler': LambdaLR(opt, lr_lambda=scheduler.schedule), - 'interval': 'step', - 'frequency': 1, + "scheduler": LambdaLR(opt, lr_lambda=scheduler.schedule), + "interval": "step", + "frequency": 1, } ] return [opt], scheduler @@ -2123,7 +1983,7 @@ class LatentDiffusion(DDPM): @torch.no_grad() def to_rgb(self, x): x = x.float() - if not hasattr(self, 'colorize'): + if not hasattr(self, "colorize"): self.colorize = torch.randn(3, x.shape[1], 1, 1).to(x) x = nn.functional.conv2d(x, weight=self.colorize) x = 2.0 * (x - x.min()) / (x.max() - x.min()) - 1.0 @@ -2135,16 +1995,14 @@ class LatentDiffusion(DDPM): if os.path.isdir(self.trainer.checkpoint_callback.dirpath): self.embedding_manager.save( - os.path.join( - self.trainer.checkpoint_callback.dirpath, 'embeddings.pt' - ) + os.path.join(self.trainer.checkpoint_callback.dirpath, "embeddings.pt") ) if (self.global_step - self.emb_ckpt_counter) > 500: self.embedding_manager.save( os.path.join( self.trainer.checkpoint_callback.dirpath, - f'embeddings_gs-{self.global_step}.pt', + f"embeddings_gs-{self.global_step}.pt", ) ) @@ -2158,26 +2016,26 @@ class DiffusionWrapper(pl.LightningModule): self.conditioning_key = conditioning_key assert self.conditioning_key in [ None, - 'concat', - 'crossattn', - 'hybrid', - 'adm', + "concat", + "crossattn", + "hybrid", + "adm", ] def forward(self, x, t, c_concat: list = None, c_crossattn: list = None): if self.conditioning_key is None: out = self.diffusion_model(x, t) - elif self.conditioning_key == 'concat': + elif self.conditioning_key == "concat": xc = torch.cat([x] + c_concat, dim=1) out = self.diffusion_model(xc, t) - elif self.conditioning_key == 'crossattn': + elif self.conditioning_key == "crossattn": cc = torch.cat(c_crossattn, 1) out = self.diffusion_model(x, t, context=cc) - elif self.conditioning_key == 'hybrid': + elif self.conditioning_key == "hybrid": cc = torch.cat(c_crossattn, 1) xc = torch.cat([x] + c_concat, dim=1) out = self.diffusion_model(xc, t, context=cc) - elif self.conditioning_key == 'adm': + elif self.conditioning_key == "adm": cc = c_crossattn[0] out = self.diffusion_model(x, t, y=cc) else: @@ -2190,31 +2048,28 @@ class Layout2ImgDiffusion(LatentDiffusion): # TODO: move all layout-specific hacks to this class def __init__(self, cond_stage_key, *args, **kwargs): assert ( - cond_stage_key == 'coordinates_bbox' + cond_stage_key == "coordinates_bbox" ), 'Layout2ImgDiffusion only for cond_stage_key="coordinates_bbox"' super().__init__(cond_stage_key=cond_stage_key, *args, **kwargs) def log_images(self, batch, N=8, *args, **kwargs): logs = super().log_images(batch=batch, N=N, *args, **kwargs) - key = 'train' if self.training else 'validation' + key = "train" if self.training else "validation" dset = self.trainer.datamodule.datasets[key] mapper = dset.conditional_builders[self.cond_stage_key] bbox_imgs = [] - map_fn = lambda catno: dset.get_textual_label( - dset.get_category_id(catno) - ) + map_fn = lambda catno: dset.get_textual_label(dset.get_category_id(catno)) for tknzd_bbox in batch[self.cond_stage_key][:N]: - bboximg = mapper.plot( - tknzd_bbox.detach().cpu(), map_fn, (256, 256) - ) + bboximg = mapper.plot(tknzd_bbox.detach().cpu(), map_fn, (256, 256)) bbox_imgs.append(bboximg) cond_img = torch.stack(bbox_imgs, dim=0) - logs['bbox_image'] = cond_img + logs["bbox_image"] = cond_img return logs + class LatentInpaintDiffusion(LatentDiffusion): def __init__( self, @@ -2229,7 +2084,6 @@ class LatentInpaintDiffusion(LatentDiffusion): assert self.masked_image_key in concat_keys self.concat_keys = concat_keys - @torch.no_grad() def get_input( self, batch, k, cond_key=None, bs=None, return_first_stage_outputs=False diff --git a/invokeai/backend/stable_diffusion/diffusion/ksampler.py b/invokeai/backend/stable_diffusion/diffusion/ksampler.py index f98ca8de21..eddcc11ea8 100644 --- a/invokeai/backend/stable_diffusion/diffusion/ksampler.py +++ b/invokeai/backend/stable_diffusion/diffusion/ksampler.py @@ -8,12 +8,12 @@ from .cross_attention_map_saving import AttentionMapSaver from .sampler import Sampler from .shared_invokeai_diffusion import InvokeAIDiffuserComponent - # at this threshold, the scheduler will stop using the Karras # noise schedule and start using the model's schedule STEP_THRESHOLD = 30 -def cfg_apply_threshold(result, threshold = 0.0, scale = 0.7): + +def cfg_apply_threshold(result, threshold=0.0, scale=0.7): if threshold <= 0.0: return result maxval = 0.0 + torch.max(result).cpu().numpy() @@ -21,35 +21,43 @@ def cfg_apply_threshold(result, threshold = 0.0, scale = 0.7): if maxval < threshold and minval > -threshold: return result if maxval > threshold: - maxval = min(max(1, scale*maxval), threshold) + maxval = min(max(1, scale * maxval), threshold) if minval < -threshold: - minval = max(min(-1, scale*minval), -threshold) + minval = max(min(-1, scale * minval), -threshold) return torch.clamp(result, min=minval, max=maxval) class CFGDenoiser(nn.Module): - def __init__(self, model, threshold = 0, warmup = 0): + def __init__(self, model, threshold=0, warmup=0): super().__init__() self.inner_model = model self.threshold = threshold self.warmup_max = warmup self.warmup = max(warmup / 10, 1) - self.invokeai_diffuser = InvokeAIDiffuserComponent(model, - model_forward_callback=lambda x, sigma, cond: self.inner_model(x, sigma, cond=cond)) - + self.invokeai_diffuser = InvokeAIDiffuserComponent( + model, + model_forward_callback=lambda x, sigma, cond: self.inner_model( + x, sigma, cond=cond + ), + ) def prepare_to_sample(self, t_enc, **kwargs): + extra_conditioning_info = kwargs.get("extra_conditioning_info", None) - extra_conditioning_info = kwargs.get('extra_conditioning_info', None) - - if extra_conditioning_info is not None and extra_conditioning_info.wants_cross_attention_control: - self.invokeai_diffuser.override_cross_attention(extra_conditioning_info, step_count = t_enc) + if ( + extra_conditioning_info is not None + and extra_conditioning_info.wants_cross_attention_control + ): + self.invokeai_diffuser.override_cross_attention( + extra_conditioning_info, step_count=t_enc + ) else: self.invokeai_diffuser.restore_default_cross_attention() - def forward(self, x, sigma, uncond, cond, cond_scale): - next_x = self.invokeai_diffuser.do_diffusion_step(x, sigma, uncond, cond, cond_scale) + next_x = self.invokeai_diffuser.do_diffusion_step( + x, sigma, uncond, cond, cond_scale + ) if self.warmup < self.warmup_max: thresh = max(1, 1 + (self.threshold - 1) * (self.warmup / self.warmup_max)) self.warmup += 1 @@ -59,8 +67,9 @@ class CFGDenoiser(nn.Module): thresh = self.threshold return cfg_apply_threshold(next_x, thresh) + class KSampler(Sampler): - def __init__(self, model, schedule='lms', device=None, **kwargs): + def __init__(self, model, schedule="lms", device=None, **kwargs): denoiser = K.external.CompVisDenoiser(model) super().__init__( denoiser, @@ -68,45 +77,49 @@ class KSampler(Sampler): steps=model.num_timesteps, ) self.sigmas = None - self.ds = None - self.s_in = None - self.karras_max = kwargs.get('karras_max',STEP_THRESHOLD) + self.ds = None + self.s_in = None + self.karras_max = kwargs.get("karras_max", STEP_THRESHOLD) if self.karras_max is None: self.karras_max = STEP_THRESHOLD def make_schedule( - self, - ddim_num_steps, - ddim_discretize='uniform', - ddim_eta=0.0, - verbose=False, + self, + ddim_num_steps, + ddim_discretize="uniform", + ddim_eta=0.0, + verbose=False, ): outer_model = self.model - self.model = outer_model.inner_model + self.model = outer_model.inner_model super().make_schedule( ddim_num_steps, - ddim_discretize='uniform', + ddim_discretize="uniform", ddim_eta=0.0, verbose=False, ) - self.model = outer_model + self.model = outer_model self.ddim_num_steps = ddim_num_steps # we don't need both of these sigmas, but storing them here to make # comparison easier later on - self.model_sigmas = self.model.get_sigmas(ddim_num_steps) + self.model_sigmas = self.model.get_sigmas(ddim_num_steps) self.karras_sigmas = K.sampling.get_sigmas_karras( n=ddim_num_steps, sigma_min=self.model.sigmas[0].item(), sigma_max=self.model.sigmas[-1].item(), - rho=7., + rho=7.0, device=self.device, ) if ddim_num_steps >= self.karras_max: - print(f'>> Ksampler using model noise schedule (steps >= {self.karras_max})') + print( + f">> Ksampler using model noise schedule (steps >= {self.karras_max})" + ) self.sigmas = self.model_sigmas else: - print(f'>> Ksampler using karras noise schedule (steps < {self.karras_max})') + print( + f">> Ksampler using karras noise schedule (steps < {self.karras_max})" + ) self.sigmas = self.karras_sigmas # ALERT: We are completely overriding the sample() method in the base class, which @@ -116,31 +129,31 @@ class KSampler(Sampler): @torch.no_grad() def decode( - self, - z_enc, - cond, - t_enc, - img_callback=None, - unconditional_guidance_scale=1.0, - unconditional_conditioning=None, - use_original_steps=False, - init_latent = None, - mask = None, - **kwargs + self, + z_enc, + cond, + t_enc, + img_callback=None, + unconditional_guidance_scale=1.0, + unconditional_conditioning=None, + use_original_steps=False, + init_latent=None, + mask=None, + **kwargs, ): - samples,_ = self.sample( - batch_size = 1, - S = t_enc, - x_T = z_enc, - shape = z_enc.shape[1:], - conditioning = cond, + samples, _ = self.sample( + batch_size=1, + S=t_enc, + x_T=z_enc, + shape=z_enc.shape[1:], + conditioning=cond, unconditional_guidance_scale=unconditional_guidance_scale, - unconditional_conditioning = unconditional_conditioning, - img_callback = img_callback, - x0 = init_latent, - mask = mask, - **kwargs - ) + unconditional_conditioning=unconditional_conditioning, + img_callback=img_callback, + x0=init_latent, + mask=mask, + **kwargs, + ) return samples # this is a no-op, provided here for compatibility with ddim and plms samplers @@ -174,26 +187,26 @@ class KSampler(Sampler): log_every_t=100, unconditional_guidance_scale=1.0, unconditional_conditioning=None, - extra_conditioning_info: InvokeAIDiffuserComponent.ExtraConditioningInfo=None, - threshold = 0, - perlin = 0, + extra_conditioning_info: InvokeAIDiffuserComponent.ExtraConditioningInfo = None, + threshold=0, + perlin=0, # this has to come in the same format as the conditioning, # e.g. as encoded tokens, ... **kwargs, ): def route_callback(k_callback_values): if img_callback is not None: - img_callback(k_callback_values['x'],k_callback_values['i']) + img_callback(k_callback_values["x"], k_callback_values["i"]) # if make_schedule() hasn't been called, we do it now if self.sigmas is None: self.make_schedule( ddim_num_steps=S, - ddim_eta = eta, - verbose = False, + ddim_eta=eta, + verbose=False, ) # sigmas are set up in make_schedule - we take the last steps items - sigmas = self.sigmas[-S-1:] + sigmas = self.sigmas[-S - 1 :] # x_T is variation noise. When an init image is provided (in x0) we need to add # more randomness to the starting image. @@ -205,27 +218,40 @@ class KSampler(Sampler): else: x = torch.randn([batch_size, *shape], device=self.device) * sigmas[0] - model_wrap_cfg = CFGDenoiser(self.model, threshold=threshold, warmup=max(0.8*S,S-10)) - model_wrap_cfg.prepare_to_sample(S, extra_conditioning_info=extra_conditioning_info) + model_wrap_cfg = CFGDenoiser( + self.model, threshold=threshold, warmup=max(0.8 * S, S - 10) + ) + model_wrap_cfg.prepare_to_sample( + S, extra_conditioning_info=extra_conditioning_info + ) # setup attention maps saving. checks for None are because there are multiple code paths to get here. attention_map_saver = None if attention_maps_callback is not None and extra_conditioning_info is not None: eos_token_index = extra_conditioning_info.tokens_count_including_eos_bos - 1 attention_map_token_ids = range(1, eos_token_index) - attention_map_saver = AttentionMapSaver(token_ids = attention_map_token_ids, latents_shape=x.shape[-2:]) - model_wrap_cfg.invokeai_diffuser.setup_attention_map_saving(attention_map_saver) + attention_map_saver = AttentionMapSaver( + token_ids=attention_map_token_ids, latents_shape=x.shape[-2:] + ) + model_wrap_cfg.invokeai_diffuser.setup_attention_map_saving( + attention_map_saver + ) extra_args = { - 'cond': conditioning, - 'uncond': unconditional_conditioning, - 'cond_scale': unconditional_guidance_scale, + "cond": conditioning, + "uncond": unconditional_conditioning, + "cond_scale": unconditional_guidance_scale, } - print(f'>> Sampling with k_{self.schedule} starting at step {len(self.sigmas)-S-1} of {len(self.sigmas)-1} ({S} new sampling steps)') + print( + f">> Sampling with k_{self.schedule} starting at step {len(self.sigmas)-S-1} of {len(self.sigmas)-1} ({S} new sampling steps)" + ) sampling_result = ( - K.sampling.__dict__[f'sample_{self.schedule}']( - model_wrap_cfg, x, sigmas, extra_args=extra_args, - callback=route_callback + K.sampling.__dict__[f"sample_{self.schedule}"]( + model_wrap_cfg, + x, + sigmas, + extra_args=extra_args, + callback=route_callback, ), None, ) @@ -237,25 +263,25 @@ class KSampler(Sampler): # a workaround is found. @torch.no_grad() def p_sample( - self, - img, - cond, - ts, - index, - unconditional_guidance_scale=1.0, - unconditional_conditioning=None, - extra_conditioning_info=None, - **kwargs, + self, + img, + cond, + ts, + index, + unconditional_guidance_scale=1.0, + unconditional_conditioning=None, + extra_conditioning_info=None, + **kwargs, ): if self.model_wrap is None: self.model_wrap = CFGDenoiser(self.model) extra_args = { - 'cond': cond, - 'uncond': unconditional_conditioning, - 'cond_scale': unconditional_guidance_scale, + "cond": cond, + "uncond": unconditional_conditioning, + "cond_scale": unconditional_guidance_scale, } if self.s_in is None: - self.s_in = img.new_ones([img.shape[0]]) + self.s_in = img.new_ones([img.shape[0]]) if self.ds is None: self.ds = [] @@ -270,14 +296,16 @@ class KSampler(Sampler): # so the actual formula for indexing into sigmas: # sigma_index = (steps-index) s_index = t_enc - index - 1 - self.model_wrap.prepare_to_sample(s_index, extra_conditioning_info=extra_conditioning_info) - img = K.sampling.__dict__[f'_{self.schedule}']( + self.model_wrap.prepare_to_sample( + s_index, extra_conditioning_info=extra_conditioning_info + ) + img = K.sampling.__dict__[f"_{self.schedule}"]( self.model_wrap, img, self.sigmas, s_index, - s_in = self.s_in, - ds = self.ds, + s_in=self.s_in, + ds=self.ds, extra_args=extra_args, ) @@ -287,26 +315,25 @@ class KSampler(Sampler): # we should not be multiplying by self.sigmas[0] if we # are at an intermediate step in img2img. See similar in # sample() which does work. - def get_initial_image(self,x_T,shape,steps): - print(f'WARNING: ksampler.get_initial_image(): get_initial_image needs testing') - x = (torch.randn(shape, device=self.device) * self.sigmas[0]) + def get_initial_image(self, x_T, shape, steps): + print(f"WARNING: ksampler.get_initial_image(): get_initial_image needs testing") + x = torch.randn(shape, device=self.device) * self.sigmas[0] if x_T is not None: return x_T + x else: return x - def prepare_to_sample(self,t_enc,**kwargs): - self.t_enc = t_enc + def prepare_to_sample(self, t_enc, **kwargs): + self.t_enc = t_enc self.model_wrap = None - self.ds = None - self.s_in = None + self.ds = None + self.s_in = None - def q_sample(self,x0,ts): - ''' + def q_sample(self, x0, ts): + """ Overrides parent method to return the q_sample of the inner model. - ''' - return self.model.inner_model.q_sample(x0,ts) + """ + return self.model.inner_model.q_sample(x0, ts) - def conditioning_key(self)->str: + def conditioning_key(self) -> str: return self.model.inner_model.model.conditioning_key - diff --git a/invokeai/backend/stable_diffusion/diffusion/plms.py b/invokeai/backend/stable_diffusion/diffusion/plms.py index f4aa9bb12d..df37afcc24 100644 --- a/invokeai/backend/stable_diffusion/diffusion/plms.py +++ b/invokeai/backend/stable_diffusion/diffusion/plms.py @@ -1,52 +1,58 @@ """SAMPLING ONLY.""" -import torch -import numpy as np -from tqdm import tqdm from functools import partial + +import numpy as np +import torch +from tqdm import tqdm + from ...util import choose_torch_device -from .shared_invokeai_diffusion import InvokeAIDiffuserComponent -from .sampler import Sampler from ..diffusionmodules.util import noise_like +from .sampler import Sampler +from .shared_invokeai_diffusion import InvokeAIDiffuserComponent class PLMSSampler(Sampler): - def __init__(self, model, schedule='linear', device=None, **kwargs): - super().__init__(model,schedule,model.num_timesteps, device) + def __init__(self, model, schedule="linear", device=None, **kwargs): + super().__init__(model, schedule, model.num_timesteps, device) def prepare_to_sample(self, t_enc, **kwargs): super().prepare_to_sample(t_enc, **kwargs) - extra_conditioning_info = kwargs.get('extra_conditioning_info', None) - all_timesteps_count = kwargs.get('all_timesteps_count', t_enc) + extra_conditioning_info = kwargs.get("extra_conditioning_info", None) + all_timesteps_count = kwargs.get("all_timesteps_count", t_enc) - if extra_conditioning_info is not None and extra_conditioning_info.wants_cross_attention_control: - self.invokeai_diffuser.override_cross_attention(extra_conditioning_info, step_count = all_timesteps_count) + if ( + extra_conditioning_info is not None + and extra_conditioning_info.wants_cross_attention_control + ): + self.invokeai_diffuser.override_cross_attention( + extra_conditioning_info, step_count=all_timesteps_count + ) else: self.invokeai_diffuser.restore_default_cross_attention() - # this is the essential routine @torch.no_grad() def p_sample( - self, - x, # image, called 'img' elsewhere - c, # conditioning, called 'cond' elsewhere - t, # timesteps, called 'ts' elsewhere - index, - repeat_noise=False, - use_original_steps=False, - quantize_denoised=False, - temperature=1.0, - noise_dropout=0.0, - score_corrector=None, - corrector_kwargs=None, - unconditional_guidance_scale=1.0, - unconditional_conditioning=None, - old_eps=[], - t_next=None, - step_count:int=1000, # total number of steps - **kwargs, + self, + x, # image, called 'img' elsewhere + c, # conditioning, called 'cond' elsewhere + t, # timesteps, called 'ts' elsewhere + index, + repeat_noise=False, + use_original_steps=False, + quantize_denoised=False, + temperature=1.0, + noise_dropout=0.0, + score_corrector=None, + corrector_kwargs=None, + unconditional_guidance_scale=1.0, + unconditional_conditioning=None, + old_eps=[], + t_next=None, + step_count: int = 1000, # total number of steps + **kwargs, ): b, *_, device = *x.shape, x.device @@ -59,24 +65,24 @@ class PLMSSampler(Sampler): e_t = self.model.apply_model(x, t, c) else: # step_index counts in the opposite direction to index - step_index = step_count-(index+1) - e_t = self.invokeai_diffuser.do_diffusion_step(x, t, - unconditional_conditioning, c, - unconditional_guidance_scale, - step_index=step_index) + step_index = step_count - (index + 1) + e_t = self.invokeai_diffuser.do_diffusion_step( + x, + t, + unconditional_conditioning, + c, + unconditional_guidance_scale, + step_index=step_index, + ) if score_corrector is not None: - assert self.model.parameterization == 'eps' + assert self.model.parameterization == "eps" e_t = score_corrector.modify_score( self.model, e_t, x, t, c, **corrector_kwargs ) return e_t - alphas = ( - self.model.alphas_cumprod - if use_original_steps - else self.ddim_alphas - ) + alphas = self.model.alphas_cumprod if use_original_steps else self.ddim_alphas alphas_prev = ( self.model.alphas_cumprod_prev if use_original_steps @@ -96,9 +102,7 @@ class PLMSSampler(Sampler): def get_x_prev_and_pred_x0(e_t, index): # select parameters corresponding to the currently considered timestep a_t = torch.full((b, 1, 1, 1), alphas[index], device=device) - a_prev = torch.full( - (b, 1, 1, 1), alphas_prev[index], device=device - ) + a_prev = torch.full((b, 1, 1, 1), alphas_prev[index], device=device) sigma_t = torch.full((b, 1, 1, 1), sigmas[index], device=device) sqrt_one_minus_at = torch.full( (b, 1, 1, 1), sqrt_one_minus_alphas[index], device=device @@ -110,11 +114,7 @@ class PLMSSampler(Sampler): pred_x0, _, *_ = self.model.first_stage_model.quantize(pred_x0) # direction pointing to x_t dir_xt = (1.0 - a_prev - sigma_t**2).sqrt() * e_t - noise = ( - sigma_t - * noise_like(x.shape, device, repeat_noise) - * temperature - ) + noise = sigma_t * noise_like(x.shape, device, repeat_noise) * temperature if noise_dropout > 0.0: noise = torch.nn.functional.dropout(noise, p=noise_dropout) x_prev = a_prev.sqrt() * pred_x0 + dir_xt + noise @@ -135,10 +135,7 @@ class PLMSSampler(Sampler): elif len(old_eps) >= 3: # 4nd order Pseudo Linear Multistep (Adams-Bashforth) e_t_prime = ( - 55 * e_t - - 59 * old_eps[-1] - + 37 * old_eps[-2] - - 9 * old_eps[-3] + 55 * e_t - 59 * old_eps[-1] + 37 * old_eps[-2] - 9 * old_eps[-3] ) / 24 x_prev, pred_x0 = get_x_prev_and_pred_x0(e_t_prime, index) diff --git a/invokeai/backend/stable_diffusion/diffusion/sampler.py b/invokeai/backend/stable_diffusion/diffusion/sampler.py index 5f9ee2da29..beb74eaefb 100644 --- a/invokeai/backend/stable_diffusion/diffusion/sampler.py +++ b/invokeai/backend/stable_diffusion/diffusion/sampler.py @@ -1,31 +1,37 @@ -''' +""" invokeai.models.diffusion.sampler Base class for invokeai.models.diffusion.ddim, invokeai.models.diffusion.ksampler, etc -''' -import torch -import numpy as np -from tqdm import tqdm +""" from functools import partial -from ...util import choose_torch_device -from .shared_invokeai_diffusion import InvokeAIDiffuserComponent +import numpy as np +import torch +from tqdm import tqdm + +from ...util import choose_torch_device from ..diffusionmodules.util import ( + extract_into_tensor, make_ddim_sampling_parameters, make_ddim_timesteps, noise_like, - extract_into_tensor, ) +from .shared_invokeai_diffusion import InvokeAIDiffuserComponent + class Sampler(object): - def __init__(self, model, schedule='linear', steps=None, device=None, **kwargs): + def __init__(self, model, schedule="linear", steps=None, device=None, **kwargs): self.model = model self.ddim_timesteps = None self.ddpm_num_timesteps = steps self.schedule = schedule - self.device = device or choose_torch_device() - self.invokeai_diffuser = InvokeAIDiffuserComponent(self.model, - model_forward_callback = lambda x, sigma, cond: self.model.apply_model(x, sigma, cond)) + self.device = device or choose_torch_device() + self.invokeai_diffuser = InvokeAIDiffuserComponent( + self.model, + model_forward_callback=lambda x, sigma, cond: self.model.apply_model( + x, sigma, cond + ), + ) def register_buffer(self, name, attr): if type(attr) == torch.Tensor: @@ -36,11 +42,11 @@ class Sampler(object): # This method was copied over from ddim.py and probably does stuff that is # ddim-specific. Disentangle at some point. def make_schedule( - self, - ddim_num_steps, - ddim_discretize='uniform', - ddim_eta=0.0, - verbose=False, + self, + ddim_num_steps, + ddim_discretize="uniform", + ddim_eta=0.0, + verbose=False, ): self.total_steps = ddim_num_steps self.ddim_timesteps = make_ddim_timesteps( @@ -52,38 +58,33 @@ class Sampler(object): alphas_cumprod = self.model.alphas_cumprod assert ( alphas_cumprod.shape[0] == self.ddpm_num_timesteps - ), 'alphas have to be defined for each timestep' - to_torch = ( - lambda x: x.clone() - .detach() - .to(torch.float32) - .to(self.model.device) - ) + ), "alphas have to be defined for each timestep" + to_torch = lambda x: x.clone().detach().to(torch.float32).to(self.model.device) - self.register_buffer('betas', to_torch(self.model.betas)) - self.register_buffer('alphas_cumprod', to_torch(alphas_cumprod)) + self.register_buffer("betas", to_torch(self.model.betas)) + self.register_buffer("alphas_cumprod", to_torch(alphas_cumprod)) self.register_buffer( - 'alphas_cumprod_prev', to_torch(self.model.alphas_cumprod_prev) + "alphas_cumprod_prev", to_torch(self.model.alphas_cumprod_prev) ) # calculations for diffusion q(x_t | x_{t-1}) and others self.register_buffer( - 'sqrt_alphas_cumprod', to_torch(np.sqrt(alphas_cumprod.cpu())) + "sqrt_alphas_cumprod", to_torch(np.sqrt(alphas_cumprod.cpu())) ) self.register_buffer( - 'sqrt_one_minus_alphas_cumprod', + "sqrt_one_minus_alphas_cumprod", to_torch(np.sqrt(1.0 - alphas_cumprod.cpu())), ) self.register_buffer( - 'log_one_minus_alphas_cumprod', + "log_one_minus_alphas_cumprod", to_torch(np.log(1.0 - alphas_cumprod.cpu())), ) self.register_buffer( - 'sqrt_recip_alphas_cumprod', + "sqrt_recip_alphas_cumprod", to_torch(np.sqrt(1.0 / alphas_cumprod.cpu())), ) self.register_buffer( - 'sqrt_recipm1_alphas_cumprod', + "sqrt_recipm1_alphas_cumprod", to_torch(np.sqrt(1.0 / alphas_cumprod.cpu() - 1)), ) @@ -98,19 +99,17 @@ class Sampler(object): eta=ddim_eta, verbose=verbose, ) - self.register_buffer('ddim_sigmas', ddim_sigmas) - self.register_buffer('ddim_alphas', ddim_alphas) - self.register_buffer('ddim_alphas_prev', ddim_alphas_prev) - self.register_buffer( - 'ddim_sqrt_one_minus_alphas', np.sqrt(1.0 - ddim_alphas) - ) + self.register_buffer("ddim_sigmas", ddim_sigmas) + self.register_buffer("ddim_alphas", ddim_alphas) + self.register_buffer("ddim_alphas_prev", ddim_alphas_prev) + self.register_buffer("ddim_sqrt_one_minus_alphas", np.sqrt(1.0 - ddim_alphas)) sigmas_for_original_sampling_steps = ddim_eta * torch.sqrt( (1 - self.alphas_cumprod_prev) / (1 - self.alphas_cumprod) * (1 - self.alphas_cumprod / self.alphas_cumprod_prev) ) self.register_buffer( - 'ddim_sigmas_for_original_num_steps', + "ddim_sigmas_for_original_num_steps", sigmas_for_original_sampling_steps, ) @@ -129,20 +128,19 @@ class Sampler(object): noise = torch.randn_like(x0) return ( extract_into_tensor(sqrt_alphas_cumprod, t, x0.shape) * x0 - + extract_into_tensor(sqrt_one_minus_alphas_cumprod, t, x0.shape) - * noise + + extract_into_tensor(sqrt_one_minus_alphas_cumprod, t, x0.shape) * noise ) @torch.no_grad() def sample( self, - S, # S is steps + S, # S is steps batch_size, shape, conditioning=None, callback=None, normals_sequence=None, - img_callback=None, # TODO: this is very confusing because it is called "step_callback" elsewhere. Change. + img_callback=None, # TODO: this is very confusing because it is called "step_callback" elsewhere. Change. quantize_x0=False, eta=0.0, mask=None, @@ -159,7 +157,6 @@ class Sampler(object): # this has to come in the same format as the conditioning, # e.g. as encoded tokens, ... **kwargs, ): - if conditioning is not None: if isinstance(conditioning, dict): ctmp = conditioning[list(conditioning.keys())[0]] @@ -167,17 +164,21 @@ class Sampler(object): ctmp = ctmp[0] cbs = ctmp.shape[0] if cbs != batch_size: - print(f"Warning: Got {cbs} conditionings but batch-size is {batch_size}") + print( + f"Warning: Got {cbs} conditionings but batch-size is {batch_size}" + ) else: if conditioning.shape[0] != batch_size: - print(f"Warning: Got {conditioning.shape[0]} conditionings but batch-size is {batch_size}") + print( + f"Warning: Got {conditioning.shape[0]} conditionings but batch-size is {batch_size}" + ) # check to see if make_schedule() has run, and if not, run it if self.ddim_timesteps is None: self.make_schedule( ddim_num_steps=S, - ddim_eta = eta, - verbose = False, + ddim_eta=eta, + verbose=False, ) ts = self.get_timesteps(S) @@ -204,32 +205,32 @@ class Sampler(object): unconditional_guidance_scale=unconditional_guidance_scale, unconditional_conditioning=unconditional_conditioning, steps=S, - **kwargs + **kwargs, ) return samples, intermediates @torch.no_grad() def do_sampling( - self, - cond, - shape, - timesteps=None, - x_T=None, - ddim_use_original_steps=False, - callback=None, - quantize_denoised=False, - mask=None, - x0=None, - img_callback=None, - log_every_t=100, - temperature=1.0, - noise_dropout=0.0, - score_corrector=None, - corrector_kwargs=None, - unconditional_guidance_scale=1.0, - unconditional_conditioning=None, - steps=None, - **kwargs + self, + cond, + shape, + timesteps=None, + x_T=None, + ddim_use_original_steps=False, + callback=None, + quantize_denoised=False, + mask=None, + x0=None, + img_callback=None, + log_every_t=100, + temperature=1.0, + noise_dropout=0.0, + score_corrector=None, + corrector_kwargs=None, + unconditional_guidance_scale=1.0, + unconditional_conditioning=None, + steps=None, + **kwargs, ): b = shape[0] time_range = ( @@ -238,29 +239,24 @@ class Sampler(object): else np.flip(timesteps) ) - total_steps=steps + total_steps = steps iterator = tqdm( time_range, - desc=f'{self.__class__.__name__}', + desc=f"{self.__class__.__name__}", total=total_steps, dynamic_ncols=True, ) old_eps = [] - self.prepare_to_sample(t_enc=total_steps,all_timesteps_count=steps,**kwargs) - img = self.get_initial_image(x_T,shape,total_steps) + self.prepare_to_sample(t_enc=total_steps, all_timesteps_count=steps, **kwargs) + img = self.get_initial_image(x_T, shape, total_steps) # probably don't need this at all - intermediates = {'x_inter': [img], 'pred_x0': [img]} + intermediates = {"x_inter": [img], "pred_x0": [img]} for i, step in enumerate(iterator): index = total_steps - i - 1 - ts = torch.full( - (b,), - step, - device=self.device, - dtype=torch.long - ) + ts = torch.full((b,), step, device=self.device, dtype=torch.long) ts_next = torch.full( (b,), time_range[min(i + 1, len(time_range) - 1)], @@ -290,7 +286,7 @@ class Sampler(object): unconditional_conditioning=unconditional_conditioning, old_eps=old_eps, t_next=ts_next, - step_count=steps + step_count=steps, ) img, pred_x0, e_t = outs @@ -300,11 +296,11 @@ class Sampler(object): if callback: callback(i) if img_callback: - img_callback(img,i) + img_callback(img, i) if index % log_every_t == 0 or index == total_steps - 1: - intermediates['x_inter'].append(img) - intermediates['pred_x0'].append(pred_x0) + intermediates["x_inter"].append(img) + intermediates["pred_x0"].append(pred_x0) return img, intermediates @@ -312,18 +308,18 @@ class Sampler(object): # The variable names are changed in order to be confusing. @torch.no_grad() def decode( - self, - x_latent, - cond, - t_start, - img_callback=None, - unconditional_guidance_scale=1.0, - unconditional_conditioning=None, - use_original_steps=False, - init_latent = None, - mask = None, - all_timesteps_count = None, - **kwargs + self, + x_latent, + cond, + t_start, + img_callback=None, + unconditional_guidance_scale=1.0, + unconditional_conditioning=None, + use_original_steps=False, + init_latent=None, + mask=None, + all_timesteps_count=None, + **kwargs, ): timesteps = ( np.arange(self.ddpm_num_timesteps) @@ -334,12 +330,16 @@ class Sampler(object): time_range = np.flip(timesteps) total_steps = timesteps.shape[0] - print(f'>> Running {self.__class__.__name__} sampling starting at step {self.total_steps - t_start} of {self.total_steps} ({total_steps} new sampling steps)') + print( + f">> Running {self.__class__.__name__} sampling starting at step {self.total_steps - t_start} of {self.total_steps} ({total_steps} new sampling steps)" + ) - iterator = tqdm(time_range, desc='Decoding image', total=total_steps) - x_dec = x_latent - x0 = init_latent - self.prepare_to_sample(t_enc=total_steps, all_timesteps_count=all_timesteps_count, **kwargs) + iterator = tqdm(time_range, desc="Decoding image", total=total_steps) + x_dec = x_latent + x0 = init_latent + self.prepare_to_sample( + t_enc=total_steps, all_timesteps_count=all_timesteps_count, **kwargs + ) for i, step in enumerate(iterator): index = total_steps - i - 1 @@ -370,81 +370,85 @@ class Sampler(object): use_original_steps=use_original_steps, unconditional_guidance_scale=unconditional_guidance_scale, unconditional_conditioning=unconditional_conditioning, - t_next = ts_next, - step_count=len(self.ddim_timesteps) + t_next=ts_next, + step_count=len(self.ddim_timesteps), ) x_dec, pred_x0, e_t = outs if img_callback: - img_callback(x_dec,i) + img_callback(x_dec, i) return x_dec - def get_initial_image(self,x_T,shape,timesteps=None): + def get_initial_image(self, x_T, shape, timesteps=None): if x_T is None: return torch.randn(shape, device=self.device) else: return x_T def p_sample( - self, - img, - cond, - ts, - index, - repeat_noise=False, - use_original_steps=False, - quantize_denoised=False, - temperature=1.0, - noise_dropout=0.0, - score_corrector=None, - corrector_kwargs=None, - unconditional_guidance_scale=1.0, - unconditional_conditioning=None, - old_eps=None, - t_next=None, - steps=None, + self, + img, + cond, + ts, + index, + repeat_noise=False, + use_original_steps=False, + quantize_denoised=False, + temperature=1.0, + noise_dropout=0.0, + score_corrector=None, + corrector_kwargs=None, + unconditional_guidance_scale=1.0, + unconditional_conditioning=None, + old_eps=None, + t_next=None, + steps=None, ): - raise NotImplementedError("p_sample() must be implemented in a descendent class") + raise NotImplementedError( + "p_sample() must be implemented in a descendent class" + ) - def prepare_to_sample(self,t_enc,**kwargs): - ''' + def prepare_to_sample(self, t_enc, **kwargs): + """ Hook that will be called right before the very first invocation of p_sample() to allow subclass to do additional initialization. t_enc corresponds to the actual number of steps that will be run, and may be less than total steps if img2img is active. - ''' + """ pass - def get_timesteps(self,ddim_steps): - ''' + def get_timesteps(self, ddim_steps): + """ The ddim and plms samplers work on timesteps. This method is called after ddim_timesteps are created in make_schedule(), and selects the portion of timesteps that will be used for sampling, depending on the t_enc in img2img. - ''' + """ return self.ddim_timesteps[:ddim_steps] - def q_sample(self,x0,ts): - ''' + def q_sample(self, x0, ts): + """ Returns self.model.q_sample(x0,ts). Is overridden in the k* samplers to return self.model.inner_model.q_sample(x0,ts) - ''' - return self.model.q_sample(x0,ts) + """ + return self.model.q_sample(x0, ts) - def conditioning_key(self)->str: + def conditioning_key(self) -> str: return self.model.model.conditioning_key - def uses_inpainting_model(self)->bool: - return self.conditioning_key() in ('hybrid','concat') + def uses_inpainting_model(self) -> bool: + return self.conditioning_key() in ("hybrid", "concat") - def adjust_settings(self,**kwargs): - ''' + def adjust_settings(self, **kwargs): + """ This is a catch-all method for adjusting any instance variables after the sampler is instantiated. No type-checking performed here, so use with care! - ''' + """ for k in kwargs.keys(): try: - setattr(self,k,kwargs[k]) + setattr(self, k, kwargs[k]) except AttributeError: - print(f'** Warning: attempt to set unknown attribute {k} in sampler of type {type(self)}') + print( + f"** Warning: attempt to set unknown attribute {k} in sampler of type {type(self)}" + ) diff --git a/invokeai/backend/stable_diffusion/diffusion/shared_invokeai_diffusion.py b/invokeai/backend/stable_diffusion/diffusion/shared_invokeai_diffusion.py index b02a381d70..7e3ab455b9 100644 --- a/invokeai/backend/stable_diffusion/diffusion/shared_invokeai_diffusion.py +++ b/invokeai/backend/stable_diffusion/diffusion/shared_invokeai_diffusion.py @@ -1,7 +1,7 @@ from contextlib import contextmanager from dataclasses import dataclass from math import ceil -from typing import Callable, Optional, Union, Any, Dict +from typing import Any, Callable, Dict, Optional, Union import numpy as np import torch @@ -9,17 +9,28 @@ from diffusers.models.cross_attention import AttnProcessor from typing_extensions import TypeAlias from invokeai.backend.globals import Globals -from .cross_attention_control import Arguments, \ - restore_default_cross_attention, override_cross_attention, Context, get_cross_attention_modules, \ - CrossAttentionType, SwapCrossAttnContext + +from .cross_attention_control import ( + Arguments, + Context, + CrossAttentionType, + SwapCrossAttnContext, + get_cross_attention_modules, + override_cross_attention, + restore_default_cross_attention, +) from .cross_attention_map_saving import AttentionMapSaver ModelForwardCallback: TypeAlias = Union[ # x, t, conditioning, Optional[cross-attention kwargs] - Callable[[torch.Tensor, torch.Tensor, torch.Tensor, Optional[dict[str, Any]]], torch.Tensor], - Callable[[torch.Tensor, torch.Tensor, torch.Tensor], torch.Tensor] + Callable[ + [torch.Tensor, torch.Tensor, torch.Tensor, Optional[dict[str, Any]]], + torch.Tensor, + ], + Callable[[torch.Tensor, torch.Tensor, torch.Tensor], torch.Tensor], ] + @dataclass(frozen=True) class PostprocessingSettings: threshold: float @@ -29,20 +40,20 @@ class PostprocessingSettings: class InvokeAIDiffuserComponent: - ''' + """ The aim of this component is to provide a single place for code that can be applied identically to all InvokeAI diffusion procedures. At the moment it includes the following features: * Cross attention control ("prompt2prompt") * Hybrid conditioning (used for inpainting) - ''' + """ + debug_thresholding = False sequential_guidance = False @dataclass class ExtraConditioningInfo: - tokens_count_including_eos_bos: int cross_attention_control_args: Optional[Arguments] = None @@ -50,10 +61,12 @@ class InvokeAIDiffuserComponent: def wants_cross_attention_control(self): return self.cross_attention_control_args is not None - - def __init__(self, model, model_forward_callback: ModelForwardCallback, - is_running_diffusers: bool=False, - ): + def __init__( + self, + model, + model_forward_callback: ModelForwardCallback, + is_running_diffusers: bool = False, + ): """ :param model: the unet model to pass through to cross attention control :param model_forward_callback: a lambda with arguments (x, sigma, conditioning_to_apply). will be called repeatedly. most likely, this should simply call model.forward(x, sigma, conditioning) @@ -66,23 +79,29 @@ class InvokeAIDiffuserComponent: self.sequential_guidance = Globals.sequential_guidance @contextmanager - def custom_attention_context(self, - extra_conditioning_info: Optional[ExtraConditioningInfo], - step_count: int): - do_swap = extra_conditioning_info is not None and extra_conditioning_info.wants_cross_attention_control + def custom_attention_context( + self, extra_conditioning_info: Optional[ExtraConditioningInfo], step_count: int + ): + do_swap = ( + extra_conditioning_info is not None + and extra_conditioning_info.wants_cross_attention_control + ) old_attn_processor = None if do_swap: - old_attn_processor = self.override_cross_attention(extra_conditioning_info, - step_count=step_count) + old_attn_processor = self.override_cross_attention( + extra_conditioning_info, step_count=step_count + ) try: yield None finally: if old_attn_processor is not None: self.restore_default_cross_attention(old_attn_processor) # TODO resuscitate attention map saving - #self.remove_attention_map_saving() + # self.remove_attention_map_saving() - def override_cross_attention(self, conditioning: ExtraConditioningInfo, step_count: int) -> Dict[str, AttnProcessor]: + def override_cross_attention( + self, conditioning: ExtraConditioningInfo, step_count: int + ) -> Dict[str, AttnProcessor]: """ setup cross attention .swap control. for diffusers this replaces the attention processor, so the previous attention processor is returned so that the caller can restore it later. @@ -90,18 +109,24 @@ class InvokeAIDiffuserComponent: self.conditioning = conditioning self.cross_attention_control_context = Context( arguments=self.conditioning.cross_attention_control_args, - step_count=step_count + step_count=step_count, + ) + return override_cross_attention( + self.model, + self.cross_attention_control_context, + is_running_diffusers=self.is_running_diffusers, ) - return override_cross_attention(self.model, - self.cross_attention_control_context, - is_running_diffusers=self.is_running_diffusers) - def restore_default_cross_attention(self, restore_attention_processor: Optional['AttnProcessor']=None): + def restore_default_cross_attention( + self, restore_attention_processor: Optional["AttnProcessor"] = None + ): self.conditioning = None self.cross_attention_control_context = None - restore_default_cross_attention(self.model, - is_running_diffusers=self.is_running_diffusers, - restore_attention_processor=restore_attention_processor) + restore_default_cross_attention( + self.model, + is_running_diffusers=self.is_running_diffusers, + restore_attention_processor=restore_attention_processor, + ) def setup_attention_map_saving(self, saver: AttentionMapSaver): def callback(slice, dim, offset, slice_size, key): @@ -110,26 +135,40 @@ class InvokeAIDiffuserComponent: return saver.add_attention_maps(slice, key) - tokens_cross_attention_modules = get_cross_attention_modules(self.model, CrossAttentionType.TOKENS) + tokens_cross_attention_modules = get_cross_attention_modules( + self.model, CrossAttentionType.TOKENS + ) for identifier, module in tokens_cross_attention_modules: - key = ('down' if identifier.startswith('down') else - 'up' if identifier.startswith('up') else - 'mid') + key = ( + "down" + if identifier.startswith("down") + else "up" + if identifier.startswith("up") + else "mid" + ) module.set_attention_slice_calculated_callback( - lambda slice, dim, offset, slice_size, key=key: callback(slice, dim, offset, slice_size, key)) + lambda slice, dim, offset, slice_size, key=key: callback( + slice, dim, offset, slice_size, key + ) + ) def remove_attention_map_saving(self): - tokens_cross_attention_modules = get_cross_attention_modules(self.model, CrossAttentionType.TOKENS) + tokens_cross_attention_modules = get_cross_attention_modules( + self.model, CrossAttentionType.TOKENS + ) for _, module in tokens_cross_attention_modules: module.set_attention_slice_calculated_callback(None) - def do_diffusion_step(self, x: torch.Tensor, sigma: torch.Tensor, - unconditioning: Union[torch.Tensor,dict], - conditioning: Union[torch.Tensor,dict], - unconditional_guidance_scale: float, - step_index: Optional[int]=None, - total_step_count: Optional[int]=None, - ): + def do_diffusion_step( + self, + x: torch.Tensor, + sigma: torch.Tensor, + unconditioning: Union[torch.Tensor, dict], + conditioning: Union[torch.Tensor, dict], + unconditional_guidance_scale: float, + step_index: Optional[int] = None, + total_step_count: Optional[int] = None, + ): """ :param x: current latents :param sigma: aka t, passed to the internal model to control how much denoising will occur @@ -140,33 +179,55 @@ class InvokeAIDiffuserComponent: :return: the new latents after applying the model to x using unscaled unconditioning and CFG-scaled conditioning. """ - cross_attention_control_types_to_do = [] context: Context = self.cross_attention_control_context if self.cross_attention_control_context is not None: - percent_through = self.calculate_percent_through(sigma, step_index, total_step_count) - cross_attention_control_types_to_do = context.get_active_cross_attention_control_types_for_step(percent_through) + percent_through = self.calculate_percent_through( + sigma, step_index, total_step_count + ) + cross_attention_control_types_to_do = ( + context.get_active_cross_attention_control_types_for_step( + percent_through + ) + ) - wants_cross_attention_control = (len(cross_attention_control_types_to_do) > 0) + wants_cross_attention_control = len(cross_attention_control_types_to_do) > 0 wants_hybrid_conditioning = isinstance(conditioning, dict) if wants_hybrid_conditioning: - unconditioned_next_x, conditioned_next_x = self._apply_hybrid_conditioning(x, sigma, unconditioning, - conditioning) + unconditioned_next_x, conditioned_next_x = self._apply_hybrid_conditioning( + x, sigma, unconditioning, conditioning + ) elif wants_cross_attention_control: - unconditioned_next_x, conditioned_next_x = self._apply_cross_attention_controlled_conditioning(x, sigma, - unconditioning, - conditioning, - cross_attention_control_types_to_do) + ( + unconditioned_next_x, + conditioned_next_x, + ) = self._apply_cross_attention_controlled_conditioning( + x, + sigma, + unconditioning, + conditioning, + cross_attention_control_types_to_do, + ) elif self.sequential_guidance: - unconditioned_next_x, conditioned_next_x = self._apply_standard_conditioning_sequentially( - x, sigma, unconditioning, conditioning) + ( + unconditioned_next_x, + conditioned_next_x, + ) = self._apply_standard_conditioning_sequentially( + x, sigma, unconditioning, conditioning + ) else: - unconditioned_next_x, conditioned_next_x = self._apply_standard_conditioning( - x, sigma, unconditioning, conditioning) + ( + unconditioned_next_x, + conditioned_next_x, + ) = self._apply_standard_conditioning( + x, sigma, unconditioning, conditioning + ) - combined_next_x = self._combine(unconditioned_next_x, conditioned_next_x, unconditional_guidance_scale) + combined_next_x = self._combine( + unconditioned_next_x, conditioned_next_x, unconditional_guidance_scale + ) return combined_next_x @@ -176,24 +237,33 @@ class InvokeAIDiffuserComponent: latents: torch.Tensor, sigma, step_index, - total_step_count + total_step_count, ) -> torch.Tensor: if postprocessing_settings is not None: - percent_through = self.calculate_percent_through(sigma, step_index, total_step_count) - latents = self.apply_threshold(postprocessing_settings, latents, percent_through) - latents = self.apply_symmetry(postprocessing_settings, latents, percent_through) + percent_through = self.calculate_percent_through( + sigma, step_index, total_step_count + ) + latents = self.apply_threshold( + postprocessing_settings, latents, percent_through + ) + latents = self.apply_symmetry( + postprocessing_settings, latents, percent_through + ) return latents def calculate_percent_through(self, sigma, step_index, total_step_count): if step_index is not None and total_step_count is not None: # 🧨diffusers codepath - percent_through = step_index / total_step_count # will never reach 1.0 - this is deliberate + percent_through = ( + step_index / total_step_count + ) # will never reach 1.0 - this is deliberate else: # legacy compvis codepath # TODO remove when compvis codepath support is dropped if step_index is None and sigma is None: raise ValueError( - f"Either step_index or sigma is required when doing cross attention control, but both are None.") + f"Either step_index or sigma is required when doing cross attention control, but both are None." + ) percent_through = self.estimate_percent_through(step_index, sigma) return percent_through @@ -204,24 +274,30 @@ class InvokeAIDiffuserComponent: x_twice = torch.cat([x] * 2) sigma_twice = torch.cat([sigma] * 2) both_conditionings = torch.cat([unconditioning, conditioning]) - both_results = self.model_forward_callback(x_twice, sigma_twice, both_conditionings) + both_results = self.model_forward_callback( + x_twice, sigma_twice, both_conditionings + ) unconditioned_next_x, conditioned_next_x = both_results.chunk(2) - if conditioned_next_x.device.type == 'mps': + if conditioned_next_x.device.type == "mps": # prevent a result filled with zeros. seems to be a torch bug. conditioned_next_x = conditioned_next_x.clone() return unconditioned_next_x, conditioned_next_x - - def _apply_standard_conditioning_sequentially(self, x: torch.Tensor, sigma, unconditioning: torch.Tensor, conditioning: torch.Tensor): + def _apply_standard_conditioning_sequentially( + self, + x: torch.Tensor, + sigma, + unconditioning: torch.Tensor, + conditioning: torch.Tensor, + ): # low-memory sequential path unconditioned_next_x = self.model_forward_callback(x, sigma, unconditioning) conditioned_next_x = self.model_forward_callback(x, sigma, conditioning) - if conditioned_next_x.device.type == 'mps': + if conditioned_next_x.device.type == "mps": # prevent a result filled with zeros. seems to be a torch bug. conditioned_next_x = conditioned_next_x.clone() return unconditioned_next_x, conditioned_next_x - def _apply_hybrid_conditioning(self, x, sigma, unconditioning, conditioning): assert isinstance(conditioning, dict) assert isinstance(unconditioning, dict) @@ -236,48 +312,80 @@ class InvokeAIDiffuserComponent: ] else: both_conditionings[k] = torch.cat([unconditioning[k], conditioning[k]]) - unconditioned_next_x, conditioned_next_x = self.model_forward_callback(x_twice, sigma_twice, both_conditionings).chunk(2) + unconditioned_next_x, conditioned_next_x = self.model_forward_callback( + x_twice, sigma_twice, both_conditionings + ).chunk(2) return unconditioned_next_x, conditioned_next_x - - def _apply_cross_attention_controlled_conditioning(self, - x: torch.Tensor, - sigma, - unconditioning, - conditioning, - cross_attention_control_types_to_do): + def _apply_cross_attention_controlled_conditioning( + self, + x: torch.Tensor, + sigma, + unconditioning, + conditioning, + cross_attention_control_types_to_do, + ): if self.is_running_diffusers: - return self._apply_cross_attention_controlled_conditioning__diffusers(x, sigma, unconditioning, - conditioning, - cross_attention_control_types_to_do) + return self._apply_cross_attention_controlled_conditioning__diffusers( + x, + sigma, + unconditioning, + conditioning, + cross_attention_control_types_to_do, + ) else: - return self._apply_cross_attention_controlled_conditioning__compvis(x, sigma, unconditioning, conditioning, - cross_attention_control_types_to_do) + return self._apply_cross_attention_controlled_conditioning__compvis( + x, + sigma, + unconditioning, + conditioning, + cross_attention_control_types_to_do, + ) - def _apply_cross_attention_controlled_conditioning__diffusers(self, - x: torch.Tensor, - sigma, - unconditioning, - conditioning, - cross_attention_control_types_to_do): + def _apply_cross_attention_controlled_conditioning__diffusers( + self, + x: torch.Tensor, + sigma, + unconditioning, + conditioning, + cross_attention_control_types_to_do, + ): context: Context = self.cross_attention_control_context - cross_attn_processor_context = SwapCrossAttnContext(modified_text_embeddings=context.arguments.edited_conditioning, - index_map=context.cross_attention_index_map, - mask=context.cross_attention_mask, - cross_attention_types_to_do=[]) + cross_attn_processor_context = SwapCrossAttnContext( + modified_text_embeddings=context.arguments.edited_conditioning, + index_map=context.cross_attention_index_map, + mask=context.cross_attention_mask, + cross_attention_types_to_do=[], + ) # no cross attention for unconditioning (negative prompt) - unconditioned_next_x = self.model_forward_callback(x, sigma, unconditioning, - {"swap_cross_attn_context": cross_attn_processor_context}) + unconditioned_next_x = self.model_forward_callback( + x, + sigma, + unconditioning, + {"swap_cross_attn_context": cross_attn_processor_context}, + ) # do requested cross attention types for conditioning (positive prompt) - cross_attn_processor_context.cross_attention_types_to_do = cross_attention_control_types_to_do - conditioned_next_x = self.model_forward_callback(x, sigma, conditioning, - {"swap_cross_attn_context": cross_attn_processor_context}) + cross_attn_processor_context.cross_attention_types_to_do = ( + cross_attention_control_types_to_do + ) + conditioned_next_x = self.model_forward_callback( + x, + sigma, + conditioning, + {"swap_cross_attn_context": cross_attn_processor_context}, + ) return unconditioned_next_x, conditioned_next_x - - def _apply_cross_attention_controlled_conditioning__compvis(self, x:torch.Tensor, sigma, unconditioning, conditioning, cross_attention_control_types_to_do): + def _apply_cross_attention_controlled_conditioning__compvis( + self, + x: torch.Tensor, + sigma, + unconditioning, + conditioning, + cross_attention_control_types_to_do, + ): # print('pct', percent_through, ': doing cross attention control on', cross_attention_control_types_to_do) # slower non-batched path (20% slower on mac MPS) # We are only interested in using attention maps for conditioned_next_x, but batching them with generation of @@ -287,24 +395,28 @@ class InvokeAIDiffuserComponent: # representing batched uncond + cond, but then when it comes to applying the saved attention, the # wrangler gets an attention tensor which only has shape[0]=8, representing just self.edited_conditionings.) # todo: give CrossAttentionControl's `wrangler` function more info so it can work with a batched call as well. - context:Context = self.cross_attention_control_context + context: Context = self.cross_attention_control_context try: unconditioned_next_x = self.model_forward_callback(x, sigma, unconditioning) # process x using the original prompt, saving the attention maps - #print("saving attention maps for", cross_attention_control_types_to_do) + # print("saving attention maps for", cross_attention_control_types_to_do) for ca_type in cross_attention_control_types_to_do: context.request_save_attention_maps(ca_type) _ = self.model_forward_callback(x, sigma, conditioning) context.clear_requests(cleanup=False) # process x again, using the saved attention maps to control where self.edited_conditioning will be applied - #print("applying saved attention maps for", cross_attention_control_types_to_do) + # print("applying saved attention maps for", cross_attention_control_types_to_do) for ca_type in cross_attention_control_types_to_do: context.request_apply_saved_attention_maps(ca_type) - edited_conditioning = self.conditioning.cross_attention_control_args.edited_conditioning - conditioned_next_x = self.model_forward_callback(x, sigma, edited_conditioning) + edited_conditioning = ( + self.conditioning.cross_attention_control_args.edited_conditioning + ) + conditioned_next_x = self.model_forward_callback( + x, sigma, edited_conditioning + ) context.clear_requests(cleanup=True) except: @@ -323,17 +435,21 @@ class InvokeAIDiffuserComponent: self, postprocessing_settings: PostprocessingSettings, latents: torch.Tensor, - percent_through: float + percent_through: float, ) -> torch.Tensor: - - if postprocessing_settings.threshold is None or postprocessing_settings.threshold == 0.0: + if ( + postprocessing_settings.threshold is None + or postprocessing_settings.threshold == 0.0 + ): return latents threshold = postprocessing_settings.threshold warmup = postprocessing_settings.warmup if percent_through < warmup: - current_threshold = threshold + threshold * 5 * (1 - (percent_through / warmup)) + current_threshold = threshold + threshold * 5 * ( + 1 - (percent_through / warmup) + ) else: current_threshold = threshold @@ -347,10 +463,14 @@ class InvokeAIDiffuserComponent: if self.debug_thresholding: std, mean = [i.item() for i in torch.std_mean(latents)] - outside = torch.count_nonzero((latents < -current_threshold) | (latents > current_threshold)) - print(f"\nThreshold: %={percent_through} threshold={current_threshold:.3f} (of {threshold:.3f})\n" - f" | min, mean, max = {minval:.3f}, {mean:.3f}, {maxval:.3f}\tstd={std}\n" - f" | {outside / latents.numel() * 100:.2f}% values outside threshold") + outside = torch.count_nonzero( + (latents < -current_threshold) | (latents > current_threshold) + ) + print( + f"\nThreshold: %={percent_through} threshold={current_threshold:.3f} (of {threshold:.3f})\n" + f" | min, mean, max = {minval:.3f}, {mean:.3f}, {maxval:.3f}\tstd={std}\n" + f" | {outside / latents.numel() * 100:.2f}% values outside threshold" + ) if maxval < current_threshold and minval > -current_threshold: return latents @@ -363,17 +483,23 @@ class InvokeAIDiffuserComponent: latents = torch.clone(latents) maxval = np.clip(maxval * scale, 1, current_threshold) num_altered += torch.count_nonzero(latents > maxval) - latents[latents > maxval] = torch.rand_like(latents[latents > maxval]) * maxval + latents[latents > maxval] = ( + torch.rand_like(latents[latents > maxval]) * maxval + ) if minval < -current_threshold: latents = torch.clone(latents) minval = np.clip(minval * scale, -current_threshold, -1) num_altered += torch.count_nonzero(latents < minval) - latents[latents < minval] = torch.rand_like(latents[latents < minval]) * minval + latents[latents < minval] = ( + torch.rand_like(latents[latents < minval]) * minval + ) if self.debug_thresholding: - print(f" | min, , max = {minval:.3f}, , {maxval:.3f}\t(scaled by {scale})\n" - f" | {num_altered / latents.numel() * 100:.2f}% values altered") + print( + f" | min, , max = {minval:.3f}, , {maxval:.3f}\t(scaled by {scale})\n" + f" | {num_altered / latents.numel() * 100:.2f}% values altered" + ) return latents @@ -381,9 +507,8 @@ class InvokeAIDiffuserComponent: self, postprocessing_settings: PostprocessingSettings, latents: torch.Tensor, - percent_through: float + percent_through: float, ) -> torch.Tensor: - # Reset our last percent through if this is our first step. if percent_through == 0.0: self.last_percent_through = 0.0 @@ -393,36 +518,52 @@ class InvokeAIDiffuserComponent: # Check for out of bounds h_symmetry_time_pct = postprocessing_settings.h_symmetry_time_pct - if (h_symmetry_time_pct is not None and (h_symmetry_time_pct <= 0.0 or h_symmetry_time_pct > 1.0)): + if h_symmetry_time_pct is not None and ( + h_symmetry_time_pct <= 0.0 or h_symmetry_time_pct > 1.0 + ): h_symmetry_time_pct = None v_symmetry_time_pct = postprocessing_settings.v_symmetry_time_pct - if (v_symmetry_time_pct is not None and (v_symmetry_time_pct <= 0.0 or v_symmetry_time_pct > 1.0)): + if v_symmetry_time_pct is not None and ( + v_symmetry_time_pct <= 0.0 or v_symmetry_time_pct > 1.0 + ): v_symmetry_time_pct = None dev = latents.device.type - latents.to(device='cpu') + latents.to(device="cpu") if ( - h_symmetry_time_pct != None and - self.last_percent_through < h_symmetry_time_pct and - percent_through >= h_symmetry_time_pct + h_symmetry_time_pct != None + and self.last_percent_through < h_symmetry_time_pct + and percent_through >= h_symmetry_time_pct ): # Horizontal symmetry occurs on the 3rd dimension of the latent width = latents.shape[3] x_flipped = torch.flip(latents, dims=[3]) - latents = torch.cat([latents[:, :, :, 0:int(width/2)], x_flipped[:, :, :, int(width/2):int(width)]], dim=3) + latents = torch.cat( + [ + latents[:, :, :, 0 : int(width / 2)], + x_flipped[:, :, :, int(width / 2) : int(width)], + ], + dim=3, + ) if ( - v_symmetry_time_pct != None and - self.last_percent_through < v_symmetry_time_pct and - percent_through >= v_symmetry_time_pct + v_symmetry_time_pct != None + and self.last_percent_through < v_symmetry_time_pct + and percent_through >= v_symmetry_time_pct ): # Vertical symmetry occurs on the 2nd dimension of the latent height = latents.shape[2] y_flipped = torch.flip(latents, dims=[2]) - latents = torch.cat([latents[:, :, 0:int(height / 2)], y_flipped[:, :, int(height / 2):int(height)]], dim=2) + latents = torch.cat( + [ + latents[:, :, 0 : int(height / 2)], + y_flipped[:, :, int(height / 2) : int(height)], + ], + dim=2, + ) self.last_percent_through = percent_through return latents.to(device=dev) @@ -430,7 +571,9 @@ class InvokeAIDiffuserComponent: def estimate_percent_through(self, step_index, sigma): if step_index is not None and self.cross_attention_control_context is not None: # percent_through will never reach 1.0 (but this is intended) - return float(step_index) / float(self.cross_attention_control_context.step_count) + return float(step_index) / float( + self.cross_attention_control_context.step_count + ) # find the best possible index of the current sigma in the sigma sequence smaller_sigmas = torch.nonzero(self.model.sigmas <= sigma) sigma_index = smaller_sigmas[-1].item() if smaller_sigmas.shape[0] > 0 else 0 @@ -439,33 +582,38 @@ class InvokeAIDiffuserComponent: return 1.0 - float(sigma_index + 1) / float(self.model.sigmas.shape[0]) # print('estimated percent_through', percent_through, 'from sigma', sigma.item()) - # todo: make this work @classmethod - def apply_conjunction(cls, x, t, forward_func, uc, c_or_weighted_c_list, global_guidance_scale): + def apply_conjunction( + cls, x, t, forward_func, uc, c_or_weighted_c_list, global_guidance_scale + ): x_in = torch.cat([x] * 2) - t_in = torch.cat([t] * 2) # aka sigmas + t_in = torch.cat([t] * 2) # aka sigmas deltas = None uncond_latents = None - weighted_cond_list = c_or_weighted_c_list if type(c_or_weighted_c_list) is list else [(c_or_weighted_c_list, 1)] + weighted_cond_list = ( + c_or_weighted_c_list + if type(c_or_weighted_c_list) is list + else [(c_or_weighted_c_list, 1)] + ) # below is fugly omg num_actual_conditionings = len(c_or_weighted_c_list) - conditionings = [uc] + [c for c,weight in weighted_cond_list] - weights = [1] + [weight for c,weight in weighted_cond_list] - chunk_count = ceil(len(conditionings)/2) + conditionings = [uc] + [c for c, weight in weighted_cond_list] + weights = [1] + [weight for c, weight in weighted_cond_list] + chunk_count = ceil(len(conditionings) / 2) deltas = None for chunk_index in range(chunk_count): - offset = chunk_index*2 - chunk_size = min(2, len(conditionings)-offset) + offset = chunk_index * 2 + chunk_size = min(2, len(conditionings) - offset) if chunk_size == 1: c_in = conditionings[offset] latents_a = forward_func(x_in[:-1], t_in[:-1], c_in) latents_b = None else: - c_in = torch.cat(conditionings[offset:offset+2]) + c_in = torch.cat(conditionings[offset : offset + 2]) latents_a, latents_b = forward_func(x_in, t_in, c_in).chunk(2) # first chunk is guaranteed to be 2 entries: uncond_latents + first conditioining @@ -478,11 +626,15 @@ class InvokeAIDiffuserComponent: deltas = torch.cat((deltas, latents_b - uncond_latents)) # merge the weighted deltas together into a single merged delta - per_delta_weights = torch.tensor(weights[1:], dtype=deltas.dtype, device=deltas.device) + per_delta_weights = torch.tensor( + weights[1:], dtype=deltas.dtype, device=deltas.device + ) normalize = False if normalize: per_delta_weights /= torch.sum(per_delta_weights) - reshaped_weights = per_delta_weights.reshape(per_delta_weights.shape + (1, 1, 1)) + reshaped_weights = per_delta_weights.reshape( + per_delta_weights.shape + (1, 1, 1) + ) deltas_merged = torch.sum(deltas * reshaped_weights, dim=0, keepdim=True) # old_return_value = super().forward(x, sigma, uncond, cond, cond_scale) diff --git a/invokeai/backend/stable_diffusion/diffusionmodules/model.py b/invokeai/backend/stable_diffusion/diffusionmodules/model.py index 94b0dfc4c7..62cb45d508 100644 --- a/invokeai/backend/stable_diffusion/diffusionmodules/model.py +++ b/invokeai/backend/stable_diffusion/diffusionmodules/model.py @@ -1,16 +1,17 @@ # pytorch_diffusion + derived encoder decoder import gc import math + +import numpy as np +import psutil import torch import torch.nn as nn -from torch.nn.functional import silu -import numpy as np from einops import rearrange +from torch.nn.functional import silu from ...util import instantiate_from_config from ..attention import LinearAttention -import psutil def get_timestep_embedding(timesteps, embedding_dim): """ @@ -29,12 +30,14 @@ def get_timestep_embedding(timesteps, embedding_dim): emb = timesteps.float()[:, None] * emb[None, :] emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1) if embedding_dim % 2 == 1: # zero pad - emb = torch.nn.functional.pad(emb, (0,1,0,0)) + emb = torch.nn.functional.pad(emb, (0, 1, 0, 0)) return emb def Normalize(in_channels, num_groups=32): - return torch.nn.GroupNorm(num_groups=num_groups, num_channels=in_channels, eps=1e-6, affine=True) + return torch.nn.GroupNorm( + num_groups=num_groups, num_channels=in_channels, eps=1e-6, affine=True + ) class Upsample(nn.Module): @@ -42,22 +45,25 @@ class Upsample(nn.Module): super().__init__() self.with_conv = with_conv if self.with_conv: - self.conv = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=3, - stride=1, - padding=1) + self.conv = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=3, stride=1, padding=1 + ) def forward(self, x): - cpu_m1_cond = True if hasattr(torch.backends, 'mps') and torch.backends.mps.is_available() and \ - x.size()[0] * x.size()[1] * x.size()[2] * x.size()[3] % 2**27 == 0 else False + cpu_m1_cond = ( + True + if hasattr(torch.backends, "mps") + and torch.backends.mps.is_available() + and x.size()[0] * x.size()[1] * x.size()[2] * x.size()[3] % 2**27 == 0 + else False + ) if cpu_m1_cond: - x = x.to('cpu') # send to cpu + x = x.to("cpu") # send to cpu x = torch.nn.functional.interpolate(x, scale_factor=2.0, mode="nearest") if self.with_conv: x = self.conv(x) if cpu_m1_cond: - x = x.to('mps') # return to mps + x = x.to("mps") # return to mps return x @@ -67,15 +73,13 @@ class Downsample(nn.Module): self.with_conv = with_conv if self.with_conv: # no asymmetric padding in torch conv, must do it ourselves - self.conv = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=3, - stride=2, - padding=0) + self.conv = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=3, stride=2, padding=0 + ) def forward(self, x): if self.with_conv: - pad = (0,1,0,1) + pad = (0, 1, 0, 1) x = torch.nn.functional.pad(x, pad, mode="constant", value=0) x = self.conv(x) else: @@ -84,8 +88,15 @@ class Downsample(nn.Module): class ResnetBlock(nn.Module): - def __init__(self, *, in_channels, out_channels=None, conv_shortcut=False, - dropout, temb_channels=512): + def __init__( + self, + *, + in_channels, + out_channels=None, + conv_shortcut=False, + dropout, + temb_channels=512, + ): super().__init__() self.in_channels = in_channels out_channels = in_channels if out_channels is None else out_channels @@ -93,50 +104,41 @@ class ResnetBlock(nn.Module): self.use_conv_shortcut = conv_shortcut self.norm1 = Normalize(in_channels) - self.conv1 = torch.nn.Conv2d(in_channels, - out_channels, - kernel_size=3, - stride=1, - padding=1) + self.conv1 = torch.nn.Conv2d( + in_channels, out_channels, kernel_size=3, stride=1, padding=1 + ) if temb_channels > 0: - self.temb_proj = torch.nn.Linear(temb_channels, - out_channels) + self.temb_proj = torch.nn.Linear(temb_channels, out_channels) self.norm2 = Normalize(out_channels) self.dropout = torch.nn.Dropout(dropout) - self.conv2 = torch.nn.Conv2d(out_channels, - out_channels, - kernel_size=3, - stride=1, - padding=1) + self.conv2 = torch.nn.Conv2d( + out_channels, out_channels, kernel_size=3, stride=1, padding=1 + ) if self.in_channels != self.out_channels: if self.use_conv_shortcut: - self.conv_shortcut = torch.nn.Conv2d(in_channels, - out_channels, - kernel_size=3, - stride=1, - padding=1) + self.conv_shortcut = torch.nn.Conv2d( + in_channels, out_channels, kernel_size=3, stride=1, padding=1 + ) else: - self.nin_shortcut = torch.nn.Conv2d(in_channels, - out_channels, - kernel_size=1, - stride=1, - padding=0) + self.nin_shortcut = torch.nn.Conv2d( + in_channels, out_channels, kernel_size=1, stride=1, padding=0 + ) def forward(self, x, temb): - if hasattr(torch.backends, 'mps') and torch.backends.mps.is_available(): + if hasattr(torch.backends, "mps") and torch.backends.mps.is_available(): x_size = x.size() if (x_size[0] * x_size[1] * x_size[2] * x_size[3]) % 2**29 == 0: - self.to('cpu') - x = x.to('cpu') + self.to("cpu") + x = x.to("cpu") else: - self.to('mps') - x = x.to('mps') + self.to("mps") + x = x.to("mps") h = self.norm1(x) h = silu(h) h = self.conv1(h) if temb is not None: - h = h + self.temb_proj(silu(temb))[:,:,None,None] + h = h + self.temb_proj(silu(temb))[:, :, None, None] h = self.norm2(h) h = silu(h) @@ -151,8 +153,10 @@ class ResnetBlock(nn.Module): return x + h + class LinAttnBlock(LinearAttention): """to match AttnBlock usage""" + def __init__(self, in_channels): super().__init__(dim=in_channels, heads=1, dim_head=in_channels) @@ -163,27 +167,18 @@ class AttnBlock(nn.Module): self.in_channels = in_channels self.norm = Normalize(in_channels) - self.q = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0) - self.k = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0) - self.v = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0) - self.proj_out = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=1, - stride=1, - padding=0) - + self.q = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=1, stride=1, padding=0 + ) + self.k = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=1, stride=1, padding=0 + ) + self.v = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=1, stride=1, padding=0 + ) + self.proj_out = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=1, stride=1, padding=0 + ) def forward(self, x): h_ = x @@ -195,21 +190,21 @@ class AttnBlock(nn.Module): # compute attention b, c, h, w = q1.shape - q2 = q1.reshape(b, c, h*w) + q2 = q1.reshape(b, c, h * w) del q1 - q = q2.permute(0, 2, 1) # b,hw,c + q = q2.permute(0, 2, 1) # b,hw,c del q2 - k = k1.reshape(b, c, h*w) # b,c,hw + k = k1.reshape(b, c, h * w) # b,c,hw del k1 h_ = torch.zeros_like(k, device=q.device) - if q.device.type == 'cuda': + if q.device.type == "cuda": stats = torch.cuda.memory_stats(q.device) - mem_active = stats['active_bytes.all.current'] - mem_reserved = stats['reserved_bytes.all.current'] + mem_active = stats["active_bytes.all.current"] + mem_reserved = stats["reserved_bytes.all.current"] mem_free_cuda, _ = torch.cuda.mem_get_info(torch.cuda.current_device()) mem_free_torch = mem_reserved - mem_active mem_free_total = mem_free_cuda + mem_free_torch @@ -219,31 +214,37 @@ class AttnBlock(nn.Module): steps = 1 if mem_required > mem_free_total: - steps = 2**(math.ceil(math.log(mem_required / mem_free_total, 2))) + steps = 2 ** (math.ceil(math.log(mem_required / mem_free_total, 2))) - slice_size = q.shape[1] // steps if (q.shape[1] % steps) == 0 else q.shape[1] + slice_size = ( + q.shape[1] // steps if (q.shape[1] % steps) == 0 else q.shape[1] + ) else: if psutil.virtual_memory().available / (1024**3) < 12: slice_size = 1 else: - slice_size = min(q.shape[1], math.floor(2**30 / (q.shape[0] * q.shape[1]))) + slice_size = min( + q.shape[1], math.floor(2**30 / (q.shape[0] * q.shape[1])) + ) for i in range(0, q.shape[1], slice_size): end = i + slice_size - w1 = torch.bmm(q[:, i:end], k) # b,hw,hw w[b,i,j]=sum_c q[b,i,c]k[b,c,j] - w2 = w1 * (int(c)**(-0.5)) + w1 = torch.bmm(q[:, i:end], k) # b,hw,hw w[b,i,j]=sum_c q[b,i,c]k[b,c,j] + w2 = w1 * (int(c) ** (-0.5)) del w1 w3 = torch.nn.functional.softmax(w2, dim=2) del w2 # attend to values - v1 = v.reshape(b, c, h*w) - w4 = w3.permute(0, 2, 1) # b,hw,hw (first hw of k, second of q) + v1 = v.reshape(b, c, h * w) + w4 = w3.permute(0, 2, 1) # b,hw,hw (first hw of k, second of q) del w3 - h_[:, :, i:end] = torch.bmm(v1, w4) # b, c,hw (hw of q) h_[b,c,j] = sum_i v[b,c,i] w_[b,i,j] + h_[:, :, i:end] = torch.bmm( + v1, w4 + ) # b, c,hw (hw of q) h_[b,c,j] = sum_i v[b,c,i] w_[b,i,j] del v1, w4 h2 = h_.reshape(b, c, h, w) @@ -258,7 +259,7 @@ class AttnBlock(nn.Module): def make_attn(in_channels, attn_type="vanilla"): - assert attn_type in ["vanilla", "linear", "none"], f'attn_type {attn_type} unknown' + assert attn_type in ["vanilla", "linear", "none"], f"attn_type {attn_type} unknown" print(f" | Making attention of type '{attn_type}' with {in_channels} in_channels") if attn_type == "vanilla": return AttnBlock(in_channels) @@ -269,13 +270,27 @@ def make_attn(in_channels, attn_type="vanilla"): class Model(nn.Module): - def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, - attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, - resolution, use_timestep=True, use_linear_attn=False, attn_type="vanilla"): + def __init__( + self, + *, + ch, + out_ch, + ch_mult=(1, 2, 4, 8), + num_res_blocks, + attn_resolutions, + dropout=0.0, + resamp_with_conv=True, + in_channels, + resolution, + use_timestep=True, + use_linear_attn=False, + attn_type="vanilla", + ): super().__init__() - if use_linear_attn: attn_type = "linear" + if use_linear_attn: + attn_type = "linear" self.ch = ch - self.temb_ch = self.ch*4 + self.temb_ch = self.ch * 4 self.num_resolutions = len(ch_mult) self.num_res_blocks = num_res_blocks self.resolution = resolution @@ -285,70 +300,80 @@ class Model(nn.Module): if self.use_timestep: # timestep embedding self.temb = nn.Module() - self.temb.dense = nn.ModuleList([ - torch.nn.Linear(self.ch, - self.temb_ch), - torch.nn.Linear(self.temb_ch, - self.temb_ch), - ]) + self.temb.dense = nn.ModuleList( + [ + torch.nn.Linear(self.ch, self.temb_ch), + torch.nn.Linear(self.temb_ch, self.temb_ch), + ] + ) # downsampling - self.conv_in = torch.nn.Conv2d(in_channels, - self.ch, - kernel_size=3, - stride=1, - padding=1) + self.conv_in = torch.nn.Conv2d( + in_channels, self.ch, kernel_size=3, stride=1, padding=1 + ) curr_res = resolution - in_ch_mult = (1,)+tuple(ch_mult) + in_ch_mult = (1,) + tuple(ch_mult) self.down = nn.ModuleList() for i_level in range(self.num_resolutions): block = nn.ModuleList() attn = nn.ModuleList() - block_in = ch*in_ch_mult[i_level] - block_out = ch*ch_mult[i_level] + block_in = ch * in_ch_mult[i_level] + block_out = ch * ch_mult[i_level] for i_block in range(self.num_res_blocks): - block.append(ResnetBlock(in_channels=block_in, - out_channels=block_out, - temb_channels=self.temb_ch, - dropout=dropout)) + block.append( + ResnetBlock( + in_channels=block_in, + out_channels=block_out, + temb_channels=self.temb_ch, + dropout=dropout, + ) + ) block_in = block_out if curr_res in attn_resolutions: attn.append(make_attn(block_in, attn_type=attn_type)) down = nn.Module() down.block = block down.attn = attn - if i_level != self.num_resolutions-1: + if i_level != self.num_resolutions - 1: down.downsample = Downsample(block_in, resamp_with_conv) curr_res = curr_res // 2 self.down.append(down) # middle self.mid = nn.Module() - self.mid.block_1 = ResnetBlock(in_channels=block_in, - out_channels=block_in, - temb_channels=self.temb_ch, - dropout=dropout) + self.mid.block_1 = ResnetBlock( + in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout, + ) self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) - self.mid.block_2 = ResnetBlock(in_channels=block_in, - out_channels=block_in, - temb_channels=self.temb_ch, - dropout=dropout) + self.mid.block_2 = ResnetBlock( + in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout, + ) # upsampling self.up = nn.ModuleList() for i_level in reversed(range(self.num_resolutions)): block = nn.ModuleList() attn = nn.ModuleList() - block_out = ch*ch_mult[i_level] - skip_in = ch*ch_mult[i_level] - for i_block in range(self.num_res_blocks+1): + block_out = ch * ch_mult[i_level] + skip_in = ch * ch_mult[i_level] + for i_block in range(self.num_res_blocks + 1): if i_block == self.num_res_blocks: - skip_in = ch*in_ch_mult[i_level] - block.append(ResnetBlock(in_channels=block_in+skip_in, - out_channels=block_out, - temb_channels=self.temb_ch, - dropout=dropout)) + skip_in = ch * in_ch_mult[i_level] + block.append( + ResnetBlock( + in_channels=block_in + skip_in, + out_channels=block_out, + temb_channels=self.temb_ch, + dropout=dropout, + ) + ) block_in = block_out if curr_res in attn_resolutions: attn.append(make_attn(block_in, attn_type=attn_type)) @@ -358,18 +383,16 @@ class Model(nn.Module): if i_level != 0: up.upsample = Upsample(block_in, resamp_with_conv) curr_res = curr_res * 2 - self.up.insert(0, up) # prepend to get consistent order + self.up.insert(0, up) # prepend to get consistent order # end self.norm_out = Normalize(block_in) - self.conv_out = torch.nn.Conv2d(block_in, - out_ch, - kernel_size=3, - stride=1, - padding=1) + self.conv_out = torch.nn.Conv2d( + block_in, out_ch, kernel_size=3, stride=1, padding=1 + ) def forward(self, x, t=None, context=None): - #assert x.shape[2] == x.shape[3] == self.resolution + # assert x.shape[2] == x.shape[3] == self.resolution if context is not None: # assume aligned context, cat along channel axis x = torch.cat((x, context), dim=1) @@ -391,7 +414,7 @@ class Model(nn.Module): if len(self.down[i_level].attn) > 0: h = self.down[i_level].attn[i_block](h) hs.append(h) - if i_level != self.num_resolutions-1: + if i_level != self.num_resolutions - 1: hs.append(self.down[i_level].downsample(hs[-1])) # middle @@ -402,9 +425,10 @@ class Model(nn.Module): # upsampling for i_level in reversed(range(self.num_resolutions)): - for i_block in range(self.num_res_blocks+1): + for i_block in range(self.num_res_blocks + 1): h = self.up[i_level].block[i_block]( - torch.cat([h, hs.pop()], dim=1), temb) + torch.cat([h, hs.pop()], dim=1), temb + ) if len(self.up[i_level].attn) > 0: h = self.up[i_level].attn[i_block](h) if i_level != 0: @@ -421,12 +445,27 @@ class Model(nn.Module): class Encoder(nn.Module): - def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, - attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, - resolution, z_channels, double_z=True, use_linear_attn=False, attn_type="vanilla", - **ignore_kwargs): + def __init__( + self, + *, + ch, + out_ch, + ch_mult=(1, 2, 4, 8), + num_res_blocks, + attn_resolutions, + dropout=0.0, + resamp_with_conv=True, + in_channels, + resolution, + z_channels, + double_z=True, + use_linear_attn=False, + attn_type="vanilla", + **ignore_kwargs, + ): super().__init__() - if use_linear_attn: attn_type = "linear" + if use_linear_attn: + attn_type = "linear" self.ch = ch self.temb_ch = 0 self.num_resolutions = len(ch_mult) @@ -435,56 +474,64 @@ class Encoder(nn.Module): self.in_channels = in_channels # downsampling - self.conv_in = torch.nn.Conv2d(in_channels, - self.ch, - kernel_size=3, - stride=1, - padding=1) + self.conv_in = torch.nn.Conv2d( + in_channels, self.ch, kernel_size=3, stride=1, padding=1 + ) curr_res = resolution - in_ch_mult = (1,)+tuple(ch_mult) + in_ch_mult = (1,) + tuple(ch_mult) self.in_ch_mult = in_ch_mult self.down = nn.ModuleList() for i_level in range(self.num_resolutions): block = nn.ModuleList() attn = nn.ModuleList() - block_in = ch*in_ch_mult[i_level] - block_out = ch*ch_mult[i_level] + block_in = ch * in_ch_mult[i_level] + block_out = ch * ch_mult[i_level] for i_block in range(self.num_res_blocks): - block.append(ResnetBlock(in_channels=block_in, - out_channels=block_out, - temb_channels=self.temb_ch, - dropout=dropout)) + block.append( + ResnetBlock( + in_channels=block_in, + out_channels=block_out, + temb_channels=self.temb_ch, + dropout=dropout, + ) + ) block_in = block_out if curr_res in attn_resolutions: attn.append(make_attn(block_in, attn_type=attn_type)) down = nn.Module() down.block = block down.attn = attn - if i_level != self.num_resolutions-1: + if i_level != self.num_resolutions - 1: down.downsample = Downsample(block_in, resamp_with_conv) curr_res = curr_res // 2 self.down.append(down) # middle self.mid = nn.Module() - self.mid.block_1 = ResnetBlock(in_channels=block_in, - out_channels=block_in, - temb_channels=self.temb_ch, - dropout=dropout) + self.mid.block_1 = ResnetBlock( + in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout, + ) self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) - self.mid.block_2 = ResnetBlock(in_channels=block_in, - out_channels=block_in, - temb_channels=self.temb_ch, - dropout=dropout) + self.mid.block_2 = ResnetBlock( + in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout, + ) # end self.norm_out = Normalize(block_in) - self.conv_out = torch.nn.Conv2d(block_in, - 2*z_channels if double_z else z_channels, - kernel_size=3, - stride=1, - padding=1) + self.conv_out = torch.nn.Conv2d( + block_in, + 2 * z_channels if double_z else z_channels, + kernel_size=3, + stride=1, + padding=1, + ) def forward(self, x): # timestep embedding @@ -498,7 +545,7 @@ class Encoder(nn.Module): if len(self.down[i_level].attn) > 0: h = self.down[i_level].attn[i_block](h) hs.append(h) - if i_level != self.num_resolutions-1: + if i_level != self.num_resolutions - 1: hs.append(self.down[i_level].downsample(hs[-1])) # middle @@ -515,12 +562,28 @@ class Encoder(nn.Module): class Decoder(nn.Module): - def __init__(self, *, ch, out_ch, ch_mult=(1,2,4,8), num_res_blocks, - attn_resolutions, dropout=0.0, resamp_with_conv=True, in_channels, - resolution, z_channels, give_pre_end=False, tanh_out=False, use_linear_attn=False, - attn_type="vanilla", **ignorekwargs): + def __init__( + self, + *, + ch, + out_ch, + ch_mult=(1, 2, 4, 8), + num_res_blocks, + attn_resolutions, + dropout=0.0, + resamp_with_conv=True, + in_channels, + resolution, + z_channels, + give_pre_end=False, + tanh_out=False, + use_linear_attn=False, + attn_type="vanilla", + **ignorekwargs, + ): super().__init__() - if use_linear_attn: attn_type = "linear" + if use_linear_attn: + attn_type = "linear" self.ch = ch self.temb_ch = 0 self.num_resolutions = len(ch_mult) @@ -531,43 +594,52 @@ class Decoder(nn.Module): self.tanh_out = tanh_out # compute in_ch_mult, block_in and curr_res at lowest res - in_ch_mult = (1,)+tuple(ch_mult) - block_in = ch*ch_mult[self.num_resolutions-1] - curr_res = resolution // 2**(self.num_resolutions-1) - self.z_shape = (1,z_channels,curr_res,curr_res) - print(" | Working with z of shape {} = {} dimensions.".format( - self.z_shape, np.prod(self.z_shape))) + in_ch_mult = (1,) + tuple(ch_mult) + block_in = ch * ch_mult[self.num_resolutions - 1] + curr_res = resolution // 2 ** (self.num_resolutions - 1) + self.z_shape = (1, z_channels, curr_res, curr_res) + print( + " | Working with z of shape {} = {} dimensions.".format( + self.z_shape, np.prod(self.z_shape) + ) + ) # z to block_in - self.conv_in = torch.nn.Conv2d(z_channels, - block_in, - kernel_size=3, - stride=1, - padding=1) + self.conv_in = torch.nn.Conv2d( + z_channels, block_in, kernel_size=3, stride=1, padding=1 + ) # middle self.mid = nn.Module() - self.mid.block_1 = ResnetBlock(in_channels=block_in, - out_channels=block_in, - temb_channels=self.temb_ch, - dropout=dropout) + self.mid.block_1 = ResnetBlock( + in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout, + ) self.mid.attn_1 = make_attn(block_in, attn_type=attn_type) - self.mid.block_2 = ResnetBlock(in_channels=block_in, - out_channels=block_in, - temb_channels=self.temb_ch, - dropout=dropout) + self.mid.block_2 = ResnetBlock( + in_channels=block_in, + out_channels=block_in, + temb_channels=self.temb_ch, + dropout=dropout, + ) # upsampling self.up = nn.ModuleList() for i_level in reversed(range(self.num_resolutions)): block = nn.ModuleList() attn = nn.ModuleList() - block_out = ch*ch_mult[i_level] - for i_block in range(self.num_res_blocks+1): - block.append(ResnetBlock(in_channels=block_in, - out_channels=block_out, - temb_channels=self.temb_ch, - dropout=dropout)) + block_out = ch * ch_mult[i_level] + for i_block in range(self.num_res_blocks + 1): + block.append( + ResnetBlock( + in_channels=block_in, + out_channels=block_out, + temb_channels=self.temb_ch, + dropout=dropout, + ) + ) block_in = block_out if curr_res in attn_resolutions: attn.append(make_attn(block_in, attn_type=attn_type)) @@ -577,18 +649,16 @@ class Decoder(nn.Module): if i_level != 0: up.upsample = Upsample(block_in, resamp_with_conv) curr_res = curr_res * 2 - self.up.insert(0, up) # prepend to get consistent order + self.up.insert(0, up) # prepend to get consistent order # end self.norm_out = Normalize(block_in) - self.conv_out = torch.nn.Conv2d(block_in, - out_ch, - kernel_size=3, - stride=1, - padding=1) + self.conv_out = torch.nn.Conv2d( + block_in, out_ch, kernel_size=3, stride=1, padding=1 + ) def forward(self, z): - #assert z.shape[1:] == self.z_shape[1:] + # assert z.shape[1:] == self.z_shape[1:] self.last_z_shape = z.shape # timestep embedding @@ -604,12 +674,12 @@ class Decoder(nn.Module): # prepare for up sampling gc.collect() - if h.device.type == 'cuda': + if h.device.type == "cuda": torch.cuda.empty_cache() # upsampling for i_level in reversed(range(self.num_resolutions)): - for i_block in range(self.num_res_blocks+1): + for i_block in range(self.num_res_blocks + 1): h = self.up[i_level].block[i_block](h, temb) if len(self.up[i_level].attn) > 0: h = self.up[i_level].attn[i_block](h) @@ -631,29 +701,40 @@ class Decoder(nn.Module): class SimpleDecoder(nn.Module): def __init__(self, in_channels, out_channels, *args, **kwargs): super().__init__() - self.model = nn.ModuleList([nn.Conv2d(in_channels, in_channels, 1), - ResnetBlock(in_channels=in_channels, - out_channels=2 * in_channels, - temb_channels=0, dropout=0.0), - ResnetBlock(in_channels=2 * in_channels, - out_channels=4 * in_channels, - temb_channels=0, dropout=0.0), - ResnetBlock(in_channels=4 * in_channels, - out_channels=2 * in_channels, - temb_channels=0, dropout=0.0), - nn.Conv2d(2*in_channels, in_channels, 1), - Upsample(in_channels, with_conv=True)]) + self.model = nn.ModuleList( + [ + nn.Conv2d(in_channels, in_channels, 1), + ResnetBlock( + in_channels=in_channels, + out_channels=2 * in_channels, + temb_channels=0, + dropout=0.0, + ), + ResnetBlock( + in_channels=2 * in_channels, + out_channels=4 * in_channels, + temb_channels=0, + dropout=0.0, + ), + ResnetBlock( + in_channels=4 * in_channels, + out_channels=2 * in_channels, + temb_channels=0, + dropout=0.0, + ), + nn.Conv2d(2 * in_channels, in_channels, 1), + Upsample(in_channels, with_conv=True), + ] + ) # end self.norm_out = Normalize(in_channels) - self.conv_out = torch.nn.Conv2d(in_channels, - out_channels, - kernel_size=3, - stride=1, - padding=1) + self.conv_out = torch.nn.Conv2d( + in_channels, out_channels, kernel_size=3, stride=1, padding=1 + ) def forward(self, x): for i, layer in enumerate(self.model): - if i in [1,2,3]: + if i in [1, 2, 3]: x = layer(x, None) else: x = layer(x) @@ -665,8 +746,16 @@ class SimpleDecoder(nn.Module): class UpsampleDecoder(nn.Module): - def __init__(self, in_channels, out_channels, ch, num_res_blocks, resolution, - ch_mult=(2,2), dropout=0.0): + def __init__( + self, + in_channels, + out_channels, + ch, + num_res_blocks, + resolution, + ch_mult=(2, 2), + dropout=0.0, + ): super().__init__() # upsampling self.temb_ch = 0 @@ -680,10 +769,14 @@ class UpsampleDecoder(nn.Module): res_block = [] block_out = ch * ch_mult[i_level] for i_block in range(self.num_res_blocks + 1): - res_block.append(ResnetBlock(in_channels=block_in, - out_channels=block_out, - temb_channels=self.temb_ch, - dropout=dropout)) + res_block.append( + ResnetBlock( + in_channels=block_in, + out_channels=block_out, + temb_channels=self.temb_ch, + dropout=dropout, + ) + ) block_in = block_out self.res_blocks.append(nn.ModuleList(res_block)) if i_level != self.num_resolutions - 1: @@ -692,11 +785,9 @@ class UpsampleDecoder(nn.Module): # end self.norm_out = Normalize(block_in) - self.conv_out = torch.nn.Conv2d(block_in, - out_channels, - kernel_size=3, - stride=1, - padding=1) + self.conv_out = torch.nn.Conv2d( + block_in, out_channels, kernel_size=3, stride=1, padding=1 + ) def forward(self, x): # upsampling @@ -717,31 +808,50 @@ class LatentRescaler(nn.Module): super().__init__() # residual block, interpolate, residual block self.factor = factor - self.conv_in = nn.Conv2d(in_channels, - mid_channels, - kernel_size=3, - stride=1, - padding=1) - self.res_block1 = nn.ModuleList([ResnetBlock(in_channels=mid_channels, - out_channels=mid_channels, - temb_channels=0, - dropout=0.0) for _ in range(depth)]) + self.conv_in = nn.Conv2d( + in_channels, mid_channels, kernel_size=3, stride=1, padding=1 + ) + self.res_block1 = nn.ModuleList( + [ + ResnetBlock( + in_channels=mid_channels, + out_channels=mid_channels, + temb_channels=0, + dropout=0.0, + ) + for _ in range(depth) + ] + ) self.attn = AttnBlock(mid_channels) - self.res_block2 = nn.ModuleList([ResnetBlock(in_channels=mid_channels, - out_channels=mid_channels, - temb_channels=0, - dropout=0.0) for _ in range(depth)]) + self.res_block2 = nn.ModuleList( + [ + ResnetBlock( + in_channels=mid_channels, + out_channels=mid_channels, + temb_channels=0, + dropout=0.0, + ) + for _ in range(depth) + ] + ) - self.conv_out = nn.Conv2d(mid_channels, - out_channels, - kernel_size=1, - ) + self.conv_out = nn.Conv2d( + mid_channels, + out_channels, + kernel_size=1, + ) def forward(self, x): x = self.conv_in(x) for block in self.res_block1: x = block(x, None) - x = torch.nn.functional.interpolate(x, size=(int(round(x.shape[2]*self.factor)), int(round(x.shape[3]*self.factor)))) + x = torch.nn.functional.interpolate( + x, + size=( + int(round(x.shape[2] * self.factor)), + int(round(x.shape[3] * self.factor)), + ), + ) x = self.attn(x) for block in self.res_block2: x = block(x, None) @@ -750,17 +860,42 @@ class LatentRescaler(nn.Module): class MergedRescaleEncoder(nn.Module): - def __init__(self, in_channels, ch, resolution, out_ch, num_res_blocks, - attn_resolutions, dropout=0.0, resamp_with_conv=True, - ch_mult=(1,2,4,8), rescale_factor=1.0, rescale_module_depth=1): + def __init__( + self, + in_channels, + ch, + resolution, + out_ch, + num_res_blocks, + attn_resolutions, + dropout=0.0, + resamp_with_conv=True, + ch_mult=(1, 2, 4, 8), + rescale_factor=1.0, + rescale_module_depth=1, + ): super().__init__() intermediate_chn = ch * ch_mult[-1] - self.encoder = Encoder(in_channels=in_channels, num_res_blocks=num_res_blocks, ch=ch, ch_mult=ch_mult, - z_channels=intermediate_chn, double_z=False, resolution=resolution, - attn_resolutions=attn_resolutions, dropout=dropout, resamp_with_conv=resamp_with_conv, - out_ch=None) - self.rescaler = LatentRescaler(factor=rescale_factor, in_channels=intermediate_chn, - mid_channels=intermediate_chn, out_channels=out_ch, depth=rescale_module_depth) + self.encoder = Encoder( + in_channels=in_channels, + num_res_blocks=num_res_blocks, + ch=ch, + ch_mult=ch_mult, + z_channels=intermediate_chn, + double_z=False, + resolution=resolution, + attn_resolutions=attn_resolutions, + dropout=dropout, + resamp_with_conv=resamp_with_conv, + out_ch=None, + ) + self.rescaler = LatentRescaler( + factor=rescale_factor, + in_channels=intermediate_chn, + mid_channels=intermediate_chn, + out_channels=out_ch, + depth=rescale_module_depth, + ) def forward(self, x): x = self.encoder(x) @@ -769,15 +904,41 @@ class MergedRescaleEncoder(nn.Module): class MergedRescaleDecoder(nn.Module): - def __init__(self, z_channels, out_ch, resolution, num_res_blocks, attn_resolutions, ch, ch_mult=(1,2,4,8), - dropout=0.0, resamp_with_conv=True, rescale_factor=1.0, rescale_module_depth=1): + def __init__( + self, + z_channels, + out_ch, + resolution, + num_res_blocks, + attn_resolutions, + ch, + ch_mult=(1, 2, 4, 8), + dropout=0.0, + resamp_with_conv=True, + rescale_factor=1.0, + rescale_module_depth=1, + ): super().__init__() - tmp_chn = z_channels*ch_mult[-1] - self.decoder = Decoder(out_ch=out_ch, z_channels=tmp_chn, attn_resolutions=attn_resolutions, dropout=dropout, - resamp_with_conv=resamp_with_conv, in_channels=None, num_res_blocks=num_res_blocks, - ch_mult=ch_mult, resolution=resolution, ch=ch) - self.rescaler = LatentRescaler(factor=rescale_factor, in_channels=z_channels, mid_channels=tmp_chn, - out_channels=tmp_chn, depth=rescale_module_depth) + tmp_chn = z_channels * ch_mult[-1] + self.decoder = Decoder( + out_ch=out_ch, + z_channels=tmp_chn, + attn_resolutions=attn_resolutions, + dropout=dropout, + resamp_with_conv=resamp_with_conv, + in_channels=None, + num_res_blocks=num_res_blocks, + ch_mult=ch_mult, + resolution=resolution, + ch=ch, + ) + self.rescaler = LatentRescaler( + factor=rescale_factor, + in_channels=z_channels, + mid_channels=tmp_chn, + out_channels=tmp_chn, + depth=rescale_module_depth, + ) def forward(self, x): x = self.rescaler(x) @@ -789,14 +950,27 @@ class Upsampler(nn.Module): def __init__(self, in_size, out_size, in_channels, out_channels, ch_mult=2): super().__init__() assert out_size >= in_size - num_blocks = int(np.log2(out_size//in_size))+1 - factor_up = 1.+ (out_size % in_size) - print(f"Building {self.__class__.__name__} with in_size: {in_size} --> out_size {out_size} and factor {factor_up}") - self.rescaler = LatentRescaler(factor=factor_up, in_channels=in_channels, mid_channels=2*in_channels, - out_channels=in_channels) - self.decoder = Decoder(out_ch=out_channels, resolution=out_size, z_channels=in_channels, num_res_blocks=2, - attn_resolutions=[], in_channels=None, ch=in_channels, - ch_mult=[ch_mult for _ in range(num_blocks)]) + num_blocks = int(np.log2(out_size // in_size)) + 1 + factor_up = 1.0 + (out_size % in_size) + print( + f"Building {self.__class__.__name__} with in_size: {in_size} --> out_size {out_size} and factor {factor_up}" + ) + self.rescaler = LatentRescaler( + factor=factor_up, + in_channels=in_channels, + mid_channels=2 * in_channels, + out_channels=in_channels, + ) + self.decoder = Decoder( + out_ch=out_channels, + resolution=out_size, + z_channels=in_channels, + num_res_blocks=2, + attn_resolutions=[], + in_channels=None, + ch=in_channels, + ch_mult=[ch_mult for _ in range(num_blocks)], + ) def forward(self, x): x = self.rescaler(x) @@ -810,37 +984,47 @@ class Resize(nn.Module): self.with_conv = learned self.mode = mode if self.with_conv: - print(f"Note: {self.__class__.__name} uses learned downsampling and will ignore the fixed {mode} mode") + print( + f"Note: {self.__class__.__name} uses learned downsampling and will ignore the fixed {mode} mode" + ) raise NotImplementedError() assert in_channels is not None # no asymmetric padding in torch conv, must do it ourselves - self.conv = torch.nn.Conv2d(in_channels, - in_channels, - kernel_size=4, - stride=2, - padding=1) + self.conv = torch.nn.Conv2d( + in_channels, in_channels, kernel_size=4, stride=2, padding=1 + ) def forward(self, x, scale_factor=1.0): - if scale_factor==1.0: + if scale_factor == 1.0: return x else: - x = torch.nn.functional.interpolate(x, mode=self.mode, align_corners=False, scale_factor=scale_factor) + x = torch.nn.functional.interpolate( + x, mode=self.mode, align_corners=False, scale_factor=scale_factor + ) return x -class FirstStagePostProcessor(nn.Module): - def __init__(self, ch_mult:list, in_channels, - pretrained_model:nn.Module=None, - reshape=False, - n_channels=None, - dropout=0., - pretrained_config=None): +class FirstStagePostProcessor(nn.Module): + def __init__( + self, + ch_mult: list, + in_channels, + pretrained_model: nn.Module = None, + reshape=False, + n_channels=None, + dropout=0.0, + pretrained_config=None, + ): super().__init__() if pretrained_config is None: - assert pretrained_model is not None, 'Either "pretrained_model" or "pretrained_config" must not be None' + assert ( + pretrained_model is not None + ), 'Either "pretrained_model" or "pretrained_config" must not be None' self.pretrained_model = pretrained_model else: - assert pretrained_config is not None, 'Either "pretrained_model" or "pretrained_config" must not be None' + assert ( + pretrained_config is not None + ), 'Either "pretrained_model" or "pretrained_config" must not be None' self.instantiate_pretrained(pretrained_config) self.do_reshape = reshape @@ -848,22 +1032,26 @@ class FirstStagePostProcessor(nn.Module): if n_channels is None: n_channels = self.pretrained_model.encoder.ch - self.proj_norm = Normalize(in_channels,num_groups=in_channels//2) - self.proj = nn.Conv2d(in_channels,n_channels,kernel_size=3, - stride=1,padding=1) + self.proj_norm = Normalize(in_channels, num_groups=in_channels // 2) + self.proj = nn.Conv2d( + in_channels, n_channels, kernel_size=3, stride=1, padding=1 + ) blocks = [] downs = [] ch_in = n_channels for m in ch_mult: - blocks.append(ResnetBlock(in_channels=ch_in,out_channels=m*n_channels,dropout=dropout)) + blocks.append( + ResnetBlock( + in_channels=ch_in, out_channels=m * n_channels, dropout=dropout + ) + ) ch_in = m * n_channels downs.append(Downsample(ch_in, with_conv=False)) self.model = nn.ModuleList(blocks) self.downsampler = nn.ModuleList(downs) - def instantiate_pretrained(self, config): model = instantiate_from_config(config) self.pretrained_model = model.eval() @@ -871,24 +1059,23 @@ class FirstStagePostProcessor(nn.Module): for param in self.pretrained_model.parameters(): param.requires_grad = False - @torch.no_grad() - def encode_with_pretrained(self,x): + def encode_with_pretrained(self, x): c = self.pretrained_model.encode(x) if isinstance(c, DiagonalGaussianDistribution): c = c.mode() - return c + return c - def forward(self,x): + def forward(self, x): z_fs = self.encode_with_pretrained(x) z = self.proj_norm(z_fs) z = self.proj(z) z = silu(z) - for submodel, downmodel in zip(self.model,self.downsampler): - z = submodel(z,temb=None) + for submodel, downmodel in zip(self.model, self.downsampler): + z = submodel(z, temb=None) z = downmodel(z) if self.do_reshape: - z = rearrange(z,'b c h w -> b (h w) c') + z = rearrange(z, "b c h w -> b (h w) c") return z diff --git a/invokeai/backend/stable_diffusion/diffusionmodules/openaimodel.py b/invokeai/backend/stable_diffusion/diffusionmodules/openaimodel.py index d6baa76a1c..867a1a30ca 100644 --- a/invokeai/backend/stable_diffusion/diffusionmodules/openaimodel.py +++ b/invokeai/backend/stable_diffusion/diffusionmodules/openaimodel.py @@ -1,23 +1,22 @@ +import math from abc import abstractmethod from functools import partial -import math from typing import Iterable import numpy as np import torch as th import torch.nn as nn import torch.nn.functional as F - +from ldm.modules.attention import SpatialTransformer from ldm.modules.diffusionmodules.util import ( + avg_pool_nd, checkpoint, conv_nd, linear, - avg_pool_nd, - zero_module, normalization, timestep_embedding, + zero_module, ) -from ldm.modules.attention import SpatialTransformer # dummy replace @@ -100,9 +99,7 @@ class Upsample(nn.Module): upsampling occurs in the inner-two dimensions. """ - def __init__( - self, channels, use_conv, dims=2, out_channels=None, padding=1 - ): + def __init__(self, channels, use_conv, dims=2, out_channels=None, padding=1): super().__init__() self.channels = channels self.out_channels = out_channels or channels @@ -117,10 +114,10 @@ class Upsample(nn.Module): assert x.shape[1] == self.channels if self.dims == 3: x = F.interpolate( - x, (x.shape[2], x.shape[3] * 2, x.shape[4] * 2), mode='nearest' + x, (x.shape[2], x.shape[3] * 2, x.shape[4] * 2), mode="nearest" ) else: - x = F.interpolate(x, scale_factor=2, mode='nearest') + x = F.interpolate(x, scale_factor=2, mode="nearest") if self.use_conv: x = self.conv(x) return x @@ -151,9 +148,7 @@ class Downsample(nn.Module): downsampling occurs in the inner-two dimensions. """ - def __init__( - self, channels, use_conv, dims=2, out_channels=None, padding=1 - ): + def __init__(self, channels, use_conv, dims=2, out_channels=None, padding=1): super().__init__() self.channels = channels self.out_channels = out_channels or channels @@ -237,9 +232,7 @@ class ResBlock(TimestepBlock): nn.SiLU(), linear( emb_channels, - 2 * self.out_channels - if use_scale_shift_norm - else self.out_channels, + 2 * self.out_channels if use_scale_shift_norm else self.out_channels, ), ) self.out_layers = nn.Sequential( @@ -247,9 +240,7 @@ class ResBlock(TimestepBlock): nn.SiLU(), nn.Dropout(p=dropout), zero_module( - conv_nd( - dims, self.out_channels, self.out_channels, 3, padding=1 - ) + conv_nd(dims, self.out_channels, self.out_channels, 3, padding=1) ), ) @@ -260,9 +251,7 @@ class ResBlock(TimestepBlock): dims, channels, self.out_channels, 3, padding=1 ) else: - self.skip_connection = conv_nd( - dims, channels, self.out_channels, 1 - ) + self.skip_connection = conv_nd(dims, channels, self.out_channels, 1) def forward(self, x, emb): """ @@ -320,7 +309,7 @@ class AttentionBlock(nn.Module): else: assert ( channels % num_head_channels == 0 - ), f'q,k,v channels {channels} is not divisible by num_head_channels {num_head_channels}' + ), f"q,k,v channels {channels} is not divisible by num_head_channels {num_head_channels}" self.num_heads = channels // num_head_channels self.use_checkpoint = use_checkpoint self.norm = normalization(channels) @@ -337,7 +326,7 @@ class AttentionBlock(nn.Module): def forward(self, x): return checkpoint( self._forward, (x,), self.parameters(), True - ) # TODO: check checkpoint usage, is True # TODO: fix the .half call!!! + ) # TODO: check checkpoint usage, is True # TODO: fix the .half call!!! # return pt_checkpoint(self._forward, x) # pytorch def _forward(self, x): @@ -387,15 +376,13 @@ class QKVAttentionLegacy(nn.Module): bs, width, length = qkv.shape assert width % (3 * self.n_heads) == 0 ch = width // (3 * self.n_heads) - q, k, v = qkv.reshape(bs * self.n_heads, ch * 3, length).split( - ch, dim=1 - ) + q, k, v = qkv.reshape(bs * self.n_heads, ch * 3, length).split(ch, dim=1) scale = 1 / math.sqrt(math.sqrt(ch)) weight = th.einsum( - 'bct,bcs->bts', q * scale, k * scale + "bct,bcs->bts", q * scale, k * scale ) # More stable with f16 than dividing afterwards weight = th.softmax(weight.float(), dim=-1).type(weight.dtype) - a = th.einsum('bts,bcs->bct', weight, v) + a = th.einsum("bts,bcs->bct", weight, v) return a.reshape(bs, -1, length) @staticmethod @@ -424,14 +411,12 @@ class QKVAttention(nn.Module): q, k, v = qkv.chunk(3, dim=1) scale = 1 / math.sqrt(math.sqrt(ch)) weight = th.einsum( - 'bct,bcs->bts', + "bct,bcs->bts", (q * scale).view(bs * self.n_heads, ch, length), (k * scale).view(bs * self.n_heads, ch, length), ) # More stable with f16 than dividing afterwards weight = th.softmax(weight.float(), dim=-1).type(weight.dtype) - a = th.einsum( - 'bts,bcs->bct', weight, v.reshape(bs * self.n_heads, ch, length) - ) + a = th.einsum("bts,bcs->bct", weight, v.reshape(bs * self.n_heads, ch, length)) return a.reshape(bs, -1, length) @staticmethod @@ -500,12 +485,12 @@ class UNetModel(nn.Module): if use_spatial_transformer: assert ( context_dim is not None - ), 'Fool!! You forgot to include the dimension of your cross-attention conditioning...' + ), "Fool!! You forgot to include the dimension of your cross-attention conditioning..." if context_dim is not None: assert ( use_spatial_transformer - ), 'Fool!! You forgot to use the spatial transformer for your cross-attention conditioning...' + ), "Fool!! You forgot to use the spatial transformer for your cross-attention conditioning..." from omegaconf.listconfig import ListConfig if type(context_dim) == ListConfig: @@ -517,12 +502,12 @@ class UNetModel(nn.Module): if num_heads == -1: assert ( num_head_channels != -1 - ), 'Either num_heads or num_head_channels has to be set' + ), "Either num_heads or num_head_channels has to be set" if num_head_channels == -1: assert ( num_heads != -1 - ), 'Either num_heads or num_head_channels has to be set' + ), "Either num_heads or num_head_channels has to be set" self.image_size = image_size self.in_channels = in_channels @@ -641,11 +626,7 @@ class UNetModel(nn.Module): dim_head = num_head_channels if legacy: # num_heads = 1 - dim_head = ( - ch // num_heads - if use_spatial_transformer - else num_head_channels - ) + dim_head = ch // num_heads if use_spatial_transformer else num_head_channels self.middle_block = TimestepEmbedSequential( ResBlock( ch, @@ -741,9 +722,7 @@ class UNetModel(nn.Module): up=True, ) if resblock_updown - else Upsample( - ch, conv_resample, dims=dims, out_channels=out_ch - ) + else Upsample(ch, conv_resample, dims=dims, out_channels=out_ch) ) ds //= 2 self.output_blocks.append(TimestepEmbedSequential(*layers)) @@ -752,9 +731,7 @@ class UNetModel(nn.Module): self.out = nn.Sequential( normalization(ch), nn.SiLU(), - zero_module( - conv_nd(dims, model_channels, out_channels, 3, padding=1) - ), + zero_module(conv_nd(dims, model_channels, out_channels, 3, padding=1)), ) if self.predict_codebook_ids: self.id_predictor = nn.Sequential( @@ -790,11 +767,9 @@ class UNetModel(nn.Module): """ assert (y is not None) == ( self.num_classes is not None - ), 'must specify y if and only if the model is class-conditional' + ), "must specify y if and only if the model is class-conditional" hs = [] - t_emb = timestep_embedding( - timesteps, self.model_channels, repeat_only=False - ) + t_emb = timestep_embedding(timesteps, self.model_channels, repeat_only=False) emb = self.time_embed(t_emb) if self.num_classes is not None: @@ -842,7 +817,7 @@ class EncoderUNetModel(nn.Module): use_scale_shift_norm=False, resblock_updown=False, use_new_attention_order=False, - pool='adaptive', + pool="adaptive", *args, **kwargs, ): @@ -962,7 +937,7 @@ class EncoderUNetModel(nn.Module): ) self._feature_size += ch self.pool = pool - if pool == 'adaptive': + if pool == "adaptive": self.out = nn.Sequential( normalization(ch), nn.SiLU(), @@ -970,7 +945,7 @@ class EncoderUNetModel(nn.Module): zero_module(conv_nd(dims, ch, out_channels, 1)), nn.Flatten(), ) - elif pool == 'attention': + elif pool == "attention": assert num_head_channels != -1 self.out = nn.Sequential( normalization(ch), @@ -979,13 +954,13 @@ class EncoderUNetModel(nn.Module): (image_size // ds), ch, num_head_channels, out_channels ), ) - elif pool == 'spatial': + elif pool == "spatial": self.out = nn.Sequential( nn.Linear(self._feature_size, 2048), nn.ReLU(), nn.Linear(2048, self.out_channels), ) - elif pool == 'spatial_v2': + elif pool == "spatial_v2": self.out = nn.Sequential( nn.Linear(self._feature_size, 2048), normalization(2048), @@ -993,7 +968,7 @@ class EncoderUNetModel(nn.Module): nn.Linear(2048, self.out_channels), ) else: - raise NotImplementedError(f'Unexpected {pool} pooling') + raise NotImplementedError(f"Unexpected {pool} pooling") def convert_to_fp16(self): """ @@ -1016,18 +991,16 @@ class EncoderUNetModel(nn.Module): :param timesteps: a 1-D batch of timesteps. :return: an [N x K] Tensor of outputs. """ - emb = self.time_embed( - timestep_embedding(timesteps, self.model_channels) - ) + emb = self.time_embed(timestep_embedding(timesteps, self.model_channels)) results = [] h = x.type(self.dtype) for module in self.input_blocks: h = module(h, emb) - if self.pool.startswith('spatial'): + if self.pool.startswith("spatial"): results.append(h.type(x.dtype).mean(dim=(2, 3))) h = self.middle_block(h, emb) - if self.pool.startswith('spatial'): + if self.pool.startswith("spatial"): results.append(h.type(x.dtype).mean(dim=(2, 3))) h = th.cat(results, axis=-1) return self.out(h) diff --git a/invokeai/backend/stable_diffusion/diffusionmodules/util.py b/invokeai/backend/stable_diffusion/diffusionmodules/util.py index a23f1abba4..b71b0f06f9 100644 --- a/invokeai/backend/stable_diffusion/diffusionmodules/util.py +++ b/invokeai/backend/stable_diffusion/diffusionmodules/util.py @@ -8,11 +8,12 @@ # thanks! -import os import math +import os + +import numpy as np import torch import torch.nn as nn -import numpy as np from einops import repeat from ...util.util import instantiate_from_config @@ -21,7 +22,7 @@ from ...util.util import instantiate_from_config def make_beta_schedule( schedule, n_timestep, linear_start=1e-4, linear_end=2e-2, cosine_s=8e-3 ): - if schedule == 'linear': + if schedule == "linear": betas = ( torch.linspace( linear_start**0.5, @@ -32,10 +33,9 @@ def make_beta_schedule( ** 2 ) - elif schedule == 'cosine': + elif schedule == "cosine": timesteps = ( - torch.arange(n_timestep + 1, dtype=torch.float64) / n_timestep - + cosine_s + torch.arange(n_timestep + 1, dtype=torch.float64) / n_timestep + cosine_s ) alphas = timesteps / (1 + cosine_s) * np.pi / 2 alphas = torch.cos(alphas).pow(2) @@ -43,15 +43,13 @@ def make_beta_schedule( betas = 1 - alphas[1:] / alphas[:-1] betas = np.clip(betas, a_min=0, a_max=0.999) - elif schedule == 'sqrt_linear': + elif schedule == "sqrt_linear": betas = torch.linspace( linear_start, linear_end, n_timestep, dtype=torch.float64 ) - elif schedule == 'sqrt': + elif schedule == "sqrt": betas = ( - torch.linspace( - linear_start, linear_end, n_timestep, dtype=torch.float64 - ) + torch.linspace(linear_start, linear_end, n_timestep, dtype=torch.float64) ** 0.5 ) else: @@ -62,19 +60,14 @@ def make_beta_schedule( def make_ddim_timesteps( ddim_discr_method, num_ddim_timesteps, num_ddpm_timesteps, verbose=True ): - if ddim_discr_method == 'uniform': + if ddim_discr_method == "uniform": c = num_ddpm_timesteps // num_ddim_timesteps if c < 1: - c = 1 + c = 1 ddim_timesteps = (np.arange(0, num_ddim_timesteps) * c).astype(int) - elif ddim_discr_method == 'quad': + elif ddim_discr_method == "quad": ddim_timesteps = ( - ( - np.linspace( - 0, np.sqrt(num_ddpm_timesteps * 0.8), num_ddim_timesteps - ) - ) - ** 2 + (np.linspace(0, np.sqrt(num_ddpm_timesteps * 0.8), num_ddim_timesteps)) ** 2 ).astype(int) else: raise NotImplementedError( @@ -87,18 +80,14 @@ def make_ddim_timesteps( # steps_out = ddim_timesteps if verbose: - print(f'Selected timesteps for ddim sampler: {steps_out}') + print(f"Selected timesteps for ddim sampler: {steps_out}") return steps_out -def make_ddim_sampling_parameters( - alphacums, ddim_timesteps, eta, verbose=True -): +def make_ddim_sampling_parameters(alphacums, ddim_timesteps, eta, verbose=True): # select alphas for computing the variance schedule alphas = alphacums[ddim_timesteps] - alphas_prev = np.asarray( - [alphacums[0]] + alphacums[ddim_timesteps[:-1]].tolist() - ) + alphas_prev = np.asarray([alphacums[0]] + alphacums[ddim_timesteps[:-1]].tolist()) # according the the formula provided in https://arxiv.org/abs/2010.02502 sigmas = eta * np.sqrt( @@ -106,11 +95,11 @@ def make_ddim_sampling_parameters( ) if verbose: print( - f'Selected alphas for ddim sampler: a_t: {alphas}; a_(t-1): {alphas_prev}' + f"Selected alphas for ddim sampler: a_t: {alphas}; a_(t-1): {alphas_prev}" ) print( - f'For the chosen value of eta, which is {eta}, ' - f'this results in the following sigma_t schedule for ddim sampler {sigmas}' + f"For the chosen value of eta, which is {eta}, " + f"this results in the following sigma_t schedule for ddim sampler {sigmas}" ) return sigmas, alphas, alphas_prev @@ -150,9 +139,7 @@ def checkpoint(func, inputs, params, flag): explicitly take as arguments. :param flag: if False, disable gradient checkpointing. """ - if ( - False - ): # disabled checkpointing to allow requires_grad = False for main model + if False: # disabled checkpointing to allow requires_grad = False for main model args = tuple(inputs) + tuple(params) return CheckpointFunction.apply(func, len(inputs), *args) else: @@ -172,9 +159,7 @@ class CheckpointFunction(torch.autograd.Function): @staticmethod def backward(ctx, *output_grads): - ctx.input_tensors = [ - x.detach().requires_grad_(True) for x in ctx.input_tensors - ] + ctx.input_tensors = [x.detach().requires_grad_(True) for x in ctx.input_tensors] with torch.enable_grad(): # Fixes a bug where the first op in run_function modifies the # Tensor storage in place, which is not allowed for detach()'d @@ -216,7 +201,7 @@ def timestep_embedding(timesteps, dim, max_period=10000, repeat_only=False): [embedding, torch.zeros_like(embedding[:, :1])], dim=-1 ) else: - embedding = repeat(timesteps, 'b -> b d', d=dim) + embedding = repeat(timesteps, "b -> b d", d=dim) return embedding @@ -269,7 +254,7 @@ def conv_nd(dims, *args, **kwargs): return nn.Conv2d(*args, **kwargs) elif dims == 3: return nn.Conv3d(*args, **kwargs) - raise ValueError(f'unsupported dimensions: {dims}') + raise ValueError(f"unsupported dimensions: {dims}") def linear(*args, **kwargs): @@ -289,21 +274,19 @@ def avg_pool_nd(dims, *args, **kwargs): return nn.AvgPool2d(*args, **kwargs) elif dims == 3: return nn.AvgPool3d(*args, **kwargs) - raise ValueError(f'unsupported dimensions: {dims}') + raise ValueError(f"unsupported dimensions: {dims}") class HybridConditioner(nn.Module): def __init__(self, c_concat_config, c_crossattn_config): super().__init__() self.concat_conditioner = instantiate_from_config(c_concat_config) - self.crossattn_conditioner = instantiate_from_config( - c_crossattn_config - ) + self.crossattn_conditioner = instantiate_from_config(c_crossattn_config) def forward(self, c_concat, c_crossattn): c_concat = self.concat_conditioner(c_concat) c_crossattn = self.crossattn_conditioner(c_crossattn) - return {'c_concat': [c_concat], 'c_crossattn': [c_crossattn]} + return {"c_concat": [c_concat], "c_crossattn": [c_crossattn]} def noise_like(shape, device, repeat=False): diff --git a/invokeai/backend/stable_diffusion/distributions/distributions.py b/invokeai/backend/stable_diffusion/distributions/distributions.py index 67ed535791..016be35523 100644 --- a/invokeai/backend/stable_diffusion/distributions/distributions.py +++ b/invokeai/backend/stable_diffusion/distributions/distributions.py @@ -1,5 +1,5 @@ -import torch import numpy as np +import torch class AbstractDistribution: @@ -64,9 +64,7 @@ class DiagonalGaussianDistribution(object): return torch.Tensor([0.0]) logtwopi = np.log(2.0 * np.pi) return 0.5 * torch.sum( - logtwopi - + self.logvar - + torch.pow(sample - self.mean, 2) / self.var, + logtwopi + self.logvar + torch.pow(sample - self.mean, 2) / self.var, dim=dims, ) @@ -86,7 +84,7 @@ def normal_kl(mean1, logvar1, mean2, logvar2): if isinstance(obj, torch.Tensor): tensor = obj break - assert tensor is not None, 'at least one argument must be a Tensor' + assert tensor is not None, "at least one argument must be a Tensor" # Force variances to be Tensors. Broadcasting helps convert scalars to # Tensors, but it does not work for torch.exp(). diff --git a/invokeai/backend/stable_diffusion/ema.py b/invokeai/backend/stable_diffusion/ema.py index 2ceec5f0e7..880ca3d205 100644 --- a/invokeai/backend/stable_diffusion/ema.py +++ b/invokeai/backend/stable_diffusion/ema.py @@ -6,12 +6,12 @@ class LitEma(nn.Module): def __init__(self, model, decay=0.9999, use_num_upates=True): super().__init__() if decay < 0.0 or decay > 1.0: - raise ValueError('Decay must be between 0 and 1') + raise ValueError("Decay must be between 0 and 1") self.m_name2s_name = {} - self.register_buffer('decay', torch.tensor(decay, dtype=torch.float32)) + self.register_buffer("decay", torch.tensor(decay, dtype=torch.float32)) self.register_buffer( - 'num_updates', + "num_updates", torch.tensor(0, dtype=torch.int) if use_num_upates else torch.tensor(-1, dtype=torch.int), @@ -20,7 +20,7 @@ class LitEma(nn.Module): for name, p in model.named_parameters(): if p.requires_grad: # remove as '.'-character is not allowed in buffers - s_name = name.replace('.', '') + s_name = name.replace(".", "") self.m_name2s_name.update({name: s_name}) self.register_buffer(s_name, p.clone().detach().data) @@ -31,9 +31,7 @@ class LitEma(nn.Module): if self.num_updates >= 0: self.num_updates += 1 - decay = min( - self.decay, (1 + self.num_updates) / (10 + self.num_updates) - ) + decay = min(self.decay, (1 + self.num_updates) / (10 + self.num_updates)) one_minus_decay = 1.0 - decay @@ -44,9 +42,7 @@ class LitEma(nn.Module): for key in m_param: if m_param[key].requires_grad: sname = self.m_name2s_name[key] - shadow_params[sname] = shadow_params[sname].type_as( - m_param[key] - ) + shadow_params[sname] = shadow_params[sname].type_as(m_param[key]) shadow_params[sname].sub_( one_minus_decay * (shadow_params[sname] - m_param[key]) ) @@ -58,9 +54,7 @@ class LitEma(nn.Module): shadow_params = dict(self.named_buffers()) for key in m_param: if m_param[key].requires_grad: - m_param[key].data.copy_( - shadow_params[self.m_name2s_name[key]].data - ) + m_param[key].data.copy_(shadow_params[self.m_name2s_name[key]].data) else: assert not key in self.m_name2s_name diff --git a/invokeai/backend/stable_diffusion/encoders/modules.py b/invokeai/backend/stable_diffusion/encoders/modules.py index 3c20b8d119..54afd12bc9 100644 --- a/invokeai/backend/stable_diffusion/encoders/modules.py +++ b/invokeai/backend/stable_diffusion/encoders/modules.py @@ -7,14 +7,14 @@ import kornia import torch import torch.nn as nn from einops import repeat -from transformers import CLIPTokenizer, CLIPTextModel +from transformers import CLIPTextModel, CLIPTokenizer -from ldm.invoke.devices import choose_torch_device -from invokeai.backend.globals import global_cache_dir -from ldm.modules.x_transformer import ( +from ...util import choose_torch_device +from ..globals import global_cache_dir +from ..x_transformer import ( # TODO: can we directly rely on lucidrains code and simply add this as a reuirement? --> test Encoder, TransformerWrapper, -) # TODO: can we directly rely on lucidrains code and simply add this as a reuirement? --> test +) def _expand_mask(mask, dtype, tgt_len=None): @@ -24,9 +24,7 @@ def _expand_mask(mask, dtype, tgt_len=None): bsz, src_len = mask.size() tgt_len = tgt_len if tgt_len is not None else src_len - expanded_mask = ( - mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype) - ) + expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype) inverted_mask = 1.0 - expanded_mask @@ -54,7 +52,7 @@ class AbstractEncoder(nn.Module): class ClassEmbedder(nn.Module): - def __init__(self, embed_dim, n_classes=1000, key='class'): + def __init__(self, embed_dim, n_classes=1000, key="class"): super().__init__() self.key = key self.embedding = nn.Embedding(n_classes, embed_dim) @@ -99,20 +97,14 @@ class TransformerEmbedder(AbstractEncoder): class BERTTokenizer(AbstractEncoder): """Uses a pretrained BERT tokenizer by huggingface. Vocab size: 30522 (?)""" - def __init__( - self, device=choose_torch_device(), vq_interface=True, max_length=77 - ): + def __init__(self, device=choose_torch_device(), vq_interface=True, max_length=77): super().__init__() - from transformers import ( - BertTokenizerFast, - ) + from transformers import BertTokenizerFast - cache = global_cache_dir('hub') + cache = global_cache_dir("hub") try: self.tokenizer = BertTokenizerFast.from_pretrained( - 'bert-base-uncased', - cache_dir=cache, - local_files_only=True + "bert-base-uncased", cache_dir=cache, local_files_only=True ) except OSError: raise SystemExit( @@ -129,10 +121,10 @@ class BERTTokenizer(AbstractEncoder): max_length=self.max_length, return_length=True, return_overflowing_tokens=False, - padding='max_length', - return_tensors='pt', + padding="max_length", + return_tensors="pt", ) - tokens = batch_encoding['input_ids'].to(self.device) + tokens = batch_encoding["input_ids"].to(self.device) return tokens @torch.no_grad() @@ -150,21 +142,19 @@ class BERTEmbedder(AbstractEncoder): """Uses the BERT tokenizr model and add some transformer encoder layers""" def __init__( - self, - n_embed, - n_layer, - vocab_size=30522, - max_seq_len=77, - device=choose_torch_device(), - use_tokenizer=True, - embedding_dropout=0.0, + self, + n_embed, + n_layer, + vocab_size=30522, + max_seq_len=77, + device=choose_torch_device(), + use_tokenizer=True, + embedding_dropout=0.0, ): super().__init__() self.use_tknz_fn = use_tokenizer if self.use_tknz_fn: - self.tknz_fn = BERTTokenizer( - vq_interface=False, max_length=max_seq_len - ) + self.tknz_fn = BERTTokenizer(vq_interface=False, max_length=max_seq_len) self.device = device self.transformer = TransformerWrapper( num_tokens=vocab_size, @@ -192,7 +182,7 @@ class SpatialRescaler(nn.Module): def __init__( self, n_stages=1, - method='bilinear', + method="bilinear", multiplier=0.5, in_channels=3, out_channels=None, @@ -202,25 +192,21 @@ class SpatialRescaler(nn.Module): self.n_stages = n_stages assert self.n_stages >= 0 assert method in [ - 'nearest', - 'linear', - 'bilinear', - 'trilinear', - 'bicubic', - 'area', + "nearest", + "linear", + "bilinear", + "trilinear", + "bicubic", + "area", ] self.multiplier = multiplier - self.interpolator = partial( - torch.nn.functional.interpolate, mode=method - ) + self.interpolator = partial(torch.nn.functional.interpolate, mode=method) self.remap_output = out_channels is not None if self.remap_output: print( - f'Spatial Rescaler mapping from {in_channels} to {out_channels} channels after resizing.' - ) - self.channel_mapper = nn.Conv2d( - in_channels, out_channels, 1, bias=bias + f"Spatial Rescaler mapping from {in_channels} to {out_channels} channels after resizing." ) + self.channel_mapper = nn.Conv2d(in_channels, out_channels, 1, bias=bias) def forward(self, x): for stage in range(self.n_stages): @@ -236,27 +222,24 @@ class SpatialRescaler(nn.Module): class FrozenCLIPEmbedder(AbstractEncoder): """Uses the CLIP transformer encoder for text (from Hugging Face)""" + tokenizer: CLIPTokenizer transformer: CLIPTextModel def __init__( self, - version:str='openai/clip-vit-large-patch14', - max_length:int=77, - tokenizer:Optional[CLIPTokenizer]=None, - transformer:Optional[CLIPTextModel]=None, + version: str = "openai/clip-vit-large-patch14", + max_length: int = 77, + tokenizer: Optional[CLIPTokenizer] = None, + transformer: Optional[CLIPTextModel] = None, ): super().__init__() - cache = global_cache_dir('hub') + cache = global_cache_dir("hub") self.tokenizer = tokenizer or CLIPTokenizer.from_pretrained( - version, - cache_dir=cache, - local_files_only=True + version, cache_dir=cache, local_files_only=True ) self.transformer = transformer or CLIPTextModel.from_pretrained( - version, - cache_dir=cache, - local_files_only=True + version, cache_dir=cache, local_files_only=True ) self.max_length = max_length self.freeze() @@ -268,7 +251,6 @@ class FrozenCLIPEmbedder(AbstractEncoder): inputs_embeds=None, embedding_manager=None, ) -> torch.Tensor: - seq_length = ( input_ids.shape[-1] if input_ids is not None @@ -289,8 +271,8 @@ class FrozenCLIPEmbedder(AbstractEncoder): return embeddings - self.transformer.text_model.embeddings.forward = ( - embedding_forward.__get__(self.transformer.text_model.embeddings) + self.transformer.text_model.embeddings.forward = embedding_forward.__get__( + self.transformer.text_model.embeddings ) def encoder_forward( @@ -313,9 +295,7 @@ class FrozenCLIPEmbedder(AbstractEncoder): else self.config.output_hidden_states ) return_dict = ( - return_dict - if return_dict is not None - else self.config.use_return_dict + return_dict if return_dict is not None else self.config.use_return_dict ) encoder_states = () if output_hidden_states else None @@ -368,13 +348,11 @@ class FrozenCLIPEmbedder(AbstractEncoder): else self.config.output_hidden_states ) return_dict = ( - return_dict - if return_dict is not None - else self.config.use_return_dict + return_dict if return_dict is not None else self.config.use_return_dict ) if input_ids is None: - raise ValueError('You have to specify either input_ids') + raise ValueError("You have to specify either input_ids") input_shape = input_ids.size() input_ids = input_ids.view(-1, input_shape[-1]) @@ -395,9 +373,7 @@ class FrozenCLIPEmbedder(AbstractEncoder): # expand attention_mask if attention_mask is not None: # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len] - attention_mask = _expand_mask( - attention_mask, hidden_states.dtype - ) + attention_mask = _expand_mask(attention_mask, hidden_states.dtype) last_hidden_state = self.encoder( inputs_embeds=hidden_states, @@ -436,9 +412,7 @@ class FrozenCLIPEmbedder(AbstractEncoder): embedding_manager=embedding_manager, ) - self.transformer.forward = transformer_forward.__get__( - self.transformer - ) + self.transformer.forward = transformer_forward.__get__(self.transformer) def freeze(self): self.transformer = self.transformer.eval() @@ -452,10 +426,10 @@ class FrozenCLIPEmbedder(AbstractEncoder): max_length=self.max_length, return_length=True, return_overflowing_tokens=False, - padding='max_length', - return_tensors='pt', + padding="max_length", + return_tensors="pt", ) - tokens = batch_encoding['input_ids'].to(self.device) + tokens = batch_encoding["input_ids"].to(self.device) z = self.transformer(input_ids=tokens, **kwargs) return z @@ -471,25 +445,25 @@ class FrozenCLIPEmbedder(AbstractEncoder): def device(self, device): self.transformer.to(device=device) -class WeightedFrozenCLIPEmbedder(FrozenCLIPEmbedder): +class WeightedFrozenCLIPEmbedder(FrozenCLIPEmbedder): fragment_weights_key = "fragment_weights" return_tokens_key = "return_tokens" - def set_textual_inversion_manager(self, manager): #TextualInversionManager): + def set_textual_inversion_manager(self, manager): # TextualInversionManager): # TODO all of the weighting and expanding stuff needs be moved out of this class self.textual_inversion_manager = manager def forward(self, text: list, **kwargs): # TODO all of the weighting and expanding stuff needs be moved out of this class - ''' + """ :param text: A batch of prompt strings, or, a batch of lists of fragments of prompt strings to which different weights shall be applied. :param kwargs: If the keyword arg "fragment_weights" is passed, it shall contain a batch of lists of weights for the prompt fragments. In this case text must contain batches of lists of prompt fragments. :return: A tensor of shape (B, 77, 768) containing weighted embeddings - ''' + """ if self.fragment_weights_key not in kwargs: # fallback to base class implementation return super().forward(text, **kwargs) @@ -507,7 +481,6 @@ class WeightedFrozenCLIPEmbedder(FrozenCLIPEmbedder): batch_z = None batch_tokens = None for fragments, weights in zip(text, fragment_weights): - # First, weight tokens in individual fragments by scaling the feature vectors as requested (effectively # applying a multiplier to the CFG scale on a per-token basis). # For tokens weighted<1, intuitively we want SD to become not merely *less* interested in the concept @@ -520,7 +493,9 @@ class WeightedFrozenCLIPEmbedder(FrozenCLIPEmbedder): # handle weights >=1 tokens, per_token_weights = self.get_tokens_and_weights(fragments, weights) - base_embedding = self.build_weighted_embedding_tensor(tokens, per_token_weights, **kwargs) + base_embedding = self.build_weighted_embedding_tensor( + tokens, per_token_weights, **kwargs + ) # this is our starting point embeddings = base_embedding.unsqueeze(0) @@ -536,12 +511,18 @@ class WeightedFrozenCLIPEmbedder(FrozenCLIPEmbedder): # such that the resulting lerped embedding is exactly half-way between "mountain man" and "mountain". for index, fragment_weight in enumerate(weights): if fragment_weight < 1: - fragments_without_this = fragments[:index] + fragments[index+1:] - weights_without_this = weights[:index] + weights[index+1:] - tokens, per_token_weights = self.get_tokens_and_weights(fragments_without_this, weights_without_this) - embedding_without_this = self.build_weighted_embedding_tensor(tokens, per_token_weights, **kwargs) + fragments_without_this = fragments[:index] + fragments[index + 1 :] + weights_without_this = weights[:index] + weights[index + 1 :] + tokens, per_token_weights = self.get_tokens_and_weights( + fragments_without_this, weights_without_this + ) + embedding_without_this = self.build_weighted_embedding_tensor( + tokens, per_token_weights, **kwargs + ) - embeddings = torch.cat((embeddings, embedding_without_this.unsqueeze(0)), dim=1) + embeddings = torch.cat( + (embeddings, embedding_without_this.unsqueeze(0)), dim=1 + ) # weight of the embedding *without* this fragment gets *stronger* as its weight approaches 0 # if fragment_weight = 0, basically we want embedding_without_this to completely overwhelm base_embedding # therefore: @@ -554,29 +535,43 @@ class WeightedFrozenCLIPEmbedder(FrozenCLIPEmbedder): # inf at PI/2 # -> tan((1-weight)*PI/2) should give us ideal lerp weights epsilon = 1e-9 - fragment_weight = max(epsilon, fragment_weight) # inf is bad - embedding_lerp_weight = math.tan((1.0 - fragment_weight) * math.pi / 2) + fragment_weight = max(epsilon, fragment_weight) # inf is bad + embedding_lerp_weight = math.tan( + (1.0 - fragment_weight) * math.pi / 2 + ) # todo handle negative weight? per_embedding_weights.append(embedding_lerp_weight) - lerped_embeddings = self.apply_embedding_weights(embeddings, per_embedding_weights, normalize=True).squeeze(0) + lerped_embeddings = self.apply_embedding_weights( + embeddings, per_embedding_weights, normalize=True + ).squeeze(0) - #print(f"assembled tokens for '{fragments}' into tensor of shape {lerped_embeddings.shape}") + # print(f"assembled tokens for '{fragments}' into tensor of shape {lerped_embeddings.shape}") # append to batch - batch_z = lerped_embeddings.unsqueeze(0) if batch_z is None else torch.cat([batch_z, lerped_embeddings.unsqueeze(0)], dim=1) - batch_tokens = tokens.unsqueeze(0) if batch_tokens is None else torch.cat([batch_tokens, tokens.unsqueeze(0)], dim=1) + batch_z = ( + lerped_embeddings.unsqueeze(0) + if batch_z is None + else torch.cat([batch_z, lerped_embeddings.unsqueeze(0)], dim=1) + ) + batch_tokens = ( + tokens.unsqueeze(0) + if batch_tokens is None + else torch.cat([batch_tokens, tokens.unsqueeze(0)], dim=1) + ) # should have shape (B, 77, 768) - #print(f"assembled all tokens into tensor of shape {batch_z.shape}") + # print(f"assembled all tokens into tensor of shape {batch_z.shape}") if should_return_tokens: return batch_z, batch_tokens else: return batch_z - def get_token_ids(self, fragments: list[str], include_start_and_end_markers: bool = True) -> list[list[int]]: + def get_token_ids( + self, fragments: list[str], include_start_and_end_markers: bool = True + ) -> list[list[int]]: """ Convert a list of strings like `["a cat", "sitting", "on a mat"]` into a list of lists of token ids like `[[bos, 0, 1, eos], [bos, 2, eos], [bos, 3, 0, 4, eos]]`. bos/eos markers are skipped if @@ -594,58 +589,81 @@ class WeightedFrozenCLIPEmbedder(FrozenCLIPEmbedder): truncation=True, max_length=self.max_length, return_overflowing_tokens=False, - padding='do_not_pad', + padding="do_not_pad", return_tensors=None, # just give me lists of ints - )['input_ids'] + )["input_ids"] result = [] for token_ids in token_ids_list: # trim eos/bos token_ids = token_ids[1:-1] # pad for textual inversions with vector length >1 - token_ids = self.textual_inversion_manager.expand_textual_inversion_token_ids_if_necessary(token_ids) + token_ids = self.textual_inversion_manager.expand_textual_inversion_token_ids_if_necessary( + token_ids + ) # restrict length to max_length-2 (leaving room for bos/eos) - token_ids = token_ids[0:self.max_length - 2] + token_ids = token_ids[0 : self.max_length - 2] # add back eos/bos if requested if include_start_and_end_markers: - token_ids = [self.tokenizer.bos_token_id] + token_ids + [self.tokenizer.eos_token_id] + token_ids = ( + [self.tokenizer.bos_token_id] + + token_ids + + [self.tokenizer.eos_token_id] + ) result.append(token_ids) return result - @classmethod - def apply_embedding_weights(self, embeddings: torch.Tensor, per_embedding_weights: list[float], normalize:bool) -> torch.Tensor: - per_embedding_weights = torch.tensor(per_embedding_weights, dtype=embeddings.dtype, device=embeddings.device) + def apply_embedding_weights( + self, + embeddings: torch.Tensor, + per_embedding_weights: list[float], + normalize: bool, + ) -> torch.Tensor: + per_embedding_weights = torch.tensor( + per_embedding_weights, dtype=embeddings.dtype, device=embeddings.device + ) if normalize: - per_embedding_weights = per_embedding_weights / torch.sum(per_embedding_weights) - reshaped_weights = per_embedding_weights.reshape(per_embedding_weights.shape + (1, 1,)) - #reshaped_weights = per_embedding_weights.reshape(per_embedding_weights.shape + (1,1,)).expand(embeddings.shape) + per_embedding_weights = per_embedding_weights / torch.sum( + per_embedding_weights + ) + reshaped_weights = per_embedding_weights.reshape( + per_embedding_weights.shape + + ( + 1, + 1, + ) + ) + # reshaped_weights = per_embedding_weights.reshape(per_embedding_weights.shape + (1,1,)).expand(embeddings.shape) return torch.sum(embeddings * reshaped_weights, dim=1) # lerped embeddings has shape (77, 768) - - def get_tokens_and_weights(self, fragments: list[str], weights: list[float]) -> (torch.Tensor, torch.Tensor): - ''' + def get_tokens_and_weights( + self, fragments: list[str], weights: list[float] + ) -> (torch.Tensor, torch.Tensor): + """ :param fragments: :param weights: Per-fragment weights (CFG scaling). No need for these to be normalized. They will not be normalized here and that's fine. :return: - ''' + """ # empty is meaningful if len(fragments) == 0 and len(weights) == 0: - fragments = [''] + fragments = [""] weights = [1] - per_fragment_token_ids = self.get_token_ids(fragments, include_start_and_end_markers=False) + per_fragment_token_ids = self.get_token_ids( + fragments, include_start_and_end_markers=False + ) all_token_ids = [] per_token_weights = [] - #print("all fragments:", fragments, weights) + # print("all fragments:", fragments, weights) for index, fragment in enumerate(per_fragment_token_ids): weight = float(weights[index]) - #print("processing fragment", fragment, weight) + # print("processing fragment", fragment, weight) this_fragment_token_ids = per_fragment_token_ids[index] - #print("fragment", fragment, "processed to", this_fragment_token_ids) + # print("fragment", fragment, "processed to", this_fragment_token_ids) # append all_token_ids += this_fragment_token_ids # fill out weights tensor with one float per token @@ -654,60 +672,85 @@ class WeightedFrozenCLIPEmbedder(FrozenCLIPEmbedder): # leave room for bos/eos max_token_count_without_bos_eos_markers = self.max_length - 2 if len(all_token_ids) > max_token_count_without_bos_eos_markers: - excess_token_count = len(all_token_ids) - max_token_count_without_bos_eos_markers + excess_token_count = ( + len(all_token_ids) - max_token_count_without_bos_eos_markers + ) # TODO build nice description string of how the truncation was applied # this should be done by calling self.tokenizer.convert_ids_to_tokens() then passing the result to # self.tokenizer.convert_tokens_to_string() for the token_ids on each side of the truncation limit. - print(f">> Prompt is {excess_token_count} token(s) too long and has been truncated") + print( + f">> Prompt is {excess_token_count} token(s) too long and has been truncated" + ) all_token_ids = all_token_ids[0:max_token_count_without_bos_eos_markers] - per_token_weights = per_token_weights[0:max_token_count_without_bos_eos_markers] + per_token_weights = per_token_weights[ + 0:max_token_count_without_bos_eos_markers + ] # pad out to a 77-entry array: [bos_token, , eos_token, pad_token…] # (77 = self.max_length) - all_token_ids = [self.tokenizer.bos_token_id] + all_token_ids + [self.tokenizer.eos_token_id] + all_token_ids = ( + [self.tokenizer.bos_token_id] + + all_token_ids + + [self.tokenizer.eos_token_id] + ) per_token_weights = [1.0] + per_token_weights + [1.0] pad_length = self.max_length - len(all_token_ids) all_token_ids += [self.tokenizer.pad_token_id] * pad_length per_token_weights += [1.0] * pad_length - all_token_ids_tensor = torch.tensor(all_token_ids, dtype=torch.long).to(self.device) - per_token_weights_tensor = torch.tensor(per_token_weights, dtype=torch.float32).to(self.device) - #print(f"assembled all_token_ids_tensor with shape {all_token_ids_tensor.shape}") + all_token_ids_tensor = torch.tensor(all_token_ids, dtype=torch.long).to( + self.device + ) + per_token_weights_tensor = torch.tensor( + per_token_weights, dtype=torch.float32 + ).to(self.device) + # print(f"assembled all_token_ids_tensor with shape {all_token_ids_tensor.shape}") return all_token_ids_tensor, per_token_weights_tensor - def build_weighted_embedding_tensor(self, token_ids: torch.Tensor, per_token_weights: torch.Tensor, weight_delta_from_empty=True, **kwargs) -> torch.Tensor: - ''' + def build_weighted_embedding_tensor( + self, + token_ids: torch.Tensor, + per_token_weights: torch.Tensor, + weight_delta_from_empty=True, + **kwargs, + ) -> torch.Tensor: + """ Build a tensor representing the passed-in tokens, each of which has a weight. :param token_ids: A tensor of shape (77) containing token ids (integers) :param per_token_weights: A tensor of shape (77) containing weights (floats) :param method: Whether to multiply the whole feature vector for each token or just its distance from an "empty" feature vector :param kwargs: passed on to self.transformer() :return: A tensor of shape (1, 77, 768) representing the requested weighted embeddings. - ''' - #print(f"building weighted embedding tensor for {tokens} with weights {per_token_weights}") + """ + # print(f"building weighted embedding tensor for {tokens} with weights {per_token_weights}") if token_ids.shape != torch.Size([self.max_length]): - raise ValueError(f"token_ids has shape {token_ids.shape} - expected [{self.max_length}]") + raise ValueError( + f"token_ids has shape {token_ids.shape} - expected [{self.max_length}]" + ) z = self.transformer(input_ids=token_ids.unsqueeze(0), **kwargs) - batch_weights_expanded = per_token_weights.reshape(per_token_weights.shape + (1,)).expand(z.shape) + batch_weights_expanded = per_token_weights.reshape( + per_token_weights.shape + (1,) + ).expand(z.shape) if weight_delta_from_empty: - empty_tokens = self.tokenizer([''] * z.shape[0], - truncation=True, - max_length=self.max_length, - padding='max_length', - return_tensors='pt' - )['input_ids'].to(self.device) + empty_tokens = self.tokenizer( + [""] * z.shape[0], + truncation=True, + max_length=self.max_length, + padding="max_length", + return_tensors="pt", + )["input_ids"].to(self.device) empty_z = self.transformer(input_ids=empty_tokens, **kwargs) z_delta_from_empty = z - empty_z weighted_z = empty_z + (z_delta_from_empty * batch_weights_expanded) - #weighted_z_delta_from_empty = (weighted_z-empty_z) - #print("weighted z has delta from empty with sum", weighted_z_delta_from_empty.sum().item(), "mean", weighted_z_delta_from_empty.mean().item() ) + # weighted_z_delta_from_empty = (weighted_z-empty_z) + # print("weighted z has delta from empty with sum", weighted_z_delta_from_empty.sum().item(), "mean", weighted_z_delta_from_empty.mean().item() ) - #print("using empty-delta method, first 5 rows:") - #print(weighted_z[:5]) + # print("using empty-delta method, first 5 rows:") + # print(weighted_z[:5]) return weighted_z @@ -716,7 +759,7 @@ class WeightedFrozenCLIPEmbedder(FrozenCLIPEmbedder): z *= batch_weights_expanded after_weighting_mean = z.mean() # correct the mean. not sure if this is right but it's what the automatic1111 fork of SD does - mean_correction_factor = original_mean/after_weighting_mean + mean_correction_factor = original_mean / after_weighting_mean z *= mean_correction_factor return z @@ -728,7 +771,7 @@ class FrozenCLIPTextEmbedder(nn.Module): def __init__( self, - version='ViT-L/14', + version="ViT-L/14", device=choose_torch_device(), max_length=77, n_repeat=1, @@ -757,7 +800,7 @@ class FrozenCLIPTextEmbedder(nn.Module): z = self(text) if z.ndim == 2: z = z[:, None, :] - z = repeat(z, 'b 1 d -> b k d', k=self.n_repeat) + z = repeat(z, "b 1 d -> b k d", k=self.n_repeat) return z @@ -779,12 +822,12 @@ class FrozenClipImageEmbedder(nn.Module): self.antialias = antialias self.register_buffer( - 'mean', + "mean", torch.Tensor([0.48145466, 0.4578275, 0.40821073]), persistent=False, ) self.register_buffer( - 'std', + "std", torch.Tensor([0.26862954, 0.26130258, 0.27577711]), persistent=False, ) @@ -794,7 +837,7 @@ class FrozenClipImageEmbedder(nn.Module): x = kornia.geometry.resize( x, (224, 224), - interpolation='bicubic', + interpolation="bicubic", align_corners=True, antialias=self.antialias, ) @@ -808,8 +851,8 @@ class FrozenClipImageEmbedder(nn.Module): return self.model.encode_image(self.preprocess(x)) -if __name__ == '__main__': - from ldm.util import count_params +if __name__ == "__main__": + from ...util.util import count_params model = FrozenCLIPEmbedder() count_params(model, verbose=True) diff --git a/invokeai/backend/stable_diffusion/image_degradation/bsrgan.py b/invokeai/backend/stable_diffusion/image_degradation/bsrgan.py index b51217bd48..1760206073 100644 --- a/invokeai/backend/stable_diffusion/image_degradation/bsrgan.py +++ b/invokeai/backend/stable_diffusion/image_degradation/bsrgan.py @@ -10,20 +10,19 @@ # -------------------------------------------- """ -import numpy as np -import cv2 -import torch - -from functools import partial import random -from scipy import ndimage +from functools import partial + +import albumentations +import cv2 +import ldm.modules.image_degradation.utils_image as util +import numpy as np import scipy import scipy.stats as ss +import torch +from scipy import ndimage from scipy.interpolate import interp2d from scipy.linalg import orth -import albumentations - -import ldm.modules.image_degradation.utils_image as util def modcrop_np(img, sf): @@ -54,9 +53,7 @@ def analytic_kernel(k): # Loop over the small kernel to fill the big one for r in range(k_size): for c in range(k_size): - big_k[2 * r : 2 * r + k_size, 2 * c : 2 * c + k_size] += ( - k[r, c] * k - ) + big_k[2 * r : 2 * r + k_size, 2 * c : 2 * c + k_size] += k[r, c] * k # Crop the edges of the big kernel to ignore very small values and increase run time of SR crop = k_size // 2 cropped_big_k = big_k[crop:-crop, crop:-crop] @@ -77,9 +74,7 @@ def anisotropic_Gaussian(ksize=15, theta=np.pi, l1=6, l2=6): """ v = np.dot( - np.array( - [[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]] - ), + np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]), np.array([1.0, 0.0]), ) V = np.array([[v[0], v[1]], [v[1], -v[0]]]) @@ -139,13 +134,11 @@ def blur(x, k): """ n, c = x.shape[:2] p1, p2 = (k.shape[-2] - 1) // 2, (k.shape[-1] - 1) // 2 - x = torch.nn.functional.pad(x, pad=(p1, p2, p1, p2), mode='replicate') + x = torch.nn.functional.pad(x, pad=(p1, p2, p1, p2), mode="replicate") k = k.repeat(1, c, 1, 1) k = k.view(-1, 1, k.shape[2], k.shape[3]) x = x.view(1, -1, x.shape[2], x.shape[3]) - x = torch.nn.functional.conv2d( - x, k, bias=None, stride=1, padding=0, groups=n * c - ) + x = torch.nn.functional.conv2d(x, k, bias=None, stride=1, padding=0, groups=n * c) x = x.view(n, c, x.shape[2], x.shape[3]) return x @@ -172,16 +165,12 @@ def gen_kernel( # Set COV matrix using Lambdas and Theta LAMBDA = np.diag([lambda_1, lambda_2]) - Q = np.array( - [[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]] - ) + Q = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) SIGMA = Q @ LAMBDA @ Q.T INV_SIGMA = np.linalg.inv(SIGMA)[None, None, :, :] # Set expectation position (shifting kernel for aligned image) - MU = k_size // 2 - 0.5 * ( - scale_factor - 1 - ) # - 0.5 * (scale_factor - k_size % 2) + MU = k_size // 2 - 0.5 * (scale_factor - 1) # - 0.5 * (scale_factor - k_size % 2) MU = MU[None, None, :, None] # Create meshgrid for Gaussian @@ -206,9 +195,7 @@ def fspecial_gaussian(hsize, sigma): hsize = [hsize, hsize] siz = [(hsize[0] - 1.0) / 2.0, (hsize[1] - 1.0) / 2.0] std = sigma - [x, y] = np.meshgrid( - np.arange(-siz[1], siz[1] + 1), np.arange(-siz[0], siz[0] + 1) - ) + [x, y] = np.meshgrid(np.arange(-siz[1], siz[1] + 1), np.arange(-siz[0], siz[0] + 1)) arg = -(x * x + y * y) / (2 * std * std) h = np.exp(arg) h[h < scipy.finfo(float).eps * h.max()] = 0 @@ -232,9 +219,9 @@ def fspecial(filter_type, *args, **kwargs): python code from: https://github.com/ronaldosena/imagens-medicas-2/blob/40171a6c259edec7827a6693a93955de2bd39e76/Aulas/aula_2_-_uniform_filter/matlab_fspecial.py """ - if filter_type == 'gaussian': + if filter_type == "gaussian": return fspecial_gaussian(*args, **kwargs) - if filter_type == 'laplacian': + if filter_type == "laplacian": return fspecial_laplacian(*args, **kwargs) @@ -275,7 +262,7 @@ def srmd_degradation(x, k, sf=3): } """ x = ndimage.filters.convolve( - x, np.expand_dims(k, axis=2), mode='wrap' + x, np.expand_dims(k, axis=2), mode="wrap" ) # 'nearest' | 'mirror' x = bicubic_degradation(x, sf=sf) return x @@ -299,7 +286,7 @@ def dpsr_degradation(x, k, sf=3): } """ x = bicubic_degradation(x, sf=sf) - x = ndimage.filters.convolve(x, np.expand_dims(k, axis=2), mode='wrap') + x = ndimage.filters.convolve(x, np.expand_dims(k, axis=2), mode="wrap") return x @@ -312,7 +299,7 @@ def classical_degradation(x, k, sf=3): Return: downsampled LR image """ - x = ndimage.filters.convolve(x, np.expand_dims(k, axis=2), mode='wrap') + x = ndimage.filters.convolve(x, np.expand_dims(k, axis=2), mode="wrap") # x = filters.correlate(x, np.expand_dims(np.flip(k), axis=2)) st = 0 return x[st::sf, st::sf, ...] @@ -336,7 +323,7 @@ def add_sharpening(img, weight=0.5, radius=50, threshold=10): blur = cv2.GaussianBlur(img, (radius, radius), 0) residual = img - blur mask = np.abs(residual) * 255 > threshold - mask = mask.astype('float32') + mask = mask.astype("float32") soft_mask = cv2.GaussianBlur(mask, (radius, radius), 0) K = img + weight * residual @@ -357,12 +344,8 @@ def add_blur(img, sf=4): l2=l2, ) else: - k = fspecial( - 'gaussian', 2 * random.randint(2, 11) + 3, wd * random.random() - ) - img = ndimage.filters.convolve( - img, np.expand_dims(k, axis=2), mode='mirror' - ) + k = fspecial("gaussian", 2 * random.randint(2, 11) + 3, wd * random.random()) + img = ndimage.filters.convolve(img, np.expand_dims(k, axis=2), mode="mirror") return img @@ -430,9 +413,9 @@ def add_speckle_noise(img, noise_level1=2, noise_level2=25): img = np.clip(img, 0.0, 1.0) rnum = random.random() if rnum > 0.6: - img += img * np.random.normal( - 0, noise_level / 255.0, img.shape - ).astype(np.float32) + img += img * np.random.normal(0, noise_level / 255.0, img.shape).astype( + np.float32 + ) elif rnum < 0.4: img += img * np.random.normal( 0, noise_level / 255.0, (*img.shape[:2], 1) @@ -458,8 +441,7 @@ def add_Poisson_noise(img): img_gray = np.dot(img[..., :3], [0.299, 0.587, 0.114]) img_gray = np.clip((img_gray * 255.0).round(), 0, 255) / 255.0 noise_gray = ( - np.random.poisson(img_gray * vals).astype(np.float32) / vals - - img_gray + np.random.poisson(img_gray * vals).astype(np.float32) / vals - img_gray ) img += noise_gray[:, :, np.newaxis] img = np.clip(img, 0.0, 1.0) @@ -470,7 +452,7 @@ def add_JPEG_noise(img): quality_factor = random.randint(30, 95) img = cv2.cvtColor(util.single2uint(img), cv2.COLOR_RGB2BGR) result, encimg = cv2.imencode( - '.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), quality_factor] + ".jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), quality_factor] ) img = cv2.imdecode(encimg, 1) img = cv2.cvtColor(util.uint2single(img), cv2.COLOR_BGR2RGB) @@ -513,7 +495,7 @@ def degradation_bsrgan(img, sf=4, lq_patchsize=72, isp_model=None): h, w = img.shape[:2] if h < lq_patchsize * sf or w < lq_patchsize * sf: - raise ValueError(f'img size ({h1}X{w1}) is too small!') + raise ValueError(f"img size ({h1}X{w1}) is too small!") hq = img.copy() @@ -538,7 +520,6 @@ def degradation_bsrgan(img, sf=4, lq_patchsize=72, isp_model=None): ) for i in shuffle_order: - if i == 0: img = add_blur(img, sf=sf) @@ -556,13 +537,11 @@ def degradation_bsrgan(img, sf=4, lq_patchsize=72, isp_model=None): interpolation=random.choice([1, 2, 3]), ) else: - k = fspecial('gaussian', 25, random.uniform(0.1, 0.6 * sf)) + k = fspecial("gaussian", 25, random.uniform(0.1, 0.6 * sf)) k_shifted = shift_pixel(k, sf) - k_shifted = ( - k_shifted / k_shifted.sum() - ) # blur with shifted kernel + k_shifted = k_shifted / k_shifted.sum() # blur with shifted kernel img = ndimage.filters.convolve( - img, np.expand_dims(k_shifted, axis=2), mode='mirror' + img, np.expand_dims(k_shifted, axis=2), mode="mirror" ) img = img[0::sf, 0::sf, ...] # nearest downsampling img = np.clip(img, 0.0, 1.0) @@ -644,7 +623,6 @@ def degradation_bsrgan_variant(image, sf=4, isp_model=None): ) for i in shuffle_order: - if i == 0: image = add_blur(image, sf=sf) @@ -665,13 +643,11 @@ def degradation_bsrgan_variant(image, sf=4, isp_model=None): interpolation=random.choice([1, 2, 3]), ) else: - k = fspecial('gaussian', 25, random.uniform(0.1, 0.6 * sf)) + k = fspecial("gaussian", 25, random.uniform(0.1, 0.6 * sf)) k_shifted = shift_pixel(k, sf) - k_shifted = ( - k_shifted / k_shifted.sum() - ) # blur with shifted kernel + k_shifted = k_shifted / k_shifted.sum() # blur with shifted kernel image = ndimage.filters.convolve( - image, np.expand_dims(k_shifted, axis=2), mode='mirror' + image, np.expand_dims(k_shifted, axis=2), mode="mirror" ) image = image[0::sf, 0::sf, ...] # nearest downsampling image = np.clip(image, 0.0, 1.0) @@ -703,7 +679,7 @@ def degradation_bsrgan_variant(image, sf=4, isp_model=None): # add final JPEG compression noise image = add_JPEG_noise(image) image = util.single2uint(image) - example = {'image': image} + example = {"image": image} return example @@ -735,7 +711,7 @@ def degradation_bsrgan_plus( h, w = img.shape[:2] if h < lq_patchsize * sf or w < lq_patchsize * sf: - raise ValueError(f'img size ({h1}X{w1}) is too small!') + raise ValueError(f"img size ({h1}X{w1}) is too small!") if use_sharp: img = add_sharpening(img) @@ -746,12 +722,8 @@ def degradation_bsrgan_plus( else: shuffle_order = list(range(13)) # local shuffle for noise, JPEG is always the last one - shuffle_order[2:6] = random.sample( - shuffle_order[2:6], len(range(2, 6)) - ) - shuffle_order[9:13] = random.sample( - shuffle_order[9:13], len(range(9, 13)) - ) + shuffle_order[2:6] = random.sample(shuffle_order[2:6], len(range(2, 6))) + shuffle_order[9:13] = random.sample(shuffle_order[9:13], len(range(9, 13))) poisson_prob, speckle_prob, isp_prob = 0.1, 0.1, 0.1 @@ -791,7 +763,7 @@ def degradation_bsrgan_plus( with torch.no_grad(): img, hq = isp_model.forward(img.copy(), hq) else: - print('check the shuffle!') + print("check the shuffle!") # resize to desired size img = cv2.resize( @@ -809,15 +781,15 @@ def degradation_bsrgan_plus( return img, hq -if __name__ == '__main__': - print('hey') - img = util.imread_uint('utils/test.png', 3) +if __name__ == "__main__": + print("hey") + img = util.imread_uint("utils/test.png", 3) print(img) img = util.uint2single(img) print(img) img = img[:448, :448] h = img.shape[0] // 4 - print('resizing to', h) + print("resizing to", h) sf = 4 deg_fn = partial(degradation_bsrgan_variant, sf=sf) for i in range(20): @@ -826,9 +798,9 @@ if __name__ == '__main__': print(img_lq) img_lq_bicubic = albumentations.SmallestMaxSize( max_size=h, interpolation=cv2.INTER_CUBIC - )(image=img)['image'] + )(image=img)["image"] print(img_lq.shape) - print('bicubic', img_lq_bicubic.shape) + print("bicubic", img_lq_bicubic.shape) print(img_hq.shape) lq_nearest = cv2.resize( util.single2uint(img_lq), @@ -843,4 +815,4 @@ if __name__ == '__main__': img_concat = np.concatenate( [lq_bicubic_nearest, lq_nearest, util.single2uint(img_hq)], axis=1 ) - util.imsave(img_concat, str(i) + '.png') + util.imsave(img_concat, str(i) + ".png") diff --git a/invokeai/backend/stable_diffusion/image_degradation/bsrgan_light.py b/invokeai/backend/stable_diffusion/image_degradation/bsrgan_light.py index 3500ef7316..1e8eee82b5 100644 --- a/invokeai/backend/stable_diffusion/image_degradation/bsrgan_light.py +++ b/invokeai/backend/stable_diffusion/image_degradation/bsrgan_light.py @@ -1,18 +1,17 @@ # -*- coding: utf-8 -*- -import numpy as np -import cv2 -import torch - -from functools import partial import random -from scipy import ndimage +from functools import partial + +import albumentations +import cv2 +import ldm.modules.image_degradation.utils_image as util +import numpy as np import scipy import scipy.stats as ss +import torch +from scipy import ndimage from scipy.interpolate import interp2d from scipy.linalg import orth -import albumentations - -import ldm.modules.image_degradation.utils_image as util """ # -------------------------------------------- @@ -54,9 +53,7 @@ def analytic_kernel(k): # Loop over the small kernel to fill the big one for r in range(k_size): for c in range(k_size): - big_k[2 * r : 2 * r + k_size, 2 * c : 2 * c + k_size] += ( - k[r, c] * k - ) + big_k[2 * r : 2 * r + k_size, 2 * c : 2 * c + k_size] += k[r, c] * k # Crop the edges of the big kernel to ignore very small values and increase run time of SR crop = k_size // 2 cropped_big_k = big_k[crop:-crop, crop:-crop] @@ -77,9 +74,7 @@ def anisotropic_Gaussian(ksize=15, theta=np.pi, l1=6, l2=6): """ v = np.dot( - np.array( - [[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]] - ), + np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]), np.array([1.0, 0.0]), ) V = np.array([[v[0], v[1]], [v[1], -v[0]]]) @@ -139,13 +134,11 @@ def blur(x, k): """ n, c = x.shape[:2] p1, p2 = (k.shape[-2] - 1) // 2, (k.shape[-1] - 1) // 2 - x = torch.nn.functional.pad(x, pad=(p1, p2, p1, p2), mode='replicate') + x = torch.nn.functional.pad(x, pad=(p1, p2, p1, p2), mode="replicate") k = k.repeat(1, c, 1, 1) k = k.view(-1, 1, k.shape[2], k.shape[3]) x = x.view(1, -1, x.shape[2], x.shape[3]) - x = torch.nn.functional.conv2d( - x, k, bias=None, stride=1, padding=0, groups=n * c - ) + x = torch.nn.functional.conv2d(x, k, bias=None, stride=1, padding=0, groups=n * c) x = x.view(n, c, x.shape[2], x.shape[3]) return x @@ -172,16 +165,12 @@ def gen_kernel( # Set COV matrix using Lambdas and Theta LAMBDA = np.diag([lambda_1, lambda_2]) - Q = np.array( - [[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]] - ) + Q = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) SIGMA = Q @ LAMBDA @ Q.T INV_SIGMA = np.linalg.inv(SIGMA)[None, None, :, :] # Set expectation position (shifting kernel for aligned image) - MU = k_size // 2 - 0.5 * ( - scale_factor - 1 - ) # - 0.5 * (scale_factor - k_size % 2) + MU = k_size // 2 - 0.5 * (scale_factor - 1) # - 0.5 * (scale_factor - k_size % 2) MU = MU[None, None, :, None] # Create meshgrid for Gaussian @@ -206,9 +195,7 @@ def fspecial_gaussian(hsize, sigma): hsize = [hsize, hsize] siz = [(hsize[0] - 1.0) / 2.0, (hsize[1] - 1.0) / 2.0] std = sigma - [x, y] = np.meshgrid( - np.arange(-siz[1], siz[1] + 1), np.arange(-siz[0], siz[0] + 1) - ) + [x, y] = np.meshgrid(np.arange(-siz[1], siz[1] + 1), np.arange(-siz[0], siz[0] + 1)) arg = -(x * x + y * y) / (2 * std * std) h = np.exp(arg) h[h < scipy.finfo(float).eps * h.max()] = 0 @@ -232,9 +219,9 @@ def fspecial(filter_type, *args, **kwargs): python code from: https://github.com/ronaldosena/imagens-medicas-2/blob/40171a6c259edec7827a6693a93955de2bd39e76/Aulas/aula_2_-_uniform_filter/matlab_fspecial.py """ - if filter_type == 'gaussian': + if filter_type == "gaussian": return fspecial_gaussian(*args, **kwargs) - if filter_type == 'laplacian': + if filter_type == "laplacian": return fspecial_laplacian(*args, **kwargs) @@ -275,7 +262,7 @@ def srmd_degradation(x, k, sf=3): } """ x = ndimage.filters.convolve( - x, np.expand_dims(k, axis=2), mode='wrap' + x, np.expand_dims(k, axis=2), mode="wrap" ) # 'nearest' | 'mirror' x = bicubic_degradation(x, sf=sf) return x @@ -299,7 +286,7 @@ def dpsr_degradation(x, k, sf=3): } """ x = bicubic_degradation(x, sf=sf) - x = ndimage.filters.convolve(x, np.expand_dims(k, axis=2), mode='wrap') + x = ndimage.filters.convolve(x, np.expand_dims(k, axis=2), mode="wrap") return x @@ -312,7 +299,7 @@ def classical_degradation(x, k, sf=3): Return: downsampled LR image """ - x = ndimage.filters.convolve(x, np.expand_dims(k, axis=2), mode='wrap') + x = ndimage.filters.convolve(x, np.expand_dims(k, axis=2), mode="wrap") # x = filters.correlate(x, np.expand_dims(np.flip(k), axis=2)) st = 0 return x[st::sf, st::sf, ...] @@ -336,7 +323,7 @@ def add_sharpening(img, weight=0.5, radius=50, threshold=10): blur = cv2.GaussianBlur(img, (radius, radius), 0) residual = img - blur mask = np.abs(residual) * 255 > threshold - mask = mask.astype('float32') + mask = mask.astype("float32") soft_mask = cv2.GaussianBlur(mask, (radius, radius), 0) K = img + weight * residual @@ -361,12 +348,8 @@ def add_blur(img, sf=4): l2=l2, ) else: - k = fspecial( - 'gaussian', random.randint(2, 4) + 3, wd * random.random() - ) - img = ndimage.filters.convolve( - img, np.expand_dims(k, axis=2), mode='mirror' - ) + k = fspecial("gaussian", random.randint(2, 4) + 3, wd * random.random()) + img = ndimage.filters.convolve(img, np.expand_dims(k, axis=2), mode="mirror") return img @@ -434,9 +417,9 @@ def add_speckle_noise(img, noise_level1=2, noise_level2=25): img = np.clip(img, 0.0, 1.0) rnum = random.random() if rnum > 0.6: - img += img * np.random.normal( - 0, noise_level / 255.0, img.shape - ).astype(np.float32) + img += img * np.random.normal(0, noise_level / 255.0, img.shape).astype( + np.float32 + ) elif rnum < 0.4: img += img * np.random.normal( 0, noise_level / 255.0, (*img.shape[:2], 1) @@ -462,8 +445,7 @@ def add_Poisson_noise(img): img_gray = np.dot(img[..., :3], [0.299, 0.587, 0.114]) img_gray = np.clip((img_gray * 255.0).round(), 0, 255) / 255.0 noise_gray = ( - np.random.poisson(img_gray * vals).astype(np.float32) / vals - - img_gray + np.random.poisson(img_gray * vals).astype(np.float32) / vals - img_gray ) img += noise_gray[:, :, np.newaxis] img = np.clip(img, 0.0, 1.0) @@ -474,7 +456,7 @@ def add_JPEG_noise(img): quality_factor = random.randint(80, 95) img = cv2.cvtColor(util.single2uint(img), cv2.COLOR_RGB2BGR) result, encimg = cv2.imencode( - '.jpg', img, [int(cv2.IMWRITE_JPEG_QUALITY), quality_factor] + ".jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), quality_factor] ) img = cv2.imdecode(encimg, 1) img = cv2.cvtColor(util.uint2single(img), cv2.COLOR_BGR2RGB) @@ -517,7 +499,7 @@ def degradation_bsrgan(img, sf=4, lq_patchsize=72, isp_model=None): h, w = img.shape[:2] if h < lq_patchsize * sf or w < lq_patchsize * sf: - raise ValueError(f'img size ({h1}X{w1}) is too small!') + raise ValueError(f"img size ({h1}X{w1}) is too small!") hq = img.copy() @@ -542,7 +524,6 @@ def degradation_bsrgan(img, sf=4, lq_patchsize=72, isp_model=None): ) for i in shuffle_order: - if i == 0: img = add_blur(img, sf=sf) @@ -560,13 +541,11 @@ def degradation_bsrgan(img, sf=4, lq_patchsize=72, isp_model=None): interpolation=random.choice([1, 2, 3]), ) else: - k = fspecial('gaussian', 25, random.uniform(0.1, 0.6 * sf)) + k = fspecial("gaussian", 25, random.uniform(0.1, 0.6 * sf)) k_shifted = shift_pixel(k, sf) - k_shifted = ( - k_shifted / k_shifted.sum() - ) # blur with shifted kernel + k_shifted = k_shifted / k_shifted.sum() # blur with shifted kernel img = ndimage.filters.convolve( - img, np.expand_dims(k_shifted, axis=2), mode='mirror' + img, np.expand_dims(k_shifted, axis=2), mode="mirror" ) img = img[0::sf, 0::sf, ...] # nearest downsampling img = np.clip(img, 0.0, 1.0) @@ -648,7 +627,6 @@ def degradation_bsrgan_variant(image, sf=4, isp_model=None): ) for i in shuffle_order: - if i == 0: image = add_blur(image, sf=sf) @@ -672,13 +650,11 @@ def degradation_bsrgan_variant(image, sf=4, isp_model=None): interpolation=random.choice([1, 2, 3]), ) else: - k = fspecial('gaussian', 25, random.uniform(0.1, 0.6 * sf)) + k = fspecial("gaussian", 25, random.uniform(0.1, 0.6 * sf)) k_shifted = shift_pixel(k, sf) - k_shifted = ( - k_shifted / k_shifted.sum() - ) # blur with shifted kernel + k_shifted = k_shifted / k_shifted.sum() # blur with shifted kernel image = ndimage.filters.convolve( - image, np.expand_dims(k_shifted, axis=2), mode='mirror' + image, np.expand_dims(k_shifted, axis=2), mode="mirror" ) image = image[0::sf, 0::sf, ...] # nearest downsampling @@ -711,29 +687,29 @@ def degradation_bsrgan_variant(image, sf=4, isp_model=None): # add final JPEG compression noise image = add_JPEG_noise(image) image = util.single2uint(image) - example = {'image': image} + example = {"image": image} return example -if __name__ == '__main__': - print('hey') - img = util.imread_uint('utils/test.png', 3) +if __name__ == "__main__": + print("hey") + img = util.imread_uint("utils/test.png", 3) img = img[:448, :448] h = img.shape[0] // 4 - print('resizing to', h) + print("resizing to", h) sf = 4 deg_fn = partial(degradation_bsrgan_variant, sf=sf) for i in range(20): print(i) img_hq = img - img_lq = deg_fn(img)['image'] + img_lq = deg_fn(img)["image"] img_hq, img_lq = util.uint2single(img_hq), util.uint2single(img_lq) print(img_lq) img_lq_bicubic = albumentations.SmallestMaxSize( max_size=h, interpolation=cv2.INTER_CUBIC - )(image=img_hq)['image'] + )(image=img_hq)["image"] print(img_lq.shape) - print('bicubic', img_lq_bicubic.shape) + print("bicubic", img_lq_bicubic.shape) print(img_hq.shape) lq_nearest = cv2.resize( util.single2uint(img_lq), @@ -748,4 +724,4 @@ if __name__ == '__main__': img_concat = np.concatenate( [lq_bicubic_nearest, lq_nearest, util.single2uint(img_hq)], axis=1 ) - util.imsave(img_concat, str(i) + '.png') + util.imsave(img_concat, str(i) + ".png") diff --git a/invokeai/backend/stable_diffusion/image_degradation/utils_image.py b/invokeai/backend/stable_diffusion/image_degradation/utils_image.py index 59236683b5..08505edde0 100644 --- a/invokeai/backend/stable_diffusion/image_degradation/utils_image.py +++ b/invokeai/backend/stable_diffusion/image_degradation/utils_image.py @@ -1,16 +1,17 @@ -import os import math +import os import random +from datetime import datetime + +import cv2 import numpy as np import torch -import cv2 from torchvision.utils import make_grid -from datetime import datetime # import matplotlib.pyplot as plt # TODO: check with Dominik, also bsrgan.py vs bsrgan_light.py -os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE' +os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" """ @@ -25,17 +26,17 @@ os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE' IMG_EXTENSIONS = [ - '.jpg', - '.JPG', - '.jpeg', - '.JPEG', - '.png', - '.PNG', - '.ppm', - '.PPM', - '.bmp', - '.BMP', - '.tif', + ".jpg", + ".JPG", + ".jpeg", + ".JPEG", + ".png", + ".PNG", + ".ppm", + ".PPM", + ".bmp", + ".BMP", + ".tif", ] @@ -44,12 +45,12 @@ def is_image_file(filename): def get_timestamp(): - return datetime.now().strftime('%y%m%d-%H%M%S') + return datetime.now().strftime("%y%m%d-%H%M%S") def imshow(x, title=None, cbar=False, figsize=None): plt.figure(figsize=figsize) - plt.imshow(np.squeeze(x), interpolation='nearest', cmap='gray') + plt.imshow(np.squeeze(x), interpolation="nearest", cmap="gray") if title: plt.title(title) if cbar: @@ -57,9 +58,9 @@ def imshow(x, title=None, cbar=False, figsize=None): plt.show() -def surf(Z, cmap='rainbow', figsize=None): +def surf(Z, cmap="rainbow", figsize=None): plt.figure(figsize=figsize) - ax3 = plt.axes(projection='3d') + ax3 = plt.axes(projection="3d") w, h = Z.shape[:2] xx = np.arange(0, w, 1) @@ -85,14 +86,14 @@ def get_image_paths(dataroot): def _get_paths_from_images(path): - assert os.path.isdir(path), '{:s} is not a valid directory'.format(path) + assert os.path.isdir(path), "{:s} is not a valid directory".format(path) images = [] for dirpath, _, fnames in sorted(os.walk(path)): for fname in sorted(fnames): if is_image_file(fname): img_path = os.path.join(dirpath, fname) images.append(img_path) - assert images, '{:s} has no valid image file'.format(path) + assert images, "{:s} has no valid image file".format(path) return images @@ -133,7 +134,7 @@ def imssave(imgs, img_path): img = img[:, :, [2, 1, 0]] new_path = os.path.join( os.path.dirname(img_path), - img_name + str('_s{:04d}'.format(i)) + '.png', + img_name + str("_s{:04d}".format(i)) + ".png", ) cv2.imwrite(new_path, img) @@ -162,9 +163,7 @@ def split_imageset( # img_name, ext = os.path.splitext(os.path.basename(img_path)) img = imread_uint(img_path, n_channels=n_channels) patches = patches_from_image(img, p_size, p_overlap, p_max) - imssave( - patches, os.path.join(taget_dataroot, os.path.basename(img_path)) - ) + imssave(patches, os.path.join(taget_dataroot, os.path.basename(img_path))) # if original_dataroot == taget_dataroot: # del img_path @@ -191,8 +190,8 @@ def mkdirs(paths): def mkdir_and_rename(path): if os.path.exists(path): - new_name = path + '_archived_' + get_timestamp() - print('Path already exists. Rename it to [{:s}]'.format(new_name)) + new_name = path + "_archived_" + get_timestamp() + print("Path already exists. Rename it to [{:s}]".format(new_name)) os.replace(path, new_name) os.makedirs(path) @@ -273,22 +272,18 @@ def read_img(path): def uint2single(img): - return np.float32(img / 255.0) def single2uint(img): - return np.uint8((img.clip(0, 1) * 255.0).round()) def uint162single(img): - return np.float32(img / 65535.0) def single2uint16(img): - return np.uint16((img.clip(0, 1) * 65535.0).round()) @@ -315,10 +310,7 @@ def uint2tensor3(img): if img.ndim == 2: img = np.expand_dims(img, axis=2) return ( - torch.from_numpy(np.ascontiguousarray(img)) - .permute(2, 0, 1) - .float() - .div(255.0) + torch.from_numpy(np.ascontiguousarray(img)).permute(2, 0, 1).float().div(255.0) ) @@ -379,18 +371,11 @@ def single2tensor5(img): def single32tensor5(img): - return ( - torch.from_numpy(np.ascontiguousarray(img)) - .float() - .unsqueeze(0) - .unsqueeze(0) - ) + return torch.from_numpy(np.ascontiguousarray(img)).float().unsqueeze(0).unsqueeze(0) def single42tensor4(img): - return ( - torch.from_numpy(np.ascontiguousarray(img)).permute(2, 0, 1, 3).float() - ) + return torch.from_numpy(np.ascontiguousarray(img)).permute(2, 0, 1, 3).float() # from skimage.io import imread, imsave @@ -403,15 +388,11 @@ def tensor2img(tensor, out_type=np.uint8, min_max=(0, 1)): tensor = ( tensor.squeeze().float().cpu().clamp_(*min_max) ) # squeeze first, then clamp - tensor = (tensor - min_max[0]) / ( - min_max[1] - min_max[0] - ) # to range [0,1] + tensor = (tensor - min_max[0]) / (min_max[1] - min_max[0]) # to range [0,1] n_dim = tensor.dim() if n_dim == 4: n_img = len(tensor) - img_np = make_grid( - tensor, nrow=int(math.sqrt(n_img)), normalize=False - ).numpy() + img_np = make_grid(tensor, nrow=int(math.sqrt(n_img)), normalize=False).numpy() img_np = np.transpose(img_np[[2, 1, 0], :, :], (1, 2, 0)) # HWC, BGR elif n_dim == 3: img_np = tensor.numpy() @@ -420,7 +401,7 @@ def tensor2img(tensor, out_type=np.uint8, min_max=(0, 1)): img_np = tensor.numpy() else: raise TypeError( - 'Only support 4D, 3D and 2D tensor. But received with dimension: {:d}'.format( + "Only support 4D, 3D and 2D tensor. But received with dimension: {:d}".format( n_dim ) ) @@ -564,7 +545,7 @@ def modcrop(img_in, scale): H_r, W_r = H % scale, W % scale img = img[: H - H_r, : W - W_r, :] else: - raise ValueError('Wrong img ndim: [{:d}].'.format(img.ndim)) + raise ValueError("Wrong img ndim: [{:d}].".format(img.ndim)) return img @@ -675,13 +656,13 @@ def bgr2ycbcr(img, only_y=True): def channel_convert(in_c, tar_type, img_list): # conversion among BGR, gray and y - if in_c == 3 and tar_type == 'gray': # BGR to gray + if in_c == 3 and tar_type == "gray": # BGR to gray gray_list = [cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) for img in img_list] return [np.expand_dims(img, axis=2) for img in gray_list] - elif in_c == 3 and tar_type == 'y': # BGR to y + elif in_c == 3 and tar_type == "y": # BGR to y y_list = [bgr2ycbcr(img, only_y=True) for img in img_list] return [np.expand_dims(img, axis=2) for img in y_list] - elif in_c == 1 and tar_type == 'RGB': # gray/y to BGR + elif in_c == 1 and tar_type == "RGB": # gray/y to BGR return [cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) for img in img_list] else: return img_list @@ -702,7 +683,7 @@ def calculate_psnr(img1, img2, border=0): # img1 = img1.squeeze() # img2 = img2.squeeze() if not img1.shape == img2.shape: - raise ValueError('Input images must have the same dimensions.') + raise ValueError("Input images must have the same dimensions.") h, w = img1.shape[:2] img1 = img1[border : h - border, border : w - border] img2 = img2[border : h - border, border : w - border] @@ -711,7 +692,7 @@ def calculate_psnr(img1, img2, border=0): img2 = img2.astype(np.float64) mse = np.mean((img1 - img2) ** 2) if mse == 0: - return float('inf') + return float("inf") return 20 * math.log10(255.0 / math.sqrt(mse)) @@ -726,7 +707,7 @@ def calculate_ssim(img1, img2, border=0): # img1 = img1.squeeze() # img2 = img2.squeeze() if not img1.shape == img2.shape: - raise ValueError('Input images must have the same dimensions.') + raise ValueError("Input images must have the same dimensions.") h, w = img1.shape[:2] img1 = img1[border : h - border, border : w - border] img2 = img2[border : h - border, border : w - border] @@ -742,7 +723,7 @@ def calculate_ssim(img1, img2, border=0): elif img1.shape[2] == 1: return ssim(np.squeeze(img1), np.squeeze(img2)) else: - raise ValueError('Wrong input image dimensions.') + raise ValueError("Wrong input image dimensions.") def ssim(img1, img2): @@ -861,7 +842,7 @@ def imresize(img, scale, antialiasing=True): math.ceil(in_W * scale), ) kernel_width = 4 - kernel = 'cubic' + kernel = "cubic" # Return the desired dimension order for performing the resize. The # strategy is to perform the resize first along the dimension with the @@ -896,9 +877,7 @@ def imresize(img, scale, antialiasing=True): idx = int(indices_H[i][0]) for j in range(out_C): out_1[j, i, :] = ( - img_aug[j, idx : idx + kernel_width, :] - .transpose(0, 1) - .mv(weights_H[i]) + img_aug[j, idx : idx + kernel_width, :].transpose(0, 1).mv(weights_H[i]) ) # process W dimension @@ -921,9 +900,7 @@ def imresize(img, scale, antialiasing=True): for i in range(out_W): idx = int(indices_W[i][0]) for j in range(out_C): - out_2[j, :, i] = out_1_aug[j, :, idx : idx + kernel_width].mv( - weights_W[i] - ) + out_2[j, :, i] = out_1_aug[j, :, idx : idx + kernel_width].mv(weights_W[i]) if need_squeeze: out_2.squeeze_() return out_2 @@ -948,7 +925,7 @@ def imresize_np(img, scale, antialiasing=True): math.ceil(in_W * scale), ) kernel_width = 4 - kernel = 'cubic' + kernel = "cubic" # Return the desired dimension order for performing the resize. The # strategy is to perform the resize first along the dimension with the @@ -983,9 +960,7 @@ def imresize_np(img, scale, antialiasing=True): idx = int(indices_H[i][0]) for j in range(out_C): out_1[i, :, j] = ( - img_aug[idx : idx + kernel_width, :, j] - .transpose(0, 1) - .mv(weights_H[i]) + img_aug[idx : idx + kernel_width, :, j].transpose(0, 1).mv(weights_H[i]) ) # process W dimension @@ -1008,17 +983,15 @@ def imresize_np(img, scale, antialiasing=True): for i in range(out_W): idx = int(indices_W[i][0]) for j in range(out_C): - out_2[:, i, j] = out_1_aug[:, idx : idx + kernel_width, j].mv( - weights_W[i] - ) + out_2[:, i, j] = out_1_aug[:, idx : idx + kernel_width, j].mv(weights_W[i]) if need_squeeze: out_2.squeeze_() return out_2.numpy() -if __name__ == '__main__': - print('---') +if __name__ == "__main__": + print("---") # img = imread_uint('test.bmp', 3) # img = uint2single(img) # img_bicubic = imresize_np(img, 1/4) diff --git a/invokeai/backend/stable_diffusion/losses/contperceptual.py b/invokeai/backend/stable_diffusion/losses/contperceptual.py index 7fa4124346..1e3e6a00c4 100644 --- a/invokeai/backend/stable_diffusion/losses/contperceptual.py +++ b/invokeai/backend/stable_diffusion/losses/contperceptual.py @@ -1,6 +1,5 @@ import torch import torch.nn as nn - from taming.modules.losses.vqperceptual import * # TODO: taming dependency yes/no? @@ -18,11 +17,10 @@ class LPIPSWithDiscriminator(nn.Module): perceptual_weight=1.0, use_actnorm=False, disc_conditional=False, - disc_loss='hinge', + disc_loss="hinge", ): - super().__init__() - assert disc_loss in ['hinge', 'vanilla'] + assert disc_loss in ["hinge", "vanilla"] self.kl_weight = kl_weight self.pixel_weight = pixelloss_weight self.perceptual_loss = LPIPS().eval() @@ -36,21 +34,15 @@ class LPIPSWithDiscriminator(nn.Module): use_actnorm=use_actnorm, ).apply(weights_init) self.discriminator_iter_start = disc_start - self.disc_loss = ( - hinge_d_loss if disc_loss == 'hinge' else vanilla_d_loss - ) + self.disc_loss = hinge_d_loss if disc_loss == "hinge" else vanilla_d_loss self.disc_factor = disc_factor self.discriminator_weight = disc_weight self.disc_conditional = disc_conditional def calculate_adaptive_weight(self, nll_loss, g_loss, last_layer=None): if last_layer is not None: - nll_grads = torch.autograd.grad( - nll_loss, last_layer, retain_graph=True - )[0] - g_grads = torch.autograd.grad( - g_loss, last_layer, retain_graph=True - )[0] + nll_grads = torch.autograd.grad(nll_loss, last_layer, retain_graph=True)[0] + g_grads = torch.autograd.grad(g_loss, last_layer, retain_graph=True)[0] else: nll_grads = torch.autograd.grad( nll_loss, self.last_layer[0], retain_graph=True @@ -73,12 +65,10 @@ class LPIPSWithDiscriminator(nn.Module): global_step, last_layer=None, cond=None, - split='train', + split="train", weights=None, ): - rec_loss = torch.abs( - inputs.contiguous() - reconstructions.contiguous() - ) + rec_loss = torch.abs(inputs.contiguous() - reconstructions.contiguous()) if self.perceptual_weight > 0: p_loss = self.perceptual_loss( inputs.contiguous(), reconstructions.contiguous() @@ -89,9 +79,7 @@ class LPIPSWithDiscriminator(nn.Module): weighted_nll_loss = nll_loss if weights is not None: weighted_nll_loss = weights * nll_loss - weighted_nll_loss = ( - torch.sum(weighted_nll_loss) / weighted_nll_loss.shape[0] - ) + weighted_nll_loss = torch.sum(weighted_nll_loss) / weighted_nll_loss.shape[0] nll_loss = torch.sum(nll_loss) / nll_loss.shape[0] kl_loss = posteriors.kl() kl_loss = torch.sum(kl_loss) / kl_loss.shape[0] @@ -132,14 +120,14 @@ class LPIPSWithDiscriminator(nn.Module): ) log = { - '{}/total_loss'.format(split): loss.clone().detach().mean(), - '{}/logvar'.format(split): self.logvar.detach(), - '{}/kl_loss'.format(split): kl_loss.detach().mean(), - '{}/nll_loss'.format(split): nll_loss.detach().mean(), - '{}/rec_loss'.format(split): rec_loss.detach().mean(), - '{}/d_weight'.format(split): d_weight.detach(), - '{}/disc_factor'.format(split): torch.tensor(disc_factor), - '{}/g_loss'.format(split): g_loss.detach().mean(), + "{}/total_loss".format(split): loss.clone().detach().mean(), + "{}/logvar".format(split): self.logvar.detach(), + "{}/kl_loss".format(split): kl_loss.detach().mean(), + "{}/nll_loss".format(split): nll_loss.detach().mean(), + "{}/rec_loss".format(split): rec_loss.detach().mean(), + "{}/d_weight".format(split): d_weight.detach(), + "{}/disc_factor".format(split): torch.tensor(disc_factor), + "{}/g_loss".format(split): g_loss.detach().mean(), } return loss, log @@ -147,17 +135,13 @@ class LPIPSWithDiscriminator(nn.Module): # second pass for discriminator update if cond is None: logits_real = self.discriminator(inputs.contiguous().detach()) - logits_fake = self.discriminator( - reconstructions.contiguous().detach() - ) + logits_fake = self.discriminator(reconstructions.contiguous().detach()) else: logits_real = self.discriminator( torch.cat((inputs.contiguous().detach(), cond), dim=1) ) logits_fake = self.discriminator( - torch.cat( - (reconstructions.contiguous().detach(), cond), dim=1 - ) + torch.cat((reconstructions.contiguous().detach(), cond), dim=1) ) disc_factor = adopt_weight( @@ -168,8 +152,8 @@ class LPIPSWithDiscriminator(nn.Module): d_loss = disc_factor * self.disc_loss(logits_real, logits_fake) log = { - '{}/disc_loss'.format(split): d_loss.clone().detach().mean(), - '{}/logits_real'.format(split): logits_real.detach().mean(), - '{}/logits_fake'.format(split): logits_fake.detach().mean(), + "{}/disc_loss".format(split): d_loss.clone().detach().mean(), + "{}/logits_real".format(split): logits_real.detach().mean(), + "{}/logits_fake".format(split): logits_fake.detach().mean(), } return d_loss, log diff --git a/invokeai/backend/stable_diffusion/losses/vqperceptual.py b/invokeai/backend/stable_diffusion/losses/vqperceptual.py index 2f94bf5281..50413d37b8 100644 --- a/invokeai/backend/stable_diffusion/losses/vqperceptual.py +++ b/invokeai/backend/stable_diffusion/losses/vqperceptual.py @@ -1,14 +1,10 @@ import torch -from torch import nn import torch.nn.functional as F from einops import repeat - -from taming.modules.discriminator.model import ( - NLayerDiscriminator, - weights_init, -) +from taming.modules.discriminator.model import NLayerDiscriminator, weights_init from taming.modules.losses.lpips import LPIPS from taming.modules.losses.vqperceptual import hinge_d_loss, vanilla_d_loss +from torch import nn def hinge_d_loss_with_exemplar_weights(logits_real, logits_fake, weights): @@ -30,9 +26,7 @@ def adopt_weight(weight, global_step, threshold=0, value=0.0): def measure_perplexity(predicted_indices, n_embed): # src: https://github.com/karpathy/deep-vector-quantization/blob/main/model.py # eval cluster perplexity. when perplexity == num_embeddings then all clusters are used exactly equally - encodings = ( - F.one_hot(predicted_indices, n_embed).float().reshape(-1, n_embed) - ) + encodings = F.one_hot(predicted_indices, n_embed).float().reshape(-1, n_embed) avg_probs = encodings.mean(0) perplexity = (-(avg_probs * torch.log(avg_probs + 1e-10)).sum()).exp() cluster_use = torch.sum(avg_probs > 0) @@ -61,27 +55,25 @@ class VQLPIPSWithDiscriminator(nn.Module): use_actnorm=False, disc_conditional=False, disc_ndf=64, - disc_loss='hinge', + disc_loss="hinge", n_classes=None, - perceptual_loss='lpips', - pixel_loss='l1', + perceptual_loss="lpips", + pixel_loss="l1", ): super().__init__() - assert disc_loss in ['hinge', 'vanilla'] - assert perceptual_loss in ['lpips', 'clips', 'dists'] - assert pixel_loss in ['l1', 'l2'] + assert disc_loss in ["hinge", "vanilla"] + assert perceptual_loss in ["lpips", "clips", "dists"] + assert pixel_loss in ["l1", "l2"] self.codebook_weight = codebook_weight self.pixel_weight = pixelloss_weight - if perceptual_loss == 'lpips': - print(f'{self.__class__.__name__}: Running with LPIPS.') + if perceptual_loss == "lpips": + print(f"{self.__class__.__name__}: Running with LPIPS.") self.perceptual_loss = LPIPS().eval() else: - raise ValueError( - f'Unknown perceptual loss: >> {perceptual_loss} <<' - ) + raise ValueError(f"Unknown perceptual loss: >> {perceptual_loss} <<") self.perceptual_weight = perceptual_weight - if pixel_loss == 'l1': + if pixel_loss == "l1": self.pixel_loss = l1 else: self.pixel_loss = l2 @@ -93,13 +85,13 @@ class VQLPIPSWithDiscriminator(nn.Module): ndf=disc_ndf, ).apply(weights_init) self.discriminator_iter_start = disc_start - if disc_loss == 'hinge': + if disc_loss == "hinge": self.disc_loss = hinge_d_loss - elif disc_loss == 'vanilla': + elif disc_loss == "vanilla": self.disc_loss = vanilla_d_loss else: raise ValueError(f"Unknown GAN loss '{disc_loss}'.") - print(f'VQLPIPSWithDiscriminator running with {disc_loss} loss.') + print(f"VQLPIPSWithDiscriminator running with {disc_loss} loss.") self.disc_factor = disc_factor self.discriminator_weight = disc_weight self.disc_conditional = disc_conditional @@ -107,12 +99,8 @@ class VQLPIPSWithDiscriminator(nn.Module): def calculate_adaptive_weight(self, nll_loss, g_loss, last_layer=None): if last_layer is not None: - nll_grads = torch.autograd.grad( - nll_loss, last_layer, retain_graph=True - )[0] - g_grads = torch.autograd.grad( - g_loss, last_layer, retain_graph=True - )[0] + nll_grads = torch.autograd.grad(nll_loss, last_layer, retain_graph=True)[0] + g_grads = torch.autograd.grad(g_loss, last_layer, retain_graph=True)[0] else: nll_grads = torch.autograd.grad( nll_loss, self.last_layer[0], retain_graph=True @@ -135,15 +123,13 @@ class VQLPIPSWithDiscriminator(nn.Module): global_step, last_layer=None, cond=None, - split='train', + split="train", predicted_indices=None, ): if not exists(codebook_loss): codebook_loss = torch.tensor([0.0]).to(inputs.device) # rec_loss = torch.abs(inputs.contiguous() - reconstructions.contiguous()) - rec_loss = self.pixel_loss( - inputs.contiguous(), reconstructions.contiguous() - ) + rec_loss = self.pixel_loss(inputs.contiguous(), reconstructions.contiguous()) if self.perceptual_weight > 0: p_loss = self.perceptual_loss( inputs.contiguous(), reconstructions.contiguous() @@ -189,14 +175,14 @@ class VQLPIPSWithDiscriminator(nn.Module): ) log = { - '{}/total_loss'.format(split): loss.clone().detach().mean(), - '{}/quant_loss'.format(split): codebook_loss.detach().mean(), - '{}/nll_loss'.format(split): nll_loss.detach().mean(), - '{}/rec_loss'.format(split): rec_loss.detach().mean(), - '{}/p_loss'.format(split): p_loss.detach().mean(), - '{}/d_weight'.format(split): d_weight.detach(), - '{}/disc_factor'.format(split): torch.tensor(disc_factor), - '{}/g_loss'.format(split): g_loss.detach().mean(), + "{}/total_loss".format(split): loss.clone().detach().mean(), + "{}/quant_loss".format(split): codebook_loss.detach().mean(), + "{}/nll_loss".format(split): nll_loss.detach().mean(), + "{}/rec_loss".format(split): rec_loss.detach().mean(), + "{}/p_loss".format(split): p_loss.detach().mean(), + "{}/d_weight".format(split): d_weight.detach(), + "{}/disc_factor".format(split): torch.tensor(disc_factor), + "{}/g_loss".format(split): g_loss.detach().mean(), } if predicted_indices is not None: assert self.n_classes is not None @@ -204,25 +190,21 @@ class VQLPIPSWithDiscriminator(nn.Module): perplexity, cluster_usage = measure_perplexity( predicted_indices, self.n_classes ) - log[f'{split}/perplexity'] = perplexity - log[f'{split}/cluster_usage'] = cluster_usage + log[f"{split}/perplexity"] = perplexity + log[f"{split}/cluster_usage"] = cluster_usage return loss, log if optimizer_idx == 1: # second pass for discriminator update if cond is None: logits_real = self.discriminator(inputs.contiguous().detach()) - logits_fake = self.discriminator( - reconstructions.contiguous().detach() - ) + logits_fake = self.discriminator(reconstructions.contiguous().detach()) else: logits_real = self.discriminator( torch.cat((inputs.contiguous().detach(), cond), dim=1) ) logits_fake = self.discriminator( - torch.cat( - (reconstructions.contiguous().detach(), cond), dim=1 - ) + torch.cat((reconstructions.contiguous().detach(), cond), dim=1) ) disc_factor = adopt_weight( @@ -233,8 +215,8 @@ class VQLPIPSWithDiscriminator(nn.Module): d_loss = disc_factor * self.disc_loss(logits_real, logits_fake) log = { - '{}/disc_loss'.format(split): d_loss.clone().detach().mean(), - '{}/logits_real'.format(split): logits_real.detach().mean(), - '{}/logits_fake'.format(split): logits_fake.detach().mean(), + "{}/disc_loss".format(split): d_loss.clone().detach().mean(), + "{}/logits_real".format(split): logits_real.detach().mean(), + "{}/logits_fake".format(split): logits_fake.detach().mean(), } return d_loss, log diff --git a/invokeai/backend/stable_diffusion/offloading.py b/invokeai/backend/stable_diffusion/offloading.py index e049f5fe09..871f994732 100644 --- a/invokeai/backend/stable_diffusion/offloading.py +++ b/invokeai/backend/stable_diffusion/offloading.py @@ -12,6 +12,7 @@ from torch.utils.hooks import RemovableHandle OFFLOAD_DEVICE = torch.device("cpu") + class _NoModel: """Symbol that indicates no model is loaded. @@ -28,6 +29,7 @@ class _NoModel: def __repr__(self): return "" + NO_MODEL = _NoModel() @@ -93,8 +95,10 @@ class ModelGroup(metaclass=ABCMeta): pass def __repr__(self) -> str: - return f"<{self.__class__.__name__} object at {id(self):x}: " \ - f"device={self.execution_device} >" + return ( + f"<{self.__class__.__name__} object at {id(self):x}: " + f"device={self.execution_device} >" + ) class LazilyLoadedModelGroup(ModelGroup): @@ -138,8 +142,11 @@ class LazilyLoadedModelGroup(ModelGroup): def _pre_hook(self, module: torch.nn.Module, forward_input): self.load(module) if len(forward_input) == 0: - warnings.warn(f"Hook for {module.__class__.__name__} got no input. " - f"Inputs must be positional, not keywords.", stacklevel=3) + warnings.warn( + f"Hook for {module.__class__.__name__} got no input. " + f"Inputs must be positional, not keywords.", + stacklevel=3, + ) return send_to_device(forward_input, self.execution_device) def load(self, module): @@ -154,7 +161,9 @@ class LazilyLoadedModelGroup(ModelGroup): self.clear_current_model() def _load(self, module: torch.nn.Module) -> torch.nn.Module: - assert self.is_empty(), f"A model is already loaded: {self._current_model_ref()}" + assert ( + self.is_empty() + ), f"A model is already loaded: {self._current_model_ref()}" module = module.to(self.execution_device) self.set_current_model(module) return module @@ -183,8 +192,12 @@ class LazilyLoadedModelGroup(ModelGroup): def device_for(self, model): if model not in self: - raise KeyError(f"This does not manage this model {type(model).__name__}", model) - return self.execution_device # this implementation only dispatches to one device + raise KeyError( + f"This does not manage this model {type(model).__name__}", model + ) + return ( + self.execution_device + ) # this implementation only dispatches to one device def ready(self): pass # always ready to load on-demand @@ -193,8 +206,10 @@ class LazilyLoadedModelGroup(ModelGroup): return model in self._hooks def __repr__(self) -> str: - return f"<{self.__class__.__name__} object at {id(self):x}: " \ - f"current_model={type(self._current_model_ref()).__name__} >" + return ( + f"<{self.__class__.__name__} object at {id(self):x}: " + f"current_model={type(self._current_model_ref()).__name__} >" + ) class FullyLoadedModelGroup(ModelGroup): @@ -203,6 +218,7 @@ class FullyLoadedModelGroup(ModelGroup): :py:meth:`.ready` loads _all_ the models to the execution device at once. """ + _models: weakref.WeakSet def __init__(self, execution_device: torch.device): @@ -240,8 +256,12 @@ class FullyLoadedModelGroup(ModelGroup): def device_for(self, model): if model not in self: - raise KeyError("This does not manage this model f{type(model).__name__}", model) - return self.execution_device # this implementation only dispatches to one device + raise KeyError( + "This does not manage this model f{type(model).__name__}", model + ) + return ( + self.execution_device + ) # this implementation only dispatches to one device def __contains__(self, model): return model in self._models diff --git a/invokeai/backend/stable_diffusion/textual_inversion_manager.py b/invokeai/backend/stable_diffusion/textual_inversion_manager.py index e45ea7362b..2b043afab7 100644 --- a/invokeai/backend/stable_diffusion/textual_inversion_manager.py +++ b/invokeai/backend/stable_diffusion/textual_inversion_manager.py @@ -5,10 +5,10 @@ from pathlib import Path from typing import Optional, Union import torch +from compel.embeddings_provider import BaseTextualInversionManager from picklescan.scanner import scan_file_path from transformers import CLIPTextModel, CLIPTokenizer -from compel.embeddings_provider import BaseTextualInversionManager from .concepts_lib import HuggingFaceConceptsLibrary diff --git a/invokeai/backend/stable_diffusion/x_transformer.py b/invokeai/backend/stable_diffusion/x_transformer.py index d6c4cc6881..b541d77ee2 100644 --- a/invokeai/backend/stable_diffusion/x_transformer.py +++ b/invokeai/backend/stable_diffusion/x_transformer.py @@ -1,23 +1,20 @@ """shout-out to https://github.com/lucidrains/x-transformers/tree/main/x_transformers""" -import torch -from torch import nn, einsum -import torch.nn.functional as F +from collections import namedtuple from functools import partial from inspect import isfunction -from collections import namedtuple -from einops import rearrange, repeat, reduce + +import torch +import torch.nn.functional as F +from einops import rearrange, reduce, repeat +from torch import einsum, nn # constants DEFAULT_DIM_HEAD = 64 -Intermediates = namedtuple( - 'Intermediates', ['pre_softmax_attn', 'post_softmax_attn'] -) +Intermediates = namedtuple("Intermediates", ["pre_softmax_attn", "post_softmax_attn"]) -LayerIntermediates = namedtuple( - 'Intermediates', ['hiddens', 'attn_intermediates'] -) +LayerIntermediates = namedtuple("Intermediates", ["hiddens", "attn_intermediates"]) class AbsolutePositionalEmbedding(nn.Module): @@ -38,16 +35,14 @@ class FixedPositionalEmbedding(nn.Module): def __init__(self, dim): super().__init__() inv_freq = 1.0 / (10000 ** (torch.arange(0, dim, 2).float() / dim)) - self.register_buffer('inv_freq', inv_freq) + self.register_buffer("inv_freq", inv_freq) def forward(self, x, seq_dim=1, offset=0): t = ( - torch.arange(x.shape[seq_dim], device=x.device).type_as( - self.inv_freq - ) + torch.arange(x.shape[seq_dim], device=x.device).type_as(self.inv_freq) + offset ) - sinusoid_inp = torch.einsum('i , j -> i j', t, self.inv_freq) + sinusoid_inp = torch.einsum("i , j -> i j", t, self.inv_freq) emb = torch.cat((sinusoid_inp.sin(), sinusoid_inp.cos()), dim=-1) return emb[None, :, :] @@ -187,8 +182,8 @@ class GRUGating(nn.Module): def forward(self, x, residual): gated_output = self.gru( - rearrange(x, 'b n d -> (b n) d'), - rearrange(residual, 'b n d -> (b n) d'), + rearrange(x, "b n d -> (b n) d"), + rearrange(residual, "b n d -> (b n) d"), ) return gated_output.reshape_as(x) @@ -245,7 +240,7 @@ class Attention(nn.Module): super().__init__() if use_entmax15: raise NotImplementedError( - 'Check out entmax activation instead of softmax activation!' + "Check out entmax activation instead of softmax activation!" ) self.scale = dim_head**-0.5 self.heads = heads @@ -323,37 +318,31 @@ class Attention(nn.Module): k = self.to_k(k_input) v = self.to_v(v_input) - q, k, v = map( - lambda t: rearrange(t, 'b n (h d) -> b h n d', h=h), (q, k, v) - ) + q, k, v = map(lambda t: rearrange(t, "b n (h d) -> b h n d", h=h), (q, k, v)) input_mask = None if any(map(exists, (mask, context_mask))): - q_mask = default( - mask, lambda: torch.ones((b, n), device=device).bool() - ) + q_mask = default(mask, lambda: torch.ones((b, n), device=device).bool()) k_mask = q_mask if not exists(context) else context_mask k_mask = default( k_mask, lambda: torch.ones((b, k.shape[-2]), device=device).bool(), ) - q_mask = rearrange(q_mask, 'b i -> b () i ()') - k_mask = rearrange(k_mask, 'b j -> b () () j') + q_mask = rearrange(q_mask, "b i -> b () i ()") + k_mask = rearrange(k_mask, "b j -> b () () j") input_mask = q_mask * k_mask if self.num_mem_kv > 0: mem_k, mem_v = map( - lambda t: repeat(t, 'h n d -> b h n d', b=b), + lambda t: repeat(t, "h n d -> b h n d", b=b), (self.mem_k, self.mem_v), ) k = torch.cat((mem_k, k), dim=-2) v = torch.cat((mem_v, v), dim=-2) if exists(input_mask): - input_mask = F.pad( - input_mask, (self.num_mem_kv, 0), value=True - ) + input_mask = F.pad(input_mask, (self.num_mem_kv, 0), value=True) - dots = einsum('b h i d, b h j d -> b h i j', q, k) * self.scale + dots = einsum("b h i d, b h j d -> b h i j", q, k) * self.scale mask_value = max_neg_value(dots) if exists(prev_attn): @@ -363,7 +352,7 @@ class Attention(nn.Module): if talking_heads: dots = einsum( - 'b h i j, h k -> b k i j', dots, self.pre_softmax_proj + "b h i j, h k -> b k i j", dots, self.pre_softmax_proj ).contiguous() if exists(rel_pos): @@ -376,9 +365,7 @@ class Attention(nn.Module): if self.causal: i, j = dots.shape[-2:] r = torch.arange(i, device=device) - mask = rearrange(r, 'i -> () () i ()') < rearrange( - r, 'j -> () () () j' - ) + mask = rearrange(r, "i -> () () i ()") < rearrange(r, "j -> () () () j") mask = F.pad(mask, (j - i, 0), value=False) dots.masked_fill_(mask, mask_value) del mask @@ -397,11 +384,11 @@ class Attention(nn.Module): if talking_heads: attn = einsum( - 'b h i j, h k -> b k i j', attn, self.post_softmax_proj + "b h i j, h k -> b k i j", attn, self.post_softmax_proj ).contiguous() - out = einsum('b h i j, b h j d -> b h i d', attn, v) - out = rearrange(out, 'b h n d -> b n (h d)') + out = einsum("b h i j, b h j d -> b h i d", attn, v) + out = rearrange(out, "b h n d -> b n (h d)") intermediates = Intermediates( pre_softmax_attn=pre_softmax_attn, @@ -437,10 +424,10 @@ class AttentionLayers(nn.Module): **kwargs, ): super().__init__() - ff_kwargs, kwargs = groupby_prefix_and_trim('ff_', kwargs) - attn_kwargs, _ = groupby_prefix_and_trim('attn_', kwargs) + ff_kwargs, kwargs = groupby_prefix_and_trim("ff_", kwargs) + attn_kwargs, _ = groupby_prefix_and_trim("attn_", kwargs) - dim_head = attn_kwargs.get('dim_head', DEFAULT_DIM_HEAD) + dim_head = attn_kwargs.get("dim_head", DEFAULT_DIM_HEAD) self.dim = dim self.depth = depth @@ -454,7 +441,7 @@ class AttentionLayers(nn.Module): assert ( rel_pos_num_buckets <= rel_pos_max_distance - ), 'number of relative position buckets must be less than the relative position max distance' + ), "number of relative position buckets must be less than the relative position max distance" self.rel_pos = None self.pre_norm = pre_norm @@ -470,21 +457,21 @@ class AttentionLayers(nn.Module): branch_fn = Rezero if use_rezero else None if cross_attend and not only_cross: - default_block = ('a', 'c', 'f') + default_block = ("a", "c", "f") elif cross_attend and only_cross: - default_block = ('c', 'f') + default_block = ("c", "f") else: - default_block = ('a', 'f') + default_block = ("a", "f") if macaron: - default_block = ('f',) + default_block + default_block = ("f",) + default_block if exists(custom_layers): layer_types = custom_layers elif exists(par_ratio): par_depth = depth * len(default_block) - assert 1 < par_ratio <= par_depth, 'par ratio out of range' - default_block = tuple(filter(not_equals('f'), default_block)) + assert 1 < par_ratio <= par_depth, "par ratio out of range" + default_block = tuple(filter(not_equals("f"), default_block)) par_attn = par_depth // par_ratio depth_cut = ( par_depth * 2 // 3 @@ -492,39 +479,35 @@ class AttentionLayers(nn.Module): par_width = (depth_cut + depth_cut // par_attn) // par_attn assert ( len(default_block) <= par_width - ), 'default block is too large for par_ratio' - par_block = default_block + ('f',) * ( - par_width - len(default_block) - ) + ), "default block is too large for par_ratio" + par_block = default_block + ("f",) * (par_width - len(default_block)) par_head = par_block * par_attn - layer_types = par_head + ('f',) * (par_depth - len(par_head)) + layer_types = par_head + ("f",) * (par_depth - len(par_head)) elif exists(sandwich_coef): assert ( sandwich_coef > 0 and sandwich_coef <= depth - ), 'sandwich coefficient should be less than the depth' + ), "sandwich coefficient should be less than the depth" layer_types = ( - ('a',) * sandwich_coef + ("a",) * sandwich_coef + default_block * (depth - sandwich_coef) - + ('f',) * sandwich_coef + + ("f",) * sandwich_coef ) else: layer_types = default_block * depth self.layer_types = layer_types - self.num_attn_layers = len(list(filter(equals('a'), layer_types))) + self.num_attn_layers = len(list(filter(equals("a"), layer_types))) for layer_type in self.layer_types: - if layer_type == 'a': - layer = Attention( - dim, heads=heads, causal=causal, **attn_kwargs - ) - elif layer_type == 'c': + if layer_type == "a": + layer = Attention(dim, heads=heads, causal=causal, **attn_kwargs) + elif layer_type == "c": layer = Attention(dim, heads=heads, **attn_kwargs) - elif layer_type == 'f': + elif layer_type == "f": layer = FeedForward(dim, **ff_kwargs) layer = layer if not macaron else Scale(0.5, layer) else: - raise Exception(f'invalid layer type {layer_type}') + raise Exception(f"invalid layer type {layer_type}") if isinstance(layer, Attention) and exists(branch_fn): layer = branch_fn(layer) @@ -558,7 +541,7 @@ class AttentionLayers(nn.Module): ): is_last = ind == (len(self.layers) - 1) - if layer_type == 'a': + if layer_type == "a": hiddens.append(x) layer_mem = mems.pop(0) @@ -567,7 +550,7 @@ class AttentionLayers(nn.Module): if self.pre_norm: x = norm(x) - if layer_type == 'a': + if layer_type == "a": out, inter = block( x, mask=mask, @@ -576,7 +559,7 @@ class AttentionLayers(nn.Module): prev_attn=prev_attn, mem=layer_mem, ) - elif layer_type == 'c': + elif layer_type == "c": out, inter = block( x, context=context, @@ -584,17 +567,17 @@ class AttentionLayers(nn.Module): context_mask=context_mask, prev_attn=prev_cross_attn, ) - elif layer_type == 'f': + elif layer_type == "f": out = block(x) x = residual_fn(out, residual) - if layer_type in ('a', 'c'): + if layer_type in ("a", "c"): intermediates.append(inter) - if layer_type == 'a' and self.residual_attn: + if layer_type == "a" and self.residual_attn: prev_attn = inter.pre_softmax_attn - elif layer_type == 'c' and self.cross_residual_attn: + elif layer_type == "c" and self.cross_residual_attn: prev_cross_attn = inter.pre_softmax_attn if not self.pre_norm and not is_last: @@ -612,7 +595,7 @@ class AttentionLayers(nn.Module): class Encoder(AttentionLayers): def __init__(self, **kwargs): - assert 'causal' not in kwargs, 'cannot set causality on encoder' + assert "causal" not in kwargs, "cannot set causality on encoder" super().__init__(causal=False, **kwargs) @@ -633,7 +616,7 @@ class TransformerWrapper(nn.Module): super().__init__() assert isinstance( attn_layers, AttentionLayers - ), 'attention layers must be one of Encoder or Decoder' + ), "attention layers must be one of Encoder or Decoder" dim = attn_layers.dim emb_dim = default(emb_dim, dim) @@ -650,9 +633,7 @@ class TransformerWrapper(nn.Module): ) self.emb_dropout = nn.Dropout(emb_dropout) - self.project_emb = ( - nn.Linear(emb_dim, dim) if emb_dim != dim else nn.Identity() - ) + self.project_emb = nn.Linear(emb_dim, dim) if emb_dim != dim else nn.Identity() self.attn_layers = attn_layers self.norm = nn.LayerNorm(dim) @@ -668,12 +649,10 @@ class TransformerWrapper(nn.Module): num_memory_tokens = default(num_memory_tokens, 0) self.num_memory_tokens = num_memory_tokens if num_memory_tokens > 0: - self.memory_tokens = nn.Parameter( - torch.randn(num_memory_tokens, dim) - ) + self.memory_tokens = nn.Parameter(torch.randn(num_memory_tokens, dim)) # let funnel encoder know number of memory tokens, if specified - if hasattr(attn_layers, 'num_memory_tokens'): + if hasattr(attn_layers, "num_memory_tokens"): attn_layers.num_memory_tokens = num_memory_tokens def init_(self): @@ -705,7 +684,7 @@ class TransformerWrapper(nn.Module): x = self.project_emb(x) if num_mem > 0: - mem = repeat(self.memory_tokens, 'n d -> b n d', b=b) + mem = repeat(self.memory_tokens, "n d -> b n d", b=b) x = torch.cat((mem, x), dim=1) # auto-handle masking after appending memory tokens @@ -734,9 +713,7 @@ class TransformerWrapper(nn.Module): else hiddens ) new_mems = list( - map( - lambda t: t[..., -self.max_mem_len :, :].detach(), new_mems - ) + map(lambda t: t[..., -self.max_mem_len :, :].detach(), new_mems) ) return out, new_mems diff --git a/invokeai/backend/training/__init.py__ b/invokeai/backend/training/__init__.py similarity index 94% rename from invokeai/backend/training/__init.py__ rename to invokeai/backend/training/__init__.py index 16bde19390..a85842dc72 100644 --- a/invokeai/backend/training/__init.py__ +++ b/invokeai/backend/training/__init__.py @@ -1,4 +1,4 @@ -''' +""" Initialization file for invokeai.backend.training -''' +""" from .textual_inversion_training import do_textual_inversion_training, parse_args diff --git a/invokeai/backend/training/textual_inversion_training.py b/invokeai/backend/training/textual_inversion_training.py index 4e9bd36eef..cd45fa894b 100644 --- a/invokeai/backend/training/textual_inversion_training.py +++ b/invokeai/backend/training/textual_inversion_training.py @@ -47,8 +47,8 @@ from tqdm.auto import tqdm from transformers import CLIPTextModel, CLIPTokenizer # invokeai stuff -from ldm.invoke.args import ArgFormatter, PagingArgumentParser -from invokeai.backend.globals import Globals, global_cache_dir +from ..args import ArgFormatter, PagingArgumentParser +from ..globals import Globals, global_cache_dir if version.parse(version.parse(PIL.__version__).base_version) >= version.parse("9.1.0"): PIL_INTERPOLATION = { @@ -441,7 +441,10 @@ class TextualInversionDataset(Dataset): self.image_paths = [ self.data_root / file_path for file_path in self.data_root.iterdir() - if file_path.is_file() and file_path.name.endswith(('.png','.PNG','.jpg','.JPG','.jpeg','.JPEG','.gif','.GIF')) + if file_path.is_file() + and file_path.name.endswith( + (".png", ".PNG", ".jpg", ".JPG", ".jpeg", ".JPEG", ".gif", ".GIF") + ) ] self.num_images = len(self.image_paths) @@ -490,7 +493,10 @@ class TextualInversionDataset(Dataset): if self.center_crop: crop = min(img.shape[0], img.shape[1]) - h, w, = ( + ( + h, + w, + ) = ( img.shape[0], img.shape[1], ) diff --git a/invokeai/backend/util/__init__.py b/invokeai/backend/util/__init__.py index 434e03c273..ca42f86fd6 100644 --- a/invokeai/backend/util/__init__.py +++ b/invokeai/backend/util/__init__.py @@ -1,18 +1,19 @@ -''' +""" Initialization file for invokeai.backend.util -''' -from .devices import (choose_torch_device, - choose_precision, - normalize_device, - torch_dtype, - CPU_DEVICE, - CUDA_DEVICE, - MPS_DEVICE, - ) -from .util import (ask_user, - download_with_resume, - instantiate_from_config, - url_attachment_name, - ) +""" +from .devices import ( + CPU_DEVICE, + CUDA_DEVICE, + MPS_DEVICE, + choose_precision, + choose_torch_device, + normalize_device, + torch_dtype, +) from .log import write_log - +from .util import ( + ask_user, + download_with_resume, + instantiate_from_config, + url_attachment_name, +) diff --git a/invokeai/backend/util/devices.py b/invokeai/backend/util/devices.py index 167847eae0..c70a43ff09 100644 --- a/invokeai/backend/util/devices.py +++ b/invokeai/backend/util/devices.py @@ -11,46 +11,51 @@ CPU_DEVICE = torch.device("cpu") CUDA_DEVICE = torch.device("cuda") MPS_DEVICE = torch.device("mps") + def choose_torch_device() -> torch.device: - '''Convenience routine for guessing which GPU device to run model on''' + """Convenience routine for guessing which GPU device to run model on""" if Globals.always_use_cpu: return CPU_DEVICE if torch.cuda.is_available(): - return torch.device('cuda') - if hasattr(torch.backends, 'mps') and torch.backends.mps.is_available(): - return torch.device('mps') + return torch.device("cuda") + if hasattr(torch.backends, "mps") and torch.backends.mps.is_available(): + return torch.device("mps") return CPU_DEVICE + def choose_precision(device: torch.device) -> str: - '''Returns an appropriate precision for the given torch device''' - if device.type == 'cuda': + """Returns an appropriate precision for the given torch device""" + if device.type == "cuda": device_name = torch.cuda.get_device_name(device) - if not ('GeForce GTX 1660' in device_name or 'GeForce GTX 1650' in device_name): - return 'float16' - return 'float32' + if not ("GeForce GTX 1660" in device_name or "GeForce GTX 1650" in device_name): + return "float16" + return "float32" + def torch_dtype(device: torch.device) -> torch.dtype: if Globals.full_precision: return torch.float32 - if choose_precision(device) == 'float16': + if choose_precision(device) == "float16": return torch.float16 else: return torch.float32 + def choose_autocast(precision): - '''Returns an autocast context or nullcontext for the given precision string''' + """Returns an autocast context or nullcontext for the given precision string""" # float16 currently requires autocast to avoid errors like: # 'expected scalar type Half but found Float' - if precision == 'autocast' or precision == 'float16': + if precision == "autocast" or precision == "float16": return autocast return nullcontext + def normalize_device(device: str | torch.device) -> torch.device: """Ensure device has a device index defined, if appropriate.""" device = torch.device(device) if device.index is None: # cuda might be the only torch backend that currently uses the device index? # I don't see anything like `current_device` for cpu or mps. - if device.type == 'cuda': + if device.type == "cuda": device = torch.device(device.type, torch.cuda.current_device()) return device diff --git a/invokeai/backend/util/log.py b/invokeai/backend/util/log.py index 8aebe62671..4643f61a6b 100644 --- a/invokeai/backend/util/log.py +++ b/invokeai/backend/util/log.py @@ -25,14 +25,15 @@ def write_log_message(results, output_cntr): if len(results) == 0: return output_cntr log_lines = [f"{path}: {prompt}\n" for path, prompt in results] - if len(log_lines)>1: + if len(log_lines) > 1: subcntr = 1 for l in log_lines: - print(f"[{output_cntr}.{subcntr}] {l}", end="") - subcntr += 1 + print(f"[{output_cntr}.{subcntr}] {l}", end="") + subcntr += 1 else: - print(f"[{output_cntr}] {log_lines[0]}", end="") - return output_cntr+1 + print(f"[{output_cntr}] {log_lines[0]}", end="") + return output_cntr + 1 + def write_log_files(results, log_path, file_types): for file_type in file_types: diff --git a/invokeai/backend/util/util.py b/invokeai/backend/util/util.py index 2bd24d3ef0..3fab6e18b0 100644 --- a/invokeai/backend/util/util.py +++ b/invokeai/backend/util/util.py @@ -17,6 +17,7 @@ from tqdm import tqdm from .devices import torch_dtype + def log_txt_as_img(wh, xc, size=10): # wh a tuple of (width, height) # xc a list of captions to plot @@ -281,14 +282,14 @@ def ask_user(question: str, answers: list): # ------------------------------------- def download_with_resume(url: str, dest: Path, access_token: str = None) -> Path: - ''' + """ Download a model file. :param url: https, http or ftp URL :param dest: A Path object. If path exists and is a directory, then we try to derive the filename from the URL's Content-Disposition header and copy the URL contents into dest/filename :param access_token: Access token to access this resource - ''' + """ header = {"Authorization": f"Bearer {access_token}"} if access_token else {} open_mode = "wb" exist_size = 0 @@ -298,7 +299,9 @@ def download_with_resume(url: str, dest: Path, access_token: str = None) -> Path if dest.is_dir(): try: - file_name = re.search('filename="(.+)"', resp.headers.get("Content-Disposition")).group(1) + file_name = re.search( + 'filename="(.+)"', resp.headers.get("Content-Disposition") + ).group(1) except: file_name = os.path.basename(url) dest = dest / file_name @@ -309,16 +312,14 @@ def download_with_resume(url: str, dest: Path, access_token: str = None) -> Path exist_size = dest.stat().st_size header["Range"] = f"bytes={exist_size}-" open_mode = "ab" - resp = requests.get(url, headers=header, stream=True) # new request with range + resp = requests.get(url, headers=header, stream=True) # new request with range if exist_size > content_length: - print('* corrupt existing file found. re-downloading') + print("* corrupt existing file found. re-downloading") os.remove(dest) exist_size = 0 - if ( - resp.status_code == 416 or exist_size == content_length - ): + if resp.status_code == 416 or exist_size == content_length: print(f"* {dest}: complete file found. Skipping.") return dest elif resp.status_code == 206 or exist_size > 0: @@ -334,12 +335,12 @@ def download_with_resume(url: str, dest: Path, access_token: str = None) -> Path return None with open(dest, open_mode) as file, tqdm( - desc=str(dest), - initial=exist_size, - total=content_length, - unit="iB", - unit_scale=True, - unit_divisor=1000, + desc=str(dest), + initial=exist_size, + total=content_length, + unit="iB", + unit_scale=True, + unit_divisor=1000, ) as bar: for data in resp.iter_content(chunk_size=1024): size = file.write(data) diff --git a/invokeai/backend/web/__init__.py b/invokeai/backend/web/__init__.py index ef771f61be..c57600f72b 100644 --- a/invokeai/backend/web/__init__.py +++ b/invokeai/backend/web/__init__.py @@ -1,4 +1,4 @@ -''' +""" Initialization file for the web backend. -''' +""" from .invoke_ai_web_server import InvokeAIWebServer diff --git a/invokeai/backend/web/invoke_ai_web_server.py b/invokeai/backend/web/invoke_ai_web_server.py index c05de2f831..80c9618098 100644 --- a/invokeai/backend/web/invoke_ai_web_server.py +++ b/invokeai/backend/web/invoke_ai_web_server.py @@ -12,32 +12,29 @@ from threading import Event from uuid import uuid4 import eventlet -import invokeai.frontend.web.dist as frontend +from compel.prompt_parser import Blend +from flask import Flask, make_response, redirect, request, send_from_directory +from flask_socketio import SocketIO from PIL import Image from PIL.Image import Image as ImageType -from compel.prompt_parser import Blend -from flask import Flask, redirect, send_from_directory, request, make_response -from flask_socketio import SocketIO from werkzeug.utils import secure_filename -from .modules.get_canvas_generation_mode import ( - get_canvas_generation_mode, -) -from .modules.parameters import parameters_to_command -from ..prompting import (get_tokens_for_prompt_object, - get_prompt_structure, - get_tokenizer - ) -from ..image_util import PngWriter, retrieve_metadata -from ..generator import infill_methods -from ..stable_diffusion import PipelineIntermediateState +import invokeai.frontend.web.dist as frontend from .. import Generate -from ..args import Args, APP_ID, APP_VERSION, calculate_init_img_hash -from ..globals import ( Globals, global_converted_ckpts_dir, - global_models_dir - ) +from ..args import APP_ID, APP_VERSION, Args, calculate_init_img_hash +from ..generator import infill_methods +from ..globals import Globals, global_converted_ckpts_dir, global_models_dir +from ..image_util import PngWriter, retrieve_metadata from ..model_management import merge_diffusion_models +from ..prompting import ( + get_prompt_structure, + get_tokenizer, + get_tokens_for_prompt_object, +) +from ..stable_diffusion import PipelineIntermediateState +from .modules.get_canvas_generation_mode import get_canvas_generation_mode +from .modules.parameters import parameters_to_command # Loading Arguments opt = Args() @@ -197,8 +194,7 @@ class InvokeAIWebServer: (width, height) = pil_image.size thumbnail_path = save_thumbnail( - pil_image, os.path.basename( - file_path), self.thumbnail_image_path + pil_image, os.path.basename(file_path), self.thumbnail_image_path ) response = { @@ -228,7 +224,7 @@ class InvokeAIWebServer: server="flask_socketio", width=1600, height=1000, - port=self.port + port=self.port, ).run() except KeyboardInterrupt: import sys @@ -269,16 +265,14 @@ class InvokeAIWebServer: # location for "finished" images self.result_path = args.outdir # temporary path for intermediates - self.intermediate_path = os.path.join( - self.result_path, "intermediates/") + self.intermediate_path = os.path.join(self.result_path, "intermediates/") # path for user-uploaded init images and masks self.init_image_path = os.path.join(self.result_path, "init-images/") self.mask_image_path = os.path.join(self.result_path, "mask-images/") # path for temp images e.g. gallery generations which are not committed self.temp_image_path = os.path.join(self.result_path, "temp-images/") # path for thumbnail images - self.thumbnail_image_path = os.path.join( - self.result_path, "thumbnails/") + self.thumbnail_image_path = os.path.join(self.result_path, "thumbnails/") # txt log self.log_path = os.path.join(self.result_path, "invoke_log.txt") # make all output paths @@ -303,21 +297,22 @@ class InvokeAIWebServer: config["infill_methods"] = infill_methods() socketio.emit("systemConfig", config) - @socketio.on('searchForModels') + @socketio.on("searchForModels") def handle_search_models(search_folder: str): try: if not search_folder: socketio.emit( "foundModels", - {'search_folder': None, 'found_models': None}, + {"search_folder": None, "found_models": None}, ) else: - search_folder, found_models = self.generate.model_manager.search_models( - search_folder) + ( + search_folder, + found_models, + ) = self.generate.model_manager.search_models(search_folder) socketio.emit( "foundModels", - {'search_folder': search_folder, - 'found_models': found_models}, + {"search_folder": search_folder, "found_models": found_models}, ) except Exception as e: self.handle_exceptions(e) @@ -326,11 +321,11 @@ class InvokeAIWebServer: @socketio.on("addNewModel") def handle_add_model(new_model_config: dict): try: - model_name = new_model_config['name'] - del new_model_config['name'] + model_name = new_model_config["name"] + del new_model_config["name"] model_attributes = new_model_config - if len(model_attributes['vae']) == 0: - del model_attributes['vae'] + if len(model_attributes["vae"]) == 0: + del model_attributes["vae"] update = False current_model_list = self.generate.model_manager.list_models() if model_name in current_model_list: @@ -339,14 +334,20 @@ class InvokeAIWebServer: print(f">> Adding New Model: {model_name}") self.generate.model_manager.add_model( - model_name=model_name, model_attributes=model_attributes, clobber=True) + model_name=model_name, + model_attributes=model_attributes, + clobber=True, + ) self.generate.model_manager.commit(opt.conf) new_model_list = self.generate.model_manager.list_models() socketio.emit( "newModelAdded", - {"new_model_name": model_name, - "model_list": new_model_list, 'update': update}, + { + "new_model_name": model_name, + "model_list": new_model_list, + "update": update, + }, ) print(f">> New Model Added: {model_name}") except Exception as e: @@ -361,8 +362,10 @@ class InvokeAIWebServer: updated_model_list = self.generate.model_manager.list_models() socketio.emit( "modelDeleted", - {"deleted_model_name": model_name, - "model_list": updated_model_list}, + { + "deleted_model_name": model_name, + "model_list": updated_model_list, + }, ) print(f">> Model Deleted: {model_name}") except Exception as e: @@ -387,41 +390,48 @@ class InvokeAIWebServer: except Exception as e: self.handle_exceptions(e) - @socketio.on('convertToDiffusers') + @socketio.on("convertToDiffusers") def convert_to_diffusers(model_to_convert: dict): try: - if (model_info := self.generate.model_manager.model_info(model_name=model_to_convert['model_name'])): - if 'weights' in model_info: - ckpt_path = Path(model_info['weights']) - original_config_file = Path(model_info['config']) - model_name = model_to_convert['model_name'] - model_description = model_info['description'] + if model_info := self.generate.model_manager.model_info( + model_name=model_to_convert["model_name"] + ): + if "weights" in model_info: + ckpt_path = Path(model_info["weights"]) + original_config_file = Path(model_info["config"]) + model_name = model_to_convert["model_name"] + model_description = model_info["description"] else: self.socketio.emit( - "error", {"message": "Model is not a valid checkpoint file"}) + "error", {"message": "Model is not a valid checkpoint file"} + ) else: self.socketio.emit( - "error", {"message": "Could not retrieve model info."}) + "error", {"message": "Could not retrieve model info."} + ) if not ckpt_path.is_absolute(): ckpt_path = Path(Globals.root, ckpt_path) if original_config_file and not original_config_file.is_absolute(): - original_config_file = Path( - Globals.root, original_config_file) + original_config_file = Path(Globals.root, original_config_file) diffusers_path = Path( - ckpt_path.parent.absolute(), - f'{model_name}_diffusers' + ckpt_path.parent.absolute(), f"{model_name}_diffusers" ) - if model_to_convert['save_location'] == 'root': + if model_to_convert["save_location"] == "root": diffusers_path = Path( - global_converted_ckpts_dir(), f'{model_name}_diffusers') + global_converted_ckpts_dir(), f"{model_name}_diffusers" + ) - if model_to_convert['save_location'] == 'custom' and model_to_convert['custom_location'] is not None: + if ( + model_to_convert["save_location"] == "custom" + and model_to_convert["custom_location"] is not None + ): diffusers_path = Path( - model_to_convert['custom_location'], f'{model_name}_diffusers') + model_to_convert["custom_location"], f"{model_name}_diffusers" + ) if diffusers_path.exists(): shutil.rmtree(diffusers_path) @@ -439,54 +449,67 @@ class InvokeAIWebServer: new_model_list = self.generate.model_manager.list_models() socketio.emit( "modelConverted", - {"new_model_name": model_name, - "model_list": new_model_list, 'update': True}, + { + "new_model_name": model_name, + "model_list": new_model_list, + "update": True, + }, ) print(f">> Model Converted: {model_name}") except Exception as e: self.handle_exceptions(e) - @socketio.on('mergeDiffusersModels') + @socketio.on("mergeDiffusersModels") def merge_diffusers_models(model_merge_info: dict): try: - models_to_merge = model_merge_info['models_to_merge'] + models_to_merge = model_merge_info["models_to_merge"] model_ids_or_paths = [ - self.generate.model_manager.model_name_or_path(x) for x in models_to_merge] + self.generate.model_manager.model_name_or_path(x) + for x in models_to_merge + ] merged_pipe = merge_diffusion_models( - model_ids_or_paths, model_merge_info['alpha'], model_merge_info['interp'], model_merge_info['force']) + model_ids_or_paths, + model_merge_info["alpha"], + model_merge_info["interp"], + model_merge_info["force"], + ) - dump_path = global_models_dir() / 'merged_models' - if model_merge_info['model_merge_save_path'] is not None: - dump_path = Path(model_merge_info['model_merge_save_path']) + dump_path = global_models_dir() / "merged_models" + if model_merge_info["model_merge_save_path"] is not None: + dump_path = Path(model_merge_info["model_merge_save_path"]) os.makedirs(dump_path, exist_ok=True) - dump_path = dump_path / model_merge_info['merged_model_name'] + dump_path = dump_path / model_merge_info["merged_model_name"] merged_pipe.save_pretrained(dump_path, safe_serialization=1) merged_model_config = dict( - model_name=model_merge_info['merged_model_name'], + model_name=model_merge_info["merged_model_name"], description=f'Merge of models {", ".join(models_to_merge)}', - commit_to_conf=opt.conf + commit_to_conf=opt.conf, ) - if vae := self.generate.model_manager.config[models_to_merge[0]].get("vae", None): - print( - f">> Using configured VAE assigned to {models_to_merge[0]}") + if vae := self.generate.model_manager.config[models_to_merge[0]].get( + "vae", None + ): + print(f">> Using configured VAE assigned to {models_to_merge[0]}") merged_model_config.update(vae=vae) self.generate.model_manager.import_diffuser_model( - dump_path, **merged_model_config) + dump_path, **merged_model_config + ) new_model_list = self.generate.model_manager.list_models() socketio.emit( "modelsMerged", - {"merged_models": models_to_merge, - "merged_model_name": model_merge_info['merged_model_name'], - "model_list": new_model_list, 'update': True}, + { + "merged_models": models_to_merge, + "merged_model_name": model_merge_info["merged_model_name"], + "model_list": new_model_list, + "update": True, + }, ) print(f">> Models Merged: {models_to_merge}") - print( - f">> New Model Added: {model_merge_info['merged_model_name']}") + print(f">> New Model Added: {model_merge_info['merged_model_name']}") except Exception as e: self.handle_exceptions(e) @@ -504,7 +527,8 @@ class InvokeAIWebServer: os.remove(thumbnail_path) except Exception as e: socketio.emit( - "error", {"message": f"Unable to delete {f}: {str(e)}"}) + "error", {"message": f"Unable to delete {f}: {str(e)}"} + ) pass socketio.emit("tempFolderEmptied") @@ -515,8 +539,7 @@ class InvokeAIWebServer: def save_temp_image_to_gallery(url): try: image_path = self.get_image_path_from_url(url) - new_path = os.path.join( - self.result_path, os.path.basename(image_path)) + new_path = os.path.join(self.result_path, os.path.basename(image_path)) shutil.copy2(image_path, new_path) if os.path.splitext(new_path)[1] == ".png": @@ -529,8 +552,7 @@ class InvokeAIWebServer: (width, height) = pil_image.size thumbnail_path = save_thumbnail( - pil_image, os.path.basename( - new_path), self.thumbnail_image_path + pil_image, os.path.basename(new_path), self.thumbnail_image_path ) image_array = [ @@ -589,8 +611,7 @@ class InvokeAIWebServer: (width, height) = pil_image.size thumbnail_path = save_thumbnail( - pil_image, os.path.basename( - path), self.thumbnail_image_path + pil_image, os.path.basename(path), self.thumbnail_image_path ) image_array.append( @@ -609,7 +630,8 @@ class InvokeAIWebServer: ) except Exception as e: socketio.emit( - "error", {"message": f"Unable to load {path}: {str(e)}"}) + "error", {"message": f"Unable to load {path}: {str(e)}"} + ) pass socketio.emit( @@ -659,8 +681,7 @@ class InvokeAIWebServer: (width, height) = pil_image.size thumbnail_path = save_thumbnail( - pil_image, os.path.basename( - path), self.thumbnail_image_path + pil_image, os.path.basename(path), self.thumbnail_image_path ) image_array.append( @@ -680,7 +701,8 @@ class InvokeAIWebServer: except Exception as e: print(f">> Unable to load {path}") socketio.emit( - "error", {"message": f"Unable to load {path}: {str(e)}"}) + "error", {"message": f"Unable to load {path}: {str(e)}"} + ) pass socketio.emit( @@ -714,10 +736,9 @@ class InvokeAIWebServer: printable_parameters["init_mask"][:64] + "..." ) - print( - f'\n>> Image Generation Parameters:\n\n{printable_parameters}\n') - print(f'>> ESRGAN Parameters: {esrgan_parameters}') - print(f'>> Facetool Parameters: {facetool_parameters}') + print(f"\n>> Image Generation Parameters:\n\n{printable_parameters}\n") + print(f">> ESRGAN Parameters: {esrgan_parameters}") + print(f">> Facetool Parameters: {facetool_parameters}") self.generate_images( generation_parameters, @@ -754,11 +775,9 @@ class InvokeAIWebServer: if postprocessing_parameters["type"] == "esrgan": progress.set_current_status("common.statusUpscalingESRGAN") elif postprocessing_parameters["type"] == "gfpgan": - progress.set_current_status( - "common.statusRestoringFacesGFPGAN") + progress.set_current_status("common.statusRestoringFacesGFPGAN") elif postprocessing_parameters["type"] == "codeformer": - progress.set_current_status( - "common.statusRestoringFacesCodeFormer") + progress.set_current_status("common.statusRestoringFacesCodeFormer") socketio.emit("progressUpdate", progress.to_formatted_dict()) eventlet.sleep(0) @@ -923,8 +942,7 @@ class InvokeAIWebServer: init_img_url = generation_parameters["init_img"] - original_bounding_box = generation_parameters["bounding_box"].copy( - ) + original_bounding_box = generation_parameters["bounding_box"].copy() initial_image = dataURL_to_image( generation_parameters["init_img"] @@ -1001,8 +1019,9 @@ class InvokeAIWebServer: elif generation_parameters["generation_mode"] == "img2img": init_img_url = generation_parameters["init_img"] init_img_path = self.get_image_path_from_url(init_img_url) - generation_parameters["init_img"] = Image.open( - init_img_path).convert('RGB') + generation_parameters["init_img"] = Image.open(init_img_path).convert( + "RGB" + ) def image_progress(sample, step): if self.canceled.is_set(): @@ -1062,8 +1081,7 @@ class InvokeAIWebServer: ) if generation_parameters["progress_latents"]: - image = self.generate.sample_to_lowres_estimated_image( - sample) + image = self.generate.sample_to_lowres_estimated_image(sample) (width, height) = image.size width *= 8 height *= 8 @@ -1082,8 +1100,7 @@ class InvokeAIWebServer: }, ) - self.socketio.emit( - "progressUpdate", progress.to_formatted_dict()) + self.socketio.emit("progressUpdate", progress.to_formatted_dict()) eventlet.sleep(0) def image_done(image, seed, first_seed, attention_maps_image=None): @@ -1110,8 +1127,7 @@ class InvokeAIWebServer: progress.set_current_status("common.statusGenerationComplete") - self.socketio.emit( - "progressUpdate", progress.to_formatted_dict()) + self.socketio.emit("progressUpdate", progress.to_formatted_dict()) eventlet.sleep(0) all_parameters = generation_parameters @@ -1122,8 +1138,7 @@ class InvokeAIWebServer: and all_parameters["variation_amount"] > 0 ): first_seed = first_seed or seed - this_variation = [ - [seed, all_parameters["variation_amount"]]] + this_variation = [[seed, all_parameters["variation_amount"]]] all_parameters["with_variations"] = ( prior_variations + this_variation ) @@ -1139,14 +1154,13 @@ class InvokeAIWebServer: if esrgan_parameters: progress.set_current_status("common.statusUpscaling") progress.set_current_status_has_steps(False) - self.socketio.emit( - "progressUpdate", progress.to_formatted_dict()) + self.socketio.emit("progressUpdate", progress.to_formatted_dict()) eventlet.sleep(0) image = self.esrgan.process( image=image, upsampler_scale=esrgan_parameters["level"], - denoise_str=esrgan_parameters['denoise_str'], + denoise_str=esrgan_parameters["denoise_str"], strength=esrgan_parameters["strength"], seed=seed, ) @@ -1154,7 +1168,7 @@ class InvokeAIWebServer: postprocessing = True all_parameters["upscale"] = [ esrgan_parameters["level"], - esrgan_parameters['denoise_str'], + esrgan_parameters["denoise_str"], esrgan_parameters["strength"], ] @@ -1163,15 +1177,14 @@ class InvokeAIWebServer: if facetool_parameters: if facetool_parameters["type"] == "gfpgan": - progress.set_current_status( - "common.statusRestoringFacesGFPGAN") + progress.set_current_status("common.statusRestoringFacesGFPGAN") elif facetool_parameters["type"] == "codeformer": progress.set_current_status( - "common.statusRestoringFacesCodeFormer") + "common.statusRestoringFacesCodeFormer" + ) progress.set_current_status_has_steps(False) - self.socketio.emit( - "progressUpdate", progress.to_formatted_dict()) + self.socketio.emit("progressUpdate", progress.to_formatted_dict()) eventlet.sleep(0) if facetool_parameters["type"] == "gfpgan": @@ -1201,8 +1214,7 @@ class InvokeAIWebServer: all_parameters["facetool_type"] = facetool_parameters["type"] progress.set_current_status("common.statusSavingImage") - self.socketio.emit( - "progressUpdate", progress.to_formatted_dict()) + self.socketio.emit("progressUpdate", progress.to_formatted_dict()) eventlet.sleep(0) # restore the stashed URLS and discard the paths, we are about to send the result to client @@ -1219,8 +1231,7 @@ class InvokeAIWebServer: if generation_parameters["generation_mode"] == "unifiedCanvas": all_parameters["bounding_box"] = original_bounding_box - metadata = self.parameters_to_generated_image_metadata( - all_parameters) + metadata = self.parameters_to_generated_image_metadata(all_parameters) command = parameters_to_command(all_parameters) @@ -1250,22 +1261,27 @@ class InvokeAIWebServer: if progress.total_iterations > progress.current_iteration: progress.set_current_step(1) - progress.set_current_status( - "common.statusIterationComplete") + progress.set_current_status("common.statusIterationComplete") progress.set_current_status_has_steps(False) else: progress.mark_complete() - self.socketio.emit( - "progressUpdate", progress.to_formatted_dict()) + self.socketio.emit("progressUpdate", progress.to_formatted_dict()) eventlet.sleep(0) - parsed_prompt, _ = get_prompt_structure( - generation_parameters["prompt"]) - tokens = None if type(parsed_prompt) is Blend else \ - get_tokens_for_prompt_object(get_tokenizer(self.generate.model), parsed_prompt) - attention_maps_image_base64_url = None if attention_maps_image is None \ + parsed_prompt, _ = get_prompt_structure(generation_parameters["prompt"]) + tokens = ( + None + if type(parsed_prompt) is Blend + else get_tokens_for_prompt_object( + get_tokenizer(self.generate.model), parsed_prompt + ) + ) + attention_maps_image_base64_url = ( + None + if attention_maps_image is None else image_to_dataURL(attention_maps_image) + ) self.socketio.emit( "generationResult", @@ -1297,7 +1313,7 @@ class InvokeAIWebServer: self.generate.prompt2image( **generation_parameters, step_callback=diffusers_step_callback_adapter, - image_callback=image_done + image_callback=image_done, ) except KeyboardInterrupt: @@ -1420,8 +1436,7 @@ class InvokeAIWebServer: self, parameters, original_image_path ): try: - current_metadata = retrieve_metadata( - original_image_path)["sd-metadata"] + current_metadata = retrieve_metadata(original_image_path)["sd-metadata"] postprocessing_metadata = {} """ @@ -1461,8 +1476,7 @@ class InvokeAIWebServer: postprocessing_metadata ) else: - current_metadata["image"]["postprocessing"] = [ - postprocessing_metadata] + current_metadata["image"]["postprocessing"] = [postprocessing_metadata] return current_metadata @@ -1558,8 +1572,7 @@ class InvokeAIWebServer: ) elif "thumbnails" in url: return os.path.abspath( - os.path.join(self.thumbnail_image_path, - os.path.basename(url)) + os.path.join(self.thumbnail_image_path, os.path.basename(url)) ) else: return os.path.abspath( @@ -1605,7 +1618,7 @@ class InvokeAIWebServer: except Exception as e: self.handle_exceptions(e) - def handle_exceptions(self, exception, emit_key: str = 'error'): + def handle_exceptions(self, exception, emit_key: str = "error"): self.socketio.emit(emit_key, {"message": (str(exception))}) print("\n") traceback.print_exc() @@ -1719,7 +1732,7 @@ def dataURL_to_image(dataURL: str) -> ImageType: return image -def image_to_dataURL(image: ImageType, image_format:str="PNG") -> str: +def image_to_dataURL(image: ImageType, image_format: str = "PNG") -> str: """ Converts an image into a base64 image dataURL. """ diff --git a/invokeai/backend/web/modules/create_cmd_parser.py b/invokeai/backend/web/modules/create_cmd_parser.py index 919caeda91..856522989b 100644 --- a/invokeai/backend/web/modules/create_cmd_parser.py +++ b/invokeai/backend/web/modules/create_cmd_parser.py @@ -1,6 +1,7 @@ import argparse import os -from ldm.invoke.args import PRECISION_CHOICES + +from ...args import PRECISION_CHOICES def create_cmd_parser(): @@ -46,10 +47,10 @@ def create_cmd_parser(): default="auto", ) parser.add_argument( - '--free_gpu_mem', - dest='free_gpu_mem', - action='store_true', - help='Force free gpu memory before final decoding', + "--free_gpu_mem", + dest="free_gpu_mem", + action="store_true", + help="Force free gpu memory before final decoding", ) return parser diff --git a/invokeai/backend/web/modules/get_canvas_generation_mode.py b/invokeai/backend/web/modules/get_canvas_generation_mode.py index 602cef09d5..55955cc33c 100644 --- a/invokeai/backend/web/modules/get_canvas_generation_mode.py +++ b/invokeai/backend/web/modules/get_canvas_generation_mode.py @@ -1,6 +1,8 @@ +from typing import Literal, Union + from PIL import Image, ImageChops from PIL.Image import Image as ImageType -from typing import Union, Literal + # https://stackoverflow.com/questions/43864101/python-pil-check-if-image-is-transparent def check_for_any_transparency(img: Union[ImageType, str]) -> bool: @@ -85,9 +87,7 @@ def main(): print( "IMAGE WITH TRANSPARENCY, NO MASK, expect outpainting, got ", - get_canvas_generation_mode( - init_img_partial_transparency, init_mask_no_mask - ), + get_canvas_generation_mode(init_img_partial_transparency, init_mask_no_mask), ) print( @@ -102,9 +102,7 @@ def main(): print( "IMAGE WITH TRANSPARENCY, WITH MASK, expect outpainting, got ", - get_canvas_generation_mode( - init_img_partial_transparency, init_mask_has_mask - ), + get_canvas_generation_mode(init_img_partial_transparency, init_mask_has_mask), ) print( diff --git a/invokeai/backend/web/modules/parameters.py b/invokeai/backend/web/modules/parameters.py index a650fa37f9..3c9c530dd2 100644 --- a/invokeai/backend/web/modules/parameters.py +++ b/invokeai/backend/web/modules/parameters.py @@ -1,6 +1,7 @@ -from .parse_seed_weights import parse_seed_weights import argparse +from .parse_seed_weights import parse_seed_weights + SAMPLER_CHOICES = [ "ddim", "k_dpm_2_a", diff --git a/invokeai/configs/stable-diffusion/v1-finetune.yaml b/invokeai/configs/stable-diffusion/v1-finetune.yaml index 96e7dd338a..8bbdb52159 100644 --- a/invokeai/configs/stable-diffusion/v1-finetune.yaml +++ b/invokeai/configs/stable-diffusion/v1-finetune.yaml @@ -1,6 +1,6 @@ model: base_learning_rate: 5.0e-03 - target: invokeai.backend.ldm.models.diffusion.ddpm.LatentDiffusion + target: invokeai.backend.stable_diffusion.diffusion.ddpm.LatentDiffusion params: linear_start: 0.00085 linear_end: 0.0120 @@ -19,7 +19,7 @@ model: embedding_reg_weight: 0.0 personalization_config: - target: invokeai.backend.ldm.modules.embedding_manager.EmbeddingManager + target: invokeai.backend.stable_diffusion.embedding_manager.EmbeddingManager params: placeholder_strings: ["*"] initializer_words: ["sculpture"] @@ -28,7 +28,7 @@ model: progressive_words: False unet_config: - target: invokeai.backend.ldm.modules.diffusionmodules.openaimodel.UNetModel + target: invokeai.backend.stable_diffusion.diffusionmodules.openaimodel.UNetModel params: image_size: 32 # unused in_channels: 4 @@ -45,7 +45,7 @@ model: legacy: False first_stage_config: - target: invokeai.models.autoencoder.AutoencoderKL + target: invokeai.backend.stable_diffusion.autoencoder.AutoencoderKL params: embed_dim: 4 monitor: val/rec_loss @@ -68,7 +68,7 @@ model: target: torch.nn.Identity cond_stage_config: - target: invokeai.backend.ldm.modules.encoders.modules.FrozenCLIPEmbedder + target: invokeai.backend.stable_diffusion.encoders.modules.FrozenCLIPEmbedder data: target: main.DataModuleFromConfig @@ -77,14 +77,14 @@ data: num_workers: 2 wrap: false train: - target: invokeai.backend.ldm.data.personalized.PersonalizedBase + target: invokeai.backend.stable_diffusion.data.personalized.PersonalizedBase params: size: 512 set: train per_image_tokens: false repeats: 100 validation: - target: invokeai.backend.ldm.data.personalized.PersonalizedBase + target: invokeai.backend.stable_diffusion.data.personalized.PersonalizedBase params: size: 512 set: val diff --git a/invokeai/configs/stable-diffusion/v1-finetune_style.yaml b/invokeai/configs/stable-diffusion/v1-finetune_style.yaml index 7433390ce3..3442971a5b 100644 --- a/invokeai/configs/stable-diffusion/v1-finetune_style.yaml +++ b/invokeai/configs/stable-diffusion/v1-finetune_style.yaml @@ -19,7 +19,7 @@ model: embedding_reg_weight: 0.0 personalization_config: - target: invokeai.backend.ldm.modules.embedding_manager.EmbeddingManager + target: invokeai.backend.stable_diffusion.embedding_manager.EmbeddingManager params: placeholder_strings: ["*"] initializer_words: ["painting"] @@ -27,7 +27,7 @@ model: num_vectors_per_token: 1 unet_config: - target: invokeai.backend.ldm.modules.diffusionmodules.openaimodel.UNetModel + target: invokeai.backend.stable_diffusion.diffusionmodules.openaimodel.UNetModel params: image_size: 32 # unused in_channels: 4 @@ -44,7 +44,7 @@ model: legacy: False first_stage_config: - target: invokeai.models.autoencoder.AutoencoderKL + target: invokeai.backend.stable_diffusion.autoencoder.AutoencoderKL params: embed_dim: 4 monitor: val/rec_loss @@ -67,7 +67,7 @@ model: target: torch.nn.Identity cond_stage_config: - target: invokeai.backend.ldm.modules.encoders.modules.FrozenCLIPEmbedder + target: invokeai.backend.stable_diffusion.encoders.modules.FrozenCLIPEmbedder data: target: main.DataModuleFromConfig @@ -76,14 +76,14 @@ data: num_workers: 16 wrap: false train: - target: invokeai.backend.ldm.data.personalized_style.PersonalizedBase + target: invokeai.backend.stable_diffusion.data.personalized_style.PersonalizedBase params: size: 512 set: train per_image_tokens: false repeats: 100 validation: - target: invokeai.backend.ldm.data.personalized_style.PersonalizedBase + target: invokeai.backend.stable_diffusion.data.personalized_style.PersonalizedBase params: size: 512 set: val diff --git a/invokeai/configs/stable-diffusion/v1-inference.yaml b/invokeai/configs/stable-diffusion/v1-inference.yaml index 911c756003..7bcfe28f53 100644 --- a/invokeai/configs/stable-diffusion/v1-inference.yaml +++ b/invokeai/configs/stable-diffusion/v1-inference.yaml @@ -18,7 +18,7 @@ model: use_ema: False scheduler_config: # 10000 warmup steps - target: invokeai.backend.ldm.lr_scheduler.LambdaLinearScheduler + target: invokeai.backend.stable_diffusion.lr_scheduler.LambdaLinearScheduler params: warm_up_steps: [ 10000 ] cycle_lengths: [ 10000000000000 ] # incredibly large number to prevent corner cases @@ -27,7 +27,7 @@ model: f_min: [ 1. ] personalization_config: - target: invokeai.backend.ldm.modules.embedding_manager.EmbeddingManager + target: invokeai.backend.stable_diffusion.embedding_manager.EmbeddingManager params: placeholder_strings: ["*"] initializer_words: ['sculpture'] @@ -36,7 +36,7 @@ model: progressive_words: False unet_config: - target: invokeai.backend.ldm.modules.diffusionmodules.openaimodel.UNetModel + target: invokeai.backend.stable_diffusion.diffusionmodules.openaimodel.UNetModel params: image_size: 32 # unused in_channels: 4 @@ -53,7 +53,7 @@ model: legacy: False first_stage_config: - target: invokeai.models.autoencoder.AutoencoderKL + target: invokeai.backend.stable_diffusion.autoencoder.AutoencoderKL params: embed_dim: 4 monitor: val/rec_loss @@ -76,4 +76,4 @@ model: target: torch.nn.Identity cond_stage_config: - target: invokeai.backend.ldm.modules.encoders.modules.WeightedFrozenCLIPEmbedder + target: invokeai.backend.stable_diffusion.encoders.modules.WeightedFrozenCLIPEmbedder diff --git a/invokeai/configs/stable-diffusion/v1-inpainting-inference.yaml b/invokeai/configs/stable-diffusion/v1-inpainting-inference.yaml index 76ef8d2750..f6433cf97d 100644 --- a/invokeai/configs/stable-diffusion/v1-inpainting-inference.yaml +++ b/invokeai/configs/stable-diffusion/v1-inpainting-inference.yaml @@ -18,7 +18,7 @@ model: finetune_keys: null scheduler_config: # 10000 warmup steps - target: invokeai.backend.ldm.lr_scheduler.LambdaLinearScheduler + target: invokeai.backend.stable_diffusion.lr_scheduler.LambdaLinearScheduler params: warm_up_steps: [ 2500 ] # NOTE for resuming. use 10000 if starting from scratch cycle_lengths: [ 10000000000000 ] # incredibly large number to prevent corner cases @@ -27,7 +27,7 @@ model: f_min: [ 1. ] personalization_config: - target: invokeai.backend.ldm.modules.embedding_manager.EmbeddingManager + target: invokeai.backend.stable_diffusion.embedding_manager.EmbeddingManager params: placeholder_strings: ["*"] initializer_words: ['sculpture'] @@ -36,7 +36,7 @@ model: progressive_words: False unet_config: - target: invokeai.backend.ldm.modules.diffusionmodules.openaimodel.UNetModel + target: invokeai.backend.stable_diffusion.diffusionmodules.openaimodel.UNetModel params: image_size: 32 # unused in_channels: 9 # 4 data + 4 downscaled image + 1 mask @@ -53,7 +53,7 @@ model: legacy: False first_stage_config: - target: invokeai.models.autoencoder.AutoencoderKL + target: invokeai.backend.stable_diffusion.autoencoder.AutoencoderKL params: embed_dim: 4 monitor: val/rec_loss @@ -76,4 +76,4 @@ model: target: torch.nn.Identity cond_stage_config: - target: invokeai.backend.ldm.modules.encoders.modules.WeightedFrozenCLIPEmbedder + target: invokeai.backend.stable_diffusion.encoders.modules.WeightedFrozenCLIPEmbedder diff --git a/invokeai/configs/stable-diffusion/v1-m1-finetune.yaml b/invokeai/configs/stable-diffusion/v1-m1-finetune.yaml index d9533d9b4b..10255a9b70 100644 --- a/invokeai/configs/stable-diffusion/v1-m1-finetune.yaml +++ b/invokeai/configs/stable-diffusion/v1-m1-finetune.yaml @@ -19,7 +19,7 @@ model: embedding_reg_weight: 0.0 personalization_config: - target: invokeai.backend.ldm.modules.embedding_manager.EmbeddingManager + target: invokeai.backend.stable_diffusion.embedding_manager.EmbeddingManager params: placeholder_strings: ["*"] initializer_words: ['sculpture'] @@ -28,7 +28,7 @@ model: progressive_words: False unet_config: - target: invokeai.backend.ldm.modules.diffusionmodules.openaimodel.UNetModel + target: invokeai.backend.stable_diffusion.diffusionmodules.openaimodel.UNetModel params: image_size: 32 # unused in_channels: 4 @@ -45,7 +45,7 @@ model: legacy: False first_stage_config: - target: invokeai.models.autoencoder.AutoencoderKL + target: invokeai.backend.stable_diffusion.autoencoder.AutoencoderKL params: embed_dim: 4 monitor: val/rec_loss @@ -68,7 +68,7 @@ model: target: torch.nn.Identity cond_stage_config: - target: invokeai.backend.ldm.modules.encoders.modules.FrozenCLIPEmbedder + target: invokeai.backend.stable_diffusion.encoders.modules.FrozenCLIPEmbedder data: target: main.DataModuleFromConfig @@ -77,14 +77,14 @@ data: num_workers: 2 wrap: false train: - target: invokeai.backend.ldm.data.personalized.PersonalizedBase + target: invokeai.backend.stable_diffusion.data.personalized.PersonalizedBase params: size: 512 set: train per_image_tokens: false repeats: 100 validation: - target: invokeai.backend.ldm.data.personalized.PersonalizedBase + target: invokeai.backend.stable_diffusion.data.personalized.PersonalizedBase params: size: 512 set: val diff --git a/invokeai/configs/stable-diffusion/v2-inference-v.yaml b/invokeai/configs/stable-diffusion/v2-inference-v.yaml index 2a426483eb..47ecbe0334 100644 --- a/invokeai/configs/stable-diffusion/v2-inference-v.yaml +++ b/invokeai/configs/stable-diffusion/v2-inference-v.yaml @@ -19,7 +19,7 @@ model: use_ema: False # we set this to false because this is an inference only config unet_config: - target: invokeai.backend.ldm.modules.diffusionmodules.openaimodel.UNetModel + target: invokeai.backend.stable_diffusion.diffusionmodules.openaimodel.UNetModel params: use_checkpoint: True use_fp16: True @@ -38,7 +38,7 @@ model: legacy: False first_stage_config: - target: invokeai.models.autoencoder.AutoencoderKL + target: invokeai.backend.stable_diffusion.autoencoder.AutoencoderKL params: embed_dim: 4 monitor: val/rec_loss @@ -62,7 +62,7 @@ model: target: torch.nn.Identity cond_stage_config: - target: invokeai.backend.ldm.modules.encoders.modules.FrozenOpenCLIPEmbedder + target: invokeai.backend.stable_diffusion.encoders.modules.FrozenOpenCLIPEmbedder params: freeze: True layer: "penultimate" diff --git a/invokeai/frontend/CLI/CLI.py b/invokeai/frontend/CLI/CLI.py index 0e74e4bff5..0a46c36ad0 100644 --- a/invokeai/frontend/CLI/CLI.py +++ b/invokeai/frontend/CLI/CLI.py @@ -8,7 +8,6 @@ from pathlib import Path from typing import Union import click - from compel import PromptParser if sys.platform == "darwin": @@ -18,22 +17,23 @@ import pyparsing # type: ignore import invokeai.version -from ...backend import Generate -from ...backend.args import (Args, - dream_cmd_from_png, - metadata_dumps, - metadata_from_png) -from ...backend.stable_diffusion import PipelineIntermediateState -from ...backend.image_util import make_grid, PngWriter, retrieve_metadata, write_metadata -from ...backend import ModelManager +from ...backend import Generate, ModelManager +from ...backend.args import Args, dream_cmd_from_png, metadata_dumps, metadata_from_png from ...backend.globals import Globals -from ...backend.util import write_log +from ...backend.image_util import ( + PngWriter, + make_grid, + retrieve_metadata, + write_metadata, +) +from ...backend.stable_diffusion import PipelineIntermediateState +from ...backend.util import url_attachment_name, write_log from .readline import Completer, get_completer -from ...backend.util import url_attachment_name # global used in multiple functions (fix) infile = None + def main(): """Initialize command-line parsers and the diffusion model""" global infile @@ -494,7 +494,7 @@ def main_loop(gen, opt): def do_command(command: str, gen, opt: Args, completer) -> tuple: global infile operation = "generate" # default operation, alternative is 'postprocess' - command = command.replace('\\','/') # windows + command = command.replace("\\", "/") # windows if command.startswith( "!dream" @@ -537,10 +537,10 @@ def do_command(command: str, gen, opt: Args, completer) -> tuple: import_model(path[1], gen, opt, completer) completer.add_history(command) except KeyboardInterrupt: - print('\n') + print("\n") operation = None - elif command.startswith(("!convert","!optimize")): + elif command.startswith(("!convert", "!optimize")): path = shlex.split(command) if len(path) < 2: print("** please provide the path to a .ckpt or .safetensors model") @@ -549,9 +549,9 @@ def do_command(command: str, gen, opt: Args, completer) -> tuple: convert_model(path[1], gen, opt, completer) completer.add_history(command) except KeyboardInterrupt: - print('\n') + print("\n") operation = None - + elif command.startswith("!edit"): path = shlex.split(command) if len(path) < 2: @@ -639,12 +639,12 @@ def import_model(model_path: str, gen, opt, completer, convert=False): ): pass else: - if model_path.startswith(('http:','https:')): + if model_path.startswith(("http:", "https:")): try: default_name = url_attachment_name(model_path) default_name = Path(default_name).stem except Exception as e: - print(f'** URL: {str(e)}') + print(f"** URL: {str(e)}") model_name, model_desc = _get_model_name_and_desc( gen.model_manager, completer, @@ -672,6 +672,7 @@ def import_model(model_path: str, gen, opt, completer, convert=False): completer.update_models(gen.model_manager.list_models()) print(f">> {imported_name} successfully installed") + def _verify_load(model_name: str, gen) -> bool: print(">> Verifying that new model loads...") current_model = gen.model_name @@ -704,6 +705,7 @@ def _get_model_name_and_desc( ) return model_name, model_description + def convert_model(model_name_or_path: Union[Path, str], gen, opt, completer): model_name_or_path = model_name_or_path.replace("\\", "/") # windows manager = gen.model_manager @@ -722,7 +724,9 @@ def convert_model(model_name_or_path: Union[Path, str], gen, opt, completer): else: print(f"** {model_name_or_path} is not a legacy .ckpt weights file") return - if vae_repo := ldm.invoke.model_manager.VAE_TO_REPO_ID.get(Path(vae).stem): + if vae_repo := invokeai.backend.model_management.model_manager.VAE_TO_REPO_ID.get( + Path(vae).stem + ): vae_repo = dict(repo_id=vae_repo) else: vae_repo = None @@ -742,7 +746,7 @@ def convert_model(model_name_or_path: Union[Path, str], gen, opt, completer): except KeyboardInterrupt: return - manager.commit(opt.conf) + manager.commit(opt.conf) if click.confirm(f"Delete the original .ckpt file at {ckpt_path}?", default=False): ckpt_path.unlink(missing_ok=True) print(f"{ckpt_path} deleted") @@ -1106,7 +1110,7 @@ def make_step_callback(gen, opt, prefix): if step % opt.save_intermediates == 0 or step == opt.steps - 1: filename = os.path.join(destination, f"{step:04}.png") image = gen.sample_to_lowres_estimated_image(latents) - image = image.resize((image.size[0]*8,image.size[1]*8)) + image = image.resize((image.size[0] * 8, image.size[1] * 8)) image.save(filename, "PNG") return callback @@ -1190,8 +1194,8 @@ def report_model_error(opt: Namespace, e: Exception): ) else: if not click.confirm( - 'Do you want to run invokeai-configure script to select and/or reinstall models?', - default=False + "Do you want to run invokeai-configure script to select and/or reinstall models?", + default=False, ): return @@ -1209,9 +1213,9 @@ def report_model_error(opt: Namespace, e: Exception): for arg in yes_to_all.split(): sys.argv.append(arg) - from ldm.invoke.config import invokeai_configure + from ..install import invokeai_configure - invokeai_configure.main() + invokeai_configure() print("** InvokeAI will now restart") sys.argv = previous_args main() # would rather do a os.exec(), but doesn't exist? @@ -1232,6 +1236,6 @@ def check_internet() -> bool: except: return False -if __name__ == '__main__': + +if __name__ == "__main__": main() - diff --git a/invokeai/frontend/CLI/__init__.py b/invokeai/frontend/CLI/__init__.py index 4db5e778ff..7e48534cb9 100644 --- a/invokeai/frontend/CLI/__init__.py +++ b/invokeai/frontend/CLI/__init__.py @@ -1,4 +1,4 @@ -''' +""" Initialization file for invokeai.frontend.CLI -''' +""" from .CLI import main as invokeai_command_line_interface diff --git a/invokeai/frontend/CLI/readline.py b/invokeai/frontend/CLI/readline.py index 21e064662b..5a877ae810 100644 --- a/invokeai/frontend/CLI/readline.py +++ b/invokeai/frontend/CLI/readline.py @@ -4,13 +4,14 @@ You may import the global singleton `completer` to get access to the completer object itself. This is useful when you want to autocomplete seeds: - from ldm.invoke.readline import completer + from invokeai.frontend.CLI.readline import completer completer.add_seed(18247566) completer.add_seed(9281839) """ +import atexit import os import re -import atexit + from ...backend.args import Args from ...backend.globals import Globals from ...backend.stable_diffusion import HuggingFaceConceptsLibrary @@ -18,92 +19,128 @@ from ...backend.stable_diffusion import HuggingFaceConceptsLibrary # ---------------readline utilities--------------------- try: import readline + readline_available = True -except (ImportError,ModuleNotFoundError) as e: - print(f'** An error occurred when loading the readline module: {str(e)}') +except (ImportError, ModuleNotFoundError) as e: + print(f"** An error occurred when loading the readline module: {str(e)}") readline_available = False -IMG_EXTENSIONS = ('.png','.jpg','.jpeg','.PNG','.JPG','.JPEG','.gif','.GIF') -WEIGHT_EXTENSIONS = ('.ckpt','.vae','.safetensors') -TEXT_EXTENSIONS = ('.txt','.TXT') -CONFIG_EXTENSIONS = ('.yaml','.yml') +IMG_EXTENSIONS = (".png", ".jpg", ".jpeg", ".PNG", ".JPG", ".JPEG", ".gif", ".GIF") +WEIGHT_EXTENSIONS = (".ckpt", ".vae", ".safetensors") +TEXT_EXTENSIONS = (".txt", ".TXT") +CONFIG_EXTENSIONS = (".yaml", ".yml") COMMANDS = ( - '--steps','-s', - '--seed','-S', - '--iterations','-n', - '--width','-W','--height','-H', - '--cfg_scale','-C', - '--threshold', - '--perlin', - '--grid','-g', - '--individual','-i', - '--save_intermediates', - '--init_img','-I', - '--init_mask','-M', - '--init_color', - '--strength','-f', - '--variants','-v', - '--outdir','-o', - '--sampler','-A','-m', - '--embedding_path', - '--device', - '--grid','-g', - '--facetool','-ft', - '--facetool_strength','-G', - '--codeformer_fidelity','-cf', - '--upscale','-U', - '-save_orig','--save_original', - '--log_tokenization','-t', - '--hires_fix', - '--inpaint_replace','-r', - '--png_compression','-z', - '--text_mask','-tm', - '--h_symmetry_time_pct', - '--v_symmetry_time_pct', - '!fix','!fetch','!replay','!history','!search','!clear', - '!models','!switch','!import_model','!optimize_model','!convert_model','!edit_model','!del_model', - '!mask','!triggers', - ) -MODEL_COMMANDS = ( - '!switch', - '!edit_model', - '!del_model', - ) -CKPT_MODEL_COMMANDS = ( - '!optimize_model', + "--steps", + "-s", + "--seed", + "-S", + "--iterations", + "-n", + "--width", + "-W", + "--height", + "-H", + "--cfg_scale", + "-C", + "--threshold", + "--perlin", + "--grid", + "-g", + "--individual", + "-i", + "--save_intermediates", + "--init_img", + "-I", + "--init_mask", + "-M", + "--init_color", + "--strength", + "-f", + "--variants", + "-v", + "--outdir", + "-o", + "--sampler", + "-A", + "-m", + "--embedding_path", + "--device", + "--grid", + "-g", + "--facetool", + "-ft", + "--facetool_strength", + "-G", + "--codeformer_fidelity", + "-cf", + "--upscale", + "-U", + "-save_orig", + "--save_original", + "--log_tokenization", + "-t", + "--hires_fix", + "--inpaint_replace", + "-r", + "--png_compression", + "-z", + "--text_mask", + "-tm", + "--h_symmetry_time_pct", + "--v_symmetry_time_pct", + "!fix", + "!fetch", + "!replay", + "!history", + "!search", + "!clear", + "!models", + "!switch", + "!import_model", + "!optimize_model", + "!convert_model", + "!edit_model", + "!del_model", + "!mask", + "!triggers", ) +MODEL_COMMANDS = ( + "!switch", + "!edit_model", + "!del_model", +) +CKPT_MODEL_COMMANDS = ("!optimize_model",) WEIGHT_COMMANDS = ( - '!import_model', - '!convert_model', - ) -IMG_PATH_COMMANDS = ( - '--outdir[=\s]', - ) -TEXT_PATH_COMMANDS=( - '!replay', - ) -IMG_FILE_COMMANDS=( - '!fix', - '!fetch', - '!mask', - '--init_img[=\s]','-I', - '--init_mask[=\s]','-M', - '--init_color[=\s]', - '--embedding_path[=\s]', - ) + "!import_model", + "!convert_model", +) +IMG_PATH_COMMANDS = ("--outdir[=\s]",) +TEXT_PATH_COMMANDS = ("!replay",) +IMG_FILE_COMMANDS = ( + "!fix", + "!fetch", + "!mask", + "--init_img[=\s]", + "-I", + "--init_mask[=\s]", + "-M", + "--init_color[=\s]", + "--embedding_path[=\s]", +) + +path_regexp = "(" + "|".join(IMG_PATH_COMMANDS + IMG_FILE_COMMANDS) + ")\s*\S*$" +weight_regexp = "(" + "|".join(WEIGHT_COMMANDS) + ")\s*\S*$" +text_regexp = "(" + "|".join(TEXT_PATH_COMMANDS) + ")\s*\S*$" -path_regexp = '(' + '|'.join(IMG_PATH_COMMANDS+IMG_FILE_COMMANDS) + ')\s*\S*$' -weight_regexp = '(' + '|'.join(WEIGHT_COMMANDS) + ')\s*\S*$' -text_regexp = '(' + '|'.join(TEXT_PATH_COMMANDS) + ')\s*\S*$' class Completer(object): def __init__(self, options, models={}): - self.options = sorted(options) - self.models = models - self.seeds = set() - self.matches = list() + self.options = sorted(options) + self.models = models + self.seeds = set() + self.matches = list() self.default_dir = None - self.linebuffer = None + self.linebuffer = None self.auto_history_active = True self.extensions = None self.concepts = None @@ -111,40 +148,41 @@ class Completer(object): return def complete(self, text, state): - ''' + """ Completes invoke command line. BUG: it doesn't correctly complete files that have spaces in the name. - ''' + """ buffer = readline.get_line_buffer() if state == 0: - # extensions defined, so go directly into path completion mode if self.extensions is not None: self.matches = self._path_completions(text, state, self.extensions) # looking for an image file - elif re.search(path_regexp,buffer): - do_shortcut = re.search('^'+'|'.join(IMG_FILE_COMMANDS),buffer) - self.matches = self._path_completions(text, state, IMG_EXTENSIONS,shortcut_ok=do_shortcut) + elif re.search(path_regexp, buffer): + do_shortcut = re.search("^" + "|".join(IMG_FILE_COMMANDS), buffer) + self.matches = self._path_completions( + text, state, IMG_EXTENSIONS, shortcut_ok=do_shortcut + ) # looking for a seed - elif re.search('(-S\s*|--seed[=\s])\d*$',buffer): - self.matches= self._seed_completions(text,state) + elif re.search("(-S\s*|--seed[=\s])\d*$", buffer): + self.matches = self._seed_completions(text, state) # looking for an embedding concept - elif re.search('<[\w-]*$',buffer): - self.matches= self._concept_completions(text,state) + elif re.search("<[\w-]*$", buffer): + self.matches = self._concept_completions(text, state) # looking for a model - elif re.match('^'+'|'.join(MODEL_COMMANDS),buffer): - self.matches= self._model_completions(text, state) + elif re.match("^" + "|".join(MODEL_COMMANDS), buffer): + self.matches = self._model_completions(text, state) # looking for a ckpt model - elif re.match('^'+'|'.join(CKPT_MODEL_COMMANDS),buffer): - self.matches= self._model_completions(text, state, ckpt_only=True) + elif re.match("^" + "|".join(CKPT_MODEL_COMMANDS), buffer): + self.matches = self._model_completions(text, state, ckpt_only=True) - elif re.search(weight_regexp,buffer): + elif re.search(weight_regexp, buffer): self.matches = self._path_completions( text, state, @@ -152,14 +190,12 @@ class Completer(object): default_dir=Globals.root, ) - elif re.search(text_regexp,buffer): + elif re.search(text_regexp, buffer): self.matches = self._path_completions(text, state, TEXT_EXTENSIONS) # This is the first time for this text, so build a match list. elif text: - self.matches = [ - s for s in self.options if s and s.startswith(text) - ] + self.matches = [s for s in self.options if s and s.startswith(text)] else: self.matches = self.options[:] @@ -171,50 +207,50 @@ class Completer(object): response = None return response - def complete_extensions(self, extensions:list): - ''' + def complete_extensions(self, extensions: list): + """ If called with a list of extensions, will force completer to do file path completions. - ''' - self.extensions=extensions + """ + self.extensions = extensions - def add_history(self,line): - ''' + def add_history(self, line): + """ Pass thru to readline - ''' + """ if not self.auto_history_active: readline.add_history(line) def clear_history(self): - ''' + """ Pass clear_history() thru to readline - ''' + """ readline.clear_history() - def search_history(self,match:str): - ''' + def search_history(self, match: str): + """ Like show_history() but only shows items that contain the match string. - ''' + """ self.show_history(match) - def remove_history_item(self,pos): + def remove_history_item(self, pos): readline.remove_history_item(pos) def add_seed(self, seed): - ''' + """ Add a seed to the autocomplete list for display when -S is autocompleted. - ''' + """ if seed is not None: self.seeds.add(str(seed)) def set_default_dir(self, path): - self.default_dir=path + self.default_dir = path - def set_options(self,options): + def set_options(self, options): self.options = options - def get_line(self,index): + def get_line(self, index): try: line = self.get_history_item(index) except IndexError: @@ -224,57 +260,58 @@ class Completer(object): def get_current_history_length(self): return readline.get_current_history_length() - def get_history_item(self,index): + def get_history_item(self, index): return readline.get_history_item(index) - def show_history(self,match=None): - ''' + def show_history(self, match=None): + """ Print the session history using the pydoc pager - ''' + """ import pydoc + lines = list() h_len = self.get_current_history_length() if h_len < 1: - print('') + print("") return - for i in range(0,h_len): - line = self.get_history_item(i+1) + for i in range(0, h_len): + line = self.get_history_item(i + 1) if match and match not in line: continue - lines.append(f'[{i+1}] {line}') - pydoc.pager('\n'.join(lines)) + lines.append(f"[{i+1}] {line}") + pydoc.pager("\n".join(lines)) - def set_line(self,line)->None: - ''' + def set_line(self, line) -> None: + """ Set the default string displayed in the next line of input. - ''' + """ self.linebuffer = line readline.redisplay() - def update_models(self,models:dict)->None: - ''' + def update_models(self, models: dict) -> None: + """ update our list of models - ''' + """ self.models = models def _seed_completions(self, text, state): - m = re.search('(-S\s?|--seed[=\s]?)(\d*)',text) + m = re.search("(-S\s?|--seed[=\s]?)(\d*)", text) if m: - switch = m.groups()[0] + switch = m.groups()[0] partial = m.groups()[1] else: - switch = '' + switch = "" partial = text matches = list() for s in self.seeds: if s.startswith(partial): - matches.append(switch+s) + matches.append(switch + s) matches.sort() return matches - def add_embedding_terms(self, terms:list[str]): + def add_embedding_terms(self, terms: list[str]): self.embedding_terms = set(terms) if self.concepts: self.embedding_terms.update(set(self.concepts.list_concepts())) @@ -294,27 +331,27 @@ class Completer(object): matches = list() for concept in self.embedding_terms: if concept.startswith(partial): - matches.append(f'<{concept}>') + matches.append(f"<{concept}>") matches.sort() return matches def _model_completions(self, text, state, ckpt_only=False): - m = re.search('(!switch\s+)(\w*)',text) + m = re.search("(!switch\s+)(\w*)", text) if m: - switch = m.groups()[0] + switch = m.groups()[0] partial = m.groups()[1] else: - switch = '' + switch = "" partial = text matches = list() for s in self.models: - format = self.models[s]['format'] - if format == 'vae': + format = self.models[s]["format"] + if format == "vae": continue - if ckpt_only and format != 'ckpt': + if ckpt_only and format != "ckpt": continue if s.startswith(partial): - matches.append(switch+s) + matches.append(switch + s) matches.sort() return matches @@ -324,14 +361,16 @@ class Completer(object): readline.redisplay() self.linebuffer = None - def _path_completions(self, text, state, extensions, shortcut_ok=True, default_dir:str=''): + def _path_completions( + self, text, state, extensions, shortcut_ok=True, default_dir: str = "" + ): # separate the switch from the partial path - match = re.search('^(-\w|--\w+=?)(.*)',text) + match = re.search("^(-\w|--\w+=?)(.*)", text) if match is None: switch = None partial_path = text else: - switch,partial_path = match.groups() + switch, partial_path = match.groups() partial_path = partial_path.lstrip() @@ -340,18 +379,18 @@ class Completer(object): if os.path.isdir(path): dir = path - elif os.path.dirname(path) != '': + elif os.path.dirname(path) != "": dir = os.path.dirname(path) else: - dir = default_dir if os.path.exists(default_dir) else '' - path= os.path.join(dir,path) + dir = default_dir if os.path.exists(default_dir) else "" + path = os.path.join(dir, path) - dir_list = os.listdir(dir or '.') - if shortcut_ok and os.path.exists(self.default_dir) and dir=='': + dir_list = os.listdir(dir or ".") + if shortcut_ok and os.path.exists(self.default_dir) and dir == "": dir_list += os.listdir(self.default_dir) for node in dir_list: - if node.startswith('.') and len(node) > 1: + if node.startswith(".") and len(node) > 1: continue full_path = os.path.join(dir, node) @@ -362,25 +401,26 @@ class Completer(object): continue if switch is None: - match_path = os.path.join(dir,node) - matches.append(match_path+'/' if os.path.isdir(full_path) else match_path) + match_path = os.path.join(dir, node) + matches.append( + match_path + "/" if os.path.isdir(full_path) else match_path + ) elif os.path.isdir(full_path): matches.append( - switch+os.path.join(os.path.dirname(full_path), node) + '/' + switch + os.path.join(os.path.dirname(full_path), node) + "/" ) elif node.endswith(extensions): - matches.append( - switch+os.path.join(os.path.dirname(full_path), node) - ) + matches.append(switch + os.path.join(os.path.dirname(full_path), node)) return matches + class DummyCompleter(Completer): - def __init__(self,options): + def __init__(self, options): super().__init__(options) self.history = list() - def add_history(self,line): + def add_history(self, line): self.history.append(line) def clear_history(self): @@ -389,37 +429,37 @@ class DummyCompleter(Completer): def get_current_history_length(self): return len(self.history) - def get_history_item(self,index): - return self.history[index-1] + def get_history_item(self, index): + return self.history[index - 1] - def remove_history_item(self,index): - return self.history.pop(index-1) + def remove_history_item(self, index): + return self.history.pop(index - 1) - def set_line(self,line): - print(f'# {line}') + def set_line(self, line): + print(f"# {line}") -def generic_completer(commands:list)->Completer: + +def generic_completer(commands: list) -> Completer: if readline_available: - completer = Completer(commands,[]) + completer = Completer(commands, []) readline.set_completer(completer.complete) readline.set_pre_input_hook(completer._pre_input_hook) - readline.set_completer_delims(' ') - readline.parse_and_bind('tab: complete') - readline.parse_and_bind('set print-completions-horizontally off') - readline.parse_and_bind('set page-completions on') - readline.parse_and_bind('set skip-completed-text on') - readline.parse_and_bind('set show-all-if-ambiguous on') + readline.set_completer_delims(" ") + readline.parse_and_bind("tab: complete") + readline.parse_and_bind("set print-completions-horizontally off") + readline.parse_and_bind("set page-completions on") + readline.parse_and_bind("set skip-completed-text on") + readline.parse_and_bind("set show-all-if-ambiguous on") else: completer = DummyCompleter(commands) return completer -def get_completer(opt:Args, models=[])->Completer: - if readline_available: - completer = Completer(COMMANDS,models) - readline.set_completer( - completer.complete - ) +def get_completer(opt: Args, models=[]) -> Completer: + if readline_available: + completer = Completer(COMMANDS, models) + + readline.set_completer(completer.complete) # pyreadline3 does not have a set_auto_history() method try: readline.set_auto_history(False) @@ -427,27 +467,29 @@ def get_completer(opt:Args, models=[])->Completer: except: completer.auto_history_active = True readline.set_pre_input_hook(completer._pre_input_hook) - readline.set_completer_delims(' ') - readline.parse_and_bind('tab: complete') - readline.parse_and_bind('set print-completions-horizontally off') - readline.parse_and_bind('set page-completions on') - readline.parse_and_bind('set skip-completed-text on') - readline.parse_and_bind('set show-all-if-ambiguous on') + readline.set_completer_delims(" ") + readline.parse_and_bind("tab: complete") + readline.parse_and_bind("set print-completions-horizontally off") + readline.parse_and_bind("set page-completions on") + readline.parse_and_bind("set skip-completed-text on") + readline.parse_and_bind("set show-all-if-ambiguous on") outdir = os.path.expanduser(opt.outdir) if os.path.isabs(outdir): - histfile = os.path.join(outdir,'.invoke_history') + histfile = os.path.join(outdir, ".invoke_history") else: - histfile = os.path.join(Globals.root, outdir, '.invoke_history') + histfile = os.path.join(Globals.root, outdir, ".invoke_history") try: readline.read_history_file(histfile) readline.set_history_length(1000) except FileNotFoundError: pass - except OSError: # file likely corrupted - newname = f'{histfile}.old' - print(f'## Your history file {histfile} couldn\'t be loaded and may be corrupted. Renaming it to {newname}') - os.replace(histfile,newname) + except OSError: # file likely corrupted + newname = f"{histfile}.old" + print( + f"## Your history file {histfile} couldn't be loaded and may be corrupted. Renaming it to {newname}" + ) + os.replace(histfile, newname) atexit.register(readline.write_history_file, histfile) else: diff --git a/invokeai/frontend/config/__init__.py b/invokeai/frontend/install/__init__.py similarity index 96% rename from invokeai/frontend/config/__init__.py rename to invokeai/frontend/install/__init__.py index 0d1d2aec75..fb8cdff1b3 100644 --- a/invokeai/frontend/config/__init__.py +++ b/invokeai/frontend/install/__init__.py @@ -1,7 +1,6 @@ -''' +""" Initialization file for invokeai.frontend.config -''' -from .model_install import main as invokeai_model_install +""" from .invokeai_configure import main as invokeai_configure from .invokeai_update import main as invokeai_update - +from .model_install import main as invokeai_model_install diff --git a/invokeai/frontend/config/invokeai_configure.py b/invokeai/frontend/install/invokeai_configure.py similarity index 93% rename from invokeai/frontend/config/invokeai_configure.py rename to invokeai/frontend/install/invokeai_configure.py index 748d7bb8ca..0df5fdb16f 100644 --- a/invokeai/frontend/config/invokeai_configure.py +++ b/invokeai/frontend/install/invokeai_configure.py @@ -1,4 +1,4 @@ -''' +""" Wrapper for invokeai.backend.configure.invokeai_configure -''' +""" from ...backend.config.invokeai_configure import main diff --git a/invokeai/frontend/config/invokeai_update.py b/invokeai/frontend/install/invokeai_update.py similarity index 56% rename from invokeai/frontend/config/invokeai_update.py rename to invokeai/frontend/install/invokeai_update.py index d3a532c627..040067cff9 100644 --- a/invokeai/frontend/config/invokeai_update.py +++ b/invokeai/frontend/install/invokeai_update.py @@ -1,9 +1,10 @@ -''' +""" Minimalist updater script. Prompts user for the tag or branch to update to and runs pip install . -''' +""" import os import platform + import requests from rich import box, print from rich.console import Console, Group, group @@ -15,8 +16,8 @@ from rich.text import Text from invokeai.version import __version__ -INVOKE_AI_SRC="https://github.com/invoke-ai/InvokeAI/archive" -INVOKE_AI_REL="https://api.github.com/repos/invoke-ai/InvokeAI/releases" +INVOKE_AI_SRC = "https://github.com/invoke-ai/InvokeAI/archive" +INVOKE_AI_REL = "https://api.github.com/repos/invoke-ai/InvokeAI/releases" OS = platform.uname().system ARCH = platform.uname().machine @@ -27,21 +28,22 @@ if OS == "Windows": else: console = Console(style=Style(color="grey74", bgcolor="grey19")) -def get_versions()->dict: + +def get_versions() -> dict: return requests.get(url=INVOKE_AI_REL).json() + def welcome(versions: dict): - @group() def text(): - yield f'InvokeAI Version: [bold yellow]{__version__}' - yield '' - yield 'This script will update InvokeAI to the latest release, or to a development version of your choice.' - yield '' - yield '[bold yellow]Options:' - yield f'''[1] Update to the latest official release ([italic]{versions[0]['tag_name']}[/italic]) + yield f"InvokeAI Version: [bold yellow]{__version__}" + yield "" + yield "This script will update InvokeAI to the latest release, or to a development version of your choice." + yield "" + yield "[bold yellow]Options:" + yield f"""[1] Update to the latest official release ([italic]{versions[0]['tag_name']}[/italic]) [2] Update to the bleeding-edge development version ([italic]main[/italic]) -[3] Manually enter the tag or branch name you wish to update''' +[3] Manually enter the tag or branch name you wish to update""" console.rule() print( @@ -57,32 +59,33 @@ def welcome(versions: dict): ) console.line() + def main(): versions = get_versions() welcome(versions) tag = None - choice = Prompt.ask('Choice:',choices=['1','2','3'],default='1') - - if choice=='1': - tag = versions[0]['tag_name'] - elif choice=='2': - tag = 'main' - elif choice=='3': - tag = Prompt.ask('Enter an InvokeAI tag or branch name') + choice = Prompt.ask("Choice:", choices=["1", "2", "3"], default="1") - print(f':crossed_fingers: Upgrading to [yellow]{tag}[/yellow]') - cmd = f'pip install {INVOKE_AI_SRC}/{tag}.zip --use-pep517' - print('') - print('') - if os.system(cmd)==0: - print(f':heavy_check_mark: Upgrade successful') + if choice == "1": + tag = versions[0]["tag_name"] + elif choice == "2": + tag = "main" + elif choice == "3": + tag = Prompt.ask("Enter an InvokeAI tag or branch name") + + print(f":crossed_fingers: Upgrading to [yellow]{tag}[/yellow]") + cmd = f"pip install {INVOKE_AI_SRC}/{tag}.zip --use-pep517" + print("") + print("") + if os.system(cmd) == 0: + print(f":heavy_check_mark: Upgrade successful") else: - print(f':exclamation: [bold red]Upgrade failed[/red bold]') - + print(f":exclamation: [bold red]Upgrade failed[/red bold]") + + if __name__ == "__main__": try: main() except KeyboardInterrupt: pass - diff --git a/invokeai/frontend/config/model_install.py b/invokeai/frontend/install/model_install.py similarity index 93% rename from invokeai/frontend/config/model_install.py rename to invokeai/frontend/install/model_install.py index f64a656211..e7b10c34e1 100644 --- a/invokeai/frontend/config/model_install.py +++ b/invokeai/frontend/install/model_install.py @@ -14,34 +14,42 @@ import os import sys from argparse import Namespace from pathlib import Path +from shutil import get_terminal_size from typing import List import npyscreen import torch from npyscreen import widget from omegaconf import OmegaConf -from shutil import get_terminal_size -from ...backend.util import choose_precision, choose_torch_device from invokeai.backend.globals import Globals, global_config_dir -from ...backend.config.model_install_backend import (Dataset_path, default_config_file, - default_dataset, get_root, - install_requested_models, - recommended_datasets, - ) -from .widgets import (MultiSelectColumns, TextBox, - OffsetButtonPress, CenteredTitleText, - set_min_terminal_size, - ) + +from ...backend.config.model_install_backend import ( + Dataset_path, + default_config_file, + default_dataset, + get_root, + install_requested_models, + recommended_datasets, +) +from ...backend.util import choose_precision, choose_torch_device +from .widgets import ( + CenteredTitleText, + MultiSelectColumns, + OffsetButtonPress, + TextBox, + set_min_terminal_size, +) # minimum size for the UI MIN_COLS = 120 MIN_LINES = 45 + class addModelsForm(npyscreen.FormMultiPage): # for responsive resizing - disabled - #FIX_MINIMUM_SIZE_WHEN_CREATED = False - + # FIX_MINIMUM_SIZE_WHEN_CREATED = False + def __init__(self, parentApp, name, multipage=False, *args, **keywords): self.multipage = multipage self.initial_models = OmegaConf.load(Dataset_path) @@ -71,13 +79,13 @@ class addModelsForm(npyscreen.FormMultiPage): npyscreen.FixedText, value="Use ctrl-N and ctrl-P to move to the ext and

revious fields,", editable=False, - color='CAUTION', + color="CAUTION", ) self.add_widget_intelligent( npyscreen.FixedText, value="Use cursor arrows to make a selection, and space to toggle checkboxes.", editable=False, - color='CAUTION' + color="CAUTION", ) self.nextrely += 1 if len(self.installed_models) > 0: @@ -147,30 +155,26 @@ class addModelsForm(npyscreen.FormMultiPage): ) self.add_widget_intelligent( CenteredTitleText, - name='== IMPORT LOCAL AND REMOTE MODELS ==', + name="== IMPORT LOCAL AND REMOTE MODELS ==", editable=False, color="CONTROL", ) self.nextrely -= 1 for line in [ - "In the box below, enter URLs, file paths, or HuggingFace repository IDs.", - "Separate model names by lines or whitespace (Use shift-control-V to paste):", + "In the box below, enter URLs, file paths, or HuggingFace repository IDs.", + "Separate model names by lines or whitespace (Use shift-control-V to paste):", ]: self.add_widget_intelligent( CenteredTitleText, name=line, editable=False, labelColor="CONTROL", - relx = 4, + relx=4, ) self.nextrely -= 1 self.import_model_paths = self.add_widget_intelligent( - TextBox, - max_height=7, - scroll_exit=True, - editable=True, - relx=4 + TextBox, max_height=7, scroll_exit=True, editable=True, relx=4 ) self.nextrely += 1 self.show_directory_fields = self.add_widget_intelligent( @@ -245,7 +249,7 @@ class addModelsForm(npyscreen.FormMultiPage): def resize(self): super().resize() - if hasattr(self,'models_selected'): + if hasattr(self, "models_selected"): self.models_selected.values = self._get_starter_model_labels() def _clear_scan_directory(self): @@ -325,10 +329,11 @@ class addModelsForm(npyscreen.FormMultiPage): selections = self.parentApp.user_selections # starter models to install/remove - if hasattr(self,'models_selected'): + if hasattr(self, "models_selected"): starter_models = dict( map( - lambda x: (self.starter_model_list[x], True), self.models_selected.value + lambda x: (self.starter_model_list[x], True), + self.models_selected.value, ) ) else: @@ -376,6 +381,7 @@ class AddModelApplication(npyscreen.NPSAppManaged): "MAIN", addModelsForm, name="Install Stable Diffusion Models" ) + # -------------------------------------------------------- def process_and_execute(opt: Namespace, selections: Namespace): models_to_remove = [ @@ -477,9 +483,9 @@ def main(): print( ">> Your InvokeAI root directory is not set up. Calling invokeai-configure." ) - import ldm.invoke.config.invokeai_configure + from invokeai.frontend.install import invokeai_configure - ldm.invoke.config.invokeai_configure.main() + invokeai_configure() sys.exit(0) try: @@ -499,6 +505,7 @@ def main(): "** Insufficient horizontal space for the interface. Please make your window wider and try again." ) + # ------------------------------------- if __name__ == "__main__": main() diff --git a/invokeai/frontend/config/widgets.py b/invokeai/frontend/install/widgets.py similarity index 61% rename from invokeai/frontend/config/widgets.py rename to invokeai/frontend/install/widgets.py index 97832e7bb4..6c57b7cbd2 100644 --- a/invokeai/frontend/config/widgets.py +++ b/invokeai/frontend/install/widgets.py @@ -1,35 +1,39 @@ -''' +""" Widget class definitions used by model_select.py, merge_diffusers.py and textual_inversion.py -''' -import math -import platform -import npyscreen -import os -import sys +""" import curses +import math +import os +import platform import struct - +import sys from shutil import get_terminal_size +import npyscreen + + # ------------------------------------- def set_terminal_size(columns: int, lines: int): OS = platform.uname().system - if OS=="Windows": - os.system(f'mode con: cols={columns} lines={lines}') - elif OS in ['Darwin', 'Linux']: - import termios + if OS == "Windows": + os.system(f"mode con: cols={columns} lines={lines}") + elif OS in ["Darwin", "Linux"]: import fcntl + import termios + winsize = struct.pack("HHHH", lines, columns, 0, 0) fcntl.ioctl(sys.stdout.fileno(), termios.TIOCSWINSZ, winsize) sys.stdout.write("\x1b[8;{rows};{cols}t".format(rows=lines, cols=columns)) sys.stdout.flush() + def set_min_terminal_size(min_cols: int, min_lines: int): # make sure there's enough room for the ui term_cols, term_lines = get_terminal_size() - cols = max(term_cols, min_cols) + cols = max(term_cols, min_cols) lines = max(term_lines, min_lines) - set_terminal_size(cols,lines) + set_terminal_size(cols, lines) + class IntSlider(npyscreen.Slider): def translate_value(self): @@ -38,18 +42,20 @@ class IntSlider(npyscreen.Slider): stri = stri.rjust(l) return stri + # ------------------------------------- class CenteredTitleText(npyscreen.TitleText): - def __init__(self,*args,**keywords): - super().__init__(*args,**keywords) + def __init__(self, *args, **keywords): + super().__init__(*args, **keywords) self.resize() - + def resize(self): super().resize() maxy, maxx = self.parent.curses_pad.getmaxyx() label = self.name self.relx = (maxx - len(label)) // 2 - + + # ------------------------------------- class CenteredButtonPress(npyscreen.ButtonPress): def resize(self): @@ -57,21 +63,24 @@ class CenteredButtonPress(npyscreen.ButtonPress): maxy, maxx = self.parent.curses_pad.getmaxyx() label = self.name self.relx = (maxx - len(label)) // 2 - + + # ------------------------------------- class OffsetButtonPress(npyscreen.ButtonPress): - def __init__(self, screen, offset=0, *args, **keywords): + def __init__(self, screen, offset=0, *args, **keywords): super().__init__(screen, *args, **keywords) self.offset = offset - + def resize(self): maxy, maxx = self.parent.curses_pad.getmaxyx() width = len(self.name) self.relx = self.offset + (maxx - width) // 2 + class IntTitleSlider(npyscreen.TitleText): _entry_type = IntSlider + class FloatSlider(npyscreen.Slider): # this is supposed to adjust display precision, but doesn't def translate_value(self): @@ -80,85 +89,114 @@ class FloatSlider(npyscreen.Slider): stri = stri.rjust(l) return stri + class FloatTitleSlider(npyscreen.TitleText): _entry_type = FloatSlider + class MultiSelectColumns(npyscreen.MultiSelect): - def __init__(self, screen, columns: int=1, values: list=[], **keywords): + def __init__(self, screen, columns: int = 1, values: list = [], **keywords): self.columns = columns self.value_cnt = len(values) self.rows = math.ceil(self.value_cnt / self.columns) - super().__init__(screen,values=values, **keywords) + super().__init__(screen, values=values, **keywords) def make_contained_widgets(self): self._my_widgets = [] column_width = self.width // self.columns for h in range(self.value_cnt): self._my_widgets.append( - self._contained_widgets(self.parent, - rely=self.rely + (h % self.rows) * self._contained_widget_height, - relx=self.relx + (h // self.rows) * column_width, - max_width=column_width, - max_height=self.__class__._contained_widget_height, - ) + self._contained_widgets( + self.parent, + rely=self.rely + (h % self.rows) * self._contained_widget_height, + relx=self.relx + (h // self.rows) * column_width, + max_width=column_width, + max_height=self.__class__._contained_widget_height, + ) ) def set_up_handlers(self): super().set_up_handlers() - self.handlers.update({ - curses.KEY_UP: self.h_cursor_line_left, - curses.KEY_DOWN: self.h_cursor_line_right, - } - ) + self.handlers.update( + { + curses.KEY_UP: self.h_cursor_line_left, + curses.KEY_DOWN: self.h_cursor_line_right, + } + ) + def h_cursor_line_down(self, ch): self.cursor_line += self.rows if self.cursor_line >= len(self.values): - if self.scroll_exit: - self.cursor_line = len(self.values)-self.rows + if self.scroll_exit: + self.cursor_line = len(self.values) - self.rows self.h_exit_down(ch) return True - else: + else: self.cursor_line -= self.rows return True def h_cursor_line_up(self, ch): self.cursor_line -= self.rows - if self.cursor_line < 0: + if self.cursor_line < 0: if self.scroll_exit: self.cursor_line = 0 self.h_exit_up(ch) - else: + else: self.cursor_line = 0 - def h_cursor_line_left(self,ch): + def h_cursor_line_left(self, ch): super().h_cursor_line_up(ch) - - def h_cursor_line_right(self,ch): + + def h_cursor_line_right(self, ch): super().h_cursor_line_down(ch) + class TextBox(npyscreen.MultiLineEdit): def update(self, clear=True): - if clear: self.clear() + if clear: + self.clear() HEIGHT = self.height - WIDTH = self.width + WIDTH = self.width # draw box. self.parent.curses_pad.hline(self.rely, self.relx, curses.ACS_HLINE, WIDTH) - self.parent.curses_pad.hline(self.rely + HEIGHT, self.relx, curses.ACS_HLINE, WIDTH) - self.parent.curses_pad.vline(self.rely, self.relx, curses.ACS_VLINE, self.height) - self.parent.curses_pad.vline(self.rely, self.relx+WIDTH, curses.ACS_VLINE, HEIGHT) - + self.parent.curses_pad.hline( + self.rely + HEIGHT, self.relx, curses.ACS_HLINE, WIDTH + ) + self.parent.curses_pad.vline( + self.rely, self.relx, curses.ACS_VLINE, self.height + ) + self.parent.curses_pad.vline( + self.rely, self.relx + WIDTH, curses.ACS_VLINE, HEIGHT + ) + # draw corners - self.parent.curses_pad.addch(self.rely, self.relx, curses.ACS_ULCORNER, ) - self.parent.curses_pad.addch(self.rely, self.relx+WIDTH, curses.ACS_URCORNER, ) - self.parent.curses_pad.addch(self.rely+HEIGHT, self.relx, curses.ACS_LLCORNER, ) - self.parent.curses_pad.addch(self.rely+HEIGHT, self.relx+WIDTH, curses.ACS_LRCORNER, ) - + self.parent.curses_pad.addch( + self.rely, + self.relx, + curses.ACS_ULCORNER, + ) + self.parent.curses_pad.addch( + self.rely, + self.relx + WIDTH, + curses.ACS_URCORNER, + ) + self.parent.curses_pad.addch( + self.rely + HEIGHT, + self.relx, + curses.ACS_LLCORNER, + ) + self.parent.curses_pad.addch( + self.rely + HEIGHT, + self.relx + WIDTH, + curses.ACS_LRCORNER, + ) + # fool our superclass into thinking drawing area is smaller - this is really hacky but it seems to work - (relx,rely,height,width) = (self.relx, self.rely, self.height, self.width) + (relx, rely, height, width) = (self.relx, self.rely, self.height, self.width) self.relx += 1 self.rely += 1 self.height -= 1 self.width -= 1 super().update(clear=False) - (self.relx,self.rely,self.height,self.width) = (relx, rely, height, width) + (self.relx, self.rely, self.height, self.width) = (relx, rely, height, width) diff --git a/invokeai/frontend/merge/__init__.py b/invokeai/frontend/merge/__init__.py index 8e46a0621b..3a2e4474a5 100644 --- a/invokeai/frontend/merge/__init__.py +++ b/invokeai/frontend/merge/__init__.py @@ -1,4 +1,4 @@ -''' +""" Initialization file for invokeai.frontend.merge -''' +""" from .merge_diffusers import main as invokeai_merge_diffusers diff --git a/invokeai/frontend/merge/merge_diffusers.py b/invokeai/frontend/merge/merge_diffusers.py index 82765af96d..18cdf4ff6a 100644 --- a/invokeai/frontend/merge/merge_diffusers.py +++ b/invokeai/frontend/merge/merge_diffusers.py @@ -1,5 +1,5 @@ """ -ldm.invoke.merge_diffusers exports a single function call merge_diffusion_models() +invokeai.frontend.merge exports a single function call merge_diffusion_models() used to merge 2-3 models together and create a new InvokeAI-registered diffusion model. Copyright (c) 2023 Lincoln Stein and the InvokeAI Development Team @@ -20,13 +20,19 @@ from diffusers import logging as dlogging from npyscreen import widget from omegaconf import OmegaConf -from ...frontend.config.widgets import FloatTitleSlider -from ...backend.globals import (Globals, global_cache_dir, global_config_file, - global_models_dir, global_set_root) +from ...backend.globals import ( + Globals, + global_cache_dir, + global_config_file, + global_models_dir, + global_set_root, +) from ...backend.model_management import ModelManager +from ...frontend.install.widgets import FloatTitleSlider DEST_MERGED_MODEL_DIR = "merged_models" + def merge_diffusion_models( model_ids_or_paths: List[Union[str, Path]], alpha: float = 0.5, @@ -310,8 +316,8 @@ class mergeModelsForm(npyscreen.FormMultiPageAction): self.merged_model_name.value = merged_model_name if selected_model3 > 0: - self.merge_method.values = ['add_difference ( A+(B-C) )'] - self.merged_model_name.value += f"+{models[selected_model3 -1]}" # In model3 there is one more element in the list (None). So we have to subtract one. + self.merge_method.values = ["add_difference ( A+(B-C) )"] + self.merged_model_name.value += f"+{models[selected_model3 -1]}" # In model3 there is one more element in the list (None). So we have to subtract one. else: self.merge_method.values = self.interpolations self.merge_method.value = 0 @@ -336,9 +342,9 @@ class mergeModelsForm(npyscreen.FormMultiPageAction): ] if self.model3.value[0] > 0: models.append(model_names[self.model3.value[0] - 1]) - interp='add_difference' + interp = "add_difference" else: - interp=self.interpolations[self.merge_method.value[0]] + interp = self.interpolations[self.merge_method.value[0]] args = dict( models=models, @@ -453,7 +459,9 @@ def main(): "** You need to have at least two diffusers models defined in models.yaml in order to merge" ) else: - print("** Not enough room for the user interface. Try making this window larger.") + print( + "** Not enough room for the user interface. Try making this window larger." + ) sys.exit(-1) except Exception: print(">> An error occurred:") diff --git a/invokeai/frontend/training/__init__.py b/invokeai/frontend/training/__init__.py index 1aeece6b5f..db5d69fc7a 100644 --- a/invokeai/frontend/training/__init__.py +++ b/invokeai/frontend/training/__init__.py @@ -1,5 +1,4 @@ -''' +""" Initialization file for invokeai.frontend.training -''' +""" from .textual_inversion import main as invokeai_textual_inversion - diff --git a/invokeai/frontend/training/textual_inversion.py b/invokeai/frontend/training/textual_inversion.py index 5cd5d71909..e97284da3d 100755 --- a/invokeai/frontend/training/textual_inversion.py +++ b/invokeai/frontend/training/textual_inversion.py @@ -21,10 +21,8 @@ from npyscreen import widget from omegaconf import OmegaConf from invokeai.backend.globals import Globals, global_set_root -from ...backend.training import ( - do_textual_inversion_training, - parse_args, -) + +from ...backend.training import do_textual_inversion_training, parse_args TRAINING_DATA = "text-inversion-training-data" TRAINING_DIR = "text-inversion-output" @@ -448,9 +446,9 @@ def main(): print( "** You need to have at least one diffusers models defined in models.yaml in order to train" ) - elif str(e).startswith('addwstr'): + elif str(e).startswith("addwstr"): print( - '** Not enough window space for the interface. Please make your window larger and try again.' + "** Not enough window space for the interface. Please make your window larger and try again." ) else: print(f"** An error has occurred: {str(e)}") diff --git a/invokeai/version/__init__.py b/invokeai/version/__init__.py index 805834bb9a..215477dce8 100644 --- a/invokeai/version/__init__.py +++ b/invokeai/version/__init__.py @@ -1,20 +1,26 @@ -''' +""" initialization file for invokeai -''' +""" import invokeai + from .invokeai_version import __version__ -__app_id__= 'invoke-ai/InvokeAI' -__app_name__= 'InvokeAI' + +__app_id__ = "invoke-ai/InvokeAI" +__app_name__ = "InvokeAI" # copy these attributes into the invokeai namespace -setattr(invokeai,'__version__',__version__) -setattr(invokeai,'__app_id__',__app_id__) -setattr(invokeai,'__app_name__',__app_name__) +setattr(invokeai, "__version__", __version__) +setattr(invokeai, "__app_id__", __app_id__) +setattr(invokeai, "__app_name__", __app_name__) + def _ignore_xformers_triton_message_on_windows(): import logging + logging.getLogger("xformers").addFilter( - lambda record: 'A matching Triton is not available' not in record.getMessage()) + lambda record: "A matching Triton is not available" not in record.getMessage() + ) + # In order to be effective, this needs to happen before anything could possibly import xformers. _ignore_xformers_triton_message_on_windows() diff --git a/invokeai/version/invokeai_version.py b/invokeai/version/invokeai_version.py index 041471f37e..711c95b0cf 100644 --- a/invokeai/version/invokeai_version.py +++ b/invokeai/version/invokeai_version.py @@ -1 +1 @@ -__version__='3.0.0+a0' +__version__ = "3.0.0+a0" diff --git a/pyproject.toml b/pyproject.toml index 45fe8ef327..ed7c6563dc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -105,15 +105,15 @@ dependencies = [ # legacy entrypoints; provided for backwards compatibility "invoke.py" = "invokeai.frontend.CLI:command_line_interface" -"configure_invokeai.py" = "invokeai.frontend.config:invokeai_configure" -"textual_inversion.py" = "invokeai.frontend.training:textual_inversion" +"configure_invokeai.py" = "invokeai.frontend.install:invokeai_configure" +"textual_inversion.py" = "invokeai.frontend.training:invokeai_textual_inversion" # modern entrypoints "invokeai" = "invokeai.frontend.CLI:invokeai_command_line_interface" -"invokeai-configure" = "invokeai.frontend.config:invokeai_configure" +"invokeai-configure" = "invokeai.frontend.install:invokeai_configure" "invokeai-merge" = "invokeai.frontend.merge:invokeai_merge_diffusers" "invokeai-ti" = "invokeai.frontend.training:invokeai_textual_inversion" -"invokeai-model-install" = "invokeai.frontend.config:invokeai_model_install" +"invokeai-model-install" = "invokeai.frontend.install:invokeai_model_install" "invokeai-update" = "invokeai.frontend.config:invokeai_update" [project.urls] @@ -144,3 +144,7 @@ version = { attr = "invokeai.version.__version__" } [tool.pytest.ini_options] addopts = "-p pytest_cov --junitxml=junit/test-results.xml --cov-report=term:skip-covered --cov=ldm/invoke --cov=backend --cov-branch" + +[flake8] +max-line-length = 120 +