【mmcv报错】ModuleNotFoundError: No module named ‘mmcv.runner

Running a code requires mmcv and mmseg.
There are two lines of code:

from mmcv.runner import load_checkpoint
from mmseg.utils import get_root_logger

I first installed mmcv and mmseg according to the official recommended installation method.

pip install -U openmim
mim install mmcv

It will automatically install mmengine for you.
My cuda is 11.6 and torch is 2.0.1.
The mmcv he installed for me is 2.1.0, mmengine is 0.9.1 , and then the mmseg version installed
by pip install mmsegmentation is 1.2.1.

After the installation, a problem occurred:
mmcv.runner was marked red and reported an error, saying that there is no runner under mmcv
ModuleNotFoundError: No module named 'mmcv.runner
. I searched online and found a blog saying that mmcv cannot be installed
and that mmcv-full must be installed. Then I uninstalled mmcv and installed mmcv-full (I was deceived) )
First, I encountered a problem. The installation of mmcv-full was stuck
. buliding wheels for mmcv
I searched online. Many
Insert image description here
solutions to this problem said that it was inconsistent with the cuda version and that the corresponding version should be installed.
I tried multiple versions, but none worked and could not be installed.
Finally, I even downloaded the offline whl package from this website. https://download.openmmlab.com/mmcv/dist/cu102/torch1.10.0/index.html
The final installation was successful, but it caused more problems.
找不到libcudaert.so.10.2
No module named ‘mmcv._ext‘
It took more than an hour to fiddle with. Anyway, all kinds of problems were probably I was saying that the version does not match CUDA and torch
(in fact, the mmcv automatically installed for you is consistent with your machine version, but the automatically installed mmcv-full cannot be used, and the manually installed version does not match,,,,)
Finally, I changed my mind. Solution:
Problem with these two lines of code:

from mmcv.runner import load_checkpoint
from mmseg.utils import get_root_logger

First: I couldn’t find mmcv.runner.
I looked at the source code and found that it was a version upgrade problem. The structure of the higher version of mmcv I used has changed.
The runner is under mmengine, so I modified it as
from mmengine.runner import load_checkpoint
follows. Second error:
Under mmseg.utils I can't find get_root_logger.
I think it's a version problem. The structure of higher versions has changed.
But I searched for get_root_logger under the source code of mmseg, and couldn't find it
. There is no such thing at all. Then I searched under the source code of mmcv and mmengine, and there was no such thing. stuff

emm, then looked at the code, this thing is used to record logs

from mmseg.utils import get_root_logger
logger = get_root_logger()

I looked at how the logs are recorded in mmcv.

import logging
logger = logging.getLogger()

Since this cannot find get_root_logger,
just comment this sentence
and change it to this

import logging
logger = logging.getLogger()

After running it, I found no problem and it was solved.

Summary:
This solution is actually not good. It is better to run other people's code without touching other people's code and configure the corresponding environment to run it. Because if you change one place, you don't know whether it will affect other people.
But this is what I said. In a special case, this code did not inform the version of the required library. I also installed various versions of mmcv and mmcv-full. Finally, I couldn’t figure it out, so I had to change her code.
In addition, the compatibility of this thing is really poor. , when you upgrade the version, you directly delete all the original functions and change the names. Then all the previous code is gone. How do you maintain it and upgrade it?

Guess you like

Origin blog.csdn.net/holly_Z_P_F/article/details/134251912