[Keras] 3D UNet源码解析之train.py

作者代码:zishang33/3DUnetCNN
本文主要解析train.py的各个部分的作用.

main函数

从运行的过程分析,运行时要把config[“overwrite”] 改为true。
这里暂不分析模型调用load_old_model的情况。

第一块代码 数据读取

if overwrite or not os.path.exists(config["data_file"]):
    training_files = fetch_training_data_files()
    write_data_to_file(training_files, config["data_file"], image_shape=config["image_shape"])
data_file_opened = open_data_file(config["data_file"])

一行行分析

training_files = fetch_training_data_files()

config[“overwrite”] = True在main中调用调用函数fetch_training_data_files
把所有nii文件的路径都保存在training_files里
看下面的fetch_training_data_files()详解

training_files包含训练数据文件的元组tuple列表。 在每个元组tuple中,几种模式应该以相同的顺序列出。 每个元组中的最后一项必须是带标签的图像(truth)。
例如:
[(‘sub1-T1.nii.gz’, ‘sub1-T2.nii.gz’, ‘sub1-truth.nii.gz’),
(‘sub2-T1.nii.gz’, ‘sub2-T2.nii.gz’, ‘sub2-truth.nii.gz’)]

write_data_to_file(training_files, config["data_file"], image_shape=config["image_shape"])

write_data_to_file 功能是接收一组训练图像并将这些图像写入hdf5文件,在data文件中具体分析

config[“data_file”]是要将hdf5文件写入的位置。其定义为

config["data_file"] = os.path.abspath("brats_data.h5")#返回绝对路径
data_file_opened = open_data_file(config["data_file"])

最后用函数open_data_file()读取table文件的数据.

第二块代码 产生数据生成器

train_generator, validation_generator, n_train_steps, n_validation_steps = get_training_and_validation_generators(
    data_file_opened,
    batch_size=config["batch_size"],
    data_split=config["validation_split"],
    overwrite=overwrite,
    validation_keys_file=config["validation_file"],
    training_keys_file=config["training_file"],
    n_labels=config["n_labels"],
    labels=config["labels"],
    patch_shape=config["patch_shape"],
    validation_batch_size=config["validation_batch_size"],
    validation_patch_overlap=config["validation_patch_overlap"],
    training_patch_start_offset=config["training_patch_start_offset"],
    permute=config["permute"],
    augment=config["augment"],
    skip_blank=config["skip_blank"],
    augment_flip=config["flip"],
    augment_distortion_factor=config["distort"])

利用函数get_training_and_validation_generators()将训练数据和测试数据打包成Keras框架类型的输入数据。为以后网络训练的fit_generator函数做好准备,不了解fit_generator的童靴可以看我之前发过的博客。这个函数之后会在其定义的文件中具体分析。

第三块代码 训练网络

# run training
    train_model(model=model,
                model_file=config["model_file"],
                training_generator=train_generator,
                validation_generator=validation_generator,
                steps_per_epoch=n_train_steps,
                validation_steps=n_validation_steps,
                initial_learning_rate=config["initial_learning_rate"],
                learning_rate_drop=config["learning_rate_drop"],
                learning_rate_patience=config["patience"],
                early_stopping_patience=config["early_stop"],
                n_epochs=config["n_epochs"])

这一块其实没什么,train_model函数其实就是调用fit_generator函数来完成我们的网络训练,其中model的定义为

model = unet_model_3d(input_shape=config["input_shape"],
                          pool_size=config["pool_size"],
                          n_labels=config["n_labels"],
                          initial_learning_rate=config["initial_learning_rate"],
                          deconvolution=config["deconvolution"])

fetch_training_data_files

返回所有preprocessed里所有nii文件的路径


一些文件处理函数

  1. glob 文件名模式匹配,不用遍历整个目录判断每个文件是不是符合。
  import glob
  #用子目录查询文件
  print ('Named explicitly:')
  for name in glob.glob('dir/subdir/*'):
      print ('\t', name)
  #用通配符* 代替子目录名
  print ('Named with wildcard:')
  for name in glob.glob('dir/*/*'):
      print ('\t', name)

  #输出

  Named explicitly:
          dir/subdir/subfile.txt
  Named with wildcard:
          dir/subdir/subfile.txt
  1. os.path.join()函数:连接两个或更多的路径名组件
    import os

    Path1 = 'home'
    Path2 = 'develop'
    Path3 = 'code'

    Path10 = Path1 + Path2 + Path3
    Path20 = os.path.join(Path1,Path2,Path3)
    print ('Path10 = ',Path10)
    print ('Path20 = ',Path20)

    #输出

    Path10 = homedevelopcode
    Path20 = home\develop\code
  1. os.path.dirname(path)功能:去掉文件名,返回目录
   __file__表示了当前文件的path
   os.path.dirname((__file__)就是得到当前文件的绝对路径

函数具体分析

training_data_files = list()

创建一个list来保存所有要处理的nii文件的路径

for subject_dir in glob.glob(os.path.join(os.path.dirname(__file__), "data", "preprocessed", "*", "*")):

先用os.path.dirname返回当前文件(夹)的绝对路径,再用join把他和data,preprocessed连接起来,最后用glob寻找其下的所有图像文件夹,并循环遍历

subject_files = list()

创建一个子文件路径来保存preprocessed文件夹下各个图像文件夹里的nii文件的路径

config["all_modalities"] = ["t1", "t1ce", "flair", "t2"]
config["training_modalities"] = config["all_modalities"]

训练集的四种数据

for modality in config["training_modalities"] + ["truth"]:

相当于preprocessed的图像文件夹里nii的五种形式:“t1”, “t1ce”, “flair”, “t2”,“truth”

subject_files.append(os.path.join(subject_dir, modality + ".nii.gz"))

纪录这五种nii文件的路径

training_data_files.append(tuple(subject_files))

把所有的路径都加到training_data_files里并返回

函数输出分析

尝试运行fetch_training_data_files并观察其输出,这里我只截取了部分preprocessed文件来观察。

training_files = fetch_training_data_files()
print(training_files)

输出

[('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0006/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0006/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0006/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0006/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0006/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0033/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0033/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0033/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0033/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0033/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0027/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0027/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0027/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0027/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0027/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0009/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0009/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0009/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0009/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0009/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0011/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0011/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0011/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0011/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0011/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0069/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0069/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0069/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0069/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0069/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0034/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0034/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0034/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0034/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0034/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0064/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0064/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0064/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0064/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0064/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0047/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0047/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0047/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0047/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0047/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0046/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0046/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0046/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0046/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0046/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0037/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0037/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0037/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0037/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0037/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0059/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0059/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0059/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0059/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0059/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0068/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0068/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0068/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0068/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0068/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0070/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0070/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0070/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0070/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0070/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0075/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0075/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0075/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0075/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0075/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5393/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5393/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5393/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5393/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5393/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5397/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5397/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5397/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5397/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5397/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-4942/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-4942/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-4942/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-4942/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-4942/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6188/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6188/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6188/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6188/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6188/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5396/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5396/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5396/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5396/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-5396/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6666/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6666/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6666/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6666/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6666/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6668/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6668/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6668/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6668/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6668/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6665/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6665/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6665/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6665/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6665/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-4944/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-4944/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-4944/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-4944/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-4944/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6186/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6186/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6186/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6186/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6186/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5851/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5851/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5851/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5851/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5851/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5855/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5855/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5855/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5855/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5855/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6669/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6669/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6669/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6669/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-CS-6669/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5854/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5854/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5854/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5854/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5854/truth.nii.gz'), ('data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5872/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5872/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5872/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5872/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_LGG_NIfTI_and_Segmentations/TCGA-DU-5872/truth.nii.gz')]

我们选择其中一个元组来看

('data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0006/t1.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0006/t1ce.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0006/flair.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0006/t2.nii.gz', 'data/preprocessed/Pre-operative_TCGA_GBM_NIfTI_and_Segmentations/TCGA-02-0006/truth.nii.gz')

可见路径都是按t1,tice,flair,t2,truth顺序来排列的
而其总长度(list中元组的个数)为preprocessed下要处理的图像文件夹总个数

print(type(training_files))
print(len(training_files))
<class 'list'>
30
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值