Files
Seg_Data_Server/README.md
2026-05-20 15:05:35 +08:00

265 lines
7.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Seg 图像分割项目使用说明
本项目是一个多路线图像分割实验与推理工程,包含自研 `segmentation_models_pytorch` 训练流程、YOLO 分割流程、MMSegmentation 流程,以及数据预处理、视频抽帧、图片叠加和结果分析工具。
## 1. 项目结构
| 路径 | 用途 |
| --- | --- |
| `Seg_All_In_One_SegModel/` | 基于 `segmentation_models_pytorch` 的语义分割训练、推理、参数量/FLOPs/FPS 统计和输出完整性检查。 |
| `Seg_All_In_One_YoloModel/` | 基于 Ultralytics YOLO 的实例/语义分割训练、推理、热力图可视化和横向对比。 |
| `Seg_All_In_One_MMSeg/` | 基于 OpenMMLab MMSegmentation 的模型配置、训练和结果汇总流程。 |
| `Seg_All_In_One_Analysis/` | 汇总不同模型的指标、FLOPs、FPS生成表格和 mIoU/FPS 图。 |
| `DataSet_Own/1. 图片预处理(内含使用手册)/` | 自有数据的重命名、尺寸统一、图像/标签配对、标签重建、叠加和拼接检查。 |
| `Seg_Predict_Own_Video_V2/` | 将视频按固定间隔抽帧,转换为 `DataSet_Public/<dataset>/images/val` 格式。 |
| `Tool-图片堆叠/` | 快速检查原图和标签是否匹配,并生成透明叠加图。 |
| `Tool-可视化/` | YOLO 标签生成、热图、FPS 等可视化辅助脚本。 |
| `Back_Up.sh` | 将算法目录同步到 `Hardisk/``Nas_BackUp_Seg/` 的本地/NAS 备份脚本。 |
大体量目录如 `DataSet_Public/``DataSet_Own/` 中的数据、`BestMode_Predict_Results_DataSet_Public/``Hardisk/``Nas_BackUp_Seg/`、模型权重和训练产物不进入 Git。
## 2. Conda 环境
推荐使用独立环境 `seg_smp`。本机已有可运行的 `SMP` 环境时,最快方式是克隆它:
```bash
conda create --name seg_smp --clone SMP -y
conda activate seg_smp
python -V
python -c "import torch; print(torch.cuda.is_available(), torch.__version__)"
```
从零安装时可参考:
```bash
conda create -n seg_smp python=3.9 -y
conda activate seg_smp
pip install torch==2.8.0+cu129 torchvision==0.23.0+cu129 --index-url https://download.pytorch.org/whl/cu129
pip install -r requirements-seg_smp.txt
cd Seg_All_In_One_MMSeg
pip install -v -e .
cd ..
```
如果使用批量脚本,默认会激活 `seg_smp`。如需临时使用旧环境:
```bash
SEG_CONDA_ENV=SMP bash yolo_train.sh
```
环境验证:
```bash
conda run -n seg_smp python -c "import torch, segmentation_models_pytorch, ultralytics, mmcv, mmengine, mmseg, cv2, albumentations; print('ok')"
```
## 3. 数据约定
SegModel 默认读取:
```text
DataSet_Public/<dataset>/
images/train
images/val
labels_GT/train
labels_GT/val
```
YOLO 默认读取:
```text
DataSet_Public/<dataset>/
images/train
images/val
labels/train
labels/val
```
切换数据集时:
- SegModel修改 `Seg_All_In_One_SegModel/config.py` 中的 `DATA_DIR``OUTPUTS_DIR``PREDICT_BEST_MODEL_DIR`、类别和图像尺寸。
- YOLO修改 `Seg_All_In_One_YoloModel/dataset.yaml` 中的 `path``train``val``test``names`;必要时修改 `yolo_config.py` 的训练参数。
- MMSeg`Seg_All_In_One_MMSeg/※使用手册/※2025_9_23_MMSeg使用手册` 生成数据集和算法配置。
## 4. SegModel 使用方式
```bash
cd Seg_All_In_One_SegModel
conda activate seg_smp
# 单模型训练
CUDA_VISIBLE_DEVICES=0 python train.py -a Unet
# 批量训练
bash train.sh
# 单模型推理
CUDA_VISIBLE_DEVICES=0 python 1_predict.py -a Unet
# 批量推理
bash predict.sh
# 参数量、FLOPs、FPS
CUDA_VISIBLE_DEVICES=0 python 2_predict_params_and_FLOPs_V2.py
# 检查预测 raw mask 是否齐全
python 1_predict_raw_masks_check.py
```
可选模型包括:
```text
Unet, UnetPlusPlus, FPN, PSPNet, DeepLabV3, DeepLabV3Plus,
Linknet, MAnet, PAN, UPerNet, Segformer, DPT
```
训练结果先写入 `DataSet_Public_outputs/<dataset>_outputs-SegModel/`,脚本结束后会移动到 `Hardisk/`;推理结果写入 `BestMode_Predict_Results_DataSet_Public/<dataset>_outputs-SegModel/`
## 5. YOLO 使用方式
```bash
cd Seg_All_In_One_YoloModel
conda activate seg_smp
# 检查当前 dataset.yaml 解析出的路径
python yolo_config.py
# 单模型训练
CUDA_VISIBLE_DEVICES=0 python yolo_train.py --model "YOLOv8n-seg"
# 批量训练
bash yolo_train.sh
# 复制最佳权重到预测目录
bash ./Tool_Yolo_Copy_Best_Model.sh --pt_name "best.pt"
# 单模型推理
CUDA_VISIBLE_DEVICES=0 python yolo_predict_V2.py --model "YOLOv8n-seg" --conf 0.2 --pt_name "best.pt"
# 批量推理
bash yolo_predict.sh --conf 0.2 --pt_name "best.pt"
# 批量热图
bash yolo_predict.sh --heatmap_method "All" --pt_name "best.pt"
# 横向对比
python yolo_predict_V2_compare_all.py --pt_name "all"
# 检查预测 raw mask 是否齐全
python yolo_predict_raw_masks_check.py --pt_name "best.pt"
```
常用模型包括:
```text
YOLOv8n-seg, YOLOv8s-seg, YOLOv8m-seg, YOLOv8l-seg, YOLOv8x-seg,
YOLOv9c-seg, YOLOv9e-seg,
YOLO11n-seg, YOLO11s-seg, YOLO11m-seg, YOLO11l-seg, YOLO11x-seg,
YOLO12-seg
```
## 6. MMSeg 使用方式
```bash
cd Seg_All_In_One_MMSeg
conda activate seg_smp
# 首次或新增模块后注册工程
pip install -v -e .
# 下载/保存必要预训练权重
python My_All_In_One/0_Initial_Save_All_Model_locally.py
# 生成数据配置
python My_All_In_One/1_Initial_Data_All_data_from_1_Data_Parameter-V2.py
# 生成算法配置
python My_All_In_One/2_Initial_Alg_All_data_from_2_Alg_Program-V2.py
# 参数量、FLOPs、FPS
CUDA_VISIBLE_DEVICES=0 python My_All_In_One/4_1_predict_params_FLOPs_FPS_V2.py
# 指标汇总
CUDA_VISIBLE_DEVICES=0 python My_All_In_One/4_2_predict_matrics_from_log_V2.py
# 生成预测图和表格
CUDA_VISIBLE_DEVICES=0 python My_All_In_One/4_3_predict_draw_pictures_and_tabels.py
# 提取 loss 和 best mIoU
CUDA_VISIBLE_DEVICES=0 python My_All_In_One/4_4_extract_loss_and_best_miou.py
```
MMSeg 对 `mmcv/mmengine/mmsegmentation` 版本较敏感;若遇到 `mmcv` CUDA 算子或版本错误,优先参考 MMSeg 使用手册中的安装记录。
## 7. 数据预处理与视频抽帧
自有图片预处理:
```bash
cd "DataSet_Own/1. 图片预处理(内含使用手册)"
bash 1_rename_pics.sh -i <ori_image_directory> -l <ori_label_directory>
bash 2_reformate_pics.sh -i <ori_image_directory> -l <ori_label_directory> -w 1920 -h 1080
bash 3_pair_ori_label.sh -i <ori_image_directory> -l <ori_label_directory>
bash 4_rebuild_labels.sh -l <ori_label_directory>
bash 5_TOOL_stack_pics.sh -i <ori_image_directory> -l <ori_label_directory> -r <stack_result_directory> -a 0.3
bash 6_TOOL_stitch_pics.sh -i <ori_image_directory> -l <ori_label_directory> -r <stitch_result_directory>
```
视频抽帧:
```bash
cd Seg_Predict_Own_Video_V2
python 1_Save_Frame_V2.py \
--video ./LC_Video_1.mp4 \
--resize "1920x1080" \
--output_dir "../DataSet_Public/5_Predict_Video" \
--interval 0.5
```
输出路径为:
```text
DataSet_Public/5_Predict_Video/<video_name>/images/val
```
图片叠加检查:
```bash
cd Tool-图片堆叠
python 1_check_picture_pair.py -i ./ori -l ./label
bash 2_TOOL_stack_pics.sh -i ./ori -l ./label -r ./result_0.3透明度 -a 0.3 -s _label
```
## 8. 结果分析
```bash
cd Seg_All_In_One_Analysis
conda activate seg_smp
python 1_Analysis_All.py \
--input_dir ../BestMode_Predict_Results_DataSet_Public \
--output_dir ./
```
脚本会交互选择数据集,合并 SegModel/MMSeg 的指标和速度数据,并生成 CSV、PNG、SVG 等分析结果。
## 9. 备份与 Git
本仓库只提交程序和轻量配置。以下内容不进入 Git
- 数据集:`DataSet_Public/`、除预处理脚本外的 `DataSet_Own/`
- 训练和预测产物:`DataSet_Public_outputs/``BestMode_Predict_Results_DataSet_Public/``Hardisk/``Nas_BackUp_Seg/`
- 大权重和视频:`*.pt``*.pth``*.onnx``*.mp4`
- Python/构建缓存:`__pycache__/``.pytest_cache/``build/``dist/`
常用 Git 检查:
```bash
git status --short
git ls-files | grep -E '\\.(pt|pth|mp4|zip)$|DataSet_Public|Hardisk|BestMode' || true
```