Windows10下Object Detection API实战记录(3)——训练自己的目标检测模型
1、创建新模型的文件夹
网上查了很多资料,发现很多都是直接在Object Detection文件夹下创建新模型的相应文件,这无疑会使文件看起来更加混乱,所以我创建一个专门的文件夹来保存新模型相关文件。(model_1/)
命 名 | 作 用 |
---|---|
data | 存放数据 |
pre_model | 存放预训练相关文件 |
result | 存放训练结果 |
saved_model | 保存训练好的最终模型 |
training | 存放训练配置文件 |
将前一篇博客生成的train.record,和test.record文件分别复制到data/train/和data/test/路径下。
后继所需要的文件会在下面过程中根据需要添加。
2、下载预训练模型
打开网站https://github.com/tensorflow/models选择想要运用的预训练模型,可供选择的模型可以满足大部分任务。
本次选用的模型为ssd_mobilenet_v2_coco,下载之后解压,存放在pre_model中:
3、创建标签分类的配置文件
在(model_1/training/)文件夹下新建txt文件,键入以下内容:
item{
id:1
name:'sun'
}
item{
id:2
name:'huang'
}
记住id和mane的对应关系要与cvs_to_tfrecord.py中的保持一致。
重命名为label_map.pbtxt。(注意是pbtxt格式的文件)
4、配置config文件
在Object Detection API安装路径中(/object_detection/samples/configs/)下找到ssd_mobilenet_v2_coco.config文件:
将他复制到model_1/training/路径下,
打开ssd_mobilenet_v2_coco.config更改config文件:
# 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: 12
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: "C:/Users/pang/Desktop/Python_Code/tensorflow_random_model/model_1/pre_model/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: 20000
data_augmentation_options {
random_horizontal_flip {
}
}
data_augmentation_options {
ssd_random_crop {
}
}
}
train_input_reader: {
tf_record_input_reader {
input_path: "C:/Users/pang/Desktop/Python_Code/tensorflow_random_model/model_1/data/train/train.record"
}
label_map_path: "C:/Users/pang/Desktop/Python_Code/tensorflow_random_model/model_1/training/label_map.pdtxt"
}
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: "C:/Users/pang/Desktop/Python_Code/tensorflow_random_model/model_1/data/test/test.record"
}
label_map_path: "C:/Users/pang/Desktop/Python_Code/tensorflow_random_model/model_1/training/label_map.pdtxt"
shuffle: false
num_readers: 1
}
(1)第9行:类别数量,我的更改为num_classes: 2
(2)第141行:根据机器性能调整,batch_size: 24
(3)第156行: 预训练模型位置(建议更改为绝对路径),我的位置更改为 fine_tune_checkpoint:“C:/Users/pang/Desktop/Python_Code/tensorflow_random_model/model_1/pre_model/model.ckpt”
(4)第175行:输入的训练文件(train.record)的地址,以及标签(labelmap.pbtxt)的所在地址
(5)第162行: num_steps: 200000 # 需要训练的step
(6)第189行:输入的测试文件(test.record)的地址,以及标签(labelmap.pbtxt)的所在地址
(7)第181行:number_examples是测试样本的个数(这里我们没有创建验证集,直接用测试集进行代替),根据实际修改
5、训练模型
Tensorflow object detection API 提供了训练脚本,将models/research/object_detection/legacy路径下的train.py文件复到model_1/文件夹下,打开终端,使用cd命令定位在model_1文件夹下:
执行一下代码:
python train.py --train_dir=result/ --pipeline_config_path=training/ssd_mobilenet_v2_coco.config --logtostderr
train_dir是生成文件的存放地址,pipeline_config_path就是配置文件的存放地址
注意:
我在这里刚开始训练会报一个错误:“error:No modul named pycocotools”
为了记录训练过程的整体性,这个错误的解决方案会在下篇博客中记录!!
训练开始:
可以按Ctrl+C随时终止训练。
result文件夹下有文件:
6、保存训练的模型
打开终端,同样定位到model_1文件夹下,输入以下命令:
python export_inference_graph.py --input_type=image_tensor --pipeline_config_path=training/ssd_mobilenet_v2_coco.config --trained_checkpoint_prefix=result/model.ckpt-3792(此处的编号与你的训练次数相同) --output_directory=saved_model
之后会在saved_model文件夹下保存最终的模型文件。
7、使用tensorboard查看训练情况
打开终端,同样定位到model_1文件夹下,输入以下命令:
tensorboard --logdir=result
复制网址http://localhost:6006/,在浏览器中打开即可。
模型训练完毕!!!
最后附上文件夹model_1的内容,其中utils和object_detection_tutorial.ipynb文件用来检测的
参考
https://blog.csdn.net/weixin_42499236/article/details/83833102
下篇博客将记录
(1)“error:No modul named pycocotools”的解决方法
(2)用训练好的模型进行检测