在 tensorflow 对象检测中导出推理图时出错 api

Error while exporting inference graph in tensorlfow object detection api

嗨,我正在做 tensorflow object detection api。我已经按照 repo 中的所有主要说明进行操作,它一直运行良好,直到 now.But 突然出现一些奇怪的错误。我之前使用 fast rcnn,现在切换到 ssd mobile v2 coco

使用命令生成推理图时

python export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_v2_pets.config --trained_checkpoint_prefix training/model.ckpt-10250 --output_directory inference_graph

我收到以下错误:

Traceback (most recent call last): File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1356, in _do_call return fn(*args) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1341, in _run_fn options, feed_dict, fetch_list, target_list, run_metadata) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1429, in _call_tf_sessionrun run_metadata) tensorflow.python.framework.errors_impl.NotFoundError: Key Conv/biases not found in checkpoint [[{{node save/RestoreV2}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1286, in restore {self.saver_def.filename_tensor_name: save_path}) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 950, in run run_metadata_ptr) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1173, in _run feed_dict_tensor, options, run_metadata) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1350, in _do_run run_metadata) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1370, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.NotFoundError: Key Conv/biases not found in checkpoint [[node save/RestoreV2 (defined at /home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py:331) ]]

Original stack trace for 'save/RestoreV2': File "export_inference_graph.py", line 162, in tf.app.run() File "/home/user/anaconda3/envs/my_env/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/user/anaconda3/envs/my_env/lib/python3.6/site-packages/absl/app.py", line 299, in run _run_main(main, args) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/absl/app.py", line 250, in _run_main sys.exit(main(argv)) File "export_inference_graph.py", line 158, in main write_inference_graph=FLAGS.write_inference_graph) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py", line 497, in export_inference_graph write_inference_graph=write_inference_graph) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py", line 426, in _export_inference_graph trained_checkpoint_prefix=checkpoint_to_use) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py", line 331, in write_graph_and_checkpoint tf.import_graph_def(inference_graph_def, name='') File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func return func(*args, **kwargs) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 443, in import_graph_def _ProcessNewOps(graph) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 236, in _ProcessNewOps for new_op in graph._add_new_tf_operations(compute_devices=False): # pylint: disable=protected-access File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3751, in _add_new_tf_operations for c_op in c_api_util.new_tf_operations(self) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3751, in for c_op in c_api_util.new_tf_operations(self) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3641, in _create_op_from_tf_operation ret = Operation(c_op, self) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2005, in init self._traceback = tf_stack.extract_stack()

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1296, in restore names_to_keys = object_graph_key_mapping(save_path) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1614, in object_graph_key_mapping object_graph_string = reader.get_tensor(trackable.OBJECT_GRAPH_PROTO_KEY) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 678, in get_tensor return CheckpointReader_GetTensor(self, compat.as_bytes(tensor_str)) 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 "export_inference_graph.py", line 162, in tf.app.run() File "/home/user/anaconda3/envs/my_env/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/user/anaconda3/envs/my_env/lib/python3.6/site-packages/absl/app.py", line 299, in run _run_main(main, args) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/absl/app.py", line 250, in _run_main sys.exit(main(argv)) File "export_inference_graph.py", line 158, in main write_inference_graph=FLAGS.write_inference_graph) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py", line 497, in export_inference_graph write_inference_graph=write_inference_graph) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py", line 426, in _export_inference_graph trained_checkpoint_prefix=checkpoint_to_use) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py", line 335, in write_graph_and_checkpoint saver.restore(sess, trained_checkpoint_prefix) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1302, in restore err, "a Variable name or other graph key that is missing") 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 Conv/biases not found in checkpoint [[node save/RestoreV2 (defined at /home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py:331) ]]

Original stack trace for 'save/RestoreV2': File "export_inference_graph.py", line 162, in tf.app.run() File "/home/user/anaconda3/envs/my_env/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/user/anaconda3/envs/my_env/lib/python3.6/site-packages/absl/app.py", line 299, in run _run_main(main, args) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/absl/app.py", line 250, in _run_main sys.exit(main(argv)) File "export_inference_graph.py", line 158, in main write_inference_graph=FLAGS.write_inference_graph) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py", line 497, in export_inference_graph write_inference_graph=write_inference_graph) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py", line 426, in _export_inference_graph trained_checkpoint_prefix=checkpoint_to_use) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/object_detection/exporter.py", line 331, in write_graph_and_checkpoint tf.import_graph_def(inference_graph_def, name='') File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func return func(*args, **kwargs) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 443, in import_graph_def _ProcessNewOps(graph) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 236, in _ProcessNewOps for new_op in graph._add_new_tf_operations(compute_devices=False): # pylint: disable=protected-access File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3751, in _add_new_tf_operations for c_op in c_api_util.new_tf_operations(self) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3751, in for c_op in c_api_util.new_tf_operations(self) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3641, in _create_op_from_tf_operation ret = Operation(c_op, self) File "/home/user/anaconda3/envs/my_env/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2005, in init self._traceback = tf_stack.extract_stack()

实际上它一直在正常工作,不知道现在发生了什么。我也尝试使用 fast rcnn(它之前工作过)但它也开始失败

这是配置 file.I 目前正在为 2 类 做

# Faster R-CNN with Inception v2, configured for Oxford-IIIT Pets Dataset.
# Users should configure the fine_tune_checkpoint field in the train config as
# well as the label_map_path and input_path fields in the train_input_reader and
# eval_input_reader. Search for "PATH_TO_BE_CONFIGURED" to find the fields that
# should be configured.

model {
  faster_rcnn {
    num_classes: 2
    image_resizer {
      keep_aspect_ratio_resizer {
        min_dimension: 600
        max_dimension: 1024
      }
    }
    feature_extractor {
      type: 'faster_rcnn_inception_v2'
      first_stage_features_stride: 16
    }
    first_stage_anchor_generator {
      grid_anchor_generator {
        scales: [0.25, 0.5, 1.0, 2.0]
        aspect_ratios: [0.5, 1.0, 2.0]
        height_stride: 16
        width_stride: 16
      }
    }
    first_stage_box_predictor_conv_hyperparams {
      op: CONV
      regularizer {
        l2_regularizer {
          weight: 0.0
        }
      }
      initializer {
        truncated_normal_initializer {
          stddev: 0.01
        }
      }
    }
    first_stage_nms_score_threshold: 0.0
    first_stage_nms_iou_threshold: 0.7
    first_stage_max_proposals: 300
    first_stage_localization_loss_weight: 2.0
    first_stage_objectness_loss_weight: 1.0
    initial_crop_size: 14
    maxpool_kernel_size: 2
    maxpool_stride: 2
    second_stage_box_predictor {
      mask_rcnn_box_predictor {
        use_dropout: false
        dropout_keep_probability: 1.0
        fc_hyperparams {
          op: FC
          regularizer {
            l2_regularizer {
              weight: 0.0
            }
          }
          initializer {
            variance_scaling_initializer {
              factor: 1.0
              uniform: true
              mode: FAN_AVG
            }
          }
        }
      }
    }
    second_stage_post_processing {
      batch_non_max_suppression {
        score_threshold: 0.0
        iou_threshold: 0.6
        max_detections_per_class: 100
        max_total_detections: 300
      }
      score_converter: SOFTMAX
    }
    second_stage_localization_loss_weight: 2.0
    second_stage_classification_loss_weight: 1.0
  }
}

train_config: {
  batch_size: 1
  optimizer {
    momentum_optimizer: {
      learning_rate: {
        manual_step_learning_rate {
          initial_learning_rate: 0.0002
          schedule {
            step: 1
            learning_rate: .0002
          }
          schedule {
            step: 900000
            learning_rate: .00002
          }
          schedule {
            step: 1200000
            learning_rate: .000002
          }
        }
      }
      momentum_optimizer_value: 0.9
    }
    use_moving_average: false
  }
  gradient_clipping_by_norm: 10.0
  fine_tune_checkpoint: "/home/user/Downloads/Data_Science/Git/models/research/object_detection/faster_rcnn_inception_v2_coco_2018_01_28/model.ckpt"
  from_detection_checkpoint: true
  load_all_detection_checkpoint_vars: false
  # Note: The below line limits the training process to 200K steps, which we
  # empirically found to be sufficient enough to train the pets dataset. This
  # effectively bypasses the learning rate schedule (the learning rate will
  # never decay). Remove the below line to train indefinitely.
  num_steps: 200000
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
}


train_input_reader: {
  tf_record_input_reader {
    input_path: "/home/user/Downloads/Data_Science/Git/models/research/object_detection/train.record"
  }
  label_map_path: "/home/user/Downloads/Data_Science/Git/models/research/object_detection/training/labelmap.pbtxt"
}

eval_config: {
  num_examples: 67
  # Note: The below line limits the evaluation process to 10 evaluations.
  # Remove the below line to evaluate indefinitely.
  max_evals: 10
}

eval_input_reader: {
  tf_record_input_reader {
    input_path: "C:/tensorflow1/models/research/object_detection/test.record"
  }
  label_map_path: "C:/tensorflow1/models/research/object_detection/training/labelmap.pbtxt"
  shuffle: false
  num_readers: 1
}

在 github.But 中发现了 one or two 个类似的错误,但没有用。任何帮助将 appreciated.If 您需要任何更多信息,请留下 comment.Thank 您!

您确定您的模型 training/model.ckpt-10250faster_rcnn_inception_v2_pets 模型吗?错误 NotFoundError: Key Conv/biases not found in checkpoint [[{{node save/RestoreV2}}]] 意味着它无法从检查点恢复 Conv/biases

或者确保您使用的是对象检测框架支持的 TF 版本。您可以找到所有版本 here.