在 Flask 中加载模型时将形状转换为 TensorShape 时出错

Error converting shape to a TensorShape when loading model in flask

我在本地机器上训练了一个 CNN 模型,使用 model.save('./models/my_model') 保存了模型,我能够加载模型 (new_model = tensorflow.keras.models.load_model('./models/my_model')) 并对我通过浏览器传递的图像进行分类网页框架 Flask.

现在,我想 运行 我的代码托管在 pythonanywhere.com 中。但是,加载模型时出现此错误:

ValueError: Error converting shape to a TensorShape: invalid literal for int() with base 10: 'class_name'.

不知道是不是跟版本有关。首先,我使用 python 3.8 和最新的 tensorflow 版本进行训练,但由于 Flask 不允许在 3.8 中使用所有必需的库,因此我在带有 tensorflow 2.0.0 的 flask 中使用了 3.7。所以我在我的电脑上用 3.7 和 tf 2.0.0 重新训练了模型,并上传了更新的模型文件。但是,同样的错误仍然存​​在。

--更新-- 我把日志错误放在这里

Error running WSGI application ValueError: Error converting shape to a TensorShape: invalid literal for int() with base 10: 'class_name'.

File "/var/www/user_pythonanywhere_com_wsgi.py", line 16, in from main import app as application # noqa

File "/home/user/mysite/main.py", line 11, in model = tensorflow.keras.models.load_model('/home/user/mysite/models/modelo')

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/save.py", line 150, in load_model return saved_model_load.load(filepath, compile)

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 86, in load model = tf_load.load_internal(path, loader_cls=KerasObjectLoader)

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/saved_model/load.py", line 541, in load_internal export_dir)

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 103, in init self._finalize()

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/keras/saving/saved_model/load.py", line 127, in _finalize node.add(layer)

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper result = method(self, *args, **kwargs)

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/sequential.py", line 174, in add batch_shape=batch_shape, dtype=dtype, name=layer.name + '_input')

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/input_layer.py", line 263, in Input input_tensor=tensor)

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/input_layer.py", line 125, in init ragged=ragged)

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/keras/backend.py", line 1057, in placeholder x = array_ops.placeholder(dtype, shape=shape, name=name)

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/ops/array_ops.py", line 2630, in placeholder return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_array_ops.py", line 6669, in placeholder shape = _execute.make_shape(shape, "shape")

File "/usr/lib/python3.7/site-packages/tensorflow_core/python/eager/execute.py", line 211, in make_shape e))

错误来自tensorflow文件,我只是传递保存模型的路径

Python 将您的 class_name 转换为整数时出现问题。

参考:ValueError: invalid literal for int() with base 10: ''

TensorFlow 目前无法在 PythonAnywhere 网络应用程序中运行。如果您使用的是 Keras,您可以尝试切换到已确认可以工作的 Theano 后端。还有一个简短的help page