2022-08-31 14:21:56 +08:00
|
|
|
|
# 使用现有模型推理
|
|
|
|
|
|
2023-03-20 15:56:09 +08:00
|
|
|
|
MMPretrain 在 [Model Zoo](../modelzoo_statistics.md) 中提供了预训练模型。
|
2022-08-31 14:21:56 +08:00
|
|
|
|
本说明将展示**如何使用现有模型对给定图像进行推理**。
|
|
|
|
|
|
2023-03-27 14:32:26 +08:00
|
|
|
|
至于如何在标准数据集上测试现有模型,请看这个[指南](./test.md)
|
2022-08-31 14:21:56 +08:00
|
|
|
|
|
|
|
|
|
## 推理单张图片
|
|
|
|
|
|
2023-03-20 15:56:09 +08:00
|
|
|
|
MMPretrain 为图像推理提供高级 Python API:
|
2022-08-31 14:21:56 +08:00
|
|
|
|
|
2023-03-02 13:29:07 +08:00
|
|
|
|
- [`get_model`](mmpretrain.apis.get_model): 根据名称获取一个模型。
|
|
|
|
|
- [`inference_model`](mmpretrain.apis.inference_model):对给定图片进行推理。
|
2022-08-31 14:21:56 +08:00
|
|
|
|
|
|
|
|
|
下面是一个示例,如何使用一个 ImageNet-1k 预训练权重初始化模型并推理给定图像。
|
|
|
|
|
|
|
|
|
|
```{note}
|
2023-04-06 20:58:52 +08:00
|
|
|
|
可以运行 `wget https://github.com/open-mmlab/mmpretrain/raw/main/demo/demo.JPEG` 下载样例图片,或使用其他图片。
|
2022-08-31 14:21:56 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```python
|
2023-03-02 13:29:07 +08:00
|
|
|
|
from mmpretrain import get_model, inference_model
|
2022-08-31 14:21:56 +08:00
|
|
|
|
|
2023-01-11 15:20:51 +08:00
|
|
|
|
img_path = 'demo.JPEG' # 可以指定自己的图片路径
|
2022-08-31 14:21:56 +08:00
|
|
|
|
|
|
|
|
|
# 构建模型
|
2023-01-11 15:20:51 +08:00
|
|
|
|
model = get_model('resnet50_8xb32_in1k', pretrained=True, device="cpu") # `device` 可以为 'cuda:0'
|
2022-08-31 14:21:56 +08:00
|
|
|
|
# 执行推理
|
|
|
|
|
result = inference_model(model, img_path)
|
|
|
|
|
```
|
|
|
|
|
|
2023-03-27 14:32:26 +08:00
|
|
|
|
`result` 为一个包含了 `pred_label`, `pred_score`, `pred_scores` 和 `pred_class`的字典,结果如下:
|
2022-08-31 14:21:56 +08:00
|
|
|
|
|
|
|
|
|
```text
|
2022-10-08 15:21:34 +08:00
|
|
|
|
{"pred_label":65,"pred_score":0.6649366617202759,"pred_class":"sea snake", "pred_scores": [..., 0.6649366617202759, ...]}
|
2022-08-31 14:21:56 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-03-20 15:56:09 +08:00
|
|
|
|
演示可以在 [demo/image_demo.py](https://github.com/open-mmlab/mmpretrain/blob/main/demo/image_demo.py) 中找到。
|