InvalidArgumentError:训练模型时图形执行错误

eh57zj3b  于 2022-09-18  发布在  Java
关注(0)|答案(1)|浏览(748)

我正在做一个个人项目,其中我使用计算机视觉和回溯算法来解决数独谜题。当我试图在一台新计算机上设置项目时,突然出现这个错误。这是我为CV零件训练模型的文件。

from tabnanny import verbose
from turtle import pu
import numpy
import cv2
import matplotlib.pyplot as plot
from keras.models import model_from_json

json_file =open('model/model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loadedModel = model_from_json(loaded_model_json)
loadedModel.load_weights('model/model.h5')
print("Loaded saved model from disk.")

def predictNumber(image):
    imageResize = cv2.resize(image,(28,28))
    imageResizeCopy = imageResize.reshape(1, 1, 28, 28)
    #loadedModelPred = loadedModel.predict_classes(imageResizeCopy, verbose=0)
    loadedModelPred = numpy.argmax(loadedModel.predict(imageResizeCopy), axis=1)
    return loadedModelPred[0]

def extract(puzzle):
    puzzle = cv2.resize(puzzle, (450,450))

    grid = numpy.zeros([9,9])
    for i in range(9):
        for j in range(9): 
            image = puzzle[i*50:(i+1)*50,j*50:(j+1)*50]
            if image.sum()>25000:
                grid[i][j] = predictNumber(image)
            else:
                grid[i][j] =0;
    return grid.astype(int)

上述代码块是显然会引发以下错误的代码的一部分。

2022-09-17 21:29:46.532 Uncaught app exception
Traceback (most recent call last):
  File "C:UserskvnkaAppDataRoamingPythonPython310site-packagesstreamlitruntimescriptrunnerscript_runner.py", line 556, in _run_script
    exec(code, module.__dict__)
  File "C:UserskvnkaOneDrive - Trinity College DublinGitHubsudoku-solverapp.py", line 27, in <module>
    grid = numberExtract.extract(image)
  File "C:UserskvnkaOneDrive - Trinity College DublinGitHubsudoku-solvercvnumberExtract.py", line 31, in extract
    grid[i][j] = predictNumber(image)
  File "C:UserskvnkaOneDrive - Trinity College DublinGitHubsudoku-solvercvnumberExtract.py", line 20, in predictNumber
    loadedModelPred = numpy.argmax(loadedModel.predict(imageResizeCopy), axis=1)
  File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasutilstraceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:UserskvnkaAppDataRoamingPythonPython310site-packagestensorflowpythoneagerexecute.py", line 54, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Detected at node 'sequential_1/max_pooling2d_1/MaxPool' defined at (most recent call last):
    File "C:Program FilesPython310libthreading.py", line 973, in _bootstrap
      self._bootstrap_inner()
    File "C:Program FilesPython310libthreading.py", line 1016, in _bootstrap_inner
      self.run()
    File "C:Program FilesPython310libthreading.py", line 953, in run
      self._target(*self._args,**self._kwargs)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packagesstreamlitruntimescriptrunnerscript_runner.py", line 295, in _run_script_thread
      self._run_script(request.rerun_data)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packagesstreamlitruntimescriptrunnerscript_runner.py", line 556, in _run_script
      exec(code, module.__dict__)
    File "C:UserskvnkaOneDrive - Trinity College DublinGitHubsudoku-solverapp.py", line 27, in <module>
      grid = numberExtract.extract(image)
    File "C:UserskvnkaOneDrive - Trinity College DublinGitHubsudoku-solvercvnumberExtract.py", line 31, in extract
      grid[i][j] = predictNumber(image)
    File "C:UserskvnkaOneDrive - Trinity College DublinGitHubsudoku-solvercvnumberExtract.py", line 20, in predictNumber
      loadedModelPred = numpy.argmax(loadedModel.predict(imageResizeCopy), axis=1)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasutilstraceback_utils.py", line 65, in error_handler
      return fn(*args,**kwargs)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasenginetraining.py", line 2344, in predict
      tmp_batch_outputs = self.predict_function(iterator)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasenginetraining.py", line 2131, in predict_function
      return step_function(self, iterator)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasenginetraining.py", line 2117, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasenginetraining.py", line 2105, in run_step
rasenginesequential.py", line 412, in call
      return super().call(inputs, training=training, mask=mask)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasenginefunctional.py", line 510, in call
      return self._run_internal_graph(inputs, training=training, mask=mask)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasenginefunctional.py", line 667, in _run_interrasenginesequential.py", line 412, in call
      return super().call(inputs, training=training, mask=mask)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasenginefunctional.py", line 510, in call            return self._run_internal_graph(inputs, training=training, mask=mask)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasenginefunctional.py", line 667, in _run_internal_graph
      outputs = node.layer(*args,**kwargs)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasutilstraceback_utils.py", line 65, in error_handler
      return fn(*args,**kwargs)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasenginebase_layer.py", line 1107, in __call__       outputs = call_fn(inputs, *args,**kwargs)
    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskerasutilstraceback_utils.py", line 96, in error_handler
      return fn(*args,**kwargs)    File "C:UserskvnkaAppDataRoamingPythonPython310site-packageskeraslayerspoolingbase_pooling2d.py", line 84, in call
      outputs = self.pool_function(
Node: 'sequential_1/max_pooling2d_1/MaxPool'
Default MaxPoolingOp only supports NHWC on device type CPU
         [[{{node sequential_1/max_pooling2d_1/MaxPool}}]] [Op:__inference_predict_function_290]

我有点不知道是什么导致了这个错误。

ffdz8vbo

ffdz8vbo1#

The problem is at the line:

imageResizeCopy = imageResize.reshape(1, 1, 28, 28)

NHWC stands for (n_samples, height, width, channels) but you are reshaping your image in a channel first format (n_samples, channels, height, width).

Channel first is usually used for PyTorch by the way, while TensorFlow's default format is channel last. You only have to reshape your image accordingly.

You want to obtain a shape like this: (1, 28, 28, 1).

相关问题