如何部署 TensorFlow 服务器

阅读 379

安装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中可用的最新版本的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安装正常。

验证安装

为了验证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。

使用TensorFlow进行图像识别

现在已经安装了TensorFlow并且您通过运行一个简单的程序验证了它,让我们来看看TensorFlow的图像识别功能。为了对图像进行分类,您需要训练模型。然后你需要编写一些代码来使用该模型。要了解有关这些概念的更多信息,您可以查看腾讯云学院人工智能课程的相关内容。

腾讯云社区提供了TensorFlow中文开发者手册,包括代码和用于分类图像的训练模型。

使用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的预先提供的图像进行分类。执行此命令以运行映像分类器程序:

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_filepython3 classify_image.py命令中添加参数来完成此操作。对于参数,您将传入图像文件的绝对路径


文章来源:网络 版权归原作者所有,如涉及知识产权问题,请权利人联系我们,我们将立即处理.
标签:
专栏:   Web服务器
文章 获得 个赞 共 个粉丝

推荐阅读 更多精彩内容