1.环境搭建
本文使用的环境为ubuntu18.04 anaconda 虚拟环境
虚拟环境搭建完成,需要在虚拟环境安装相应的tensorflow软件以及cudatoolkit,cudnn ,tensorflow-gpu版本为1.13.1 安装为: conda install tensorflow-gpu==1.13.1
conda install cudatoolkit=10.1 cudnn=7.6.5
到此基础环境搭建完。
下载后然后解压文件进入文件夹: cd /home/lijingle/deep_work/2Dimage/mobilenetssd/mode/models-1.13.0/research
然后安装相关模块: protoc object_detection/protos/*.proto --python_out=.
#安装库
python setup.py build
python setup.py install
在当前终端导出环境变量: export PYTHONPATH=$PYTHONPATH:/home/lijingle/deep_work/2Dimage/mobilenetssd/mode/models-1.13.0/research
export PYTHONPATH=$PYTHONPATH:/home/lijingle/deep_work/2Dimage/mobilenetssd/mode/models-1.13.0/research/slim
如果多次使用可以将上述环境变量添加到系统环境变量,即vi /etc/profile 文件中 测试安装是否成功,代码如下: python object_detection/builders/model_builder_test.py
2.训练
在object_detection下创建文件夹ssd_model mkdir ssd_model/
cd ssd_model
tar xvf VOCtrainval_11-May-2012.tar
返回 research/目录,执行 train 和 val 脚本 cd ../..
python ./object_detection/dataset_tools/create_pascal_tf_record.py --label_map_path=./object_detection/data/pascal_label_map.pbtxt --data_dir=object_detection/ssd_model/VOCdevkit/ --year=VOC2012 --set=train --output_path=./object_detection/ssd_model/pascal_train.record
python ./object_detection/dataset_tools/create_pascal_tf_record.py --label_map_path=./object_detection/data/pascal_label_map.pbtxt --data_dir=./object_detection/ssd_model/VOCdevkit/ --year=VOC2012 --set=val --output_path=./object_detection/ssd_model/pascal_val.record
执行后会生成两个文件: pascal_train.record 和 pascal_val.record 然后copy配置文件: cp object_detection/data/pascal_label_map.pbtxt object_detection/ssd_model/
cp object_detection/samples/configs/ssd_mobilenet_v1_pets.config object_detection/ssd_model/
修改配置文件ssd_mobilenet_v1_pets.config,修改结果如下: train_input_reader: {
tf_record_input_reader {
input_path: "/home/lijingle/deep_work/2Dimage/mobilenetssd/mode/models-1.13.0/research/object_detection/ssd_model/pascal_train.record"
}
label_map_path: "/home/lijingle/deep_work/2Dimage/mobilenetssd/mode/models-1.13.0/research/object_detection/ssd_model/pascal_label_map.pbtxt"
}
eval_input_reader: {
tf_record_input_reader {
input_path: "/home/lijingle/deep_work/2Dimage/mobilenetssd/mode/models-1.13.0/research/object_detection/ssd_model/pascal_val.record"
}
label_map_path: "/home/lijingle/deep_work/2Dimage/mobilenetssd/mode/models-1.13.0/research/object_detection/ssd_model/pascal_label_map.pbtxt"
shuffle: false
num_readers: 1
}
下载训练好的mobilenetssd文件,作为微调 ssd_mobilenet_v1_coco_11_06_2017.tar.gz将文件放入ssd_model下,然后解压: tar zxvf ssd_mobilenet_v1_coco_11_06_2017.tar.gz
mv ssd_mobilenet_v1_coco_11_06_2017 ssd_mobilenet
修改配置文件ssd_mobilenet_v1_pets.config: fine_tune_checkpoint: "/home/lijingle/deep_work/2Dimage/mobilenetssd/mode/models-1.13.0/research/object_detection/ssd_model/ssd_mobilenet_v1_coco_11_06_2017/model.ckpt"
from_detection_checkpoint: true
文件在legacy下,开始训练: python ./object_detection/legacy/train.py --train_dir ./object_detection/legacy/train/ --pipeline_config_path ./object_detection/ssd_model/ssd_mobilenet_v1_pets.config
最终运行结果如下;
模型转化,转化为pb文件: python ./object_detection/export_inference_graph.py --input_type image_tensor --pipeline_config_path ./object_detection/ssd_model/ssd_mobilenet_v1_pets.config --trained_checkpoint_prefix ./object_detection/legacy/train/model.ckpt-9000 --output_directory ./object_detection/ssd_model/model/
|