TensorFlow目标检测API评估训练性能

4

我一直在使用TensorFlow目标检测API处理自己的数据集。在训练过程中,我想知道神经网络对训练集的学习效果如何。因此,我想要在训练和评估集上分别运行评估,并在训练会话期间获取准确性(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"。我需要运行两个不同的训练会话。
有趣的是,添加了"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个回答

5

我快速浏览了一下存储库,得出以下结论:

  1. eval_training_data 仅评估训练集,并排除评估集。因此,它只在您的训练集上运行。

  2. 评分相同并不是坏事。实际上很好,表明模型没有过拟合。如果评估训练数据的分数显着高于评估数据的分数,则会导致过拟合。评估结果有时更高是因为评估数据集比较小,因此即使有几个好或不好的预测情况,分数的比例也可以有所变化。 此外,该模型正在学习特征并将其与类关联起来,而不是学习示例,因此不要期望它在训练集上表现非常出色,因为它已经看过所有示例。 模型在验证集上表现越好,说明它的泛化能力越好。

  3. 如果将 eval_training_data 设置为 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])

此外,确保您为数据集提供正确且明确的路径。请注意,如果我们根据验证分数优化超参数,则验证分数会带有偏见,不再是对泛化的良好估计。为了得到正确的泛化估计,我们必须在另一个测试集上计算分数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接