作者:书友49537618 | 来源:互联网 | 2023-08-08 15:29
cloud执行:https:github.comtensorflowmodelsblobmasterresearchobject_detectiong3docrunning_pet
cloud执行:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_pets.md
本地执行:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_locally.md
1. 获取数据Oxford-IIIT Pets Dataset
# From tensorflow/models/research/
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
最后tensorflow/models/research/下文件结构
images/
annotations/ object_detection/
others
|
2. 对数据进行转换
Tensorflow Object Detection API希望数据是TFRecode格式,所以先执行create_pet_tf_record脚本来将Oxford-IIIT pet数据集进行转换
注:要提前安装好需要的库,不然这一步会有不少错
#From tensorflow/models/research/
python object_detection/dataset_tools/create_pet_tf_record.py \
--label_map_path=object_detection/data/pet_label_map.pbtxt \
--data_dir=`pwd` \
--output_dir=`pwd`
# 在tensorflow/models/research/会生成10个标准的TFRecord文件:pet_faces_train.record-* pet_faces_val.record-*
cp pet_faces_train.record-* /tensorflow/models/research/object_detection/data
cp pet_faces_val.record-* /tensorflow/models/research/object_detection/data
cp object_detection/data/pet_label_map.pbtxt ${YOUR_DIRECTORY}/data/pet_label_map.pbtxt
最后结果:
两个TFRecode文件将会在tensorflow/models/research/下生成,分别为pet_train_with_mask.record和pet_val_with_mask.record(和例子中给出的不一样)
遇到的问题:
- TypeError: __init__() got an unexpected keyword argument 'serialized_options'
protobuf原来用的3.6.1版本,改成3.5.1就对了
可以在https://github.com/google/protobuf/releases下载exe文件,然后在系统变量中配置其路径
- NewRandomAccessFile failed to Creat/Open: xxxx No such process
文件的路径写错了,没有找到相应的文件
3. 下载已经训练好的COCO模型
下载训练好的模型,且放到data目录下
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
cp faster_rcnn_resnet101_coco_11_06_2017/model.ckpt.* ${YOUR_DIRECTORY}/data/
4. 配置对象检测pipeline
Tensorflow Object Detection API中模型参数、训练参数、评估参数都是在一个config文件中配置
object_detection/samples/configs下式一些object_detection配置文件的结构。这里用faster_rcnn_resnet101_pets.config作为配置的开始。搜索文件中的PATH_TO_BE_CONFIGURED,并修改,主要是数据存放的路径
5. object dectection代码进行打包
调用.sh文件,后面的/tmp/pycocotools是输出目录
.sh文件做的事情:
- 下载https://github.com/cocodataset/cocoapi.git
-
并且创建pycocotools目录,需要放到object_detection下
# From tensorflow/models/research/
# 下载pycocotools-2.0.tar到/tmp/pycocotools下
bash object_detection/dataset_tools/create_pycocotools_package.sh /tmp/pycocotools
# 然后解压到object_detection/下
tar -xvf faster_rcnn_resnet101_coco_11_06_2017.tar.gz /object_detection
# 进入PythonAPI,调用setup.py
python setup.py
问题:
- cl: 命令行 error D8021 :无效的数值参数“/Wno-cpp”
https://blog.csdn.net/heiheiya/article/details/81128749
可以把这个项目下载下来,然后在PythonAPI中执行set up
- 原教程中的cd slim&python setup.py sdists,是用来打包的(因为我是本地跑所以没有执行)
6. 开始训练和评估
为了开始训练和执行,在tensorflow/models/research/ 目录下执行如下命令
# From tensorflow/models/research/
python object_detection/model_main.py
--pipeline_config_path=${YOUR_DIRECTORY}\object_detection\samples\configs\faster_rcnn_resnet101_pets.config
--model_dir=${YOUR_DIRECTORY}\object_detection\data
--num_train_steps=50000
--num_eval_steps=2000
--alsologtostderr
问题:
-
\object_detection\models\faster_rcnn_inception_resnet_v2_feature_extractor.py", line 28, in from nets import inception_resnet_v2 ModuleNotFoundError: No module named 'nets'
因为我的目录中nets是在slim下的,只要到py文件中改下路径就好了
- File "xx\tensorflow\models\research\object_detection\core\post_processing.py", line 150, in multiclass_non_max_suppressionscore_threshold=score_thresh)TypeError: non_max_suppression() got an unexpected keyword argument 'score_threshold'
post_processing.py中把multiclass_non_max_suppression的参数删除就可以了
7. tensorboard对过程进行监视
tensorboard --logdir=${YOUR_DIRECTORY}/model_dir
8. 导出tensorflow图
文件保存在${YOUR_DIRECTORY}/model_dir,一般包括如下三个文件
- model.ckpt-${CHECKPOINT_NUMBER}.data-00000-of-00001
- model.ckpt-${CHECKPOINT_NUMBER}.index
- model.ckpt-${CHECKPOINT_NUMBER}.meta
找到一个要导出的checkpoint,执行命令
# From tensorflow/models/research/cp ${YOUR_DIRECTORY}/model_dir/model.ckpt-${CHECKPOINT_NUMBER}.* .
python object_detection/export_inference_graph.py \
--input_type image_tensor \
--pipeline_config_path object_detection/samples/configs/faster_rcnn_resnet101_pets.config \
--trained_checkpoint_prefix model.ckpt-${CHECKPOINT_NUMBER} \
--output_directory exported_graphs
最后exported_graphs中包含保存的模型和图
9. 一些小坑
- 原来用git clone来下models文件,很容易失败。直接下载models.zip会快一些