When I was saving the model, the shape [0] could not be saved normally because it was none

I’m trying to save my model in TF format, but the shape [0] input during saving is none, resulting in my TF Reshape cannot be used, which makes the model unable to be saved normally. Does anyone have any good solutions?

error message:
(None, 8, 8, 512)
Traceback (most recent call last):
File “C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\runpy.py”, line 193, in _run_module_as_main
main”, mod_spec)
File “C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\runpy.py”, line 85, in run_code
exec(code, run_globals)
File "c:\Users\yin.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy\adapter/…/…\debugpy\launcher/…/…\debugpy_main
.py", line 39, in
cli.main()
File “c:\Users\yin.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy\adapter/…/…\debugpy\launcher/…/…\debugpy/…\debugpy\server\cli.py”, line 430, in main
run()
File “c:\Users\yin.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy\adapter/…/…\debugpy\launcher/…/…\debugpy/…\debugpy\server\cli.py”, line 284, in run_file
runpy.run_path(target, run_name=“main”)
File “c:\Users\yin.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py”, line 322, in run_path
pkg_name=pkg_name, script_name=fname)
File “c:\Users\yin.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py”, line 136, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File “c:\Users\yin.vscode\extensions\ms-python.python-2022.14.0\pythonFiles\lib\python\debugpy_vendored\pydevd_pydevd_bundle\pydevd_runpy.py”, line 124, in _run_code
exec(code, run_globals)
File “f:\demopy\yolov5-face-tensorflow\train.py”, line 471, in
train(hyp, opt, wandb)
File “f:\demopy\yolov5-face-tensorflow\train.py”, line 245, in train
model.save(“mask_detector.h5py”, save_format=“tf”)
File “D:\python\python37\site-packages\keras\utils\traceback_utils.py”, line 69, in error_handler
raise e.with_traceback(filtered_tb) from None
File “D:\python\python37\site-packages\tensorflow\python\framework\func_graph.py”, line 1129, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
TypeError: in user code:

File "D:\python\python37\site-packages\keras\saving\saving_utils.py", line 125, in _wrapped_model  *
    outputs = model(*args, **kwargs)
File "D:\python\python37\site-packages\keras\utils\traceback_utils.py", line 69, in error_handler  **
    raise e.with_traceback(filtered_tb) from None

TypeError: Exception encountered when calling layer "model" (type Model).

in user code:

    File "f:\demopy\yolov5-face-tensorflow\models\yolo.py", line 98, in call  *
        return self.forward_once(x,profile)
    File "f:\demopy\yolov5-face-tensorflow\models\yolo.py", line 149, in forward_once  *
        x = m(x)
    File "D:\python\python37\site-packages\keras\utils\traceback_utils.py", line 69, in error_handler  **
        raise e.with_traceback(filtered_tb) from None

    TypeError: Exception encountered when calling layer "detect" (type Detect).

    in user code:

        File "f:\demopy\yolov5-face-tensorflow\models\common.py", line 443, in call  *
            x[i] = tf.reshape(x[i],(bs,self.na, self.no, ny, nx))

        TypeError: Failed to convert elements of (None, 3, 16, 32, 32) to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.


    Call arguments received:
      • x=['tf.Tensor(shape=(None, 32, 32, 48), dtype=float32)', 'tf.Tensor(shape=(None, 16, 16, 256), dtype=float32)', 'tf.Tensor(shape=(None, 8, 

8, 512), dtype=float32)’]

Call arguments received:
  • x=tf.Tensor(shape=(None, 256, 256, 3), dtype=float32)
  • augment=False
  • profile=False

I am sure that I have not configured any none when setting the storage shape

x=tf.Tensor(shape=(None, 256, 256, 3), dtype=float32)

it’s a normal shape of the input tensor, the batch dimension is set to None in order to allow the model to accept inputs of any batch size. You don’t usually try to fix the batch size

I’m not sure what you are trying to achieve though, if you just want to save the model check

You usually don’t have to reshape anything.

OK, I will set shpae [0] in tf.reshape of the last output layer to - 1, and continue to try my study