Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decode_image return tensor without shape in python #9356

Closed
ZhangBowen opened this issue Apr 21, 2017 · 16 comments
Closed

decode_image return tensor without shape in python #9356

ZhangBowen opened this issue Apr 21, 2017 · 16 comments
Labels
stat:awaiting tensorflower Status - Awaiting response from tensorflower type:feature Feature requests

Comments

@ZhangBowen
Copy link

System Information

  • OS Platform and Distribution (i.e. Linux Ubuntu 16.0): CentOS Linux release 7.0.1406 (Core)
  • TensorFlow installed from (source or binary)?: python3 pip
  • TensorFlow version : v1.0.0-65-g4763edf-dirty 1.0.1
  • Python version : Python 3.5.2 :: Anaconda custom (64-bit)

Describe the problem clearly

When I decode a jpeg file to a tensor with decode_jpeg, the code ran normally.
but if I use decode_image instead of decode_jpeg for more compatibilities, It raised a ValueError as follow.
Is It a bug?
Thanks

Source Code / Logs

def preprocess_image(image_path):
    file_content = tf.read_file(image_path)
    
    #image = tf.image.decode_jpeg(file_content)
    image = tf.image.decode_image(file_content)
    image = tf.image.per_image_standardization(image)
    image = tf.image.resize_images(image, [480, 640])

output :

Traceback (most recent call last):
  File "/data/home/zhangbowen/is_vehicle/classify.py", line 29, in <module>
    main()
  File "/data/home/zhangbowen/is_vehicle/classify.py", line 26, in main
    image = preprocess_image(image_file)
  File "/data/home/zhangbowen/is_vehicle/classify.py", line 15, in preprocess_image
    image = tf.image.resize_images(image, [480, 640])
  File "/data/home/zhangbowen/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/image_ops_impl.py", line 643, in resize_images
    raise ValueError('\'images\' contains no shape.')
ValueError: 'images' contains no shape.
@drpngx
Copy link
Contributor

drpngx commented Apr 22, 2017

The decode_image is a cascade of tf.cond.
resize_images needs to pull the shape object. However, we don't know which one it's going to be upfront.

@cwhipkey any comment here?

@drpngx drpngx added stat:awaiting tensorflower Status - Awaiting response from tensorflower type:feature Feature requests labels Apr 22, 2017
@cwhipkey
Copy link
Contributor

cwhipkey commented May 2, 2017

I think the issue is that in the decode_image cond, one branch leads to decode_gif. decode_gif returns a 4-d tensor: [num_frames, height, width, channels], which is not the same as the 3-d tensor returned by decode_png and decode_jpeg.

One possible fix would be to make decode_gif have an attribute controlling whether to handle animated gifs ,and use that in decode_image? (possibly also with an option? It's not clear to me someone would want to use decode_image with a mix of animated and non-animated images - so maybe just changing it would be fine?).

+raingo and wicke

@cwhipkey
Copy link
Contributor

cwhipkey commented May 2, 2017

@raingo @martinwicke

@martinwicke
Copy link
Member

We could definitely add "frame=None" which is without effect for png and jpg, but for gif will return all frames if None, of the given frame number if not None and the gif is animated. This would work for other animated formats if those become available.

If frame != None we can set the rank to [None, None, None], maybe even [None, None, 3] -- not sure that'll help.

@raingo
Copy link
Contributor

raingo commented May 2, 2017

@martinwicke I can send PR to add the frame=None to https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/image_ops_impl.py#L1338.

      with ops.control_dependencies([assert_decode, assert_channels]):
        res = gen_image_ops.decode_gif(contents)
        if frame is not None:
           res = res[frame]
        return res

It makes the least changes to the code, and add some flexibility.

Not sure if that is the best way to do it.

@girving
Copy link
Contributor

girving commented May 9, 2017

I think the right way to fix this is to make the image resizing machinery not need static shape. I'll take a look at how difficult that is.

@cwhipkey
Copy link
Contributor

cwhipkey commented May 9, 2017

Having decode_image sometimes return 4-d and sometimes return 3-d does not seem useful though - should it always just return frame 0 of a gif? (this was a change in behavior of decode_image when animated gif support was added)

@girving
Copy link
Contributor

girving commented May 9, 2017

@cwhipkey It was a backwards incompatible change?

@cwhipkey
Copy link
Contributor

cwhipkey commented May 9, 2017

No, I don't think so. DecodeGif always returned 4D tensor. decode_image in python was added after that, and always returned a 4D for GIF and 3D for png or jpg. So nm on what I wrote earlier about having it always return frame 0.

@drpngx
Copy link
Contributor

drpngx commented May 9, 2017

CC: @girving

@girving
Copy link
Contributor

girving commented May 10, 2017

I'm going to restrict my current change to unify decode_jpeg and decode_png: both ops will handle both formats.

@girving
Copy link
Contributor

girving commented Jun 16, 2017

decode_png and decode_jpeg now decode all formats.

@girving girving closed this as completed Jun 16, 2017
@ZhangBowen
Copy link
Author

thanks

dcunhas added a commit to dcunhas/FCN.tensorflow that referenced this issue Nov 3, 2017
dcunhas added a commit to dcunhas/FCN.tensorflow that referenced this issue Mar 20, 2018
* Mostly implemented new data pipeline.

* Changed DatasetReader record holding structure slightly.

* Fixed using wrong value for iteration.

* Some possibly working changes to reader.

* May have fixed input problem.

* Actually made the change intended in last commit.

* Fixed minor bugs.

* Fixed bug with annotation checking.

* Removed static.

* Fixed map bug.

* Reverted lambda in TFRreader after discovering cause of bug.

* Check for no annotation.

* Might have fixed bug.

* Fixed references to dataset.

* Fixed self.self

* Readded code accidentally removed previously, changed dimension to axi in annotation_pred = tf.argmax(...) because the dimension argument will be depricated in the future.

* Changed call to next to call to sess.run.

* Added resizing.

* Fixed decoding based on tf bug tensorflow/tensorflow#9356.

* Changed iteration getting method.

* Fixed not reading in annotations and other minor fixes.

* Actually fixed iterator getting in FCN.py

* Train ops now set to work with tensors rather than placeholders.

* Added trainable_var above saver.

* Revert "Added trainable_var above saver."

This reverts commit 6011e0b.

* Revert "Train ops now set to work with tensors rather than placeholders."

This reverts commit 6f12d7d.

* Should work now using placeholders.

* Might have fixed annotation shape.

* Fixed _augment_image where annotation image was set to the same as image.

* Added extra dimension to annotation.

* Some minor changes to TFReader.

* Fixed predict with new dataset workflow.

* Slight change to TFRreader.

* Fixed problem with SingleDataset.

* Fixed referencing dataset rather than DatasetReader.

* Changed Dataset Reader to also output filename.

* Fixed multiple runs of predict_names.

* Added Jupyter Notebook for viewing results

* Fixed bug with not returning 'name' if image augmentation is off
dcunhas added a commit to Trefo/FCN.tensorflow that referenced this issue May 2, 2018
* put training and validation loss in the same graph

* Renamed file and changed read in details.

* Changed read in details and pickle filename.

* Fixed hardcoded data dir.

* Change no. classes.

* Change to see if it fixes something

* removed log files from git

* Added method for predictions.

* Added printed messages to predict.

* Fixed print

* Fixed format string statements.

* Changed resize to True for predict.

* Fixed filename for saving

* Added forgotten squeeze on pred in predict code.

* Create predictions folder if it doesn't exist.

* Slight updates to dropout and BatchDatsetReader

* Added image augmentation.

* Fixed usage of wrong name of FLAGS.dropout_keep_prob.

* Fixed BatchDatasetReader and changed dropout_keep_value to dropout in FLAGS.

* Fixed bug in BatchDatasetReader

* Added back in an accidentally deleted line.

* Fixed empty list/None check which is incompatible with Numpy.

* Fixed call of np.concatenate

* removed expand_dims for annotations.

* Properly check if annotation_file is not None.

* New reader (#2)

* Mostly implemented new data pipeline.

* Changed DatasetReader record holding structure slightly.

* Fixed using wrong value for iteration.

* Some possibly working changes to reader.

* May have fixed input problem.

* Actually made the change intended in last commit.

* Fixed minor bugs.

* Fixed bug with annotation checking.

* Removed static.

* Fixed map bug.

* Reverted lambda in TFRreader after discovering cause of bug.

* Check for no annotation.

* Might have fixed bug.

* Fixed references to dataset.

* Fixed self.self

* Readded code accidentally removed previously, changed dimension to axi in annotation_pred = tf.argmax(...) because the dimension argument will be depricated in the future.

* Changed call to next to call to sess.run.

* Added resizing.

* Fixed decoding based on tf bug tensorflow/tensorflow#9356.

* Changed iteration getting method.

* Fixed not reading in annotations and other minor fixes.

* Actually fixed iterator getting in FCN.py

* Train ops now set to work with tensors rather than placeholders.

* Added trainable_var above saver.

* Revert "Added trainable_var above saver."

This reverts commit 6011e0b.

* Revert "Train ops now set to work with tensors rather than placeholders."

This reverts commit 6f12d7d.

* Should work now using placeholders.

* Might have fixed annotation shape.

* Fixed _augment_image where annotation image was set to the same as image.

* Added extra dimension to annotation.

* Some minor changes to TFReader.

* Fixed predict with new dataset workflow.

* Slight change to TFRreader.

* Fixed problem with SingleDataset.

* Fixed referencing dataset rather than DatasetReader.

* Changed Dataset Reader to also output filename.

* Fixed multiple runs of predict_names.

* Added Jupyter Notebook for viewing results

* Fixed bug with not returning 'name' if image augmentation is off
dcunhas pushed a commit to Trefo/FCN.tensorflow that referenced this issue May 7, 2018
* put training and validation loss in the same graph

* Renamed file and changed read in details.

* Changed read in details and pickle filename.

* Fixed hardcoded data dir.

* Change no. classes.

* Change to see if it fixes something

* removed log files from git

* Added method for predictions.

* Added printed messages to predict.

* Fixed print

* Fixed format string statements.

* Changed resize to True for predict.

* Fixed filename for saving

* Added forgotten squeeze on pred in predict code.

* Create predictions folder if it doesn't exist.

* Slight updates to dropout and BatchDatsetReader

* Added image augmentation.

* Fixed usage of wrong name of FLAGS.dropout_keep_prob.

* Fixed BatchDatasetReader and changed dropout_keep_value to dropout in FLAGS.

* Fixed bug in BatchDatasetReader

* Added back in an accidentally deleted line.

* Fixed empty list/None check which is incompatible with Numpy.

* Fixed call of np.concatenate

* removed expand_dims for annotations.

* Properly check if annotation_file is not None.

* Mostly implemented new data pipeline.

* Changed DatasetReader record holding structure slightly.

* Fixed using wrong value for iteration.

* Some possibly working changes to reader.

* May have fixed input problem.

* Actually made the change intended in last commit.

* Fixed minor bugs.

* Fixed bug with annotation checking.

* Removed static.

* Fixed map bug.

* Reverted lambda in TFRreader after discovering cause of bug.

* Check for no annotation.

* Might have fixed bug.

* Fixed references to dataset.

* Fixed self.self

* Readded code accidentally removed previously, changed dimension to axi in annotation_pred = tf.argmax(...) because the dimension argument will be depricated in the future.

* Changed call to next to call to sess.run.

* Added resizing.

* Fixed decoding based on tf bug tensorflow/tensorflow#9356.

* Changed iteration getting method.

* Fixed not reading in annotations and other minor fixes.

* Actually fixed iterator getting in FCN.py

* Train ops now set to work with tensors rather than placeholders.

* Added trainable_var above saver.

* Revert "Added trainable_var above saver."

This reverts commit 6011e0b.

* Revert "Train ops now set to work with tensors rather than placeholders."

This reverts commit 6f12d7d.

* Should work now using placeholders.

* Might have fixed annotation shape.

* Fixed _augment_image where annotation image was set to the same as image.

* Added extra dimension to annotation.

* Some minor changes to TFReader.

* Fixed predict with new dataset workflow.

* Slight change to TFRreader.

* Fixed problem with SingleDataset.

* Fixed referencing dataset rather than DatasetReader.

* Changed Dataset Reader to also output filename.

* Fixed multiple runs of predict_names.

* Added Jupyter Notebook for viewing results

* Update current FCN code
@lizhengwei1992
Copy link

I'm going to restrict my current change to unify decode_jpeg and decode_png: both ops will handle both formats.

how to judge the format of the image ?

@azizullah2017
Copy link

azizullah2017 commented Apr 10, 2019

I found the solution

tf.enable_eager_execution()

add this to the top of your code.

or

img_tensor = tf.image.decode_jpeg(img_raw,channels=3)
img_final = tf.image.resize_images(img_tensor, [192, 192])
img_final = img_final/255.0
print(img_final.shape)

@reubencmoyo
Copy link

I found the solution

tf.enable_eager_execution()

add this to the top of your code.

or

img_tensor = tf.image.decode_jpeg(img_raw,channels=3)
img_final = tf.image.resize_images(img_tensor, [192, 192])
img_final = img_final/255.0
print(img_final.shape)

This worked for me. Thanks

tf.enable_eager_execution()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting tensorflower Status - Awaiting response from tensorflower type:feature Feature requests
Projects
None yet
Development

No branches or pull requests

9 participants