澳门威尼斯人网址上学笔记TF02二

#include <grpc++/grpc++.h>

读书笔记TF02二:产品遭遇模型计划、Docker镜像、Bazel职业区、导出模型、服务器、客户端,tf02二docker

出品意况模型陈设,创立简单Web
应用程式,用户上传图像,运营英斯ption模型,完毕图像自动分拣。

搭建TensorFlow服务付出境遇。安装Docker,https://docs.docker.com/engine/installation/
。用配备文件在本土创造Docker镜像,docker build –pull -t
$USE福特Explorer/tensorflow-serving-devel
https://raw.githubusercontent.com/tensorflow/serving/master/tensorflow\_serving/tools/docker/Dockerfile.devel
。镜像运维容器,docker run -v $HOME:/mnt/home -p 999九:999玖 -it
$USEKoleos/tensorflow-serving-devel
,在home目录加载到容器/mnt/home路线,在顶峰专门的职业。用IDE或编辑器编辑代码,用容器运行构建筑工程具,主机通过999玖端口访问,营造服务器。exit命令退出容器终端,甘休运维。

TensorFlow服务程序C++写,使用谷歌(Google)的Bazel构建工具。容器运转Bazel。Bazel代码级处理第一方重视项。Bazel自动下载构建。项目库根目录定义WO福特ExplorerKSPACE文件。TensorFlow模型库包涵英斯ption模型代码。

TensorFlow服务在类型作为Git子模块。mkdir ~/serving_example,cd
~/serving_example,git init,git submodule add
https://github.com/tensorflow/serving.git ,tf_serving,git submodule
update –init –recursive 。

WORKSPACE文件local_repository规则定义第三方信赖为地面存款和储蓄文件。项目导入tf_workspace规则早先化TensorFlow注重项。

workspace(name = “serving”)

local_repository(
name = “tf_serving”,
path = __workspace_dir__ + “/tf_serving”,
)

local_repository(
name = “org_tensorflow”,
path = __workspace_dir__ + “/tf_serving/tensorflow”,
)

load(‘//tf_serving/tensorflow/tensorflow:workspace.bzl’,
‘tf_workspace’)
tf_workspace(“tf_serving/tensorflow/”, “@org_tensorflow”)

bind(
name = “libssl”,
actual = “@boringssl_git//:ssl”,
)

bind(
name = “zlib”,
actual = “@zlib_archive//:zlib”,
)

local_repository(
name = “inception_model”,
path = __workspace_dir__ + “/tf_serving/tf_models/inception”,
)

导出磨练好的模型,导出数据流图及变量,给产品用。模型数据流图,必须从占位符接收输入,单步估算总结输出。英斯ption模型(或相似图像识别模型),JPEG编码图像字符串输入,与从TFRecord文件读取输入分裂。定义输入占位符,调用函数转换占位符表示外部输入为本来预计模型输入格式,图像字符串转换为各分量位于[0,
1]内像素张量,缩放图像尺寸,符合模型期望宽度中度,像素值调换来模型供给区间[-1,
1]内。调用原始模型测算方法,依靠调换输入猜测结果。

想见方法各参数赋值。从检查点恢复生机参数值。周期性保存模型练习检查点文件,文件包蕴学习参数。最终叁回保存操练检查点文件包蕴最终更新模型参数。下去载预磨练检查点文件:http://download.tensorflow.org/models/imagenet/inception-v3-2016-03-01.tar.gz
。在Docker容器中,cd /tmp, curl -0
http://download.tensorflow.org/models/image/imagenet/inception-v3-2016-03-01.tar.gz,
tar -xzf inception-v3-2016-03-01.tar.gz 。

tensorflow_serving.session_bundle.exporter.Exporter类导出模型。传入保存器实例创造实例,用exporter.classification_signature创造模型签字。钦赐input_tensor、输出张量。classes_tensor
包括输出类名称列表、模型分配各体系分值(或可能率)socres_tensor。种类数多模型,配置钦赐仅返田大口tf.nntop_k选取种类,模型分配分数降序排列前K个种类。调用exporter.Exporter.init方法签字,export方法导出模型,接收输出路线、模型版本号、会话对象。Exporter类自动生成代码存在依附,Doker容器内部采纳中bazel运转导出器。代码保存到bazel职业区exporter.py。

import time
import sys

import tensorflow as tf
from tensorflow_serving.session_bundle import exporter
from inception import inception_model

NUM_CLASSES_TO_RETURN = 10

def convert_external_inputs(external_x):
image =
tf.image.convert_image_dtype(tf.image.decode_jpeg(external_x,
channels=3), tf.float32)
images = tf.image.resize_bilinear(tf.expand_dims(image, 0), [299,
299])
images = tf.mul(tf.sub(images, 0.5), 2)
return images

def inference(images):
logits, _ = inception_model.inference(images, 1001)
return logits

external_x = tf.placeholder(tf.string)
x = convert_external_inputs(external_x)
y = inference(x)

saver = tf.train.Saver()

with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(sys.argv[1])
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, sys.argv[1] + “/” +
ckpt.model_checkpoint_path)
else:
print(“Checkpoint file not found”)
raise SystemExit

scores, class_ids = tf.nn.top_k(y, NUM_CLASSES_TO_RETURN)

classes =
tf.contrib.lookup.index_to_string(tf.to_int64(class_ids),
mapping=tf.constant([str(i) for i in range(1001)]))

model_exporter = exporter.Exporter(saver)
signature = exporter.classification_signature(
input_tensor=external_x, classes_tensor=classes,
scores_tensor=scores)
model_exporter.init(default_graph_signature=signature,
init_op=tf.initialize_all_tables())
model_exporter.export(sys.argv[1] + “/export”,
tf.constant(time.time()), sess)

一个塑造规则BUILD文件。在容器命令运营导出器,cd
/mnt/home/serving_example, hazel run:export /tmp/inception-v3,依靠/tmp/inception-v三涉及的检查点文件在/tmp/inception-v3/{currenttimestamp}/创造导出器。第二回运维要对TensorFlow编写翻译。load从外表导入protobuf库,导入cc_proto_library规则定义,为proto文件定义创设规则。通过命令bazel
run :server 9999
/tmp/inception-v3/export/{timestamp},容器运转推测服务器。

py_binary(
name = “export”,
srcs = [
“export.py”,
],
deps = [
“@tf_serving//tensorflow_serving/session_bundle:exporter”,
“@org_tensorflow//tensorflow:tensorflow_py”,
“@inception_model//inception”,
],
)

load(“@protobuf//:protobuf.bzl”, “cc_proto_library”)

cc_proto_library(
name=”classification_service_proto”,
srcs=[“classification_service.proto”],
cc_libs = [“@protobuf//:protobuf”],
protoc=”@protobuf//:protoc”,
default_runtime=”@protobuf//:protobuf”,
use_grpc_plugin=1
)

cc_binary(
name = “server”,
srcs = [
“server.cc”,
],
deps = [
“:classification_service_proto”,
“@tf_serving//tensorflow_serving/servables/tensorflow:session_bundle_factory”,
“@grpc//:grpc++”,
],
)

概念服务器接口。TensorFlow服务使用gRPC协议(基于HTTP/二2进制协议)。接济成立服务器和自动生成客户端存根种种语言。在protocol
buffer定义服务契约,用于gRPC
IDL(接口定义语言)和二进制编码。接收JPEG编码待分类图像字符串输入,重临分数排列推测种类列表。定义在classification_service.proto文件。接收图像、音频片段、文字服务可用可1接口。proto编写翻译器调换proto文件为客户端和劳务器类定义。bazel
build:classification_service_proto可行创设,通过bazel-genfiles/classification_service.grpc.pb.h检查结果。预计逻辑,Classification瑟维斯::Service接口必须贯彻。检查bazel-genfiles/classification_service.pb.h查看request、response音讯定义。proto定义形成每种类型C++接口。

syntax = “proto3”;

message ClassificationRequest {
// bytes input = 1;
float petalWidth = 1;
float petalHeight = 2;
float sepalWidth = 3;
float sepalHeight = 4;
};

message ClassificationResponse {
repeated ClassificationClass classes = 1;
};

message ClassificationClass {
string name = 1;
float score = 2;
}

service ClassificationService {
rpc classify(ClassificationRequest) returns (ClassificationResponse);
}

贯彻预计服务器。加载导出模型,调用推测方法,完毕ClassificationService::Service。导出模型,创造SessionBundle对象,包括完全加载数据流图TF会话对象,定义导出工具分类具名元数据。SessionBundleFactory类成立SessionBundle对象,配置为pathToExportFiles钦点路径加载导出模型,重返创立SessionBundle实例unique指针。定义Classification瑟维斯Impl,接收SessionBundle实例参数。

加载分类签名,GetClassificationSignature函数加载模型导出元数据ClassificationSignature,签名钦定所接收图像真实名称的输入张量逻辑名称,以及数据流图输出张量逻辑名称映射估算结果。将protobuf输入调换为猜想输入张量,request参数复制JPEG编码图像字符串到算计张量。运转估计,sessionbundle获得TF会话对象,运维三回,传入输入输出张量臆想。推断输出张量转变protobuf输出,输出张量结果复制到ClassificationResponse音信钦赐形状response输出参数格式化。设置gRPC服务器,SessionBundle对象配置,创造ClassificationServiceImpl实例样板代码。

#include <iostream>
#include <memory>
#include <string>

#include <grpc++/grpc++.h>

#include “classification_service.grpc.pb.h”

#include
“tensorflow_serving/servables/tensorflow/session_bundle_factory.h”

using namespace std;
using namespace tensorflow::serving;
using namespace grpc;

unique_ptr<SessionBundle> createSessionBundle(const string&
pathToExportFiles) {
SessionBundleConfig session_bundle_config = SessionBundleConfig();
unique_ptr<SessionBundleFactory> bundle_factory;
SessionBundleFactory::Create(session_bundle_config, &bundle_factory);

unique_ptr<SessionBundle> sessionBundle;
bundle_factory->CreateSessionBundle(pathToExportFiles,
&sessionBundle);

return sessionBundle;
}

class ClassificationServiceImpl final : public
ClassificationService::Service {

private:
unique_ptr<SessionBundle> sessionBundle;

public:
ClassificationServiceImpl(unique_ptr<SessionBundle>
sessionBundle) :
sessionBundle(move(sessionBundle)) {};

Status classify(ServerContext* context, const ClassificationRequest*
request,
ClassificationResponse* response) override {

ClassificationSignature signature;
const tensorflow::Status signatureStatus =
GetClassificationSignature(sessionBundle->meta_graph_def,
&signature);

if (!signatureStatus.ok()) {
return Status(StatusCode::INTERNAL, signatureStatus.error_message());
}

tensorflow::Tensor input(tensorflow::DT_STRING,
tensorflow::TensorShape());
input.scalar<string>()() = request->input();

vector<tensorflow::Tensor> outputs;

const tensorflow::Status inferenceStatus =
sessionBundle->session->Run(
{{signature.input().tensor_name(), input}},
{signature.classes().tensor_name(),
signature.scores().tensor_name()},
{},
&outputs);

if (!inferenceStatus.ok()) {
return Status(StatusCode::INTERNAL, inferenceStatus.error_message());
}

for (int i = 0; i < outputs[0].NumElements(); ++i) {
ClassificationClass *classificationClass =
response->add_classes();
classificationClass->set_name(outputs[0].flat<string>()(i));
classificationClass->set_score(outputs[1].flat<float>()(i));
}

return Status::OK;

}
};

int main(int argc, char** argv) {

if (argc < 3) {
cerr << “Usage: server <port> /path/to/export/files”
<< endl;
return 1;
}

const string serverAddress(string(“0.0.0.0:”) + argv[1]);
const string pathToExportFiles(argv[2]);

unique_ptr<SessionBundle> sessionBundle =
createSessionBundle(pathToExportFiles);

ClassificationServiceImpl
classificationServiceImpl(move(sessionBundle));

ServerBuilder builder;
builder.AddListeningPort(serverAddress,
grpc::InsecureServerCredentials());
builder.RegisterService(&classificationServiceImpl);

unique_ptr<Server> server = builder.BuildAndStart();
cout << “Server listening on ” << serverAddress <<
endl;

server->Wait();

return 0;
}

因而服务器端组件从webapp访问估摸服务。运维Python protocol
buffer编写翻译器,生成ClassificationService Python protocol
buffer客户端:pip install grpcio cython grpcio-tools, python -m
grpc.tools.protoc -I. –python_out=. –grpc_python_out=.
classification_service.proto。生成蕴含调用服务stub
classification_service_pb二.py
。服务器收到POST请求,深入分析发送表单,成立ClassificationRequest对象
。分类服务器设置三个channel,请求提交,分类响应渲染HTML,送回用户。容器外部命令python
client.py,运转服务器。浏览器导航http://localhost:8080 访问UI。

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

import cgi
import classification_service_pb2
from grpc.beta import implementations

class ClientApp(BaseHTTPRequestHandler):
def do_GET(self):
self.respond_form()

def respond_form(self, response=””):

form = “””
<html><body>
<h1>Image classification service</h1>
<form enctype=”multipart/form-data” method=”post”>
<div>Image: <input type=”file” name=”file”
accept=”image/jpeg”></div>
<div><input type=”submit” value=”Upload”></div>
</form>
%s
</body></html>
“””

response = form % response

self.send_response(200)
self.send_header(“Content-type”, “text/html”)
self.send_header(“Content-length”, len(response))
self.end_headers()
self.wfile.write(response)

def do_POST(self):

form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={
‘REQUEST_METHOD’: ‘POST’,
‘CONTENT_TYPE’: self.headers[‘Content-Type’],
})

request = classification_service_pb2.ClassificationRequest()
request.input = form[‘file’].file.read()

channel = implementations.insecure_channel(“127.0.0.1”, 9999)
stub =
classification_service_pb2.beta_create_ClassificationService_stub(channel)
response = stub.classify(request, 10) # 10 secs timeout

self.respond_form(“<div>Response: %s</div>” % response)

if __name__ == ‘__main__’:
host_port = (‘0.0.0.0’, 8080)
print “Serving in %s:%s” % host_port
HTTPServer(host_port, ClientApp).serve_forever()

出品希图,分类服务器应用产品。编译服务器文件复制到容器永远地方,清理全部一时创设文件。容器中,mkdir
/opt/classification_server, cd /mnt/home/serving_example, cp -R
bazel-bin/. /opt/classification_server, bazel clean
。容器外部,状态提交新Docker镜像,创制记录虚拟文件系统变化快照。容器外,docker
ps, dock commit <container
id>。图像推送到协调偏好docker服务云,服务。

参照他事他说加以考查资料:
《面向机器智能的TensorFlow施行》

应接付费咨询(150元每刻钟),我的微信:qingxingfengzi

http://www.bkjia.com/Pythonjc/1214401.htmlwww.bkjia.comtruehttp://www.bkjia.com/Pythonjc/1214401.htmlTechArticle学习笔记TF022:产品环境模型部署、Docker镜像、Bazel工作区、导出模型、服务器、客户端,tf022docker
产品意况模型安插,创立轻巧Web 应用程式,用户…

WORKSPACE文件local_repository规则定义第二方依赖为地面存款和储蓄文件。项目导入tf_workspace规则开端化TensorFlow依赖项。

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

int main(int argc, char** argv) {

vector<tensorflow::Tensor> outputs;

with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(sys.argv[1])
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, sys.argv[1] + “/” +
ckpt.model_checkpoint_path)
else:
print(“Checkpoint file not found”)
raise SystemExit

local_repository(
name = “inception_model”,
path = __workspace_dir__ + “/tf_serving/tf_models/inception”,
)

加载分类签名,GetClassificationSignature函数加载模型导出元数据ClassificationSignature,签字钦命所选取图像真实名称的输入张量逻辑名称,以及数据流图输出张量逻辑名称映射测度结果。将protobuf输入调换为测度输入张量,request参数复制JPEG编码图像字符串到估算张量。运营揣度,sessionbundle拿到TF会话对象,运维贰回,传入输入输出张量推测。估计输出张量转变protobuf输出,输出张量结果复制到ClassificationResponse新闻钦命形状response输出参数格式化。设置gRPC服务器,SessionBundle对象配置,创造ClassificationServiceImpl实例样板代码。

const tensorflow::Status inferenceStatus =
sessionBundle->session->Run(
{{signature.input().tensor_name(), input}},
{signature.classes().tensor_name(),
signature.scores().tensor_name()},
{},
&outputs);

TensorFlow服务在类型作为Git子模块。mkdir ~/serving_example,cd
~/serving_example,git init,git submodule add
https://github.com/tensorflow/serving.git ,tf_serving,git submodule
update –init –recursive 。

unique_ptr<SessionBundle> createSessionBundle(const string&
pathToExportFiles) {
SessionBundleConfig session_bundle_config = SessionBundleConfig();
unique_ptr<SessionBundleFactory> bundle_factory;
SessionBundleFactory::Create(session_bundle_config, &bundle_factory);

def respond_form(self, response=””):

py_binary(
name = “export”,
srcs = [
“export.py”,
],
deps = [
“@tf_serving//tensorflow_serving/session_bundle:exporter”,
“@org_tensorflow//tensorflow:tensorflow_py”,
“@inception_model//inception”,
],
)

saver = tf.train.Saver()

三个构建规则BUILD文件。在容器命令运营导出器,cd
/mnt/home/serving_example, hazel run:export /tmp/inception-v3,依靠/tmp/inception-v3关系的检查点文件在/tmp/inception-v3/{currenttimestamp}/创设导出器。第2遍运维要对TensorFlow编写翻译。load从表面导入protobuf库,导入cc_proto_library规则定义,为proto文件定义营造规则。通过命令bazel
run :server 9999
/tmp/inception-v3/export/{timestamp},容器运维推断服务器。

def do_POST(self):

#include “classification_service.grpc.pb.h”

service ClassificationService {
rpc classify(ClassificationRequest) returns (ClassificationResponse);
}

message ClassificationRequest {
// bytes input = 1;
float petalWidth = 1;
float petalHeight = 2;
float sepalWidth = 3;
float sepalHeight = 4;
};

message ClassificationClass {
string name = 1;
float score = 2;
}

syntax = “proto3”;

ServerBuilder builder;
builder.AddListeningPort(serverAddress,
grpc::InsecureServerCredentials());
builder.RegisterService(&classificationServiceImpl);

参谋资料:
《面向机器智能的TensorFlow实行》

#include <iostream>
#include <memory>
#include <string>

完成推测服务器。加载导出模型,调用揣摸方法,完成ClassificationService::Service。导出模型,创造SessionBundle对象,包蕴完全加载数据流图TF会话对象,定义导出工具分类签名元数据。SessionBundleFactory类创建SessionBundle对象,配置为pathToExportFiles钦赐路径加载导出模型,重临成立SessionBundle实例unique指针。定义ClassificationServiceImpl,接收SessionBundle实例参数。

return Status::OK;

出品情形模型陈设,创立轻松Web
应用程式,用户上传图像,运维英斯ption模型,完毕图像自动分拣。

tensorflow_serving.session_bundle.exporter.Exporter类导出模型。传入保存器实例创立实例,用exporter.classification_signature创立模型签字。内定input_tensor、输出张量。classes_tensor
包罗输出类名称列表、模型分配各个类分值(或可能率)socres_tensor。系列数多模型,配置钦点仅返田大口tf.nntop_k采纳类别,模型分配分数降序排列前K个种类。调用exporter.Exporter.init方法具名,export方法导出模型,接收输出路径、模型版本号、会话对象。Exporter类自动生成代码存在依据,Doker容器内部使用中bazel运转导出器。代码保存到bazel专门的职业区exporter.py。

ClassificationServiceImpl
classificationServiceImpl(move(sessionBundle));

def inference(images):
logits, _ = inception_model.inference(images, 1001)
return logits

public:
ClassificationServiceImpl(unique_ptr<SessionBundle>
sessionBundle) :
sessionBundle(move(sessionBundle)) {};

import time
import sys

import tensorflow as tf
from tensorflow_serving.session_bundle import exporter
from inception import inception_model

应接付费咨询(150元每时辰),笔者的微信:qingxingfengzi

local_repository(
name = “tf_serving”,
path = __workspace_dir__ + “/tf_serving”,
)

#include
“tensorflow_serving/servables/tensorflow/session_bundle_factory.h”

workspace(name = “serving”)

概念服务器接口。TensorFlow服务使用gRPC协议(基于HTTP/二贰进制协议)。帮忙成立服务器和自动生成客户端存根各个语言。在protocol
buffer定义服务契约,用于gRPC
IDL(接口定义语言)和2进制编码。接收JPEG编码待分类图像字符串输入,再次来到分数排列估摸种类列表。定义在classification_service.proto文件。接收图像、音频片段、文字服务可用可1接口。proto编写翻译器调换proto文件为客户端和劳务器类定义。bazel
build:classification_service_proto可行营造,通过bazel-genfiles/classification_service.grpc.pb.h检查结果。猜想逻辑,ClassificationService::Service接口必须完毕。检查bazel-genfiles/classification_service.pb.h查看request、response音讯定义。proto定义变成每系列型C++接口。

form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={
‘REQUEST_METHOD’: ‘POST’,
‘CONTENT_TYPE’: self.headers[‘Content-Type’],
})

using namespace std;
using namespace tensorflow::serving;
using namespace grpc;

搭建TensorFlow服务支付意况。安装Docker,https://docs.docker.com/engine/installation/
。用配备文件在该地创设Docker镜像,docker build –pull -t
$USE途锐/tensorflow-serving-devel
https://raw.githubusercontent.com/tensorflow/serving/master/tensorflow\_serving/tools/docker/Dockerfile.devel
。镜像运转容器,docker run -v $HOME:/mnt/home -p 999九:999九 -it
$USE卡宴/tensorflow-serving-devel
,在home目录加载到容器/mnt/home路线,在巅峰职业。用IDE或编辑器编辑代码,用容器运营创设筑工程具,主机通过999玖端口访问,创设服务器。exit命令退出容器终端,甘休运转。

import cgi
import classification_service_pb2
from grpc.beta import implementations

unique_ptr<Server> server = builder.BuildAndStart();
cout << “Server listening on ” << serverAddress <<
endl;

self.respond_form(“<div>Response: %s</div>” % response)

self.send_response(200)
self.send_header(“Content-type”, “text/html”)
self.send_header(“Content-length”, len(response))
self.end_headers()
self.wfile.write(response)

load(“@protobuf//:protobuf.bzl”, “cc_proto_library”)

bind(
name = “libssl”,
actual = “@boringssl_git//:ssl”,
)

model_exporter = exporter.Exporter(saver)
signature = exporter.classification_signature(
input_tensor=external_x, classes_tensor=classes,
scores_tensor=scores)
model_exporter.init(default_graph_signature=signature,
init_op=tf.initialize_all_tables())
model_exporter.export(sys.argv[1] + “/export”,
tf.constant(time.time()), sess)

def convert_external_inputs(external_x):
image =
tf.image.convert_image_dtype(tf.image.decode_jpeg(external_x,
channels=3), tf.float32)
images = tf.image.resize_bilinear(tf.expand_dims(image, 0), [299,
299])
images = tf.mul(tf.sub(images, 0.5), 2)
return images

if (!inferenceStatus.ok()) {
return Status(StatusCode::INTERNAL, inferenceStatus.error_message());
}

return 0;
}

load(‘//tf_serving/tensorflow/tensorflow:workspace.bzl’,
‘tf_workspace’)
tf_workspace(“tf_serving/tensorflow/”, “@org_tensorflow”)

if __name__ == ‘__main__’:
host_port = (‘0.0.0.0’, 8080)
print “Serving in %s:%s” % host_port
HTTPServer(host_port, ClientApp).serve_forever()

bind(
name = “zlib”,
actual = “@zlib_archive//:zlib”,
)

通过劳务器端组件从webapp访问估摸服务。运转Python protocol
buffer编写翻译器,生成ClassificationService Python protocol
buffer客户端:pip install grpcio cython grpcio-tools, python -m
grpc.tools.protoc -I. –python_out=. –grpc_python_out=.
classification_service.proto。生成包蕴调用服务stub
classification_service_pb二.py
。服务器收到POST请求,解析发送表单,创立ClassificationRequest对象
。分类服务器设置一个channel,请求提交,分类响应渲染HTML,送回用户。容器外部命令python
client.py,运营服务器。浏览器导航http://localhost:8080 访问UI。

cc_proto_library(
name=”classification_service_proto”,
srcs=[“classification_service.proto”],
cc_libs = [“@protobuf//:protobuf”],
protoc=”@protobuf//:protoc”,
default_runtime=”@protobuf//:protobuf”,
use_grpc_plugin=1
)

class ClassificationServiceImpl final : public
ClassificationService::Service {

产品企图,分类服务器应用产品。编写翻译服务器文件复制到容器恒久地方,清理全体有时构建文件。容器中,mkdir
/opt/classification_server, cd /mnt/home/serving_example, cp -R
bazel-bin/. /opt/classification_server, bazel clean
。容器外部,状态提交新Docker镜像,创设记录虚拟文件系统变化快照。容器外,docker
ps, dock commit <container
id>。图像推送到谐和偏好docker服务云,服务。

Status classify(ServerContext* context, const ClassificationRequest*
request,
ClassificationResponse* response) override {

tensorflow::Tensor input(tensorflow::DT_STRING,
tensorflow::TensorShape());
input.scalar<string>()() = request->input();

if (!signatureStatus.ok()) {
return Status(StatusCode::INTERNAL, signatureStatus.error_message());
}

unique_ptr<SessionBundle> sessionBundle;
bundle_factory->CreateSessionBundle(pathToExportFiles,
&sessionBundle);

return sessionBundle;
}

classes =
tf.contrib.lookup.index_to_string(tf.to_int64(class_ids),
mapping=tf.constant([str(i) for i in range(1001)]))

scores, class_ids = tf.nn.top_k(y, NUM_CLASSES_TO_RETURN)

for (int i = 0; i < outputs[0].NumElements(); ++i) {
ClassificationClass *classificationClass =
response->add_classes();
classificationClass->set_name(outputs[0].flat<string>()(i));
classificationClass->set_score(outputs[1].flat<float>()(i));
}

private:
unique_ptr<SessionBundle> sessionBundle;

class ClientApp(BaseHTTPRequestHandler):
def do_GET(self):
self.respond_form()

cc_binary(
name = “server”,
srcs = [
“server.cc”,
],
deps = [
“:classification_service_proto”,
“@tf_serving//tensorflow_serving/servables/tensorflow:session_bundle_factory”,
“@grpc//:grpc++”,
],
)

unique_ptr<SessionBundle> sessionBundle =
createSessionBundle(pathToExportFiles);

猜想方法各参数赋值。从检查点苏醒参数值。周期性保存模型演习检查点文件,文件包蕴学习参数。最终贰遍保存练习检查点文件包蕴最终更新模型参数。下去载预磨炼检查点文件:http://download.tensorflow.org/models/imagenet/inception-v3-2016-03-01.tar.gz
。在Docker容器中,cd /tmp, curl -0
http://download.tensorflow.org/models/image/imagenet/inception-v3-2016-03-01.tar.gz,
tar -xzf inception-v3-2016-03-01.tar.gz 。

导出磨练好的模型,导出数据流图及变量,给产品用。模型数据流图,必须从占位符接收输入,单步测度总结输出。Inception模型(或一般图像识别模型),JPEG编码图像字符串输入,与从TFRecord文件读取输入不一致。定义输入占位符,调用函数转变占位符表示外部输入为本来测度模型输入格式,图像字符串调换为各分量位于[0,
1]内像素张量,缩放图像尺寸,符合模型期望宽度中度,像素值转换成模型需要区间[-1,
1]内。调用原始模型测算方法,依附调换输入臆度结果。

}
};

if (argc < 3) {
cerr << “Usage: server <port> /path/to/export/files”
<< endl;
return 1;
}

local_repository(
name = “org_tensorflow”,
path = __workspace_dir__ + “/tf_serving/tensorflow”,
)

external_x = tf.placeholder(tf.string)
x = convert_external_inputs(external_x)
y = inference(x)

ClassificationSignature signature;
const tensorflow::Status signatureStatus =
GetClassificationSignature(sessionBundle->meta_graph_def,
&signature);

NUM_CLASSES_TO_RETURN = 10

server->Wait();

TensorFlow服务程序C++写,使用谷歌(Google)的Bazel创设筑工程具。容器运营Bazel。巴泽l代码级处理第1方信赖项。巴泽l自动下载创设。项目库根目录定义WO福睿斯KSPACE文件。TensorFlow模型库包括英斯ption模型代码。

channel = implementations.insecure_channel(“127.0.0.1”, 9999)
stub =
classification_service_pb2.beta_create_ClassificationService_stub(channel)
response = stub.classify(request, 10) # 10 secs timeout

response = form % response

request = classification_service_pb2.ClassificationRequest()
request.input = form[‘file’].file.read()

const string serverAddress(string(“0.0.0.0:”) + argv[1]);
const string pathToExportFiles(argv[2]);

form = “””
<html><body>
<h1>Image classification service</h1>
<form enctype=”multipart/form-data” method=”post”>
<div>Image: <input type=”file” name=”file”
accept=”image/jpeg”></div>
<div><input type=”submit” value=”Upload”></div>
</form>
%s
</body></html>
“””

message ClassificationResponse {
repeated ClassificationClass classes = 1;
};

相关文章