【问题标题】:Can't set protobuf fields due to read-only attribute error由于只读属性错误,无法设置 protobuf 字段
【发布时间】:2021-05-04 03:03:04
【问题描述】:

我正在尝试创建 this proto template. 的实例编译并导入,我运行以下代码:

from object_detection.protos import image_resizer_pb2

resizer = image_resizer_pb2.ImageResizer()
resizer.keep_aspect_ratio_resizer.min_dimension = 1536
resizer.keep_aspect_ratio_resizer.max_dimension = 1536
resizer.keep_aspect_ratio_resizer.pad_to_max_dimension = True

并得到这个错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-50-30cfb0c18c35> in <module>
      3 
      4 resizer = image_resizer_pb2.ImageResizer()
----> 5 resizer.keep_aspect_ratio_resizer.min_dimension = 1536
      6 resizer.keep_aspect_ratio_resizer.max_dimension = 1536
      7 resizer.keep_aspect_ratio_resizer.pad_to_max_dimension = True

AttributeError: 'KeepAspectRatioResizer' object attribute 'min_dimension' is read-only

除了 proto 文件中没有任何内容甚至开始建议它应该是只读的,或者只读的 protobuf 字段甚至可能?我尝试从保持纵横比消息中复制字段值,但 会引发只读错误。

【问题讨论】:

    标签: python protocol-buffers object-detection-api


    【解决方案1】:

    您的 protobuf 依赖项可能已过期,请尝试 pip3 install --upgrade protobuf

    【讨论】:

    • 就是这样。 Anaconda 版本已过时; 3.14.0;升级到 3.15.8 让我直接创建消息。
    【解决方案2】:

    在这种特殊情况下,可以通过使用object_detection.utils/config_utils 文件并直接从模型配置文件加载我想要复制的图像配置来解决无法手动创建模板的问题:

    from object_detection.protos import image_resizer_pb2
    from object_detection.utils import config_util as c
    
    config =  c.get_configs_from_pipeline_file(r"C:\Users\Person\.keras\datasets\efficientdet_d7_coco17_tpu-32\pipeline.config")
    image_config = c.get_image_resizer_config(config['model'])
    print(image_config.ListFields())
    
    [(<google.protobuf.descriptor.FieldDescriptor object at 0x000002446C225F40>, min_dimension: 1536
    max_dimension: 1536
    pad_to_max_dimension: true
    )]
    

    因此回避了只读属性的问题,尽管我仍然不明白它是如何被解释为只读的,或者如何正常解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 2022-07-14
      相关资源
      最近更新 更多