搭建深度学习开发环境

目录

一、安装anaconda

二、配置虚拟环境

三、安装vscode

四、在vscode安装pylint

五、安装YAPE

六、安装TensorFlow

pip install与conda install

测试安装

git工具安装

tensorflow object detection api 下载安装

建立文件结构并下载api​

 安装依赖的python软件包

 配置环境变量

安装COCO api

一、下载编译工具visualcppbuildtools_full.exe

二、下载工具

三、完成coco api的安装

测试coco api是否安装成功

编译proto文件

 测试安装

安装LabelImag


一、安装anaconda

各类软件安装_baidu_41553551的博客-CSDN博客

二、配置虚拟环境

添加一个新的python3.6的虚拟环境

三、安装vscode

在激活虚拟环境后 在首页找的vscode,点击安装

如果首页没有vscode,解决方法Anaconda主页中没有VScode_baidu_41553551的博客-CSDN博客

四、在vscode安装pylint

在vscode的下面TERMINAL中输入pip install pylint

用于代码分析

五、安装YAPE

 pip install yape

然后配置 

选择yapf

使用的时候,按alt+shift+f

六、安装TensorFlow

输入conda install tensorflow-gpu=1.13  (3000系列显卡这里可以conda install tensorflow-gpu==2.5

如果不指名tensorflow的版本,conda会安装当前最新版,需要注意的是开源不像商业那样,商业在发布之前已经充分测试过,但开源并没有,开源在不断迭代过程中,所依赖的软件包有的迭代快,有的迭代慢,版本之间并不一定兼容,因此最新版本不一定就是最好的

在这里通过一条指明具体版本的命令就可以安装所有,包括其依赖库并且不用操心依赖库之间的版本控制问题,十分方便,但是如果不安装Anaconda,直接在python中安装,那么还需要手动安装其他的依赖库,还要确认每个依赖库版本和TensorFlow版本的对应关系,十分繁琐

pip install与conda install

pip是python官方推荐的,它只专注于python软件包之间的依赖关系,不考虑python软件包和非python软件包的依赖关系

而conda在安装时,亏同时解决tensorflow所以来的python软件包和非python软件包的关系

测试安装

python 
import tensorflow as tf
tf.enable_eager_execution()
hello=tf.constant('hello,tensorflow')
print(hello)

发现问题          

 问题解决Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA_baidu_41553551的博客-CSDN博客

python 
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
tf.enable_eager_execution()
hello=tf.constant("hello,tensorflow")
print(hello)

 运行成功图

git工具安装

git官网 https://git-scm.com下载win的版本,可以最新版,点击安装,什么都不用动,一直点next就可以

安装完成在空白处点击右键,如果右键菜单中找到git bash here 并启动git bash则说明安装成功

tensorflow object detection api 下载安装

建立文件结构并下载api

 

 在这里面也就是tf_train里面空白处点击右键,单击git bash here

输入

git clone -b r1.13.0 https://github.com/tensorflow/models.git

tensorflow是2.5的则

git clone -b r2.5.0 https://github.com/tensorflow/models.git

 

 下载完成后的文件夹结构

 安装依赖的python软件包

在anaconda的虚拟环境中输入命令

pip install matplotlib pillow lxml contextlib2 cython opencv-python

 

 配置环境变量

为了让python能够找到api依赖的软件模块,需要给python的模块搜索路径变量PYTHONPATH添加三个路径

安装COCO api

一、下载编译工具visualcppbuildtools_full.exe

下载链接为https://go.microsoft.com/fwlink/?LinkId=691126

然后直接安装 如果失败了 看https://blog.csdn.net/baidu_41553551/article/details/120380025

二、下载工具

在addons文件夹里面的空白处单击右键选择git bash here

输入命令

git clone https://github.com/philferriere/cocoapi.git

三、完成coco api的安装

打开prompt

 切换环境

conda activate tf_gpu

 

 进入d盘 输入d:

在进入工作目录

cd D:\program\tf_train\addons\cocoapi\PythonAPI

 输入安装命令

python setup.py install

测试coco api是否安装成功

输入python 启动python环境

输入语句import pycocotools

则表示安装成功 

编译proto文件

api框架使用protobuf工具配置模型和训练参数

由于在conda install tensorflow已经安装过了,所以不需要再次安装

但在运行框架前需要编译proto文件

进入tf_train\models\research文件夹,在文件夹地址栏中输入cmd,启动命令行,然后输入

conda activate tf_gpu

如果没有对conda加入环境变量,那么正常的启动cmd,然后调到上面这个文件夹内

 输入命令,然后回车完成对proto文件的编译

for /f %i in ('dir /b object_detection\protos\*.proto') do protoc object_detection\protos\%i  --python_out=.

 测试安装

下载模型

https://github.com/librahfacebook/Detection/blob/master/object_detection/g3doc/detection_model_zoo.md

如果链接失效,在谷歌中搜索detection_model_zoo.md,找github里的tensorflow的那个链接 

点击下载模型

如果下载不了,使用下面的链接

链接:https://pan.baidu.com/s/1u4J0RQH-0pw3Rb2EOjACyA 
提取码:s1lc 

下载后的文件解压,然后将解压后的文件复制到

修改

注释掉(如果后面图片不显示,就是这里没有注释掉)

使用cmd激活tf_gpu环境,然后进入到object_detection文件夹,最后输入命令

python object_detection_example_1.py

 这个py文件自己新建,复制到object_detection文件夹

代码内容:

# 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__))

# -----------------------------------------------------------
# 第二步,导入模型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()

然后报错

原因是 Numpy版本过高

pip install numpy==1.16.4

安装LabelImag

labelImage是一个用python写的开源免费的图像标注工具,其标注结果会以PASCALVOC格式存成XML文件。LabelImg也支持YOLO格式

下载地址 http://tzutalin.github.io/labelImg

 解压到addons里面

双击labelImg.exe 运行,单击open dir按钮,如果能成功打开存放图片的文件夹并能加载图片成功,则说明已经安装成功。

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贪睡的蜗牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值