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
+
|
| 3 |
+
A fine-tuned Depth Anything V2 model for depth estimation, trained on forest canopy height data.
|
| 4 |
+
|
| 5 |
+
## Model Description
|
| 6 |
+
|
| 7 |
+
This model is based on [Depth-Anything-V2-Metric-Indoor-Base](https://huggingface.co/depth-anything/Depth-Anything-V2-Metric-Indoor-Base-hf) and fine-tuned for estimating depth/canopy height from aerial imagery.
|
| 8 |
+
|
| 9 |
+
### Training Details
|
| 10 |
+
|
| 11 |
+
- **Base Model**: depth-anything/Depth-Anything-V2-Metric-Indoor-Base-hf
|
| 12 |
+
- **Max Depth**: 40.0 meters
|
| 13 |
+
- **Loss Function**: SiLog + 0.1 * L1 Loss
|
| 14 |
+
- **Hyperparameter Tuning**: Optuna (50 trials)
|
| 15 |
+
|
| 16 |
+
## Installation
|
| 17 |
+
|
| 18 |
+
```bash
|
| 19 |
+
pip install transformers torch pillow numpy
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
## Usage
|
| 23 |
+
|
| 24 |
+
### Method 1: Using Pipeline (Recommended)
|
| 25 |
+
|
| 26 |
+
The simplest way to use the model:
|
| 27 |
+
|
| 28 |
+
```python
|
| 29 |
+
from transformers import pipeline
|
| 30 |
+
from PIL import Image
|
| 31 |
+
import numpy as np
|
| 32 |
+
|
| 33 |
+
# Load pipeline
|
| 34 |
+
pipe = pipeline(task="depth-estimation", model="Boxiang/depth_chm")
|
| 35 |
+
|
| 36 |
+
# Load image
|
| 37 |
+
image = Image.open("your_image.png").convert("RGB")
|
| 38 |
+
|
| 39 |
+
# Run inference
|
| 40 |
+
result = pipe(image)
|
| 41 |
+
depth_image = result["depth"] # PIL Image (normalized 0-255)
|
| 42 |
+
|
| 43 |
+
# Convert to numpy array and scale to actual depth (0-40m)
|
| 44 |
+
max_depth = 40.0
|
| 45 |
+
depth = np.array(depth_image).astype(np.float32) / 255.0 * max_depth
|
| 46 |
+
|
| 47 |
+
print(f"Depth shape: {depth.shape}")
|
| 48 |
+
print(f"Depth range: [{depth.min():.2f}, {depth.max():.2f}] meters")
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
### Method 2: Using AutoImageProcessor + Model
|
| 52 |
+
|
| 53 |
+
For more control over the inference process:
|
| 54 |
+
|
| 55 |
+
```python
|
| 56 |
+
import torch
|
| 57 |
+
import torch.nn.functional as F
|
| 58 |
+
from transformers import AutoImageProcessor, DepthAnythingForDepthEstimation
|
| 59 |
+
from PIL import Image
|
| 60 |
+
import numpy as np
|
| 61 |
+
|
| 62 |
+
# Configuration
|
| 63 |
+
model_id = "Boxiang/depth_chm"
|
| 64 |
+
max_depth = 40.0
|
| 65 |
+
|
| 66 |
+
# Load model and processor
|
| 67 |
+
processor = AutoImageProcessor.from_pretrained(model_id)
|
| 68 |
+
model = DepthAnythingForDepthEstimation.from_pretrained(model_id)
|
| 69 |
+
|
| 70 |
+
# Use GPU if available
|
| 71 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 72 |
+
model = model.to(device)
|
| 73 |
+
model.eval()
|
| 74 |
+
|
| 75 |
+
# Load and process image
|
| 76 |
+
image = Image.open("your_image.png").convert("RGB")
|
| 77 |
+
original_size = image.size # (width, height)
|
| 78 |
+
|
| 79 |
+
# Prepare input
|
| 80 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 81 |
+
pixel_values = inputs["pixel_values"].to(device)
|
| 82 |
+
|
| 83 |
+
# Run inference
|
| 84 |
+
with torch.no_grad():
|
| 85 |
+
outputs = model(pixel_values)
|
| 86 |
+
predicted_depth = outputs.predicted_depth
|
| 87 |
+
|
| 88 |
+
# Scale by max_depth
|
| 89 |
+
pred_scaled = predicted_depth * max_depth
|
| 90 |
+
|
| 91 |
+
# Resize to original image size
|
| 92 |
+
depth = F.interpolate(
|
| 93 |
+
pred_scaled.unsqueeze(0),
|
| 94 |
+
size=(original_size[1], original_size[0]), # (height, width)
|
| 95 |
+
mode="bilinear",
|
| 96 |
+
align_corners=True
|
| 97 |
+
).squeeze().cpu().numpy()
|
| 98 |
+
|
| 99 |
+
print(f"Depth shape: {depth.shape}")
|
| 100 |
+
print(f"Depth range: [{depth.min():.2f}, {depth.max():.2f}] meters")
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
### Method 3: Local Model Path
|
| 104 |
+
|
| 105 |
+
If you have the model saved locally:
|
| 106 |
+
|
| 107 |
+
```python
|
| 108 |
+
from transformers import AutoImageProcessor, DepthAnythingForDepthEstimation
|
| 109 |
+
|
| 110 |
+
# Load from local path
|
| 111 |
+
model_path = "./depth_chm_trained"
|
| 112 |
+
processor = AutoImageProcessor.from_pretrained(model_path, local_files_only=True)
|
| 113 |
+
model = DepthAnythingForDepthEstimation.from_pretrained(model_path, local_files_only=True)
|
| 114 |
+
```
|
| 115 |
+
|
| 116 |
+
## Output Format
|
| 117 |
+
|
| 118 |
+
- **Pipeline output**: Returns a PIL Image with normalized depth values (0-255). Multiply by `max_depth / 255.0` to get actual depth in meters.
|
| 119 |
+
- **Model output**: Returns `predicted_depth` tensor with values in range [0, 1]. Multiply by `max_depth` (40.0) to get actual depth in meters.
|
| 120 |
+
|
| 121 |
+
## Depth vs Height Conversion
|
| 122 |
+
|
| 123 |
+
The model outputs **depth** (distance from camera). To convert to **height** (like CHM - Canopy Height Model):
|
| 124 |
+
|
| 125 |
+
```python
|
| 126 |
+
height = max_depth - depth
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
## Model Files
|
| 130 |
+
|
| 131 |
+
- `model.safetensors` - Model weights
|
| 132 |
+
- `config.json` - Model configuration
|
| 133 |
+
- `preprocessor_config.json` - Image processor configuration
|
| 134 |
+
- `training_info.json` - Training hyperparameters
|
| 135 |
+
|
| 136 |
+
## Citation
|
| 137 |
+
|
| 138 |
+
If you use this model, please cite:
|
| 139 |
+
|
| 140 |
+
```bibtex
|
| 141 |
+
@misc{depth_chm_2024,
|
| 142 |
+
title={Depth-CHM: Fine-tuned Depth Anything V2 for Canopy Height Estimation},
|
| 143 |
+
author={Boxiang},
|
| 144 |
+
year={2024},
|
| 145 |
+
url={https://huggingface.co/Boxiang/depth_chm}
|
| 146 |
+
}
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
## License
|
| 150 |
+
|
| 151 |
+
This model inherits the license from the base Depth Anything V2 model.
|