目录
第二章 object_detection开源框架使用案例入门
第四章 基于tensorflow-cpu的object_detection开源框架模型训练
第五章 基于GPU服务器的object_detection开源框架模型训练
第二章 object_detection开源框架使用案例入门
本章基于现成标注好的数据集,此处是一个动物检测的数据集。
2.1 下载数据集
创建目录:
/home/users/py3_project/models/research/object_detection/train_pets
1 下载图片+标注数据集
# wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz
# wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz
2 解压
# tar -xvf images.tar.gz
# tar -xvf annotations.tar.gz
2.2 转换数据集为tf_record格式
备注:下面的TFODHOME是配置好的环境变量,因为路径太长,在此只为方便操作
TFODHOME=/home/users/py3_project/models/research/object_detection
并执行以下命令
# python3 $TFODHOME/dataset_tools/create_pet_tf_record.py \
--label_map_path=$TFODHOME/data/pet_label_map.pbtxt \
--data_dir=$TFODHOME/train_pets \
--output_dir=$TFODHOME/train_pets/image_tfrecord
执行结果如下:
2.3 下载预训练模型
# wget http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_11_06_2017.tar.gz
# tar -xvf ssd_mobilenet_v1_coco_11_06_2017.tar.gz
将解压好的预训练模型目录重命名为model_ssd,存入train_pets目录下。
2.4 修改相关配置文件
1 修改配置文件:object_detection/samples/configs/ssd_mobilenet_v1_pets.config
- 修改类别数
- 修改预训练模型路径
fine_tune_checkpoint: “/home/users/py3_project/models/research/object_detection/train_pets/model_ssd/model.ckpt”
- 修改输入训练数据配置信息
train_input_reader: {
tf_record_input_reader {
input_path: “/home/users/py3_project/models/research/object_detection/train_pets/image_tfrecord/pet_train.record”
}
label_map_path: “/home/users/py3_project/models/research/object_detection/train_pets/pet_label_map.pbtxt”
}
- 修改预测数据配置信息
eval_input_reader: {
tf_record_input_reader {
input_path: “/home/users/py3_project/models/research/object_detection/train_pets/image_tfrecord/pet_val.record”
}
label_map_path: “/home/users/py3_project/models/research/object_detection/train_pets/pet_label_map.pbtxt”
shuffle: false
num_readers: 1
}
将 object_detection/samples/configs/ssd_mobilenet_v1_pets.config 拷贝到 $TFODHOME/train_pets/路径
将 object_detection/data/pet_label_map.pbtxt 拷贝到 $TFODHOME/train_pets/路径
2.5 进行模型训练
# python3 $TFODHOME/train.py \
--logtostderr \
--pipeline_config_path=$TFODHOME/train_pets/ssd_mobilenet_v1_pets.config \
--train_dir=$TFODHOME/train_pets/train_dir
2.6 模型预测与检测
# mkdir $TFODHOME/train_pets/eval_result
# python3 $TFODHOME/eval.py \
--logtostderr \
--pipeline_config_path=$TFODHOME/train_pets/ssd_mobilenet_v1_pets.config \
--checkpoint_dir=$TFODHOME/train_pets/train_dir \
--eval_dir=$TFODHOME/train_pets/eval_result
2.7 转换为图文件
将model.chk文件转换为graph
# python3 $TFODHOME/export_inference_graph.py \
--input_type=image_tensor \
--pipeline_config_path=$TFODHOME/train_pets/ssd_mobilenet_v1_pets.config \
--trained_checkpoint_prefix=$TFODHOME/train_pets/train_dir/model.ckpt-200001 \
--output_directory=$TFODHOME/train_pets/inference_graph
2.8 测试图片
安装,使用 VNC-Viewer 客户端
执行自己的一张准备好的图片
# python3 test.py temp/time.jpg