【问题标题】:Keras fine-tuning InceptionV3 tensor dimension errorKeras微调InceptionV3张量维度误差
【发布时间】:2019-04-18 02:48:55
【问题描述】:

我正在尝试在 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

【问题讨论】:

  • 你给这个模型的输入数据是什么形状的?

标签: python machine-learning keras deep-learning


【解决方案1】:

shouldn't use pop() 方法在使用功能 API 创建的模型上(即 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2018-01-06
    • 2020-09-15
    相关资源
    最近更新 更多