目录
1.mmclassification安装
2.运行,生成并使用一个配置文件
在pycharm中,点击右上角如下图所示,并在Configuration 中的配置好运行脚本路径,配置参数以及运行环境如下所示,点击OK后运行。
在D:\Code\mmclassification\tools\work_dirs\resnet18_8xb32_in1k\下面会生成整个配置文件resnet18_8xb32_in1k.py,将该配置文件复制到原配置文件路径或者自己指定位置如D:\Code\mmclassification\configs\resnet\,并修改名称如为GWF_resnet18_8xb32_in1k.py。里面的参数可根据自己的数据进行修改。最后在将该配置文件路径放在pycharm下,如下图所示,点pycharm绿色三角形即可运行。
3.使用自己数据集
GWF_resnet18_8xb32_in1k.py中
另外mmcls.dadasets.imagenet.py 中修改类别名称
.官网入门 — MMClassization 0.23.2 文档 (mmclassification.readthedocs.io)
有道教程https://note.youdao.com/s/GknDhLkT
#官网https://github.com/open-mmlab/mmclassification
选择安装可视化工具grad-cam
#pip install "grad-cam>=1.3.6"
安装好pytorch后离线安装mmcv##########################################
查看cuda版本
PS D:\Code\yolov5-master> nvcc -V
Cuda compilation tools, release 11.3, V11.3.58
查看pytorch版本
PS D:\Code\yolov5-master> pip list
torch 1.10.0+cu113
下载mmcv.复制一下链接到浏览器下载,其中/cu113/torch1.10/修改为自己的版本
https://download.openmmlab.com/mmcv/dist/cu113/torch1.10/index.html
安装mmcv,在pytorch环境下
PS D:\Code> pip install .\mmcv_full-1.6.1-cp38-cp38-win_amd64.whl
对比官网教程,可能还要安装 openmim
pip3 install openmim
官网安装教程mmcv##########################################
conda create -n open-mmlab python=3.8 pytorch=1.10 cudatoolkit=11.3 torchvision -c pytorch -y
conda activate open-mmlab
conda activate py38
pip3 install openmim
mim install mmcv-full
下载并安装mmclassification##########################################
git clone https://github.com/open-mmlab/mmclassification.git
cd mmclassification
pip3 install -e .
注意:windows下参数路径如下D:\Code\mmclassification\configs\resnet\resnet18_8xb32_in1k.py
如果出现路径错误,修改为
D:\\Code\\mmclassification\\configs\\resnet\\resnet18_8xb32_in1k.py
重点: num_classes=10, #num_classes=1000,修改为自己的类别数量
data_prefix='../mmcls/data/mnist/train', #'data/imagenet/train'修改为自己的数据位置
model = dict(
type='ImageClassifier',
backbone=dict(
type='ResNet',
depth=18,
num_stages=4,
out_indices=(3, ),
style='pytorch'),
neck=dict(type='GlobalAveragePooling'),
head=dict(
type='LinearClsHead',
num_classes=10, #num_classes=1000,修改为自己的类别数量
in_channels=512,
loss=dict(type='CrossEntropyLoss', loss_weight=1.0),
topk=(1, 5)))
dataset_type = 'ImageNet'
img_norm_cfg = dict(
mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
train_pipeline = [
dict(type='LoadImageFromFile'),
dict(type='RandomResizedCrop', size=224),
dict(type='RandomFlip', flip_prob=0.5, direction='horizontal'),
dict(
type='Normalize',
mean=[123.675, 116.28, 103.53],
std=[58.395, 57.12, 57.375],
to_rgb=True),
dict(type='ImageToTensor', keys=['img']),
dict(type='ToTensor', keys=['gt_label']),
dict(type='Collect', keys=['img', 'gt_label'])
]
test_pipeline = [
dict(type='LoadImageFromFile'),
dict(type='Resize', size=(256, -1)),
dict(type='CenterCrop', crop_size=224),
dict(
type='Normalize',
mean=[123.675, 116.28, 103.53],
std=[58.395, 57.12, 57.375],
to_rgb=True),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img'])
]
data = dict(
samples_per_gpu=32,
workers_per_gpu=2,
train=dict(
type='ImageNet',
data_prefix='../mmcls/data/mnist/train', #'data/imagenet/train'修改为自己的数据位置
pipeline=[
dict(type='LoadImageFromFile'),
dict(type='RandomResizedCrop', size=224),
dict(type='RandomFlip', flip_prob=0.5, direction='horizontal'),
dict(
type='Normalize',
mean=[123.675, 116.28, 103.53],
std=[58.395, 57.12, 57.375],
to_rgb=True),
dict(type='ImageToTensor', keys=['img']),
dict(type='ToTensor', keys=['gt_label']),
dict(type='Collect', keys=['img', 'gt_label'])
]),
val=dict(
type='ImageNet',
data_prefix='../mmcls/data/mnist/test',#'data/imagenet/val',修改为自己的数据位置
#ann_file='data/imagenet/meta/val.txt', 去掉,即换成用val里面文件夹的名字当做类别名。
pipeline=[
dict(type='LoadImageFromFile'),
dict(type='Resize', size=(256, -1)),
dict(type='CenterCrop', crop_size=224),
dict(
type='Normalize',
mean=[123.675, 116.28, 103.53],
std=[58.395, 57.12, 57.375],
to_rgb=True),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img'])
]),
test=dict(
type='ImageNet',
data_prefix='../mmcls/data/mnist/test',#'data/imagenet/val',修改为自己的数据位置
#ann_file='data/imagenet/meta/val.txt',去掉,即换成用val里面文件夹的名字当做类别名。
pipeline=[
dict(type='LoadImageFromFile'),
dict(type='Resize', size=(256, -1)),
dict(type='CenterCrop', crop_size=224),
dict(
type='Normalize',
mean=[123.675, 116.28, 103.53],
std=[58.395, 57.12, 57.375],
to_rgb=True),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img'])
]))
evaluation = dict(interval=1, metric='accuracy')
optimizer = dict(type='SGD', lr=0.1, momentum=0.9, weight_decay=0.0001)#优化方式可修改
optimizer_config = dict(grad_clip=None)
lr_config = dict(policy='step', step=[30, 60, 90])##学习率下降方式可修改
runner = dict(type='EpochBasedRunner', max_epochs=6) ##最大迭代次数可修改
checkpoint_config = dict(interval=5) #interval=1) #每隔多少个epoch进行模型保持,设大一点比较好,少站内存。
log_config = dict(interval=100, hooks=[dict(type='TextLoggerHook')])#日志保存
dist_params = dict(backend='nccl')
log_level = 'INFO'
load_from = None
resume_from = None
workflow = [('train', 1)]
work_dir = './work_dirs/resnet18_8xb32_in1k'#结果保存位置
gpu_ids = [0]
如下所示,当使用minist时:
CLASSES = ['0','1','2','3','4','5','6','7','8','9'] #add by gwf