Detectron2: run a pretrained model using cpu only

Created on 22 Nov 2019  ยท  4Comments  ยท  Source: facebookresearch/detectron2

โ“ Questions and Help

How do I use Detectron2 using cpu only in code

NOTE:

I have created a docker image. I am then calling a python script from it:
import torch, torchvision
torch.__version__

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 random

import some common detectron2 utilities

from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
im = cv2.imread("input.jpg")

cfg = get_cfg()
cfg.merge_from_file("/detectron2_repo/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model

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 = "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl"
predictor = DefaultPredictor(cfg)
outputs = predictor(im)

look at the outputs. See https://detectron2.readthedocs.io/tutorials/models.html#model-output-format for specification

outputs["instances"].pred_classes
outputs["instances"].pred_boxes

We can use Visualizer to draw the predictions on the image.

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

The error i am getting is:
Failed to load OpenCL runtime
WARNING [11/22 12:20:51 d2.config.compat]: Config '/detectron2_repo/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml' has no VERSION. Assuming it to be compatible with latest v2.
Traceback (most recent call last):
File "/detectron2_repo/detectron2/image_recognition.py", line 34, in
predictor = DefaultPredictor(cfg)
File "/detectron2_repo/detectron2/engine/defaults.py", line 149, in __init__
self.model = build_model(self.cfg)
File "/detectron2_repo/detectron2/modeling/meta_arch/build.py", line 18, in build_model
return META_ARCH_REGISTRY.get(meta_arch)(cfg)
File "/detectron2_repo/detectron2/modeling/meta_arch/rcnn.py", line 37, in __init__
pixel_mean = torch.Tensor(cfg.MODEL.PIXEL_MEAN).to(self.device).view(num_channels, 1, 1)
File "/usr/local/lib/python3.6/dist-packages/torch/cuda/__init__.py", line 192, in _lazy_init
_check_driver()
File "/usr/local/lib/python3.6/dist-packages/torch/cuda/__init__.py", line 102, in _check_driver
http://www.nvidia.com/Download/index.aspx""")
AssertionError:
Found no NVIDIA driver on your system. Please check that you
have an NVIDIA GPU and installed a driver from
http://www.nvidia.com/Download/index.aspx

Most helpful comment

You can set cfg.MODEL.DEVICE='cpu'. Also mentioned in https://github.com/facebookresearch/detectron2/blob/master/GETTING_STARTED.md#inference-with-pre-trained-models

All 4 comments

You can set cfg.MODEL.DEVICE='cpu'. Also mentioned in https://github.com/facebookresearch/detectron2/blob/master/GETTING_STARTED.md#inference-with-pre-trained-models

It worked just fine!
Thank you so much

Any idea how can I limit the number of cores?

@DeepLakhani99 can you please share docker image how you build it.
Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Cold-Winter picture Cold-Winter  ยท  3Comments

jinfagang picture jinfagang  ยท  3Comments

limsijie93 picture limsijie93  ยท  3Comments

AntonBaumannDE picture AntonBaumannDE  ยท  3Comments

Ormagardskvaedi picture Ormagardskvaedi  ยท  4Comments