Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inception build_imagenet_data.py TypeError: 'RGB' has type <class 'str'>, but expected one of: (<class 'bytes'>,)

Tags:

tensorflow

I am trying to use a slight variant of the _convert_to_example() function in build_imagenet_data.py:

def _convert_to_example(filename, image_buffer, label, bboxes, height, width):

  xmin = []
  ymin = []
  xmax = []
  ymax = []
  for b in bboxes:
    assert len(b) == 4
    # pylint: disable=expression-not-assigned
    [l.append(point) for l, point in zip([xmin, ymin, xmax, ymax], b)]
    # pylint: enable=expression-not-assigned

    colorspace = 'RGB'
    channels = 3
    image_format = 'JPEG'

    example = tf.train.Example(features=tf.train.Features(feature={
      'image/height': _int64_feature(height),
      'image/width': _int64_feature(width),
      'image/colorspace': _bytes_feature(colorspace),
      'image/channels': _int64_feature(channels),
      'image/class/label': _int64_feature(label),
      'image/object/bbox/xmin': _float_feature(xmin),
      'image/object/bbox/xmax': _float_feature(xmax),
      'image/object/bbox/ymin': _float_feature(ymin),
      'image/object/bbox/ymax': _float_feature(ymax),
      'image/object/bbox/label': _int64_feature(label),
      'image/format': _bytes_feature(image_format),
      'image/filename': _bytes_feature(os.path.basename(filename)),
      'image/encoded': _bytes_feature(image_buffer)}))
  return example

I get an error associated with the colorspace variable:

TypeError: 'RGB' has type class 'str', but expected one of: (class 'bytes',)

If I comment out the image/colorspace feature I get the same error for image/format. Likewise for image/filename. If I comment out these three features the function seems to run as expected. What am I doing wrong?

like image 557
RobR Avatar asked Jan 05 '23 16:01

RobR


1 Answers

This sounds like a Python 2/3 incompatibility issue. You can explicitly create colorspace and image_format as bytes objects by prepending a b to the string literals, as follows:

colorspace = b'RGB'
# ...
image_format = b'JPEG'
like image 142
mrry Avatar answered Jan 17 '23 10:01

mrry