Keras Merge_layer直接从文件夹加载输入,其中datagenerator来自目录方法。

时间:2017-04-28 18:56:43

标签: python numpy keras

是否有办法使用带有flow_from_directory的datagenerator将数据从目录加载到包含2个输入张量的合并层。

   x = merge([base_x, base_y], mode='concat', concat_axis=1)
   x = Dense(256, activation='relu', name="fc-1")(x)  
   x = Dropout(0.5)(x)
   predictions = Dense(2, activation='softmax', name='predictions')(x)
   model = Model(input=[base_x.input, base_y.input], output=predictions)

   # note that it is necessary to start with a fully-trained
   # classifier, including the top classifier, 
   # in order to successfully do fine-tuning


   # add the model on top of the convolutional base

  print('Model loaded.')
   # compile the model with a SGD/momentum optimizer
     # and a very slow learning rate.
        model.compile(loss="categorical_crossentropy",
          optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
          metrics=['accuracy'])
       model.summary()

    # prepare data augmentation configuration
       train_datagen = ImageDataGenerator(
        rescale=1. / 255,
          )

    test_datagen = ImageDataGenerator(rescale=1. / 255)

   train_generator = train_datagen.flow_from_directory(
      train_data_dir,
       target_size=(img_height, img_width),
        batch_size=32,
       class_mode='categorical')

    validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_height, img_width),
       batch_size=32,
      class_mode='categorical')

   model.fit_generator(
       train_generator,
         samples_per_epoch=train_generator.nb_sample,
      nb_epoch=150,
      validation_data=validation_generator,
        nb_val_samples=3000)

文件夹的结构是

  train_dir --
            --> category 1
            --> category 2 
  Test_dir --
           --> category 1
           --> category 2

我想运行此模型直接从目录中读取数据。但我得到的例外是,

     ValueError: The model expects 2 input arrays, but only received one               array. Found: array with shape (32, 224, 224, 3

如何直接从目录中添加2个输入数组。我们不想将输入转换成Numpy数组。我应该从目录本身读取输入。有没有办法做到这一点?

0 个答案:

没有答案