在 Keras 中使用预训练的 CNN 模型从图像中提取特征

Extract features from images with a pre-trained CNN model in Keras

我有一个简单的 CNN 模型,如下所示:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 64)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 12, 12, 64)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 9216)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               1179776   
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1290      
=================================================================
Total params: 1,199,882.0
Trainable params: 1,199,882.0
Non-trainable params: 0.0
_________________________________________________________________

我弹出 dense_2(softmax 层)和 dropout_2 层以从图像中提取特征:

(我正在使用此处建议的自定义弹出函数:https://github.com/fchollet/keras/issues/2640

def pop_layer(model):
    if not model.outputs:
        raise Exception('Sequential model cannot be popped: model is empty.')

    model.layers.pop()
    if not model.layers:
        model.outputs = []
        model.inbound_nodes = []
        model.outbound_nodes = []
    else:
        model.layers[-1].outbound_nodes = []
        model.outputs = [model.layers[-1].output]
    model.built = False

弹出最后两层:

pop_layer(model)
pop_layer(model)

之后做 model.summary():

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320       
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 24, 24, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 64)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 12, 12, 64)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 9216)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               1179776   
=================================================================
Total params: 1,198,592.0
Trainable params: 1,198,592.0
Non-trainable params: 0.0
_________________________________________________________________

最后两层已从模型中弹出,但是当我进行预测时:

predictions = model.predict(x_test)
print(len(predictions[0]))

10

如您所见,输出仍然是 softmax,是我做错了什么吗?

谢谢!!

事实证明,您需要 buildcompilepop 层之后建模才能使模型正常工作。