【问题标题】:How to use from_tensor_slices properly on MRI images?如何在 MRI 图像上正确使用 from_tensor_slices?
【发布时间】:2022-01-04 13:09:20
【问题描述】:

我正在处理 MRI 图像,我想使用 from_tensor_slices 来预处理路径,但我不知道如何正确使用它。下面是我的代码、问题信息和数据集的链接。

首先我重新排列我的数据。 484 张图片和 484 个标签

image_data_path = './drive/MyDrive/Brain Tumour/Task01_BrainTumour/imagesTr/'
label_data_path = './drive/MyDrive/Brain Tumour/Task01_BrainTumour/labelsTr/'

image_paths = [image_data_path + name 
               for name in os.listdir(image_data_path) 
               if not name.startswith(".")]

label_paths = [label_data_path + name
               for name in os.listdir(label_data_path)
               if not name.startswith(".")]

image_paths = sorted(image_paths)
label_paths = sorted(label_paths)

然后,加载1个例子的函数(我使用nibabel加载nii文件)

def load_one_sample(image_path, label_path):

  image = nib.load(image_path).get_fdata()
  image = tf.convert_to_tensor(image, dtype = 'float32')
  label = nib.load(label_path).get_fdata()
  label = tf.convert_to_tensor(label, dtype = 'uint8')

  return image, label

接下来,我尝试使用 from_tensor_slices

image_filenames = tf.constant(image_paths)
label_filenames = tf.constant(label_paths)

dataset = tf.data.Dataset.from_tensor_slices((image_filenames, label_filenames))

all_data = dataset.map(load_one_sample)

错误来了:TypeError: stat: path should be string, bytes, os.PathLike or integer, not Tensor

可能出了什么问题,我该如何解决?

数据链路:https://drive.google.com/drive/folders/1HqEgzS8BV2c7xYNrZdEAnrHk7osJJ--2(任务 1 - 脑肿瘤)

如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: python tensorflow image-segmentation image-preprocessing


    【解决方案1】:

    nib.load 不是 TensorFlow 函数。
    如果您想在 tf.data 管道中使用不是 TensorFlow 函数的任何内容,则必须使用 tf.py_function 包装它。
    代码:

    image_data_path = 'Task01_BrainTumour/imagesTr/'
    label_data_path = 'Task01_BrainTumour/labelsTr/'
    
    image_paths = [image_data_path + name 
                   for name in os.listdir(image_data_path) 
                   if not name.startswith(".")]
    label_paths = [label_data_path + name
                   for name in os.listdir(label_data_path)
                   if not name.startswith(".")]
    
    image_paths = sorted(image_paths)
    label_paths = sorted(label_paths)
    
    def load_one_sample(image_path, label_path):
      image = nib.load(image_path.numpy().decode()).get_fdata()
      image = tf.convert_to_tensor(image, dtype = 'float32')
      label = nib.load(label_path.numpy().decode()).get_fdata()
      label = tf.convert_to_tensor(label, dtype = 'uint8')
      return image, label
    
    def wrapper_load(img_path, label_path):
      img, label = tf.py_function(func = load_one_sample, inp = [img_path, label_path], Tout = [tf.float32, tf.uint8])
      return img, label
    
    dataset = tf.data.Dataset.from_tensor_slices((image_paths, label_paths)).map(wrapper_load)
    

    错误不是由于 from_tensor_slices 函数引起的,而是由于 nibs.load 期待一个字符串但得到一个张量而出现。

    但是,更好的方法是创建 tfrecord 并使用它们来训练模型。

    【讨论】:

    • 谢谢,wrapper_load 函数有效。关于 TFRecords,我正在查看本指南 tensorflow.org/tutorials/load_data/tfrecord,但大部分内容我都不了解。你知道我还能在哪里阅读它吗?
    • 我认为这是 tfrecords 的最佳指南。
    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    相关资源
    最近更新 更多