带有自定义数据生成器的暹罗网络输入问题

Input problem with siamese network with customize datagenerator

大家好,在此先感谢您的帮助。

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

"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
     

如果您还需要什么,我会提供。

你可以解决改变你的发电机 return

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

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

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