-
环境
- 环境安装
-
pip install openmim mim install mmengine mim install mmcv mim install mmpretrain # 安装多模态模型 pip install "mmpretrain[multimodal]"
- 验证环境
-
In [1]: import mmengine In [2]: mmengine.__version__ Out[2]: '0.7.3' In [3]: import mmcv In [4]: mmcv.__version__ Out[4]: '2.0.0' In [5]: import mmpretrain In [6]: mmpretrain.__version__ Out[6]: '1.0.0rc8'
- 初始应用
- 导入MMPretrain相关包:模型获取,模型列举,模型推理
-
from mmpretrain import get_model, list_models, inference_model
- 获取分类任务中resnet18相关模型
-
In [8]: list_models(task="Image Classification", pattern="resnet18") Out[8]: ['resnet18_8xb16_cifar10', 'resnet18_8xb32_in1k']
- 获取模型冰查看模型类型及其对应的backbone
-
In [9]: model = get_model('resnet18_8xb32_in1k') In [10]: type(model) Out[10]: mmpretrain.models.classifiers.image.ImageClassifier In [11]: type(model.backbone) Out[11]: mmpretrain.models.backbones.resnet.ResNet
- 模型推理
-
inference_model(mode, r"D:\workspace\.git\OpenMMLabCamp\bird.jpg", show=True)
没有加载预训练权重导致上述问题
-
In [2]: list_models(task='Image Caption', pattern='blip') Out[2]: ['blip-base_3rdparty_caption', 'blip2-opt2.7b_3rdparty-zeroshot_caption'] In [3]: inference_model('blip-base_3rdparty_caption', './mmpretrain/demo/cat-dog.png', show=True)
推理结果如下:
- 猫狗分类训练
- 训练结果如下:
-
2023/06/06 22:37:47 - mmengine - INFO - Exp name: dog_and_cat_20230606_223441 2023/06/06 22:37:47 - mmengine - INFO - Saving checkpoint at 5 epochs 2023/06/06 22:37:49 - mmengine - INFO - Epoch(val) [5][51/51] accuracy/top1: 95.7527 data_time: 0.0022 time: 0.0252
- 在测试集上测试结果
-
.\tools\test.py configs2/dog_and_cat.py --checkpoint work_dirs/dog_and_cat/epoch_5.pth
- 样本测试结果存入pkl
-
.\tools\test.py mmpretrain configs2/dog_and_cat.py --checkpoint work_dirs/dog_and_cat/epoch_5.pth
- 分析测试结果
-
.\tools\analysis_tools\analyze_results.py configs2/dog_and_cat.py rersult.pkl --out-dir annalyze
- 生成混淆矩阵
-
.\tools\analysis_tools\confusion_matrix.py configs2/dog_and_cat.py rersult.pkl --show --include-values