tensorflow/models

Using eval() for Decode JPEG

danleh93 opened this issue · 6 comments

tensorflow/models/slim/preprocessing/inception_preprocessing

I modified the preprocess_for_eval(...) function to look like:

def preprocess_for_eval(img, height, width,
                        central_fraction=0.875, scope=None):

  image = tf.image.convert_image_dtype(tf.image.decode_jpeg(img, channels=3), dtype=tf.float32)

  with tf.name_scope(scope, 'eval_image', [image, height, width]):
    if image.dtype != tf.float32:
      image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    # Crop the central region of the image with an area containing 87.5% of
    # the original image.
    if central_fraction:
      image = tf.image.central_crop(image, central_fraction=central_fraction)

    if height and width:
      # Resize the image to the specified height and width.
      image = tf.expand_dims(image, 0)
      image = tf.image.resize_bilinear(image, [height, width],
                                       align_corners=False)
      image = tf.squeeze(image, [0])
    image = tf.sub(image, 0.5)
    image = tf.mul(image, 2.0)
    return image

The code executes perfectly, however, when I do something like

image_tensor = preprocess_for_eval("path/to/image.jpeg",300,300).eval(session=sess)

I get an error stating:

Traceback (most recent call last):
  File "preprocess_for_eval.py", line 53, in <module>
    print(image.eval())
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 564, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3784, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InvalidArgumentError: Invalid JPEG data, size 52
     [[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"](DecodeJpeg/contents)]]

Caused by op u'DecodeJpeg', defined at:
  File "preprocess_for_eval.py", line 50, in <module>
    image = preprocess_for_eval('/home/ec2-user/workspace/results/flowers/daisy2.jpeg', 300,300)
  File "preprocess_for_eval.py", line 29, in preprocess_for_eval
    image = tf.image.convert_image_dtype(tf.image.decode_jpeg(img, channels=3), dtype=tf.float32)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_image_ops.py", line 283, in decode_jpeg
    name=name)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 748, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2403, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1305, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Invalid JPEG data, size 52
     [[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"](DecodeJpeg/contents)]]

I know for a fact that my image is a JPEG because when I do:

file image.jpeg on the command line I get:

JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, baseline, precision 8, 300x300, frames 3

Even when I try tf.image.decode_png I get an invalid PNG header message.

@danleh93 : It is possible that the image is not valid (at 52-bytes, it seems really small :)
Note that file image.jpeg is just reading the header, so it is possible that the image file's content was truncated making it an invalid jpeg but still keeping the small header intact enough for file to think that it is an image.

Does the image load up correctly in an image viewer? You can also try the identify tool to see if it is really a valid image.

Let us know what you find.
Thanks.

@asimshankar : Thanks for the response. I did some further investigation: The image does, in fact, load up correctly in an image viewer. Furthermore, when running identify -verbose image.jpeg the resulting output is:

  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Class: DirectClass
  Geometry: 300x300+0+0
  Resolution: 72x72
  Print size: 4.16667x4.16667
  Units: PixelsPerInch
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
  Channel statistics:
    Red:
      min: 76 (0.298039)
      max: 255 (1)
      mean: 249.772 (0.979499)
      standard deviation: 17.1539 (0.0672703)
      kurtosis: 21.3926
      skewness: -4.31776
    Green:
      min: 55 (0.215686)
      max: 255 (1)
      mean: 249.395 (0.978019)
      standard deviation: 19.7617 (0.077497)
      kurtosis: 26.8176
      skewness: -4.87254
    Blue:
      min: 0 (0)
      max: 255 (1)
      mean: 246.014 (0.964762)
      standard deviation: 33.5775 (0.131676)
      kurtosis: 33.2542
      skewness: -5.51364
  Image statistics:
    Overall:
      min: 0 (0)
      max: 255 (1)
      mean: 248.394 (0.974093)
      standard deviation: 24.578 (0.0963842)
      kurtosis: 46.7875
      skewness: -6.14705
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Interlace: None
  Background color: white
  Border color: srgb(223,223,223)
  Matte color: grey74
  Transparent color: black
  Compose: Over
  Page geometry: 300x300+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: JPEG
  Quality: 90
  Orientation: Undefined
  Properties:
    date:create: 2016-10-19T17:28:35+00:00
    date:modify: 2016-10-19T17:28:35+00:00
    jpeg:colorspace: 2
    jpeg:sampling-factor: 2x2,1x1,1x1
    signature: 40b91d70c11d16b555bc242d95343424f6f79a10b11350c392b4291730d8a304
  Artifacts:
    filename: image.jpeg
    verbose: true
  Tainted: False
  Filesize: 6.77KB
  Number pixels: 90K
  Pixels per second: 0B
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.7.8-9 2016-06-22 Q16 http://www.imagemagick.org

I am not sure why Tensorflow is only reading it as 52-bytes. When I do a ls -l image.jpeg the result is about 6.7KB.

Oh, wait, sorry, I missed something.
You're passing the filename to tf.decode_jpeg, which is not what it takes. It takes a string tensor that is the contents of the file. See https://www.tensorflow.org/api_docs/python/image.html#decode_jpeg

So the 52-bytes is the filename.
Instead you want something like this:

contents = ''
with open('path/to/image.jpeg') as f:
   contents = f.read()
tf.image.decode_jpeg(contents)

Hope that helps.

That worked! Can't believe I forgot about that part. Thanks so much!

Hi~, I meet a problem about convert_image_dtype.

    with open('/home/give/Documents/dataset/ADEChallengeData2016/images/validation/ADE_val_00000001.jpg') as f:
        content = f.read()
    image = tf.image.decode_jpeg(content, channels=3)
    image_float = tf.image.convert_image_dtype(image, dtype=tf.float32)
    img, img_float = sess.run([image, image_float])
    # print img
    # print img_float
    print np.max(img_float), np.min(img_float)

when i execute this code, why np.max(img_float), np.min(img_float) both are nan? The value of img is normal. Do you know the reason? Thanks

@danleh93 , 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!