qubvel / segmentation_models

Segmentation models with pretrained backbones. Keras and TensorFlow Keras.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prediction on test set is fine, failed on single image prediction

ttkrpink opened this issue · comments

I followed the sample all the way to the end. Right after the last prediction example. I add the following line. I tried other methods of reading the image file. The results are the same.

Thanks for your help

import PIL, cv2, imageio
filename = "./data/CamVid/test/0001TP_008580.png"
img = imageio.imread(filename) # novel image
img = preprocess_input(img) # use the preprocessing input method 
image = np.expand_dims(img, axis=0)
pr_mask = model.predict(image)

This section of code generate an error message:

image

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
d:\PythonProjects\AI_Segmentation_Models\example.ipynb Cell 22 in <cell line: 8>()
      [6](vscode-notebook-cell:/d%3A/PythonProjects/AI_Segmentation_Models/example.ipynb#X30sZmlsZQ%3D%3D?line=5) img = preprocess_input(img) # use the preprocessing input method 
      [7](vscode-notebook-cell:/d%3A/PythonProjects/AI_Segmentation_Models/example.ipynb#X30sZmlsZQ%3D%3D?line=6) image = np.expand_dims(img, axis=0)
----> [8](vscode-notebook-cell:/d%3A/PythonProjects/AI_Segmentation_Models/example.ipynb#X30sZmlsZQ%3D%3D?line=7) pr_mask = model.predict(image)

File d:\python38\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     65 except Exception as e:  # pylint: disable=broad-except
     66   filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67   raise e.with_traceback(filtered_tb) from None
     68 finally:
     69   del filtered_tb

File d:\python38\lib\site-packages\tensorflow\python\eager\execute.py:54, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     52 try:
     53   ctx.ensure_initialized()
---> 54   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     55                                       inputs, attrs, num_outputs)
     56 except core._NotOkStatusException as e:
     57   if name is not None:

InvalidArgumentError: Graph execution error:

Detected at node 'model_1/decoder_stage0_concat/concat' defined at (most recent call last):
    File "C:\Users\chao\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File "C:\Users\chao\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
      exec(code, run_globals)
    File "d:\python38\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "d:\python38\lib\site-packages\traitlets\config\application.py", line 846, in launch_instance
      app.start()
    File "d:\python38\lib\site-packages\ipykernel\kernelapp.py", line 677, in start
      self.io_loop.start()
    File "d:\python38\lib\site-packages\tornado\platform\asyncio.py", line 199, in start
      self.asyncio_loop.run_forever()
    File "C:\Users\chao\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 570, in run_forever
      self._run_once()
    File "C:\Users\chao\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 1859, in _run_once
      handle._run()
    File "C:\Users\chao\AppData\Local\Programs\Python\Python38\lib\asyncio\events.py", line 81, in _run
      self._context.run(self._callback, *self._args)
    File "d:\python38\lib\site-packages\ipykernel\kernelbase.py", line 473, in dispatch_queue
      await self.process_one()
    File "d:\python38\lib\site-packages\ipykernel\kernelbase.py", line 462, in process_one
      await dispatch(*args)
    File "d:\python38\lib\site-packages\ipykernel\kernelbase.py", line 369, in dispatch_shell
      await result
    File "d:\python38\lib\site-packages\ipykernel\kernelbase.py", line 664, in execute_request
      reply_content = await reply_content
    File "d:\python38\lib\site-packages\ipykernel\ipkernel.py", line 355, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "d:\python38\lib\site-packages\ipykernel\zmqshell.py", line 532, in run_cell
      return super().run_cell(*args, **kwargs)
    File "d:\python38\lib\site-packages\IPython\core\interactiveshell.py", line 2854, in run_cell
      result = self._run_cell(
    File "d:\python38\lib\site-packages\IPython\core\interactiveshell.py", line 2900, in _run_cell
      return runner(coro)
    File "d:\python38\lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
      coro.send(None)
    File "d:\python38\lib\site-packages\IPython\core\interactiveshell.py", line 3098, in run_cell_async
      has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
    File "d:\python38\lib\site-packages\IPython\core\interactiveshell.py", line 3301, in run_ast_nodes
      if await self.run_code(code, result, async_=asy):
    File "d:\python38\lib\site-packages\IPython\core\interactiveshell.py", line 3361, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "C:\Users\chao\AppData\Local\Temp\ipykernel_25708\419308408.py", line 8, in <cell line: 8>
      pr_mask = model.predict(image)
    File "d:\python38\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "d:\python38\lib\site-packages\keras\engine\training.py", line 2033, in predict
      tmp_batch_outputs = self.predict_function(iterator)
    File "d:\python38\lib\site-packages\keras\engine\training.py", line 1845, in predict_function
      return step_function(self, iterator)
    File "d:\python38\lib\site-packages\keras\engine\training.py", line 1834, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "d:\python38\lib\site-packages\keras\engine\training.py", line 1823, in run_step
      outputs = model.predict_step(data)
    File "d:\python38\lib\site-packages\keras\engine\training.py", line 1791, in predict_step
      return self(x, training=False)
    File "d:\python38\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "d:\python38\lib\site-packages\keras\engine\training.py", line 490, in __call__
      return super().__call__(*args, **kwargs)
    File "d:\python38\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "d:\python38\lib\site-packages\keras\engine\base_layer.py", line 1014, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "d:\python38\lib\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
      return fn(*args, **kwargs)
    File "d:\python38\lib\site-packages\keras\engine\functional.py", line 458, in call
      return self._run_internal_graph(
    File "d:\python38\lib\site-packages\keras\engine\functional.py", line 596, in _run_internal_graph
      outputs = node.layer(*args, **kwargs)
    File "d:\python38\lib\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "d:\python38\lib\site-packages\keras\engine\base_layer.py", line 1014, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "d:\python38\lib\site-packages\keras\utils\traceback_utils.py", line 92, in error_handler
      return fn(*args, **kwargs)
    File "d:\python38\lib\site-packages\keras\layers\merging\base_merge.py", line 178, in call
      return self._merge_function(inputs)
    File "d:\python38\lib\site-packages\keras\layers\merging\concatenate.py", line 126, in _merge_function
      return backend.concatenate(inputs, axis=self.axis)
    File "d:\python38\lib\site-packages\keras\backend.py", line 3313, in concatenate
      return tf.concat([to_dense(x) for x in tensors], axis)
Node: 'model_1/decoder_stage0_concat/concat'
ConcatOp : Dimension 2 in both shapes must be equal: shape[0] = [1,2048,24,30] vs. shape[1] = [1,1024,23,30]
	 [[{{node model_1/decoder_stage0_concat/concat}}]] [Op:__inference_predict_function_26153]

Figured out the problem.
Dataset class has cv2 function after imread, you have to do the same operation for the single image prediction.