Models: _bytes_feature(encoded_image) will throw an exception

Created on 29 Dec 2016  路  6Comments  路  Source: tensorflow/models

Please let us know which model this issue is about (specify the top-level directory)

When I using ptyhon3.5.2 to run script build_mscoco_data,there will throw an exception in line


 context = tf.train.Features(feature={
      "image/image_id": _int64_feature(image.image_id),
      "image/data": _bytes_feature(encoded_image),
  })

the debug info before this has no warning,error and exception

Loaded caption metadata for 200 images from /Users/apple1/Desktop/im2txt_new/im2txt/im2txt/data/raw-data/annotations/captions_train2014.json
Proccessing captions.
Finished processing 1000 captions for 200 images in /Users/apple1/Desktop/im2txt_new/im2txt/im2txt/data/raw-data/annotations/captions_train2014.json
Loaded caption metadata for 50 images from /Users/apple1/Desktop/im2txt_new/im2txt/im2txt/data/raw-data/annotations/captions_val2014.json
Proccessing captions.
Finished processing 250 captions for 50 images in /Users/apple1/Desktop/im2txt_new/im2txt/im2txt/data/raw-data/annotations/captions_val2014.json
Creating vocabulary.
Total words: 1601
Words in vocabulary: 463
Wrote vocabulary file: /tmp/word_counts.txt
Launching 8 threads for spacings: [[0, 151], [151, 302], [302, 453], [453, 605], [605, 756], [756, 907], [907, 1058], [1058, 1210]]

I only use 200 train images and 50 val images to test, I have processed two json files, captions and images all paired.

when step on

def _bytes_feature(value):
  """Wrapper for inserting a bytes Feature into a SequenceExample proto."""
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[str(value)]))

there will throw an exception

Exception in thread Thread-6:
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/apple1/Desktop/im2txt_new/im2txt/im2txt/data/build_mscoco_data.py", line 278, in _process_image_files
    sequence_example = _to_sequence_example(image, decoder, vocab)
  File "/Users/apple1/Desktop/im2txt_new/im2txt/im2txt/data/build_mscoco_data.py", line 224, in _to_sequence_example
    "image/data": _bytes_feature(encoded_image),
  File "/Users/apple1/Desktop/im2txt_new/im2txt/im2txt/data/build_mscoco_data.py", line 189, in _bytes_feature
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[str(value)]))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/google/protobuf/internal/python_message.py", line 517, in init
    copy.extend(field_value)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/google/protobuf/internal/containers.py", line 275, in extend
    new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/google/protobuf/internal/containers.py", line 275, in <listcomp>
    new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/google/protobuf/internal/type_checkers.py", line 108, in CheckValue
    raise TypeError(message)
TypeError: 'b\'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x01\\x01\\x01\\x00d\\x00d\\x00\\x00\\xff\\xe2\\x0cXICC_PROFILE\\x00\\x01\\x01\\x00\\x00\\x0cHLino\\x02\\x10\\x00\\x00mntrRGB XYZ \\x07\\xce\\x00\\x02\\x00\\t\\x00\\x06\\x001\\x00\\x00acspMSFT\\x00\\x00\\x00\\x00IEC sRGB\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xf6\\xd6\\x00\\x01\\x00\\x00\\x00\\x00\\xd3-HP  \\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x11cprt\\x00\\x00\\x01P\\x00\\x00\\x003desc\\x00\\x00\\x01\\x84\\x00\\x00\\x00lwtpt\\x00\\x00\\x01\\xf0\\x00\\x00\\x00\\x14bkpt\\x00\\x00\\x02\\x04\\x00\\x00\\x00\\x14rXYZ\\x00\\x00\\x02\\x18\\x00\\x00\\x00\\x14gXYZ\\x00\\x00\\x02,\\x00\\x00\\x00\\x14bXYZ\\x00\\x00\\x02@\\x00\\x00\\x00\\x14dmnd\\x00\\x00\\x02T\\x00\\x00\\x00pdmdd\\x00\\x00\\x02\\xc4\\x00\\x00\\x00\\x88vued\\x00\\x00\\x03L\\x00\\x00\\ has type <class 'str'>, but expected one of: ((<class 'bytes'>,),)
awaiting maintainer bug

Most helpful comment

I also had this issue. I am wondering if this is not an issue in python 2.7

In order to move on I removed the str() cast in _bytes_feature:

def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

then I removed the image decoder check, and was able to run the script.

All 6 comments

I also had this issue. I am wondering if this is not an issue in python 2.7

In order to move on I removed the str() cast in _bytes_feature:

def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

then I removed the image decoder check, and was able to run the script.

I am confronting with the same issue now.

I was able to modify that script as I mentioned before, but I also had to modify gfile:

def _bytes_feature(value):
    """Wrapper for inserting a bytes Feature into a SequenceExample proto."""
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

def _to_sequence_example(image, decoder, vocab):

    with tf.gfile.Open(image.filename, "rb") as f: 
        encoded_image = f.read()

    context = tf.train.Features(feature={
        "image/image_id": _int64_feature(image.image_id),
        "image/data": _bytes_feature(encoded_image),
    })

    ....

in order to open the image, and I removed the try, except image decoder check that follows reading the image.

@cshallue Can you comment?

@PowerAndSpeed Is this still an issue? If not, please close the issue or let me know. Thanks

Closing due to lack of recent activity. Please update the issue when new information becomes available, and we will open a new issue. Thanks!

Was this page helpful?
0 / 5 - 0 ratings