Labelme加载AI(Segment-Anything)模型进行图像标注
时间:2024-04-13 20:50:38 来源:网络cs 作者:言安琪 栏目:卖家故事 阅读:
阅读本书更多章节>>>>
labelme是使用python写的基于QT的跨平台图像标注工具,可用来标注分类、检测、分割、关键点等常见的视觉任务,支持VOC格式和COCO等的导出,代码简单易读,是非常利用上手的良心工具。
第一步:
下载源码进行安装。
git clone https://github.com/wkentaro/labelme.gitcd labelmepip 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", # 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", ), ),]
(2)在labelme/labelme/文件夹下自建一个文件夹model_file。
(3)依次输入以下几个网址下载onnx到model_file文件目录。
https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_b_01ec64.quantized.encoder.onnxhttps://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_b_01ec64.quantized.decoder.onnxhttps://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_l_0b3195.quantized.encoder.onnxhttps://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_l_0b3195.quantized.decoder.onnxhttps://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_h_4b8939.quantized.encoder.onnxhttps://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_h_4b8939.quantized.decoder.onnx
(4)修改labelme/labelme/ai/init.py,代码如下:
import collectionsfrom .models.segment_anything import SegmentAnythingModel # NOQAModel = 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", # NOQA md5="80fd8d0ab6c6ae8cb7b3bd5f368a752c", ), decoder_weight=Weight( url="E:\labelme\labelme\model_file\sam_vit_b_01ec64.quantized.decoder.onnx", # NOQA md5="4253558be238c15fc265a7a876aaec82", ), ), Model( name="Segment-Anything (balanced)", encoder_weight=Weight( url="E:\labelme\labelme\model_file\sam_vit_l_0b3195.quantized.encoder.onnx", # NOQA md5="080004dc9992724d360a49399d1ee24b", ), decoder_weight=Weight( url="E:\labelme\labelme\model_file\sam_vit_l_0b3195.quantized.decoder.onnx", # NOQA md5="851b7faac91e8e23940ee1294231d5c7", ), ), Model( name="Segment-Anything (accuracy)", encoder_weight=Weight( url="E:\labelme\labelme\model_file\sam_vit_h_4b8939.quantized.encoder.onnx", # NOQA md5="958b5710d25b198d765fb6b94798f49e", ), decoder_weight=Weight( url="E:\labelme\labelme\model_file\sam_vit_h_4b8939.quantized.decoder.onnx", # NOQA md5="a997a408347aa081b17a3ffff9f42a80", ), ),]
(5)修改labelme/labelme/widgets/canvas.py,代码如下:
def initializeAiModel(self, name): if name not in [model.name for model in labelme.ai.MODELS]: raise ValueError("Unsupported ai model: %s" % name) model = [model for model in 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 labelmelabelme
阅读本书更多章节>>>>
本文链接:https://www.kjpai.cn/gushi/2024-04-13/158272.html,文章来源:网络cs,作者:言安琪,版权归作者所有,如需转载请注明来源和作者,否则将追究法律责任!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。