Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

python - How to use tf.Dataset with TIFF files in image segmentation?

I have two sets of files: masks and images. There is no tiff decoder in 'tensorflow', but there is 'tfio.experimental'. Tiff files have more than 4 channels.

this code doesnt work:

    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)
    )

it is impossible to keep entire dataset in the memory, saving to numpy arrays also gives no easy solution. Although code provided above gives no error directly. But shape of images is (None, None, None)

'model.fit' gives error

Is there alternative way to save arrays? I only see bruteforce solution with manual feeding random batches during custom training.

question from:https://stackoverflow.com/questions/65944224/how-to-use-tf-dataset-with-tiff-files-in-image-segmentation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found solution for my question: DataGenerator allows to work with any files

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])

It works anyway without any problem


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...