【问题标题】:Tensorflow Object Detection not working, mAP low how to increaseTensorflow Object Detection 不工作,mAP 低如何增加
【发布时间】:2020-02-04 13:37:17
【问题描述】:

试图让物体检测器工作以检测一些水果。 最初尝试使用ssd_mobilenet_v2_coco_2018_03_29。我跑了大约 50k 步,损失始终显示在 2 Total loss graph 左右 但是 1 类的 mAP 为 0.48,另一类为 0.16 mAP results 我总共有 1936 张图像用于训练,350 张图像用于测试,所以我不确定我哪里出错了,因为数据集并不小。 所有图片均来自images(https://github.com/openimages/dataset)

SSD 配置:

# SSD with Mobilenet v2 configuration for MSCOCO 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 {
  ssd {
    num_classes: 2
    box_coder {
      faster_rcnn_box_coder {
        y_scale: 10.0
        x_scale: 10.0
        height_scale: 5.0
        width_scale: 5.0
      }
    }
    matcher {
      argmax_matcher {
        matched_threshold: 0.5
        unmatched_threshold: 0.5
        ignore_thresholds: false
        negatives_lower_than_unmatched: true
        force_match_for_each_row: true
      }
    }
    similarity_calculator {
      iou_similarity {
      }
    }
    anchor_generator {
      ssd_anchor_generator {
        num_layers: 6
        min_scale: 0.2
        max_scale: 0.95
        aspect_ratios: 1.0
        aspect_ratios: 2.0
        aspect_ratios: 0.5
        aspect_ratios: 3.0
        aspect_ratios: 0.3333
      }
    }
    image_resizer {
      fixed_shape_resizer {
        height: 300
        width: 300
      }
    }
    box_predictor {
      convolutional_box_predictor {
        min_depth: 0
        max_depth: 0
        num_layers_before_predictor: 0
        use_dropout: false
        dropout_keep_probability: 0.8
        kernel_size: 1
        box_code_size: 4
        apply_sigmoid_to_scores: false
        conv_hyperparams {
          activation: RELU_6,
          regularizer {
            l2_regularizer {
              weight: 0.00004
            }
          }
          initializer {
            truncated_normal_initializer {
              stddev: 0.03
              mean: 0.0
            }
          }
          batch_norm {
            train: true,
            scale: true,
            center: true,
            decay: 0.9997,
            epsilon: 0.001,
          }
        }
      }
    }
    feature_extractor {
      type: 'ssd_mobilenet_v2'
      min_depth: 16
      depth_multiplier: 1.0
      conv_hyperparams {
        activation: RELU_6,
        regularizer {
          l2_regularizer {
            weight: 0.00004
          }
        }
        initializer {
          truncated_normal_initializer {
            stddev: 0.03
            mean: 0.0
          }
        }
        batch_norm {
          train: true,
          scale: true,
          center: true,
          decay: 0.9997,
          epsilon: 0.001,
        }
      }
    }
    loss {
      classification_loss {
        weighted_sigmoid {
        }
      }
      localization_loss {
        weighted_smooth_l1 {
        }
      }
      hard_example_miner {
        num_hard_examples: 3000
        iou_threshold: 0.99
        loss_type: CLASSIFICATION
        max_negatives_per_positive: 3
        min_negatives_per_image: 3
      }
      classification_weight: 1.0
      localization_weight: 1.0
    }
    normalize_loss_by_num_matches: true
    post_processing {
      batch_non_max_suppression {
        score_threshold: 1e-8
        iou_threshold: 0.6
        max_detections_per_class: 100
        max_total_detections: 100
      }
      score_converter: SIGMOID
    }
  }
}

train_config: {
  batch_size: 24
  optimizer {
    rms_prop_optimizer: {
      learning_rate: {
        exponential_decay_learning_rate {
          initial_learning_rate: 0.004
          decay_steps: 800720
          decay_factor: 0.95
        }
      }
      momentum_optimizer_value: 0.9
      decay: 0.9
      epsilon: 1.0
    }
  }
  fine_tune_checkpoint: "E:/objectdetect/model/research/object_detection/ssd_mobilenet_v2_coco_2018_03_29/model.ckpt"
  fine_tune_checkpoint_type:  "detection"
  # 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 {
    }
  }
  data_augmentation_options {
    ssd_random_crop {
    }
  }
}

train_input_reader: {
  tf_record_input_reader {
    input_path: "E:/objectdetect/model/research/object_detection/train.record"
  }
  label_map_path: "E:/objectdetect/model/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: "E:/objectdetect/model/research/object_detection/test.record"
  }
  label_map_path: "E:/objectdetect/model/research/object_detection/training/labelmap.pbtxt"
  shuffle: false
  num_readers: 1
}

然后我尝试使用 Faster R-CNN 进行类似的测试,因为我认为使用 faster_rcnn_inception_v2_coco_2018_01_28 时精度更高会更好

这次我有 3 节课。包含 2731 张用于训练的图像和 446 张用于测试的图像。

更快的 R-CNN 配置:

# 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: 3
    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: 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: "/homes/nj303/objectdetect/model/research/object_detection/faster_rcnn_inception_v2_coco_2018_01_28/model.ckpt"
  from_detection_checkpoint: true
  load_all_detection_checkpoint_vars: 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: 200000
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
}


train_input_reader: {
  tf_record_input_reader {
    input_path: "/homes/nj303/objectdetect/model/research/object_detection/train.record"
  }
  label_map_path: "/homes/nj303/objectdetect/model/research/object_detection/training/labelmap.pbtxt"
}

eval_config: {
  metrics_set: "coco_detection_metrics"
  num_examples: 67
}

eval_input_reader: {
  tf_record_input_reader {
    input_path: "/homes/nj303/objectdetect/model/research/object_detection/test.record"
  }
  label_map_path: "/homes/nj303/objectdetect/model/research/object_detection/training/labelmap.pbtxt"
  shuffle: false
  num_readers: 1
}

但是,在 mAP 方面,我仍然收到很差的结果。 我跑了 100k 步,损失范围低于 0.1,但 mAP 为 0.38!我不确定如何改善这一点? mAP results for Faster R-CNN

我是新来的,所以任何帮助都将不胜感激!我不确定我做错了什么!

【问题讨论】:

  • 您在创建 tf 记录时是否尝试过洗牌。可能会尝试训练更多像 200K 步的模型,请检查数据是否过拟合。

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


【解决方案1】:

首先,数据集大小取决于视角,因为您的目标可能足够好,但对于其他目标可能非常小。

其次,正如您在 ssd 的管道配置中看到的那样,有一条注释:

注意:以下行将训练过程限制为 200K 步,我们凭经验发现这足以训练宠物数据集。这有效地绕过了学习率计划(学习率永远不会衰减)。删除以下行以无限期训练。

Pets 数据集有 37 个类别,每个类别有 200 张图像(7400 张图像),不是一个很大的数字,但它们是如何取得好的结果的呢?答案就在步骤上,他们使用了 200k 步,这让网络有足够的时间稳定在 mAP 上并从训练数据集中获取更多特征。

所以我建议你试试这个:

  • 增加您的步数,您可以从 100K 开始测试,看看它改进了多少。
  • 扩充您的数据集以从您的数据集中生成更多图像,包括更亮、更暗、失焦、不同背景等。我建议您使用此库:https://github.com/aleju/imgaug
  • 此外,mAP 是了解网络性能的好方法,但我建议您对某些对象进行实际测试,您会看到在哪种情况下不起作用,例如我的对象在白色背景上被正确检测到,但在背景为绿色或红色时失败。这意味着您需要为对象提供更多种类的背景。另一个例子,当我离它近 30 厘米时检测到我的对象,但当我离它 50 厘米时没有检测到。这意味着您的数据集需要更多来自远处的图像。

【讨论】:

  • 就我而言,增加 num_steps 参数越多,训练中的步骤就越少。我想知道我错过了什么?令人困惑
猜你喜欢
  • 2018-01-31
  • 2019-09-24
  • 2019-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 2018-01-28
相关资源
最近更新 更多