简体   繁体   English

ValueError:Layer 需要 2 个输入,但在训练 CNN 时收到 1 个输入张量

[英]ValueError: Layer expects 2 input(s), but it received 1 input tensors when training a CNN

I am new to tensorflow , trying to build a Siamese CNN similar to what's done in this guide .我是tensorflow的新手,试图构建一个类似于本指南中所做的 Siamese CNN。
My model is built using a base model, which is then fed twice with two different pictures that go through the same network.我的 model 是使用基础 model 构建的,然后通过同一网络将两张不同的图片输入两次,即 go。
This is the code for building the network:这是构建网络的代码:

class BaseModel(Model):

  def __init__(self, base_network):
    super(BaseModel, self).__init__()
    self.network = base_network
  
  def call(self, inputs):
    print(inputs)
    return self.network(inputs)

def get_base_model():
  inputs = tf.keras.Input(shape=INPUT)

  conv2d_1 = layers.Conv2D(name='seq_1', filters=64, 
            kernel_size=20, 
            activation='relu')(inputs)
  maxpool_1 = layers.MaxPooling2D(pool_size=(2, 2))(conv2d_1)

  conv2d_2 = layers.Conv2D(filters=128, 
            kernel_size=20, 
            activation='relu')(maxpool_1)
  maxpool_2 = layers.MaxPooling2D(pool_size=(2, 2))(conv2d_2)

  conv2d_3 = layers.Conv2D(filters=128, 
            kernel_size=20, 
            activation='relu')(maxpool_2)
  maxpool_3 = layers.MaxPooling2D(pool_size=(2, 2))(conv2d_3)

  conv2d_4 = layers.Conv2D(filters=256, 
            kernel_size=10, 
            activation='relu')(maxpool_3)

  flatten_1 = layers.Flatten()(conv2d_4)
  outputs = layers.Dense(units=4096,
                        activation='sigmoid')(flatten_1)
  
  model = Model(inputs=inputs, outputs=outputs)

  return model

Then, I'm building the Siamese network using the previous method like that:然后,我正在使用之前的方法构建连体网络:

INPUT = (250, 250, 3)

def get_siamese_model():
  left_input = layers.Input(name='img1', shape=INPUT)
  right_input = layers.Input(name='img2', shape=INPUT)
  
  base_model = get_base_model()
  base_model = BaseModel(base_model)

  # bind the two input layers to the base network
  left = base_model(left_input)
  right = base_model(right_input)

  # build distance measuring layer
  l1_lambda = layers.Lambda(lambda tensors:abs(tensors[0] - tensors[1]))
  l1_dist = l1_lambda([left, right])

  pred = layers.Dense(1,activation='sigmoid')(l1_dist)

  return Model(inputs=[left_input, right_input], outputs=pred)

class SiameseNetwork(Model):

  def __init__(self, siamese_network):
    super(SiameseNetwork, self).__init__()
    self.siamese_network = siamese_network
  
  def call(self, inputs):
    print(inputs)
    return self.siamese_network(inputs)

I'm then training the network by passing a tf.data.Dataset to it:然后我通过将tf.data.Dataset传递给它来训练网络:

net.fit(x=train_dataset, epochs=10 ,verbose=True)

train_dataset is of type: train_dataset的类型为:

<PrefetchDataset shapes: ((None, 250, 250, 3), (None, 250, 250, 3)), types: (tf.float32, tf.float32)> <PrefetchDataset 形状:((None, 250, 250, 3), (None, 250, 250, 3)),类型:(tf.float32, tf.float32)>

It seems like the shape of the input is defined well, but I'm still encountering an error:似乎输入的形状定义得很好,但我仍然遇到错误:

ValueError                                Traceback (most recent call last)
<ipython-input-144-6c5586e1e205> in <module>()
----> 1 net.fit(x=train_dataset, epochs=10 ,verbose=True)

9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1098                 _r=1):
   1099               callbacks.on_train_batch_begin(step)
-> 1100               tmp_logs = self.train_function(iterator)
   1101               if data_handler.should_sync:
   1102                 context.async_wait()

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
    826     tracing_count = self.experimental_get_tracing_count()
    827     with trace.Trace(self._name) as tm:
--> 828       result = self._call(*args, **kwds)
    829       compiler = "xla" if self._experimental_compile else "nonXla"
    830       new_tracing_count = self.experimental_get_tracing_count()

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    869       # This is the first call of __call__, so we have to initialize.
    870       initializers = []
--> 871       self._initialize(args, kwds, add_initializers_to=initializers)
    872     finally:
    873       # At this point we know that the initialization is complete (or less

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _initialize(self, args, kwds, add_initializers_to)
    724     self._concrete_stateful_fn = (
    725         self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
--> 726             *args, **kwds))
    727 
    728     def invalid_creator_scope(*unused_args, **unused_kwds):

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)
   2967       args, kwargs = None, None
   2968     with self._lock:
-> 2969       graph_function, _ = self._maybe_define_function(args, kwargs)
   2970     return graph_function
   2971 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   3359 
   3360           self._function_cache.missed.add(call_context_key)
-> 3361           graph_function = self._create_graph_function(args, kwargs)
   3362           self._function_cache.primary[cache_key] = graph_function
   3363 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)
   3204             arg_names=arg_names,
   3205             override_flat_arg_shapes=override_flat_arg_shapes,
-> 3206             capture_by_value=self._capture_by_value),
   3207         self._function_attributes,
   3208         function_spec=self.function_spec,

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    988         _, original_func = tf_decorator.unwrap(python_func)
    989 
--> 990       func_outputs = python_func(*func_args, **func_kwargs)
    991 
    992       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in wrapped_fn(*args, **kwds)
    632             xla_context.Exit()
    633         else:
--> 634           out = weak_wrapped_fn().__wrapped__(*args, **kwds)
    635         return out
    636 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    975           except Exception as e:  # pylint:disable=broad-except
    976             if hasattr(e, "ag_error_metadata"):
--> 977               raise e.ag_error_metadata.to_exception(e)
    978             else:
    979               raise

ValueError: in user code:

    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
        return step_function(self, iterator)
    <ipython-input-125-de3a74f810c3>:9 call  *
        return self.siamese_network(inputs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__  **
        input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py:207 assert_input_compatibility
        ' input tensors. Inputs received: ' + str(inputs))

    ValueError: Layer model_16 expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 250, 250, 3) dtype=float32>]

I do undertand that model_16 is the BaseModel, however I can't figure out what am I doing wrong here.我确实知道model_16是BaseModel,但是我不知道我在这里做错了什么。

Following the comment, here is a possible solution with only functional API.在评论之后,这是一个可能的解决方案,只有功能 API。 Note that, you should note use activation sigmoid the embedding model ( get_base_model ).请注意,您应该注意使用嵌入 model ( get_base_model ) 的激活sigmoid

# base model 
def get_base_model():
    inputs = tf.keras.Input(shape=INPUT)
    
    conv2d_1 = layers.Conv2D(name='seq_1', filters=64, 
            kernel_size=20, 
            activation='relu')(inputs)
    maxpool_1 = layers.MaxPooling2D(pool_size=(2, 2))(conv2d_1)

    conv2d_2 = layers.Conv2D(filters=128, 
            kernel_size=20, 
            activation='relu')(maxpool_1)
    maxpool_2 = layers.MaxPooling2D(pool_size=(2, 2))(conv2d_2)

    conv2d_3 = layers.Conv2D(filters=128, 
            kernel_size=20, 
            activation='relu')(maxpool_2)
    maxpool_3 = layers.MaxPooling2D(pool_size=(2, 2))(conv2d_3)

    conv2d_4 = layers.Conv2D(filters=256, 
            kernel_size=10, 
            activation='relu')(maxpool_3)

    flatten_1 = layers.Flatten()(conv2d_4)
    outputs = layers.Dense(units=4096)(flatten_1)
    
    model = Model(inputs=inputs, outputs=outputs)
    return model

Siamesenetwork连体网

INPUT = (250, 250, 3)

def get_siamese_model():
    # two input 
    left_input  = layers.Input(name='img1', shape=INPUT)
    right_input = layers.Input(name='img2', shape=INPUT)

    # one model
    base_model = get_base_model()

    # bind the two input layers to the base network
    left  = base_model(left_input)
    right = base_model(right_input)

    # build distance measuring layer
    l1_lambda = layers.Lambda(lambda tensors:abs(tensors[0] - tensors[1]))
    l1_dist   = l1_lambda([left, right])

    pred = layers.Dense(1,activation='sigmoid')(l1_dist)
    return Model(inputs=[left_input, right_input], outputs=pred)

Build and check构建和检查

net = get_siamese_model()
# net.summary()
# tf.keras.utils.plot_model(net)

Test测试

import numpy as np 

A2_i = np.random.randint(0, 256, size=(2, 250, 250, 3)).astype("float32")
A2_j = np.random.randint(0, 256, size=(2, 250, 250, 3)).astype("float32")

net([A2_i, A2_j]).numpy()
array([[0.4786834],
       [0.484886 ]], dtype=float32)

I have figured out the problem.我已经弄清楚了问题所在。 When passing tf.data.Dataset as x to tensorflow 's default fit method, it expects to receive both the input and the target in that same Dataset .当将tf.data.Dataset作为x传递给tensorflow的默认fit方法时,它希望同一个Dataset中接收输入目标。
Therefore, when passing a dataset with two input images, the first one was passed to the actual network and the second one was left out and treated as the true_y (target) value.因此,当传递具有两个输入图像的数据集时,第一个被传递到实际网络,第二个被忽略并被视为true_y (目标)值。

The fix in that case, where the network expects n inputs, is to have a dataset in which every entry is of length 2 , where the first is a tuple of length n representing the input to the network, and the second value is the true_y , eg 0 or 1 in a binary classification task.在这种情况下,网络需要n输入的解决方法是拥有一个数据集,其中每个条目的长度为2 ,其中第一个是长度为ntuple ,表示网络的输入,第二个值是true_y ,例如二进制分类任务中的0 or 1

The explanation above goes down in my case to the following structure of train_dataset , validation_dataset and test_dataset .在我的例子中,上面的解释归结为train_datasetvalidation_datasettest_dataset的以下结构。

<PrefetchDataset shapes: (((None, 250, 250, 3), (None, 250, 250, 3)), (None,)), types: ((tf.float32, tf.float32), tf.int32)> <PrefetchDataset 形状:(((None, 250, 250, 3), (None, 250, 250, 3)), (None,)),类型:((tf.float32, tf.float32), tf.int32) >

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:层顺序需要 1 个输入,但它收到 239 个输入张量 - ValueError: Layer sequential expects 1 input(s), but it received 239 input tensors ValueError:model 层需要 21 个输入,但它接收到 1 个输入张量 - ValueError: Layer model expects 21 input(s), but it received 1 input tensors ValueError:层鉴别器需要 1 个输入,但它收到 2 个输入张量 - ValueError: Layer Discriminator expects 1 input(s), but it received 2 input tensors ValueError:层顺序_16 需要 1 个输入,但它收到 8 个输入张量 - ValueError: Layer sequential_16 expects 1 input(s), but it received 8 input tensors 尝试在 Tensorflow 中组合数字和文本特征:ValueError:层模型需要 2 个输入,但它接收到 1 个输入张量 - Attempting to Combine Numeric and Text Features in Tensorflow: ValueError: Layer model expects 2 input(s), but it received 1 input tensors ValueError:层顺序需要 1 个输入,但它接收到 250 个输入张量 - ValueError: Layer sequential expects 1 inputs, but it received 250 input tensors ValueError:层 sequential_20 需要 1 个输入,但它收到了 2 个输入张量 - ValueError: Layer sequential_20 expects 1 inputs, but it received 2 input tensors 我的输入层需要是什么形状? 我不断收到 ValueError:层“模型”需要 1 个输入,但它收到了 2 个输入张量 - What shape does my Input layer need to be? I keep getting ValueError: Layer "model" expects 1 input(s), but it received 2 input tensors Tensorflow &amp; Keras 层“顺序”需要 1 个输入,但它接收到 2 个输入张量 - Tensorflow & Keras Layer "sequential" expects 1 input(s), but it received 2 input tensors 口罩识别。 错误 - “ValueError:层“model_1”需要 1 个输入,但它接收到 3 个输入张量”,因为帧中出现了多个面 - Mask Recognition. Error - "ValueError: Layer "model_1" expects 1 input(s), but it received 3 input tensors" as more than one face appears in frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM