如何修复对象检测中的 'Constructing a tf.Module without calling the super constructor is not supported'-api.model_main.py

How to fix 'Constructing a tf.Module without calling the super constructor is not supported' in object-detection-api.model_main.py

我是对象检测的新手-api,成功安装了API, python object_detection_tutorial.ipynb 工作正常。 但是当遵循 this tutorial 时,我从 tensorflow/python/module/module.py

得到 ValueError

this tutorial, iv'e 成功地完成了最后一步 "trainnig model" 之前的所有事情, 比我 运行 这个命令时:

python model_main.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config

(所有路径都正确) 我收到此错误:

ValueError:
Constructing a tf.Module without calling the super constructor is not supported. Add the following as the first line in your __init__ method:

super(FasterRCNNMetaArch, self).__init__()

我没有找到任何帮助,我尝试了全新安装对象检测-API,但没有帮助。

在本 models/research/object_detection/g3doc/installation.md 教程之后,我遇到了同样的错误。

当尝试 运行:

python object_detection/builders/model_builder_test.py

我希望模型能够像教程中那样开始训练。 却得到了这个:

/home/gal/.virtualenvs/dl4cv/bin/python /home/gal/TensorFlow/models/research/object_detection/model_main.py --logtostderr --train_dir=training / --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config

WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
  * https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
  * https://github.com/tensorflow/addons
If you depend on functionality not listed there, please file an issue.

W0610 17:22:52.758328 140705573046080 model_lib.py:615] Forced number of epochs for all eval validations to be 1.
I0610 17:22:52.758429 140705573046080 config_util.py:484] Maybe overwriting train_steps: None
I0610 17:22:52.758468 140705573046080 config_util.py:484] Maybe overwriting sample_1_of_n_eval_examples: 1
I0610 17:22:52.758504 140705573046080 config_util.py:484] Maybe overwriting use_bfloat16: False
I0610 17:22:52.758536 140705573046080 config_util.py:484] Maybe overwriting eval_num_epochs: 1
I0610 17:22:52.758566 140705573046080 config_util.py:484] Maybe overwriting load_pretrained: True
I0610 17:22:52.758595 140705573046080 config_util.py:494] Ignoring config override key: load_pretrained
W0610 17:22:52.758645 140705573046080 model_lib.py:631] Expected number of evaluation epochs is 1, but instead encountered `eval_on_train_input_config.num_epochs` = 0. Overwriting `num_epochs` to 1.
I0610 17:22:52.758683 140705573046080 model_lib.py:666] create_estimator_and_inputs: use_tpu False, export_to_tpu False
W0610 17:22:52.758958 140705573046080 estimator.py:1758] Using temporary folder as model directory: /tmp/tmp1lw9am0f
I0610 17:22:52.759102 140705573046080 estimator.py:202] Using config: {'_model_dir': '/tmp/tmp1lw9am0f', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
  rewrite_options {
    meta_optimizer_iterations: ONE
  }
}
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_service': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x7ff8060b5f28>, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}
W0610 17:22:52.759208 140705573046080 estimator.py:1931] Estimator's model_fn (<function create_model_fn.<locals>.model_fn at 0x7ff8060c6158>) includes params argument, but params are not passed to Estimator.
I0610 17:22:52.759698 140705573046080 estimator_training.py:186] Not using Distribute Coordinator.
I0610 17:22:52.759793 140705573046080 training.py:612] Running training and evaluation locally (non-distributed).
I0610 17:22:52.759924 140705573046080 training.py:700] Start train and evaluate loop. The evaluate will happen after every checkpoint. Checkpoint frequency is determined based on RunConfig arguments: save_checkpoints_steps None or save_checkpoints_secs 600.
W0610 17:22:52.763085 140705573046080 deprecation.py:323] From /home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow/python/training/training_util.py:238: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.
Instructions for updating:
Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.
Traceback (most recent call last):
  File "/home/gal/TensorFlow/models/research/object_detection/model_main.py", line 109, in <module>
    tf.app.run(main)
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/absl/app.py", line 300, in run
    _run_main(main, args)
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/absl/app.py", line 251, in _run_main
    sys.exit(main(argv))
  File "/home/gal/TensorFlow/models/research/object_detection/model_main.py", line 105, in main
    tf.estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/training.py", line 473, in train_and_evaluate
    return executor.run()
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/training.py", line 613, in run
    return self.run_local()
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/training.py", line 714, in run_local
    saving_listeners=saving_listeners)
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 359, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1132, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1159, in _train_model_default
    input_fn, ModeKeys.TRAIN))
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1000, in _get_features_and_labels_from_input_fn
    self._call_input_fn(input_fn, mode))
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/estimator.py", line 1087, in _call_input_fn
    return input_fn(**kwargs)
  File "/home/gal/TensorFlow/models/research/object_detection/inputs.py", line 446, in _train_input_fn
    params=params)
  File "/home/gal/TensorFlow/models/research/object_detection/inputs.py", line 512, in train_input
    model_config, is_training=True).preprocess
  File "/home/gal/TensorFlow/models/research/object_detection/builders/model_builder.py", line 135, in build
    add_summaries)
  File "/home/gal/TensorFlow/models/research/object_detection/builders/model_builder.py", line 597, in _build_faster_rcnn_model
    **common_kwargs)
  File "/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow/python/module/module.py", line 109, in __call__
    "super(%s, self).__init__()" % cls.__name__)
ValueError: Constructing a tf.Module without calling the super constructor is not supported. Add the following as the first line in your __init__ method:

super(FasterRCNNMetaArch, self).__init__()

经过深入挖掘,我发现:

/TensorFlow/models/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py

第 466 行:

    super(FasterRCNNMetaArch, self).__init__(num_classes=num_classes)

所以也许不是这样。 在第 97 行('finally')中找到一些断点后:

/home/gal/.virtualenvs/dl4cv/lib/python3.6/site-packages/tensorflow/python/module/module.py

有这一部分,引发 ValueError:

    finally:
      # The base Module constructor enters the modules name scope before
      # returning such that other functionality in the ctor happens within the
      # modules name scope.
      scope = getattr(module, "_ctor_name_scope", None)
      exc_info = sys.exc_info()
      if scope is None:
        if exc_info[0] is None:
          raise ValueError(
              "Constructing a tf.Module without calling the super constructor "
              "is not supported. Add the following as the first line in your "
              "__init__ method:\n\n"
              "super(%s, self).__init__()" % cls.__name__)
      else:
        scope.__exit__(*exc_info)
        del module._ctor_name_scope

当我运行时,scopeNoneexc_info[0]也是None。

有什么想法吗? 谢谢

好的,

在咨询了一些朋友后,这似乎是一个 python3/python2 错误,而不是 tensor-flow_object_detection_API 错误....

我已经切换到 python 2.7 并且一切正常:)

所以我不知道问题出在哪里,但我已经解决了。

希望对大家有所帮助。 加油,加尔