AttributeError: module 'tensorflow. compat .v2. __ internal __' has no attribute 'register _ load _ context _ function'

Hi Team, I’m trying to run this “model_main_tf2.py” from this link

I’m getting this attribute error.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/Documents/Indian_License_Plate_Recognition/models/research/object_detection/model_main_tf2.py:32
     30 import tensorflow.compat.v2 as tf
     31 #from object_detection import model_lib_v2
---> 32 import model_lib_v2
     34 flags.DEFINE_string('pipeline_config_path', None, 'Path to pipeline config '
     35                     'file.')
     36 flags.DEFINE_integer('num_train_steps', None, 'Number of train steps.')

File ~/Documents/Indian_License_Plate_Recognition/models/research/object_detection/model_lib_v2.py:30
     27 import tensorflow.compat.v1 as tf
     29 from object_detection import eval_util
---> 30 from object_detection import inputs
     31 from object_detection import model_lib
     32 from object_detection.builders import optimizer_builder

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/object_detection/inputs.py:27
     25 from object_detection.builders import dataset_builder
     26 from object_detection.builders import image_resizer_builder
---> 27 from object_detection.builders import model_builder
     28 from object_detection.builders import preprocessor_builder
     29 from object_detection.core import box_list

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/object_detection/builders/model_builder.py:25
     23 from object_detection.builders import anchor_generator_builder
     24 from object_detection.builders import box_coder_builder
---> 25 from object_detection.builders import box_predictor_builder
     26 from object_detection.builders import hyperparams_builder
     27 from object_detection.builders import image_resizer_builder

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/object_detection/builders/box_predictor_builder.py:20
     18 import collections
     19 import tensorflow.compat.v1 as tf
---> 20 from object_detection.predictors import convolutional_box_predictor
     21 from object_detection.predictors import convolutional_keras_box_predictor
     22 from object_detection.predictors import mask_rcnn_box_predictor

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/object_detection/predictors/convolutional_box_predictor.py:25
     23 import tensorflow.compat.v1 as tf
     24 import tf_slim as slim
---> 25 from object_detection.core import box_predictor
     26 from object_detection.utils import shape_utils
     27 from object_detection.utils import static_shape

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/object_detection/core/box_predictor.py:137
    111     """Implementations must override this method.
    112 
    113     Args:
   (...)
    132           feature map in the input `image_features` list.
    133     """
    134     pass
--> 137 class KerasBoxPredictor(tf.keras.layers.Layer):
    138   """Keras-based BoxPredictor."""
    140   def __init__(self, is_training, num_classes, freeze_batchnorm,
    141                inplace_batchnorm_update, name=None):

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/tensorflow/python/util/lazy_loader.py:58, in LazyLoader.__getattr__(self, item)
     57 def __getattr__(self, item):
---> 58   module = self._load()
     59   return getattr(module, item)

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/tensorflow/python/util/lazy_loader.py:41, in LazyLoader._load(self)
     39 """Load the module and insert it into the parent's globals."""
     40 # Import the target module and insert it into the parent's namespace
---> 41 module = importlib.import_module(self.__name__)
     42 self._parent_module_globals[self._local_name] = module
     44 # Emit a warning if one was specified

File ~/anaconda3/envs/tfsetup/lib/python3.10/importlib/__init__.py:126, in import_module(name, package)
    124             break
    125         level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/keras/api/_v1/__init__.py:8
      3 """Public API for tf. namespace.
      4 """
      6 import sys as _sys
----> 8 from keras.api._v1 import keras
      9 from tensorflow.python.util import module_wrapper as _module_wrapper
     11 if not isinstance(_sys.modules[__name__], _module_wrapper.TFModuleWrapper):

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/keras/api/_v1/keras/__init__.py:27
     25 from keras.api._v1.keras import metrics
     26 from keras.api._v1.keras import mixed_precision
---> 27 from keras.api._v1.keras import models
     28 from keras.api._v1.keras import optimizers
     29 from keras.api._v1.keras import preprocessing

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/keras/api/_v1/keras/models/__init__.py:16
     14 from keras.saving.legacy.model_config import model_from_json
     15 from keras.saving.legacy.model_config import model_from_yaml
---> 16 from keras.saving.legacy.save import load_model
     17 from keras.saving.legacy.save import save_model
     18 from tensorflow.python.util import module_wrapper as _module_wrapper

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/keras/saving/legacy/save.py:24
     22 from keras.saving.legacy import serialization
     23 from keras.saving.legacy.saved_model import load as saved_model_load
---> 24 from keras.saving.legacy.saved_model import load_context
     25 from keras.saving.legacy.saved_model import save as saved_model_save
     26 from keras.utils import traceback_utils

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/keras/saving/legacy/saved_model/load_context.py:68
     64     """Returns whether under a load context."""
     65     return _load_context.in_load_context()
---> 68 tf.__internal__.register_load_context_function(in_load_context)

AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'register_load_context_function'

I checked this thread and tried everything suggested there. but the error doesnt go. Can you please help

@Leelaram_Jayaram,

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/Documents/Indian_License_Plate_Recognition/models/research/object_detection/model_main_tf2.py:32
     30 import tensorflow.compat.v2 as tf
     31 #from object_detection import model_lib_v2
---> 32 import model_lib_v2
     34 flags.DEFINE_string('pipeline_config_path', None, 'Path to pipeline config '
     35                     'file.')
     36 flags.DEFINE_integer('num_train_steps', None, 'Number of train steps.')

Can you try to import the model_lib_v2 from the object_detection module as shown in source code ?

Thank you!

@chunduriv thanks a lot for responding, I was able to import the module. Now I’m currently getting this error.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/Documents/Indian_License_Plate_Recognition/models/research/object_detection/model_main_tf2.py:31
     29 from absl import flags
     30 import tensorflow.compat.v2 as tf
---> 31 from object_detection import model_lib_v2
     33 flags.DEFINE_string('pipeline_config_path', None, 'Path to pipeline config '
     34                     'file.')
     35 flags.DEFINE_integer('num_train_steps', None, 'Number of train steps.')

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/object_detection/model_lib_v2.py:29
     26 import numpy as np
     27 import tensorflow.compat.v1 as tf
---> 29 from object_detection import eval_util
     30 from object_detection import inputs
     31 from object_detection import model_lib

File ~/anaconda3/envs/tfsetup/lib/python3.10/site-packages/object_detection/eval_util.py:53
     44 EVAL_KEYPOINT_METRIC = 'coco_keypoint_metrics'
     46 # A dictionary of metric names to classes that implement the metric. The classes
     47 # in the dictionary must implement
     48 # utils.object_detection_evaluation.DetectionEvaluator interface.
     49 EVAL_METRICS_CLASS_DICT = {
     50     'coco_detection_metrics':
     51         coco_evaluation.CocoDetectionEvaluator,
     52     'coco_keypoint_metrics':
---> 53         coco_evaluation.CocoKeypointEvaluator,
     54     'coco_mask_metrics':
     55         coco_evaluation.CocoMaskEvaluator,
     56     'coco_panoptic_metrics':
     57         coco_evaluation.CocoPanopticSegmentationEvaluator,
     58     'lvis_mask_metrics':
     59         lvis_evaluation.LVISMaskEvaluator,
     60     'oid_challenge_detection_metrics':
     61         object_detection_evaluation.OpenImagesDetectionChallengeEvaluator,
     62     'oid_challenge_segmentation_metrics':
     63         object_detection_evaluation
     64         .OpenImagesInstanceSegmentationChallengeEvaluator,
     65     'pascal_voc_detection_metrics':
     66         object_detection_evaluation.PascalDetectionEvaluator,
     67     'weighted_pascal_voc_detection_metrics':
     68         object_detection_evaluation.WeightedPascalDetectionEvaluator,
     69     'precision_at_recall_detection_metrics':
     70         object_detection_evaluation.PrecisionAtRecallDetectionEvaluator,
     71     'pascal_voc_instance_segmentation_metrics':
     72         object_detection_evaluation.PascalInstanceSegmentationEvaluator,
     73     'weighted_pascal_voc_instance_segmentation_metrics':
     74         object_detection_evaluation.WeightedPascalInstanceSegmentationEvaluator,
     75     'oid_V2_detection_metrics':
     76         object_detection_evaluation.OpenImagesDetectionEvaluator,
     77 }
     79 EVAL_DEFAULT_METRIC = 'coco_detection_metrics'
     82 def write_metrics(metrics, global_step, summary_dir):

AttributeError: module 'object_detection.metrics.coco_evaluation' has no attribute 'CocoKeypointEvaluator'

I’m able to see this CocoKeypointEvaluator class in coco_evalution.py file in my local. I’m not sure why I’m getting this error. Can you please share your thoughts.

Issue resolved by setting the right environment variables for PATH and PYTHONPATH.

could you please clarify more what exactly was the solution

I made a mistake of not adding models/research, models/research/slim, models/research/object_detection in system path, as a result I was getting this errors, when I did the same it got resolved. same with PYTHON_PATH as well.