Views
No views yet
pip install transformers torch pillow numpy1from transformers import pipeline
2from PIL import Image
3import numpy as np
4
5# Load pipeline
6pipe = pipeline(task="depth-estimation", model="Boxiang/depth_chm")
7
8# Load image
9image = Image.open("your_image.png").convert("RGB")
10
11# Run inference
12result = pipe(image)
13depth_image = result["depth"] # PIL Image (normalized 0-255)
14
15# Convert to numpy array and scale to actual depth (0-40m)
16max_depth = 40.0
17depth = np.array(depth_image).astype(np.float32) / 255.0 * max_depth
18
19print(f"Depth shape: {depth.shape}")
20print(f"Depth range: [{depth.min():.2f}, {depth.max():.2f}] meters")1import torch
2import torch.nn.functional as F
3from transformers import AutoImageProcessor, DepthAnythingForDepthEstimation
4from PIL import Image
5import numpy as np
6
7# Configuration
8model_id = "Boxiang/depth_chm"
9max_depth = 40.0
10
11# Load model and processor
12processor = AutoImageProcessor.from_pretrained(model_id)
13model = DepthAnythingForDepthEstimation.from_pretrained(model_id)
14
15# Use GPU if available
16device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17model = model.to(device)
18model.eval()
19
20# Load and process image
21image = Image.open("your_image.png").convert("RGB")
22original_size = image.size # (width, height)
23
24# Prepare input
25inputs = processor(images=image, return_tensors="pt")
26pixel_values = inputs["pixel_values"].to(device)
27
28# Run inference
29with torch.no_grad():
30 outputs = model(pixel_values)
31 predicted_depth = outputs.predicted_depth
32
33 # Scale by max_depth
34 pred_scaled = predicted_depth * max_depth
35
36 # Resize to original image size
37 depth = F.interpolate(
38 pred_scaled.unsqueeze(0),
39 size=(original_size[1], original_size[0]), # (height, width)
40 mode="bilinear",
41 align_corners=True
42 ).squeeze().cpu().numpy()
43
44print(f"Depth shape: {depth.shape}")
45print(f"Depth range: [{depth.min():.2f}, {depth.max():.2f}] meters")1from transformers import AutoImageProcessor, DepthAnythingForDepthEstimation
2
3# Load from local path
4model_path = "./depth_chm_trained"
5processor = AutoImageProcessor.from_pretrained(model_path, local_files_only=True)
6model = DepthAnythingForDepthEstimation.from_pretrained(model_path, local_files_only=True)max_depth / 255.0 to get actual depth in meters.predicted_depth tensor with values in range [0, 1]. Multiply by max_depth (40.0) to get actual depth in meters.height = max_depth - depthmodel.safetensors - Model weightsconfig.json - Model configurationpreprocessor_config.json - Image processor configurationtraining_info.json - Training hyperparameters1@misc{depth_chm_2024,
2 title={Depth-CHM: Fine-tuned Depth Anything V2 for Canopy Height Estimation},
3 author={Boxiang},
4 year={2024},
5 url={https://huggingface.co/Boxiang/depth_chm}
6}