提取inference_model中var_name对应的矩阵特征向量,出错
Created by: taoja12
比如输入的图片转换成特征矩阵后如图,图片是224x224,所以生成224x224x3的矩阵
输出是图片分类的种类1000种,所以结果的输出是1x1000的矩阵特征值
我想在想得到网络中任意一层的这种矩阵特征值输出该如何做? (比如得到第一层卷积层之后的输出,或者某一个全连接层之后的输出)
paddle版本1.7 python 3.6 模型inference_model下载地址 链接: https://pan.baidu.com/s/1Yz_uphSJungpYl_4jWjkCA 提取码: 7va9
代码如下:
import cv2 import numpy as np import paddle.fluid as fluid
place = fluid.CPUPlace() exe = fluid.Executor(place) infer_scope = fluid.core.Scope()
with fluid.scope_guard(infer_scope): [prog, feed, fetch] = fluid.io.load_inference_model('../inference_model', exe)
image = 'imgnet_val_1.jpg'
image = cv2.imread(image)
image = cv2.resize(image, (224, 224))
image = np.array(image).astype('float32').transpose((2, 0, 1)) / 255
res_list = [image]
res = res_list[0]
res = res[np.newaxis, :]
feed_list = {feed[0]: res}
# input_image matrix output
# print(feed_list)
x = fluid.framework._get_var('vgg_16_conv1_conv1_2_weights', program=prog)
tmp = exe.run(prog, feed=feed_list, fetch_list=fetch)
res_np = np.array(tmp).flatten()
print(res_np)
我使用 x = fluid.framework._get_var('vgg_16_conv1_conv1_2_weights', program=prog) tmp = exe.run(prog, feed=feed_list, fetch_list=[x]) 出现错误‘fetch_targets’ does not have save_infer_model/scale_0.tmp_0 variable