【问题标题】:How to convert Tensor to string如何将张量转换为字符串
【发布时间】:2023-03-04 15:26:01
【问题描述】:

我正在测试 tf.data(),这是推荐的批量输入数据的方式, 但是,我正在加载自定义数据集,因此我需要“str”格式的文件名。但是在创建 tf.Dataset.from_tensor_slices 时,它们是 Tensor 对象。

def load_image(file, label):
        nifti = np.asarray(nibabel.load(file).get_fdata()) # <- here is the problem

        xs, ys, zs = np.where(nifti != 0)
        nifti = nifti[min(xs):max(xs) + 1, min(ys):max(ys) + 1, min(zs):max(zs) + 1]
        nifti = nifti[0:100, 0:100, 0:100]
        nifti = np.reshape(nifti, (100, 100, 100, 1))
        nifti = tf.convert_to_tensor(nifti, np.float32)
        return nifti, label


    def load_image_wrapper(file, labels):
       file = tf.py_function(load_image, [file, labels], (tf.string, tf.int32))
       return file


    dataset = tf.data.Dataset.from_tensor_slices((train, labels))
    dataset = dataset.map(load_image_wrapper, num_parallel_calls=6)
    dataset = dataset.batch(6)
    dataset = dataset.prefetch(buffer_size=6)
    iterator = iter(dataset)
    batch_of_images = iterator.get_next()

这是错误: typeerror expected str bytes or os.pathlike object not Tensor

我尝试使用 'py_function' 包装器,但无济于事。 有什么想法吗?

【问题讨论】:

  • 哪一行导致错误?
  • line 2: nifti = np.asarray(nibabel.load(file).get_fdata()) #

标签: python tensorflow tensorflow-datasets


【解决方案1】:

解决了问题,TensorFlow 2.1:

    def load_image(file, label):
    nifti = np.asarray(nibabel.load(file.numpy().decode('utf-8')).get_fdata())

    xs, ys, zs = np.where(nifti != 0)
    nifti = nifti[min(xs):max(xs) + 1, min(ys):max(ys) + 1, min(zs):max(zs) + 1]
    nifti = nifti[0:100, 0:100, 0:100]
    nifti = np.reshape(nifti, (100, 100, 100, 1))
    nifti = tf.convert_to_tensor(nifti, np.float64)
    return nifti, label


def load_image_wrapper(file, labels):
    return tf.py_function(load_image, [file, labels], [tf.float64, tf.float64])


dataset = tf.data.Dataset.from_tensor_slices((train, labels))
dataset = dataset.map(load_image_wrapper, num_parallel_calls=6)
dataset = dataset.batch(2)
dataset = dataset.prefetch(buffer_size=2)
iterator = iter(dataset)
batch_of_images = iterator.get_next()

【讨论】:

    猜你喜欢
    • 2018-08-04
    • 2020-12-31
    • 1970-01-01
    • 2022-06-16
    • 2023-01-23
    • 2017-11-20
    • 2021-01-04
    • 2019-12-11
    • 1970-01-01
    相关资源
    最近更新 更多