将模型训练层中提取的特征向量显示为图像
Display extracted feature vector from trained layer of the model as an image
我正在使用迁移学习来识别对象。我使用经过训练的 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)
但是它抛出一个错误:
"Invalid dimensions for image data"
我想知道如何将提取的特征向量显示为图像。请有任何建议。
你的特征形状是(1,224,224,64)
,你不能直接绘制64
通道图像。您可以做的是独立绘制各个通道,如下所示
imge = np.squeeze(intermediate_output, axis=0)
filters = imge.shape[2]
plt.figure(1, figsize=(32, 32)) # plot image of size (32x32)
n_columns = 8
n_rows = math.ceil(filters / n_columns) + 1
for i in range(filters):
plt.subplot(n_rows, n_columns, i+1)
plt.title('Filter ' + str(i))
plt.imshow(imge[:,:,i], interpolation="nearest", cmap="gray")
这将在 8
行和 8
列中绘制 64
个图像。
一种可能的方法是通过像这样的加权和将 64 个通道组合成一个单通道图像:
weighted_imge = np.sum(imge*weights, axis=-1)
其中weights
是一个有64个加权系数的数组。
如果您希望为所有通道赋予相同的权重,您可以简单地计算平均值:
weighted_imge = np.mean(imge, axis=-1)
演示
import numpy as np
import matplotlib.pyplot as plt
intermediate_output = np.random.randint(size=(1, 224, 224, 64),
low=0, high=2**8, dtype=np.uint8)
imge = np.squeeze(intermediate_output, axis=0)
weights = np.random.random(size=(imge.shape[-1],))
weighted_imge = np.sum(imge*weights, axis=-1)
plt.imshow(weighted_imge)
plt.colorbar()
In [33]: intermediate_output.shape
Out[33]: (1, 224, 224, 64)
In [34]: imge.shape
Out[34]: (224, 224, 64)
In [35]: weights.shape
Out[35]: (64,)
In [36]: weighted_imge.shape
Out[36]: (224, 224)
我正在使用迁移学习来识别对象。我使用经过训练的 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)
但是它抛出一个错误:
"Invalid dimensions for image data"
我想知道如何将提取的特征向量显示为图像。请有任何建议。
你的特征形状是(1,224,224,64)
,你不能直接绘制64
通道图像。您可以做的是独立绘制各个通道,如下所示
imge = np.squeeze(intermediate_output, axis=0)
filters = imge.shape[2]
plt.figure(1, figsize=(32, 32)) # plot image of size (32x32)
n_columns = 8
n_rows = math.ceil(filters / n_columns) + 1
for i in range(filters):
plt.subplot(n_rows, n_columns, i+1)
plt.title('Filter ' + str(i))
plt.imshow(imge[:,:,i], interpolation="nearest", cmap="gray")
这将在 8
行和 8
列中绘制 64
个图像。
一种可能的方法是通过像这样的加权和将 64 个通道组合成一个单通道图像:
weighted_imge = np.sum(imge*weights, axis=-1)
其中weights
是一个有64个加权系数的数组。
如果您希望为所有通道赋予相同的权重,您可以简单地计算平均值:
weighted_imge = np.mean(imge, axis=-1)
演示
import numpy as np
import matplotlib.pyplot as plt
intermediate_output = np.random.randint(size=(1, 224, 224, 64),
low=0, high=2**8, dtype=np.uint8)
imge = np.squeeze(intermediate_output, axis=0)
weights = np.random.random(size=(imge.shape[-1],))
weighted_imge = np.sum(imge*weights, axis=-1)
plt.imshow(weighted_imge)
plt.colorbar()
In [33]: intermediate_output.shape
Out[33]: (1, 224, 224, 64)
In [34]: imge.shape
Out[34]: (224, 224, 64)
In [35]: weights.shape
Out[35]: (64,)
In [36]: weighted_imge.shape
Out[36]: (224, 224)