Windows下Tensorflow2 Object detection API目标检测环境搭建

当前环境:

Win7

Anaconda3

1.安装Tensorflow

conda创建虚拟环境,我创建了名为tf2test的虚拟环境,安装了Tensorflow2.1。

conda install -n tf2test python=3.6
conda activate tf2test
conda install tensorflow-cpu==2.1.0

2.下载Tensorflow Object detection models

下载地址:https://github.com/tensorflow/models

下载完成后放在D盘根目录下,修改文件夹名称为models

3.下载protobuf

这个工具是用来把.proto文件转换成.py文件,下载地址:https://github.com/protocolbuffers/protobuf/releases

我下载的是最新的protoc-3.12.3-win64版本,下载解压后把protoc.exe文件放入windows文件夹(也可以把protoc.exe文件所在的目录加入环境变量)

启动Anaconda自带的PowerShell

(base) PS C:\Users\abc> conda activate tf2test
(tf2test) PS C:\Users\abc> D:
(tf2test) PS D:\>
(tf2test) PS D:\>
(tf2test) PS D:\> cd .\models
(tf2test) PS D:\models> cd .\research
(tf2test) PS D:\models\research>
(tf2test) PS D:\models\research>
(tf2test) PS D:\models\research>
(tf2test) PS D:\models\research> protoc object_detection/protos/*.proto --python
_out=.
object_detection/protos/input_reader.proto:5:1: warning: Import object_detection
/protos/image_resizer.proto is unused.

查看D:\models\research\object_detection\protos是否生成了.py文件,如果有则说明配置成功

4.安装Tensorflow model以及slim

(tf2test) PS D:\models\research> python setup.py build
(tf2test) PS D:\models\research> python setup.py install

(tf2test) PS D:\models\research> cd slim
(tf2test) PS D:\models\research\slim> python setup.py build
(tf2test) PS D:\models\research\slim> python setup.py install

在slim下执行安装命令时,可能会出现如下错误打印:error: could not create 'build': 当文件已存在时,无法创建该文件。这是因为slim下有一个BUILD的文件与要创建的build文件夹重合,把这个BUILD文件删除或者重命名再重新执行安装命令即可。

安装完成后执行:python .\object_detection\builders\model_builder_test.py测试,如果没有报错,则说明安装成功。

(tf2test) PS D:\models\research> python .\object_detection\builders\model_builder_test.py
(tf2test) PS D:\models\research>

如果出现“ImportError:No module named nets”,一般是没有安装slim,需要执行上面的命令安装slim。

安装完成后需要把D:\models\research和D:\models\research\slim加入环境变量,否则导入相关模块时会报错,在“环境变量-系统变量”新建一个变量PYTHONPATH,加入上述两个路径,用分号隔开。

5.测试

把D:\models\research\object_detection目录拷贝到jupyter notebook的工作目录下,由于官方自带的例子object_detection_tutorial.ipynb只支持tensorflow1.x,在tf2.x下无法运行,所以我们需要自己写代码测试。 

打开jupyter notebook,在object_detection目录下新建一个object_detection_test.ipynb文件,选择kernel为刚才新建的虚拟环境,参考了一位大佬的博客https://blog.csdn.net/xundh/article/details/103557820,完整代码如下:

import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
import pathlib
 
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
from IPython.display import display
 
#导入目标检测模型
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util
 
# patch tf1 into `utils.ops`
utils_ops.tf = tf.compat.v1
 
# Patch the location of gfile
tf.gfile = tf.io.gfile
 
#加载模型函数
def load_model(model_name):
	base_url = 'http://download.tensorflow.org/models/object_detection/'
	model_file = model_name + '.tar.gz'
	model_dir = tf.keras.utils.get_file(
		fname=model_name, 
		origin=base_url + model_file,
		untar=True)
	print(model_dir)
	model_dir = pathlib.Path(model_dir)/"saved_model"
 
	model = tf.saved_model.load(str(model_dir))
	model = model.signatures['serving_default']
 
	return model
 
#加载标签集合
# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = '.\data\mscoco_label_map.pbtxt'
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
 
# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
PATH_TO_TEST_IMAGES_DIR = pathlib.Path('test_images')
print(PATH_TO_TEST_IMAGES_DIR)
TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpg")))
TEST_IMAGE_PATHS
 
#加载mobilenet_v1模型
model_name = 'ssd_mobilenet_v1_coco_2017_11_17'
detection_model = load_model(model_name)
 
#调用模型的函数,赋值给本地变量
def run_inference_for_single_image(model, image):
	image = np.asarray(image)
	# The input needs to be a tensor, convert it using `tf.convert_to_tensor`.
	input_tensor = tf.convert_to_tensor(image)
	# The model expects a batch of images, so add an axis with `tf.newaxis`.
	input_tensor = input_tensor[tf.newaxis,...]
 
	# Run inference
	output_dict = model(input_tensor)
 
	# All outputs are batches tensors.
	# Convert to numpy arrays, and take index [0] to remove the batch dimension.
	# We're only interested in the first num_detections.
	num_detections = int(output_dict.pop('num_detections'))
	output_dict = {key:value[0, :num_detections].numpy() 
                 for key,value in output_dict.items()}
	output_dict['num_detections'] = num_detections
 
	# detection_classes should be ints.
	output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)
   
	# Handle models with masks:
	if 'detection_masks' in output_dict:
	# Reframe the the bbox mask to the image size.
		detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
			output_dict['detection_masks'], output_dict['detection_boxes'],
			image.shape[0], image.shape[1])      
		detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5,
                                       tf.uint8)
		output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()
    
	return output_dict
 
#在测试图片上显示结果
def show_inference(model, image_path):
	# the array based representation of the image will be used later in order to prepare the
	# result image with boxes and labels on it.
	image_np = np.array(Image.open(image_path))
	# 调用检测函数
	output_dict = run_inference_for_single_image(model, image_np)
	# 可视化.
	vis_util.visualize_boxes_and_labels_on_image_array(
		image_np,
		output_dict['detection_boxes'],
		output_dict['detection_classes'],
		output_dict['detection_scores'],
		category_index,
		instance_masks=output_dict.get('detection_masks_reframed', None),
		use_normalized_coordinates=True,
		line_thickness=8)
 
	display(Image.fromarray(image_np))
 
for image_path in TEST_IMAGE_PATHS:
	show_inference(detection_model, image_path)

最终的执行结果:

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值