Tensorflow: has no attribute 'load_labelmap

2019-05-11 17:02发布

I followed the steps described on their Github to install the Object Detection API and then I ran this script:

python object_detection/builders/model_builder_test.py

and the test were successful, so I assumed everything was setup correctly. Next I tried to run the Jupyter Notebook with qtconsole to detect objects in the test images. However it returns this error:

AttributeError                            Traceback (most recent call last)
<ipython-input-3-be6fe1ba8733> in <module>()
----> 1 from utils import label_map_util
      2 
      3 from utils import visualization_utils as vis_util
      4 

~\Desktop\Objectdetection\models-master\object_detection\utils\label_map_util.py in <module>()
     20 import tensorflow as tf
     21 from google.protobuf import text_format
---> 22 from object_detection.protos import string_int_label_map_pb2
     23 
     24 

~\Desktop\Objectdetection\models-master\object_detection\object_detection.py in <module>()
    114 
    115 
--> 116 label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    117 categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
    118 category_index = label_map_util.create_category_index(categories)

AttributeError: module 'utils.label_map_util' has no attribute 'load_labelmap'

Does anyone have an idea what the cause of this problem is?

Thanks.

1条回答
ら.Afraid
2楼-- · 2019-05-11 17:29

In the file ~\Desktop\Objectdetection\models-master\object_detection\utils\label_map_util.py

Try changing this:

from object_detection.protos import string_int_label_map_pb2

to this:

from protos import string_int_label_map_pb2

Explanation:

The function load_labelmap in the module label_map_util isn't accessible because the import of string_int_label_map_pb2 is failing.

You can see this if you look at the output of print(dir(label_map_util)).

When using object_detection.protos:

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'logging', 'text_format', 'tf']

After the changing the relative path to protos the function should be accessible:

['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_validate_label_map', 'convert_label_map_to_categories', 'create_category_index', 'get_label_map_dict', 'load_labelmap', 'logging', 'string_int_label_map_pb2', 'text_format', 'tf']

查看更多
登录 后发表回答