Inference on test images #4353
Unanswered
rabiyaabbasi
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi I have used detectron 2 for instance segmentation of my custom data. My model is trained and is saved as "model_final.pth". I have jupyter notebook to perform training. My question is how to reload the model in different notebook and run inference on test images. I have used the following code but its not doing anything except displaying the image without mask, prediction and bounding box. Can anyone tell me what I am doing wrong? When I run the same code in the jupyter notebook where I trained my model, it works perfectly fine but when I run it in different jupyter notebook, its not giving me desired results. Thanks alot.
`%cd detectron2
!python setup.py build develop
from detectron2.engine import DefaultTrainer
from detectron2.config import get_cfg
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
import numpy as np
import os, json, cv2, random
from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog, DatasetCatalog
from detectron2.structures import BoxMode
import torch
import matplotlib.pyplot as plt
%matplotlib inline
def cv2_imshow(im):
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
plt.figure(), plt.imshow(im), plt.axis('off');
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml"))
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
cfg.DATASETS.TEST = ("my_dataset_test", )
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7 # set the testing threshold for this model
predictor = DefaultPredictor(cfg)
test_metadata = MetadataCatalog.get("my_dataset_test")
from detectron2.utils.visualizer import ColorMode
im = cv2.imread("Phase3-3 (18).jpg")
im=cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
outputs = predictor(im)
v = Visualizer(im[:, :, ::-1],
metadata=test_metadata,
scale=0.8
)
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
cv2_imshow(out.get_image()[:, :, ::-1])
`
Beta Was this translation helpful? Give feedback.
All reactions