isl-org / MiDaS

Code for robust monocular depth estimation described in "Ranftl et. al., Towards Robust Monocular Depth Estimation: Mixing Datasets for Zero-shot Cross-dataset Transfer, TPAMI 2022"
MIT License
4.24k stars 595 forks source link

New ‘ModuleNotFoundError’ #270

Open WorldofDepth opened 3 months ago

WorldofDepth commented 3 months ago

Hi, until recently I could run this via a Google Colab notebook (with timm 0.6.13 and torch 2.0.1 to avoid other errors), but today running the following cell:

%cd /content/MiDaS/
!python run.py --model_type dpt_beit_large_512 --input_path input --output_path output

Produces the error output below:

/content/MiDaS
Traceback (most recent call last):
  File "/content/MiDaS/run.py", line 14, in <module>
    from midas.model_loader import default_models, load_model
  File "/content/MiDaS/midas/model_loader.py", line 4, in <module>
    from midas.dpt_depth import DPTDepthModel
  File "/content/MiDaS/midas/dpt_depth.py", line 5, in <module>
    from .blocks import (
  File "/content/MiDaS/midas/blocks.py", line 4, in <module>
    from .backbones.beit import (
  File "/content/MiDaS/midas/backbones/beit.py", line 1, in <module>
    import timm
  File "/usr/local/lib/python3.10/dist-packages/timm/__init__.py", line 2, in <module>
    from .models import create_model, list_models, is_model, list_modules, model_entrypoint, \
  File "/usr/local/lib/python3.10/dist-packages/timm/models/__init__.py", line 1, in <module>
    from .beit import *
  File "/usr/local/lib/python3.10/dist-packages/timm/models/beit.py", line 49, in <module>
    from timm.data import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
  File "/usr/local/lib/python3.10/dist-packages/timm/data/__init__.py", line 5, in <module>
    from .dataset import ImageDataset, IterableImageDataset, AugMixDataset
  File "/usr/local/lib/python3.10/dist-packages/timm/data/dataset.py", line 12, in <module>
    from .parsers import create_parser
  File "/usr/local/lib/python3.10/dist-packages/timm/data/parsers/__init__.py", line 1, in <module>
    from .parser_factory import create_parser
  File "/usr/local/lib/python3.10/dist-packages/timm/data/parsers/parser_factory.py", line 3, in <module>
    from .parser_image_folder import ParserImageFolder
  File "/usr/local/lib/python3.10/dist-packages/timm/data/parsers/parser_image_folder.py", line 11, in <module>
    from timm.utils.misc import natural_key
  File "/usr/local/lib/python3.10/dist-packages/timm/utils/__init__.py", line 2, in <module>
    from .checkpoint_saver import CheckpointSaver
  File "/usr/local/lib/python3.10/dist-packages/timm/utils/checkpoint_saver.py", line 15, in <module>
    from .model import unwrap_model, get_state_dict
  File "/usr/local/lib/python3.10/dist-packages/timm/utils/model.py", line 8, in <module>
    from torchvision.ops.misc import FrozenBatchNorm2d
  File "/usr/local/lib/python3.10/dist-packages/torchvision/__init__.py", line 6, in <module>
    from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils
  File "/usr/local/lib/python3.10/dist-packages/torchvision/_meta_registrations.py", line 4, in <module>
    import torch._custom_ops
ModuleNotFoundError: No module named 'torch._custom_ops'

Through experimentation, I find that deleting references to ‘torch._custom_ops' in the _meta_registrations.py file allows MiDaS to run, but obviously this is not the most correct fix. Any advice for a better solution? Thank you!

heyoeyo commented 3 months ago

It's hard to say for sure, but I would guess this has to do with the libraries being updated, possibly by some other project. Based on the pathing of the libraries in the error message (e.g. /usr/local/lib/python3.10/dist-packages/...) all of the libraries are installed in the base system copy of python, which means they can get modified by system updates (since the operating system also uses this copy of python) and potentially other projects which try to use the same python.

The general solution for this is to use virtual environments, either through python itself (using venv) or something like conda. These act like dedicated sandboxes where you'd install all of the libraries for a single project in one place, which only gets used by that one project, and therefore won't be affected by updates to other projects (which should have their own separate environments).

(unrelated) Also, since you're using beit, I figured I'd mention that there there are improved models available that might be worth checking out. One is called Depth-Anything (specifically the vit-large model) and another much higher quality (but very slow to run) model called Marigold.