0


Labelme加载AI(Segment-Anything)模型进行图像标注

  labelme是使用python写的基于QT的跨平台图像标注工具,可用来标注分类、检测、分割、关键点等常见的视觉任务,支持VOC格式和COCO等的导出,代码简单易读,是非常利用上手的良心工具。
在这里插入图片描述
第一步:
  下载源码进行安装。

git clone https://github.com/wkentaro/labelme.git
cd labelme
pip install-e.

第二步:
   找到源码所在路径进行修改。
  (1)打开labelme/labelme/ai/init.py,源码如下:

MODELS =[
    Model(name="Segment-Anything (speed)",
        encoder_weight=Weight(url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_b_01ec64.quantized.encoder.onnx",  # NOQAmd5="80fd8d0ab6c6ae8cb7b3bd5f368a752c",
        ),
        decoder_weight=Weight(url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_b_01ec64.quantized.decoder.onnx",  # NOQAmd5="4253558be238c15fc265a7a876aaec82",
        ),
    ),
    Model(name="Segment-Anything (balanced)",
        encoder_weight=Weight(url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_l_0b3195.quantized.encoder.onnx",  # NOQAmd5="080004dc9992724d360a49399d1ee24b",
        ),
        decoder_weight=Weight(url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_l_0b3195.quantized.decoder.onnx",  # NOQAmd5="851b7faac91e8e23940ee1294231d5c7",
        ),
    ),
    Model(name="Segment-Anything (accuracy)",
        encoder_weight=Weight(url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_h_4b8939.quantized.encoder.onnx",  # NOQAmd5="958b5710d25b198d765fb6b94798f49e",
        ),
        decoder_weight=Weight(url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_h_4b8939.quantized.decoder.onnx",  # NOQAmd5="a997a408347aa081b17a3ffff9f42a80",
        ),
    ),
]

  (2)在labelme/labelme/文件夹下自建一个文件夹model_file。
  (3)依次输入以下几个网址下载onnx到model_file文件目录。

https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_b_01ec64.quantized.encoder.onnx
https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_b_01ec64.quantized.decoder.onnx

https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_l_0b3195.quantized.encoder.onnx
https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_l_0b3195.quantized.decoder.onnx

https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_h_4b8939.quantized.encoder.onnx
https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_h_4b8939.quantized.decoder.onnx

在这里插入图片描述
  (4)修改labelme/labelme/ai/init.py,代码如下:

import collections

from .models.segment_anything import SegmentAnythingModel  # NOQA

Model = collections.namedtuple("Model", ["name", "encoder_weight", "decoder_weight"])

Weight = collections.namedtuple("Weight", ["url", "md5"])# MODELS = [#     Model(#         name="Segment-Anything (speed)",#         encoder_weight=Weight(#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_b_01ec64.quantized.encoder.onnx",  # NOQA#             md5="80fd8d0ab6c6ae8cb7b3bd5f368a752c",#         ),#         decoder_weight=Weight(#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_b_01ec64.quantized.decoder.onnx",  # NOQA#             md5="4253558be238c15fc265a7a876aaec82",#         ),#     ),#     Model(#         name="Segment-Anything (balanced)",#         encoder_weight=Weight(#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_l_0b3195.quantized.encoder.onnx",  # NOQA#             md5="080004dc9992724d360a49399d1ee24b",#         ),#         decoder_weight=Weight(#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_l_0b3195.quantized.decoder.onnx",  # NOQA#             md5="851b7faac91e8e23940ee1294231d5c7",#         ),#     ),#     Model(#         name="Segment-Anything (accuracy)",#         encoder_weight=Weight(#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_h_4b8939.quantized.encoder.onnx",  # NOQA#             md5="958b5710d25b198d765fb6b94798f49e",#         ),#         decoder_weight=Weight(#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_h_4b8939.quantized.decoder.onnx",  # NOQA#             md5="a997a408347aa081b17a3ffff9f42a80",#         ),#     ),# ]

MODELS =[
    Model(name="Segment-Anything (speed)",
        encoder_weight=Weight(url="E:\labelme\labelme\model_file\sam_vit_b_01ec64.quantized.encoder.onnx",  # NOQAmd5="80fd8d0ab6c6ae8cb7b3bd5f368a752c",
        ),
        decoder_weight=Weight(url="E:\labelme\labelme\model_file\sam_vit_b_01ec64.quantized.decoder.onnx",  # NOQAmd5="4253558be238c15fc265a7a876aaec82",
        ),
    ),
    Model(name="Segment-Anything (balanced)",
        encoder_weight=Weight(url="E:\labelme\labelme\model_file\sam_vit_l_0b3195.quantized.encoder.onnx",  # NOQAmd5="080004dc9992724d360a49399d1ee24b",
        ),
        decoder_weight=Weight(url="E:\labelme\labelme\model_file\sam_vit_l_0b3195.quantized.decoder.onnx",  # NOQAmd5="851b7faac91e8e23940ee1294231d5c7",
        ),
    ),
    Model(name="Segment-Anything (accuracy)",
        encoder_weight=Weight(url="E:\labelme\labelme\model_file\sam_vit_h_4b8939.quantized.encoder.onnx",  # NOQAmd5="958b5710d25b198d765fb6b94798f49e",
        ),
        decoder_weight=Weight(url="E:\labelme\labelme\model_file\sam_vit_h_4b8939.quantized.decoder.onnx",  # NOQAmd5="a997a408347aa081b17a3ffff9f42a80",
        ),
    ),
]

  (5)修改labelme/labelme/widgets/canvas.py,代码如下:

    def initializeAiModel(self, name):
        if name not in[model.name formodelin labelme.ai.MODELS]:
            raise ValueError("Unsupported ai model: %s" % name)
        model =[model formodelin labelme.ai.MODELS if model.name == name][0]if self._ai_model is not None and self._ai_model.name == model.name:
            logger.debug("AI model is already initialized: %r" % model.name)
        else:
            logger.debug("Initializing AI model: %r" % model.name)
            self._ai_model = labelme.ai.SegmentAnythingModel(name=model.name,
                # encoder_path=gdown.cached_download(#     url=model.encoder_weight.url,#     md5=model.encoder_weight.md5,# ),# decoder_path=gdown.cached_download(#     url=model.decoder_weight.url,#     md5=model.decoder_weight.md5,# ),encoder_path=model.encoder_weight.url,
                decoder_path=model.decoder_weight.url,
            )

        self._ai_model.set_image(image=labelme.utils.img_qt_to_arr(self.pixmap.toImage()))

第三步:
  启动labelme

cd labelme
labelme

在这里插入图片描述


本文转载自: https://blog.csdn.net/qq_50993557/article/details/134616280
版权归原作者 Make_magic 所有, 如有侵权,请联系我们删除。

“Labelme加载AI(Segment-Anything)模型进行图像标注”的评论:

还没有评论