nnUnet介紹
本文主要解決拿來就用的問題,具體nnUet技術(shù)細節(jié)可以查看原文,代碼,以及一些介紹的文章。
nnUnet Docker
- 必須是要linux環(huán)境,不推薦windows
- 習(xí)慣用ubuntu,注意支持的版本最高是20.04。
推薦用rufus制作U盤啟動,選擇ubuntu 20.04光盤。部分筆記本安裝ubuntu 20.04,網(wǎng)卡驅(qū)動無法識別,需要更新內(nèi)核。下載新版內(nèi)核保存,用sudo dpkg -i * 安裝后重啟就可以了。 - 安裝Nvidia驅(qū)動,使用軟件和更新->附加驅(qū)動,選擇高版本驅(qū)動即可。不需要安裝
ubuntu-drivers devices
sudo apt-get remove --purge nvidia*
sudo apt install nvidia-driver-510
使用帶有 cuda 環(huán)境的 docker 容器,需要安裝 nvidia-docker 組件。
sudo apt-get install curl
curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun
sudo systemctl enable docker
sudo systemctl start docker
sudo usermod -aG docker $USER
distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
&& curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - \
&& curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update
sudo apt-get install nvidia-docker2
sudo systemctl restart docker
- 測試CUDA是否能夠運行
sudo docker run --rm --gpus all nvidia/cuda:11.0-base nvidia-smi
- docker nnUnet
從nnUNet官網(wǎng)下載最新代碼 MIC-DKFZ/nnUNet
進入nnUNet代碼文件夾,創(chuàng)建空的Dockerfile文件
touch Dockerfile
在Dockerfile中復(fù)制如下內(nèi)容
FROM nvcr.io/nvidia/pytorch:21.10-py3
RUN apt-get update && apt-get install -y --no-install-recommends \
python3-pip \
python3-setuptools \
build-essential \
&& \
apt-get clean && \
python -m pip install --upgrade pip
WORKDIR /workspace
COPY ./ /workspace
RUN pip install pip -U && pip config set global.index-url https://pypi.mirrors.ustc.edu.cn/simple/
#RUN git clone https://github.com/MIC-DKFZ/nnUNet.git
#RUN cd nnUNet && pip install -e .
RUN pip install -e .
RUN pip install --upgrade git+https://github.com/FabianIsensee/hiddenlayer.git@more_plotted_details#egg=hiddenlayer
#RUN pip install --upgrade git+https://github.com/FabianIsensee/hiddenlayer.git@more_plotted_details#egg=hiddenlayer
ENV nnUNet_raw_data_base="/workspace/data/"
ENV nnUNet_preprocessed="/workspace/data/nnUNet_preprocessed"
ENV RESULTS_FOLDER="/workspace/data/RESULTS_FOLDER"
docker build -t nnunet_docker .
docker container run --ipc=host -it --rm --gpus "device=0" --name nnunet -v $HOME/nnUNet-master/data:/workspace/data nnunet_docker /bin/bash
docker container run --ipc=host -it --rm --gpus "device=0" --name nnunet -v $HOME/data:/workspace/data nnunet_docker /bin/bash
docker container run -d --ipc=host -it -p 8889:8888 --gpus "device=0" --name nnunet -v $HOME/work:/workspace/data nnunet_docker /bin/bash -c 'jupyter notebook'
docker container run -d --ipc=host -it -p 8889:8888 --net nlink --ip 172.18.0.24 --gpus "device=0" --name nnunet -v $HOME/work:/workspace/data nnunet_docker /bin/bash -c 'jupyter notebook --no-browser --ip="*" '
docker container run -d --name science --ipc=host -it -p 8889:8888 --gpus "device=0" -v $HOME/work:/workspace/data nvcr.io/nvidia/pytorch:21.10-py3 /bin/bash -c 'jupyter notebook'
nvidia-docker run --rm -it --gpus all nvcr.io/nvidia/pytorch:21.10-py3 /bin/bash
docker run --ipc=host -it --rm -v $HOME/work:/tf/notebooks -p 8899:8888 tensorflow/tensorflow:latest-jupyter
docker run --ipc=host -it -d --name tensorflow -v $HOME/work:/tf/notebooks -p 8899:8888 --gpus "device=0" tensorflow/tensorflow:latest-jupyter
docker run --runtime nvidia --rm -it gcr.io/kaggle-gpu-images/python /bin/bash
docker run --ipc=host -it -d --name kaggle -v $HOME/work:/tf/notebooks -p 8889:8888 --gpus "device=0" --runtime nvidia - gcr.io/kaggle-gpu-images/python
本地數(shù)據(jù)準備
首先按照官方教程熟悉數(shù)據(jù)結(jié)構(gòu)和訓(xùn)練流程,按照教程《Example: 3D U-Net training on the Hippocampus dataset》來依樣畫葫蘆
run a training with the 3d full resolution U-Net on the Hippocampus dataset. See here.
數(shù)據(jù)構(gòu)建
設(shè)置目錄Task91_Innerear,包括子目錄 'imagesTr', 'labelsTr', 'imagesTs' ,其中l(wèi)abelsTr保存標簽文件,imagesTr保存標簽對應(yīng)原始數(shù)據(jù),imagesTs保存其他原始數(shù)據(jù)。
根據(jù)數(shù)據(jù)結(jié)構(gòu)要求,準備半規(guī)管標簽數(shù)據(jù),要求標簽和原始數(shù)據(jù)的shape保持一致,文件名稱按照規(guī)范,舉例:MR_12345.nii.gz
用3D Slicer軟件查看一下原始文件和標簽文件信息包括shape是否一致,標簽是否準確。
主要信息包括shape,Spacing,Origin,Direction。
nnUnet對于Direction的識別可能會有問題,可以將Origin和Direction信息歸零。
path_ci3d="/home/yakeworld/nnUNet-master/data/Task92_miniinnerear/labelsTr"
for PatientID in files:
ci3dFile=path_ci3dout+PatientID
itkimage = sitk.ReadImage(ci3dFile)
data = sitk.GetArrayFromImage(itkimage)
lab_img=sitk.GetImageFromArray(data)
lab_img.SetSpacing(itkimage.GetSpacing())
#lab_img.SetOrigin(itkimage.GetOrigin())
#lab_img.SetOrigin(itkimage.GetOrigin())
lab_img.SetDirection((-1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0))
sitk.WriteImage(lab_img, './dicom_out/' + PatientID + '.gz')
本地數(shù)據(jù)訓(xùn)練
1.數(shù)據(jù)轉(zhuǎn)換
nnUNet_convert_decathlon_task -i Task91_innerear
轉(zhuǎn)換后的數(shù)據(jù)保存在
data/nnUNet_raw_data 目錄下,主要是原始數(shù)據(jù)的文件名從MR_12345.nii.gz改為MR_12345_0000.nii.gz形式。
注:MRI支持多模態(tài)數(shù)據(jù)
2. 預(yù)處理
You can now run nnU-Nets pipeline configuration (and the preprocessing) with the following line:
nnUNet_plan_and_preprocess -t 91 --verify_dataset_integrity
91代表innerear數(shù)據(jù)任務(wù)id
3. 數(shù)據(jù)訓(xùn)練
3.1 3d_fullres
nnUNet_train 3d_fullres nnUNetTrainerV2 91 4
nnUNet_train 3d_fullres nnUNetTrainerV2 92 4
91代表你的任務(wù)ID,4代表五折交叉驗證(0代表一折)。所有的任務(wù)都應(yīng)當在“4”的情況下,也就是五折交叉驗證下進行。

3.2 3D U-Net cascade
nnUNet_train 2d nnUNetTrainerV2 91 4 --npz
nnUNet_train 3d_lowres nnUNetTrainerV2 91 4 --npz
nnUNet_train 3d_cascade_fullres nnUNetTrainerV2CascadeFullRes 91 4 --npz
3.3 殘差網(wǎng)絡(luò)
nnUNet_plan_and_preprocess -t 91
nnUNet_plan_and_preprocess -t 91 -pl2d None
nnUNet_plan_and_preprocess -t 91 -pl3d ExperimentPlanner3DFabiansResUNet_v21 -pl2d None
nnUNet_train 3d_fullres nnUNetTrainerV2_ResencUNet 91 4 -p nnUNetPlans_FabiansResUNet_v2.1
3.4
nnUNet_find_best_configuration -m 2d 3d_fullres 3d_lowres 3d_cascade_fullres -t 91 --strict
3.5
nnUNet_predict -i nnUNet_raw_data/Task091_innerear/imagesTs -o OUTPUT_FOLDER -t 91 -m CONFIGURATION --save_npz
nnUNet_predict -i nnUNet_raw_data/Task091_innerear/imagesTs -o OUTPUT_FOLDER -t http://hostname:8888/?token=fb34fb50ebfa53746bd8d511f75d41bab07b60be89777de691 -m 3d_fullres -f 4
nnUNet_predict -i nnUNet_raw_data/Task091_innerear/imagesTs -o OUTPUT_FOLDER -t 91 -tr nnUNetTrainerV2 -ctr nnUNetTrainerV2_ResencUNet -m 3d_fullres -f 4 -chk model_best
nnUNet_train 3d_fullres nnUNetTrainerV2 91 3
nnUNet_predict -i nnUNet_raw_data/Task091_innerear/imagesTs -o OUTPUT_FOLDERMINI -t 92 -m 3d_fullres
nnUNet_find_best_configuration -m 2d 3d_fullres -t 91
nnUNet_predict -i nnUNet_raw_data/Task091_innerear/imagesTs -o OUTPUT_FOLDER -t 91 -m 3d_fullres
半規(guī)管分割任務(wù)
1.原始數(shù)據(jù)+標簽
對原始數(shù)據(jù)不做處理
2.預(yù)處理數(shù)據(jù)+標簽
對數(shù)據(jù)進行預(yù)處理,各向同性
3.crop數(shù)據(jù)+標簽
按照標簽crop原始數(shù)據(jù)進行訓(xùn)練。好像nnUnet原理就是這么干的。
效果還是可以的,周邊小組織需要清理。
數(shù)據(jù)下載
1.預(yù)訓(xùn)練模型
https://zenodo.org/record/3734294#.YYPv1rq-suU
2.數(shù)據(jù)集
MSD - Google 云端硬盤
task03: https://pan.baidu.com/s/1CReeZ6m2dl5ryxQtJvn8rA 提取碼: hino