TensorFlow对象检测API评估训练效果

时间:2019-10-08 17:46:28

标签: python tensorflow machine-learning object-detection object-detection-api

我一直在自己的数据集上使用Tensorflow Object Detection API。在训练时,我想知道NN在训练集中学习得如何。因此,我想对训练和评估集进行评估,并在训练期间分别获得准确性(mAP)。

我的配置文件:

model {
  faster_rcnn {
    num_classes: 50
    image_resizer {
      fixed_shape_resizer {
        height: 960
        width: 960
      }
    }
    number_of_stages: 3
    feature_extractor {
      type: 'faster_rcnn_resnet101'
      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.00999999977648
        }
      }
    }
    first_stage_nms_score_threshold: 0.0
    first_stage_nms_iou_threshold: 0.699999988079
    first_stage_max_proposals: 100
    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
            }
          }
        }
        conv_hyperparams {
          op: CONV
          regularizer {
            l2_regularizer {
              weight: 0.0
            }
          }
          initializer {
            truncated_normal_initializer {
              stddev: 0.00999999977648
            }
          }
        }
        predict_instance_masks: true
        mask_height: 33
        mask_width: 33
        mask_prediction_conv_depth: 0
        mask_prediction_num_conv_layers: 4
      }
    }
    second_stage_post_processing {
      batch_non_max_suppression {
        score_threshold: 0.300000011921
        iou_threshold: 0.600000023842
        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
    second_stage_mask_prediction_loss_weight: 4.0
  }
}
train_config: {
  batch_size: 1
  optimizer {
    momentum_optimizer: {
      learning_rate: {
        manual_step_learning_rate {
          initial_learning_rate: 0.003
          schedule {
            step: 3000
            learning_rate: 0.00075
          }
          schedule {
            step: 6000
            learning_rate: 0.000300000014249
          }
          schedule {
            step: 15000
            learning_rate: 0.000075
          }
          schedule {
            step: 18000
            learning_rate: 0.0000314249
          }
          schedule {
            step: 900000
            learning_rate: 2.99999992421e-05
          }
          schedule {
            step: 1200000
            learning_rate: 3.00000010611e-06
          }
        }
      }
      momentum_optimizer_value: 0.899999976158
    }
    use_moving_average: false
  }
  gradient_clipping_by_norm: 10.0
  fine_tune_checkpoint: "./mask_rcnn_resnet101_atrous_coco/model.ckpt"
  from_detection_checkpoint: true
  num_steps: 200000
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
}
train_input_reader: {
  label_map_path: "./map901_label_map.pbtxt"
  load_instance_masks: true
  mask_type: PNG_MASKS
  tf_record_input_reader {
    input_path: ["./my_coco_train.record-?????-of-00005"]
  }
}
eval_config: {
  num_examples: 8000
  max_evals: 100
  num_visualizations: 25
}
eval_input_reader: {
  label_map_path: "./map901_label_map.pbtxt"
  shuffle: false
  load_instance_masks: true
  mask_type: PNG_MASKS
  num_readers: 1
  tf_record_input_reader {
    input_path: ["./my_coco_val.record-?????-of-00001"]
  }
}

我使用这些参数运行了脚本

python model_main.py --alsologtostderr \
  --pipeline_config_path=${PIPELINE_CONFIG_PATH} \
  --model_dir=${TRAIN_DIR} \
  --num_train_steps=24000 \
  --sample_1_of_n_eval_on_train_examples=25 \
  --num_eval_steps=100 \
  --sample_1_of_n_eval_examples=1 

我认为这将对Eval示例进行评估。为了评估培训数据(以检查从培训中捕获了多少个功能),我添加了 --eval_training_data=True到参数。

我无法随时添加“ eval_training_data”。我需要参加2次不同的培训课程。

有趣的是,我添加了“ eval_training_data”参数,

 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.165
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.281
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.167
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.051
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.109
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.202
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.164
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.202
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.202
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.057
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.141
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.236

没有“ eval_training_data”,我得到了

 Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.168
 Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets=100 ] = 0.283
 Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets=100 ] = 0.173
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.049
 Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.108
 Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.208
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=  1 ] = 0.170
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets= 10 ] = 0.208
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets=100 ] = 0.208
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.056
 Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.139
 Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.248

我很困惑。我的问题是:

  1. “ eval_training_data”是否不强制对象检测API对训练集进行评估?
  2. 为什么在我的情况下两个分数几乎相同,但在某些情况下,评估分数更好?
  3. 需要添加哪些参数,以便在培训期间分别评估培训和评估集并打印出来?

1 个答案:

答案 0 :(得分:2)

通过快速查看回购协议,我发现:

  1. eval_training_data仅对训练集进行评估,并从评估过程中排除评估集。因此,它仅在您的训练集中运行。

  2. 得分相同并不是一件坏事。这实际上很好,表明您的模型没有过拟合,如果训练数据得分的评估明显高于评估数据的评估得分,情况就是如此。在某些情况下,评估的分数较高是因为它必须是一个较小的数据集,因此,即使有好或坏的预测情况,分数也可能相差很大。 此外,该模型正在学习功能并将其与类相关联,而不是学习示例,因此不要指望它在训练集上表现出色,因为它已经看到了所有功能。 您的模型在验证集上执行的越好,意味着它推广的越好。

  3. 如果将eval_training_data = True设置为true,则实际上已经在单独评估训练集,并且如果将其设置为false(默认情况下),它将仅评估评估集。 我不确定他们是否添加了用于共同评估的功能,但是您可以通过对model_main.py进行很小的更改来实现。只需添加即可。它不是干净和优化的,但我想您已经明白了这一点,可以对此进行相应的修改。

flags.DEFINE_boolean('eval_training_data_and_eval_data', False,
                     'This will evaluate botht the training data and evaluation data sequentially')

  if FLAGS.checkpoint_dir:
    if FLAGS.eval_training_data_and_eval_data:

      name = 'training_data'
      input_fn = eval_on_train_input_fn
      if FLAGS.run_once:
        estimator.evaluate(input_fn,
                           steps=None,
                           checkpoint_path=tf.train.latest_checkpoint(
                               FLAGS.checkpoint_dir))
      else:
        model_lib.continuous_eval(estimator, FLAGS.checkpoint_dir, input_fn,
                                  train_steps, name)

      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=None,
                           checkpoint_path=tf.train.latest_checkpoint(
                               FLAGS.checkpoint_dir))
      else:
        model_lib.continuous_eval(estimator, FLAGS.checkpoint_dir, input_fn,
                                  train_steps, name)

  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])

此外,还要确保为数据集提供正确且不同的路径。请注意,如果我们基于验证分数优化超参数,则验证分数会产生偏差,并且不再是一般化的好估计。为了获得适当的泛化估计,我们必须在另一个测试集上计算分数。