简体   繁体   中英

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

Please help me get out of this error while building a model for face comparison/similarity check. I am getting X_val.shape, y_val.shape, X_train.shape, y_train.shape (2, 2, 32, 32, 1) (2,) (40, 2, 32, 32, 1) (40,) which are as expected still I am getting this error. I got the code from this link 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)

Tensorflow and keras can work in 2 different ways: channel first and channel last, assuming that you are working with 32x32 images the "1" is the number of channels (i am not talking about the first size, that is the batch size that you put to 1).

What is happening is that your model is expecting a channel last image but you are giving him a channel first image.

Reshape the images or update the model.

I built model using 'channel last' input, also I was giving same type of input 'channel last'... Problem was with 'train' which was converting 'channel last' into 'channel first' before feeding to model.

I corrected train method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3) ValueError: Input 0 of layer "sequential_32" is incompatible with the layer: expected shape=(None, 3, 1), found shape=(32, 0, 1) ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(32, 128, 3) ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 32, 32] Input 0 of layer "lstm" is incompatible with the layer: expected shape=(1, None, 1), found shape=(32, 9, 1) ValueError: Input 0 of layer max_pooling1d is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 128, 1, 32) ValueError: Input 0 of layer sequential_32 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 256] ValueError: Input 0 of layer "model_1" is incompatible with the layer: expected shape=(None, 224, 224, 3), found shape=(None, 290, 290, 3) ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [32, 64, 3] nput 0 of layer "sequential_14" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 32, 32, 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM