Ubuntu 安装 TensorFlow object detection API (MS COCO, The Open Images Dataset)

本文用于记录tensorflow object detection api的安装与使用,主要包括COCO 数据集和更大的The Open Images Dataset数据集,以便将来快速查找。

主要步骤为:

  1. 下载代码 (https://github.com/tensorflow/models)
    使用zip压缩包下载,将“models-master”修改为“models”
  2. 创建并激活conda环境:
	conda create -n tfapi python=3.6 tensorflow-gpu=1.12.0

安装tensorflow-gpu(2019/8/24 jupyter测试代码中要求使用1.12.0及以上版本)
可能出现cuda 与 tensorflow的版本匹配问题,可参考tensorflow各个版本的CUDA以及Cudnn版本对应关系进行选择
3. 安装其他依赖包
pillow,lxml,Cython,jupyter,matplotlib,pandas,opencv-python

	pip install 依赖包名称

可能出现cuda 与 tensorflow的版本匹配问题,可参考tensorflow各个版本的CUDA以及Cudnn版本对应关系进行选择
CUDA和cudnn搭配

此步可能提示版本不匹配问题,根据提示更改即可。
4. 安装protoc
打开网址https://github.com/protocolbuffers/protobuf/releases,根据自己的ubuntu位数,下载想要的包,我这里下载的是protoc-3.14.0-linux-x86_64.zip。
protoc
下载后解压,执行如下命令:

	sudo cp bin/protoc /usr/bin/protoc

编译proto文件
#在models/research下运行

	protoc object_detection/protos/*.proto --python_out=.

运行完成后,可以检查object_detection/protos/文件夹,如果每个proto文件都成了对应的以py为后缀的python源码,就说明编译成功了。
5. 将slim加入PATHPATH
(1)在models/research/slim中找到setup.py,执行以下命令:

	python setup.py build
	python setup.py install

(2)将slim加入PYTHONPATH,输入命令如下:

	sudo gedit ~/.bashrc

在打开的文件最下方输入:
pwd: 指的是slim所在的绝对路径

	export PYTHONPATH=$PYTHONPATH:'pwd':'pwd'/slim

使得~/.bashrc的修改生效:

	source  ~/.bashrc

执行import slim成功则说明已经正确设置好了。

  1. 运行models/research下的setup.py
	python setup.py build
	python setup.py install

安装完成测试
在models/research下运行如下命令:

	python object_detection/builders/model_builder_test.py

出现如下信息,说明已安装成功:

  1. 安装COCO API
	git clone https://github.com/cocodataset/cocoapi.git
	cd cocoapi/PythonAPI
	python setup.py build
	python setup.py install
  1. 执行已经训练好的模型
    在research文件夹下运行命令:jupyter notebook,接着在jupyter中打开object_detection文件夹,并单击object_detection_tutorial.ipynb运行试例文件。
    1)安装环境直接使用
    2)转换成.py文件,本地使用,删除get_ipython()之类的代码,删除 Instance Segmentation部分的代码。
    3)Error: load() missing 2 required positional arguments: ‘tags’ and ‘export_dir’
    解决办法,删除1.x老版的TensorFlow,更新为2.x
	pip uninstall tensorflow
	pip install tensorflow-gpu==2.0

4)Segmentation fault或者 Process finished with exit code 139。 GPU现存溢出,可以使用CPU版本的TensorFlow做inference。
解决方法:将tensorflow-gpu的版本从2.x降低到1.15即可使用GPU加速。

tensorflow-gpu==2.0 时不能GPU加速,会报错Segmentation fault,但是在运行object_detection_tutorial.ipynb时,需要2.0才不会报错load() missing 2 required positional arguments: 'tags' and 'export_dir'

tensorflow-gpu==1.15.0 时可以使用GPU加速,但是运行object_detection_tutorial.ipynb时,会报错load() missing 2 required positional arguments: 

5)ImportError: No module named tensorflow.compat.v1 解决方法:更新TensorFlow版本,例如更到1.15 或者 2.x。

运行所有代码后的结果如下:
结果

  1. 使用Open Images Dataset 预训练模型
    (1)将PATH_TO_LABELS替换
PATH_TO_LABELS = "models/research/object_detection/data/oid_v4_label_map.pbtxt"

(2)将代码中的model替换

	 model_name = "faster_rcnn_inception_resnet_v2_atrous_oid_v4_2018_12_12"

删除Instance Segmentation模块,只留下Object Detection部分。

  1. 编译及测试
    参见:Ubuntu18.04下安装TensorFlow Object Detection API

运行jupyter notebook测试时,未能绘制图像,问题及解决方法参考:

解决问题:UserWarning: Matplotlib is currently using agg, which is a non-GUI backend.
执行代码,没有弹出图像窗口,提示下面的内容

训练自己的数据集, TensorFlow Object Detection API 技术手册(6)——模型文件配置及模型训练添加链接描述

参考:
1-Ubuntu18.04下安装TensorFlow Object Detection API
2-tensorflow object detection api 详细实践教程
3-ubuntu18.04 tensorflow object detection api 安装

基于TensorFlow Object Detection API搭建自己的物体识别模型的代码如下: 1. 准备工作: - 安装TensorFlow Object Detection API - 准备训练和测试数据集 - 下载预训练的模型权重 2. 导入所需库: ```python import tensorflow as tf from object_detection.utils import dataset_util from object_detection.utils import label_map_util from object_detection.utils import visualization_utils as vis_util ``` 3. 加载label map和模型: ```python PATH_TO_LABELS = 'path_to_label_map.pbtxt' PATH_TO_MODEL = 'path_to_pretrained_model' label_map = label_map_util.load_labelmap(PATH_TO_LABELS) categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=90, use_display_name=True) category_index = label_map_util.create_category_index(categories) detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_MODEL, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') ``` 4. 定义函数进行物体识别: ```python def detect_objects(image): with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') detection_classes = detection_graph.get_tensor_by_name('detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') image_expanded = np.expand_dims(image, axis=0) (boxes, scores, classes, num) = sess.run( [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_expanded}) vis_util.visualize_boxes_and_labels_on_image_array( image, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=8) return image ``` 5. 加载测试图像并进行物体识别: ```python image = cv2.imread('test_image.jpg') image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) output_image = detect_objects(image) cv2.imshow('Object Detection', output_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 通过以上代码,可以使用自己的训练数据集、预训练模型权重和标签映射文件来搭建自己的物体识别模型。设置好路径并加载模型后,将待识别的图像传入`detect_objects`函数即可返回识别结果,并在图像上进行可视化展示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值