我在训练 CNN 从我自己的数据集中检测对象时遇到了一个奇怪的问题。我正在使用迁移学习,并且在训练开始时,损失值正在下降(如预期)。但过了一段时间,它变得越来越高,我不知道为什么会发生这种情况。
与此同时,当我看着Images使用 Tensorboard 上的选项卡来检查 CNN 预测物体的效果,我可以看到它做得非常好,而且看起来并没有随着时间的推移而变得越来越糟。此外,精确率和召回率图表看起来不错,只有损失图表(尤其是分类损失)显示出随着时间的推移而增加的趋势。
以下是一些具体细节:
- 我有 10 种不同类别的徽标(例如 DHL、BMW、FedEx 等)
- 每班大约 600 张图像
- 我在 Ubuntu 18.04 上使用tensorflow-gpu
-
我尝试了多个预训练模型,最新的是fast_rcnn_resnet101_coco使用此配置管道:
model {
faster_rcnn {
num_classes: 10
image_resizer {
keep_aspect_ratio_resizer {
min_dimension: 600
max_dimension: 1024
}
}
feature_extractor {
type: 'faster_rcnn_resnet101'
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.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/franciszek/Pobrane/models-master/research/object_detection/logo_detection/models2/faster_rcnn_resnet101_coco/model.ckpt"
from_detection_checkpoint: true
data_augmentation_options {
random_horizontal_flip {
}
}
}
train_input_reader: {
tf_record_input_reader {
input_path: "/home/franciszek/Pobrane/models-master/research/object_detection/logo_detection/data2/train.record"
}
label_map_path: "/home/franciszek/Pobrane/models-master/research/object_detection/logo_detection/data2/label_map.pbtxt"
}
eval_config: {
num_examples: 8000
# 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/franciszek/Pobrane/models-master/research/object_detection/logo_detection/data2/test.record"
}
label_map_path: "/home/franciszek/Pobrane/models-master/research/object_detection/logo_detection/data2/label_map.pbtxt"
shuffle: false
num_readers: 1
}
在这里您可以看到我训练近 23 小时并达到超过 12 万步后获得的结果:
所以,我的问题是,为什么损失值会随着时间的推移而增加?它应该变得更小或者保持或多或少的恒定,但是您可以在上面的图表中清楚地看到增加的趋势。
我认为一切都配置正确,并且我的数据集相当不错(.tfrecord 文件也正确“构建”)。
为了检查是否是我的错,我尝试使用其他人的数据集和配置文件。所以我用了浣熊数据集作者的文件(他提供了所有必要的文件his repo)。我只是下载了它们并开始训练,没有进行任何修改,以检查我是否会得到与他相似的结果。
令人惊讶的是,在 82k 步之后,我得到的图表与链接文章中显示的图表(在 22k 步后捕获的)完全不同。在这里您可以看到我们结果的比较:
-
我的损失 vs 他的全盘损失
-
我的精确度 vs his mAP
显然,有些东西在我的电脑上的工作方式有所不同。我怀疑这可能与我自己的数据集损失增加的原因相同,这就是我提到它的原因。
The totalLoss
是其他四个损失的加权和。 (RPN cla 和 reg 损失、BoxCla cla 和 reg 损失)并且它们都是评价损失。在张量板上,您可以选中或取消选中以查看仅用于训练或仅用于评估的评估结果。 (比如下图有训练总结和评估总结)
![enter image description here](https://i.stack.imgur.com/9i0OX.png)
如果评估损失增加,这可能表明模型过度拟合,此外,精度指标略有下降。
为了尝试更好的微调结果,您可以尝试调整四个损失的权重,例如,您可以增加BoxClassifierLoss/classification_loss
让模型更好地关注这个指标。在您的配置文件中,损失权重为second_stage_classification_loss_weight
and first_stage_objectness_loss_weight
都是 1,而另外两个都是 2,因此该模型目前更多地关注另外两个。
关于为什么的额外问题loss_1
and loss_2
是相同的。这可以通过查看张量流图来解释。
![enter image description here](https://i.stack.imgur.com/Jk0lc.png)
Here loss_2
是总结total_loss
,(注意这个total_loss与totalLoss中的不一样),红圈节点是tf.identity节点。该节点将输出与输入相同的张量,因此loss_1
是相同的loss_2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)