InvalidArgumentError: Graph execution error while training model

Viewed 39

I am doing a personal project where I use Computer Vision and a Backtracking algorithm to solve sudoku puzzles. This error is suddenly popping up when I tried to setup the project on a new computer. This is the file where I train my model for the CV part.

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)

This above code block is part of the code that apparently brings up the following error.

2022-09-17 21:29:46.532 Uncaught app exception
Traceback (most recent call last):
  File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 556, in _run_script
    exec(code, module.__dict__)
  File "C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\sudoku-solver\app.py", line 27, in <module>
    grid = numberExtract.extract(image)
  File "C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\sudoku-solver\cv\numberExtract.py", line 31, in extract
    grid[i][j] = predictNumber(image)
  File "C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\sudoku-solver\cv\numberExtract.py", line 20, in predictNumber
    loadedModelPred = numpy.argmax(loadedModel.predict(imageResizeCopy), axis=1)
  File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\eager\execute.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 Files\Python310\lib\threading.py", line 973, in _bootstrap
      self._bootstrap_inner()
    File "C:\Program Files\Python310\lib\threading.py", line 1016, in _bootstrap_inner
      self.run()
    File "C:\Program Files\Python310\lib\threading.py", line 953, in run
      self._target(*self._args, **self._kwargs)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 295, in _run_script_thread
      self._run_script(request.rerun_data)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 556, in _run_script
      exec(code, module.__dict__)
    File "C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\sudoku-solver\app.py", line 27, in <module>
      grid = numberExtract.extract(image)
    File "C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\sudoku-solver\cv\numberExtract.py", line 31, in extract
      grid[i][j] = predictNumber(image)
    File "C:\Users\kvnka\OneDrive - Trinity College Dublin\GitHub\sudoku-solver\cv\numberExtract.py", line 20, in predictNumber
      loadedModelPred = numpy.argmax(loadedModel.predict(imageResizeCopy), axis=1)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 2344, in predict
      tmp_batch_outputs = self.predict_function(iterator)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 2131, in predict_function
      return step_function(self, iterator)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 2117, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 2105, in run_step
ras\engine\sequential.py", line 412, in call
      return super().call(inputs, training=training, mask=mask)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\engine\functional.py", line 510, in call
      return self._run_internal_graph(inputs, training=training, mask=mask)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\engine\functional.py", line 667, in _run_interras\engine\sequential.py", line 412, in call
      return super().call(inputs, training=training, mask=mask)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\engine\functional.py", line 510, in call            return self._run_internal_graph(inputs, training=training, mask=mask)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\engine\functional.py", line 667, in _run_internal_graph
      outputs = node.layer(*args, **kwargs)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\engine\base_layer.py", line 1107, in __call__       outputs = call_fn(inputs, *args, **kwargs)
    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 96, in error_handler
      return fn(*args, **kwargs)    File "C:\Users\kvnka\AppData\Roaming\Python\Python310\site-packages\keras\layers\pooling\base_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]

I'm a bit clueless about what's causing this error.

1 Answers

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).

Related