我有两组文件:掩模和图像。在'tensorflow'中没有tiff解码器,但有'tfio.experimental'。Tiff文件具有超过4个通道。
此代码无效:
import numpy as np
import tiffile as tiff
import tensorflow as tf
for i in range(100):
a = np.random.random((30, 30, 8))
b = np.random.randint(10, size = (30, 30, 8))
tiff.imsave('new1//images'+str(i)+'.tif', a)
tiff.imsave('new2//images'+str(i)+'.tif', b)
import glob
paths1 = glob.glob('new1//*.*')
paths2 = glob.glob('new2//*.*')
def load(image_file, mask_file):
image = tf.io.read_file(image_file)
image = tfio.experimental.image.decode_tiff(image)
mask = tf.io.read_file(mask_file)
mask = tfio.experimental.image.decode_tiff(mask)
input_image = tf.cast(image, tf.float32)
mask_image = tf.cast(mask, tf.uint8)
return input_image, mask_image
AUTO = tf.data.experimental.AUTOTUNE
BATCH_SIZE = 32
dataloader = tf.data.Dataset.from_tensor_slices((paths1, paths2))
dataloader = (
dataloader
.shuffle(1024)
.map(load, num_parallel_calls=AUTO)
.batch(BATCH_SIZE)
.prefetch(AUTO)
)
无法将整个数据集保存在内存中,将其保存到numpy数组中也没有简单的解决方案。尽管上面提供的代码直接没有错误。但是图像的形状为(None,None,None)
'model.fit'会出错
是否有其他方法可以保存数组?我只看到了使用手动提供随机批次进行自定义训练的暴力解决方案。