Skip to content

Instantly share code, notes, and snippets.

@JosephKJ
Created December 24, 2019 14:15
Show Gist options
  • Save JosephKJ/e523f77af6c5768538eda0c7e2af375a to your computer and use it in GitHub Desktop.
Save JosephKJ/e523f77af6c5768538eda0c7e2af375a to your computer and use it in GitHub Desktop.
Visualize models trained with Detectron2
import cv2
from detectron2.utils.logger import setup_logger
setup_logger()
from detectron2.config import get_cfg
from detectron2.engine import DefaultPredictor
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
# Get image
im = cv2.imread("000002.jpg")
# Get the configuration ready
cfg = get_cfg()
cfg.merge_from_file("configs/PascalVOC-Detection/faster_rcnn_R_50_C4.yaml")
cfg.MODEL.WEIGHTS = "model_final.pth"
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
predictor = DefaultPredictor(cfg)
outputs = predictor(im)
v = Visualizer(im[:,:,::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
v = v.draw_instance_predictions(outputs['instances'].to('cpu'))
img = v.get_image()[:, :, ::-1]
cv2.imwrite('output.jpg', img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment