我正在尝试在 mlflow 中实现基于 tensorflow(keras)的模型,同时了解它的工作原理以及它是否适合我们的需求。我正在尝试从 tensorflow 网站实现 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")
然后我现在用模型服务子命令来服务它
$ 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 数据帧?