1
我有一个简单CNN模型看起来像这样:特征与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,是我做错了什么?
谢谢!
你能告诉我们'print(predictions.shape)'吗? –
当然,'(10000,10)'。谢谢 – Eric
你可以试试'model.pop()'而不是你的函数吗? –