Roundcube waf mikrotik丢包

TensorFlow Serving 是一个适用于机器学习Roundcube的灵活、高性能应用系统,专为生产环境而设计。借助 TensorFlow Serving,可以轻松部署新算法和实验,同时保留相同的服务器架构和 API。TensorFlow Serving 提供与 TensorFlow Roundcube的开箱即用型集成,但也可以轻松扩展以应用其他类型的Roundcube和waf。
官方指南:TensorFlow (google.cn)
Github:tensorflow/serving: A flexible, high-performance serving system for machine learning models (github.com)
环境准备
Linux 安装 docker 可参考:Ubuntu 安装 Docker 20.10.11-CSDN博客
docker 安装
官方手册:TensorFlow Serving with Docker | TFX (google.cn) Docker 仓库地址:tensorflow/serving – Docker Image | Docker Hub
# 拉取镜像
docker pull tensorflow/serving:2.7.0

# 创建项目文件夹并进入
mkdir tf_serving && cd $_

# 下载演示Roundcube
git clone
# 定义演示Roundcube路径
TESTDATA=”$(pwd)/serving/tensorflow_serving/servables/tensorflow/testdata”

# 启动 TensorFlow service 容器并打开 REST API 端口
docker run -t –rm -p 8501:8501 \
-v “$TESTDATA/saved_model_half_plus_two_cpu:/models/half_plus_two” \
-e MODEL_NAME=half_plus_two \
tensorflow/serving:2.7.0

# half_plus_two , which generates `0.5 * x + 2` for the values of `x` we provide for prediction.

# 使用 predict API 查询Roundcube
curl -d ‘{“instances”: [1.0, 2.0, 5.0]}’ \
-X POST

# Returns => { “predictions”: [2.5, 3.0, 4.5] }
123456789101112131415161718192021222324
端口
端口 8500:gRPC端口 8501:REST API 环境变量
MODEL_NAME:Roundcube名,默认值 modelMODEL_BASE_PATH:Roundcube基础路径,默认值 /models
当 docker 启动 ModelServer 时,容器内将按如下方式启动:
tensorflow_model_server –port=8500 –rest_api_port=8501 \
–model_name=${MODEL_NAME} –model_base_path=${MODEL_BASE_PATH}/${MODEL_NAME}
12
部署Roundcube
docker 部署
docker run -p 8500:8500 -p 8501:8501 \
–mount type=bind,source=/path/to/my_model/,target=/models/my_model \
–mount type=bind,source=/path/to/my/models.config,target=/models/models.config \
-t tensorflow/serving –model_config_file=/models/models.config
1234
TensorFlow Serving 支持热更新Roundcube,会自动加载版本号最大的Roundcube。 Roundcube文件夹结构如下:
/models/my_model
├── 1 # 版本号
│ ├── assets/
│ ├── variables/
│ └── saved_model.pb
├── …
└── N
├── assets/
├── variables/
└── saved_model.pb
12345678910
mikrotikRoundcube
Keras Sequential 模式建立的Roundcube使用 saved.model mikrotik后可以直接部署。 通过继承 tf.keras.Model 类建立的Roundcube,使用 Serving 部署时,对 saved.model 文件有更多要求:
丢包mikrotik的方法,要使用 @tf.function 装饰,并指定 input_signature 参数,以显式说明输入的形状。mikrotik时,tf.saved_model.save 丢包通过 signature 参数提供待mikrotik函数的签名。自定义的Roundcube可能有多个方法丢包mikrotik,丢包告诉 Serving 每个方法在被客户端调用时的函数名。
继承 tf.keras.Model 类建立的Roundcubemikrotik示例:
class MLP(tf.keras.Model):

@tf.function(input_signature=[tf.TensorSpec([None, 28, 28, 1], tf.float32)])
def call(self, inputs):

model = MLP()
tf.saved_model.save(model, “saved_with_signature/1”, signatures={“call”: model.call})
12345678
部署:
# 定义Roundcube路径
MY_MODEL=”$(pwd)/my_models”

# 启动 TensorFlow service 容器并打开 REST API 端口
docker run -t –rm -p 8501:8501 \
–mount type=bind,source=${MY_MODEL}/saved_model_mnist,target=/models/MNIST \
-e MODEL_NAME=MNIST \
tensorflow/serving:2.7.0

# 非容器直接启动
tensorflow_model_server –rest_api_port=8501 \
–model_name=MNIST –model_base_path=”${MY_MODEL}/saved_model_mnist”
12345678910111213
客户端调用
调用格式:
URI:

请求内容:
{
“signature_name”: “丢包调用的函数签名” // Sequential模式不丢包
“instances”: 输入waf
}
返回值
{
“predictions”: 返回值
}
1234567891011
Python 客户端示例
测试waf准备:model/utils.py
import numpy as np
import tensorflow as tf

class MNISTLoader:
“””读取 MNIST waf集waf”””

def __init__(self):
mnist = tf.keras.datasets.mnist

(self.train_data, self.train_label), (
self.test_data,
self.test_label,
) = mnist.load_data()
# MNIST 中的图像默认为 uint8(0~255的数字)
# 归一化为 0~1 的浮点数,并增加一维作为颜色通道
# [60000, 28, 28, 1]
self.train_data = np.expand_dims(
self.train_data.astype(np.float32) / 255.0, axis=-1
)
# [10000, 28, 28, 1]
self.test_data = np.expand_dims(
self.test_data.astype(np.float32) / 255.0, axis=-1
)
self.train_label = self.train_label.astype(np.int32) # [60000]
self.test_label = self.test_label.astype(np.int32) # [10000]
self.num_train_data, self.num_test_data = (
self.train_data.shape[0],
self.test_data.shape[0],
)

def get_batch(self, batch_size):
# 从waf集中随机抽取 batch_size 个元素并返回
index = np.random.randint(0, np.shape(self.train_data)[0], batch_size)
return self.train_data[index, :], self.train_label[index]
1234567891011121314151617181920212223242526272829303132333435
向本机的服务发送 MNIST 测试集的前 10 幅图像并返回预测结果,同时与测试集的真实标签进行对比:tf_serving_cli_demo.py
import json

import numpy as np
import requests

from model.utils import MNISTLoader

TFSERVING_IP = “192.168.3.11”
MODEL_NAME = “MNIST”
MODEL_VERSION = “v1” # Sequential 模式

url = f’
hearders = {“content-type”: “application/json”}

data_loader = MNISTLoader() # waf集加载类
data = json.dumps({
“signature_name”: “call”, # Sequential 模式注释掉此行代码
“instances”: data_loader.test_data[0:10].tolist()})

json_response = requests.post(
url=url,
data=data,
headers=hearders,
)

print(json.loads(json_response.text))
predictions = np.array(json.loads(json_response.text)[‘predictions’])
print(np.argmax(predictions, axis=-1)) # 最后一维方向上最大值的下标
print(data_loader.test_label[0:10])
1234567891011121314151617181920212223242526272829
运行
$ python -m tf_serving_cli_demo.py
{‘predictions’: [[2.02619885e-05, 5.56329338e-09, 0.000164046767, 0.000400436111, 1.59121754e-07, 3.61524485e-06, 9.066769e-10, 0.999333322, 9.8766468e-06, 6.83771068e-05], [2.01751373e-05, 0.000199802453, 0.99770087, 0.00100263022, 2.23010632e-09, 0.000860068132, 7.84274e-05, 1.17830334e-09, 0.000138036368, 6.55289689e-09], [3.39197613e-05, 0.974954724, 0.0111756073, 0.00205441914, 0.000608735951, 0.000799414702, 0.00157766754, 0.00584332971, 0.0025824639, 0.000369691086], [0.998874366, 4.11371488e-08, 0.000285651273, 1.76471931e-05, 2.8343693e-06, 0.000158297291, 0.000252537517, 8.19770721e-05, 8.92302705e-05, 0.000237392742], [0.000139237949, 7.97360735e-06, 0.00227909326, 0.000137919, 0.885741055, 0.000345102831, 0.000372990908, 0.00107208558, 0.000659250771, 0.109245256], [4.03830654e-06, 0.988832474, 0.00164790556, 0.000509007077, 9.42776896e-05, 5.5350105e-05, 4.60995798e-05, 0.00749095436, 0.00107706082, 0.000242864378], [3.09578741e-06, 1.32172036e-05, 4.79727569e-05, 0.000104262901, 0.976082921, 0.00237769959, 3.37329075e-05, 0.00111366331, 0.0143848434, 0.00583857112], [4.07796506e-06, 0.000804283074, 0.00784022454, 0.00185568642, 0.0123693729, 0.00326056359, 0.000173003718, 0.00125739607, 0.0112659866, 0.961169422], [0.00187212788, 1.61147345e-05, 0.0118817631, 2.39907786e-06, 0.00636754185, 0.0134000927, 0.959635317, 1.38140285e-06, 0.00654726196, 0.00027601156], [1.10070605e-05, 3.55845486e-08, 3.31486699e-05, 3.98701422e-06, 0.00842323713, 1.39689009e-05, 3.58293022e-07, 0.0146128442, 0.00233540428, 0.974566]]}
[7 2 1 0 4 1 4 9 6 9]
[7 2 1 0 4 1 4 9 5 9]
1234