Keras优化运行InceptionV3张量尺寸错误

时间:2018-11-15 03:30:52

标签: python machine-learning keras deep-learning finetunning

我正在尝试微调Keras中的模型:

    inception_model = InceptionV3(weights=None, include_top=False, input_shape=(150, 
150, 1))

    x = inception_model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(256, activation='relu', name='fc1')(x)
    x = Dropout(0.5)(x)
    predictions = Dense(10, activation='softmax', name='predictions')(x)
    classifier = Model(inception_model.input, predictions)


    ####training training training ... save weights


    classifier.load_weights("saved_weights.h5")

    classifier.layers.pop()
    classifier.layers.pop()
    classifier.layers.pop()
    classifier.layers.pop()
    ###enough poping to reach standard InceptionV3 

    x = classifier.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(256, activation='relu', name='fc1')(x)
    x = Dropout(0.5)(x)
    predictions = Dense(10, activation='softmax', name='predictions')(x)
    classifier = Model(classifier.input, predictions)

但是我得到了错误:

ValueError: Input 0 is incompatible with layer global_average_pooling2d_3: expected ndim=4, found ndim=2

1 个答案:

答案 0 :(得分:0)

您使用功能性API(即pop())创建的模型上的shouldn't use keras.models.Model方法。仅顺序模型(即keras.models.Sequential)具有内置的pop()方法(用法:model.pop())。而是使用索引或图层名称访问特定图层:

classifier.load_weights("saved_weights.h5")
x = classifier.layers[-5].output   # use index of the layer directly
x = GlobalAveragePooling2D()(x)