Keras微调InceptionV3张量尺寸误差 [英] Keras fine-tuning InceptionV3 tensor dimension error

查看:174
本文介绍了Keras微调InceptionV3张量尺寸误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to fine-tune 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)

但是我得到了错误:

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


推荐答案

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

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)

这篇关于Keras微调InceptionV3张量尺寸误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆