MMSegmentation
MMSegmentation简介
- 项目地址:https://github.com/open-mmlab/mmsegmentation
- 文档:https://mmsegmentation.readthedocs.io/en/latest/
MMSegmentation的项目结构
- 整体架构
- 分割模型的模块化设计
MMSegmentation 将分割模型统一拆解为如下模块,方便用户根据自己的需求进行组装和扩展。
MMSegmentation代码实践
1. 配置环境
1)使用conda创建虚拟环境
# 创建 python=3.8 的det环境
conda create --name mmsegmentation python=3.8
# 激活环境
source activate mmsegmentation
2)安装torch
# 安装 torch
conda install pytorch torchvision torchaudio pytorch-cuda=11.6 -c pytorch -c nvidia
3)安装mmcv
mmcv模块安装时候需要注意 torch 和 cuda 版本,参考安装文档https://mmcv.readthedocs.io/zh_CN/latest/get_started/installation.html
pip install mmcv-full==1.7.0 -f https://download.openmmlab.com/mmcv/dist/cu116/torch1.13/index.html
pip install mmengine
4)安装MMSegmentation模块(从源码)
git clone https://github.com/open-mmlab/mmsegmentation.git
# git clone https://github.com/open-mmlab/mmsegmentation.git -b dev-1.x
cd mmsegmentation
# "-v "指详细说明,或更多的输出
# "-e" 表示在可编辑模式下安装项目,因此对代码所做的任何本地修改都会生效,从而无需重新安装。
pip install -v -e .
2. 创建数据集
1)下载数据集
# 进入mmsgementation目录
cd mmsegmentation
# 创建data目录
mkdir data && cd data
# 下载数据集
wget https://zihao-openmmlab.obs.cn-east-3.myhuaweicloud.com/20230130-mmseg/dataset/Glomeruli-dataset.zip
# 解压
unzip Glomeruli-dataset.zip
2)划分数据集
# 进入mmsgementation目录
cd mmsegmentation
# 创建脚本
vim split_data.py
# 执行数据划分
python split_data.py
split_data.py
import os
import random
# 获取全部数据文件名列表
PATH_IMAGE = 'data/Glomeruli-dataset/images'
all_file_list = os.listdir(PATH_IMAGE)
all_file_num = len(all_file_list)
# 随机打乱全部数据文件名列表
random.shuffle(all_file_list)
# 指定训练集和测试集比例
train_ratio = 0.8
test_ratio = 1 - train_ratio
train_file_list = all_file_list