I'm using my model like this:
Step 1: Loading weights
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.MODEL.WEIGHTS = "data/my_model_final.pth"
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 42
predictor = DefaultPredictor(cfg)
outputs = predictor(im)
Step 2: Get metadata
from detectron2.data.datasets import register_coco_instances
register_coco_instances("my_dataset", {}, "data/drive/train_coco/annotations.json", "data/drive/train_coco")
my_metadata = MetadataCatalog.get("my_dataset")
Step 3: Predict
from detectron2.utils.visualizer import ColorMode
v = Visualizer(im[:, :, ::-1], metadata=my_metadata, scale=1.2, instance_mode=ColorMode.IMAGE_BW)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
plt.imshow(v.get_image())
plt.show()
But in step 2, I have to register the entire dataset only to get the metadata. Is there anyway I can save metadata after training similar to saving 'model.pth'
Thank you
You can specify the metadata without registering the dataset, see https://detectron2.readthedocs.io/tutorials/datasets.html#metadata-for-datasets
For anyone having same problems, I have found a way to specify metadata without registering dataset:
from detectron2.data.catalog import Metadata
my_metadata = Metadata()
my_metadata.set(thing_classes = ['dog', 'cat'])
Most helpful comment
For anyone having same problems, I have found a way to specify metadata without registering dataset: