配置TensorFlow的objetc_detection api,训练自己的数据

objetc_detection api:

https://opensource.googleblog.com/2017/06/supercharge-your-computer-vision-models.html

github:

https://github.com/tensorflow/models/tree/master/research/object_detection


配置:


sudo apt-get install protobuf-compiler python-pil python-lxml
sudo pip install jupyter
sudo pip install matplotlib
sudo pip install pillow
sudo pip install lxml
sudo pip install jupyter
sudo pip install matplotlib
# From tensorflow/models/research/
protoc object_detection/protos/*.proto --python_out=.

vim ~/.bashrc
.bashrc中添加:
export PYTHONPATH=$PYTHONPATH:/path/to/research:/path/to/research/slim
保存
source ~/.bashrc
测试一下:
python object_detection/builders/model_builder_test.py


在这个过程可能会遇到一个问题:
" protoc object_detection/protos/*.proto --python_out=.",
保存:
object_detection/protos/anchor_generator.proto:11:3: Expected "required", "optio nal", or "repeated".
object_detection/protos/anchor_generator.proto:11:32: Missing field number.
解决方法:
tensorflow$ mkdir protoc_3.3
tensorflow$ cd protoc_3.3
tensorflow/protoc_3.3$ chmod 775 protoc-3.3.0-linux-x86_64.zip
tensorflow/protoc_3.3$ unzip protoc-3.3.0-linux-x86_64.zip
tensorflow/protoc_3.3$ cd ../models/
tensorflow/protoc_3.3$ /home/tensorflow/protoc_3.3/bin/protoc object_detection/protos/*.proto --python_out=.

训练自己的数据



需要准备的东西:
image文件夹,里面是你要训练的图片
xml文件,里面是这些图片的xml
image_list,里面是图片的文件名(不带后缀)


1、转为tfrecode
官方给的create_pascal_tf_record.py是按照pascal数据写的,咱们用的时候需要做一定更改
大家可以直接用我改好的
https://github.com/withyou1771/object_detection_api/blob/master/create_tf_record.py
python create_tf_record.py --output_path=/path/to/train.record

只需要修改main()函数下的data_dir,annotations_dir,examples_path,label_map_dict

label_map_dict可以参考data/pascal_label_map.pbtxt,根据自己的label写一个,但是注意,id必须是1,2,3,4...如果用别的id,比如说01,02,03...就会报错
报错:
label_map.ParseFromString(label_map_string)
google.protobuf.message.DecodeError: Error parsing message

有的xml文件会没有标注框,这时候就会报错,
报错:
KeyError: 'object'
把没有标注框的图片去掉就好啦



2、下载预训练的模型
  1. wget http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_coco_11_06_2017.tar.gz  
  2. tar -zxvf faster_rcnn_resnet101_coco_11_06_2017.tar.gz  

3、修改.config
object_detection/samples/configs下有各式各样的网络,选一个你喜欢的
修改:
num_classes: 类别数目,有10类就是10
fine_tune_checkpoint:faster_rcnn_resnet101_coco_11_06_2017解压出的模型
from_detection_checkpoint: 为true
num_steps: 训练步数
PATH_TO_BE_CONFIGURED修改为相应的路径

4、训练

python object_detection/train.py \  
     --pipeline_config_path='/path/to/train.config' \  
     --train_dir='/path/to/model/train'  
train_dir是保存训练模型的地方

如果遇到这个错:
报错:
RuntimeError: generator didn't yield
ERROR:tensorflow:==================================
Object was never used (type <class 'tensorflow.python.framework.ops.Tensor'>):
<tf.Tensor 'init_ops/report_uninitialized_variables/boolean_mask/Gather:0' shape=(?,) dtype=string>
If you want to mark it as used call its "mark_used()" method.
It was originally created here:
那么可能是你的预训练模型和你的输出模型在一个文件夹,或者是你的预训练模型缺少文件,建议重新解压一次
接着就是等待训练完成啦



展开阅读全文

没有更多推荐了,返回首页