How to fix this Import error problem when training a model in google colab

Traceback (most recent call last):
File “model_main_tf2.py”, line 31, in
from object_detection import model_lib_v2
File “/content/drive/MyDrive/ Tf2/object_detection/object_detection/model_lib_v2.py”, line 29, in
from object_detection import eval_util
File “/content/drive/MyDrive/ Tf2/object_detection/object_detection/eval_util.py”, line 35, in
from object_detection.metrics import coco_evaluation
File “/content/drive/MyDrive/ Tf2/object_detection/object_detection/metrics/coco_evaluation.py”, line 28, in
from object_detection.utils import object_detection_evaluation
File “/content/drive/MyDrive/ Tf2/object_detection/object_detection/utils/object_detection_evaluation.py”, line 46, in
from object_detection.utils import label_map_util
File “/content/drive/MyDrive/ Tf2/object_detection/object_detection/utils/label_map_util.py”, line 29, in
from object_detection.protos import string_int_label_map_pb2
File “/content/drive/MyDrive/ Tf2/object_detection/object_detection/protos/string_int_label_map_pb2.py”, line 5, in
from google.protobuf.internal import builder as _builder
ImportError: cannot import name ‘builder’ from ‘google.protobuf.internal’ (/usr/local/lib/python3.7/dist-packages/google/protobuf/internal/init.py)

You need to upgrade to the latest version of the protobuf package as shown below

!pip install --upgrade protobuf

I have the same Error and It doesn’t work

@Mohamed_Amr, Please share standalone code to replicate your issue? Thank you.

Traceback (most recent call last):
File “Tensorflow\models\research\object_detection\model_main_tf2.py”, line 31, in
from object_detection import model_lib_v2
File “C:\Users\C.m™\AppData\Local\Programs\Python\Python36\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\model_lib_v2.py”, line 29, in
from object_detection import eval_util
File “C:\Users\C.m™\AppData\Local\Programs\Python\Python36\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\eval_util.py”, line 35, in
from object_detection.metrics import coco_evaluation
File “C:\Users\C.m™\AppData\Local\Programs\Python\Python36\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\metrics\coco_evaluation.py”, line 25, in
from object_detection.metrics import coco_tools
File “C:\Users\C.m™\AppData\Local\Programs\Python\Python36\lib\site-packages\object_detection-0.1-py3.6.egg\object_detection\metrics\coco_tools.py”, line 51, in
from pycocotools import coco
File “C:\Users\C.m™\AppData\Local\Programs\Python\Python36\lib\site-packages\pycocotools\coco.py”, line 49, in
import matplotlib.pyplot as plt
File “C:\Users\C.m™\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py”, line 2336, in
switch_backend(rcParams[“backend”])
File “C:\Users\C.m™\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py”, line 276, in switch_backend
class backend_mod(matplotlib.backend_bases.Backend):
File “C:\Users\C.m™\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\pyplot.py”, line 277, in backend_mod
locals().update(vars(importlib.import_module(backend_name)))
File "C:\Users\C.m™\AppData\Local\Programs\Python\Python36\lib\importlib_init
.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named ‘matplotlib_inline’

Please try to install matplotlib-inline

pip install matplotlib-inline

Thanks for sharing the error stack trace. For quick troubleshooting please share the complete standalone code to replicate your issue reported above.

Greetings! I’m also unable to start training with the module.
Can you suggest a solution?

Traceback (most recent call last):
File “/content/models/research/object_detection/model_main_tf2.py”, line 30, in
import tensorflow.compat.v2 as tf
File “/usr/local/lib/python3.7/dist-packages/tensorflow/init.py”, line 37, in
from tensorflow.python.tools import module_util as _module_util
File “/usr/local/lib/python3.7/dist-packages/tensorflow/python/init.py”, line 37, in
from tensorflow.python.eager import context
File “/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/context.py”, line 29, in
from tensorflow.core.framework import function_pb2
File “/usr/local/lib/python3.7/dist-packages/tensorflow/core/framework/function_pb2.py”, line 16, in
from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2
File “/usr/local/lib/python3.7/dist-packages/tensorflow/core/framework/attr_value_pb2.py”, line 16, in
from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
File “/usr/local/lib/python3.7/dist-packages/tensorflow/core/framework/tensor_pb2.py”, line 16, in
from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2
File “/usr/local/lib/python3.7/dist-packages/tensorflow/core/framework/resource_handle_pb2.py”, line 16, in
from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
File “/usr/local/lib/python3.7/dist-packages/tensorflow/core/framework/tensor_shape_pb2.py”, line 42, in
serialized_options=None, file=DESCRIPTOR),
File “/usr/local/lib/python3.7/dist-packages/google/protobuf/descriptor.py”, line 560, in new
_message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:

  1. Downgrade the protobuf package to 3.20.x or lower.
  2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
1 Like

Please provide standalone code to reproduce the issue reported above? Thank you.

ImportError: cannot import name ‘model_lib_v2’ from ‘object_detection’

Hi, I found this issue in training the model using the codes below :

python Tensorflow/models/research/object_detection/model_main_tf2.py --model_dir=Tensorflow/workspace/models/my_ssd_mobnet --pipeline_config_path=Tensorflow/workspace/models/my_ssd_mobnet/pipeline.config --num_train_steps=10000

It appeared like this :

91: RequestsDependencyWarning: urllib3 (1.26.13) or chardet (3.0.4) doesn’t match a supported version!
RequestsDependencyWarning)
Traceback (most recent call last):
File “Tensorflow/models/research/object_detection/model_main_tf2.py”, line 31, in
from object_detection import model_lib_v2

ImportError: cannot import name ‘model_lib_v2’ from ‘object_detection’

How to fix this error?
I upgraded the protobuf but it still didn’t work.

Traceback (most recent call last):
File “C:\Users\hp\Desktop\Object Detection\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py”, line 29, in
from object_detection.utils import dataset_util, label_map_util
File “C:\Users\hp.pyenv\pyenv-win\versions\3.9.7\lib\site-packages\object_detection\utils\label_map_util.py”, line 21, in
from object_detection.protos import string_int_label_map_pb2
ImportError: cannot import name ‘string_int_label_map_pb2’ from ‘object_detection.protos’ (C:\Users\hp.pyenv\pyenv-win\versions\3.9.7\lib\site-packages\object_detection\protos_init_.py)
Traceback (most recent call last):
File “C:\Users\hp\Desktop\Object Detection\RealTimeObjectDetection\Tensorflow\scripts\generate_tfrecord.py”, line 29, in
from object_detection.utils import dataset_util, label_map_util
File “C:\Users\hp.pyenv\pyenv-win\versions\3.9.7\lib\site-packages\object_detection\utils\label_map_util.py”, line 21, in
from object_detection.protos import string_int_label_map_pb2
ImportError: cannot import name ‘string_int_label_map_pb2’ from ‘object_detection.protos’ (C:\Users\hp.pyenv\pyenv-win\versions\3.9.7\lib\site-packages\object_detection\protos_init_.py)

Alos, downgrade the protobuf package, still throws an error

@chunduriv Hi, I tried to run model_main_tf2.py from this models/research/object_detection at master · tensorflow/models · GitHub

I got the below attribute error, I tried to upgrade protobuff as well but no luck,

---------------------------------------------------------------------------
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: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'

Can you please help.

Traceback (most recent call last):
File “C:\Users\abhis\YouTube\Tensorflow Object Detection\TFODCourse\Tensorflow\models\research\object_detection\model_main_tf2.py”, line 31, in
from object_detection import model_lib_v2
ImportError: cannot import name ‘model_lib_v2’ from ‘object_detection’ (C:\Users\abhis\AppData\Local\Programs\Python\Python39\lib\site-packages\object_detection_init_.py)

!python {SCRIPTS_PATH + ‘/generate_tfrecord.py’} -x {IMAGE_PATH + ‘/train’} -l {ANNOTATION_PATH + ‘/label_map.pbtxt’} -o {ANNOTATION_PATH + ‘/train.record’}
!python {SCRIPTS_PATH + ‘/generate_tfrecord.py’} -x{IMAGE_PATH + ‘/test’} -l {ANNOTATION_PATH + ‘/label_map.pbtxt’} -o {ANNOTATION_PATH + ‘/test.record’}

Traceback (most recent call last):
File “Tensorflow/scripts/generate_tfrecord.py”, line 27, in
import tensorflow.compat.v1 as tf
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_init_.py”, line 38, in
from tensorflow.python.tools import module_util as module_util
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python_init
.py", line 45, in
from tensorflow.python.feature_column import feature_column_lib as feature_column
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\feature_column\feature_column_lib.py”, line 18, in
from tensorflow.python.feature_column.feature_column import *
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\feature_column\feature_column.py”, line 143, in
from tensorflow.python.layers import base
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\layers\base.py”, line 16, in
from tensorflow.python.keras.legacy_tf_layers import base
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras_init_.py”, line 25, in
from tensorflow.python.keras import models
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\models.py”, line 22, in
from tensorflow.python.keras.engine import functional
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\functional.py”, line 32, in
from tensorflow.python.keras.engine import training as training_lib
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py”, line 53, in
from tensorflow.python.keras.saving import hdf5_format
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py”, line 37, in
import h5py
File “C:\ProgramData\Anaconda3\lib\site-packages\h5py_init_.py”, line 46, in
from .conv import register_converters as register_converters
File “h5py\h5t.pxd”, line 14, in init h5py.conv
File “h5py\h5t.pyx”, line 293, in init h5py.h5t
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy_init
.py", line 320, in getattr
raise AttributeError("module {!r} has no attribute "
AttributeError: module ‘numpy’ has no attribute ‘typeDict’
Traceback (most recent call last):
File “Tensorflow/scripts/generate_tfrecord.py”, line 27, in
import tensorflow.compat.v1 as tf
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow_init
.py", line 38, in
from tensorflow.python.tools import module_util as module_util
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python_init
.py", line 45, in
from tensorflow.python.feature_column import feature_column_lib as feature_column
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\feature_column\feature_column_lib.py”, line 18, in
from tensorflow.python.feature_column.feature_column import *
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\feature_column\feature_column.py”, line 143, in
from tensorflow.python.layers import base
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\layers\base.py”, line 16, in
from tensorflow.python.keras.legacy_tf_layers import base
File "C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras_init
.py", line 25, in
from tensorflow.python.keras import models
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\models.py”, line 22, in
from tensorflow.python.keras.engine import functional
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\functional.py”, line 32, in
from tensorflow.python.keras.engine import training as training_lib
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py”, line 53, in
from tensorflow.python.keras.saving import hdf5_format
File “C:\ProgramData\Anaconda3\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py”, line 37, in
import h5py
File “C:\ProgramData\Anaconda3\lib\site-packages\h5py_init_.py”, line 46, in
from ._conv import register_converters as _register_converters
File “h5py\h5t.pxd”, line 14, in init h5py.conv
File “h5py\h5t.pyx”, line 293, in init h5py.h5t
File "C:\ProgramData\Anaconda3\lib\site-packages\numpy_init
.py", line 320, in getattr
raise AttributeError("module {!r} has no attribute "
AttributeError: module ‘numpy’ has no attribute ‘typeDict’

what is this error