io icon indicating copy to clipboard operation
io copied to clipboard

Error opening Multi-page Tiff Files

Open JVedant opened this issue 4 years ago • 6 comments

Hi, I am trying to load Tiff files using tf.data. The code is as follows

def parse_image(image_path) -> dict:
    
    image = tf.io.read_file(image_path)
    image = tfio.experimental.image.decode_tiff(image)
    return {'image': image}

The above code when executed with tf.data api as

images = glob.glob(DIRECTORY_PATH/*)
tf_ds = tf.data.Dataset.list_files(images)
tf_ds = tf_ds.map(parse_image)

Throws an error that says

InvalidArgumentError: unable to read directory: 0
	 [[{{node IO>DecodeTiff_1}}]] [Op:IteratorGetNext]
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-25-be6909196fd9> in <module>
----> 1 for each in train_ds:
      2     print(each.shape)
      3     break

~/miniconda3/envs/tf_2.5/lib/python3.8/site-packages/tensorflow/python/data/ops/iterator_ops.py in __next__(self)
    759   def __next__(self):
    760     try:
--> 761       return self._next_internal()
    762     except errors.OutOfRangeError:
    763       raise StopIteration

~/miniconda3/envs/tf_2.5/lib/python3.8/site-packages/tensorflow/python/data/ops/iterator_ops.py in _next_internal(self)
    742     # to communicate that there is no more data to iterate over.
    743     with context.execution_mode(context.SYNC):
--> 744       ret = gen_dataset_ops.iterator_get_next(
    745           self._iterator_resource,
    746           output_types=self._flat_output_types,

~/miniconda3/envs/tf_2.5/lib/python3.8/site-packages/tensorflow/python/ops/gen_dataset_ops.py in iterator_get_next(iterator, output_types, output_shapes, name)
   2726       return _result
   2727     except _core._NotOkStatusException as e:
-> 2728       _ops.raise_from_not_ok_status(e, name)
   2729     except _core._FallbackException:
   2730       pass

~/miniconda3/envs/tf_2.5/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
   6895   message = e.message + (" name: " + name if name is not None else "")
   6896   # pylint: disable=protected-access
-> 6897   six.raise_from(core._status_to_exception(e.code, message), None)
   6898   # pylint: enable=protected-access
   6899 

~/.local/lib/python3.8/site-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: unable to read directory: 0
	 [[{{node IO>DecodeTiff_1}}]] [Op:IteratorGetNext]

What can be done to overcome this error ?

Note: The tiff image shape is (128, 128, 128, 4)

JVedant avatar Sep 09 '21 22:09 JVedant

@JVedant did you try reading only a single tiff file?

kvignesh1420 avatar Sep 10 '21 02:09 kvignesh1420

Hi @kvignesh1420, No I haven't. The files are originally saved in the shape (128, 128, 128, 4) so I'm not sure how to read single tiff files. How can we read a single file in such cases ??

Ps: by single files you mean 128 individual images of shape (128, 128, 4), right ?

JVedant avatar Sep 10 '21 03:09 JVedant

@JVedant for multipage tiff files. you can use the index parameter to select content at that index. Usage:

image = tfio.experimental.image.decode_tiff(tf.io.read_file(filename), index=i)

in the above case, i will vary from 0 to 127

kvignesh1420 avatar Sep 10 '21 05:09 kvignesh1420

Thanks for the response. So as per my understanding, I need to read the image as 128 different images using index param and then concat them to generate a tensor of shape (h, w, d, c) and then feed that into a 3D Neural network model.

feel free to let me know if I got it wrong.

JVedant avatar Sep 10 '21 06:09 JVedant

@JVedant for multipage tiff files. you can use the index parameter to select content at that index. Usage:

image = tfio.experimental.image.decode_tiff(tf.io.read_file(filename), index=i)

in the above case, i will vary from 0 to 127

@kvignesh1420, it's still showing the same error:

InvalidArgumentError: unable to read directory: 8
	 [[{{node IO>DecodeTiff_8}}]] [Op:IteratorGetNext]
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-35-55c8041be3e3> in <module>
----> 1 for each in train_ds:
      2     print(each['image'].shape)
      3     break

~/miniconda3/envs/tf_2.5/lib/python3.8/site-packages/tensorflow/python/data/ops/iterator_ops.py in __next__(self)
    759   def __next__(self):
    760     try:
--> 761       return self._next_internal()
    762     except errors.OutOfRangeError:
    763       raise StopIteration

~/miniconda3/envs/tf_2.5/lib/python3.8/site-packages/tensorflow/python/data/ops/iterator_ops.py in _next_internal(self)
    742     # to communicate that there is no more data to iterate over.
    743     with context.execution_mode(context.SYNC):
--> 744       ret = gen_dataset_ops.iterator_get_next(
    745           self._iterator_resource,
    746           output_types=self._flat_output_types,

~/miniconda3/envs/tf_2.5/lib/python3.8/site-packages/tensorflow/python/ops/gen_dataset_ops.py in iterator_get_next(iterator, output_types, output_shapes, name)
   2726       return _result
   2727     except _core._NotOkStatusException as e:
-> 2728       _ops.raise_from_not_ok_status(e, name)
   2729     except _core._FallbackException:
   2730       pass

~/miniconda3/envs/tf_2.5/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
   6895   message = e.message + (" name: " + name if name is not None else "")
   6896   # pylint: disable=protected-access
-> 6897   six.raise_from(core._status_to_exception(e.code, message), None)
   6898   # pylint: enable=protected-access
   6899 

~/.local/lib/python3.8/site-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: unable to read directory: 8
	 [[{{node IO>DecodeTiff_8}}]] [Op:IteratorGetNext]

Also, the number in InvalidArgumentError: unable to read directory: 8 changes every time I run the code on same tiff file.

Ps: I'm trying to load an image somewhat similar to https://keras.io/examples/vision/3D_image_classification/

JVedant avatar Sep 10 '21 11:09 JVedant

Any solution to this issue? I'm having the same issue trying to read TIFF images.

pwernette avatar Jul 26 '22 00:07 pwernette