1、搭建深度学习图像识别开发环境

1、前言

1.1 本文章约定

  • 操作 :表示你需要跟着说明进行相应的操作。
  • 输入命令 xxxxxx:表示你需要在控制台键入命令。

1.2 开始条件

为了方便大家,我已把相关软件都打包至百度云盘了,就大家就不用挨个去下载了,网盘下载地址:
链接:https://pan.baidu.com/s/12coly8nikksg2jSzizxCbA
提取码:iubd
在这里插入图片描述

1.3 视频教程

视频教程请看:https://www.bilibili.com/video/BV1k5411W7cS/

1.4 整个流程概览

在这里插入图片描述

1.5 所需硬件

Nvidia 显卡,并且计算能力 >= 3.5,GeForce GTX 1050/1060/1070 等都是性价比都是非常高的 GPU。显卡计算能力参考地址:https://developer.nvidia.com/cuda-gpus#compute
在这里插入图片描述

1.6 软件版本概览

软件版本
AnacondaAnaconda3-5.2.0-Windows-x86_64.exe
Python3.6(Anaconda 自带)
tensorflow-gpu1.13
PyCharm2019.1.2
Git最新版
TensorFlow Object Detection API1.13
COCO API最新版
LabelImg最新版

对于开源软件,下载最新版不一定是最好的选择。开源软件在不断迭代过程中,所依赖的软件包有的迭代快、有的迭代慢,版本之间还不一定兼容。就我们学习来说,上面的软件版本已经完全够用,并且经过开源社区的充分测试,问题较少。

2、安装 Anaconda

下载地址:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/,下载的版本为:Anaconda3-5.2.0-Windows-x86_64.exe,该 Anaconda 对应 Python3.6。
操作 下载完成后,双击进行安装,一切默认设置。
在这里插入图片描述

2.1 创建环境 tf_gpu

操作 1、Anaconda 安装完成后,打开 Anaconda Navigator -> Environments -> Create
在这里插入图片描述
操作 2、创建一个名为 tf_gpu 的环境,选择 Python 版本为 3.6,点击 Create,然后耐心等待 Anaconda 创建新环境。
在这里插入图片描述


操作 3、左键单击我们刚刚创建的环境 tf_gpu 的启动按钮,选择 Open Terminal
在这里插入图片描述

操作 4、打开控制台后,你就能看到你已经进入叫 tf_gpu 的环境了,接下来的操作都在这个环境里操作。
在这里插入图片描述

2.2 配置 Anaconda 镜像源

输入命令 因为 Anaconda 官方的下载服务器在国外,我们下载十分缓慢,所以我们设置清华的镜像源为默认下载源,可以大大提高下载效率。
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

一行一行的复制过去,Ctrl+ V 粘贴在控制台,然后回车运行。

在这里插入图片描述

2.3 配置虚拟环境 tf_gpu

输入命令 镜像源配置完成后,就可以开始安装 tensorflow-gpu 这个库了。直接把下面的安装命令复制到控制台,回车运行,然后根据提示输入 y
conda install tensorflow-gpu=1.13

在这里插入图片描述

可能运行安装命令一段时间后控制台没有任何反应,这个时候按一下回车,才会出现安装进度。

3、安装 PyCharm(非必要选项)

PyCharm 是一个用来开发 Python 程序的工具。
可以参考:https://blog.csdn.net/lemon4869/article/details/106834450

4、安装Git

Git 是一个版本控制软件,可以让我们很方便地克隆别人的开源项目。这个安装很简单,百度下载一个,然后全部默认安装。

5、安装 TensorFlow Object Decetion API 框架

TensorFlow Object Decetion API 是一个在 TensorFlow 基础上开发出来的用于图像检测并定位的软件框架。

5.1 下载 TensorFlow Object Decetion API

在这里我们就需要上一步刚刚安装的 Git。

操作 1、首先新建一个文件夹 tf_train,再在里面创建两个文件夹:addons 和 workspaces,然后在空白处右键,选择 Git Bash Here。

在这里插入图片描述

输入命令 2、在 Git Bash 中输入 :git clone -b r1.13.0 https://github.com.cnpmjs.org/tensorflow/models.git ,回车后等待下载完成。

5.2 安装依赖的 Python 软件包

输入命令1、打开 tf_gpu 的 terminal(参考 1.1 节),先设置 pip 镜像源,使用国内清华镜像源,速度可以提高很多。 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

输入命令 2、安装相关包,pip install matplot lib pillow lxml contextlib2 cython opencv-python

5.3 配置环境变量

给系统添加一个 PYTHONPATH 环境变量,这个变量的作用是告诉 Python 解释器可以到哪里寻找我们依赖的包。

操作1、进入新建系统环境变量界面
在这里插入图片描述
操作2、新建变量名为:PYTHONPATH,变量值为之前下载的 TensorFlow Object Decetion API 的路径。比如我的为:
E:\tf_gpu\models\research
E:\tf_gpu\models\research\slim
E:\tf_gpu\models\research\object_detection
然后将这三个路径粘贴在变量值那里,记得用英文逗号隔开。
在这里插入图片描述

6、安装 COCO API

输入命令1、下载 COCO API

我们将使用 COCO API 来作为 TensorFlow Object Detection API 的评估指标,用来评估我们模型的表现能力。

进入之前创建的 addons 文件夹,打开 Git Bash,输入下面命令:
git clone https://github.com.cnpmjs.org/philferriere/cocoapi.git

在这里插入图片描述

(本图有错误:目录 `18-test` 应为 `tf_gpu`,博主忘记改了)

输入命令 2、安装 COCO API 安装 COCO API 要依赖 Visual C++ Build Tools 2015,所以我们先安装 Visual C++ Build Tools 2015。如果没有的话直接进行下一步就会出现 unable to find vcvarsall.bat 错误。下载地址:http://go.microsoft.com/fwlink/?LinkId=691126。

Visual C++ Build Tools 2015 安装完成后,打开 tf_gpu 的 terminal(参考 1.1 节),进入 cocoapi\PythonAPI 目录下:cd E:\tf_gpu\addons\cocoapi\PythonAPI (根据自己实际情况修改),
然后运行安装命令:python setup.py install

7、编译 proto 文件

TensorFlow Object Detection API 框架使用 protobuf 工具来配置模型和训练参数,我们在安装 tensorflow-gpu 的时候已经附带安装好了。但是没有经过编译,该工具无法使用,所以接下来的操作就是对 proto 文件进行编译。

输入命令 打开 tf_gpu 的 terminal(参考 1.1 节),输入命令:
cd E:\1-tf_train\models\research(进入 research 目录下进行操作)
for /f %i in ('dir /b object_detection\protos\*.proto') do protoc object_detection\protos\%i --python_out=.(直接复制过去运行)

8、测试安装

8.1 下载 ssd_inception_v2_coco 模型

操作 从 TensorFlow 预训练模型库下载 ssd_inception_v2_coco 模型,地址:
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf1_detection_zoo.md
在这里插入图片描述
操作解压出来后放到 E:\1-tf_train\models\research\object_detection
在这里插入图片描述

8.2 创建 object_detection_example_1.py 文件

操作 在 E:\1-tf_train\models\research\object_detection 目录下创建一个 object_detection_example_1.py 文件
在这里插入图片描述
操作 然后用记事本打开,复制以下代码进去:

# object_detection_example_1.py演示一个完整的推理(Inference)过程

# -----------------------------------------------------------
# 第一步,导入相关的软件包
# -----------------------------------------------------------
import numpy as np 
import os
import tensorflow as tf 
import matplotlib.pyplot as plt 
from PIL import Image 
from utils import label_map_util
from utils import visualization_utils as vis_util 
from utils import ops as utils_ops

# 检查tensorflow 版本,须≥1.12.0
from pkg_resources import parse_version
if parse_version(tf.__version__) < parse_version('1.12.0'):
    raise ImportError("parse_version:Please upgrade your TensorFlow to V1.12.* or higher")
print("The version of installed TensorFlow is {0:s}".format(tf.__version__))

# GPU 按需分配
config = tf.compat.v1.ConfigProto(allow_soft_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.3
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

# -----------------------------------------------------------
# 第二步,导入模型ssd_inception_v2_coco_2018_01_28到内存
# ssd_inception_v2_coco_2018_01_28文件夹应与本程序放在models\research\object_detection文件夹下
# -----------------------------------------------------------
MODEL_NAME = 'ssd_inception_v2_coco_2018_01_28'
PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')

# -----------------------------------------------------------
# 第三步,导入标签映射文件(Label map),这样假如神经网络输出'5',我
# 们就知道对应的是'airplane'
# -----------------------------------------------------------
category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)

# -----------------------------------------------------------
# 第四步,执行推理(Inference),检测图片中的对象
# -----------------------------------------------------------

# ## 导入图像数据到numpy array 子程序
def load_image_into_numpy_array(image):
    (im_width, im_height) = image.size 
    return np.array(image.getdata()).reshape((im_height,im_width,3)).astype(np.uint8)

# ## 从单张图片中检测对象子程序
# ## 图片名称:image1.jpg, image2.jpg,存放在
# ## models\research\object_detection\test_images文件夹下
PATH_TO_IMAGES_DIR = 'test_images'
TEST_IMAGE_PATHS = [os.path.join(PATH_TO_IMAGES_DIR, 'image{0:d}.jpg'.format(i)) for i in range(1,3)]

# 显示图像的尺寸,单位inches
IMAGE_SIZE = (12, 8)

def run_inference_for_single_image(image, graph):
    with graph.as_default():
        with tf.Session() as sess:
            ops = tf.get_default_graph().get_operations()
            all_tensor_names = {output.name for op in ops for output in op.outputs}
            tensor_dict = {}
            for key in ['num_detections', 'detection_boxes', 'detection_scores',
                'detection_classes', 'detection_masks']:
                tensor_name = key + ':0'
                if tensor_name in all_tensor_names:
                    tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(tensor_name)
            if 'detection_masks' in tensor_dict:
                detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
                detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
                real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
                detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
                detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
                detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
                    detection_masks, detection_boxes, image.shape[1], image.shape[2])
                detection_masks_reframed = tf.cast(tf.greater(detection_masks_reframed, 0.5), tf.uint8)
                tensor_dict['detection_masks'] = tf.expand_dims(detection_masks_reframed, 0)
            image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')

            # 运行推理(Inference)
            output_dict = sess.run(tensor_dict,feed_dict={image_tensor: image})

            output_dict['num_detections'] = int(output_dict['num_detections'][0])
            output_dict['detection_classes'] = output_dict['detection_classes'][0].astype(np.int64)
            output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
            output_dict['detection_scores'] = output_dict['detection_scores'][0]
            if 'detection_masks' in output_dict:
                output_dict['detection_masks'] = output_dict['detection_masks'][0]
    return output_dict

for image_path in TEST_IMAGE_PATHS:
    image = Image.open(image_path)
    image_np = load_image_into_numpy_array(image)
    # 扩展维度,因为模型要求图像的形状为:[1, None, None, 3]
    image_np_expanded = np.expand_dims(image_np, axis=0)
    # 运行检测程序.
    output_dict = run_inference_for_single_image(image_np_expanded, detection_graph)
    # 可视化检测结果.
    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'),
        use_normalized_coordinates=True,
        line_thickness=8)
    plt.figure(figsize=IMAGE_SIZE)
    plt.imshow(image_np)
plt.show()

8.3 注释 visualization_utils.py 第 29 行

操作 打开 E:\1-tf_train\models\research\object_detection\utils\visualization_utils.py,注释掉第 29 行: import matplotlib; matplotlib.use(‘Agg’) # pylint: disable=multiple-statements
在这里插入图片描述

8.4 运行 object_detection_example_1.py

输入命令 打开 tf_gpu 的 terminal(参考 1.1 节),
cd E:\1-tf_train\models\research\object_detection(进入 object_dection 目录)
python object_detection_example_1.py(运行程序)
运行成功会得到如下结果:
在这里插入图片描述

9、安装 LabelImg

LabelImg 是一个图像标注工具,其标注结果会以 PASCAL VOC 格式存成 XML 文件。
操作第一步:下载,http://tzutalin.github.io/labelImg/
在这里插入图片描述

操作第二步:解压。该软件是一个绿色软件,解压后即可使用,但是必须解压到 addons 目录下才可使用,否则无法运行。
在这里插入图片描述

(本图有错误:目录 `18-test` 应为 `tf_gpu`,博主忘记改了)

操作第三步:运行。进入 windows_v1.8.0 目录,然后运行程序。
在这里插入图片描述

  • 20
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值