ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: (None, 28, 28)
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: (None, 28, 28)
我正在使用 Tensorflow 和 Flask 做一个有界面的 AI。我在 PythonAnywhere 上托管了以下代码:
@app.route('/output', methods=["GET", "POST"])
def processing():
if request.args.get('sample'):
image = Image.open(f'/home/ainum/web/static/samples/{request.args.get("sample")}.jpg')
filename = f'/home/ainum/web/static/samples/{request.args.get("sample")}.jpg'
else:
img = request.files['img']
name, extension = img.filename.split('.')
filename = name + datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + '.' + extension
img.save('/home/ainum/web/inputs/' + filename)
image = Image.open('/home/ainum/web/inputs/' + filename)
model = load_model('/home/ainum/web/model_fmr_all.h5')
img_width, img_height = Image.HAMMING, Image.HAMMING
img = image.resize((28, 28), 1)
img = img.convert('L')
image = np.array(img, dtype='float64') / 255.
image = np.expand_dims(image, axis=0)
pred = model.predict(image)
return render_template('res.html', res=str(pred))
上传图片时出现的错误是:
**NO MATCH**
2022-01-28 08:36:09,018: Exception on /output [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2051, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1501, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1499, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1485, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/home/ainum/web/app.py", line 38, in processing
pred = model.predict(image)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 1629, in predict
tmp_batch_outputs = self.predict_function(iterator)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 828, in __call__
result = self._call(*args, **kwds)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 871, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 726, in _initialize
*args, **kwds))
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 2969, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 3361, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 3206, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py", line 990, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 634, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py", line 977, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
**NO MATCH**
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1478 predict_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1468 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1461 run_step **
outputs = model.predict_step(data)
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1434 predict_step
return self(x, training=False)
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py:223 assert_input_compatibility
str(tuple(shape)))
**NO MATCH**
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: (None, 28, 28)
**NO MATCH**
另一方面,在本地主机上一切都是正确的。你能给我解释一下吗?我完全不了解人工智能;代码不是我的;我只是在创建界面。
谢谢!
我认为如果将图像重塑为 4D,就可以开始了。所以,在 image = np.expand_dims(image, axis=0)
之后,只是 运行 image = image.reshape(image.shape + (1,))
:
import numpy as np
img = np.random.random((28, 28))
image = np.array(img, dtype='float64') / 255.
image = np.expand_dims(image, axis=0)
image = image.reshape(image.shape + (1,))
我正在使用 Tensorflow 和 Flask 做一个有界面的 AI。我在 PythonAnywhere 上托管了以下代码:
@app.route('/output', methods=["GET", "POST"])
def processing():
if request.args.get('sample'):
image = Image.open(f'/home/ainum/web/static/samples/{request.args.get("sample")}.jpg')
filename = f'/home/ainum/web/static/samples/{request.args.get("sample")}.jpg'
else:
img = request.files['img']
name, extension = img.filename.split('.')
filename = name + datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + '.' + extension
img.save('/home/ainum/web/inputs/' + filename)
image = Image.open('/home/ainum/web/inputs/' + filename)
model = load_model('/home/ainum/web/model_fmr_all.h5')
img_width, img_height = Image.HAMMING, Image.HAMMING
img = image.resize((28, 28), 1)
img = img.convert('L')
image = np.array(img, dtype='float64') / 255.
image = np.expand_dims(image, axis=0)
pred = model.predict(image)
return render_template('res.html', res=str(pred))
上传图片时出现的错误是:
**NO MATCH**
2022-01-28 08:36:09,018: Exception on /output [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2051, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1501, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1499, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1485, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/home/ainum/web/app.py", line 38, in processing
pred = model.predict(image)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 1629, in predict
tmp_batch_outputs = self.predict_function(iterator)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 828, in __call__
result = self._call(*args, **kwds)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 871, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 726, in _initialize
*args, **kwds))
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 2969, in _get_concrete_function_internal_garbage_collected
graph_function, _ = self._maybe_define_function(args, kwargs)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 3361, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/function.py", line 3206, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py", line 990, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py", line 634, in wrapped_fn
out = weak_wrapped_fn().__wrapped__(*args, **kwds)
File "/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py", line 977, in wrapper
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
**NO MATCH**
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1478 predict_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1468 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/site-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1461 run_step **
outputs = model.predict_step(data)
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:1434 predict_step
return self(x, training=False)
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/usr/local/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py:223 assert_input_compatibility
str(tuple(shape)))
**NO MATCH**
ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: (None, 28, 28)
**NO MATCH**
另一方面,在本地主机上一切都是正确的。你能给我解释一下吗?我完全不了解人工智能;代码不是我的;我只是在创建界面。
谢谢!
我认为如果将图像重塑为 4D,就可以开始了。所以,在 image = np.expand_dims(image, axis=0)
之后,只是 运行 image = image.reshape(image.shape + (1,))
:
import numpy as np
img = np.random.random((28, 28))
image = np.array(img, dtype='float64') / 255.
image = np.expand_dims(image, axis=0)
image = image.reshape(image.shape + (1,))