一个专业的深度学习训练框架,专为医学图像分割任务设计,提供灵活的数据集管理、模型训练和结果分析功能。
克隆项目
git clone https://github.com/rinbarpen/NeuroTrain.git
cd NeuroTrain
创建并激活Conda环境
conda create -n ntrain python=3.10
conda activate ntrain
安装依赖
# 使用uv安装依赖(推荐)
uv pip install -e '.[cu128]' # 支持CUDA 12.8
# 或者选择其他版本:cpu, cu118, cu126
# 或者使用传统方式
pip install -e '.[cu128]'
验证安装
python -c "import torch; print(f'PyTorch版本: {torch.__version__}')"
python -c "import torch; print(f'CUDA可用: {torch.cuda.is_available()}')"
将数据集放在 data 目录下,例如:
data/
├── drive/
│ ├── images/
│ └── masks/
├── chasedb1/
│ ├── images/
│ └── masks/
├── stare/
│ ├── images/
│ └── masks/
└── coco/ # COCO数据集(可选)
├── annotations/
├── train2017/
└── val2017/
下载COCO数据集(可选):
# 使用自动下载脚本
bash scripts/download_coco.sh 2017 all
# 或者手动下载,详见 docs/COCO_DATASET_GUIDE.md
复制并修改配置文件:
cp configs/single/train.template.toml configs/my_training.toml
# 激活环境
conda activate ntrain
# 检查配置
python main.py -c configs/my_training.toml --check
# 开始训练
python main.py -c configs/my_training.toml --train
# 训练并测试
python main.py -c configs/my_training.toml --train --test
我们提供了完整的文档来帮助您使用NeuroTrain框架:
NeuroTrain/
├── configs/ # 配置文件
│ ├── single/ # 单次训练配置
│ └── pipeline/ # 管道配置
├── src/ # 核心源代码
│ ├── dataset/ # 数据集模块
│ ├── engine/ # 训练引擎
│ ├── metrics/ # 评估指标
│ └── utils/ # 工具函数
├── data/ # 数据集存储
├── runs/ # 训练结果输出
├── cache/ # 模型缓存
├── tests/ # 测试文件
├── docs/ # 文档
├── tools/ # 分析工具
├── scripts/ # 脚本文件
└── examples/ # 使用示例
[basic]
task_name = "Retina_Vessel_Segmentation"
run_id = "experiment_001"
[model]
name = "UNet"
n_channels = 3
n_classes = 2
[dataset]
name = "drive"
root_dir = "data/drive"
is_rgb = true
[training]
epochs = 100
batch_size = 8
learning_rate = 0.001
[dataset]
name = "enhanced_hybrid"
datasets = ["drive", "chasedb1", "stare"]
sampling_strategy = "weighted"
ratios = [0.5, 0.3, 0.2]
weights = [1.0, 1.2, 0.8]
[dataset.drive]
root_dir = "data/drive"
is_rgb = true
[dataset.chasedb1]
root_dir = "data/chasedb1"
is_rgb = true
[dataset.stare]
root_dir = "data/stare"
is_rgb = true
适用于在单个数据集上训练模型:
python main.py -c configs/single/train-drive.toml --train
使用多个数据集进行联合训练:
python main.py -c configs/single/train.enhanced.template.toml --train
对训练好的模型进行测试:
python main.py -c configs/my_config.toml --test
执行一系列预定义的实验:
python main_pipeline.py -c configs/pipeline/pipeline-template.toml
训练扩散模型进行图像生成:
# 运行示例
python examples/diffusion_dataset_example.py
# 或使用配置文件
python main.py -c examples/config_diffusion_example.toml --train
了解更多:Diffusion数据集指南 和 快速开始README
训练结果保存在 runs/{run_id}/ 目录下:
runs/
└── experiment_001/
├── train/
│ ├── model_summary.txt # 模型结构摘要
│ ├── model_flop_count.txt # 计算复杂度分析
│ └── training.log # 训练日志
├── test/
│ ├── metrics.json # 测试指标
│ └── predictions/ # 预测结果
└── checkpoints/
├── best.pth # 最佳模型
└── last.pth # 最新模型
tail -f runs/{run_id}/train/training.log
python tools/analyzers/metrics_analyzer.py --run_id {run_id}
NeuroTrain支持多种模型量化方法,帮助减少模型大小和推理时间:
from src.utils.quantization import QuantizationConfig, QuantizationManager
# 创建量化配置
config = QuantizationConfig(method="dynamic", dtype="qint8")
# 量化模型
manager = QuantizationManager(config)
quantized_model = manager.quantize_model(your_model)
# 获取模型信息
size_info = manager.get_model_size_info(quantized_model)
print(f"模型大小: {size_info['model_size_mb']:.2f}MB")
# 量化模型
python tools/quantization_cli.py quantize model.pt output/ --method dynamic
# 分析量化效果
python tools/quantization_cli.py analyze original.pt quantized.pt analysis/
# 运行示例
python tools/quantization_cli.py example --method dynamic
# config.yaml
quantization:
enabled: true
method: "dynamic"
dtype: "qint8"
详细使用说明请参考 量化模块文档。
运行测试以确保框架正常工作:
# 激活环境
conda activate ntrain
# 运行所有测试
python -m pytest tests/ -v
# 运行特定测试
python -m pytest tests/test_dataset_creation.py -v
# 测试数据集创建
python test_dataset_creation.py
# 测试增强配置
python test_enhanced_config.py
欢迎贡献代码!请遵循以下步骤:
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。
感谢以下开源项目的支持:
如有问题或建议,请通过以下方式联系:
NeuroTrain - 让医学图像分割变得简单高效! 🚀 │ │ ├── config[.json|.toml|.yaml] # 实验配置文件 │ │ ├── [mean|std]_metric.csv # 所有类别的均值指标和标准差指标 │ │ ├── mean_metrics_per_classes.png # 每个类别的均值指标可视化图 │ │ └── mean_metrics.png # 所有类别的均值指标可视化图 │ ├── predict/ # 预测相关输出 │ │ ├── {predicted_files} # 预测输出文件 │ │ └── config[.json|.toml|.yaml] # 实验配置文件 ├── logs/ # 训练日志 | └── [train|test|predict].log # 每个任务的日志文件 │ model_flop_count.txt # 模型 FLOP 统计 └── model_summary.txt # 模型参数统计
## 贡献
我们欢迎所有形式的贡献,包括但不限于代码、文档、问题报告和功能建议。请通过提交 Pull Request 或打开 Issue 来参与贡献。
## 许可证
本项目基于 MIT 许可证开源。详情请见 `LICENSE` 文件。
111 commits
Python
73.9%
Jupyter Notebook
23.1%
Shell
1.1%
一个专业的深度学习训练框架,专为医学图像分割任务设计,提供灵活的数据集管理、模型训练和结果分析功能。
克隆项目
git clone https://github.com/rinbarpen/NeuroTrain.git
cd NeuroTrain
创建并激活Conda环境
conda create -n ntrain python=3.10
conda activate ntrain
安装依赖
# 使用uv安装依赖(推荐)
uv pip install -e '.[cu128]' # 支持CUDA 12.8
# 或者选择其他版本:cpu, cu118, cu126
# 或者使用传统方式
pip install -e '.[cu128]'
验证安装
python -c "import torch; print(f'PyTorch版本: {torch.__version__}')"
python -c "import torch; print(f'CUDA可用: {torch.cuda.is_available()}')"
将数据集放在 data 目录下,例如:
data/
├── drive/
│ ├── images/
│ └── masks/
├── chasedb1/
│ ├── images/
│ └── masks/
├── stare/
│ ├── images/
│ └── masks/
└── coco/ # COCO数据集(可选)
├── annotations/
├── train2017/
└── val2017/
下载COCO数据集(可选):
# 使用自动下载脚本
bash scripts/download_coco.sh 2017 all
# 或者手动下载,详见 docs/COCO_DATASET_GUIDE.md
复制并修改配置文件:
cp configs/single/train.template.toml configs/my_training.toml
# 激活环境
conda activate ntrain
# 检查配置
python main.py -c configs/my_training.toml --check
# 开始训练
python main.py -c configs/my_training.toml --train
# 训练并测试
python main.py -c configs/my_training.toml --train --test
我们提供了完整的文档来帮助您使用NeuroTrain框架:
NeuroTrain/
├── configs/ # 配置文件
│ ├── single/ # 单次训练配置
│ └── pipeline/ # 管道配置
├── src/ # 核心源代码
│ ├── dataset/ # 数据集模块
│ ├── engine/ # 训练引擎
│ ├── metrics/ # 评估指标
│ └── utils/ # 工具函数
├── data/ # 数据集存储
├── runs/ # 训练结果输出
├── cache/ # 模型缓存
├── tests/ # 测试文件
├── docs/ # 文档
├── tools/ # 分析工具
├── scripts/ # 脚本文件
└── examples/ # 使用示例
[basic]
task_name = "Retina_Vessel_Segmentation"
run_id = "experiment_001"
[model]
name = "UNet"
n_channels = 3
n_classes = 2
[dataset]
name = "drive"
root_dir = "data/drive"
is_rgb = true
[training]
epochs = 100
batch_size = 8
learning_rate = 0.001
[dataset]
name = "enhanced_hybrid"
datasets = ["drive", "chasedb1", "stare"]
sampling_strategy = "weighted"
ratios = [0.5, 0.3, 0.2]
weights = [1.0, 1.2, 0.8]
[dataset.drive]
root_dir = "data/drive"
is_rgb = true
[dataset.chasedb1]
root_dir = "data/chasedb1"
is_rgb = true
[dataset.stare]
root_dir = "data/stare"
is_rgb = true
适用于在单个数据集上训练模型:
python main.py -c configs/single/train-drive.toml --train
使用多个数据集进行联合训练:
python main.py -c configs/single/train.enhanced.template.toml --train
对训练好的模型进行测试:
python main.py -c configs/my_config.toml --test
执行一系列预定义的实验:
python main_pipeline.py -c configs/pipeline/pipeline-template.toml
训练扩散模型进行图像生成:
# 运行示例
python examples/diffusion_dataset_example.py
# 或使用配置文件
python main.py -c examples/config_diffusion_example.toml --train
了解更多:Diffusion数据集指南 和 快速开始README
训练结果保存在 runs/{run_id}/ 目录下:
runs/
└── experiment_001/
├── train/
│ ├── model_summary.txt # 模型结构摘要
│ ├── model_flop_count.txt # 计算复杂度分析
│ └── training.log # 训练日志
├── test/
│ ├── metrics.json # 测试指标
│ └── predictions/ # 预测结果
└── checkpoints/
├── best.pth # 最佳模型
└── last.pth # 最新模型
tail -f runs/{run_id}/train/training.log
python tools/analyzers/metrics_analyzer.py --run_id {run_id}
NeuroTrain支持多种模型量化方法,帮助减少模型大小和推理时间:
from src.utils.quantization import QuantizationConfig, QuantizationManager
# 创建量化配置
config = QuantizationConfig(method="dynamic", dtype="qint8")
# 量化模型
manager = QuantizationManager(config)
quantized_model = manager.quantize_model(your_model)
# 获取模型信息
size_info = manager.get_model_size_info(quantized_model)
print(f"模型大小: {size_info['model_size_mb']:.2f}MB")
# 量化模型
python tools/quantization_cli.py quantize model.pt output/ --method dynamic
# 分析量化效果
python tools/quantization_cli.py analyze original.pt quantized.pt analysis/
# 运行示例
python tools/quantization_cli.py example --method dynamic
# config.yaml
quantization:
enabled: true
method: "dynamic"
dtype: "qint8"
详细使用说明请参考 量化模块文档。
运行测试以确保框架正常工作:
# 激活环境
conda activate ntrain
# 运行所有测试
python -m pytest tests/ -v
# 运行特定测试
python -m pytest tests/test_dataset_creation.py -v
# 测试数据集创建
python test_dataset_creation.py
# 测试增强配置
python test_enhanced_config.py
欢迎贡献代码!请遵循以下步骤:
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。
感谢以下开源项目的支持:
如有问题或建议,请通过以下方式联系:
NeuroTrain - 让医学图像分割变得简单高效! 🚀 │ │ ├── config[.json|.toml|.yaml] # 实验配置文件 │ │ ├── [mean|std]_metric.csv # 所有类别的均值指标和标准差指标 │ │ ├── mean_metrics_per_classes.png # 每个类别的均值指标可视化图 │ │ └── mean_metrics.png # 所有类别的均值指标可视化图 │ ├── predict/ # 预测相关输出 │ │ ├── {predicted_files} # 预测输出文件 │ │ └── config[.json|.toml|.yaml] # 实验配置文件 ├── logs/ # 训练日志 | └── [train|test|predict].log # 每个任务的日志文件 │ model_flop_count.txt # 模型 FLOP 统计 └── model_summary.txt # 模型参数统计
## 贡献
我们欢迎所有形式的贡献,包括但不限于代码、文档、问题报告和功能建议。请通过提交 Pull Request 或打开 Issue 来参与贡献。
## 许可证
本项目基于 MIT 许可证开源。详情请见 `LICENSE` 文件。
111 commits
Python
73.9%
Jupyter Notebook
23.1%
Shell
1.1%