如何使用需要使用MLflow进行二维输入的形状的模型进行预测? [英] How to make predictions using a model that requires an input shape with more than two dimensions using MLflow?

查看:113
本文介绍了如何使用需要使用MLflow进行二维输入的形状的模型进行预测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将基于张量流(keras)的模型导入mlflow,同时了解其工作方式以及是否满足我们的需求.我正在尝试从tensorflow网站上实现Fashion MNIST示例在此链接

I'm trying to implement a tensorflow (keras) based model into mlflow while learning how it works and if it suite our needs. I'm trying to implement the Fashion MNIST example from tensorflow website Here the link

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

I was able to train and to log the model successfully into mlflow using this code:

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子命令一起使用

Then I'm now serving it with the models serve subcommand

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

问题是我无法做出预测:

The problem is that I'm not able to make predictions:

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

上面的代码与一维模型配合良好

That code above worked fine with a one dimension model

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

The error seems to me related to the fact that a pandas DataFrame is a two dimensional data structure and the model instead requires a three dimensional input.

错误"...但是得到形状为(1,28)的数组"的最新单词.输入形状应改为(1、28、28)

The latest words from the error "...but got array with shape (1, 28)". The input shape should be (1, 28, 28) instead

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

There is a way to use this kind of models with mlflow? There is a way to serialize and send numpy arrays directly as input instead of pandas dataframes?

推荐答案

尝试在base64中转换您的输入

Try to convert your input in base64

import base64

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

然后将其转换为数据框并发送请求

then convert it to Dataframe and send request

通过后端将其解码回原始文件

decode it back to original at backend by

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

这篇关于如何使用需要使用MLflow进行二维输入的形状的模型进行预测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆