1

I am working on an image classification project involving multi-image TIFF files. I have my current directory set up like this:

data
|
|__class1
|  |
|  |__ file1.tiff
|  |__ file2.tiff
|
|__class2
   |
   |__ file3.tiff
    ...

Each TIFF represents a multi-channel microscopy image (3 channels) so I would like to use all of them for learning.

I tried the following code for PNG files, with some success:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import preprocessing

keras.backend.clear_session()

image_size = (1280, 1024)
batch_size = 2

# Import images as a dataset
training = tf.keras.preprocessing.image_dataset_from_directory(
    "./data/",
    validation_split=0.1,
    color_mode = 'grayscale',
    subset="training",
    label_mode='int',
    seed=1337,
    image_size=image_size,
    batch_size=batch_size,
)

and I am getting the following error:

Traceback (most recent call last):
  File "/home/E627046/miniconda3/envs/cellimaging/lib/python3.8/site-packages/te                                                                             nsorflow/python/framework/op_def_library.py", line 517, in _apply_op_helper
    values = ops.convert_to_tensor(
  File "/home/E627046/miniconda3/envs/cellimaging/lib/python3.8/site-packages/te                                                                             nsorflow/python/profiler/trace.py", line 163, in wrapped
    return func(*args, **kwargs)
  File "/home/E627046/miniconda3/envs/cellimaging/lib/python3.8/site-packages/te                                                                             nsorflow/python/framework/ops.py", line 1507, in convert_to_tensor
    raise ValueError(
ValueError: Tensor conversion requested dtype string for Tensor with dtype float                                                                             32: <tf.Tensor 'args_0:0' shape=() dtype=float32>

I am more overall confused as to the best solution to load these TIFF files into python to start the classification pipeline. I debated whether to convert these TIFF files to PNG files, but I was not sure if that would work because these TIFF files are multi-imaged, which I am pretty sure is not supported by PNG. I would really like to preserve the dimensionality. Please let me know if there are any suggestions on how to approach this "TIFF stack" problem.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
niketp
  • 409
  • 1
  • 9
  • 20
  • 1
    There is currently only limited & experimental support of TIFF files by Tensorflow; see https://stackoverflow.com/a/67186083/4685471 – desertnaut Apr 20 '21 at 20:48
  • `tf.keras.utils.image_dataset_from_directory` Supports image formats: jpeg, png, bmp, gif. Animated gifs are truncated to the first frame. For Tiff files use `tfio.experimental.image.decode_tiff` . For information read Tensorflow doc on [TIFF file decoding](https://www.tensorflow.org/io/api_docs/python/tfio/experimental/image/decode_tiff). Thanks! –  Oct 04 '21 at 05:50

0 Answers0