MiDaS

模型描述

MiDaS 透過單張影像計算相對逆深度。該儲存庫提供了多種模型,涵蓋了不同的用例,從小型、高速模型到提供最高精度的大型模型。這些模型已使用多目標最佳化在 10 個不同的資料集上進行訓練,以確保在各種輸入上都能實現高質量。

依賴項

MiDaS 依賴於 timm。使用以下命令安裝:

pip install timm

示例用法

從 PyTorch 主頁下載一張圖片

import cv2
import torch
import urllib.request

import matplotlib.pyplot as plt

url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg", "dog.jpg")
urllib.request.urlretrieve(url, filename)

載入模型(請參閱 https://github.com/intel-isl/MiDaS/#Accuracy 獲取概述)

model_type = "DPT_Large"     # MiDaS v3 - Large     (highest accuracy, slowest inference speed)
#model_type = "DPT_Hybrid"   # MiDaS v3 - Hybrid    (medium accuracy, medium inference speed)
#model_type = "MiDaS_small"  # MiDaS v2.1 - Small   (lowest accuracy, highest inference speed)

midas = torch.hub.load("intel-isl/MiDaS", model_type)

如果可用,將模型移至 GPU

device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
midas.to(device)
midas.eval()

載入變換以調整大模型或小模型的影像大小並進行歸一化

midas_transforms = torch.hub.load("intel-isl/MiDaS", "transforms")

if model_type == "DPT_Large" or model_type == "DPT_Hybrid":
    transform = midas_transforms.dpt_transform
else:
    transform = midas_transforms.small_transform

載入影像並應用變換

img = cv2.imread(filename)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

input_batch = transform(img).to(device)

預測並調整到原始解析度

with torch.no_grad():
    prediction = midas(input_batch)

    prediction = torch.nn.functional.interpolate(
        prediction.unsqueeze(1),
        size=img.shape[:2],
        mode="bicubic",
        align_corners=False,
    ).squeeze()

output = prediction.cpu().numpy()

顯示結果

plt.imshow(output)
# plt.show()

參考文獻

邁向魯棒的單目深度估計:混合資料集實現零樣本跨資料集遷移

用於密集預測的視覺 Transformer

如果您使用我們的模型,請引用我們的論文

@article{Ranftl2020,
	author    = {Ren\'{e} Ranftl and Katrin Lasinger and David Hafner and Konrad Schindler and Vladlen Koltun},
	title     = {Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-shot Cross-dataset Transfer},
	journal   = {IEEE Transactions on Pattern Analysis and Machine Intelligence (TPAMI)},
	year      = {2020},
}
@article{Ranftl2021,
	author    = {Ren\'{e} Ranftl and Alexey Bochkovskiy and Vladlen Koltun},
	title     = {Vision Transformers for Dense Prediction},
	journal   = {ArXiv preprint},
	year      = {2021},
}

MiDaS 模型用於從單個影像計算相對深度。

模型型別: 視覺
提交者: Intel ISL