首页 > 代码库 > tensorflow训练Oxford-IIIT Pets

tensorflow训练Oxford-IIIT Pets

参考链接https://github.com/tensorflow/models/blob/master/object_detection/g3doc/running_pets.md

先参考https://github.com/tensorflow/models/blob/master/object_detection/g3doc/installation.md安装好环境

 

以下默认都在models目录下操作

mkdir petstrain

 

注意PYTHONPATH库路径的设置

# From tensorflow/models/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

下载Oxford-IIIT Pets的图片和标注信息

# From tensorflow/models/
cd petstrain 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 tar -xvf images.tar.gz tar -xvf annotations.tar.gz
cd ..

转换成TFRecord格式

# From tensorflow/models/
cp object_detection/data/pet_label_map.pbtxt petstrain/
python3 object_detection/create_pet_tf_record.py --label_map_path=petstrain/pet_label_map.pbtxt --data_dir=petstrain --output_dir=petstrain

在petstrain目录生成pet_train.record    pet_val.record

 

下载预训练的模型

cd petstrain
wget http://storage.googleapis.com/download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_coco_11_06_2017.tar.gz tar -xvf faster_rcnn_resnet101_coco_11_06_2017.tar.gz
mv faster_rcnn_resnet101_coco_11_06_2017/model.ckpt.* .
cd ..

修改配置文件

cp object_detection/samples/configs/faster_rcnn_resnet101_pets.config petstrain/

# From tensorflow/models/

# Edit the faster_rcnn_resnet101_pets.config template. Please note that there
# are multiple places where PATH_TO_BE_CONFIGURED needs to be set.
sed -i "s|PATH_TO_BE_CONFIGURED|petstrain|g" petstrain/faster_rcnn_resnet101_pets.config

训练的目录下会有以下文件

  + petstrain/
    - faster_rcnn_resnet101_pets.config
    - model.ckpt.index
    - model.ckpt.meta
    - model.ckpt.data-00000-of-00001
    - pet_label_map.pbtxt
    - pet_train.record
    - pet_val.record

开始训练

# From tensorflow/models/
python3 object_detection/train.py --logtostderr --pipeline_config_path=petstrain/faster_rcnn_resnet101_pets.config  --train_dir=petstrain/res/

查看训练进度

# From tensorflow/models/
tensorboard --logdir=pet-train/res/

打开浏览器localhost:6006

 

导出训练好的图

# From tensorflow/models/

object_detection/export_inference_graph.py --input_type image_tensor \
    --pipeline_config_path petstrain/faster_rcnn_resnet101_pets.config \
    --checkpoint_path petstrain/res/model.ckpt-445 \
    --inference_graph_path petstrain/output_inference_graph.pb

这样便得到output_inference_graph.pb文件

 

tensorflow训练Oxford-IIIT Pets