Models: TF Object Detection API incompatible with TF 2.1.X

Created on 6 Dec 2019  ·  25Comments  ·  Source: tensorflow/models

System information

  • What is the top-level directory of the model you are using: object_detection
  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): no
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): MacOS 10.15.1
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): tf-nightly==2.1.0.dev20191203
  • Bazel version (if compiling from source): N/A
  • CUDA/cuDNN version: N/A
  • GPU model and memory: N/A
  • Exact command to reproduce:

from object_detection import model_lib_v2

Describe the problem

Model training fails in versions of TensorFlow greater than 2.0 due to the removal of the contrib library. The evaluation utils still depend on this module which has officially been deprecated in 2.1.X.

Source code / logs

from object_detection import model_lib_v2
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-fe4a9e95a33f> in <module>
----> 1 from object_detection import model_lib_v2

~/.pyenv/versions/3.7.5/envs/fritzml-tf2/lib/python3.7/site-packages/object_detection/model_lib_v2.py in <module>
     24 import tensorflow as tf
     25 
---> 26 from object_detection import eval_util
     27 from object_detection import inputs
     28 from object_detection import model_lib

~/.pyenv/versions/3.7.5/envs/fritzml-tf2/lib/python3.7/site-packages/object_detection/eval_util.py in <module>
     38 from object_detection.utils import visualization_utils as vis_utils
     39 
---> 40 slim = tf.contrib.slim
     41 
     42 # A dictionary of metric names to classes that implement the metric. The classes

AttributeError: module 'tensorflow' has no attribute 'contrib'
research bug

Most helpful comment

Stumbled over the same problem, since the inference example notebook left the impression that TF2 is actually supported https://github.com/tensorflow/models/blob/fc4ce16b50e68bb15370d64d79021bd8578e8d86/research/object_detection/object_detection_tutorial.ipynb . However training a model for example won't work, due to the deprecated dependencies. Because slim has dependencies on contrib (e.g. tensorflow.framework.python.ops.variables) and since it was extracted into a separate package tf_slim, not only code in the object dection module needs to be adapted (e.g. dependencies like tensorflow.contrib.quantize.python) , but also a TF2 compatible tf_slim version is needed (as long as slim is a dependency). Would be great if the object detection package also would be properly versioned, so that it is clear which object_detection version can be used with which TF version.

All 25 comments

Any udates on this yet?

is there a workaround?

Hej,
same problem here: Would like to go on with Tensorflow 2.0.0
Had this working lately, but obviously not on TF2.
Using the tf docker container under Ubuntu 18.04.
Regards,
Jarvis

Command: python model_builder_test.py
Error:

Traceback (most recent call last):
  File "model_builder_test.py", line 23, in <module>
    from object_detection.builders import model_builder
  File "/tf/models/research/object_detection/builders/model_builder.py", line 22, in <module>
    from object_detection.builders import box_predictor_builder
  File "/tf/models/research/object_detection/builders/box_predictor_builder.py", line 20, in <module>
    from object_detection.predictors import convolutional_box_predictor
  File "/tf/models/research/object_detection/predictors/convolutional_box_predictor.py", line 23, in <module>
    slim = tf.contrib.slim
AttributeError: module 'tensorflow' has no attribute 'contrib'

I had /object_detection/legacy/train.py running with tensorflow 2.0.0-__beta1__, which requires _tensorflow.contrib_.

__EDIT__ 23.12.2019
found: migrate TF2.0, but i don't understand how to adapt. Also I would like not to roll back to TF1.x.

if the import is just for slim, the workaround is to install tf-slim directly: https://github.com/google-research/tf-slim and make sure not other dependency on tf.contrib

Hej,
tried the same call, but installed slim and changed slim=tf.contrib.slim in /models/research/object_detection/predictors/convolutional_box_predictor.py to import tf_slim as slim.
But sadly tf_slim somehow requires tf.contrib.

  File "/tf/models/research/object_detection/predictors/convolutional_box_predictor.py", line 24, in <module>
    import tf_slim as slim
  File "/usr/local/lib/python3.6/dist-packages/tf_slim/__init__.py", line 23, in <module>
    from tf_slim import evaluation
  File "/usr/local/lib/python3.6/dist-packages/tf_slim/evaluation.py", line 131, in <module>
    from tensorflow.contrib.training.python.training import evaluation
ModuleNotFoundError: No module named 'tensorflow.contrib'

Stumbled over the same problem, since the inference example notebook left the impression that TF2 is actually supported https://github.com/tensorflow/models/blob/fc4ce16b50e68bb15370d64d79021bd8578e8d86/research/object_detection/object_detection_tutorial.ipynb . However training a model for example won't work, due to the deprecated dependencies. Because slim has dependencies on contrib (e.g. tensorflow.framework.python.ops.variables) and since it was extracted into a separate package tf_slim, not only code in the object dection module needs to be adapted (e.g. dependencies like tensorflow.contrib.quantize.python) , but also a TF2 compatible tf_slim version is needed (as long as slim is a dependency). Would be great if the object detection package also would be properly versioned, so that it is clear which object_detection version can be used with which TF version.

Are there any updates? I still have the same issue. "tf_upgrade_v2" does not work? Or, is there time expectation of release?

I have the same issue

its already months issue pending... =( waiting for the upgrade, or at least some timeline..

I'd like to note there is already an older issue discussing the same problem: https://github.com/tensorflow/models/issues/7920

Edit: https://github.com/tensorflow/models/issues/6423#issuecomment-582169024 oops :laughing:

I'd like to note there is already an older issue discussing the same problem: #7920

I'd like to note there is already an older issue discussing the same problem: #7920

Same issue, any updates?

As the migration might not be trivial, could you use tf 1.15 for research/object_detection and wait the project owner to announce support for tf 2.x?

Still waiting for TF2.0 support since May 2019. frust

Still waiting for this. Any updates?

我感觉google想要放弃tensorflow/models这个库, 不然TF hub的意义何在呢

Any updates regarding the tf.contrib dependencies??? Update scripts wont work.

any updates on this?

I am also interested in TF2 support.

Yeah me too, TF2 compatability feels natural especially as most of the documentation references Tensorflow without specifying a version.

Seems like a lot of people are waiting on this. At this point, I think the best thing to do would be to look at some newer repos that have been released. The efficientdet model looks like a great option: https://github.com/google/automl/tree/master/efficientdet

In case anyone wants to work with tensorflow 2.0:
Run the below code

from collections import deque
import numpy as np
import argparse
from sklearn.metrics import pairwise
from imutils.video import FPS
import os
import sys
import tensorflow as tf
from imutils.video import VideoStream
import six.moves.urllib as urllib
import cv2
import imutils
import time
from imutils.video import FPS
from sklearn.metrics import pairwise
import copy
import pathlib
from collections import defaultdict

from utils import ops as utils_ops
from utils import label_map_util
from utils import visualization_utils as vis_util

utils_ops.tf = tf.compat.v1
tf.gfile = tf.io.gfile
PATH_TO_LABELS = '../../bigdata/data/mscoco_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)

model_name = 'ssdlite_mobilenet_v2_coco_2018_05_09'
model_dir =  "../../bigdata/models/" + model_name + "/saved_model"
detection_model = tf.saved_model.load(str(model_dir))
detection_model = detection_model.signatures['serving_default']

def run_inference_for_single_image(model, image):
  image = np.asarray(image)
  input_tensor = tf.convert_to_tensor(image)
  input_tensor = input_tensor[tf.newaxis,...]

  # output_dict is a dict  with keys detection_classes , num_detections , detection_boxes(4 coordinates of each box) , detection_scores for 100 boxes
  output_dict = model(input_tensor)
  # print(1,output_dict)

  # num_detections gives number of objects in current frame
  num_detections = int(output_dict.pop('num_detections'))
  # print(2,num_detections)

  # output_dict is a dict  with keys detection_classes , detection_boxes(4 coordinates of each box) , detection_scores for num_detections boxes
  output_dict = {key:value[0, :num_detections].numpy() 
                 for key,value in output_dict.items()}

  # adding num_detections that was earlier popped out
  output_dict['num_detections'] = num_detections

  # converting all values in detection_classes as ints.
  output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)
  print(5,output_dict)

  # Handle models with masks:
  if 'detection_masks' in output_dict:
    # Reframe the the bbox mask to the image size.
    detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
              output_dict['detection_masks'], output_dict['detection_boxes'],
               image.shape[0], image.shape[1])   
    detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
                                       tf.uint8)
    output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()
    print(5,detection_masks_reframed) 
  return output_dict


def show_inference(model, image_path):
  # the array based representation of the image will be used later in order to prepare the
  # result image with boxes and labels on it.
  # image_np = np.array(Image.open(image_path))
  image_np = np.array(image_path)
  print(image_np.shape)

  # Actual detection.
  output_dict = run_inference_for_single_image(model, image_np)
  # Visualization of the results of a detection.
  vis_util.visualize_boxes_and_labels_on_image_array(
      image_np,
      output_dict['detection_boxes'],
      output_dict['detection_classes'],
      output_dict['detection_scores'],
      category_index,
      instance_masks=output_dict.get('detection_masks_reframed', None),
      use_normalized_coordinates=True,
      line_thickness=8)
  return image_np

cap=cv2.VideoCapture('../../videos/a.mp4')
time.sleep(2.0)
while True:
    (grabbed, frame) = cap.read()
    frame=show_inference(detection_model, frame)
    cv2.imshow("version", frame)
    key=cv2.waitKey(1)
    if key & 0xFF == ord("q"):
        break

cap.release()
cv2.destroyAllWindows() 

Make sure the files from utils folder are imported correctly, else use sys module to append the location of utils folder.

In case anyone wants to work with tensorflow 2.0:
Run the below code

That is for detection. Did you succeed training using TF2?

In case anyone wants to work with tensorflow 2.0:
Run the below code

That is for detection. Did you succeed training using TF2?

Haven't tried that yet. It should work anyways. What's the error you getting?
If any of these could help:
https://github.com/AshishGusain17/via_google_colab

Was this page helpful?
0 / 5 - 0 ratings