初始build_imagenet_data.py TypeError:'RGB'具有类型<class'str'>,但应为以下之一:(<class'bytes'>,)

Rob*_*obR 4 tensorflow

我想用在_convert_to_example()函数的稍加改变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
Run Code Online (Sandbox Code Playgroud)

我收到与colorspace变量相关的错误:

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

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

mrr*_*rry 5

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

colorspace = b'RGB'
# ...
image_format = b'JPEG'
Run Code Online (Sandbox Code Playgroud)

  • 谢谢并确认。py3.4指出了该问题,但py2.7并未出现该问题。另外,除了建议在字符串文字前加上b之外,encode()方法对于将字符串变量应用也是有用的。 (3认同)