People commonly tend to put much effort on hyperparameter tuning and training while using Tensoflow&Deep Learning. A realistic problem for TF is how to integrate models into industry: saving pre-trained models, restoring them when necessary, and doing predictions regarding to request input. Fortunately, Google AI helps!

Actually, while a model is trained, tensorflow has two different modes to save it. Most people and blog posts adopt Checkpoint, which refers to 'Training Mode'. The training work continues if someone load the checkpoint. But a drawback is you have to define the architecture once and once again before restore the checkpoint. Another mode called 'SavedModel' is more suitable for serving (release version product). Applications can send prediction requests to a server where the 'SavedModel' is deployed, and then responses will be sent back.

Before that, we only need to follow three steps: save the model properly, deploy it onto Google AI, transform data to required format then request. I am going to illustrate them one by one.

1. Save the model into SavedModel:

In a typical tensorflow training work, architecture is defined first, then it is trained, finally comes to saving part. We just jump to the saving code: the function used here is 'simple_save', and four parameters are session, saving folder, input variable&name, output variable&name.

tf.saved_model.simple_save(sess, 'simple_save/model', \
inputs={"x": tf_x},outputs={"pred": pred})

After that, we got the saved model on the target directory:

saved_model.pb

/variables/variables.index

/variables/variables.data-00000-of-00001

2. Deploy SavedModel onto Google AI:

On Google Cloud, files are stored on Google Bucket, so first a Tensorflow model (.pb file and variables folder) need to be uploaded. Then create a Google AI model and a version. Actually there can be multiple versions under a model, which is quite like solving one task by different ways. You can even use several deep learning architectures as

different version, and then switch solutions when request predictions. Versions and Google Bucket location that stores the SavedModel are bound.

3. Doing online predictions:

Because we request prediction inside the application, and require immediate response, so we choose online prediction. The official code to request is shown below, which is HTTP Request and HTTP Response. The input and output data are all in Json Format. We can transform our input data into List, and call this function.

def predict_json(project, model, instances, version=None):
"""Send json data to a deployed model for prediction. Args:
project (str): project where the AI Platform Model is deployed.
model (str): model name.
instances ([Mapping[str: Any]]): Keys should be the names of Tensors
your deployed model expects as inputs. Values should be datatypes
convertible to Tensors, or (potentially nested) lists of datatypes
convertible to tensors.
version: str, version of the model to target.
Returns:
Mapping[str: any]: dictionary of prediction results defined by the
model.
"""
# Create the AI Platform service object.
# To authenticate set the environment variable
# GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
service = googleapiclient.discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(project, model) if version is not None:
name += '/versions/{}'.format(version) response = service.projects().predict(
name=name,
body={'instances': instances}
).execute() if 'error' in response:
raise RuntimeError(response['error']) return response['predictions']

The response is also in Json format, I wrote a piece of code to transform it into Numpy Array:

def from_json_to_array(dict_list):
value_list = []
for dict_instance in dict_list:
instance = dict_instance.get('pred')
value_list.append(instance)
value_array = np.asarray(value_list)
return value_array

Yeah, that's it!  Let's get your hands dirty!

Reference:

https://www.tensorflow.org/guide/saved_model

https://cloud.google.com/blog/products/ai-machine-learning/simplifying-ml-predictions-with-google-cloud-functions

https://cloud.google.com/ml-engine/docs/tensorflow/online-predict

Run Your Tensorflow Deep Learning Models on Google AI的更多相关文章

  1. How to Grid Search Hyperparameters for Deep Learning Models in Python With Keras

    Hyperparameter optimization is a big part of deep learning. The reason is that neural networks are n ...

  2. a Javascript library for training Deep Learning models

    w强化算法和数学,来迎接机器学习.神经网络. http://cs.stanford.edu/people/karpathy/convnetjs/ ConvNetJS is a Javascript l ...

  3. Towards Deep Learning Models Resistant to Adversarial Attacks

    目录 概 主要内容 Note Madry A, Makelov A, Schmidt L, et al. Towards Deep Learning Models Resistant to Adver ...

  4. (转) Awesome Deep Learning

    Awesome Deep Learning  Table of Contents Free Online Books Courses Videos and Lectures Papers Tutori ...

  5. What are some good books/papers for learning deep learning?

    What's the most effective way to get started with deep learning?       29 Answers     Yoshua Bengio, ...

  6. (转) Deep Learning Resources

    转自:http://www.jeremydjacksonphd.com/category/deep-learning/ Deep Learning Resources Posted on May 13 ...

  7. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  8. The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near

    The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near ...

  9. Coursera Deep Learning 2 Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization - week1, Assignment(Regularization)

    声明:所有内容来自coursera,作为个人学习笔记记录在这里. Regularization Welcome to the second assignment of this week. Deep ...

随机推荐

  1. 在navcat中清空数据后,设置id归零方法

    写后台完成后,需要清空Mysql数据库中的测试数据,但是后面新增的数据,一直是以原来所删除数据的最大id为增量基本,比如,对于一些id敏感的项,十分不便,如图 原有10条数据,清空后,新增一两条,手动 ...

  2. 案例 element 表单名两端对齐

    >>> .el-form-item label:after { content: ""; display: inline-block; width: 100%; ...

  3. C++基础之static(静态)变量

    static 表示静态   作用: 1.在函数体内,静态变量的值维持不变(记忆功能) 2.是一个本地的全局函数,即只能被本模块的函数访问(隐藏功能)   static变量: static全局变量和普通 ...

  4. Linux性能优化从入门到实战:16 文件系统篇:总结磁盘I/O指标/工具、问题定位和调优

    (1)磁盘 I/O 性能指标 文件系统和磁盘 I/O 指标对应的工具 文件系统和磁盘 I/O 工具对应的指标 (2)磁盘 I/O 问题定位分析思路 (3)I/O 性能优化思路 Step 1:首先采用 ...

  5. 网络拓扑_VLAN与Trunk配置

    实验目的: 1.实现VLAN10的两台主机互通; VLAN20的两台主机互通; 2.VLAN10与VLAN20主机不能互通. 拓扑图: 步骤: 1.依图配置PC1,PC2,PC3,PC4的IP,掩码, ...

  6. Oracle Set操作

    并集合 union/uinon all union 会去重,uinon all 不去重 交集 intersect 差集 minus

  7. c++函数参数或返回值为函数指针

    C++中函数指针的形式为:返回值类型 + 参数类型,函数没有值类型,但是却可以声明函数的指针,因为函数是可寻址的,存放在内存中的代码段,可以从指针访问. 函数指针可以声明为: void (*pF)(v ...

  8. sublime下载emmet

    Emmet是一款Web前端开发工具Sublime非常有用的插件,使用仿CSS选择器的语法来生成代码,大大提高了HTML和CSS代码编写的速度.只需按住Tab键即可把一个简写展开成HTML和CSS的代码 ...

  9. 揭示编译器API

    编译器管道功能区 .NET编译器平台(“Roslyn”)通过提供一个API层,是一个传统编译器管道镜像,向你这样的消费者揭示了C#和Visual Basic编译器的代码分析. 这条管道的每一部分,现在 ...

  10. windows2008R2-AD域控组策略设置与其它相关设置

    防火墙设置 修改>计算机配置>策略>安全设置>高级安全windows防火墙>高级安全windows防火墙 修改入站规则 1.组名-文件和打印机共享(SMB-In)> ...