【问题标题】:TypeError: '...' has type str, but expected one of: bytesTypeError: '...' 具有 str 类型,但应为以下之一:字节
【发布时间】:2021-01-12 06:19:23
【问题描述】:

我正在尝试在 Open Images Dataset (v6) 上进行基本的 Tensorflow 边界框对象检测...

  File "/home/work/models/research/object_detection/dataset_tools/create_oid_tf_record.py", line 115, in main
    tf_example = oid_tfrecord_creation.tf_example_from_annotations_data_frame(
  File "/root/anaconda3/lib/python3.8/site-packages/object_detection/dataset_tools/oid_tfrecord_creation.py", line 71, in tf_example_from_annotations_data_frame
    dataset_util.bytes_feature('{}.jpg'.format(image_id)),
  File "/root/anaconda3/lib/python3.8/site-packages/object_detection/utils/dataset_util.py", line 33, in bytes_feature
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
TypeError: '000411001ff7dd4f.jpg' has type str, but expected one of: bytes

相关代码好像在这里:

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

我以为value=[value.encode()] 可能会修复它,但后来它说:

AttributeError: 'bytes' object has no attribute 'encode'

(嗯,是哪个,TF?bytes 还是 str?)

输入文件中的行包含:

ImageID,Source,LabelName,Confidence,XMin,XMax,YMin,YMax,IsOccluded,IsTruncated,IsGroupOf,IsDepiction,IsInside,XClick1X,XClick2X,XClick3X,XClick4X,XClick1Y,XClick2Y,XClick3Y,XClick4Y
000411001ff7dd4f,xclick,/m/09b5t,1,0.1734375,0.46875,0.19791667,0.7916667,0,0,1,0,0

TFRecord 的特征图:

feature_map = {
standard_fields.TfExampleFields.object_bbox_ymin:
dataset_util.float_list_feature(
filtered_data_frame_boxes.YMin.to_numpy()),
standard_fields.TfExampleFields.object_bbox_xmin:
dataset_util.float_list_feature(
filtered_data_frame_boxes.XMin.to_numpy()),
standard_fields.TfExampleFields.object_bbox_ymax:
dataset_util.float_list_feature(
filtered_data_frame_boxes.YMax.to_numpy()),
standard_fields.TfExampleFields.object_bbox_xmax:
dataset_util.float_list_feature(
filtered_data_frame_boxes.XMax.to_numpy()),
standard_fields.TfExampleFields.object_class_text:
dataset_util.bytes_list_feature(
filtered_data_frame_boxes.LabelName.to_numpy()),
standard_fields.TfExampleFields.object_class_label:
dataset_util.int64_list_feature(
filtered_data_frame_boxes.LabelName.map(lambda x: label_map[x])
.to_numpy()),
standard_fields.TfExampleFields.filename:
dataset_util.bytes_feature('{}.jpg'.format(image_id)),
standard_fields.TfExampleFields.source_id:
dataset_util.bytes_feature(image_id),
standard_fields.TfExampleFields.image_encoded:
dataset_util.bytes_feature(encoded_image),
}

有什么想法吗?我安装了 pip3 并且必须修复一堆包弃用错误才能达到这一点。

pip3 install tensorflow
pip3 install tensorflow-object-detection-api

编辑:

版本:

tensorflow                         2.3.1
tensorflow-object-detection-api    0.1.1

我试过了

  standard_fields.TfExampleFields.filename:
      dataset_util.bytes_feature(bytes(('{}.jpg'.format(image_id)),'ascii')),

但它得到以下内容:

TypeError: '000411001ff7dd4f' has type str, but expected one of: bytes

(.jpg 去哪儿了?)

【问题讨论】:

  • 我很确定 API 需要的是文件的 contents,而不是文件名。您甚至可能不得不使用其他东西将图像数据加载到表示位图的数组中。
  • "输入文件中的行包含:"你在说什么输入文件?这显然不是 .jpg。
  • 无论如何,您应该绝对不要试图通过编辑库代码来解决问题。我不清楚这里的任何代码框是否代表您编写的代码
  • 您好 Karl,该字段名为“文件名”。我很确定它需要文件名。
  • 我不习惯 3.x 代码以字节表示文件名,但我猜这是 TF 接口的结果。 ://

标签: python tensorflow tensorflow2.0 object-detection object-detection-api


【解决方案1】:

TypeError 似乎是指在这一行中创建的文件名:

dataset_util.bytes_feature('{}.jpg'.format(image_id)),

也许这个图像名称应该是字节,像这样:

dataset_util.bytes_feature('{}.jpg'.format(image_id).encode()),

【讨论】:

  • 谢谢,这给出了“'bytes' 对象没有属性 'format'”。这个 b'...' 技巧有一些运算符优先级问题。
  • 啊...我修改了我的答案。 str.format 明明只针对字符串,所以格式化后必须转成字节,用str.encode
【解决方案2】:

所以理想情况下,Karl 是对的,我们“应该绝对不试图通过编辑库代码来解决问题。”

但我的库代码目前似乎无法正常工作。找到一个相关的github issue bug

并使用 .encode() 修复它似乎 bytes() 解决方案也有效。丢失的“.jpg”是因为它移动到下一次并死在那里。

固定代码:

  standard_fields.TfExampleFields.filename:
      dataset_util.bytes_feature(('{}.jpg'.format(image_id)).encode('utf-8')),
  standard_fields.TfExampleFields.source_id:
      dataset_util.bytes_feature(image_id.encode('utf-8')),

刚刚进入下一个错误。

TypeError: '/m/09b5t' has type str, but expected one of: bytes

同样的错误,但对于bytes_list_feature() 调用。通过更改修复:

  standard_fields.TfExampleFields.object_class_text:
      dataset_util.bytes_list_feature(
          filtered_data_frame_boxes.LabelName.to_numpy()),

到:

  standard_fields.TfExampleFields.object_class_text:
      dataset_util.bytes_list_feature(
          filtered_data_frame_boxes.LabelName.map(lambda x: x.encode('utf8')).to_numpy()()),

在拉取请求之后:https://github.com/tensorflow/models/pull/4771/files(并且还必须将 as_matrix() 更改为 to_numpy()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-19
    • 2020-02-06
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    相关资源
    最近更新 更多