如何使用MLflow使用需要两个以上维度的输入形状的模型进行预测?

2024-06-01 00:37:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在mlflow中实现一个基于tensorflow(keras)的模型,同时了解它是如何工作的,以及它是否适合我们的需要。我正在尝试从tensorflow网站Here the link实现Fashion MNIST示例

我能够使用以下代码训练并成功地将模型记录到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")

现在我用modelserve子命令来处理它

$ 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'}

上面的代码在一维模型中运行良好

在我看来,这个错误与这样一个事实有关:熊猫数据帧是一个二维数据结构,而模型需要一个三维输入。你知道吗

来自错误“…但是得到了形状为(1,28)”的数组”的最新单词。输入形状应改为(1,28,28)

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


Tags: testimportinputdatalabelsmodeltensorflowtrain