我使用Tensorflow和Keras,并利用IAM数据集创建了一个机器学习模型。如何将这个模型加载为API来预测图像呢?当我尝试集成时,出现了错误
return self.function(inputs, **arguments) File "test2.py", line 136, in resize_image return tf.image.resize_images(image,[56,56]) NameError: name 'tf' is not defined
我已经使用 from keras.models import load_model 加载了模型,并且尝试预测手写图像。我尝试集成的模型是 low_loss.hdf5
。
def testmodel(image_path): global model # 加载预训练的Keras模型 model = load_model('low_loss.hdf5') model.summary() img = Image.open(image_path).convert("L") img = np.resize(image_path, (28,28,1)) im2arr = np.array(img) im2arr = im2arr.reshape(1,28,28,1) y_pred = model.predict_classes(im2arr) return y_pred
我希望预测手写图像数据。
回答:
您遇到错误是因为您在代码中没有导入TensorFlow,或者如果您已经导入了,您没有给它一个别名。
import tensorflow as tf