1

大家好,提前感谢您的帮助。

我正在尝试为我的图像识别项目实现一个连体网络(第一次),但我无法克服这个错误:

"Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays"

我认为我的数据生成器是问题所在,但我不知道如何解决它。

以下是一些信息:

##################### MODEL

  input_dim = (200, 200, 1)
  img_a = Input(shape = input_dim)
  img_b = Input(shape = input_dim)
  base_net = build_base_network(input_dim)

  features_a = base_net(img_a)
  features_b = base_net(img_b)

  distance = Lambda(euclidean_distance, output_shape = eucl_dist_output_shape)([features_a, features_b])
  model = Model(inputs=[img_a, img_b], outputs=distance)


##################### NETWORK

def build_base_network(input_shape):

  seq = Sequential()

  #Layer_1
  seq.add(Conv2D(96, (11, 11), subsample=(4, 4), input_shape=(input_shape), init='glorot_uniform', dim_ordering='tf'))
  seq.add(Activation('relu'))
  seq.add(BatchNormalization())
  seq.add(MaxPooling2D((3,3), strides=(2, 2)))
  seq.add(Dropout(0.4))

  .
  .
  .
  .

  #Flatten
  seq.add(Flatten())
  seq.add(Dense(1024, activation='relu'))
  seq.add(Dropout(0.5))
  seq.add(Dense(1024, activation='relu'))
  seq.add(Dropout(0.5))

  return seq

##################### LAST PART OF DATAGENERATOR
 .
 .
 .
  if len(Pair_equal) > len(Pair_diff):
    Pair_equal = Pair_equal[0:len(Pair_diff)]
    y_equal = y_equal[0:len(y_diff)]

  elif len(Pair_equal) < len(Pair_diff):
    Pair_diff = Pair_diff[0:len(Pair_equal)]
    y_diff = y_diff[0:len(y_equal)]


  Pair_equal = np.array(Pair_equal) #contains pairs of the same image
  Pair_diff = np.array(Pair_diff) #contains pairs of different images
  y_equal = np.array(y_equal)
  y_diff = np.array(y_diff)

  X = np.concatenate([Pair_equal, Pair_diff], axis=0)
  y = np.concatenate([y_equal, y_diff], axis=0)

  return X, y

##################### SHAPES

(16, 2, 200, 200, 1) --> Pair_equal
(16, 2, 200, 200, 1) --> Pair_diff
(16,) --> y_equal
(16,) --> y_diff
     

如果您需要任何其他要求,我会提供。

4

1 回答 1

1

你可以解决改变你的发电机返回的问题

return [X[:,0,...], X[:,1,...]], y

你它是一个连体网络,需要 2 个输入并产生 1 个输出,但通常这对于所有需要多个输入(或输出)的 keras 模型都有效。

要提供这种模型,您必须为每个输入传递一个数组。在您的情况下,一个 image_a 数组和另一个 image_b 数组。每个数组(在多个输入/输出的情况下)必须放在一个列表中

于 2020-07-25T14:51:17.977 回答