由于不开学,我用的是自己的笔记本,环境配置如下:
环境配置:Ubuntu16.04+CUDA9.0+CuDNN7.0.5+NVIDIA Driver 418.56+tensorflow-CPU 1.
用GPU跑笔记本散热不好,实在是太热。
电脑显卡:GetForce MX250 4G
电脑内存:8G
处理器:intel core i7
参考:
[1] ubuntu16.04下Tensorflow object detection API安装
git clone https://github.com/tensorflow/models.git
得到一个models的文件夹,里面包含了google开源的模型代码,目标检测源码存放在/research/object_detection目录下
也可点击此处下载model文件,下载日期2020年2月,并以更改slim文件夹下的参数适用于tensorflow1版本。
# 查看protoc版本
protoc --version
cd models/research/object_detection/protos
# 编译proto文件生成对应的py文件
protoc object_detection/protos/*.proto --python_out=.
# 编译安装research
cd /models/research
python setup.py install
# 编译安装slim
cd /models/research/slim
python setup.py build
python setup.py install
# 将slim添加到PYTHONPATH
# 方式1
sudo gedit ~/.bashrc
末尾添加:export PYTHONPATH=$PYTHONPATH:/home/×××/models/research:/home/lenovo/models/research/slim
source ~/.bashrc
# 方式2
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
cd /models/research
python object_detection/builders/model_builder_test.py
运行成功即可。
参数不匹配
、找不到参数
的问题,主要原因是下载的git 方式下载model文件已经使用了tf2+版本编写,儿我们使用的tf为tf1+。compat.v1
参数的问题,如:AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'
tensorflow.compat.v1.******
,更改后:`tensorflow.******``from nets import inception_resnet_v2 ImportError: No module named 'nets'
的报错解决方案。主要使用labeling软件进行操作,介绍资源比较多,不再赘述。
生成的xml文件与对应jpg文件名称保持一致存放在一个文件夹下。
参考:
[3] Tensorflow object detection API训练自己的数据
[4] TensorFlow object detection API应用
[5] 利用TensorFlow object detection API训练自己的检测器
-data/
--test_labels.csv #我没有 不影响
--test.record
--train_labels.csv #我没有 不影响
--train.record
-images/ # 新建
--test/
---xxx.jpg
---xxx.xml
--train/
---xxx.jpg
---xxx.xml
-training/ # 新建
--object_label.pbtxt #定义类别的标签
--ssdlite_mobilenet_v2.config #训练配置文件
---ssdlite_mobilenet_v2_coco_11_06_2017
.record文件的生成
由于object_detection需要的输入格式是TFRecord, 所以我们必须把原图.jpg 和 LabelImg生成的.xml 先转换成 .csv 最后再转成 .record。具体需要通过两个脚本xml2csv.py,generate_tfrecord.py,得到data/train.record与data/test.record文件。
object_label.pbtxt
在training文件夹下新建并按原博提供的格式添加即可。
ssdlite_mobilenet_v2.config
方法1:objec_detection/samples下,寻找需要的对于模型的config文件。
方法2:官方提供的model zoo里下载训练好的模型。在 object_dection文件夹下,解压 你的模型*.tar.gz,
将对应的*****.config 放在training 文件夹下。
接下来开始对config文件进行修改,更改的地方我都标#,并以ssdlite_mobilenet_v2.config为例简要介绍 :
model {
ssd {
num_classes: 1 #类别数
image_resizer {
fixed_shape_resizer {
height: 300 # 可选
width: 300 # 可选
}
}
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
use_depthwise: true
}
}
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: 6 #原本是24,越小越不容易出现内存溢出被killed的现象,最小为1
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
}
}
# 用finetune,在它原来训练好的模型数据上进行训练,这样可以快很多。不然从头训练好慢。
fine_tune_checkpoint: "ssdlite_mobilenet_v2_coco_2018_05_09/model.ckpt" # 下载的模型路径下的ckpt文件
num_steps: 10000 # 训练的歩数
fine_tune_checkpoint_type: "detection"
}
train_input_reader {
label_map_path: "training/object_label.pbtxt" # label_map_path
tf_record_input_reader {
input_path: "data/train.record" # train.record路径
}
}
# eval部分的 num_examples 和 运行设置参数--num_eval_steps 时任何一个值只要比你数据集中训练图片数目要大就会出现警告,
# 因为它没那么多图片来评估,所以这两个值直接设置成训练图片数量就好了。
eval_config {
num_examples: 1863
max_evals: 10
use_moving_averages: false
}
eval_input_reader {
label_map_path: "training/object_label.pbtxt" # label_map_path
shuffle: false
num_readers: 1
tf_record_input_reader {
input_path: "data/test.record" # test.record路径
}
}
# 调用model_main.py执行
python model_main.py --model_dir=training --pipeline_config_path=training/ssdlite_mobilenet_v2_coco.config --num_train_steps=60000 --num_eval_steps=20 --alsologtostderr
# 也可以使用旧版本训练
python legacy/train.py --train_dir=training/ --pipeline_config_path=training/ssdlite_mobilenet_v2_coco.config --logtostderr
我使用model_main.py无法训练,使用legacy/train.py可以正常训练。
cd ~/models/research/object_detection/training
tensorboard --logdir=training
ensorboard出现No dashboards are active for the current data set. Probable causes: ...
问题。#当前路径和--logdir的关系:
#tensorboard --logdir=C:\Users\dbsdz\Desktop\TensorBoardTest\log 可以
#cd E://TensorBoardTest tensorboard --logdir=log 可以
#e: cd TensorBoardTest tensorboard --logdir=log 可以