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

will load the MiDaS v2.1 model. The model expects 3-channel RGB images of shape (3 x H x W). Images are expected to be normalized using mean=[0.485, 0.456, 0.406] and std=[0.229, 0.224, 0.225]. H and W need to be divisible by 32. For optimal results H and W should be close to 384 (the training resolution). We provide a custom transformation that performs resizing while maintaining aspect ratio.

Model Description

MiDaS computes relative inverse depth from a single image. The model has been trained on 10 distinct dataset using multi-objective optimization to ensure high quality on a wide range of inputs.

Example Usage

Download an image from the PyTorch homepage

import cv2
import torch
import urllib.request

import matplotlib.pyplot as plt

url, filename = ("", "dog.jpg")
urllib.request.urlretrieve(url, filename)

Load large or small model

use_large_model = True

if use_large_model:
    midas = torch.hub.load("intel-isl/MiDaS", "MiDaS")
    midas = torch.hub.load("intel-isl/MiDaS", "MiDaS_small")

Move model to GPU if available

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

Load transforms to resize and normalize the image for large or small model

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

if use_large_model:
    transform = midas_transforms.default_transform
    transform = midas_transforms.small_transform

Load image and apply transforms

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

input_batch = transform(img).to(device)

Predict and resize to original resolution

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

    prediction = torch.nn.functional.interpolate(
output = prediction.cpu().numpy()

Show result



Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-shot Cross-dataset Transfer

Please cite our paper if you use our model:

	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},