Tensorflow 对象检测:不使用自定义数据进行预测
Tensorflow Object Detection: Not Predicting using Custom Data
我正在使用此存储库的基本代码 (https://github.com/ndaidong/tf-object-detection),它使用 Tensorflow 对象检测来检测评论屏幕截图中的评论、日期、喜欢和评分。
我浏览了 100 张图片(只是想测试这是否可行),用 4 个标签(评论、日期、喜欢和评分)对图片进行注释,从 XML 转换为 CSV,然后生成TF记录。这是针对训练和评估数据完成的。 100 张图像用于训练,20 张图像用于评估。这是我注释的屏幕截图。
Click here for an example of the annotated image
为了训练,我使用了以下配置
model {
ssd {
num_classes: 4
image_resizer {
fixed_shape_resizer {
height: 500
width: 2000
}
}
feature_extractor {
type: "ssd_mobilenet_v2"
depth_multiplier: 1.0
min_depth: 16
conv_hyperparams {
regularizer {
l2_regularizer {
weight: 3.99999989895e-05
}
}
initializer {
truncated_normal_initializer {
mean: 0.0
stddev: 0.0299999993294
}
}
activation: RELU_6
batch_norm {
decay: 0.999700009823
center: true
scale: true
epsilon: 0.0010000000475
train: true
}
}
use_depthwise: true
}
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 {
}
}
box_predictor {
convolutional_box_predictor {
conv_hyperparams {
regularizer {
l2_regularizer {
weight: 3.99999989895e-05
}
}
initializer {
truncated_normal_initializer {
mean: 0.0
stddev: 0.0299999993294
}
}
activation: RELU_6
batch_norm {
decay: 0.999700009823
center: true
scale: true
epsilon: 0.0010000000475
train: true
}
}
min_depth: 0
max_depth: 0
num_layers_before_predictor: 0
use_dropout: false
dropout_keep_probability: 0.800000011921
kernel_size: 3
box_code_size: 4
apply_sigmoid_to_scores: false
}
}
anchor_generator {
ssd_anchor_generator {
num_layers: 6
min_scale: 0.20000000298
max_scale: 0.949999988079
aspect_ratios: 1.0
aspect_ratios: 2.0
aspect_ratios: 0.5
aspect_ratios: 3.0
aspect_ratios: 0.333299994469
}
}
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: SIGMOID
}
normalize_loss_by_num_matches: true
loss {
localization_loss {
weighted_smooth_l1 {
}
}
classification_loss {
weighted_sigmoid {
}
}
hard_example_miner {
num_hard_examples: 3000
iou_threshold: 0.990000009537
loss_type: CLASSIFICATION
max_negatives_per_positive: 3
min_negatives_per_image: 3
}
classification_weight: 1.0
localization_weight: 1.0
}
}
}
train_config {
batch_size: 35
data_augmentation_options {
random_horizontal_flip {
}
}
data_augmentation_options {
ssd_random_crop {
}
}
optimizer {
rms_prop_optimizer {
learning_rate {
exponential_decay_learning_rate {
initial_learning_rate: 0.00400000018999
decay_steps: 800720
decay_factor: 0.949999988079
}
}
momentum_optimizer_value: 0.899999976158
decay: 0.899999976158
epsilon: 1.0
}
}
num_steps: 200
}
train_input_reader: {
tf_record_input_reader {
input_path: "temp/data/train.record"
}
label_map_path: "configs/reviews/labels.pbtxt"
}
eval_config {
num_examples: 20
max_evals: 10
use_moving_averages: false
}
eval_input_reader: {
tf_record_input_reader {
input_path: "temp/data/test.record"
}
label_map_path: "configs/reviews/labels.pbtxt"
shuffle: false
num_readers: 1
}
如您所见,我正在尝试从头开始训练数据,而不是使用现有模型。这样做的原因是因为我不是在寻找已经训练过的一般对象。
我调整了 fixed_shape_resizer,因为评论的图像尺寸大约在宽 2000 和高 500 左右。
我只用了 200 步进行训练(我需要做更多吗?),正如我在 Tensorboard 中注意到的那样,在 100 步之后它开始学习,因为 'Loss' 结果开始下降下降了不少。 Click here to see Tensorboard results
然而,当我 export/freeze 图表然后尝试预测时。什么都预测不到。在我看来一切都很好。我做错了什么吗?
使用这个 API 涉及很多步骤,所以不确定您可能弄错了哪一个,但我的感觉是,如果您从头开始训练对象检测模型,那么您需要庞大的数据集。 100 张图像是非常小的数据集。您可以查看训练预训练对象检测模型(如 ResNet、ImageNet)的数据集的大小。那里的数据集非常庞大。因此,要获得结果,您需要使用预训练模型或更大的数据集。为了增加数据集,您可以对 API 之外的现有图像进行图像增强,并使用算法调整它们的注释。当我做类似的练习时,我使用了大约 10K 个带有预训练模型和图像增强的图像。
如果您使用 labelImg 进行图像注释,请确保您了解工具中的this问题
我正在使用此存储库的基本代码 (https://github.com/ndaidong/tf-object-detection),它使用 Tensorflow 对象检测来检测评论屏幕截图中的评论、日期、喜欢和评分。
我浏览了 100 张图片(只是想测试这是否可行),用 4 个标签(评论、日期、喜欢和评分)对图片进行注释,从 XML 转换为 CSV,然后生成TF记录。这是针对训练和评估数据完成的。 100 张图像用于训练,20 张图像用于评估。这是我注释的屏幕截图。 Click here for an example of the annotated image
为了训练,我使用了以下配置
model {
ssd {
num_classes: 4
image_resizer {
fixed_shape_resizer {
height: 500
width: 2000
}
}
feature_extractor {
type: "ssd_mobilenet_v2"
depth_multiplier: 1.0
min_depth: 16
conv_hyperparams {
regularizer {
l2_regularizer {
weight: 3.99999989895e-05
}
}
initializer {
truncated_normal_initializer {
mean: 0.0
stddev: 0.0299999993294
}
}
activation: RELU_6
batch_norm {
decay: 0.999700009823
center: true
scale: true
epsilon: 0.0010000000475
train: true
}
}
use_depthwise: true
}
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 {
}
}
box_predictor {
convolutional_box_predictor {
conv_hyperparams {
regularizer {
l2_regularizer {
weight: 3.99999989895e-05
}
}
initializer {
truncated_normal_initializer {
mean: 0.0
stddev: 0.0299999993294
}
}
activation: RELU_6
batch_norm {
decay: 0.999700009823
center: true
scale: true
epsilon: 0.0010000000475
train: true
}
}
min_depth: 0
max_depth: 0
num_layers_before_predictor: 0
use_dropout: false
dropout_keep_probability: 0.800000011921
kernel_size: 3
box_code_size: 4
apply_sigmoid_to_scores: false
}
}
anchor_generator {
ssd_anchor_generator {
num_layers: 6
min_scale: 0.20000000298
max_scale: 0.949999988079
aspect_ratios: 1.0
aspect_ratios: 2.0
aspect_ratios: 0.5
aspect_ratios: 3.0
aspect_ratios: 0.333299994469
}
}
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: SIGMOID
}
normalize_loss_by_num_matches: true
loss {
localization_loss {
weighted_smooth_l1 {
}
}
classification_loss {
weighted_sigmoid {
}
}
hard_example_miner {
num_hard_examples: 3000
iou_threshold: 0.990000009537
loss_type: CLASSIFICATION
max_negatives_per_positive: 3
min_negatives_per_image: 3
}
classification_weight: 1.0
localization_weight: 1.0
}
}
}
train_config {
batch_size: 35
data_augmentation_options {
random_horizontal_flip {
}
}
data_augmentation_options {
ssd_random_crop {
}
}
optimizer {
rms_prop_optimizer {
learning_rate {
exponential_decay_learning_rate {
initial_learning_rate: 0.00400000018999
decay_steps: 800720
decay_factor: 0.949999988079
}
}
momentum_optimizer_value: 0.899999976158
decay: 0.899999976158
epsilon: 1.0
}
}
num_steps: 200
}
train_input_reader: {
tf_record_input_reader {
input_path: "temp/data/train.record"
}
label_map_path: "configs/reviews/labels.pbtxt"
}
eval_config {
num_examples: 20
max_evals: 10
use_moving_averages: false
}
eval_input_reader: {
tf_record_input_reader {
input_path: "temp/data/test.record"
}
label_map_path: "configs/reviews/labels.pbtxt"
shuffle: false
num_readers: 1
}
如您所见,我正在尝试从头开始训练数据,而不是使用现有模型。这样做的原因是因为我不是在寻找已经训练过的一般对象。
我调整了 fixed_shape_resizer,因为评论的图像尺寸大约在宽 2000 和高 500 左右。
我只用了 200 步进行训练(我需要做更多吗?),正如我在 Tensorboard 中注意到的那样,在 100 步之后它开始学习,因为 'Loss' 结果开始下降下降了不少。 Click here to see Tensorboard results
然而,当我 export/freeze 图表然后尝试预测时。什么都预测不到。在我看来一切都很好。我做错了什么吗?
使用这个 API 涉及很多步骤,所以不确定您可能弄错了哪一个,但我的感觉是,如果您从头开始训练对象检测模型,那么您需要庞大的数据集。 100 张图像是非常小的数据集。您可以查看训练预训练对象检测模型(如 ResNet、ImageNet)的数据集的大小。那里的数据集非常庞大。因此,要获得结果,您需要使用预训练模型或更大的数据集。为了增加数据集,您可以对 API 之外的现有图像进行图像增强,并使用算法调整它们的注释。当我做类似的练习时,我使用了大约 10K 个带有预训练模型和图像增强的图像。
如果您使用 labelImg 进行图像注释,请确保您了解工具中的this问题