如何使用python中的后端张量流访问在keras中生成的特征图模型?

时间:2019-05-07 06:06:48

标签: python tensorflow keras

如何访问模型中每个卷积层中的所有特征图?我使用keras作为后端tensorflow的API。以下是我的代码的主要问题:

idx.imshow(featureMaps[0, :, :, i], cmap=’viridis’) 

我被困在这里已经很久了!

outputs = [self.model.layers[i].output for i in index]
self.model = Model(inputs=self.model.inputs, outputs=outputs)
featureMaps = self.model.predict(self.testImage)

print((np.shape(featureMaps[0][0])))
print(“FeatureMapsLen: “+str(np.shape(featureMaps[0]))[2])
numOfFeaturemaps = (np.shape(featureMaps[0][0]))[2]

print(“numOfFeatureMaps: “+str(numOfFeaturemaps)

fig=plt.figure(figsize=(16,16)) 
subplotNum=int(np.ceil(np.sqrt(numOfFeaturemaps)))
for i in range(int(numOfFeaturemaps)):
idx = fig.add_subplot(subplotNum, subplotNum, i+1)
idx.imshow(featureMaps[0, :, :, i], cmap=’viridis’) 
plt.xticks(np.array([]))
plt.yticks(np.array([]))
plt.tight_layout()

plt.savefig(“featureMaps/featuremaps@Layer{}”.format(self.layerNum) + ‘.png’)
outputImg = QtGui.QPixmap(“featureMaps/featuremaps@Layer{}”.format(self.layerNum) + ‘.png’)
self.ui.labelImageContainer.setScaledContents(False)
self.ui.labelImageContainer.setPixmap(outputImg)

0 个答案:

没有答案