|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import torch |
| 8 | +from executorch.examples.models.model_base import EagerModelBase |
| 9 | + |
| 10 | + |
| 11 | +class DepthAnythingV2Model(EagerModelBase): |
| 12 | + def __init__(self, model_name="depth-anything/Depth-Anything-V2-Small-hf"): |
| 13 | + self.model_name = model_name |
| 14 | + |
| 15 | + def _load_model(self): |
| 16 | + """Load the Depth Anything V2 model from HuggingFace""" |
| 17 | + try: |
| 18 | + from transformers import AutoImageProcessor, AutoModelForDepthEstimation |
| 19 | + except ImportError: |
| 20 | + raise ImportError( |
| 21 | + "transformers is required for DepthAnythingV2Model. " |
| 22 | + "Install with: pip install transformers" |
| 23 | + ) |
| 24 | + |
| 25 | + # Load model and processor |
| 26 | + self.processor = AutoImageProcessor.from_pretrained(self.model_name) |
| 27 | + model = AutoModelForDepthEstimation.from_pretrained(self.model_name) |
| 28 | + |
| 29 | + return model |
| 30 | + |
| 31 | + def get_eager_model(self) -> torch.nn.Module: |
| 32 | + return DepthAnythingV2Wrapper(self.model_name) |
| 33 | + |
| 34 | + def get_example_inputs(self): |
| 35 | + """Get example inputs for the model""" |
| 36 | + # Standard input size for Depth Anything V2 models |
| 37 | + # The model expects images of size (3, 518, 518) based on the processor configuration |
| 38 | + return (torch.randn(1, 3, 518, 518),) |
| 39 | + |
| 40 | + def get_dynamic_shapes(self): |
| 41 | + """Dynamic shapes for variable input sizes""" |
| 42 | + return {"pixel_values": {0: "batch_size", 2: "height", 3: "width"}} |
| 43 | + |
| 44 | + |
| 45 | +class DepthAnythingV2Wrapper(torch.nn.Module): |
| 46 | + """ |
| 47 | + Wrapper for Depth Anything V2 model that handles preprocessing and provides a clean interface. |
| 48 | + """ |
| 49 | + |
| 50 | + def __init__(self, model_name="depth-anything/Depth-Anything-V2-Small-hf"): |
| 51 | + super().__init__() |
| 52 | + try: |
| 53 | + from transformers import AutoImageProcessor, AutoModelForDepthEstimation |
| 54 | + except ImportError: |
| 55 | + raise ImportError( |
| 56 | + "transformers is required for DepthAnythingV2Model. " |
| 57 | + "Install with: pip install transformers" |
| 58 | + ) |
| 59 | + |
| 60 | + self.processor = AutoImageProcessor.from_pretrained(model_name) |
| 61 | + self.model = AutoModelForDepthEstimation.from_pretrained(model_name) |
| 62 | + |
| 63 | + # Set to evaluation mode |
| 64 | + self.model.eval() |
| 65 | + |
| 66 | + def forward(self, pixel_values: torch.Tensor) -> torch.Tensor: |
| 67 | + """ |
| 68 | + Forward pass for depth estimation. |
| 69 | +
|
| 70 | + Args: |
| 71 | + pixel_values: Input image tensor of shape (batch_size, 3, height, width) |
| 72 | + Values should be normalized to [0, 1] range |
| 73 | +
|
| 74 | + Returns: |
| 75 | + predicted_depth: Depth map tensor of shape (batch_size, height, width) |
| 76 | + """ |
| 77 | + # The model expects inputs to be preprocessed |
| 78 | + # pixel_values should already be properly normalized and sized |
| 79 | + |
| 80 | + with torch.no_grad(): |
| 81 | + outputs = self.model(pixel_values=pixel_values) |
| 82 | + predicted_depth = outputs.predicted_depth |
| 83 | + |
| 84 | + # The model outputs depth in a specific format - we may need to interpolate |
| 85 | + # to match the input image size |
| 86 | + if predicted_depth.shape[-2:] != pixel_values.shape[-2:]: |
| 87 | + predicted_depth = torch.nn.functional.interpolate( |
| 88 | + predicted_depth.unsqueeze(1), |
| 89 | + size=pixel_values.shape[-2:], |
| 90 | + mode="bilinear", |
| 91 | + align_corners=False, |
| 92 | + ).squeeze(1) |
| 93 | + |
| 94 | + return predicted_depth |
| 95 | + |
| 96 | + def preprocess_image(self, image): |
| 97 | + """ |
| 98 | + Preprocess a PIL image for the model. |
| 99 | + This method is not used in the forward pass but can be helpful for testing. |
| 100 | + """ |
| 101 | + inputs = self.processor(images=image, return_tensors="pt") |
| 102 | + return inputs["pixel_values"] |
0 commit comments