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



我正在尝试创建这个原型模板的实例。编译并导入后,我运行以下代码:

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消息中复制字段值,但也引发了只读错误。

您的protobuf依赖项可能已过期,请尝试pip3 install --upgrade 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:UsersPerson.kerasdatasetsefficientdet_d7_coco17_tpu-32pipeline.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
)]

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

最新更新