facebookresearch / detectron2

Detectron2 is a platform for object detection, segmentation and other visual recognition tasks.
https://detectron2.readthedocs.io/en/latest/
Apache License 2.0
29.98k stars 7.41k forks source link

KeyError: "No object named 'DensePoseROIHeads' found in 'ROI_HEADS' registry!" #3155

Open shashimalcse opened 3 years ago

shashimalcse commented 3 years ago

from detectron2.engine import DefaultPredictor from detectron2.config import get_cfg

cfg = get_cfg() add_densepose_config(cfg) cfg.merge_from_file("/content/detectron2_repo/projects/DensePose/configs/densepose_rcnn_R_50_FPN_s1x.yaml")

Find a model from detectron2's model zoo. You can either use the https://dl.fbaipublicfiles.... url, or use the following shorthand

cfg.MODEL.WEIGHTS = "https://dl.fbaipublicfiles.com/densepose/densepose_rcnn_R_50_FPN_s1x/143908701/model_final_dd99d2.pkl" predictor = DefaultPredictor(cfg) outputs = predictor(im)

Logs

KeyError Traceback (most recent call last)

in () 7 # Find a model from detectron2's model zoo. You can either use the https://dl.fbaipublicfiles.... url, or use the following shorthand 8 cfg.MODEL.WEIGHTS = "https://dl.fbaipublicfiles.com/densepose/densepose_rcnn_R_50_FPN_s1x/143908701/model_final_dd99d2.pkl" ----> 9 predictor = DefaultPredictor(cfg) 10 outputs = predictor(im) 6 frames /usr/local/lib/python3.7/dist-packages/fvcore/common/registry.py in get(self, name) 70 if ret is None: 71 raise KeyError( ---> 72 "No object named '{}' found in '{}' registry!".format(name, self._name) 73 ) 74 return ret KeyError: "No object named 'DensePoseROIHeads' found in 'ROI_HEADS' registry!" ## Environment: sys.platform linux Python 3.7.10 (default, May 3 2021, 02:48:31) [GCC 7.5.0] numpy 1.19.5 detectron2 0.4 @/usr/local/lib/python3.7/dist-packages/detectron2 Compiler GCC 7.3 CUDA compiler CUDA 10.1 detectron2 arch flags 3.7, 5.0, 5.2, 6.0, 6.1, 7.0, 7.5 DETECTRON2_ENV_MODULE PyTorch 1.8.1+cu101 @/usr/local/lib/python3.7/dist-packages/torch PyTorch debug build False GPU available True GPU 0 Tesla T4 (arch=7.5) CUDA_HOME /usr/local/cuda Pillow 7.1.2 torchvision 0.9.1+cu101 @/usr/local/lib/python3.7/dist-packages/torchvision torchvision arch flags 3.5, 5.0, 6.0, 7.0, 7.5 fvcore 0.1.3.post20210317 cv2 4.1.2 ---------------------- --------------------------------------------------------------- PyTorch built with: - GCC 7.3 - C++ Version: 201402 - Intel(R) Math Kernel Library Version 2020.0.0 Product Build 20191122 for Intel(R) 64 architecture applications - Intel(R) MKL-DNN v1.7.0 (Git Hash 7aed236906b1f7a05c0917e5257a1af05e9ff683) - OpenMP 201511 (a.k.a. OpenMP 4.5) - NNPACK is enabled - CPU capability usage: AVX2 - CUDA Runtime 10.1 - 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_70,code=sm_70 - CuDNN 7.6.3 - Magma 2.5.2 - Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=10.1, CUDNN_VERSION=7.6.3, CXX_COMPILER=/opt/rh/devtoolset-7/root/usr/bin/c++, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_KINETO -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -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-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -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 -Werror=format -Wno-stringop-overflow, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_VERSION=1.8.1, USE_CUDA=ON, USE_CUDNN=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,
DomiSchmauser commented 3 years ago

Any solution for this problem? Facing the same issue when doing inference with a model with additional ROI Heads.

TysonLinger commented 2 years ago

I have a similar environment as OP and encountered a similar error. In my case the error occured because I was picking and choosing parts of the project to use rather than using it as-is. When I had the densepose project directory contained within my project directory, I used the same code as OP and it worked fine for me.

This error occurs because the ROI head needs to be registered with a particular instance of one of Detectron2's registries, in particular the registry instance called ROI_HEADS_REGISTRY. The Registry class is a global key -> object store. ROI_HEADS_REGISTRY is one particular instance of this class. I noticed a few different kinds of registries being used, such as ROI_HEADS_REGISTRY, ROI_DENSEPOSE_HEAD_REGISTRY, DENSEPOSE_PREDICTOR_REGISTRY, and DENSEPOSE_LOSS_REGISTRY. They are all used kind of like globally scoped dicts.

In OP's case, for some reason the code in densepose -> modeling -> roi_heads -> roi_head.py is not being executed. In particular, this part:

@ROI_HEADS_REGISTRY.register()
class DensePoseROIHeads(StandardROIHeads):
    """
    A Standard ROIHeads which contains an addition of DensePose head.
    """

@DomiSchmauser For your case, I think you might be able to solve your problem by importing the ROI_HEADS_REGISTRY instance like so: from detectron2.modeling import ROI_HEADS_REGISTRY and then using its "register" decorator to decorate your custom ROI Head classes and/or functions just like the above DensePoseROIHeads class.

FurkanGozukara commented 9 months ago

I have a similar environment as OP and encountered a similar error. In my case the error occured because I was picking and choosing parts of the project to use rather than using it as-is. When I had the densepose project directory contained within my project directory, I used the same code as OP and it worked fine for me.

This error occurs because the ROI head needs to be registered with a particular instance of one of Detectron2's registries, in particular the registry instance called ROI_HEADS_REGISTRY. The Registry class is a global key -> object store. ROI_HEADS_REGISTRY is one particular instance of this class. I noticed a few different kinds of registries being used, such as ROI_HEADS_REGISTRY, ROI_DENSEPOSE_HEAD_REGISTRY, DENSEPOSE_PREDICTOR_REGISTRY, and DENSEPOSE_LOSS_REGISTRY. They are all used kind of like globally scoped dicts.

In OP's case, for some reason the code in densepose -> modeling -> roi_heads -> roi_head.py is not being executed. In particular, this part:

@ROI_HEADS_REGISTRY.register()
class DensePoseROIHeads(StandardROIHeads):
    """
    A Standard ROIHeads which contains an addition of DensePose head.
    """

@DomiSchmauser For your case, I think you might be able to solve your problem by importing the ROI_HEADS_REGISTRY instance like so: from detectron2.modeling import ROI_HEADS_REGISTRY and then using its "register" decorator to decorate your custom ROI Head classes and/or functions just like the above DensePoseROIHeads class.

can you help me please

below my code and I have same error

error is

Traceback (most recent call last):
  File "G:\magic_animate\compose_densepose\pose_maker.py", line 46, in <module>
    process_video("ex1.mp4", "pose1.mp4", cfg)
  File "G:\magic_animate\compose_densepose\pose_maker.py", line 27, in process_video
    predictor = DefaultPredictor(cfg)
  File "G:\magic_animate\compose_densepose\detectron2\venv\lib\site-packages\detectron2\engine\defaults.py", line 282, in __init__
    self.model = build_model(self.cfg)
  File "G:\magic_animate\compose_densepose\detectron2\venv\lib\site-packages\detectron2\modeling\meta_arch\build.py", line 22, in build_model
    model = META_ARCH_REGISTRY.get(meta_arch)(cfg)
  File "G:\magic_animate\compose_densepose\detectron2\venv\lib\site-packages\detectron2\config\config.py", line 189, in wrapped
    explicit_args = _get_args_from_config(from_config_func, *args, **kwargs)
  File "G:\magic_animate\compose_densepose\detectron2\venv\lib\site-packages\detectron2\config\config.py", line 245, in _get_args_from_config
    ret = from_config_func(*args, **kwargs)
  File "G:\magic_animate\compose_densepose\detectron2\venv\lib\site-packages\detectron2\modeling\meta_arch\rcnn.py", line 77, in from_config
    "roi_heads": build_roi_heads(cfg, backbone.output_shape()),
  File "G:\magic_animate\compose_densepose\detectron2\venv\lib\site-packages\detectron2\modeling\roi_heads\roi_heads.py", line 43, in build_roi_heads
    return ROI_HEADS_REGISTRY.get(name)(cfg, input_shape)
  File "G:\magic_animate\compose_densepose\detectron2\venv\lib\site-packages\fvcore\common\registry.py", line 71, in get
    raise KeyError(
KeyError: "No object named 'DensePoseROIHeads' found in 'ROI_HEADS' registry!"
Press any key to continue . . .

code is

import cv2
import torch
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
from config import add_densepose_config
from detectron2.modeling import ROI_HEADS_REGISTRY

def setup_cfg():
    cfg = get_cfg()
    add_densepose_config(cfg)
    cfg.merge_from_file("densepose_rcnn_R_101_FPN_DL_s1x.yaml")
    cfg.MODEL.WEIGHTS = "model_final_844d15.pkl"
    cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
    return cfg

def process_video(input_video_path, output_video_path, cfg):
    video = cv2.VideoCapture(input_video_path)
    width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = video.get(cv2.CAP_PROP_FPS)

    output_video = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))

    predictor = DefaultPredictor(cfg)

    while True:
        ret, frame = video.read()
        if not ret:
            break

        outputs = predictor(frame)
        v = Visualizer(frame[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
        output = v.draw_instance_predictions(outputs["instances"].to("cpu"))
        output_frame = output.get_image()[:, :, ::-1]

        output_video.write(output_frame)

    video.release()
    output_video.release()

cfg = setup_cfg()

process_video("ex1.mp4", "pose1.mp4", cfg)