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

2024-04-27 19:45:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建已编译和导入的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字段是只读的,甚至可能?我尝试从keep aspect ratio消息中复制字段值,但是抛出只读错误


Tags: toimagetrueobjectminmaxprotoresizer
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
)]

这样就避免了只读属性的问题,尽管我仍然不知道它是如何被解释为只读的,或者如何正常地解决这个问题

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

相关问题 更多 >