如何将labelmap.prototxt转换为集合?咖啡

问题描述 投票:0回答:1

我有labelmap.prototxt,我想将它转换成一个集合,因为我想将它与Intel Neural Compute Stick SDK一起使用。

输入

家用/ labelmap.prototxt

item {
  name: "none_of_the_above"
  label: 0
  display_name: "background"
}
item {
  name: "1"
  label: 1
  display_name: "person"
}
item {
  name: "2"
  label: 2
  display_name: "bicycle"
}
item {
  name: "3"
  label: 3
  display_name: "car"
}

输出:

set(["background","person","bicycle","car"])

我尝试如下:

>>> with open('labelmap_coco.prototxt') as f:
...     d = literal_eval(f)
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python2.7/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string
python dictionary caffe
1个回答
1
投票

在caffe ssd proto文件持久化消息LabelMap中阅读有关proto缓冲区的内容,这样您就可以从prototxt中读取对象数据。这是非常好的事情。看看代码:

from caffe.proto import caffe_pb2
from google.protobuf import text_format as tf

f = open('labelmap.prototxt', 'r')
lm = caffe_pb2.LabelMap()
lm = tf.Parse(f.read(), lm)
display_names = [x.display_name for x in lm.item]
© www.soinside.com 2019 - 2024. All rights reserved.