Keras finetunning InceptionV3 ошибка измерения тензора - PullRequest
0 голосов
/ 15 ноября 2018

Я пытаюсь настроить модель в Керасе:

    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 голосов
/ 15 ноября 2018

Вы не должны использовать метод 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)
...