'In detectron2 there are class IDs instead of class names
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.DATASETS.TEST = ("fruit_test", )
predictor = DefaultPredictor(cfg)
image_path = "/content/detectron2_custom_dataset/testimages/test2.jpg"
def on_image(image_path,predictor):
im = cv2.imread(image_path)
outputs = predictor(im)
v = Visualizer(im[:,:,::-1], metadata = {}, scale=0.5, instance_mode = ColorMode.SEGMENTATION)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
plt.figure(figsize=(14,10))
plt.imshow(v.get_image())
plt.show()
on_image(image_path, predictor)
In conclusion, I want test my model with I uploaded now and I don't want to have class id on the images. I want class names like orange,banana,apple on it
Solution 1:[1]
After searching for an easy solution, I came with this, which is the easiest IMO.
Create a new Metadata class
Class Metadata:
def get(self, _):
return ['apple','banana','orange','etc'] #your class labels
then, provide Metadata in the Visualizer line
v = Visualizer(im[:, :, ::-1], Metadata, scale=0.5, instance_mode = ColorMode.SEGMENTATION)
Otherwise, you're required to register a model with (a dummy) data or even 1 image with annotations, then load its metadata. Which I find a bit irrelevant at this stage, if you need it for the inference code only.
Solution 2:[2]
You can get the labels by populating the metadata
kwarg, which contains the mapping.
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TEST[0]), scale=0.5, instance_mode = ColorMode.SEGMENTATION)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Y Kesem |
Solution 2 | erip |