python - 如何使用需要使用 MLflow 的二维以上输入形状的模型进行预测?

我正在尝试将基于 tensorflow (keras) 的模型实现到 mlflow 中,同时了解它的工作原理以及它是否满足我们的需求。我正在尝试从 tensorflow 网站实现 Fashion MNIST 示例 Here the link

我能够使用以下代码训练模型并将其成功记录到 mlflow 中:

import mlflow
import mlflow.tensorflow
import mlflow.keras

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
           'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

train_images = train_images / 255.0

test_images = test_images / 255.0

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
          loss='sparse_categorical_crossentropy',
          metrics=['accuracy'])

if __name__ == "__main__":

    model.fit(train_images, train_labels, epochs=10)
    test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
    print('\nTest accuracy:', test_acc)

    mlflow.log_metric("validation accuracy", float(test_acc))
    mlflow.log_metric("validation loss", float(test_loss))
    mlflow.keras.log_model(model, 
                        "model", 
                        registered_model_name = "Fashion MNIST")

然后我现在使用 models serve 子命令为它提供服务

$ mlflow models serve -m [model_path_here] -p 1234

问题是我无法做出预测:

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0
labels = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
           'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

url = "http://127.0.0.1:1234/invocations"

to_predict = test_images[0]

data = {
    "data": [to_predict.tolist()]
}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)
res = r.json()

我收到这个错误:

{'error_code': 'BAD_REQUEST', 'message': 'Encountered an unexpected error while evaluating the model. Verify that the serialized input Dataframe is compatible with the model for inference.', 'stack_trace': 'Traceback (most recent call last):\n  File "/home/ferama/.local/lib/python3.6/site-packages/mlflow/pyfunc/scoring_server/__init__.py", line 196, in transformation\n    raw_predictions = model.predict(data)\n  File "/home/ferama/.local/lib/python3.6/site-packages/mlflow/keras.py", line 298, in predict\n    predicted = pd.DataFrame(self.keras_model.predict(dataframe))\n  File "/home/ferama/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 909, in predict\n    use_multiprocessing=use_multiprocessing)\n  File "/home/ferama/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_arrays.py", line 715, in predict\n    x, check_steps=True, steps_name=\'steps\', steps=steps)\n  File "/home/ferama/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 2472, in _standardize_user_data\n    exception_prefix=\'input\')\n  File "/home/ferama/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 564, in standardize_input_data\n    \'with shape \' + str(data_shape))\nValueError: Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (1, 28)\n'}

上面的代码适用于一维模型

在我看来,该错误与以下事实有关:pandas DataFrame 是二维数据结构,而模型需要三维输入。

错误“...但得到形状为 (1, 28) 的数组”的最新语句。输入形状应该是 (1, 28, 28) 而不是

有一种方法可以将这种模型与 mlflow 一起使用吗?有一种方法可以直接序列化和发送 numpy 数组作为输入而不是 pandas 数据帧吗?

最佳答案

尝试将您的输入转换为 base64

import base64

to_predict = test_images[0]
inputs = base64.b64encode(to_predict)

然后将其转换为Dataframe并发送请求

在后端将其解码回原始

np.frombuffer(base64.b64decode(encoded), np.uint8)

https://stackoverflow.com/questions/58917918/

相关文章:

azure - 从 Azure 部署中排除 Azure Function 中的文件

gradle - pivotal/LicenseFinder 为 Gradle 项目返回 "No d

react-native - 从选项卡导航选项卡打开抽屉导航

matplotlib - 如何在 seaborn 中有一个带有暗网格背景的透明图形?

javascript - 单击通知打开已安装的 PWA

android-studio - 为什么我只看到官方 android 类的反编译源代码?

python - 鼠兔连接丢失 Error : pika. exceptions.StreamLos

amazon-web-services - 状态机忽略阶跃函数错误

typescript - 如何使用 TestCafe 在失败时截屏

pycharm - 文件级无检查 PyCharm