开源预训练框架 MMPRETRAIN现有的推理模型(三)

推理器类型:

(1)ImageClassificationInferencer:对给定图像进行图像分类。
(2)ImageRetrievalInferencer:从给定图像集上的给定图像执行图像到图像检索。
(3)ImageCaptionInferencer:在给定图像上生成标题
(4)VisualQuestionAnsweringInferencer:根据给定的图片回答问题。
(5)VisualGroundingInferencer:从给定图像的描述中找到一个对象。
(6)TextToImageRetrievalInferencer:根据给定图像集的给定描述执行文本到图像检索。
(7)ImageToTextRetrievalInferencer:从给定图像对一系列文本执行图像到文本检索。
(8)NLVRInferencer:对给定的图像对和文本执行自然语言视觉推理。
(9)FeatureExtractor:通过视觉主干从图像文件中提取特征。

列出可用型号

列出 MMPreTrain 中的所有模型。

bash 复制代码
from mmpretrain import list_models
list_models()#目前共有539个模型

可用型号搜索

list_models支持Unix文件名模式匹配,可以使用*** * **来匹配任何字符。

bash 复制代码
from mmpretrain import list_models
list_models("*convnext-b*21k")

获取相应任务的可用模型

您可以使用list_models推理器的方法来获取相应任务的可用模型。

1、Get a model

you can use get_model get the model.

bash 复制代码
from mmpretrain import get_model
model = get_model("convnext-base_in21k-pre_3rdparty_in1k")
model = get_model("convnext-base_in21k-pre_3rdparty_in1k", pretrained=True)
model = get_model("convnext-base_in21k-pre_3rdparty_in1k", pretrained="your_local_checkpoint_path")
model = get_model("convnext-base_in21k-pre_3rdparty_in1k", head=dict(num_classes=10))
model_headless = get_model("resnet18_8xb32_in1k", head=None, neck=None, backbone=dict(out_indices=(1, 2, 3)))

获得的模型是常用的 PyTorch 模块

bash 复制代码
import torch
from mmpretrain import get_model
model = get_model('convnext-base_in21k-pre_3rdparty_in1k', pretrained=True)
x = torch.rand((1, 3, 224, 224))
y = model(x)
print(type(y), y.shape)

2、ImageClassificationInferencer:对给定图像进行图像分类任务可用模型

bash 复制代码
from mmpretrain import ImageClassificationInferencer
ImageClassificationInferencer.list_models()

1、通过inference_model进行任务推理

bash 复制代码
from mmpretrain import list_models, inference_model
list_models('resnet50', task='Image Classification')#给定任务和骨干网络预训练模型
inference_model('resnet50_8xb32_in1k', '/media/lhy/mmpretrain/demo/bird.JPEG', show=True)#给定预训练模型和图像的路径

2、通过ImageClassificationInferencer进行任务推理

该inference_model API仅用于演示,不能保留模型实例或对多个样本进行推理。您可以使用推理器进行多次调用。

1. 使用MMPreTrain中的预训练模型对图像进行推理。

bash 复制代码
from mmpretrain import  list_models, ImageClassificationInferencer
image = '/media/lhy/mmpretrain/demo/bird.JPEG'
inferencer = ImageClassificationInferencer('resnet50_8xb32_in1k')
# Note that the inferencer output is a list of result even if the input is a single sample.
result = inferencer(image)[0]
print(result['pred_class'])
print(result['pred_score'])
print(result['pred_label'])
# You can also use is for multiple images.
image_list = ['demo/demo.JPEG', 'demo/bird.JPEG'] * 16
results = inferencer(image_list, batch_size=8)
print(len(results))
print(results[1]['pred_class'])

2. 使用配置文件和检查点来推断GPU上的多个图像,并将可视化结果保存在文件夹中。

bash 复制代码
from mmpretrain import ImageClassificationInferencer
inferencer = ImageClassificationInferencer(model='configs/resnet/resnet50_8xb32_in1k.py',
pretrained='https://download.openmmlab.com/mmclassification/v0/resnet/resnet50_8xb32_in1k_20210831-ea4938fc.pth',device='cuda')
inferencer(['demo/dog.jpg', 'demo/bird.JPEG'], show_dir="./visualize/")

visualize_kwargs: set = {
      'resize', 'rescale_factor', 'draw_score', 'show', 'show_dir',
      'wait_time'
  }

3、gradio 演示进行任务推理

3、ImageRetrievalInferencer:从给定图像集上的给定图像执行图像到图像检索

bash 复制代码
from mmpretrain import ImageRetrievalInferencer
ImageRetrievalInferencer.list_models()

2、通过ImageRetrievalInferencer进行任务推理

bash 复制代码
from mmpretrain import list_models,ImageRetrievalInferencer
inferencer = ImageRetrievalInferencer('resnet50-arcface_inshop',prototype=['/media/lhy/mmpretrain/demo/demo.JPEG', '/media/lhy/mmpretrain/demo/dog.jpg'],prototype_cache='img_retri.pth')
#prototype检索的图像列表
inferencer('/media/lhy/mmpretrain/demo/cat-dog.png', topk=2)[0][1]

{'match_score': tensor(0.4088, device='cuda:0'),
   'sample_idx': 3,
   'sample': {'img_path': './demo/dog.jpg'}}
"""  # noqa: E501

visualize_kwargs: set = {
  'draw_score', 'resize', 'show_dir', 'show', 'wait_time', 'topk'
}
postprocess_kwargs: set = {'topk'}

4、ImageCaptionInferencer(在给定图像上生成标题)任务可用模型

bash 复制代码
from mmpretrain import ImageCaptionInferencer
ImageCaptionInferencer.list_models()
bash 复制代码
['blip-base_3rdparty_caption',
'blip2-opt2.7b_3rdparty-zeroshot_caption', 
'flamingo_3rdparty-zeroshot_caption', 
'llava-7b-v1_caption',
'minigpt-4_vicuna-7b_caption',
'ofa-base_3rdparty-finetuned_caption', 
'otter-9b_3rdparty_caption']

1、通过inference_model进行任务推理

bash 复制代码
from mmpretrain import list_models, inference_model
list_models(task='Image Caption') #给定任务和骨干网络预训练模型
inference_model('ofa-base_3rdparty-finetuned_caption', '/media/lhy/mmpretrain/demo/cat-dog.png', show=True)

2、通过ImageCaptionInferencer进行任务推理

bash 复制代码
from mmpretrain import ImageCaptionInferencer
inferencer = ImageCaptionInferencer('blip-base_3rdparty_caption')
inferencer('/media/lhy/mmpretrain/demo/cat-dog.png')[0]

{'pred_caption': 'a puppy and a cat sitting on a blanket'}

5、VisualGroundingInferencer:从给定图像的描述中找到一个对象任务可用模型

bash 复制代码
from mmpretrain import VisualGroundingInferencer
VisualGroundingInferencer.list_models()

'blip-base_8xb16_refcoco', 'ofa-base_3rdparty-finetuned_refcoco'

1、通过inference_model进行任务推理

bash 复制代码
from mmpretrain import list_models, inference_model
list_models(task='Visual Grounding') #给定任务和骨干网络预训练模型
inference_model('ofa-base_3rdparty-finetuned_refcoco', '/media/lhy/mmpretrain/demo/cat-dog.png', 'cat', show=True)

2、通过ImageCaptionInferencer进行任务推理

bash 复制代码
from mmpretrain import VisualGroundingInferencer
inferencer = VisualGroundingInferencer('ofa-base_3rdparty_refcocinferencer('/media/lhy/mmpretrain/demo/cat-dog.png', 'dog')[0]
{'pred_bboxes': tensor([[ 36.6000,  29.6000, 355.8000, 395.2000]])}
    """  # noqa: E501

6、TextToImageRetrievalInferencer:(根据给定图像集的给定描述执行文本到图像检索)任务可用模型

bash 复制代码
from mmpretrain import TextToImageRetrievalInferencer
TextToImageRetrievalInferencer.list_models()
bash 复制代码
['blip-base_3rdparty_retrieval', 'blip2_3rdparty_retrieval']

1、通过TextToImageRetrievalInferencer进行任务推理

bash 复制代码
from mmpretrain import TextToImageRetrievalInferencer
inferencer = TextToImageRetrievalInferencer('blip-base_3rdparty_retrieval',prototype='/media/lhy/mmpretrain/demo/',prototype_cache='t2i_retri.pth')
inferencer('A cat and a dog.')[0]

{'match_score': tensor(0.3855, device='cuda:0'),
		        'sample_idx': 1,
		        'sample': {'img_path': './demo/cat-dog.png'}}

7、ImageToTextRetrievalInferencer(从给定图像对一系列文本执行图像到文本检索)

bash 复制代码
from mmpretrain import ImageToTextRetrievalInferencer
ImageToTextRetrievalInferencer.list_models()
相关推荐
云和数据.ChenGuang2 分钟前
Golang AI 工程师路线图:从入门到实战
开发语言·人工智能·后端·golang
2601_956743688 分钟前
上海GEO营销公司技术评估解析|以盾码无界公开产品路线为观察对象,梳理问题池构建、知识库事实锚定、内容审核与跨模型监测流程,明确现场演示和任务追溯的核验要求
大数据·人工智能·产品运营·技术分享·geo·上海
梦仔生信进阶9 分钟前
【机器学习】ACC = 1 是否意味着过拟合?还是数据泄露?一个好的模型应该是什么样的
人工智能·深度学习·机器学习
救救孩子把11 分钟前
AI面试八股文-12_11-多模态大模型面试题
人工智能·面试·职场和发展·多模态·clip·rag·llave
zxz520zmg15 分钟前
地质学笔记
人工智能·深度学习·机器学习
0春华秋实18 分钟前
团队落地 RAGAS 的五阶段路线图(13/13)
人工智能·aigc·开源软件
QinHsiu23 分钟前
5种方案、3个工程问题、1个务实路线图:AI Agent上下文管理的“战国时代”
人工智能
y = xⁿ24 分钟前
拆解cloud code记忆系统
java·人工智能·python·github
Dawson Zhu27 分钟前
【AI Infra小白入门④】AI Infra学习路线图:从入门到精通的完整指南(附核心思维方法)
人工智能·语言模型·架构·aigc·agi
QinHsiu27 分钟前
99%的AI短剧创作者月入不足10元:工具已经白菜价,但赚钱的依然是那1%
人工智能