【发布时间】: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