automl icon indicating copy to clipboard operation
automl copied to clipboard

README.md: Unable to use model trained on Pascal VOC for inference

Open felgru opened this issue 6 years ago • 7 comments

I'm trying to create a model trained on Pascal VOC like described in the README.md. The training and finetuning steps described in sections 6 and 7 worked flawlessly, but I'm unable to use the generated model for inferrence. I've tried to adapt the COCO inference example from section 3, to use the checkpoint in /tmp/efficientdet-d0-scratch that I've created using the Pascal VOC training data. The command that I've run was thus

python model_inspect.py --runmode=infer --model_name=efficientdet-d0   \
   --input_image_size=1920x1280 --max_boxes_to_draw=100  --min_score_thresh=0.2 \
   --ckpt_path=/tmp/efficientdet-d0-scratch/ --input_image=img.png \
   --output_image_dir=/tmp

While the unmodified example, using the pretrained COCO model, worked, I get the following error with my Pascal VOC checkpoint:

2020-04-16 15:59:36.532778: W tensorflow/core/framework/op_kernel.cc:1753] OP_REQUIRES failed at save_restore_v2_ops.cc:184 : Not found: Key box_net/box-0-bn-3/beta/ExponentialMovingAverage not found in checkpoint
Traceback (most recent call last):
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1365, in _do_call
    return fn(*args)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1349, in _run_fn
    return self._call_tf_sessionrun(options, feed_dict, fetch_list,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1441, in _call_tf_sessionrun
    return tf_session.TF_SessionRun_wrapper(self._session, options, feed_dict,
tensorflow.python.framework.errors_impl.NotFoundError: Key box_net/box-0-bn-3/beta/ExponentialMovingAverage not found in checkpoint
	 [[{{node save/RestoreV2}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 1298, in restore
    sess.run(self.saver_def.restore_op_name,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 957, in run
    result = self._run(None, fetches, feed_dict, options_ptr,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1180, in _run
    results = self._do_run(handle, final_targets, final_fetches,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1358, in _do_run
    return self._do_call(_run_fn, feeds, fetches, targets, options,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1384, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Key box_net/box-0-bn-3/beta/ExponentialMovingAverage not found in checkpoint
	 [[node save/RestoreV2 (defined at /home/lumi/src/Keras-poteaux/automl/efficientdet/inference.py:144) ]]

Original stack trace for 'save/RestoreV2':
  File "model_inspect.py", line 405, in <module>
    tf.app.run(main)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/platform/app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/absl/app.py", line 299, in run
    _run_main(main, args)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/absl/app.py", line 250, in _run_main
    sys.exit(main(argv))
  File "model_inspect.py", line 399, in main
    inspector.run_model(FLAGS.runmode, FLAGS.threads)
  File "model_inspect.py", line 370, in run_model
    self.inference_single_image(
  File "model_inspect.py", line 245, in inference_single_image
    driver.inference(image_image_path, output_dir, **kwargs)
  File "/home/lumi/src/Keras-poteaux/automl/efficientdet/inference.py", line 580, in inference
    restore_ckpt(
  File "/home/lumi/src/Keras-poteaux/automl/efficientdet/inference.py", line 144, in restore_ckpt
    saver = tf.train.Saver(var_dict, max_to_keep=1)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 836, in __init__
    self.build()
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 848, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 876, in _build
    self.saver_def = self._builder._build_internal(  # pylint: disable=protected-access
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 515, in _build_internal
    restore_op = self._AddRestoreOps(filename_tensor, saveables,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 335, in _AddRestoreOps
    all_tensors = self.bulk_restore(filename_tensor, saveables, preferred_shard,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 583, in bulk_restore
    return io_ops.restore_v2(filename_tensor, names, slices, dtypes)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/ops/gen_io_ops.py", line 1503, in restore_v2
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py", line 742, in _apply_op_helper
    op = g._create_op_internal(op_type_name, inputs, dtypes=None,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3319, in _create_op_internal
    ret = Operation(
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1791, in __init__
    self._traceback = tf_stack.extract_stack()


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/py_checkpoint_reader.py", line 69, in get_tensor
    return CheckpointReader.CheckpointReader_GetTensor(
RuntimeError: Key _CHECKPOINTABLE_OBJECT_GRAPH not found in checkpoint

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 1309, in restore
    names_to_keys = object_graph_key_mapping(save_path)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 1627, in object_graph_key_mapping
    object_graph_string = reader.get_tensor(trackable.OBJECT_GRAPH_PROTO_KEY)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/py_checkpoint_reader.py", line 74, in get_tensor
    error_translator(e)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/py_checkpoint_reader.py", line 35, in error_translator
    raise errors_impl.NotFoundError(None, None, error_message)
tensorflow.python.framework.errors_impl.NotFoundError: Key _CHECKPOINTABLE_OBJECT_GRAPH not found in checkpoint

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "model_inspect.py", line 405, in <module>
    tf.app.run(main)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/platform/app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/absl/app.py", line 299, in run
    _run_main(main, args)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/absl/app.py", line 250, in _run_main
    sys.exit(main(argv))
  File "model_inspect.py", line 399, in main
    inspector.run_model(FLAGS.runmode, FLAGS.threads)
  File "model_inspect.py", line 370, in run_model
    self.inference_single_image(
  File "model_inspect.py", line 245, in inference_single_image
    driver.inference(image_image_path, output_dir, **kwargs)
  File "/home/lumi/src/Keras-poteaux/automl/efficientdet/inference.py", line 580, in inference
    restore_ckpt(
  File "/home/lumi/src/Keras-poteaux/automl/efficientdet/inference.py", line 145, in restore_ckpt
    saver.restore(sess, ckpt_path)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 1314, in restore
    raise _wrap_restore_error_with_msg(
tensorflow.python.framework.errors_impl.NotFoundError: Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Key box_net/box-0-bn-3/beta/ExponentialMovingAverage not found in checkpoint
	 [[node save/RestoreV2 (defined at /home/lumi/src/Keras-poteaux/automl/efficientdet/inference.py:144) ]]

Original stack trace for 'save/RestoreV2':
  File "model_inspect.py", line 405, in <module>
    tf.app.run(main)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/platform/app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/absl/app.py", line 299, in run
    _run_main(main, args)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/absl/app.py", line 250, in _run_main
    sys.exit(main(argv))
  File "model_inspect.py", line 399, in main
    inspector.run_model(FLAGS.runmode, FLAGS.threads)
  File "model_inspect.py", line 370, in run_model
    self.inference_single_image(
  File "model_inspect.py", line 245, in inference_single_image
    driver.inference(image_image_path, output_dir, **kwargs)
  File "/home/lumi/src/Keras-poteaux/automl/efficientdet/inference.py", line 580, in inference
    restore_ckpt(
  File "/home/lumi/src/Keras-poteaux/automl/efficientdet/inference.py", line 144, in restore_ckpt
    saver = tf.train.Saver(var_dict, max_to_keep=1)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 836, in __init__
    self.build()
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 848, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 876, in _build
    self.saver_def = self._builder._build_internal(  # pylint: disable=protected-access
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 515, in _build_internal
    restore_op = self._AddRestoreOps(filename_tensor, saveables,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 335, in _AddRestoreOps
    all_tensors = self.bulk_restore(filename_tensor, saveables, preferred_shard,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/training/saver.py", line 583, in bulk_restore
    return io_ops.restore_v2(filename_tensor, names, slices, dtypes)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/ops/gen_io_ops.py", line 1503, in restore_v2
    _, _, _op, _outputs = _op_def_library._apply_op_helper(
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py", line 742, in _apply_op_helper
    op = g._create_op_internal(op_type_name, inputs, dtypes=None,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3319, in _create_op_internal
    ret = Operation(
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1791, in __init__
    self._traceback = tf_stack.extract_stack()

For those tests I've used the latest version of TensorFlow, i.e. 2.2.0-rc3. Am I missing some step to use the model trained on Pascal VOC for inference?

felgru avatar Apr 16 '20 14:04 felgru

Hello, If you have trained with --hparams="moving_average_decay=0", maybe try the flag --enable_ema=False for inference.

DamGameHero avatar Apr 16 '20 15:04 DamGameHero

Thanks. The --enable_ema=False flag seems to resolve this error.

Unfortunately, now I get another error

Parsing Inputs...
Traceback (most recent call last):
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1365, in _do_call
    return fn(*args)
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1349, in _run_fn
    return self._call_tf_sessionrun(options, feed_dict, fetch_list,
  File "/home/lumi/src/Keras-poteaux/venv/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1441, in _call_tf_sessionrun
    return tf_session.TF_SessionRun_wrapper(self._session, options, feed_dict,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [810] rhs shape= [180]
	 [[{{node save/Assign_141}}]]

Maybe I'm still missing another flag?

felgru avatar Apr 16 '20 15:04 felgru

It is likely you forgot to set num_classes during eval.

mingxingtan avatar Apr 16 '20 17:04 mingxingtan

Thank you. Adding num_classes indeed fixed the remaining error.

In the end, I've used the following command to run the inference using the model trained on the Pascal VOC dataset:

python model_inspect.py --runmode=infer --model_name=efficientdet-d0 \
  --input_image_size=1920x1280 --max_boxes_to_draw=100 --min_score_thresh=0.2 \
  --ckpt_path=/tmp/efficientdet-d0-scratch/ --input_image=img.png \
  --output_image_dir=/tmp --enable_ema=False --num_classes=20

Maybe you could add this command under the Pascal VOC training sections in the README.md. That would be very helpful for using EfficientDet on datasets other than COCO.

felgru avatar Apr 17 '20 08:04 felgru

python model_inspect.py --runmode=infer model_name=efficientdet-d0 --ckpt_path=efficientdet-d0 input_image_size=512 --max_boxes_to_draw=100 --min_score_thresh=0.2 --line_thickness=4 --saved_model_dir=/tmp/saved_model --input_image=img.png --output_image_dir=/tmp/ --enable_ema=False

I also tried python model_inspect.py --runmode=infer model_name=efficientdet-d0 --ckpt_path=efficientdet-d0 input_image_size=512 --max_boxes_to_draw=100 --min_score_thresh=0.2 --line_thickness=4 --saved_model_dir=/tmp/saved_model --input_image=img.png --output_image_dir=/tmp/ --enable_ema=False --num_classes=90

Still gives me an error

Key efficientnet-b1/blocks_0/conv2d/kernel not found in checkpoint [[node save/RestoreV2 (defined at /mnt/efficientdet/inference.py:144) ]]

Original stack trace for 'save/RestoreV2': File "model_inspect.py", line 408, in tf.app.run(main) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/platform/app.py", line 40, in run _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 299, in run _run_main(main, args) File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 250, in _run_main sys.exit(main(argv)) File "model_inspect.py", line 402, in main inspector.run_model(FLAGS.runmode, FLAGS.threads) File "model_inspect.py", line 374, in run_model FLAGS.input_image, FLAGS.output_image_dir, **config_dict) File "model_inspect.py", line 248, in inference_single_image driver.inference(image_image_path, output_dir, **kwargs) File "/mnt/efficientdet/inference.py", line 607, in inference sess, self.ckpt_path, enable_ema=self.enable_ema, export_ckpt=None) File "/mnt/efficientdet/inference.py", line 144, in restore_ckpt saver = tf.train.Saver(var_dict, max_to_keep=1) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 828, in init self.build() File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 840, in build self._build(self._filename, build_save=True, build_restore=True) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 878, in _build build_restore=build_restore) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 508, in _build_internal restore_sequentially, reshape) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 328, in _AddRestoreOps restore_sequentially) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 575, in bulk_restore return io_ops.restore_v2(filename_tensor, names, slices, dtypes) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/gen_io_ops.py", line 1506, in restore_v2 name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/op_def_library.py", line 742, in _apply_op_helper attrs=attr_protos, op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 3322, in _create_op_internal op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 1756, in init self._traceback = tf_stack.extract_stack()

varghesealex90 avatar Apr 17 '20 10:04 varghesealex90

python model_inspect.py --runmode=infer model_name=efficientdet-d0 --ckpt_path=efficientdet-d0 input_image_size=512 --max_boxes_to_draw=100 --min_score_thresh=0.2 --line_thickness=4 --saved_model_dir=/tmp/saved_model --input_image=img.png --output_image_dir=/tmp/ --enable_ema=False

I also tried python model_inspect.py --runmode=infer model_name=efficientdet-d0 --ckpt_path=efficientdet-d0 input_image_size=512 --max_boxes_to_draw=100 --min_score_thresh=0.2 --line_thickness=4 --saved_model_dir=/tmp/saved_model --input_image=img.png --output_image_dir=/tmp/ --enable_ema=False --num_classes=90

Still gives me an error

Key efficientnet-b1/blocks_0/conv2d/kernel not found in checkpoint [[node save/RestoreV2 (defined at /mnt/efficientdet/inference.py:144) ]]

Original stack trace for 'save/RestoreV2': File "model_inspect.py", line 408, in tf.app.run(main) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/platform/app.py", line 40, in run _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef) File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 299, in run _run_main(main, args) File "/usr/local/lib/python3.6/dist-packages/absl/app.py", line 250, in _run_main sys.exit(main(argv)) File "model_inspect.py", line 402, in main inspector.run_model(FLAGS.runmode, FLAGS.threads) File "model_inspect.py", line 374, in run_model FLAGS.input_image, FLAGS.output_image_dir, **config_dict) File "model_inspect.py", line 248, in inference_single_image driver.inference(image_image_path, output_dir, **kwargs) File "/mnt/efficientdet/inference.py", line 607, in inference sess, self.ckpt_path, enable_ema=self.enable_ema, export_ckpt=None) File "/mnt/efficientdet/inference.py", line 144, in restore_ckpt saver = tf.train.Saver(var_dict, max_to_keep=1) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 828, in init self.build() File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 840, in build self._build(self._filename, build_save=True, build_restore=True) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 878, in _build build_restore=build_restore) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 508, in _build_internal restore_sequentially, reshape) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 328, in _AddRestoreOps restore_sequentially) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/saver.py", line 575, in bulk_restore return io_ops.restore_v2(filename_tensor, names, slices, dtypes) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/gen_io_ops.py", line 1506, in restore_v2 name=name) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/op_def_library.py", line 742, in _apply_op_helper attrs=attr_protos, op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 3322, in _create_op_internal op_def=op_def) File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py", line 1756, in init self._traceback = tf_stack.extract_stack()

我也遇到了相同的问题,请问您解决了吗

15241192958 avatar Apr 21 '20 07:04 15241192958

Be careful, using that flags. Using "enable_ema" seems like available with old source code model_inspect.py. Latest version of it needs "hparams" same as Finetune on PASCAL VOC 2012 with detector COCO ckpt.

DameNianch avatar May 02 '20 08:05 DameNianch