9

I have noticed that Tensorflow provides standard procedures for decoding jpeg, png and gif images after reading files. For instance for png:

import tensorflow as tf
filename_queue = tf.train.string_input_producer(['/Image.png']) #  list of files to read
reader = tf.WholeFileReader()    
key, value = reader.read(filename_queue)
decoded_image = tf.image.decode_png(value) # use png or jpg decoder based on your files.

However, the tiff format decoder seems to be missing.

So what solutions exist for tiff files? Surely, I could convert my input images to png, but this doesn't seem to be a very smart solution.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Dr_Zaszuś
  • 546
  • 1
  • 7
  • 15
  • Hi, I am currently facing the same issue. Which method did you end up using? Writing your own TIFF format decoder or simply converting your files to a supported format e.g. PNG? Thanks! – jlhw Sep 25 '17 at 16:39
  • 1
    @jlhw Hi, At the end of the day I am loading the images myself in Python (through standard Python image libraries) and then feed them as tensors to TensorFlow. It has the advantage that it is easy to pre-process the images the way you want with numpy or pillow in Python before the actual network training. My images are large, but few, so I only need to pre-load them once, and it's not a bottleneck of the process. Hope this helps. – Dr_Zaszuś Oct 17 '17 at 16:50

3 Answers3

9

There's currently no decoder for TIFF images. Look in tensorflow/core/kernels and you see

decode_csv_op.cc
decode_gif_op.cc
decode_jpeg_op.cc
decode_png_op.cc
decode_raw_op.cc

No decode_tiff_op.cc. This could be a good target for community contribution.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197
3

As of February 2019, some (limited & experimental) TIFF support has been added as part of the Tensorflow I/O library:

Added a very preliminary TIFF support. TIFF format is rather complex so compressions such as JPEG have not been supported yet, but could be added if needed.

The following methods are currently available:

tfio.experimental.image.decode_tiff

Decode a TIFF-encoded image to a uint8 tensor.

tfio.experimental.image.decode_tiff_info

Decode a TIFF-encoded image meta data.

An example usage from a Tensorflow tutorial:

import tensorflow as tf
import tensorflow.io as tfio
...
def parse_image(img_path: str) -> dict:
...
image = tf.io.read_file(img_path)
tfio.experimental.image.decode_tiff(image)
...
desertnaut
  • 57,590
  • 26
  • 140
  • 166
0

If tf.experimental.image.decode_tiff() won't work for you (as it won't work with my 32-bit TIFF files), you could try using cv2 as described in the answer to this post.

Other options are to use the .map() function with (a) rasterio, (b) skimage, or (c) pillow packages.