在 detectron2 中有 class 个 ID 而不是 class 个名称

In detectron2 there are class IDs instead of class names

I finished training model for instance segmentation in detectron2 when I test images in training files there is no problem class names(apple,banana,orange) are written on the image but I downloaded some fruit images from the internet and class names are not written on the photos. There are class ID's.

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)

总而言之,我想用我现在上传的模型来测试我的模型,我不想在图像上使用 class id。我想要 class 个名字,比如橙子、香蕉、苹果

您可以通过填充包含映射的 metadata kwarg 来获取标签。

v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TEST[0]), scale=0.5, instance_mode = ColorMode.SEGMENTATION)

在搜索了一个简单的解决方案之后,我想到了这个,这是最简单的 IMO。

创建新元数据class

Class Metadata:
    def get(self, _):
        return ['apple','banana','orange','etc'] #your class labels

然后,在 Visualizer 行中提供元数据

v = Visualizer(im[:, :, ::-1], Metadata, scale=0.5, instance_mode = ColorMode.SEGMENTATION)

否则,您需要使用(虚拟)数据或什至 1 张带有注释的图像注册模型,然后加载其元数据。如果您仅在推理代码中需要它,我发现在现阶段这有点无关紧要。