在深度学习中,如果我们想获得某一个层上的feature map,就像下面的图这样,怎么做呢?
我们的代码是使用keras写的VGG16网络,网络结构如图:
那么我们随便抽取一层的数据吧,就拿第四层的pooling以后的结果作为输出吧,参考上面的网络结构,得到的结果维度应该是[1,56,56,128]的尺度。
怎么做呢?
首先通过keras构建模型:
model = VGG16(include_top=True, weights='imagenet')
然后设置输入和输出为:原始的输入和该层对应的输出,然后使用predict函数得到对应的结果
dense_result = Model(inputs=model.input,outputs=model.get_layer("block2_pool").output)
dense_res = dense_result.predict(x)#使用predict得到该层结果
设置随机数(或者固定的数字)来获取某一层的结果
rand_layer = random.randint(10,128)
x_output = dense_res[0,:,:,rand_layer] #获取某一层的数据:因为原始数据维度是[1,x,x,depths]的,我们仅仅提取某一个depth对应的[x,x]维度的信息
# 获取最大值,然后对该层数据进行归一化之后投影到0-255之间
max = np.max(x_output)