由于只读属性错误,无法设置 protobuf 字段

Can't set protobuf fields due to read-only attribute error

我正在尝试创建一个 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 字段是只读的甚至 可能?我试图从保持纵横比消息中复制字段值,但是 抛出只读错误。

在这种特殊情况下,可以通过使用 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
)]

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

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