如何安装tensorflow_Tensorflow 1 ->Tensorflow 2 官方模块梳理(1)|模块安装

序言:

Tensorflow1的版本问题:

重要提示:本教程面向TensorFlow1.14,它(在编写本教程时)是TensorFlow2.x之前的最新稳定版本。 Tensorflow 1.15也已发布,但似乎显示出不稳定性问题。

TensorFlow 1.9的版本可以在这里找到。

TensorFlow Object Detection API tutorial​tensorflow-object-detection-api-tutorial.readthedocs.io

在纠正本教程时,对象检测模型培训和评估未迁移到TensorFlow 2.x。从个人测试来看,使用预先训练的模型进行检测似乎是可行的,但是还不可能训练和评估模型。迁移完成后,将生成TensorFlow 2.x的版本。

tensoflow1和tensorflow2的不兼容问题实时讨论链接:

https://github.com/tensorflow/models/issues/6423#issuecomment-600925072​github.com

这是一个循序渐进的教程/指南,介绍如何设置和使用TensorFlow的对象检测API来图像/视频中的对象检测。

为了确保我们没有包冲突和/或我们可以安装几个不同版本/变体的TensorFlow(例如CPU和GPU),通常建议使用某种虚拟环境。在本教程中,我们将使用Anaconda创建和管理我们的虚拟环境, 虽然安装和使用TensorFlow并不需要使用Anaconda,但我建议您这样做,因为它是管理包和设置新虚拟环境的直观方法。

我们将在本教程中使用的软件工具如下表所示:

80ee020458896188fda0c37654b47238.png

Tensorflow的安装

一、Anaconda虚拟环境管理器

  • Windows
  1. Go to Individual Edition | Anaconda
  2. Download Anaconda Python 3.7 version for Windows
  3. Run the downloaded executable (.exe) file to begin the installation. See here for more details.
  4. (Optional) In the next step, check the box “Add Anaconda to my PATH environment variable”. This will make Anaconda your default Python distribution, which should ensure that you have the same default Python distribution across all editors.
  • Linux(重点介绍)
  1. • Go to Individual Edition | Anaconda
  2. • Download Anaconda Python 3.7 version for Linux
  3. • Run the downloaded bash script (.sh) file to begin the installation. See here for more details.
  4. • When prompted with the question “Do you wish the installer to prepend the Anaconda<2 or 3> install location to PA TH in your /home/<user>/.bashrc ?”, answer “Y es”. If you enter “No”, you must manually add the path to Anaconda or conda will not work.

1)下载地址:

Individual Edition | Anaconda​www.anaconda.com
3721f6f0b11b1b21f1c4614e62765797.png

67e93565e0c59db8e224702b81f892f5.png
Download Anaconda Python 3.7 version for Linux

2)安装方法与验证成功方法:

Installing on Linux​docs.anaconda.com

二、tensorflow的安装

如备注部分所述,TensorFlow有两种通用变体,它们使用计算机上的不同硬件来运行计算量很大的机器学习算法。

  1. 最简单的安装,但在大多数情况下也是最慢的性能,是TensorFlow CPU,它直接运行在您的计算机的CPU上。
  2. 如果你的计算机拥有英伟达的显卡。你可以利用cuda 核心的优势来加速运算进程,TensorFlow GPU

1) TensorFlow CPU安装

  1. 创建新的Conda虚拟环境(可选): 创建一个名为tensorflow_cpu的新虚拟环境
conda create -n tensorflow_cpu pip python=3.7

激活: 现在,让我们通过在终端窗口中运行以下命令来激活新创建的虚拟环境:

activate tensorflow_cpu

激活虚拟环境后,环境的名称应显示在cmd路径说明符开头的括号内,例如:

(tensorflow_cpu) C:Userssglvladi>

2. 为Python安装TensorFlow CPU

打开一个新的终端窗口并激活tensorflow_cpu环境

pip install --ignore-installed --upgrade tensorflow==1.14

3. 测试您的安装

通过运行以下命令启动新的Python解释器会话:

python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

正常运行结果:

2019-02-28 11:59:25.810663: I
˓ →T:srcgithubtensorflowtensorflowcoreplatformcpu_feature_guard.
˓ →cc:141] Your CPU supports instructions that this TensorFlow binary was
˓ →not compiled to use: AVX2

2)tensorflow GPU安装

TesnorFlow GPU的安装比TensorFlow CPU稍微复杂一些,主要是因为需要安装相关的图形和CUDE驱动程序。有一个很好的Youtube教程下面,解释如何安装TensorFlow GPU。

b48304e410e130b1495453108f689d6a.png
TesnorFlow GPU安装视频https://www.zhihu.com/video/1243235889364914176

安装之前, 您需要确保您的系统能够满足以下要求:

64b309d45d8d57fe2fe0598607319df7.png
  1. 安装 CUDA Toolkit


Windows: Follow this link to download and install CUDA Toolkit 10.0.

CUDA Toolkit 10.0 Archive​developer.nvidia.com
520ad13548cc8d8be7813825db84ceea.png

7686ddb9b3e221638c5783755a9a0a36.png

Linux: Follow this link to download and install CUDA Toolkit 10.0 for your Linux distribution.

3fdeb9cf7875f0e1162770664b9b30d3.png

2. 安装 CUDNN

Windows

• Go to https://developer.nvidia.com/rdp/cudnn-download

• 根据需要创建用户配置文件并登录

• Select cuDNN v7.6.5 (Nov 5, 2019), for CUDA 10.0

• Download cuDNN v7.6.5 Library for Windows 10

• Extract the contents of the zip file (i.e. the folder named cuda) inside <INSTALL_PATH>NVIDIA

GPU Computing ToolkitCUDAv10.0, where <INSTALL_PATH> points to the installation directory specified during the installation of the CUDA Toolkit. By default <INSTALL_PATH> = C:ProgramFiles.

Linux

• Go to https://developer.nvidia.com/rdp/cudnn-download

• 根据需要创建用户配置文件并登录

• Select cuDNN v7.6.5 (Nov 5, 2019), for CUDA 10.0

• Download cuDNN v7.6.5 Library for Linux

• Follow the instructions under Section 2.3.1 of the CuDNN Installation Guide to install CuDNN.

NVIDIA Deep Learning SDK Documentation​docs.nvidia.com

3. 环境设置

As per Section 7.1.1 of the CUDA Installation Guide for Linux, append the following lines to ~/.bashrc:

# CUDA related exports
export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_
˓ →PATH}}

4. 更新GPU驱动程序.

如果在CUDA工具包的安装过程中(请参阅Install CUDA Toolkit)选择了Express installation 选项,那么GPU驱动程序将被CUDA工具包附带的驱动程序覆盖。这些驱动程序通常不是最新的驱动程序,因此,您可能希望升级您的驱动程序:

  • • Go to http://www.nvidia.com/Download/index.aspx
  • • Select your GPU version to download
  • • Install the driver for your chosen OS

5. 创建新的Conda虚拟环境

conda create -n tensorflow_gpu pip python=3.7

以上将创建一个名为tensorflow_gpu的新虚拟环境,激活:

activate tensorflow_gpu

激活虚拟环境后,环境的名称应显示在cmd路径说明符开头的括号内:

(tensorflow_gpu) C:Userssglvladi>

6. 为Python安装TensorFlow GPU

pip install --upgrade tensorflow-gpu==1.14

7. 测试您的安装

python
import tensorflow as tf
 hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
2019-11-25 07:20:32.415386: I tensorflow/stream_executor/platform/default/
˓ →dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2019-11-25 07:20:32.449116: I tensorflow/core/common_runtime/gpu/gpu_
˓ →device.cc:1618] Found device 0 with properties:
name: GeForce GTX 1070 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.683
pciBusID: 0000:01:00.0
b'Hello, TensorFlow!'

三、 TensorFlow模型安装

现在您已经安装了TensorFlow,是时候安装TensorFlow使用的模型来实现它的魔力了。

1) 安装必备组件

假设您刚刚创建了新的虚拟环境(无论是tensorflow_cpu、tensorflow_gpu还是您可能使用过的其他名称),在安装模型之前,需要安装一些包。

2c8a769b1a975c40d86d349ba8bb77d0.png

通过运行以下命令,可以使用conda安装包:

conda install <package_name>(=<version>), <package_name>(=<version>), ..., <package_name>(=<version>)

要简单地安装最新版本的所有软件包,您可以运行:

conda install pillow, lxml, jupyter, matplotlib, opencv, cython

或者,如果不想使用Anaconda,可以使用pip安装软件包:

pip install <package_name>(==<version>) <package_name>(==<version>) ... <package_name>(==<version>)

但是您需要安装opencv python而不是opencv。

2) 下载TensorFlow模型

为了保证与安装的tensorflow(v1.14.0)环境的兼容性,我们最好选择发行版:Tensorflow Models releases,版本v1.13.0是v2.0之前的最后一个非官方版本,因此在这里使用。

15c36cc0987478a138a5f66b71a43a4e.png
  1. 在您选择的路径下创建一个新文件夹,并将其命名为TensorFlow。

(e.g.C:UserssglvladiDocumentsTensorFlow).

2. cd TensorFlow

3. 可以使用下面两种方法: TensorFlow Models v.1.13.0 release

  • 直接下载ZIP
  • git clong https://github.com/tensorflow/models/tree/r1.13.0

为保持文件的一致的, 在里面这个后者的案例你将有到重命 这个提取文件夹 models-r1.13.0改为models, 您现在应该在TensorFlow文件夹下有一个名为models的文件夹,其中包含另外4个文件夹:

48a4e545dd925b2503db4bc99235fcd8.png

3)Protobuf安装/编译

Tensorflow对象检测API使用Protobufs配置模型和训练参数。在使用框架之前,必须下载并编译Protobuf库。应按如下方式进行:

  1. 在https://github.com/protocolbuffers/protobuf/releases上下载最新的protoc-*-*.zip发行版,当前是:protobuf-all-3.12.0-rc-1.tar.gz for linux
  2. 解压下载的protobuf-all-3.12.0-rc-1.tar.gz放到你选择的文件夹中 <PATH_TO_PB>
  3. 把<PATH_TO_PB>加到路径环境变量当中
  4. cd TensorFlow/models/research/
# From within TensorFlow/models/research/
protoc object_detection/protos/*.proto --python_out=.

4) 添加必要的环境变量
1. 安装Tensorflowmodelsresearchobject_detection路径下的包:

# From within TensorFlow/models/research/
pip install

2. Add research/slim to your PYTHONPATH:

# From within tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:<PATH_TO_TF>/TensorFlow/models/research/slim
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md​github.com

5) COCO API 安装(可选)

如果您对使用COCO评估指标感兴趣,应该安装pycocotools包,如评估模型(可选)中所述。

  • 下载https://github.com/cocodataset/cocoapi
  • make
  • 将pycocotools子文件夹复制到Tensorflow/models/research
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools <PATH_TO_TF>/TensorFlow/models/research/

默认指标基于Pascal VOC评估中使用的指标。

  • 要使用COCO对象检测度量,请将metrics_set:“COCO_detection_metrics”添加到配置文件中的eval_config消息。
  • 要使用COCO实例分段度量,请将metrics_set:“COCO_mask_metrics”添加到配置文件中的eval_config消息中。

6) 测试您的安装

  1. 打开一个新的终端窗口并激活tensorflow_gpu环境(如果您还没有这样做的话)
  2. cd TensorFlowmodelsresearchobject_detection
# From within TensorFlow/models/research/object_detection
jupyter notebook

这将在您的计算机上启动一个新的jupyter笔记本服务器,并将您重定向到默认浏览器的新选项卡。 一旦到了那里,只需遵循森德克斯的Youtube视频,以确保一切顺利运行。

0b12446e18de4c1ad20f4526ff51fca0.png
Test your Installationhttps://www.zhihu.com/video/1243259579854708736
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值