TensorFlow 2 模型部署方法实践--使用 Gradio 快速部署模型
Jachin111关注IP属地: 浙江
2021.05.26 07:17:21字数 953阅读 1,012
使用 Gradio 快速部署模型
环境配置
与前面的实验环境都有所不同,本次实验将使用到实验楼的 Linux 桌面环境。首先,打开 Xfce 终端,创建 Python3.6 虚拟环境。
同时为了留出足够的空间以保证第三方库正常安装,我们需要临时删除线上环境中的一些非必要文件。
$ sudo rm -rf node_modules golang
$ virtualenv -p /usr/bin/python3.6 pyenv
进入虚拟环境,安装实验需要的库 TensorFlow 和 Gradio。
$ . pyenv/bin/activate
$ pip install tensorflow gradio==0.8.1
接下来,下载预训练模型和之前实验中所使用的的图片,并将预训练模型其放于 ~/.keras/models 目录下。
$ wget https://labfile.oss.aliyuncs.com/courses/1435/image.jpg
$ wget https://labfile.oss.aliyuncs.com/courses/1435/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224.h5
$ mkdir -p ~/.keras/models
$ cp mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224.h5 ~/.keras/models
修改 Gradio 以适配 TensorFlow 2
Gradio 目前只支持 TensorFlow 1.x 版本,而 TensorFlow 2 中移除了 Session 的使用,因此,只需在 Gradio 源码中注释掉此部分就可以运行在 TensorFlow 2 上了。
需要修改的文件在目录 /home/shiyanlou/pyenv/lib/python3.6/site-packages/gradio/ 下。使用文本编辑器如 Sublime 打开 interface.py 文件,修改如下:
注释第 94,95 行。
image.png
注释第 143,144 行,减少第 145 行的两个缩进。
image.png
基本用法
使用 Gradio 创建界面只需在现有代码中添加几行即可。如在实验楼目录 /home/shiyanlou/ 下创建文件 quick_start.py,在文件中写入如下代码就可以使用 Keras 预训练模型创建界面了。
import gradio
import tensorflow as tf
# 导入 ImageNet 预训练的 MobileNetV2
model = tf.keras.applications.mobilenet_v2.MobileNetV2(weights='imagenet')
'''
创建界面对象
输入方式 inputs 使用图片上传的方式
输出方式 outputs 使用模型所对应的索引号
模型类型 model_type 使用 keras
模型传入上面定义的 MobileNet 模型
'''
io = gradio.Interface(inputs="imageupload", outputs="label", model_type="keras", model=model)
# 启动服务
io.launch()
在终端运行代码。
$ python quick_start.py
从终端可以看到,服务运行在地址 http://localhost:7860 上,打开浏览器,访问该地址就可以进行使用了。
https://labfile.oss.aliyuncs.com/courses/1435/6-1.mp4
使用 ImageNet
从上面的过程可以看到,服务界面 output 的输出是一串数字,对应的是模型类别所在索引号,而我们使用的是 ImageNet 预训练的模型,因此需要传入 ImageNet 所对应标签的 list。Gradio 中已内置 ImageNet 索引所对应的标签,只需要把 output 的参数改成 gradio.outputs 对象,同时设定标签类型为 ImageNet 即可。
进一步修改 quick_start.py 文件如下:
import gradio
import tensorflow as tf
# 导入 ImageNet 预训练的 MobileNetV2
model = tf.keras.applications.mobilenet_v2.MobileNetV2(weights='imagenet')
# 定义输出为 gradio.outputs 对象,同时传入 imagenet 参数
outputs = gradio.outputs.Label(label_names='imagenet1000')
# 创建界面对象
io = gradio.Interface(inputs="imageupload", outputs=outputs, model_type="keras", model=model)
# 启动服务
io.launch()
在终端终止运行(Ctrl + C)之前的脚本,然后重新运行 python quick_start.py,再次访问 http://localhost:7860,就可以看到上传的图片的预测值是 ImageNet 所对应的标签了。要注意这里程序可能会在 7860 端口启动失败,那么就会尝试在 7861 启动,启动成功后可以在终端看到服务的具体端口号。
https://labfile.oss.aliyuncs.com/courses/1435/6-2.mp4
使用手写输入
$ wget https://labfile.oss.aliyuncs.com/courses/1435/mnist_model.h5
在实验楼目录 /home/shiyanlou/ 下创建文件 mnist.py,写入以下代码。
import gradio
import tensorflow as tf
# 导入手写体识别模型
model = tf.keras.models.load_model('mnist_model.h5')
# 初始化一个手写输入的对象,flatten=True 表示将图片一维化
inp = gradio.inputs.Sketchpad(flatten=True)
# 创建界面对象
io = gradio.Interface(inputs=inp, outputs='label', model_type="keras", model=model)
# 启动服务
io.launch()
在终端终止之前的代码,运行 python mnist.py,再次访问 http://localhost:7860,就可以通过手写数字进行识别了。
https://labfile.oss.aliyuncs.com/courses/1435/6-3.mp4
使用文本输入
Gradio 还支持文本框输入与自定义的 Python 函数,在实验楼目录 /home/shiyanlou/ 下创建文件 text.py,我们定义一个大写转换函数来使用文本输入功能。
import gradio
# 定义一个英文的大写转换函数
def upper(x):
return x.upper()
# 创建界面对象时设置输入为文本输入框,模型类别为 Python 函数
io = gradio.Interface(inputs="textbox", outputs="textbox", model=upper, model_type='pyfunc')
io.launch()
在终端终止之前的代码,运行 python text.py,再次访问 http://localhost:7860,就可以在文本框内输入英文进行大写转换了。