gpt4 book ai didi

python - 如何在图像分割中将 tf.Dataset 与 TIFF 文件一起使用?

转载 作者:行者123 更新时间:2023-12-04 14:09:07 25 4
gpt4 key购买 nike

我有两组文件:掩码和图像。 “tensorflow”中没有 tiff 解码器,但有“tfio.experimental”。 Tiff 文件有 4 个以上的 channel 。
此代码不起作用:

    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 数组也没有简单的解决方案。虽然上面提供的代码没有直接给出错误。但是图像的形状是(无,无,无)
'model.fit' 给出错误
有没有其他方法来保存数组?我只看到在自定义训练期间手动提供随机批次的蛮力解决方案。

最佳答案

我为我的问题找到了解决方案:DataGenerator 允许使用任何文件

class Gen(tf.keras.utils.Sequence):

def __init__(self, x_set, y_set, batch_size):
self.x, self.y = x_set, y_set
self.batch_size = batch_size

def __len__(self):
return math.ceil(len(self.x) / self.batch_size)

def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) *
self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) *
self.batch_size]

return np.array([
tiff.imread(file_name_x)
for file_name_x in batch_x]), np.array([
tiff.imread(file_name_y)
for file_name_y in batch_y])
无论如何它都可以正常工作

关于python - 如何在图像分割中将 tf.Dataset 与 TIFF 文件一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65944224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com