Keras finetunning InceptionV3 tensor dimension error

社会主义新天地 提交于 2019-12-11 16:56:22

问题


I am trying to finetune a model in 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)

But I get the error:

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

回答1:


You shouldn't use pop() method on models created using functional API (i.e. keras.models.Model). Only Sequential models (i.e. keras.models.Sequential) have a built-in pop() method (usage: model.pop()). Instead, use index or the names of the layers to access a specific layer:

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


来源:https://stackoverflow.com/questions/53312025/keras-finetunning-inceptionv3-tensor-dimension-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!