You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tf.image.decode_image would be nice (handling both png and jpeg) #4009
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.
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.
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
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.
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.
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.
@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?
@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.
@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.
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`
@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!
Activity
girving commentedon Aug 25, 2016
That is a PNG.
girving commentedon Aug 25, 2016
Actually, I'll leave this open in case and renamed in case someone wants to finally make
decode_imagewhich handles both. It's almost possible to do it in Python, or it could be done in C++ with a bunch of code sharing withdecode_pnganddecode_jpeg.[-]tf.image.decode_jpeg crashes on valid input[/-][+]tf.image.decode_image would be nice (handling both png and jpeg)[/+]girving commentedon Aug 25, 2016
Cc @dave-andersen to trigger painful memories. :)
girving commentedon Aug 25, 2016
Also, anyone is welcome to make the error message for
decode_pngordecode_jpeginclude a suitably escaped version of the first few bytes, which generally give it away.samjabrahams commentedon Sep 6, 2016
@girving would using Python's imghdr library and calling the appropriate
decode_<type>function suffice?girving commentedon Sep 6, 2016
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.startswithop for strings, so addingtf.startswithis one way.samjabrahams commentedon Sep 6, 2016
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.samjabrahams commentedon Sep 6, 2016
@girving what do you think of a
tf.substring_matchop instead oftf.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.condoperations than reasonable. What are your thoughts?girving commentedon Sep 6, 2016
@samjabrahams I'm not sure I follow. By "way more
tf.condoperations 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.samjabrahams commentedon Sep 6, 2016
@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_funcop, but I don't know whether or notpy_funcis apropos for Op wrappers.girving commentedon Sep 6, 2016
You can use
tf.Assertto raise errors. As fortf.substring_match: what would it do, and how would it be better thantf.startswith?samjabrahams commentedon Sep 6, 2016
Awesome, thanks.
tf.substring_matchwould 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 oftf.startswithas a special case oftf.substring_match, where the selected subsection is at the beginning ofs1.Proposed api:
girving commentedon Sep 6, 2016
@samjabrahams It'd be better to write
tf.substrand then usetf.equal.samjabrahams commentedon Sep 6, 2016
@girving Sounds good. Opening up an issue to track
tf.substr.TimZaman commentedon Sep 20, 2016
This is great, guys; just check the magic I guess.
girving commentedon Apr 24, 2017
Fixed long ago by db49056.
clovking commentedon Feb 23, 2018
@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!