博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
K.function用法
阅读量:4005 次
发布时间:2019-05-24

本文共 1316 字,大约阅读时间需要 4 分钟。

# load the modelprint("[INFO] loading network...")model = load_model("fashion.model")# load the imageimage_path = "10026.jpg"image = cv2.imread(image_path)# pre-process the image for classificationimage = cv2.resize(image, (96, 96))image = image.astype("float") / 255.0image = img_to_array(image)image = np.expand_dims(image, axis=0)print(image, type(image))# extract the layer featureget_3rd_layer_output = K.function([model.layers[0].input],[model.layers[3].output])feature = get_3rd_layer_output(image)[0]# prob = model.predict(image)[0]

报错:TypeError: inputs to a TensorFlow backend function should be a list or tuple

原因在于,在使用get_3rd_layer时没有用[ ]将image框起来,变成一个list。

将该句

feature = get_3rd_layer_output(image)[0]

修改为:

feature = get_3rd_layer_output([image])[0]

K.function作用:

1.一种简单的方法是创建一个新的Model,使得它的输出是你想要的那个输出

from keras.models import Model   model = ...  # create the original model   layer_name = 'my_layer' intermediate_layer_model = Model(input=model.input,                                  output=model.get_layer(layer_name).output) intermediate_output = intermediate_layer_model.predict(data)

2.此外,我们也可以建立一个Keras的函数来达到这一目的:

from keras import backend as K# with a Sequential modelget_3rd_layer_output = K.function([model.layers[0].input], [model.layers[3].output])layer_output = get_3rd_layer_output([X])[0]

转载地址:http://adzfi.baihongyu.com/

你可能感兴趣的文章
iphone开发基础之objective-c学习
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>
C primer plus 基础总结(一)
查看>>
剑指offer算法题分析与整理(一)
查看>>
剑指offer算法题分析与整理(三)
查看>>
部分笔试算法题整理
查看>>
Ubuntu 13.10使用fcitx输入法
查看>>
pidgin-lwqq 安装
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>
retext出现Could not parse file contents, check if you have the necessary module installed解决方案
查看>>
pyQt不同窗体间的值传递(一)——对话框关闭时返回值给主窗口
查看>>
linux mint下使用外部SMTP(如网易yeah.net)发邮件
查看>>
北京联通华为光猫HG8346R破解改桥接
查看>>
python使用win32*模块模拟人工操作——城通网盘下载器(一)
查看>>
python append 与浅拷贝
查看>>