我正在测试tf.data(),这是目前推荐的批量输入数据的方法, 但是,我正在加载一个自定义数据集,因此需要“str”格式的文件名。但是当从张量切片创建tf.Dataset.from_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_函数”包装器,但没有效果。 有什么想法吗
解决了TensorFlow 2.1中的问题:
相关问题 更多 >
编程相关推荐