TannerGilbert / Object-Detection-and-Image-Segmentation-with-Detectron2

Object Detection and Image Segmentation with Detectron2
https://gilberttanner.com/blog/detectron-2-object-detection-with-pytorch
MIT License
153 stars 40 forks source link

Error while inferences from the trained detectron2 model on image #9

Closed letdivedeep closed 2 years ago

letdivedeep commented 2 years ago

Hi @TannerGilbert, Thanks for the wonderful repos and awesome blogs

I have used a mocov2 pre-trained model and train on the pascal voc 2012 dataset as listed here : https://github.com/facebookresearch/moco/tree/master/detection

Model training is completed successfully and generates model_final.pth in the output dir.

But when i try to infer this model on a single image using the following code

from detectron2.utils.logger import setup_logger
setup_logger()
import cv2
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog

config_file_path = "output/config.yml"
weights_path = "output/model_final.pth"
image_path = "sample_images/test.jpg"

### Read the image
image = cv2.imread(image_path)

### Load the config
cfg = get_cfg()
cfg.merge_from_file(config_file_path)
### Load weights
cfg.MODEL.WEIGHTS = weights_path
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.8

### Create predictor (model for inference)
predictor = DefaultPredictor(cfg)

### Infer image 
outputs = predictor(image)
MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).thing_classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]

v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
cv2.imshow('',v.get_image()[:, :, ::-1])
cv2.waitKey(0)

Give the following error at creating the predictor as :

KeyError                                  Traceback (most recent call last)
<ipython-input-25-117421ecd992> in <module>
      1 cfg.MODEL.WEIGHTS = weights_path
      2 cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
----> 3 predictor = DefaultPredictor(cfg)

~/livesense/Detectron2/detectron2/detectron2/engine/defaults.py in __init__(self, cfg)
    279     def __init__(self, cfg):
    280         self.cfg = cfg.clone()  # cfg can be modified by model
--> 281         self.model = build_model(self.cfg)
    282         self.model.eval()
    283         if len(cfg.DATASETS.TEST):

~/livesense/Detectron2/detectron2/detectron2/modeling/meta_arch/build.py in build_model(cfg)
     20     """
     21     meta_arch = cfg.MODEL.META_ARCHITECTURE
---> 22     model = META_ARCH_REGISTRY.get(meta_arch)(cfg)
     23     model.to(torch.device(cfg.MODEL.DEVICE))
     24     _log_api_usage("modeling.meta_arch." + meta_arch)

~/livesense/Detectron2/detectron2/detectron2/config/config.py in wrapped(self, *args, **kwargs)
    187 
    188             if _called_with_cfg(*args, **kwargs):
--> 189                 explicit_args = _get_args_from_config(from_config_func, *args, **kwargs)
    190                 init_func(self, **explicit_args)
    191             else:

~/livesense/Detectron2/detectron2/detectron2/config/config.py in _get_args_from_config(from_config_func, *args, **kwargs)
    243             if name not in supported_arg_names:
    244                 extra_kwargs[name] = kwargs.pop(name)
--> 245         ret = from_config_func(*args, **kwargs)
    246         # forward the other arguments to __init__
    247         ret.update(extra_kwargs)

~/livesense/Detectron2/detectron2/detectron2/modeling/meta_arch/rcnn.py in from_config(cls, cfg)
     74             "backbone": backbone,
     75             "proposal_generator": build_proposal_generator(cfg, backbone.output_shape()),
---> 76             "roi_heads": build_roi_heads(cfg, backbone.output_shape()),
     77             "input_format": cfg.INPUT.FORMAT,
     78             "vis_period": cfg.VIS_PERIOD,

~/livesense/Detectron2/detectron2/detectron2/modeling/roi_heads/roi_heads.py in build_roi_heads(cfg, input_shape)
     41     """
     42     name = cfg.MODEL.ROI_HEADS.NAME
---> 43     return ROI_HEADS_REGISTRY.get(name)(cfg, input_shape)
     44 
     45 

~/anaconda3/envs/detectron_env/lib/python3.8/site-packages/fvcore/common/registry.py in get(self, name)
     69         ret = self._obj_map.get(name)
     70         if ret is None:
---> 71             raise KeyError(
     72                 "No object named '{}' found in '{}' registry!".format(name, self._name)
     73             )

KeyError: "No object named 'Res5ROIHeadsExtraNorm' found in 'ROI_HEADS' registry!"

I even checked is the key exits in config and it exits

Screenshot 2021-09-14 at 11 53 17 PM

Can you help me on this issue, have attached the model and the config file at this Gdrive link

TannerGilbert commented 2 years ago

Hello @letdivedeep,

sorry for the late reply. I'm currently quite busy and, therefore, slow to respond. I'll try this out as soon as possible. Also, it might be a good idea to create an issue on the Detectron2 repository so that you can get a quicker response (please link the issue here if you decide on creating one).

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days