'AttributeError: module 'object_detection.protos.faster_rcnn_pb2' has no attribute 'AttentionPosition' while model_builders is getting imported

While trying to run the following code:-

import os
import tensorflow as tf
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as viz_utils
from object_detection.builders import model_builder
from object_detection.utils import config_util

I am facing the following errors:-

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [16], in <cell line: 5>()
      3 from object_detection.utils import label_map_util
      4 from object_detection.utils import visualization_utils as viz_utils
----> 5 from object_detection.builders import model_builder
      6 from object_detection.utils import config_util

File ~\anaconda3\envs\anprsys\lib\site-packages\object_detection\builders\model_builder.py:36, in <module>
     34 from object_detection.meta_architectures import ssd_meta_arch
     35 from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res
---> 36 from object_detection.models import faster_rcnn_inception_v2_feature_extractor as frcnn_inc_v2
     37 from object_detection.models import faster_rcnn_nas_feature_extractor as frcnn_nas
     38 from object_detection.models import faster_rcnn_pnas_feature_extractor as frcnn_pnas

File ~\anaconda3\envs\anprsys\lib\site-packages\object_detection\meta_architectures\context_rcnn_meta_arch.py:42, in <module>
     37 from object_detection.utils import tf_version
     39 _UNINITIALIZED_FEATURE_EXTRACTOR = '__uninitialized__'
---> 42 class ContextRCNNMetaArch(faster_rcnn_meta_arch.FasterRCNNMetaArch):
     43   """Context R-CNN Meta-architecture definition."""
     45   def __init__(self,
     46                is_training,
     47                num_classes,
   (...)
     95                    faster_rcnn_pb2.AttentionPosition.POST_BOX_CLASSIFIER)
     96                ):

File ~\anaconda3\envs\anprsys\lib\site-packages\object_detection\meta_architectures\context_rcnn_meta_arch.py:95, in ContextRCNNMetaArch()
     42 class ContextRCNNMetaArch(faster_rcnn_meta_arch.FasterRCNNMetaArch):
     43   """Context R-CNN Meta-architecture definition."""
     45   def __init__(self,
     46                is_training,
     47                num_classes,
     48                image_resizer_fn,
     49                feature_extractor,
     50                number_of_stages,
     51                first_stage_anchor_generator,
     52                first_stage_target_assigner,
     53                first_stage_atrous_rate,
     54                first_stage_box_predictor_arg_scope_fn,
     55                first_stage_box_predictor_kernel_size,
     56                first_stage_box_predictor_depth,
     57                first_stage_minibatch_size,
     58                first_stage_sampler,
     59                first_stage_non_max_suppression_fn,
     60                first_stage_max_proposals,
     61                first_stage_localization_loss_weight,
     62                first_stage_objectness_loss_weight,
     63                crop_and_resize_fn,
     64                initial_crop_size,
     65                maxpool_kernel_size,
     66                maxpool_stride,
     67                second_stage_target_assigner,
     68                second_stage_mask_rcnn_box_predictor,
     69                second_stage_batch_size,
     70                second_stage_sampler,
     71                second_stage_non_max_suppression_fn,
     72                second_stage_score_conversion_fn,
     73                second_stage_localization_loss_weight,
     74                second_stage_classification_loss_weight,
     75                second_stage_classification_loss,
     76                second_stage_mask_prediction_loss_weight=1.0,
     77                hard_example_miner=None,
     78                parallel_iterations=16,
     79                add_summaries=True,
     80                clip_anchors_to_image=False,
     81                use_static_shapes=False,
     82                resize_masks=True,
     83                freeze_batchnorm=False,
     84                return_raw_detections_during_predict=False,
     85                output_final_box_features=False,
     86                output_final_box_rpn_features=False,
     87                attention_bottleneck_dimension=None,
     88                attention_temperature=None,
     89                use_self_attention=False,
     90                use_long_term_attention=True,
     91                self_attention_in_sequence=False,
     92                num_attention_heads=1,
     93                num_attention_layers=1,
     94                attention_position=(
---> 95                    faster_rcnn_pb2.AttentionPosition.POST_BOX_CLASSIFIER)
     96                ):
     97     """ContextRCNNMetaArch Constructor.
     98 
     99     Args:
   (...)
    253         grid_anchor_generator.GridAnchorGenerator.
    254     """
    255     super(ContextRCNNMetaArch, self).__init__(
    256         is_training,
    257         num_classes,
   (...)
    297         output_final_box_features=output_final_box_features,
    298         output_final_box_rpn_features=output_final_box_rpn_features)

AttributeError: module 'object_detection.protos.faster_rcnn_pb2' has no attribute 'AttentionPosition'

I did a search in the net and there doesn't seems to be a clear cut reason for the error and any way to solve it.

There seems to be something of adding to the path which I have already done. Added both tensorflow/models/research/obj-detection to the path.

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source