【发布时间】:2019-02-03 13:05:59
【问题描述】:
我正在使用迁移学习来识别物体。我使用经过训练的 VGG16 模型作为基础模型,并使用 Keras 在其上添加了我的分类器。然后我根据我的数据训练模型,模型运行良好。我想查看模型的中间层为给定数据生成的特征。为此,我使用了以下代码:
def ModeloutputAtthisLayer(model, layernme, imgnme, width, height):
layer_name = layernme
intermediate_layer_model = Model(inputs=model.input,
outputs=model.get_layer(layer_name).output)
img = image.load_img(imgnme, target_size=(width, height))
imageArray = image.img_to_array(img)
image_batch = np.expand_dims(imageArray, axis=0)
processed_image = preprocess_input(image_batch.copy())
intermediate_output = intermediate_layer_model.predict(processed_image)
print("outshape of ", layernme, "is ", intermediate_output.shape)
在代码中,我使用np.expand_dims 为批次添加了一个额外维度,因为网络的输入矩阵应采用(batchsize, height, width, channels) 的形式。这段代码工作正常。特征向量的形状为1, 224, 224, 64。
现在我希望将其显示为图像,为此我知道有一个附加维度作为批处理添加,因此我应该将其删除。在此之后,我使用了以下代码行:
imge = np.squeeze(intermediate_output, axis=0)
plt.imshow(imge)
但是它会抛出一个错误:
“图像数据的尺寸无效”
我想知道如何将提取的特征向量显示为图像。请有任何建议。
【问题讨论】:
标签: python numpy keras feature-extraction keras-layer