【问题标题】:Extract features from images with a pre-trained CNN model in Keras在 Keras 中使用预训练的 CNN 模型从图像中提取特征
【发布时间】:2017-04-05 19:42:04
【问题描述】:

我有一个简单的 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,是我做错了什么吗?

谢谢!!

【问题讨论】:

  • 你能告诉我们print(predictions.shape)吗?
  • 当然,(10000, 10)。谢谢
  • 你可以尝试两次model.pop() 来代替你的函数吗?
  • 同样的结果,似乎当你在做预测时,keras 使用了完整的模型
  • 弹出后尝试model.build()。

标签: computer-vision keras conv-neural-network feature-extraction


【解决方案1】:

事实证明,您需要在pop 层之后进行build 或compile 模型才能使您的模型正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-14
    • 2016-12-21
    • 2018-05-03
    • 2019-10-05
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多