【问题标题】:Inception build_imagenet_data.py TypeError: 'RGB' has type <class 'str'>, but expected one of: (<class 'bytes'>,)Inception build_imagenet_data.py TypeError: 'RGB' 的类型为 <class 'str'>,但预期为以下之一:(<class 'bytes'>,)
【发布时间】:2016-07-23 21:43:13
【问题描述】:

我正在尝试在build_imagenet_data.py 中使用 _convert_to_example() 函数的轻微变体:

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

我收到与色彩空间变量相关的错误:

TypeError: 'RGB' 具有类型类 'str',但应为以下之一:(class 'bytes',)

如果我注释掉图像/色彩空间功能,我会收到相同的图像/格式错误。对于图像/文件名也是如此。如果我注释掉这三个功能,该功能似乎按预期运行。我做错了什么?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    这听起来像是 Python 2/3 不兼容问题。您可以通过在字符串文字前添加 b 来显式创建 colorspaceimage_format 作为 bytes 对象,如下所示:

    colorspace = b'RGB'
    # ...
    image_format = b'JPEG'
    

    【讨论】:

    • 感谢并确认。该问题在 py3.4 中已注意到,但在 py2.7 中不存在。此外,除了您建议将 b 附加到字符串文字之外,encode() 方法对于应用于字符串变量很有用。
    【解决方案2】:

    我注意到了一些额外的变化。

    o     #shuffled_index = range(len(filenames))
    o     shuffled_index = list(range(len(filenames)))
    o     #for i in xrange(len(spacing) - 1):
    o     for i in range(len(spacing) - 1):
    o         'image/class/text': _bytes_feature(text.encode()),
    o         'image/filename': _bytes_feature(os.path.basename(filename.encode())),
    o     colorspace = 'RGB'.encode()
    o     channels = 3
    o     image_format = 'JPEG'.encode()
    

    【讨论】:

      猜你喜欢
      • 2020-10-19
      • 2020-02-09
      • 2021-01-12
      • 1970-01-01
      • 2022-10-23
      • 2020-07-21
      • 2019-11-15
      • 1970-01-01
      • 2017-03-21
      相关资源
      最近更新 更多