Instructions to use kerasformers/depth_anything_v2_base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- KerasFormers
How to use kerasformers/depth_anything_v2_base with KerasFormers:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Keras
How to use kerasformers/depth_anything_v2_base with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://kerasformers/depth_anything_v2_base") - DepthAnythingV2
How to use kerasformers/depth_anything_v2_base with DepthAnythingV2:
# Install from https://github.com/DepthAnything/Depth-Anything-V2 # Load the model and infer depth from an image import cv2 import torch from depth_anything_v2.dpt import DepthAnythingV2 # instantiate the model model = DepthAnythingV2(encoder="<ENCODER>", features=<NUMBER_OF_FEATURES>, out_channels=<OUT_CHANNELS>) # load the weights filepath = hf_hub_download(repo_id="kerasformers/depth_anything_v2_base", filename="depth_anything_v2_<ENCODER>.pth", repo_type="model") state_dict = torch.load(filepath, map_location="cpu") model.load_state_dict(state_dict).eval() raw_img = cv2.imread("your/image/path") depth = model.infer_image(raw_img) # HxW raw depth map in numpy - Notebooks
- Google Colab
- Kaggle
| pipeline_tag: depth-estimation | |
| license: cc-by-nc-4.0 | |
| base_model: depth-anything/Depth-Anything-V2-Base-hf | |
| library_name: kerasformers | |
| tags: | |
| - keras | |
| - kerasformers | |
| - depth-anything-v2 | |
| - depth-estimation | |
| - arxiv:2406.09414 | |
| - pytorch | |
| - jax | |
| - tf | |
| ## ***See [our collection](https://huggingface.co/collections/kerasformers/depth-anything-v1-and-v2-6a6a965e3e9e4847e424dde2) for all versions of Depth Anything V2.*** | |
| # Run Depth Anything V2 with Keras 3: JAX, PyTorch, or TensorFlow | |
| [](https://github.com/IMvision12/KerasFormers) [](https://imvision12.github.io/KerasFormers/depth_anything_v2/) [](https://huggingface.co/collections/kerasformers/depth-anything-v1-and-v2-6a6a965e3e9e4847e424dde2) | |
| # kerasformers/depth_anything_v2_base | |
| Paper: [Depth Anything V2 (arXiv:2406.09414)](https://arxiv.org/abs/2406.09414) · [HF Papers](https://huggingface.co/papers/2406.09414) | |
| Depth Anything V2 keeps V1's architecture and improves data (synthetic labels plus large-scale pseudo-labeling). Relative heads return unitless inverse depth; metric indoor/outdoor heads return metres. | |
| For more details on the model, please go to the upstream [model card](https://huggingface.co/depth-anything/Depth-Anything-V2-Base-hf). | |
| Pure-**Keras 3** conversion of [`depth-anything/Depth-Anything-V2-Base-hf`](https://huggingface.co/depth-anything/Depth-Anything-V2-Base-hf) for [kerasformers](https://github.com/IMvision12/KerasFormers). One implementation runs unmodified on **TensorFlow / Torch / JAX**. | |
| This is a **monocular depth** checkpoint (`DepthAnythingV2DepthEstimation`, relative). | |
| ## ✨ Quick start | |
| ```python | |
| import os | |
| os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow" | |
| from PIL import Image | |
| from kerasformers.models.depth_anything_v2 import DepthAnythingV2DepthEstimation, DepthAnythingV2ImageProcessor | |
| model = DepthAnythingV2DepthEstimation.from_weights("kerasformers/depth_anything_v2_base") | |
| processor = DepthAnythingV2ImageProcessor.from_weights("kerasformers/depth_anything_v2_base") | |
| image = Image.open("your_image.jpg").convert("RGB") | |
| output = model(processor(image)["pixel_values"], training=False) | |
| depth = processor.post_process_depth_estimation( | |
| output, original_size=(image.height, image.width) | |
| ) | |
| print(depth.shape) | |
| ``` | |
| Load any Depth Anything V2 variant the same way with `from_weights("kerasformers/<variant>")`: | |
| | Variant | Hub | Output | | |
| |---|---|---| | |
| | `depth_anything_v2_small` | [`kerasformers/depth_anything_v2_small`](https://huggingface.co/kerasformers/depth_anything_v2_small) | relative | | |
| | `depth_anything_v2_base` | [`kerasformers/depth_anything_v2_base`](https://huggingface.co/kerasformers/depth_anything_v2_base) | relative | | |
| | `depth_anything_v2_large` | [`kerasformers/depth_anything_v2_large`](https://huggingface.co/kerasformers/depth_anything_v2_large) | relative | | |
| | `depth_anything_v2_metric_indoor_small` | [`kerasformers/depth_anything_v2_metric_indoor_small`](https://huggingface.co/kerasformers/depth_anything_v2_metric_indoor_small) | metric indoor | | |
| | `depth_anything_v2_metric_indoor_base` | [`kerasformers/depth_anything_v2_metric_indoor_base`](https://huggingface.co/kerasformers/depth_anything_v2_metric_indoor_base) | metric indoor | | |
| | `depth_anything_v2_metric_indoor_large` | [`kerasformers/depth_anything_v2_metric_indoor_large`](https://huggingface.co/kerasformers/depth_anything_v2_metric_indoor_large) | metric indoor | | |
| | `depth_anything_v2_metric_outdoor_small` | [`kerasformers/depth_anything_v2_metric_outdoor_small`](https://huggingface.co/kerasformers/depth_anything_v2_metric_outdoor_small) | metric outdoor | | |
| | `depth_anything_v2_metric_outdoor_base` | [`kerasformers/depth_anything_v2_metric_outdoor_base`](https://huggingface.co/kerasformers/depth_anything_v2_metric_outdoor_base) | metric outdoor | | |
| | `depth_anything_v2_metric_outdoor_large` | [`kerasformers/depth_anything_v2_metric_outdoor_large`](https://huggingface.co/kerasformers/depth_anything_v2_metric_outdoor_large) | metric outdoor | | |
| ## Tips | |
| - Set `KERAS_BACKEND` **before** importing Keras / kerasformers. | |
| - Indoor and outdoor metric heads are not interchangeable. | |
| - See [Depth Anything V2 docs]({DOCS_URL}) and [Loading Weights](https://imvision12.github.io/KerasFormers/loading_weights/). | |
| - Community / upstream weights: `DepthAnythingV2DepthEstimation.from_weights("hf:depth-anything/Depth-Anything-V2-Base-hf")`. | |
| ## Special Thanks | |
| A huge thank you to the Depth Anything authors for creating and releasing these models. | |
| License: Apache 2.0. | |