open-mmlab / mmdetection

OpenMMLab Detection Toolbox and Benchmark
https://mmdetection.readthedocs.io
Apache License 2.0
29.55k stars 9.46k forks source link

ModuleNotFoundError: No module named 'mmcv._ext' #11475

Open lsch0lz opened 8 months ago

lsch0lz commented 8 months ago

Thanks for your error report and we appreciate it a lot.

Checklist

  1. I have searched related issues but cannot get the expected help.
  2. I have read the FAQ documentation but cannot get the expected help.
  3. The bug has not been fixed in the latest version.

Describe the bug I can't install mmdetection local on my M1 MacbookPro. I follow each step in the GET STARTED Guide, but no matter what I do, I always get the following error:

ModuleNotFoundError: No module named 'mmcv._ext'

Reproduction

conda create --name openmmlab python=3.8 -y
conda activate openmmlab
conda install pytorch torchvision cpuonly -c pytorch

pip install -U openmim
mim install mmengine
mim install "mmcv>=2.0.0"

mim install mmdet

I use mmdet as a dependency in my project.

  1. What command or script did you run?

test_pipeline.py

import os
import logging

# from mmdet.utils import setup_cache_size_limit_of_dynamo

from mmengine.config import Config
from mmengine.runner import Runner

logger = logging.getLogger(__name__)

class TestPipeline:
    __test__ = False

    def test(self, test_configuration):
        logger.info('Setting up cache size limit of Dynamo')
        # setup_cache_size_limit_of_dynamo()

        logger.info(f'Test configuration: {test_configuration}')
        model_config: Config = Config.fromfile(test_configuration.config_path)
        logger.info(f'Model config: {model_config}')

        # if test_configuration.cfg_options is not None:
        #     model_config.merge_from_dict(test_configuration.cfg_options)

        if test_configuration.work_dir is not None:
            model_config.work_dir = test_configuration.work_dir
        elif model_config.get('work_dir', None) is None:
            model_config.work_dir = os.path.join('./work_dirs',
                                                 os.path.splitext(os.path.basename(test_configuration.config_path))[0])

        model_config.load_from = test_configuration.checkpoint_path

        runner = Runner.from_cfg(model_config)

        runner.test()

And the test, which should execute the test_pipeline:

import unittest

from src.test_pipeline import TestPipeline
from src.test_configuration import TestConfiguration

class TestTestPipeline(unittest.TestCase):
    def test_if_test_is_executed_with_given_config_and_model_file(self):
        config_path = "test/data/faster-rcnn_r50_fpn.py"
        model_checkpoint = "test/data/resnet50-19c8e357.pth"

        test_pipeline = TestPipeline()
        test_configuration = TestConfiguration(config_path=config_path,
                                               checkpoint_path=model_checkpoint)
        test_pipeline.test(test_configuration)
        assert True

I just wanted to test, if everything works with the setup, so don't mind the logic behind this test. Instead of an output I get the following error-log:

___________________________________________________________________________________ TestTestPipeline.test_if_test_is_executed_with_given_config_and_model_file ___________________________________________________________________________________

self = <unit.test_pipeline.TestTestPipeline testMethod=test_if_test_is_executed_with_given_config_and_model_file>

    def test_if_test_is_executed_with_given_config_and_model_file(self):
        config_path = "test/data/rtmdet_tiny_8xb32-300e_coco.py"
        model_checkpoint = "test/data/rtmdet_tiny_8xb32-300e_coco.py"

        test_pipeline = TestPipeline()
        test_configuration = TestConfiguration(config_path=config_path,
                                               checkpoint_path=model_checkpoint)
>       test_pipeline.test(test_configuration)

test/unit/test_pipeline.py:15: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
src/test_pipeline.py:34: in test
    runner = Runner.from_cfg(model_config)
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmengine/runner/runner.py:462: in from_cfg
    runner = cls(
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmengine/runner/runner.py:397: in __init__
    self.log_processor = self.build_log_processor(log_processor)
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmengine/runner/runner.py:1650: in build_log_processor
    log_processor = LOG_PROCESSORS.build(log_processor_cfg)
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmengine/registry/registry.py:570: in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmengine/registry/build_functions.py:98: in build_from_cfg
    obj_cls = registry.get(obj_type)
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmengine/registry/registry.py:451: in get
    self.import_from_location()
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmengine/registry/registry.py:376: in import_from_location
    import_module(loc)
../../opt/anaconda3/envs/openmmlab/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1014: in _gcd_import
    ???
<frozen importlib._bootstrap>:991: in _find_and_load
    ???
<frozen importlib._bootstrap>:975: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:671: in _load_unlocked
    ???
<frozen importlib._bootstrap_external>:843: in exec_module
    ???
<frozen importlib._bootstrap>:219: in _call_with_frames_removed
    ???
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/engine/__init__.py:2: in <module>
    from .hooks import *  # noqa: F401, F403
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/engine/hooks/__init__.py:10: in <module>
    from .visualization_hook import (DetVisualizationHook,
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/engine/hooks/visualization_hook.py:14: in <module>
    from mmdet.datasets.samplers import TrackImgSampler
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/datasets/__init__.py:31: in <module>
    from .utils import get_loading_pipeline
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/datasets/utils.py:5: in <module>
    from mmdet.datasets.transforms import LoadAnnotations, LoadPanopticAnnotations
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/datasets/transforms/__init__.py:6: in <module>
    from .formatting import (ImageToTensor, PackDetInputs, PackReIDInputs,
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/datasets/transforms/formatting.py:11: in <module>
    from mmdet.structures.bbox import BaseBoxes
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/structures/bbox/__init__.py:2: in <module>
    from .base_boxes import BaseBoxes
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/structures/bbox/base_boxes.py:9: in <module>
    from mmdet.structures.mask.structures import BitmapMasks, PolygonMasks
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/structures/mask/__init__.py:3: in <module>
    from .structures import (BaseInstanceMasks, BitmapMasks, PolygonMasks,
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmdet/structures/mask/structures.py:12: in <module>
    from mmcv.ops.roi_align import roi_align
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/ops/__init__.py:3: in <module>
    from .active_rotated_filter import active_rotated_filter
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/ops/active_rotated_filter.py:10: in <module>
    ext_module = ext_loader.load_ext(
../../opt/anaconda3/envs/openmmlab/lib/python3.8/site-packages/mmcv/utils/ext_loader.py:13: in load_ext
    ext = importlib.import_module('mmcv.' + name)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

name = 'mmcv._ext', package = None

    def import_module(name, package=None):
        """Import a module.

        The 'package' argument is required when performing a relative import. It
        specifies the package to use as the anchor point from which to resolve the
        relative import to an absolute import.

        """
        level = 0
        if name.startswith('.'):
            if not package:
                msg = ("the 'package' argument is required to perform a relative "
                       "import for {!r}")
                raise TypeError(msg.format(name))
            for character in name:
                if character != '.':
                    break
                level += 1
>       return _bootstrap._gcd_import(name[level:], package, level)
E       ModuleNotFoundError: No module named 'mmcv._ext'

../../opt/anaconda3/envs/openmmlab/lib/python3.8/importlib/__init__.py:127: ModuleNotFoundError
  1. Did you make any modifications on the code or config? Did you understand what you have modified? I copied the basic functionalities of your test method.

Environment

❯ conda list
# packages in environment at /Users/lukasscholz/opt/anaconda3/envs/openmmlab:
#
# Name                    Version                   Build  Channel
addict                    2.4.0                    pypi_0    pypi
aliyun-python-sdk-core    2.14.0                   pypi_0    pypi
aliyun-python-sdk-kms     2.16.2                   pypi_0    pypi
blas                      1.0                         mkl  
brotli-python             1.0.9            py38he9d5cce_7  
ca-certificates           2023.12.12           hecd8cb5_0  
certifi                   2024.2.2         py38hecd8cb5_0  
cffi                      1.16.0                   pypi_0    pypi
charset-normalizer        2.0.4              pyhd3eb1b0_0  
click                     8.1.7                    pypi_0    pypi
colorama                  0.4.6                    pypi_0    pypi
contourpy                 1.1.1                    pypi_0    pypi
cpuonly                   2.0                           0    pytorch
crcmod                    1.7                      pypi_0    pypi
cryptography              42.0.2                   pypi_0    pypi
cycler                    0.12.1                   pypi_0    pypi
exceptiongroup            1.2.0                    pypi_0    pypi
filelock                  3.13.1           py38hecd8cb5_0  
fonttools                 4.48.1                   pypi_0    pypi
freetype                  2.12.1               hd8bbffd_0  
gmp                       6.2.1                he9d5cce_3  
gmpy2                     2.1.2            py38hd5de756_0  
idna                      3.4              py38hecd8cb5_0  
importlib-metadata        7.0.1                    pypi_0    pypi
importlib-resources       6.1.1                    pypi_0    pypi
iniconfig                 2.0.0                    pypi_0    pypi
intel-openmp              2021.4.0          hecd8cb5_3538  
jinja2                    3.1.3            py38hecd8cb5_0  
jmespath                  0.10.0                   pypi_0    pypi
jpeg                      9e                   h6c40b1e_1  
kiwisolver                1.4.5                    pypi_0    pypi
lcms2                     2.12                 hf1fd2bf_0  
lerc                      3.0                  he9d5cce_0  
libcxx                    14.0.6               h9765a3e_0  
libdeflate                1.17                 hb664fd8_1  
libffi                    3.4.4                hecd8cb5_0  
libjpeg-turbo             2.0.0                hca72f7f_0  
libpng                    1.6.39               h6c40b1e_0  
libtiff                   4.5.1                hcec6c5f_0  
libwebp-base              1.3.2                h6c40b1e_0  
lz4-c                     1.9.4                hcec6c5f_0  
markdown                  3.5.2                    pypi_0    pypi
markdown-it-py            3.0.0                    pypi_0    pypi
markupsafe                2.1.3            py38h6c40b1e_0  
matplotlib                3.7.4                    pypi_0    pypi
mdurl                     0.1.2                    pypi_0    pypi
mkl                       2021.4.0           hecd8cb5_637  
mkl-service               2.4.0            py38h9ed2024_0  
mkl_fft                   1.3.1            py38h4ab4a9b_0  
mkl_random                1.2.2            py38hb2f4e1b_0  
mmcv                      2.1.0                    pypi_0    pypi
mmdet                     3.3.0                    pypi_0    pypi
mmengine                  0.10.3                   pypi_0    pypi
model-index               0.1.11                   pypi_0    pypi
mpc                       1.1.0                h6ef4df4_1  
mpfr                      4.0.2                h9066e36_1  
mpmath                    1.3.0            py38hecd8cb5_0  
ncurses                   6.4                  hcec6c5f_0  
networkx                  3.1              py38hecd8cb5_0  
numpy                     1.24.3           py38he696674_0  
numpy-base                1.24.3           py38h9cd3388_0  
opencv-python             4.9.0.80                 pypi_0    pypi
opendatalab               0.0.10                   pypi_0    pypi
openjpeg                  2.4.0                h66ea3da_0  
openmim                   0.3.9                    pypi_0    pypi
openssl                   3.0.13               hca72f7f_0  
openxlab                  0.0.34                   pypi_0    pypi
ordered-set               4.1.0                    pypi_0    pypi
oss2                      2.17.0                   pypi_0    pypi
packaging                 23.2                     pypi_0    pypi
pandas                    2.0.3                    pypi_0    pypi
pillow                    10.2.0           py38h6c40b1e_0  
pip                       23.3.1           py38hecd8cb5_0  
platformdirs              4.2.0                    pypi_0    pypi
pluggy                    1.4.0                    pypi_0    pypi
pycocotools               2.0.7                    pypi_0    pypi
pycparser                 2.21                     pypi_0    pypi
pycryptodome              3.20.0                   pypi_0    pypi
pygments                  2.17.2                   pypi_0    pypi
pyparsing                 3.1.1                    pypi_0    pypi
pysocks                   1.7.1                    py38_1  
pytest                    8.0.0                    pypi_0    pypi
python                    3.8.18               h5ee71fb_0  
python-dateutil           2.8.2                    pypi_0    pypi
pytorch                   2.2.0                   py3.8_0    pytorch
pytorch-mutex             1.0                         cpu    pytorch
pytz                      2023.4                   pypi_0    pypi
pyyaml                    6.0.1            py38h6c40b1e_0  
readline                  8.2                  hca72f7f_0  
requests                  2.28.2                   pypi_0    pypi
rich                      13.4.2                   pypi_0    pypi
scipy                     1.10.1                   pypi_0    pypi
setuptools                60.2.0                   pypi_0    pypi
shapely                   2.0.2                    pypi_0    pypi
six                       1.16.0             pyhd3eb1b0_1  
sqlite                    3.41.2               h6c40b1e_0  
sympy                     1.12             py38hecd8cb5_0  
tabulate                  0.9.0                    pypi_0    pypi
termcolor                 2.4.0                    pypi_0    pypi
terminaltables            3.1.10                   pypi_0    pypi
tk                        8.6.12               h5d9f67b_0  
tomli                     2.0.1                    pypi_0    pypi
torchvision               0.17.0                 py38_cpu    pytorch
tqdm                      4.65.2                   pypi_0    pypi
typing_extensions         4.9.0            py38hecd8cb5_1  
tzdata                    2024.1                   pypi_0    pypi
urllib3                   1.26.18                  pypi_0    pypi
wheel                     0.41.2           py38hecd8cb5_0  
xz                        5.4.5                h6c40b1e_0  
yaml                      0.2.5                haf1e3a3_0  
yapf                      0.40.2                   pypi_0    pypi
zipp                      3.17.0                   pypi_0    pypi
zlib                      1.2.13               h4dc903c_0  
zstd                      1.5.5                hc035e20_0  

Also I already tried every possible solution from this issue #3271 . I cant downgrade to mmcv-full, since mmdet 3.0.0 is not working with mmcv-full 1.7.X.

I also tried this with several python versions.. Any suggestions whats wrong here?

Edit: When doing the same steps on a linux machine everything works fine...

Leeyuucheer commented 8 months ago

Try using pytorch 1.X version

V1nk4n commented 7 months ago

I have the same question, have you solved this problem?

vetsasai commented 7 months ago

Try installing mmcv with MCV_WITH_OPS=1, FORCE_CUDA=1 and install mmcv from source. That resolved my problem as it generated mmcv._ext file....

ruckc commented 7 months ago

@vetsasai - ideally shouldn't have to do that. Also, pip keeps installing the pypi version and overwriting the built from source version.

lmw0320 commented 5 months ago

I want to install the mmcv with cpu version. How can I install it on an edge device not from Nvidia.. I try several time for installation, but always got the error no module named: mmcv._ext。。Hope someone can help to give usefule advice, thanks in advance. method 1:
git clone -b v1.7.1 https://github.com/open-mmlab/mmcv.git cd mmcv && pip install -r requirements/optional.txt MMCV_WITH_OPS=1 pip install -e .

install successfully, but got error when testing: no module named: mmcv._ext

method 2: pip install -U openmim mim install mmcv found installation error。。。

method 3: pip install torch==2.1.0 MMCV_WITH_OPS=1 pip install git+https://github.com/open-mmlab/mmcv -v

install successfully, but got error when testing: no module named: mmcv._ext

henryrobbins commented 2 weeks ago

I managed to resolve the mmcv._ext issue when running the test installation script: python demo/image_demo.py demo/demo.jpg rtmdet_tiny_8xb32-300e_coco.py --weights rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth --device cpu!

I have an M3 Pro and a Python 3.11 miniconda installation. I created a virtual environment with python -m venv venv. Then, I installed the following packages:

pip install torch==2.1.2
pip install torchvision==0.16.2
pip install numpy==1.26.4
pip install openmim
mim install mmengine
mim install "mmcv==2.1.0"
mim install mmdet

I found it's important to use mim and not pip when installing the openmmlab packages.