【问题标题】:Keras:Unable to add Dense layer to VGG16Keras:无法将 Dense 层添加到 VGG16
【发布时间】:2018-02-10 02:13:50
【问题描述】:

我正在尝试微调 vgg16(imagenet 预训练)的最后一个卷积块,并在顶部添加一些密集层。我的代码如下。我无法弄清楚为什么在执行Error when checking target: expected sequential_9 to have shape (None, 11) but got array with shape (4, 1) 时会出现此错误。我的数据集中的类数为 11,批量大小为 4。我是否以某种方式将这两者混合在一起?请帮忙。

def finetune( epochs):

    num_classes = 11
    batch_size = 4
    base_model = VGG16(weights='imagenet', include_top=False, input_shape = (224,224,3))
    print('Model loaded.')
    print(base_model.output_shape[1:])
    top_model = Sequential()  
    top_model.add(Flatten(input_shape=base_model.output_shape[1:])) 
    top_model.add(Dense(512, activation='relu',kernel_regularizer=regularizers.l2(0.01)))
    top_model.add(Dropout(0.25))
    top_model.add(Dense(256, activation='relu', kernel_regularizer=regularizers.l2(0.01)))  
    top_model.add(Dropout(0.25))  
    top_model.add(Dense(num_classes, activation='softmax')) 


    top_model.load_weights('vgg_ft_best.h5')

    # add the model on top of the convolutional base
    #model = Model(inputs= base_model.input, outputs= top_model(base_model.output))

    #base_model.add(top_model)
    #print(base_model.summary())

    new_model = Sequential()
    for l in base_model.layers:
        new_model.add(l)


    # CONCATENATE THE TWO MODELS
    new_model.add(top_model)
    print(new_model.summary())

    # set the first 10 layers (up to the last conv block)
    # to non-trainable (weights will not be updated)
    for layer in new_model.layers[:11]:
        layer.trainable = False

    # prepare data augmentation configuration
    train_datagen = ImageDataGenerator(
        rescale=1. / 255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

    train_data_dir = "./images/train"
    validation_data_dir = "./images/validation"
    test_datagen = ImageDataGenerator(rescale=1. / 255)

    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(224, 224),
        batch_size=batch_size,
        class_mode='binary')

    validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(224, 224),
        batch_size=batch_size,
        class_mode='binary')

    num_train_samples = len(train_generator.filenames) 
    num_validation_samples = len(validation_generator.filenames)
    print(num_validation_samples)
    new_model.compile(loss='categorical_crossentropy',
              optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
              metrics=['accuracy'])
    # fine-tune the model

    new_model.fit_generator(
        train_generator,
        steps_per_epoch=int(num_train_samples/batch_size),
        epochs=epochs,
        validation_data=validation_generator,
        validation_steps = int(num_validation_samples/batch_size))

【问题讨论】:

    标签: machine-learning neural-network keras pre-trained-model


    【解决方案1】:

    问题在于您的数据(目标 = 真实输出、真实标签等)。

    您的目标具有(batch,1) 的形状,而您的具有 11 个类的模型期待(batch,11)

    所以,问题出在您的生成器上。它必须输出 11 个类别的张量。

    为此,请参阅the documentation for flow_from_directory 和突出显示的部分:

    classes:可选的类子目录列表(例如 ['dogs', 'cats'])。默认值:无。如果未提供,类列表将自动从目录下的子目录名称/结构推断,其中每个子目录将被视为不同的类(以及类的顺序,这将映射到标签索引,将是字母数字)。包含从类名到类索引的映射的字典可以通过属性 class_indices 获得。

    class_mode:“分类”、“二进制”、“稀疏”、“输入”或无之一。默认值:“分类”。确定返回的标签数组的类型:“categorical”将是 2D one-hot 编码标签,“binary”将是 1D 二进制标签,“sparse”将是 1D 整数标签,“input”将是与输入图像相同的图像(主要用于自动编码器)。如果为 None,则不返回任何标签(生成器只会产生批量图像数据,这对于使用 model.predict_generator()、model.evaluate_generator() 等很有用)。请注意,在 class_mode None 的情况下,数据仍然需要驻留在 directory 的子目录中才能正常工作。

    解决方案

    • 您需要将图像放置在 11 个不同的文件夹中,每个文件夹属于不同的类。
    • 您需要使用class_mode='categorical' 来获得(batch,11) 格式。

    现在,如果您的类不是分类的(一张图像可以有两个或更多类),那么您需要创建自己的自定义生成器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 2019-01-08
      • 2018-06-16
      • 1970-01-01
      • 2017-07-28
      相关资源
      最近更新 更多