作者提出一种nnUNet(no-new-Net)框架,基于原始的UNet(很小的修改),不去采用哪些新的结构,如相残差连接、dense连接、注意力机制等花里胡哨的东西。相反的,把重心放在:预处理(resampling和normalization)、训练(loss,optimizer设置、数据增广)、推理(patch-based策略、test-time-augmentations集成和模型集成等)、后处理(如增强单连通域等)。
1. 环境搭建
操作系统:支持 Linux (ubuntu22.04)
GPU:RTX 4090D(24GB)
Python版本:3.9 或更高版本
1.1. 安装 PyTorch
注意:在正确安装 PyTorch 之前,请勿直接使用"pip install nnunetv2"进行安装。
按照PyTorchStart Locally | PyTorch官方网站的说明安装 PyTorch。请安装支持你硬件(CUDA、MPS、CPU)的最新版本。
使用Conda安装:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
使用Pip安装:
pip install torch torchvision torchaudio
自行编译安装:
为了获得最快的速度,考虑自行编译 PyTorch(需要一点技术含量……)pytorch/pytorch:Python 中的张量和动态神经网络,具有强大的 GPU 加速
# 克隆 PyTorch 源代码仓库
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch# 安装编译所需的依赖
# ...# 编译并安装 PyTorch
python setup.py install
1.2. 安装 nnU-Net(根据你的使用场景)
安装 nnU-Net 会在你的终端中添加几个新命令(用于运行整个 nnU-Net 流水线):
①所有 nnU-Net 命令都带有"nnUNetv2_"前缀,便于识别。
②请注意,这些命令只是执行 Python 脚本。(pyproject.toml文件中的 project.scripts 写明了执行的是哪些脚本/函数。)
[project.scripts]
nnUNetv2_plan_and_preprocess = "nnunetv2.experiment_planning.plan_and_preprocess_entrypoints:plan_and_preprocess_entry"
nnUNetv2_extract_fingerprint = "nnunetv2.experiment_planning.plan_and_preprocess_entrypoints:extract_fingerprint_entry"
nnUNetv2_plan_experiment = "nnunetv2.experiment_planning.plan_and_preprocess_entrypoints:plan_experiment_entry"
nnUNetv2_preprocess = "nnunetv2.experiment_planning.plan_and_preprocess_entrypoints:preprocess_entry"
nnUNetv2_train = "nnunetv2.run.run_training:run_training_entry"
# 使用指定模型文件夹中的模型对原始数据进行预测。
nnUNetv2_predict_from_modelfolder = "nnunetv2.inference.predict_from_raw_data:predict_entry_point_modelfolder"
# 对原始数据进行预测,生成分割结果。
nnUNetv2_predict = "nnunetv2.inference.predict_from_raw_data:predict_entry_point"
nnUNetv2_convert_old_nnUNet_dataset = "nnunetv2.dataset_conversion.convert_raw_dataset_from_old_nnunet_format:convert_entry_point"
# 在交叉验证结果中寻找最佳模型配置。
nnUNetv2_find_best_configuration = "nnunetv2.evaluation.find_best_configuration:find_best_configuration_entry_point"
# 确定后处理参数,例如去除连通组件。
nnUNetv2_determine_postprocessing = "nnunetv2.postprocessing.remove_connected_components:entry_point_determine_postprocessing_folder"
# 应用后处理步骤,例如去除预测结果中的小连通区域。
nnUNetv2_apply_postprocessing = "nnunetv2.postprocessing.remove_connected_components:entry_point_apply_postprocessing"
nnUNetv2_ensemble = "nnunetv2.ensembling.ensemble:entry_point_ensemble_folders"
nnUNetv2_accumulate_crossval_results = "nnunetv2.evaluation.find_best_configuration:accumulate_crossval_results_entry_point"
nnUNetv2_plot_overlay_pngs = "nnunetv2.utilities.overlay_plots:entry_point_generate_overlay"
nnUNetv2_download_pretrained_model_by_url = "nnunetv2.model_sharing.entry_points:download_by_url"
nnUNetv2_install_pretrained_model_from_zip = "nnunetv2.model_sharing.entry_points:install_from_zip_entry_point"
nnUNetv2_export_model_to_zip = "nnunetv2.model_sharing.entry_points:export_pretrained_model_entry"
nnUNetv2_move_plans_between_datasets = "nnunetv2.experiment_planning.plans_for_pretraining.move_plans_between_datasets:entry_point_move_plans_between_datasets"
nnUNetv2_evaluate_folder = "nnunetv2.evaluation.evaluate_predictions:evaluate_folder_entry_point"
nnUNetv2_evaluate_simple = "nnunetv2.evaluation.evaluate_predictions:evaluate_simple_entry_point"
nnUNetv2_convert_MSD_dataset = "nnunetv2.dataset_conversion.convert_MSD_dataset:entry_point"
③所有 nnU-Net 命令都有一个"-h"选项,用于提供如何使用它们的信息。
方法一
pip install nnunetv2
解释:这是一种简单直接的安装方式,通过 pip
命令从 Python 包索引(通常是 PyPI)下载并安装 nnUNetv2
包。这种方式安装的是已经打包好的稳定版本,你可以直接使用其中提供的标准化基准、分割算法,或者利用预训练模型进行推理任务。你无需关心代码的具体实现细节,就像使用一个现成的工具一样。
适用场景:如果你只是想快速使用 nnUNetv2
来完成一些分割任务,比如对一些医学图像进行分割,或者验证某个数据集在 nnUNetv2
上的表现,而不需要对代码进行修改和定制,那么这种安装方式就很合适。
方法二
git clone https://github.com/MIC-DKFZ/nnUNet.git
cd nnUNet
pip install -e .
解释:这种方式首先使用 git clone
命令从 GitHub 仓库克隆 nnUNet
的源代码到本地计算机,然后进入克隆下来的代码目录,最后使用 pip install -e .
命令进行安装。其中,-e
选项表示以 “可编辑模式” 安装,这意味着你对本地代码所做的任何修改都会立即生效,无需重新安装。通过这种方式,你可以在本地拥有一份完整的 nnUNet
代码副本,方便你对代码进行修改、调试和扩展。
适用场景:如果你希望深入学习 nnUNetv2
的算法原理,对代码进行定制化开发,例如修改模型结构、调整训练策略、添加新的功能等,那么这种安装方式就更适合你。
这里使用第二种方式
root@autodl-container-7f114d8374-8fa873f5:~# git clone https://github.com/MIC-DKFZ/nnUNet.git
Cloning into 'nnUNet'...
remote: Enumerating objects: 13663, done.
remote: Counting objects: 100% (890/890), done.
remote: Compressing objects: 100% (347/347), done.
remote: Total 13663 (delta 635), reused 747 (delta 543), pack-reused 12773 (from 3)
Receiving objects: 100% (13663/13663), 7.96 MiB | 9.36 MiB/s, done.
Resolving deltas: 100% (10404/10404), done.
发现这就是一个下载源码的指令……我之前还在官网下载了之后传到autodl……多此一举了。
root@autodl-container-7f114d8374-8fa873f5:~# cd nnUNet
root@autodl-container-7f114d8374-8fa873f5:~/nnUNet# pip install -e .……一些安装显示
先执行 bash
命令“cd nnUNet
”,用于切换到名为 nnUNet
的目录。(在执行 git clone
命令后,你需要进入下载的 nnUNet
代码目录,以便后续对该项目进行操作),再执行pip
命令
pip install -e .
-e
选项表示以 “可编辑模式”(也称为开发模式)安装项目。
.
表示当前目录(即 nnUNet
目录)。
这个命令的作用:
①会安装 nnUNet
项目及其所有依赖项(对于包含 setup.py
或 pyproject.toml
的项目)
又学到了一个配置项目环境的方法,但是前提是:项目是以 Python 编写;项目遵循 Python 包的标准结构;项目里有 setup.py
或 pyproject.toml
②将项目安装为可编辑模式(Editable Install),适合本地开发调试,安装后对本地代码所做的任何修改都会立即生效,而不需要重新安装整个包。
1.3. 设置环境变量
nnU-Net 依靠环境变量来确定原始数据、预处理数据以及训练好的模型权重的存储位置。
要充分使用 nnU-Net 的全部功能,必须设置三个环境变量:
①nnUNet_raw:放置原始数据集的位置。
②nnUNet_preprocessed:这是保存预处理数据的文件夹。
③nnUNet_results:保存模型权重的位置。如果下载了预训练模型,也会将其保存在这里。
设置环境变量分为永久设置和临时设置,这里选择永久设置。
在 Ubuntu 系统中,.bashrc
文件位于你的主目录(即你的用户目录)下。它是一个隐藏文件&