Open shashimalcse opened 3 years ago
Any solution for this problem? Facing the same issue when doing inference with a model with additional ROI Heads.
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.
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)
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)