天天看點

基于NiftyNet訓練自己的資料1. 環境準備2. 訓練

1. 環境準備

參考官方文檔:https://niftynet.readthedocs.io/en/dev/installation.html

# 建立 conda 環境
conda create -n tensorflow-gpu python=3.6
# 安裝 tensorflow
pip install tensorflow-gpu==1.10.0
# 安裝 niftynet
git clone https://github.com/NifTK/NiftyNet.git
# 安裝依賴
cd NiftyNet/
pip install -r requirements-gpu.txt
           

2. 訓練

2.1 資料準備

将訓練資料放在

home/niftynet/data/my_test

目錄下,資料格式為 nii,訓練和測試資料分别打包為一個 nii 檔案。

注意檔案名中的關鍵詞,在後面的配置檔案中需要設定

2.2 修改配置檔案

參考官方文檔:https://niftynet.readthedocs.io/en/dev/config_spec.html#configuration-sections

~/niftynet/extensions/

下建立檔案夾

mytest

,複制

dense_vnet_abdominal_ct

下的配置檔案

config.ini

到該檔案夾,根據自己的情況進行修改。

配置檔案

config.ini

至少需要包含兩部分:

[SYSTEM]

[NETWORK]

,訓練和測試階段分别對應

[TRAINING]

[INFERENCE]

,對于分割任務還需要包含

[SEGMENTATION]

以 dense v-net 的配置檔案為例:

############################ input configuration sections
# 用于指定訓練圖像
[ct]
# 訓練圖像路徑
path_to_search = ./data/dense_vnet_abdominal_ct/
# 用于比對檔案名的關鍵字,在訓練時此關鍵字将被删除
filename_contains = CT
# 輸入大小(H,W,img_num)
spatial_window_size = (144, 144, 144)
# 輸入資料的插值順序,1為線性插值
interp_order = 1
# 圖像讀入記憶體轉換為 A R S 方向
axcodes=(A, R, S)

[label]
# 訓練對象對應的mask
path_to_search = ./data/dense_vnet_abdominal_ct/
filename_contains = Label
spatial_window_size = (144, 144, 144)
interp_order = 0
axcodes=(A, R, S)

############################## system configuration sections
[SYSTEM]
# 設定使用的 GPU
cuda_devices = ""
# 預處理線程數
num_threads = 1
# 可用 GPU 數量
num_gpus = 1
# 加載模型的目錄,預設是目前路徑
model_dir = models/dense_vnet_abdominal_ct
queue_length = 36

[NETWORK]
# 網絡名稱,設定為 niftynet.network.toynet.ToyNet則會導入 ToyNet類
name = dense_vnet
# batch size 1 for inference
# batch size 6 for training
batch_size = 1

# volume level preprocessing
volume_padding_size = 0
# 将圖檔 resize 到 window size
window_sampling = resize

[TRAINING]
sample_per_volume = 1
lr = 0.001
loss_type = dense_vnet_abdominal_ct.dice_hinge.dice
starting_iter = 0
# 每訓練多少次儲存一次模型
save_every_n = 1000
# 最大疊代次數
max_iter = 3001

[INFERENCE]
# 裁剪網絡的輸出大小
border = (0, 0, 0)
# 指定用于測試的模型
inference_iter = 3000
# 網絡輸出的插值循序
output_interp_order = 0
spatial_window_size = (144, 144, 144)
# 測試結果儲存路徑
save_seg_dir = ./segmentation_output/

############################ custom configuration sections
[SEGMENTATION]
image = ct
label = label
label_normalisation = False
output_prob = False
num_classes = 9
           

2.3 訓練

參考官方文檔:https://niftynet.readthedocs.io/en/dev/config_spec.html#overview

訓練指令:

# command to run from git-cloned NiftyNet source code folder
python net_run.py train -c <path_to/config.ini> -a <application>
           

其中,

<path_to/config.ini>

是配置檔案的路徑,

<application>

内容格式為 user.path.python.module.MyApplication,效果是導入 user/path/python/module.py 檔案下的 MyApplication 類。

現成兒可以用的在

./niftynet/application/

目錄下,這裡使用圖像分割為例:

python net_run.py train -c <path_to/config.ini> -a niftynet.application.segmentation_application.SegmentationApplication 
           

舉個例子:

python net_run.py train -c /home/tzq-zyy/niftynet/extensions/mytest/config.ini -a niftynet.application.segmentation_application.SegmentationApplication
           

訓練正常的話,終端輸出如下:

dice[0.177198395 0.103725933 0.0715316236 0.0127105657 0.00791870896 0.100049123 0.100663938 0.0575987399 0.0468616374]
INFO:niftynet: training iter 1, total_loss=3.3650364875793457, loss=3.3650364875793457 (30.331078s)
dice[0.199683234 0.0769364685 0.128822058 0.0285055339 0.0270173885 0.0912262872 0.0768199712 0.143819273 0.0744452626]
INFO:niftynet: training iter 2, total_loss=2.26554536819458, loss=2.26554536819458 (11.347821s)
dice[0.243362054 0.177675933 0.108794935 0.0613077283 0.050180003 0.0818266049 0.199541628 0.152265966 0.0877473578]
INFO:niftynet: training iter 3, total_loss=1.3663134574890137, loss=1.3663134574890137 (11.688515s)
dice[0.294665605 0.201383099 0.116870329 0.16179 0.102572314 0.168376252 0.15632163 0.154163659 0.0982201174]
INFO:niftynet: training iter 4, total_loss=0.8387561440467834, loss=0.8387561440467834 (10.815102s)
dice[0.334134877 0.209191054 0.114639401 0.173099115 0.101677962 0.242945343 0.135729268 0.146350488 0.101767704]
INFO:niftynet: training iter 5, total_loss=0.8267183303833008, loss=0.8267183303833008 (11.016553s)
...
           

訓練過程中儲存的 model 儲存在

model_dir/models

下。

繼續閱讀