Skip to content

tf.image.decode_image would be nice (handling both png and jpeg) #4009

Closed

Description

@untom

tf.image.decode_jpeg crashes on a seemingly valid input image with an InvalidArgumentError: Invalid JPEG data, size 107746. I've used both the current nightly from tonight, as well as TF 0.9.0, using Python 3.5.2 on CentOS 7 on two different machines.

The image can be opened/displayed without problems in Firefox, GIMP and other image viewers, as well as with the PIL image library within Python. The image is part of the ILSVRC2015 dataset, n02105855/n02105855_2933.JPEG. I've uploaded it to http://imgur.com/a/pblKL for your reference (i hope imgur doesn't recode the image, let me know).

This is a minimal code example:

import tensorflow as tf
fn = './imagenet/ILSVRC2015/Data/CLS-LOC/train/n02105855/n02105855_2933.JPEG'
with tf.Graph().as_default():
    image_contents = tf.read_file(fn)
    image = tf.image.decode_jpeg(image_contents, channels=3)
    init_op = tf.initialize_all_tables()
    with tf.Session() as sess:
        sess.run(init_op)
        tmp = sess.run(image)

Which crashes with the following error:

InvalidArgumentError: Invalid JPEG data, size 107746
 [[Node: DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=3, fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"](ReadFile)]]
Caused by op 'DecodeJpeg', defined at:

For reference, here the full stack trace is here: http://pastebin.com/GZMWDjge

Activity

  1. girving commented on Aug 25, 2016

    @girving
    Contributor

    That is a PNG.

  2. girving commented on Aug 25, 2016

    @girving
    Contributor

    Actually, I'll leave this open in case and renamed in case someone wants to finally make decode_image which handles both. It's almost possible to do it in Python, or it could be done in C++ with a bunch of code sharing with decode_png and decode_jpeg.

  3. changed the title [-]tf.image.decode_jpeg crashes on valid input[/-] [+]tf.image.decode_image would be nice (handling both png and jpeg)[/+] on Aug 25, 2016
  4. girving commented on Aug 25, 2016

    @girving
    Contributor

    Cc @dave-andersen to trigger painful memories. :)

  5. girving commented on Aug 25, 2016

    @girving
    Contributor

    Also, anyone is welcome to make the error message for decode_png or decode_jpeg include a suitably escaped version of the first few bytes, which generally give it away.

  6. samjabrahams commented on Sep 6, 2016

    @samjabrahams
    Contributor

    @girving would using Python's imghdr library and calling the appropriate decode_<type> function suffice?

  7. girving commented on Sep 6, 2016

    @girving
    Contributor

    Except for using a separate library, yes. Just check magic numbers instead: it would be a few lines of pure Python if we only had a tf.startswith op for strings, so adding tf.startswith is one way.

  8. samjabrahams commented on Sep 6, 2016

    @samjabrahams
    Contributor

    Cool. I whipped up a first pass with tests in #4222, but will need to hard code the imghdr.what() function. It's already implemented in pure Python, so the core logic is already available.

  9. samjabrahams commented on Sep 6, 2016

    @samjabrahams
    Contributor

    @girving what do you think of a tf.substring_match op instead of tf.startswith? Should be more flexible in the long-term, and allows me to keep using the lazy way of detecting JPEGs that imghdr already uses :)

    On further inspection, it looks it may be difficult to pull this off without doing the entire implementation in C++, as you can't read the bytes of the image before the graph is executed. That means there can't be any pure Python if/else blocks that require knowledge of the image type/header. The best I can think of for a mostly-Python implementation involves way more tf.cond operations than reasonable. What are your thoughts?

  10. girving commented on Sep 6, 2016

    @girving
    Contributor

    @samjabrahams I'm not sure I follow. By "way more tf.cond operations than reasonable", do you mean 2? The main advantage of doing it in C++ is nicer error reporting; I don't see any other advantages.

  11. samjabrahams commented on Sep 6, 2016

    @samjabrahams
    Contributor

    @girving - not sure why, but last night I thought it would be three. Sorry for that! It may make more sense to just use a tf.case() op for the sake extensibility (easier to add GIF/bitmap support).

    One final question: what is the best way to throw an error for the default case (bytes not recognized as any available image type)? The easiest thing I can think of right now is simply raising an error in a py_func op, but I don't know whether or not py_func is apropos for Op wrappers.

  12. girving commented on Sep 6, 2016

    @girving
    Contributor

    You can use tf.Assert to raise errors. As for tf.substring_match: what would it do, and how would it be better than tf.startswith?

  13. samjabrahams commented on Sep 6, 2016

    @samjabrahams
    Contributor

    Awesome, thanks. tf.substring_match would check to see if a selected subsection of a string (or array of strings), s1, matches another passed in string, s2. It would return true if the substring matches, false otherwise. You can think of tf.startswith as a special case of tf.substring_match, where the selected subsection is at the beginning of s1.

    Proposed api:

    Args:
      s1: Tensor of type `string`. The strings that will have a substring compared against `s2`
      s2: 0-D Tensor of type `string`. The string that the `s1` substring(s) will be compared against
      start_idx: 0-D integer. The starting index of the substring in `s1` 
    
  14. girving commented on Sep 6, 2016

    @girving
    Contributor

    @samjabrahams It'd be better to write tf.substr and then use tf.equal.

  15. samjabrahams commented on Sep 6, 2016

    @samjabrahams
    Contributor

    @girving Sounds good. Opening up an issue to track tf.substr.

  16. TimZaman commented on Sep 20, 2016

    @TimZaman
    Contributor

    This is great, guys; just check the magic I guess.

  17. added
    type:feature
    and removed on Feb 9, 2017
  18. girving commented on Apr 24, 2017

    @girving
    Contributor

    Fixed long ago by db49056.

  19. clovking commented on Feb 23, 2018

    @clovking

    @untom , I meet a silimar question with you when i train models on google TensorFlow Object Detection API, May i ask if the question has been solved? what's the problem and how to solved? thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

stat:contribution welcometype:feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions