ValueError:协议消息 StringIntLabelMapItem 没有字段频率

问题描述

我正在尝试使用 YouTube 教程中的 tensorflow 构建对象检测器。我已经创建了一个 conda 环境并安装了所有必需的包。现在当我尝试在 VScode 中运行它的脚本时,它显示了这个错误

Traceback (most recent call last):
File "updated_old_example.py",line 79,in <module>
categories = label_map_util.convert_label_map_to_categories(label_map,max_num_classes=NUM_CLASSES,use_display_name=True)
File "/Users/Home/Desktop/Project/Object_Detection/models/research/object_detection/utils/label_map_util.py",line 133,in convert_label_map_to_categories
if item.HasField('frequency'):
ValueError: Protocol message StringIntLabelMapItem has no field frequency.

这是我第一次遇到这个错误。一些修复将不胜感激。

解决方法

我以前从未发现过这种问题,但是从 tensorflow API 下载新模型后,这个错误就出来了。 然后我尝试将写在错误消息上的文件与旧版本的 tensorflow api 模型进行比较,这就是错误出现的地方。你需要做的就是找到这个文件:

Object_Detection/models/research/object_detection/utils/label_map_util.py",line 133

然后删除此代码

  if item.HasField('frequency'):
    if item.frequency == string_int_label_map_pb2.LVISFrequency.Value(
        'FREQUENT'):
      category['frequency'] = 'f'
    elif item.frequency == string_int_label_map_pb2.LVISFrequency.Value(
        'COMMON'):
      category['frequency'] = 'c'
    elif item.frequency == string_int_label_map_pb2.LVISFrequency.Value(
        'RARE'):
      category['frequency'] = 'r'
  if item.HasField('instance_count'):
    category['instance_count'] = item.instance_count