首页 > 解决方案 > 提取cnn的输出

问题描述

我已经训练了一个 cnn 模型来对狗和猫的图像进行分类,它给出了 98% 的准确率但是我想可视化 cnn 层的输出,即我的 cnn 预测它是狗还是猫的特征如果有的话可视化 cnn 的输出?

标签: pythonkerasconv-neural-network

解决方案


您可以将模型分为两个模型:

以前的型号:

input = Input(...)

# Your Layers
output = Dense(1)
old_model = Model(inputs=[input], output)

新模式:

input = Input(...)

#Add the first layers and the CNN here
cnn_layer = Conv2D(...)
feature_extraction_model = Model(inputs=[input], outputs=cnn_layer)

input_cnn = Input(...) # The shape of your CNN output

# Add the classification layer here
output = Dense(1)

classifier_model = Model(inputs=[input_cnn], outputs=output)

现在您将新模型定义为以下组合:feature_extraction_modelclassifier_model

new_model = Model(inputs=[input], outputs=classifier_model(input_cnn))

# Train the model
new_model.fit(x, y)

现在您可以访问 CNNlayer 后期培训:

cnn_output = feature_extraction_model.predict(x)

推荐阅读