介绍
TensorFlow https://www.tensorflow.org/ 是一款由 Google 构建的用于训练神经网络的开源机器学习软件. TensorFlow 的神经网络以有状态数据流图的形式表示. 图中的每个节点表示神经网络在多维阵列上执行的操作. 这些多维数组通常称为 "张量", 因此称为 TensorFlow.
TensorFlow 架构允许在台式机, 服务器或移动设备中的多个 CPU 或 GPU 上进行部署. 还有与 Nvidia https://developer.nvidia.com/cuda-zone 的并行计算平台 CUDA https://developer.nvidia.com/cuda-zone 集成的扩展. 这使得在 GPU 上部署的用户可以直接访问并行计算任务所需的虚拟指令集和 GPU 的其他元素.
在本教程中, 您将安装 TensorFlow 的 "仅 CPU 支持" 版本. 此安装非常适合 TensorFlow 初学者, CPU 版本不需要 Nvidia 显卡.
您可以通过多种方式安装 TensorFlow. 每种方法都有不同的用例和开发环境:
Python 和 Virtualenv: 在这种方法中, 您可以安装 TensorFlow 以及在 Python 虚拟环境中使用 TensorFlow 所需的所有软件包. 这将您的 TensorFlow 环境与同一台机器上的其他 Python 程序隔离开来.
Native pip: 在此方法中, 您在全局系统上安装 TensorFlow. 对于想要在多用户系统上为每个人提供 TensorFlow 的人, 建议使用此方法. 此安装方法不会在包含的环境中隔离 TensorFlow, 并且可能会干扰其他 Python 安装或库.
Docker:Docker 是一个容器运行时环境, 它将其内容与系统上预先存在的包完全隔离. 在此方法中, 您使用包含 TensorFlow 及其所有依赖项的 Docker 容器. 此方法非常适合将 TensorFlow 合并到已使用 Docker 的更大应用程序体系结构中. 但是, Docker 镜像的大小会非常大.
在本教程中, 您将在 Python 虚拟环境 virtualenv 中安装 TensorFlow. 这种方法隔离了 TensorFlow 安装并快速启动和运行. 完成安装后, 您将通过运行简短的 TensorFlow 程序验证安装, 然后使用 TensorFlow 进行图像识别.
准备
在开始本教程之前, 您需要以下内容:
一台 RAM 至少是 1G 的 Ubuntu 16.04 服务器
一个可以使用 sudo 命令的账户
Python 3.3 或更高版本并且已安装 virtualenv
安装了 Git
没有服务器的用户可以直接在腾讯云实验室 Ubuntu 服务器 https://cloud.tencent.com/developer/labs/lab/10081 体验 Tensorflow 的安装过程.
第 1 步 - 安装 TensorFlow
在此步骤中, 我们将创建一个虚拟环境并安装 TensorFlow.
首先, 创建一个名为 tf-demo 的项目目录:
mkdir ~/tf-demo
复制代码
导航到新创建的 tf-demo 目录:
cd ~/tf-demo
复制代码
然后创建一个名为 tensorflow-dev 的新虚拟环境. 运行以下命令以创建环境:
python3 -m venv tensorflow-dev
复制代码
这将创建一个新 tensorflow-dev 目录, 其中包含您在激活此环境时安装的所有软件包. 它还包括 pip 和一个独立版本的 Python.
现在激活您的虚拟环境:
source tensorflow-dev/bin/activate
复制代码
激活后, 您将在终端中看到与此类似的内容:
(tensorflow-dev)username@hostname:~/tf-demo $
复制代码
现在, 您可以在虚拟环境中安装 TensorFlow.
运行以下命令安装并升级到 PyPi 中 https://pypi.python.org/pypi 可用的最新版本的 TensorFlow :
pip3 install --upgrade tensorflow
复制代码
TensorFlow 将会安装:
- Collecting tensorflow
- Downloading tensorflow-1.4.0-cp36-cp36m-macosx_10_11_x86_64.whl (39.3MB)
- 100% || 39.3MB 35kB/s
- ...
- Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.5.0.post1 setuptools-38.2.3 six-1.11.0 tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.12.2 wheel-0.30.0
复制代码
如果您想随时停用虚拟环境, 则命令为: deactivate 要在以后重新激活环境, 请导航到项目目录
source tensorflow-dev/bin/activate
并运行.
现在, 您已经安装了 TensorFlow, 让我们确保 TensorFlow 安装正常.
第 2 步 - 验证安装
为了验证 TensorFlow 的安装, 我们将在 TensorFlow 中以非 root 用户身份运行一个简单的程序. 我们将使用规范初学者的例子 "Hello,world!" 作为一种验证形式. 我们将使用 Python 的交互式控制台创建此程序, 而不是创建 Python 文件.
要编写程序, 请启动 Python 解释器:
python
复制代码
您将在终端中看到以下提示
>>> 复制代码
这是 Python 解释器的提示, 它表明它已准备好开始输入一些 Python 语句.
首先, 输入此行以导入 TensorFlow 包并使其可用作本地变量 tf. 输入代码行后按 ENTER:
import tensorflow as tf
复制代码
接下来, 添加以下代码行来设置消息 "Hello,world!":
hello = tf.constant("Hello, world!")
复制代码
然后创建一个新的 TensorFlow 会话并将其分配给变量 sess:
sess = tf.Session()
复制代码
注意: 根据您的环境, 您可能会看到以下输出:
- 2017-06-18 16:22:45.956946: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
- 2017-06-18 16:22:45.957158: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
- 2017-06-18 16:22:45.957282: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
- 2017-06-18 16:22:45.957404: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
- 2017-06-18 16:22:45.957527: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
复制代码
这告诉您, 您有一个可能针对 TensorFlow 进行优化以获得更好性能的指令集. 如果你看到这个, 你可以放心地忽略它并继续.
最后, 输入这行代码打印出 hello:
print(sess.run(hello))
复制代码
您将在控制台中看到此输出:
Hello, world!
复制代码
这表明一切正常, 您可以开始使用 TensorFlow 来做一些更有趣的事情.
按下 CTRL+D 退出 Python 交互式控制台.
现在让我们使用 TensorFlow 的图像识别 API 来更熟悉 TensorFlow.
第 3 步 - 使用 TensorFlow 进行图像识别
现在已经安装了 TensorFlow 并且您通过运行一个简单的程序验证了它, 让我们来看看 TensorFlow 的图像识别功能. 为了对图像进行分类, 您需要训练模型. 然后你需要编写一些代码来使用该模型. 要了解有关这些概念的更多信息, 您可以查看腾讯云学院人工智能课程 https://cloud.tencent.com/developer/edu/courses?cid=10017 的相关内容.
腾讯云社区提供了 TensorFlow 中文开发者手册 https://cloud.tencent.com/developer/doc/1241 , 包括代码和用于分类图像的训练模型.
使用 Git 将 TensorFlow 模型库从 GitHub 克隆到项目目录中:
git clone https://github.com/tensorflow/models.git
复制代码
当 Git 将存储库检出到一个名为 models 的新文件夹时, 您将看到以下输出:
- Cloning into 'models'...
- remote: Counting objects: 8785, done.
- remote: Total 8785 (delta 0), reused 0 (delta 0), pack-reused 8785
- Receiving objects: 100% (8785/8785), 203.16 MiB | 24.16 MiB/s, done.
- Resolving deltas: 100% (4942/4942), done.
- Checking connectivity... done.
复制代码
切换到
models/tutorials/image/imagenet
目录:
cd models/tutorials/image/imagenet
复制代码
此目录包含 classify_image.py 使用 TensorFlow 识别图像的文件. 该程序从 tensorflow.org 第一次运行开始下载训练好的模型. 下载此模型需要您的磁盘上有 200MB 的可用空间.
在这个例子中, 我们将对 Panda 的预先提供的图像 https://www.tensorflow.org/images/cropped_panda.jpg 进行分类. 执行此命令以运行映像分类器程序:
python classify_image.py
复制代码
您将看到类似于此的输出:
- giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.89107)
- indri, indris, Indri indri, Indri brevicaudatus (score = 0.00779)
- lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00296)
- custard apple (score = 0.00147)
- earthstar (score = 0.00117)
复制代码
您已使用 TensorFlow 的图像识别功能对第一张图像进行了分类.
如果您想使用其他图像, 可以通过 -- image_file 在
python3 classify_image.py
命令中添加参数来完成此操作. 对于参数, 您将传入图像文件的绝对路径.
结论
来源: https://juejin.im/post/5b5195d8e51d45162679b56d