TensorFlow ValueError: Input 0 is in compatible with layer model_1: expected shape=(None, 32, 32, 1)

Posted

技术标签:

【中文标题】TensorFlow ValueError: Input 0 is in compatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32)【英文标题】:TensorFlow ValueError: Input 0 is incompatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32) 【发布时间】:2022-01-17 15:02:37 【问题描述】:

在构建人脸比较/相似性检查模型时,请帮助我摆脱这个错误。 我正进入(状态 X_val.shape, y_val.shape, X_train.shape, y_train.shape (2, 2, 32, 32, 1) (2,) (40, 2, 32, 32, 1) (40,) 符合预期 我仍然收到此错误。 我从这个链接得到了代码 https://github.com/mrm202/facematch

https://github.com/aleju/face-comparer(python 2.7)

-----------------------
Loading validation dataset...
-----------------------

-----------------------
Loading training dataset...
-----------------------

-----------------------

Loading image contents from hard drive...
X_val.shape, y_val.shape, X_train.shape, y_train.shape
(2, 2, 32, 32, 1) (2,) (40, 2, 32, 32, 1) (40,)
Creating model...
    Compiling model...
    Training...
    Epoch 0
    Traceback (most recent call last):
      File "train.py", line 563, in <module>
        main()
      File "train.py", line 214, in main
        la_plotter, ia_train, ia_val, X_train, y_train, X_val, y_val)
      File "train.py", line 350, in train_loop
        loss, acc = model.train_on_batch(X_batch, Y_batch)
      File "C:\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1825, in train_on_batch
        logs = self.train_function(iterator)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 889, in __call__
        result = self._call(*args, **kwds)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 933, in _call
        self._initialize(args, kwds, add_initializers_to=initializers)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 764, in _initialize
        *args, **kwds))
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3050, in _get_concrete_function_internal_garbage_collected
        graph_function, _ = self._maybe_define_function(args, kwargs)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3444, in _maybe_define_function
        graph_function = self._create_graph_function(args, kwargs)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\function.py", line 3289, in _create_graph_function
        capture_by_value=self._capture_by_value),
      File "C:\Python37\lib\site-packages\tensorflow\python\framework\func_graph.py", line 999, in func_graph_from_py_func
        func_outputs = python_func(*func_args, **func_kwargs)
      File "C:\Python37\lib\site-packages\tensorflow\python\eager\def_function.py", line 672, in wrapped_fn
        out = weak_wrapped_fn().__wrapped__(*args, **kwds)
      File "C:\Python37\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in wrapper
        raise e.ag_error_metadata.to_exception(e)
    ValueError: in user code:

    C:\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:855 train_function  *
        return step_function(self, iterator)
    C:\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:845 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    C:\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1285 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    C:\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2833 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    C:\Python37\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:3608 _call_for_each_replica
        return fn(*args, **kwargs)
    C:\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:838 run_step  **
        outputs = model.train_step(data)
    C:\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py:795 train_step
        y_pred = self(x, training=True)
    C:\Python37\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:1013 __call__
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    C:\Python37\lib\site-packages\tensorflow\python\keras\engine\input_spec.py:270 assert_input_compatibility
        ', found shape=' + display_shape(x.shape))

    ValueError: Input 0 is incompatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32)

【问题讨论】:

【参考方案1】:

Tensorflow 和 keras 可以以两种不同的方式工作:通道优先和通道最后,假设您使用 32x32 图像,“1”是通道数(我不是在谈论第一个尺寸,即批次您放入 1) 的大小。

发生的情况是,您的模型期望的是频道最后一张图片,但您却给了他一张频道第一张图片。

重塑图像或更新模型。

【讨论】:

以上是关于TensorFlow ValueError: Input 0 is in compatible with layer model_1: expected shape=(None, 32, 32, 1)的主要内容,如果未能解决你的问题,请参考以下文章

ValueError: Attempt to reuse RNNCell <tensorflow.contrib.rnn.python.ops.core_rnn_cell_impl.BasicL

TensorFlow:ValueError:形状不兼容

ValueError:没有为任何变量提供梯度 - Tensorflow 2.0/Keras

Tensorflow:ValueError:形状必须排名2但排名3

TensorFlow 中的 ValueError

Tensorflow:ValueError:预期的非整数,得到<dtype:'int32'>