Models: Compatibility issues with Python3 in the refactored API

Created on 2 Aug 2018  路  12Comments  路  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):16.04
  • TensorFlow installed from (source or binary):Binary
  • TensorFlow version (use command below):1.8
  • Bazel version (if compiling from source):N/A
  • CUDA/cuDNN version:9.0
  • GPU model and memory:N/A
  • Exact command to reproduce:N/A

Describe the problem

With the refactored API, which was done after the Open Image Challenge was hosted on Kaggle, there are too many incompatibilities issues when using the API with Python3. Two of the sample errors that I faced today while trying to run the mobilenet version of RetinaNet are:

models/research/object_detection/models/feature_map_generators.py", line 225, in fpn_top_down_feature_maps
reversed(zip(output_feature_map_keys, output_feature_maps_list)))
TypeError: argument to reversed() must be a sequence
models/research/object_detection/model_lib.py", line 282, in model_fn
    losses = [loss_tensor for loss_tensor in losses_dict.itervalues()]
AttributeError: 'dict' object has no attribute 'itervalues'

Both the issues are because of the syntax error if you run it with Python3. The first error can be resolved by using the line reversed(list(zip(output_feature_map_keys, output_feature_maps_list))))at 225 in feature_map_generators.py and the second error is because of the fact that itervalues() was removed in Python3

Can you please look into all the files once again?

Most helpful comment

Another problem related to Python3:
models/research/object_detection/utils/object_detection_evaluation.py", line 290, in evaluate
category_name = unicode(category_name, 'utf-8')
NameError: name 'unicode' is not defined

replace this: category_name = unicode(category_name, 'utf-8')
to this: category_name = str(category_name, 'utf-8')

All 12 comments

I am facing similar issue while training:
models/research/object_detection/model_lib.py", line 395, in model_fn
eval_metric_ops = {str(k): v for k, v in eval_metric_ops.iteritems()}
AttributeError: 'dict' object has no attribute 'iteritems'

Python3 does not have iteritems() anymore, should use items() instead.

Another problem related to Python3:
models/research/object_detection/utils/object_detection_evaluation.py", line 290, in evaluate
category_name = unicode(category_name, 'utf-8')
NameError: name 'unicode' is not defined

replace this: category_name = unicode(category_name, 'utf-8')
to this: category_name = str(category_name, 'utf-8')

If I run the API with Python2, as per these instructions given in the docs:

python object_detection/model_main.py \
    --pipeline_config_path=${PIPELINE_CONFIG_PATH} \
    --model_dir=${MODEL_DIR} \
    --num_train_steps=${NUM_TRAIN_STEPS} \
    --num_eval_steps=${NUM_EVAL_STEPS} \
    --alsologtostderr

I cannot see the training steps progress in my terminal.

AakashKumarNain, add tf.logging.set_verbosity(tf.logging.INFO) to model_main after the imports.

Did you meet the problem:
AttributeError: module 'json.encoder' has no attribute 'FLOAT_REPR'
when you run the coco_tools_test.py in tensorflow/tf.models/models/research/object_detection/metrics/coco_tools_test.py

@fernandorovai I met with the same issue with iteritems(). After changing it to items() there is some other error:

INFO:tensorflow:Loading and preparing annotation results...
2018-09-07 11:58:48.515029: W tensorflow/core/framework/op_kernel.cc:1263] Invalid argument: TypeError: can't pickle dict_values objects
Traceback (most recent call last):

  File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/script_ops.py", line 206, in __call__
    ret = func(*args)

  File ".../tensorflow/models/research/object_detection/metrics/coco_evaluation.py", line 339, in first_value_func
    self._metrics = self.evaluate()

  File ".../tensorflow/models/research/object_detection/metrics/coco_evaluation.py", line 193, in evaluate
    self._detection_boxes_list)

  File ".../tensorflow/models/research/object_detection/metrics/coco_tools.py", line 118, in LoadAnnotations
    results.dataset['categories'] = copy.deepcopy(self.dataset['categories'])

  File "/usr/lib/python3.7/copy.py", line 169, in deepcopy
    rv = reductor(4)

TypeError: can't pickle dict_values objects


Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1278, in _do_call
    return fn(*args)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1263, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1350, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: TypeError: can't pickle dict_values objects
Traceback (most recent call last):

  File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/script_ops.py", line 206, in __call__
    ret = func(*args)

  File ".../tensorflow/models/research/object_detection/metrics/coco_evaluation.py", line 339, in first_value_func
    self._metrics = self.evaluate()

  File ".../tensorflow/models/research/object_detection/metrics/coco_evaluation.py", line 193, in evaluate
    self._detection_boxes_list)

  File ".../tensorflow/models/research/object_detection/metrics/coco_tools.py", line 118, in LoadAnnotations
    results.dataset['categories'] = copy.deepcopy(self.dataset['categories'])

  File "/usr/lib/python3.7/copy.py", line 169, in deepcopy
    rv = reductor(4)

TypeError: can't pickle dict_values objects


     [[Node: PyFunc_1 = PyFunc[Tin=[], Tout=[DT_FLOAT], token="pyfunc_3", _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
     [[Node: map_1/while/TensorArrayWrite/TensorArrayWriteV3/_1553 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_623_map_1/while/TensorArrayWrite/TensorArrayWriteV3", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](^_cloopmap_1/while/NextIteration_2/_1443)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File ".../tensorflow/models/research/object_detection/model_main.py", line 105, in <module>
    tf.app.run()
  File "/usr/lib/python3.7/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "/home/yjin/DL/tensorflow/models/research/object_detection/model_main.py", line 101, in main
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/training.py", line 451, in train_and_evaluate
    return executor.run()
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/training.py", line 590, in run
    return self.run_local()
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/training.py", line 691, in run_local
    saving_listeners=saving_listeners)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/estimator.py", line 376, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/estimator.py", line 1145, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/estimator.py", line 1173, in _train_model_default
    saving_listeners)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/estimator.py", line 1451, in _train_with_estimator_spec
    _, loss = mon_sess.run([estimator_spec.train_op, estimator_spec.loss])
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/monitored_session.py", line 583, in run
    run_metadata=run_metadata)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/monitored_session.py", line 1059, in run
    run_metadata=run_metadata)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/monitored_session.py", line 1150, in run
    raise six.reraise(*original_exc_info)
  File "/usr/lib/python3.7/site-packages/six.py", line 693, in reraise
    raise value
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/monitored_session.py", line 1135, in run
    return self._sess.run(*args, **kwargs)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/monitored_session.py", line 1215, in run
    run_metadata=run_metadata))
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/basic_session_run_hooks.py", line 464, in after_run
    if self._save(run_context.session, global_step):
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/basic_session_run_hooks.py", line 489, in _save
    if l.after_save(session, step):
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/training.py", line 497, in after_save
    self._evaluate(global_step_value)  # updates self.eval_result
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/training.py", line 517, in _evaluate
    self._evaluator.evaluate_and_export())
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/training.py", line 884, in evaluate_and_export
    hooks=self._eval_spec.hooks)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/estimator.py", line 470, in evaluate
    output_dir=self.eval_dir(name))
  File "/usr/lib/python3.7/site-packages/tensorflow/python/estimator/estimator.py", line 1501, in _evaluate_run
    config=self._session_config)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/evaluation.py", line 212, in _evaluate_once
    session.run(eval_ops, feed_dict)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/monitored_session.py", line 695, in __exit__
    self._close_internal(exception_type)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/monitored_session.py", line 727, in _close_internal
    h.end(self._coordinated_creator.tf_sess)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/training/basic_session_run_hooks.py", line 824, in end
    self._final_ops, feed_dict=self._final_ops_feed_dict)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 877, in run
    run_metadata_ptr)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1100, in _run
    feed_dict_tensor, options, run_metadata)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1272, in _do_run
    run_metadata)
  File "/usr/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1291, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: TypeError: can't pickle dict_values objects
Traceback (most recent call last):

  File "/usr/lib/python3.7/site-packages/tensorflow/python/ops/script_ops.py", line 206, in __call__
    ret = func(*args)

  File ".../tensorflow/models/research/object_detection/metrics/coco_evaluation.py", line 339, in first_value_func
    self._metrics = self.evaluate()

  File ".../tensorflow/models/research/object_detection/metrics/coco_evaluation.py", line 193, in evaluate
    self._detection_boxes_list)

  File ".../tensorflow/models/research/object_detection/metrics/coco_tools.py", line 118, in LoadAnnotations
    results.dataset['categories'] = copy.deepcopy(self.dataset['categories'])

  File "/usr/lib/python3.7/copy.py", line 169, in deepcopy
    rv = reductor(4)

TypeError: can't pickle dict_values objects

@fisheess Your error and a fix has been reported in https://github.com/tensorflow/models/issues/4780

@sraimund Thanks. It works.

Closing as this is resolved

Did you meet the problem:
AttributeError: module 'json.encoder' has no attribute 'FLOAT_REPR'
when you run the coco_tools_test.py in tensorflow/tf.models/models/research/object_detection/metrics/coco_tools_test.py

Did you reslove this question?

I still have this problem.

Another problem related to Python3:
models/research/object_detection/utils/object_detection_evaluation.py", line 290, in evaluate
category_name = unicode(category_name, 'utf-8')
NameError: name 'unicode' is not defined

replace this: category_name = unicode(category_name, 'utf-8') to this: category_name = str(category_name, 'utf-8')

I am new to object detection api. What works for me is to make the replacement in "object_detection_evaluation.py" under "x:...\Lib\site-packages\object_detection-0.1-py3.7.egg\object_detection". I don't know the reason, but it also works.

Was this page helpful?
0 / 5 - 0 ratings