detectron2: ImportError: cannot import name '_C' from 'detectron2'

I had no problem using Densepose. However when I was trying to use PointRend I had this issue. I tried reinstalling but the same thing happened

If you do not know the root cause of the problem / bug, and wish someone to help you, please post according to this template:

Instructions To Reproduce the Issue:

  1. what code you wrote
# You may need to restart your runtime prior to this, to let your installation take effect
# Some basic setup:
# Setup detectron2 logger
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()

# import some common libraries
import numpy as np
import cv2
import torch


# import some common detectron2 utilities
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer, ColorMode
from detectron2.data import MetadataCatalog
coco_metadata = MetadataCatalog.get("coco_2017_val")

# import PointRend project
import sys; sys.path.insert(1, "detectron2_repo/projects/PointRend")
import point_rend

im = cv2.imread("frank_front.jpg")
cv2.imshow(im)
  1. what exact command you run: I ran the code
  2. what you observed (including the full logs):
Traceback (most recent call last):
  File "/home/elysium/detectron2-master/projects/practice.py", line 15, in <module>
    from detectron2 import model_zoo
  File "/home/elysium/detectron2-master/detectron2/model_zoo/__init__.py", line 7, in <module>
    from .model_zoo import get, get_config_file, get_checkpoint_url
  File "/home/elysium/detectron2-master/detectron2/model_zoo/model_zoo.py", line 8, in <module>
    from detectron2.modeling import build_model
  File "/home/elysium/detectron2-master/detectron2/modeling/__init__.py", line 4, in <module>
    from detectron2.layers import ShapeSpec
  File "/home/elysium/detectron2-master/detectron2/layers/__init__.py", line 3, in <module>
    from .deform_conv import DeformConv, ModulatedDeformConv
  File "/home/elysium/detectron2-master/detectron2/layers/deform_conv.py", line 10, in <module>
    from detectron2 import _C
ImportError: cannot import name '_C' from 'detectron2' (/home/elysium/detectron2-master/detectron2/__init__.py)

  1. please also simplify the steps as much as possible so they do not require additional resources to run, such as a private dataset.

Expected behavior:

I expected no error

Environment:

Run python -m detectron2.utils.collect_env in the environment where you observerd the issue, and paste the output.


sys.platform linux Python 3.7.6 (default, Jan 8 2020, 19:59:22) [GCC 7.3.0] numpy 1.18.1 detectron2._C failed to import DETECTRON2_ENV_MODULE <not set> PyTorch 1.4.0+cu100 @/home/elysium/anaconda3/envs/detectron2-master/lib/python3.7/site-packages/torch PyTorch debug build False CUDA available True GPU 0 GeForce GTX 1070 Ti CUDA_HOME /usr NVCC Cuda compilation tools, release 7.5, V7.5.17 Pillow 7.0.0 torchvision 0.5.0+cu100 @/home/elysium/anaconda3/envs/detectron2-master/lib/python3.7/site-packages/torchvision torchvision arch flags sm_35, sm_50, sm_60, sm_70, sm_75 cv2 4.2.0


PyTorch built with:

  • GCC 7.3
  • Intel® Math Kernel Library Version 2020.0.0 Product Build 20191122 for Intel® 64 architecture applications
  • Intel® MKL-DNN v0.21.1 (Git Hash 7d2fd500bc78936d1d648ca713b901012f470dbc)
  • OpenMP 201511 (a.k.a. OpenMP 4.5)
  • NNPACK is enabled
  • CUDA Runtime 10.0
  • NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_37,code=compute_37
  • CuDNN 7.6.3
  • Magma 2.5.1
  • Build settings: BLAS=MKL, BUILD_NAMEDTENSOR=OFF, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -fopenmp -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -O2 -fPIC -Wno-narrowing -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Wno-stringop-overflow, DISABLE_NUMA=1, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON, USE_STATIC_DISPATCH=OFF,

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (4 by maintainers)

Most upvoted comments

I got the exact same problem.

Just an action of rename will help our soul.

Suppose we are in experiment folder, after git clone detectron2, we have:

experiment
|— detectron2
|—|— configs
|—|— datasets
|—|— detectron2

The why: Suppose we want to run /experiment/detectron2/demo/demo.py, The error of cannot import name '_C' will appear. As demo.py try to import detectron2 from the folder of detectron2/detectron2, Instead of importing from this folder instead of our Detectron2 installation somewhere else.

Take a look at detectron2/detectron2, there is a __init.py__, which causes this mistake.

Solution: Rename detectron2/detectron2 to example detectron2/detectron2_you_make_me_trouble

Then you probably did not install detectron2. See https://github.com/facebookresearch/detectron2/blob/master/INSTALL.md

If you install detectron2 following INSTALL.md, you cannot import detectron2 at the root of the github repo because it will not find the one you installed, but will find the one in the repo (which has not been compiled). Try a different directory.

i’m facing this issue on google colab. From last 2 months i’m running the same code but today i start facing this issue. i install dependencies like this way

!pip install -U torch==1.5 torchvision==0.6 -f https://download.pytorch.org/whl/cu101/torch_stable.html !pip install cython pyyaml==5.1 !pip install -U ‘git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI’ import torch, torchvision print(torch.version, torch.cuda.is_available()) !gcc --version

install detectron2: !pip install detectron2==0.1.3 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.5/index.html

and then i just import the libraries

import detectron2 from detectron2.utils.logger import setup_logger setup_logger() import numpy as np import cv2 import random from google.colab.patches import cv2_imshow

from detectron2 import model_zoo from detectron2.engine import DefaultPredictor from detectron2.config import get_cfg from detectron2.utils.visualizer import Visualizer from detectron2.data import MetadataCatalog from detectron2.data.catalog import DatasetCatalog

and i got the following error

ImportError: /usr/local/lib/python3.6/dist-packages/detectron2/_C.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZN6caffe28TypeMeta21_typeMetaDataInstanceISt7complexIdEEEPKNS_6detail12TypeMetaDataEv

Hi,

I had the same problem on Colab using torch 1.9 and cuda 11. Re-installing with cuda 10.2 solved it.

I got the exact same problem.

Just an action of rename will help our soul.

Suppose we are in experiment folder, after git clone detectron2, we have:

experiment |— detectron2 |—|— configs |—|— datasets |—|— detectron2

The why: Suppose we want to run /experiment/detectron2/demo/demo.py, The error of cannot import name '_C' will appear. As demo.py try to import detectron2 from the folder of detectron2/detectron2, Instead of importing from this folder instead of our Detectron2 installation somewhere else.

Take a look at detectron2/detectron2, there is a __init.py__, which causes this mistake.

Solution: Rename detectron2/detectron2 to example detectron2/detectron2_you_make_me_trouble

Nice,I follow this method and have solved the TensorMask “import _C error”. I just rename the tensormask folder to new_name, such as “tensor_mask_src”