File size: 2,228 Bytes
d0d0fc1
 
 
 
 
a138d14
d0d0fc1
a138d14
d0d0fc1
a138d14
b91dbc2
d0d0fc1
 
a138d14
 
 
d0d0fc1
a138d14
 
 
 
 
 
 
b91dbc2
a138d14
d0d0fc1
 
a138d14
 
 
b91dbc2
a138d14
 
 
 
d0d0fc1
 
a138d14
 
 
 
b91dbc2
a138d14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0d0fc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
---
license: apple-ascl
pipeline_tag: depth-estimation
---

# DepthPro: Monocular Depth Estimation

Install the required libraries:
```bash
pip install -q numpy pillow torch torchvision
pip install -q git+https://github.com/geetu040/transformers.git@depth-pro#egg=transformers
```

Import the required libraries:
```py
import requests
from PIL import Image
import torch
import torch.nn as nn
import torch.nn.functional as F
from huggingface_hub import hf_hub_download
import matplotlib.pyplot as plt

# custom installation from this PR: https://github.com/huggingface/transformers/pull/34583
# !pip install git+https://github.com/geetu040/transformers.git@depth-pro#egg=transformers
from transformers import DepthProConfig, DepthProImageProcessorFast, DepthProForDepthEstimation
```

Load the model and image processor:
```py
checkpoint = "geetu040/DepthPro"
revision = "main"
image_processor = DepthProImageProcessorFast.from_pretrained(checkpoint, revision=revision)
model = DepthProForDepthEstimation.from_pretrained(checkpoint, revision=revision)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
```

Inference:
```py
# inference

url = "https://huggingface.co/geetu040/DepthPro/resolve/main/assets/tiger.jpg"

image = Image.open(requests.get(url, stream=True).raw)
image = image.convert("RGB")

# prepare image for the model
inputs = image_processor(images=image, return_tensors="pt")
inputs = {k: v.to(device) for k, v in inputs.items()}

with torch.no_grad():
  outputs = model(**inputs)

# interpolate to original size
post_processed_output = image_processor.post_process_depth_estimation(
  outputs, target_sizes=[(image.height, image.width)],
)

# visualize the prediction
depth = post_processed_output[0]["predicted_depth"]
depth = (depth - depth.min()) / depth.max()
depth = depth * 255.
depth = depth.detach().cpu().numpy()
depth = Image.fromarray(depth.astype("uint8"))

# visualize the prediction
fig, axes = plt.subplots(1, 2, figsize=(20, 20))
axes[0].imshow(image)
axes[0].set_title(f'Image {image.size}')
axes[0].axis('off')
axes[1].imshow(depth)
axes[1].set_title(f'Depth {depth.size}')
axes[1].axis('off')
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
```