Skip to content

Commit 556c6a1

Browse files
fix: Update DepthAnything to use the transformers implementation
1 parent e5d9ca0 commit 556c6a1

File tree

14 files changed

+10
-1613
lines changed

14 files changed

+10
-1613
lines changed

invokeai/app/invocations/controlnet_image_processors.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# initial implementation by Gregg Helt, 2023
33
# heavily leverages controlnet_aux package: https://github.com/patrickvonplaten/controlnet_aux
44
from builtins import bool, float
5-
from pathlib import Path
65
from typing import Dict, List, Literal, Union
76

87
import cv2
@@ -21,6 +20,7 @@
2120
from controlnet_aux.util import HWC3, ade_palette
2221
from PIL import Image
2322
from pydantic import BaseModel, Field, field_validator, model_validator
23+
from transformers import pipeline
2424

2525
from invokeai.app.invocations.baseinvocation import (
2626
BaseInvocation,
@@ -44,13 +44,11 @@
4444
from invokeai.app.services.shared.invocation_context import InvocationContext
4545
from invokeai.app.util.controlnet_utils import CONTROLNET_MODE_VALUES, CONTROLNET_RESIZE_VALUES, heuristic_resize
4646
from invokeai.backend.image_util.canny import get_canny_edges
47-
from invokeai.backend.image_util.depth_anything import DEPTH_ANYTHING_MODELS, DepthAnythingDetector
4847
from invokeai.backend.image_util.dw_openpose import DWPOSE_MODELS, DWOpenposeDetector
4948
from invokeai.backend.image_util.hed import HEDProcessor
5049
from invokeai.backend.image_util.lineart import LineartProcessor
5150
from invokeai.backend.image_util.lineart_anime import LineartAnimeProcessor
5251
from invokeai.backend.image_util.util import np_to_pil, pil_to_np
53-
from invokeai.backend.util.devices import TorchDevice
5452

5553

5654
class ControlField(BaseModel):
@@ -593,14 +591,19 @@ def run_processor(self, image: Image.Image) -> Image.Image:
593591

594592

595593
DEPTH_ANYTHING_MODEL_SIZES = Literal["large", "base", "small"]
594+
DEPTH_ANYTHING_MODELS = {
595+
"large": "LiheYoung/depth-anything-large-hf",
596+
"base": "LiheYoung/depth-anything-base-hf",
597+
"small": "depth-anything/Depth-Anything-V2-Small-hf",
598+
}
596599

597600

598601
@invocation(
599602
"depth_anything_image_processor",
600603
title="Depth Anything Processor",
601604
tags=["controlnet", "depth", "depth anything"],
602605
category="controlnet",
603-
version="1.1.2",
606+
version="1.1.3",
604607
)
605608
class DepthAnythingImageProcessorInvocation(ImageProcessorInvocation):
606609
"""Generates a depth map based on the Depth Anything algorithm"""
@@ -611,17 +614,9 @@ class DepthAnythingImageProcessorInvocation(ImageProcessorInvocation):
611614
resolution: int = InputField(default=512, ge=1, description=FieldDescriptions.image_res)
612615

613616
def run_processor(self, image: Image.Image) -> Image.Image:
614-
def loader(model_path: Path):
615-
return DepthAnythingDetector.load_model(
616-
model_path, model_size=self.model_size, device=TorchDevice.choose_torch_device()
617-
)
618-
619-
with self._context.models.load_remote_model(
620-
source=DEPTH_ANYTHING_MODELS[self.model_size], loader=loader
621-
) as model:
622-
depth_anything_detector = DepthAnythingDetector(model, TorchDevice.choose_torch_device())
623-
processed_image = depth_anything_detector(image=image, resolution=self.resolution)
624-
return processed_image
617+
depth_anything_pipeline = pipeline(task="depth-estimation", model=DEPTH_ANYTHING_MODELS[self.model_size])
618+
depth_map = depth_anything_pipeline(image)["depth"]
619+
return depth_map
625620

626621

627622
@invocation(

invokeai/backend/image_util/depth_anything/__init__.py

Lines changed: 0 additions & 65 deletions
This file was deleted.

invokeai/backend/image_util/depth_anything/utils/blocks.py

Lines changed: 0 additions & 147 deletions
This file was deleted.

0 commit comments

Comments
 (0)