导出推理图在 num_of_stages 时给出错误:tensorflow 对象检测中的 1(仅限 RPN)api
export inference graph gives error when num_of_stages: 1 (RPN only) in tensorflow object-detection api
我正在做 tensorflow (v1.14) 对象检测 api。我在配置中使用 faster_rcnn_inception_resnet_v2_atrous_coco
和 num_of_stages : 1
。
我尝试使用命令生成推理图:
python3 export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_resnet_v2_atrous_coco.config --trained_checkpoint_prefix training/model.ckpt-125846 --output_directory inference_graph/
它给我错误:
Traceback (most recent call last):
File "export_inference_graph.py", line 162, in <module>
tf.app.run()
File "/home/tfs-people-analytics/.local/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/tfs-people-analytics/.local/lib/python3.6/site-packages/absl/app.py", line 300, in run
_run_main(main, args)
File "/home/tfs-people-analytics/.local/lib/python3.6/site-packages/absl/app.py", line 251, 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/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/exporter.py", line 515, in export_inference_graph
write_inference_graph=write_inference_graph)
File "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/exporter.py", line 418, in _export_inference_graph
graph_hook_fn=graph_hook_fn)
File "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/exporter.py", line 385, in build_detection_graph
output_collection_name=output_collection_name)
File "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/exporter.py", line 363, in _get_outputs_from_inputs
output_collection_name)
File "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/exporter.py", line 246, in add_output_tensor_nodes
detection_fields.detection_classes) + label_id_offset
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
问题似乎在 exporter.py
、line 246
classes = postprocessed_tensors.get(detection_fields.detection_classes) + label_id_offset
其中 postprocessed_tensors.get(detection_fields.detection_classes)
returns Nonetype
当我使用 num_of_stages: 1
仅训练 RPN 时。有什么办法可以为我的模型导出推理图吗?
这是我的配置文件:
# Faster R-CNN with Inception Resnet v2, Atrous version;
# Configured for MSCOCO Dataset.
model {
faster_rcnn {
number_of_stages: 1
num_classes: 1
image_resizer {
fixed_shape_resizer {
height: 100
width: 100
}
}
feature_extractor {
type: 'faster_rcnn_inception_resnet_v2'
first_stage_features_stride: 8
}
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: 8
width_stride: 8
}
}
first_stage_atrous_rate: 2
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: 70
first_stage_localization_loss_weight: 2.0
first_stage_objectness_loss_weight: 1.0
initial_crop_size: 17
maxpool_kernel_size: 1
maxpool_stride: 1
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: 100
}
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.0003
schedule {
step: 900000
learning_rate: .00003
}
schedule {
step: 1200000
learning_rate: .000003
}
}
}
momentum_optimizer_value: 0.9
}
use_moving_average: false
}
gradient_clipping_by_norm: 10.0
fine_tune_checkpoint: "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/faster_rcnn_inception_resnet_v2_atrous_coco_2018_01_28/model.ckpt"
from_detection_checkpoint: true
# 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: 350000
data_augmentation_options {
random_horizontal_flip {
}
}
}
train_input_reader: {
tf_record_input_reader {
input_path: "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/train.record"
}
label_map_path: "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/training/labelmap.pbtxt"
}
eval_config: {
num_examples: 3000
# 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: "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/test.record"
}
label_map_path: "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/training/labelmap.pbtxt"
shuffle: false
num_readers: 1
}
在 github.But 中发现 similar error 没有用。任何帮助将 appreciated.If 您需要任何更多信息,请留下一个 comment.Thank 你!
好的,找到了解决方案,事实证明 github 解决方案确实有效,尤其是 this 一个。所以我刚刚在 exporter.py
:
上添加了这些行
classes = postprocessed_tensors.get(
detection_fields.detection_classes)
if classes is not None:
classes += label_id_offset
else:
one = tf.constant(1, dtype=tf.int32, shape=(1, 1), name='classes_dummy')
classes = tf.tile(one, tf.shape(scores))
同一个 github 线程中另一个 solutions 的问题是常量需要在创建时已知形状。
我正在做 tensorflow (v1.14) 对象检测 api。我在配置中使用 faster_rcnn_inception_resnet_v2_atrous_coco
和 num_of_stages : 1
。
我尝试使用命令生成推理图:
python3 export_inference_graph.py --input_type image_tensor --pipeline_config_path training/faster_rcnn_inception_resnet_v2_atrous_coco.config --trained_checkpoint_prefix training/model.ckpt-125846 --output_directory inference_graph/
它给我错误:
Traceback (most recent call last):
File "export_inference_graph.py", line 162, in <module>
tf.app.run()
File "/home/tfs-people-analytics/.local/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/tfs-people-analytics/.local/lib/python3.6/site-packages/absl/app.py", line 300, in run
_run_main(main, args)
File "/home/tfs-people-analytics/.local/lib/python3.6/site-packages/absl/app.py", line 251, 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/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/exporter.py", line 515, in export_inference_graph
write_inference_graph=write_inference_graph)
File "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/exporter.py", line 418, in _export_inference_graph
graph_hook_fn=graph_hook_fn)
File "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/exporter.py", line 385, in build_detection_graph
output_collection_name=output_collection_name)
File "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/exporter.py", line 363, in _get_outputs_from_inputs
output_collection_name)
File "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/exporter.py", line 246, in add_output_tensor_nodes
detection_fields.detection_classes) + label_id_offset
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
问题似乎在 exporter.py
、line 246
classes = postprocessed_tensors.get(detection_fields.detection_classes) + label_id_offset
其中 postprocessed_tensors.get(detection_fields.detection_classes)
returns Nonetype
当我使用 num_of_stages: 1
仅训练 RPN 时。有什么办法可以为我的模型导出推理图吗?
这是我的配置文件:
# Faster R-CNN with Inception Resnet v2, Atrous version;
# Configured for MSCOCO Dataset.
model {
faster_rcnn {
number_of_stages: 1
num_classes: 1
image_resizer {
fixed_shape_resizer {
height: 100
width: 100
}
}
feature_extractor {
type: 'faster_rcnn_inception_resnet_v2'
first_stage_features_stride: 8
}
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: 8
width_stride: 8
}
}
first_stage_atrous_rate: 2
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: 70
first_stage_localization_loss_weight: 2.0
first_stage_objectness_loss_weight: 1.0
initial_crop_size: 17
maxpool_kernel_size: 1
maxpool_stride: 1
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: 100
}
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.0003
schedule {
step: 900000
learning_rate: .00003
}
schedule {
step: 1200000
learning_rate: .000003
}
}
}
momentum_optimizer_value: 0.9
}
use_moving_average: false
}
gradient_clipping_by_norm: 10.0
fine_tune_checkpoint: "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/faster_rcnn_inception_resnet_v2_atrous_coco_2018_01_28/model.ckpt"
from_detection_checkpoint: true
# 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: 350000
data_augmentation_options {
random_horizontal_flip {
}
}
}
train_input_reader: {
tf_record_input_reader {
input_path: "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/train.record"
}
label_map_path: "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/training/labelmap.pbtxt"
}
eval_config: {
num_examples: 3000
# 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: "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/test.record"
}
label_map_path: "/home/tfs-people-analytics/Documents/tensorflow/models/research/object_detection/training/labelmap.pbtxt"
shuffle: false
num_readers: 1
}
在 github.But 中发现 similar error 没有用。任何帮助将 appreciated.If 您需要任何更多信息,请留下一个 comment.Thank 你!
好的,找到了解决方案,事实证明 github 解决方案确实有效,尤其是 this 一个。所以我刚刚在 exporter.py
:
classes = postprocessed_tensors.get(
detection_fields.detection_classes)
if classes is not None:
classes += label_id_offset
else:
one = tf.constant(1, dtype=tf.int32, shape=(1, 1), name='classes_dummy')
classes = tf.tile(one, tf.shape(scores))
同一个 github 线程中另一个 solutions 的问题是常量需要在创建时已知形状。