开源预训练框架 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()
相关推荐
零售ERP菜鸟5 分钟前
范式革命:从“信息化”到“数字化”的本质跃迁
大数据·人工智能·职场和发展·创业创新·学习方法·业界资讯
光羽隹衡7 分钟前
计算机视觉——Opencv(图像拼接)
人工智能·opencv·计算机视觉
SEO_juper23 分钟前
2026内容营销破局指南:告别流量内卷,以价值赢信任
人工智能·ai·数字营销·2026
初恋叫萱萱25 分钟前
数据即燃料:用 `cann-data-augmentation` 实现高效训练预处理
人工智能
一战成名99634 分钟前
CANN 仓库揭秘:昇腾 AI 算子开发的宝藏之地
人工智能
hnult41 分钟前
2026 在线培训考试系统选型指南:核心功能拆解与选型逻辑
人工智能·笔记·课程设计
A小码哥41 分钟前
AI 设计时代的到来:从 PS 到 Pencil,一个人如何顶替一个团队
人工智能
AIGCmitutu1 小时前
PS 物体底部阴影怎么做?3 步做出自然逼真的投影效果
人工智能·电子商务·photoshop·ps·美工
开源技术1 小时前
Claude Opus 4.6 发布,100万上下文窗口,越贵越好用
人工智能·python
聆风吟º1 小时前
CANN hccl 深度解析:异构计算集群通信库的跨节点通信与资源管控实现逻辑
人工智能·wpf·transformer·cann