models icon indicating copy to clipboard operation
models copied to clipboard

TypeError: create_estimator_and_inputs() missing 1 required positional argument: 'hparams'

Open PiotrG1996 opened this issue 3 years ago • 2 comments

Prerequisites

Please answer the following question for yourself before submitting an issue.

  • [x] I checked to make sure that this issue has not been filed already.

1. URL to the documentation with the issue.

https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/tensorflow-1.14/training.html#training-the-model

2. Describe the issue

After I set up entire workspace for the training process of the MobilenetV1 Tensorflow Object Detection API 1.14, suggested solution didn't work...

Should I change something in model_hparams.py?

model_main.py
"""Binary to run train and evaluation on object detection model."""


"""Binary to run train and evaluation on object detection model."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import warnings
import os

# warnings.filterwarnings("ignore")

from absl import flags
from tensorflow import keras

import tensorflow.compat.v1 as tf
from tensorflow.compat.v1 import estimator as tf_estimator
from keras_applications.resnet import ResNet50


from object_detection import model_lib
from object_detection import model_hparams

flags.DEFINE_string(
    "model_dir",
    "D:\TensorFlow\training_demo\models\ssd_mobilenet_v1_coco\mobilenetv1.pb",
    "where event and checkpoint files will be written.",
)
flags.DEFINE_string("pipeline_config_path", "D:\TensorFlow\training_demo\annotations\pipeline.config", "D:\TensorFlow\training_demo\annotations\pipeline.config")
flags.DEFINE_integer("num_train_steps", 2000, "Number of train steps.")
flags.DEFINE_boolean(
    "eval_training_data",
    False,
    "If training data should be evaluated for this job. Note "
    "that one call only use this in eval-only mode, and "
    "`checkpoint_dir` must be supplied.",
)
flags.DEFINE_integer(
    "sample_1_of_n_eval_examples",
    1,
    "Will sample one of " "every n eval input examples, where n is provided.",
)
flags.DEFINE_integer(
    "sample_1_of_n_eval_on_train_examples",
    1,
    "Will sample "
    "one of every n train input examples for evaluation, "
    "where n is provided. This is only used if "
    "`eval_training_data` is True.",
)
flags.DEFINE_string(
    "checkpoint_dir",
    "D:\TensorFlow\training_demo\pre-trained-models\ssd_mobilenet_v1_coco",
    "Path to directory holding a checkpoint.  If "
    "`checkpoint_dir` is provided, this binary operates in eval-only mode, "
    "writing resulting metrics to `model_dir`.",
)
flags.DEFINE_boolean(
    "run_once",
    False,
    "If running in eval-only mode, whether to run just "
    "one round of eval vs running continuously (default).",
)
flags.DEFINE_integer(
    "max_eval_retries",
    0,
    "If running continuous eval, the maximum number of "
    "retries upon encountering tf.errors.InvalidArgumentError. If negative, "
    "will always retry the evaluation.",
)
FLAGS = flags.FLAGS


def main(unused_argv):
    flags.mark_flag_as_required("model_dir")
    flags.mark_flag_as_required("pipeline_config_path")
    config = tf_estimator.RunConfig(model_dir=FLAGS.model_dir)

    train_and_eval_dict = model_lib.create_estimator_and_inputs(
        run_config=config,
        pipeline_config_path=FLAGS.pipeline_config_path,
        train_steps=FLAGS.num_train_steps,
        sample_1_of_n_eval_examples=FLAGS.sample_1_of_n_eval_examples,
        sample_1_of_n_eval_on_train_examples=(
            FLAGS.sample_1_of_n_eval_on_train_examples
        ),
    )
    estimator = train_and_eval_dict["estimator"]
    train_input_fn = train_and_eval_dict["train_input_fn"]
    eval_input_fns = train_and_eval_dict["eval_input_fns"]
    eval_on_train_input_fn = train_and_eval_dict["eval_on_train_input_fn"]
    predict_input_fn = train_and_eval_dict["predict_input_fn"]
    train_steps = train_and_eval_dict["train_steps"]

    if FLAGS.checkpoint_dir:
        if FLAGS.eval_training_data:
            name = "training_data"
            input_fn = eval_on_train_input_fn
        else:
            name = "validation_data"
            # The first eval input will be evaluated.
            input_fn = eval_input_fns[0]
        if FLAGS.run_once:
            estimator.evaluate(
                input_fn,
                steps=2000,
                checkpoint_path=tf.train.latest_checkpoint(FLAGS.checkpoint_dir),
            )
        else:
            model_lib.continuous_eval(
                estimator,
                FLAGS.checkpoint_dir,
                input_fn,
                train_steps,
                name,
                FLAGS.max_eval_retries,
            )
    else:
        train_spec, eval_specs = model_lib.create_train_and_eval_specs(
            train_input_fn,
            eval_input_fns,
            eval_on_train_input_fn,
            predict_input_fn,
            train_steps,
            eval_on_train_data=False,
        )

        # Currently only a single Eval Spec is allowed.
        tf_estimator.train_and_evaluate(estimator, train_spec, eval_specs[0])


if __name__ == "__main__":
    tf.app.run()


WARNING:tensorflow:From C:\Users\Pioter\anaconda3\envs\tensorflow_gpu\lib\site-packages\slim-0.1-py3.7.egg\nets\inception_resnet_v2.py:373: The name tf.GraphKeys is deprecated. Please use tf.compat.v1.GraphKeys instead.

WARNING:tensorflow:From C:\Users\Pioter\anaconda3\envs\tensorflow_gpu\lib\site-packages\slim-0.1-py3.7.egg\nets\mobilenet\mobilenet.py:389: The name tf.nn.avg_pool is deprecated. Please use tf.nn.avg_pool2d instead.

model_main.py:75: UserWarning: Flag --model_dir has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!
  flags.mark_flag_as_required("model_dir")
model_main.py:76: UserWarning: Flag --pipeline_config_path has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!
  flags.mark_flag_as_required("pipeline_config_path")
Traceback (most recent call last):
  File "model_main.py", line 133, in <module>
    tf.app.run()
  File "C:\Users\Pioter\anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\platform\app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "C:\Users\Pioter\anaconda3\envs\tensorflow_gpu\lib\site-packages\absl\app.py", line 312, in run
    _run_main(main, args)
  File "C:\Users\Pioter\anaconda3\envs\tensorflow_gpu\lib\site-packages\absl\app.py", line 258, in _run_main
    sys.exit(main(argv))
  File "model_main.py", line 85, in main
    FLAGS.sample_1_of_n_eval_on_train_examples
TypeError: create_estimator_and_inputs() missing 1 required positional argument: 'hparams'

This was verified by me:

cd models/research
# Compile protos.
protoc object_detection/protos/*.proto --pytcd models/research
# Compile protos.
protoc object_detection/protos/*.proto --python_out=.
# Install TensorFlow Object Detection API.
cp object_detection/packages/tf1/setup.py .
python -m pip install --use-feature=2020-resolver .

# Test the installation
python object_detection/builders/model_builder_tf1_test.pyhon_out=.
# Install TensorFlow Object Detection API.
cp object_detection/packages/tf1/setup.py .
python -m pip install --use-feature=2020-resolver .

# Test the installation
python object_detection/builders/model_builder_tf1_test.py
Python packages

absl-py 1.1.0 argon2-cffi 21.3.0 argon2-cffi-bindings 21.2.0 astor 0.8.1 attrs 21.4.0 backcall 0.2.0 beautifulsoup4 4.11.1 bleach 5.0.1 certifi 2022.6.15 cffi 1.15.1 colorama 0.4.5 cycler 0.11.0 Cython 0.29.30 debugpy 1.6.2 decorator 5.1.1 defusedxml 0.7.1 entrypoints 0.4 fastjsonschema 2.15.3 gast 0.5.3 google-pasta 0.2.0 grpcio 1.47.0 h5py 3.7.0 importlib-metadata 4.12.0 importlib-resources 5.8.0 ipykernel 6.15.1 ipython 7.34.0 ipython-genutils 0.2.0 ipywidgets 7.7.1 jedi 0.18.1 Jinja2 3.1.2 jsonschema 4.7.2 jupyter 1.0.0 jupyter-client 7.3.4 jupyter-console 6.4.4 jupyter-core 4.11.1 jupyterlab-pygments 0.2.2 jupyterlab-widgets 1.1.1 Keras-Applications 1.0.8 Keras-Preprocessing 1.1.2 kiwisolver 1.4.3 lxml 4.4.1 Markdown 3.4 MarkupSafe 2.1.1 matplotlib 3.1.1 matplotlib-inline 0.1.3 mistune 0.8.4 nbclient 0.6.6 nbconvert 6.5.0 nbformat 5.4.0 nest-asyncio 1.5.5 notebook 6.4.12 numpy 1.16.4 object-detection 0.1 opencv-python 4.6.0.66 packaging 21.3 pandocfilters 1.5.0 parso 0.8.3 pathlib 1.0.1 pickleshare 0.7.5 Pillow 6.2.1 pip 22.1.2 prometheus-client 0.14.1 prompt-toolkit 3.0.30 protobuf 3.20.0 psutil 5.9.1 pycocotools 2.0.4 pycparser 2.21 Pygments 2.12.0 pyparsing 3.0.9 pyrsistent 0.18.1 python-dateutil 2.8.2 pywin32 304 pywinpty 2.0.6 pyzmq 23.2.0 qtconsole 5.3.1 QtPy 2.1.0 Send2Trash 1.8.0 setuptools 61.2.0 six 1.16.0 slim 0.1 soupsieve 2.3.2.post1 tensorboard 1.14.0 tensorflow-estimator 1.14.0 tensorflow-gpu 1.14.0 termcolor 1.1.0 terminado 0.15.0 tinycss2 1.1.1 tornado 6.2 traitlets 5.3.0 typing_extensions 4.3.0 wcwidth 0.2.5 webencodings 0.5.1 Werkzeug 2.1.2 wheel 0.37.1 widgetsnbextension 3.6.1 wincertstore 0.2 wrapt 1.14.1 zipp 3.8.1

PiotrG1996 avatar Jul 15 '22 15:07 PiotrG1996

@PiotrG1996,

In model_main.py, please try to add

from object_detection import model_hparams

Thank you.

chunduriv avatar Jul 18 '22 10:07 chunduriv

Hey @chunduriv

I run the following command with the same error, although model_hparams was added by me previously:

python model_main.py --alsologtostderr --model_dir=D:\TensorFlow\training_demo\models\ssd_mobilenet_v1_coco --pipeline_config_path=D:\TensorFlow\training_demo\training\pipeline.config

I would be glad for any further suggestions. Btw I'm running on Windows 10.

PiotrG1996 avatar Jul 18 '22 11:07 PiotrG1996