最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

keras K.function獲取某層的輸出操作

 更新時間:2020年06月29日 08:43:48   作者:脫貧&&脫單&&不脫發(fā)  
這篇文章主要介紹了keras K.function獲取某層的輸出操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

如下所示:

from keras import backend as K
from keras.models import load_model

models = load_model('models.hdf5')
image=r'image.png'
images=cv2.imread(r'image.png')
image_arr = process_image(image, (224, 224, 3))
image_arr = np.expand_dims(image_arr, axis=0)
layer_1 = K.function([base_model.get_input_at(0)], [base_model.get_layer('layer_name').output])
f1 = layer_1([image_arr])[0]

加載訓(xùn)練好并保存的網(wǎng)絡(luò)模型

加載數(shù)據(jù)(圖像),并將數(shù)據(jù)處理成array形式

指定輸出層

將處理后的數(shù)據(jù)輸入,然后獲取輸出

其中,K.function有兩種不同的寫法:

1. 獲取名為layer_name的層的輸出

layer_1 = K.function([base_model.get_input_at(0)], [base_model.get_layer('layer_name').output]) #指定輸出層的名稱

2. 獲取第n層的輸出

layer_1 = K.function([model.get_input_at(0)], [model.layers[5].output]) #指定輸出層的序號(層號從0開始)

另外,需要注意的是,書寫不規(guī)范會導(dǎo)致報錯:

報錯:

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

將該句:

f1 = layer_1(image_arr)[0]

修改為:

f1 = layer_1([image_arr])[0]

補充知識:keras.backend.function()

如下所示:

def function(inputs, outputs, updates=None, **kwargs):
 """Instantiates a Keras function.
 Arguments:
   inputs: List of placeholder tensors.
   outputs: List of output tensors.
   updates: List of update ops.
   **kwargs: Passed to `tf.Session.run`.
 Returns:
   Output values as Numpy arrays.
 Raises:
   ValueError: if invalid kwargs are passed in.
 """
 if kwargs:
  for key in kwargs:
   if (key not in tf_inspect.getargspec(session_module.Session.run)[0] and
     key not in tf_inspect.getargspec(Function.__init__)[0]):
    msg = ('Invalid argument "%s" passed to K.function with Tensorflow '
        'backend') % key
    raise ValueError(msg)
 return Function(inputs, outputs, updates=updates, **kwargs)

這是keras.backend.function()的源碼。其中函數(shù)定義開頭的注釋就是官方文檔對該函數(shù)的解釋。

我們可以發(fā)現(xiàn)function()函數(shù)返回的是一個Function對象。下面是Function類的定義。

class Function(object):
 """Runs a computation graph.
 Arguments:
   inputs: Feed placeholders to the computation graph.
   outputs: Output tensors to fetch.
   updates: Additional update ops to be run at function call.
   name: a name to help users identify what this function does.
 """

 def __init__(self, inputs, outputs, updates=None, name=None,
        **session_kwargs):
  updates = updates or []
  if not isinstance(inputs, (list, tuple)):
   raise TypeError('`inputs` to a TensorFlow backend function '
           'should be a list or tuple.')
  if not isinstance(outputs, (list, tuple)):
   raise TypeError('`outputs` of a TensorFlow backend function '
           'should be a list or tuple.')
  if not isinstance(updates, (list, tuple)):
   raise TypeError('`updates` in a TensorFlow backend function '
           'should be a list or tuple.')
  self.inputs = list(inputs)
  self.outputs = list(outputs)
  with ops.control_dependencies(self.outputs):
   updates_ops = []
   for update in updates:
    if isinstance(update, tuple):
     p, new_p = update
     updates_ops.append(state_ops.assign(p, new_p))
    else:
     # assumed already an op
     updates_ops.append(update)
   self.updates_op = control_flow_ops.group(*updates_ops)
  self.name = name
  self.session_kwargs = session_kwargs

 def __call__(self, inputs):
  if not isinstance(inputs, (list, tuple)):
   raise TypeError('`inputs` should be a list or tuple.')
  feed_dict = {}
  for tensor, value in zip(self.inputs, inputs):
   if is_sparse(tensor):
    sparse_coo = value.tocoo()
    indices = np.concatenate((np.expand_dims(sparse_coo.row, 1),
                 np.expand_dims(sparse_coo.col, 1)), 1)
    value = (indices, sparse_coo.data, sparse_coo.shape)
   feed_dict[tensor] = value
  session = get_session()
  updated = session.run(
    self.outputs + [self.updates_op],
    feed_dict=feed_dict,
    **self.session_kwargs)
  return updated[:len(self.outputs)]

所以,function函數(shù)利用我們之前已經(jīng)創(chuàng)建好的comuptation graph。遵循計算圖,從輸入到定義的輸出。這也是為什么該函數(shù)經(jīng)常用于提取中間層結(jié)果。

以上這篇keras K.function獲取某層的輸出操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python使用googletrans報錯的解決方法

    Python使用googletrans報錯的解決方法

    這篇文章主要給大家介紹了關(guān)于Python使用googletrans報錯的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Python中numpy數(shù)組的維度增減方法詳解

    Python中numpy數(shù)組的維度增減方法詳解

    這篇文章主要介紹了Python中numpy數(shù)組的維度增減方法詳解,在操作數(shù)組情況下,需要按照某個軸將不同數(shù)組的維度對齊,這時候需要為數(shù)組添加維度(特別是將二維數(shù)組變成高維張量的情況下),numpy提供了expand_dims()函數(shù)來為數(shù)組增加維度,需要的朋友可以參考下
    2023-09-09
  • Python實現(xiàn)輸出某區(qū)間范圍內(nèi)全部素數(shù)的方法

    Python實現(xiàn)輸出某區(qū)間范圍內(nèi)全部素數(shù)的方法

    這篇文章主要介紹了Python實現(xiàn)輸出某區(qū)間范圍內(nèi)全部素數(shù)的方法,涉及Python數(shù)值運算、排序、判斷等相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05
  • PyTorch實現(xiàn)圖像識別實戰(zhàn)指南

    PyTorch實現(xiàn)圖像識別實戰(zhàn)指南

    圖像識別是從給定圖像中提取有意義的信息(例如圖像內(nèi)容)的過程,下面這篇文章主要給大家介紹了關(guān)于PyTorch實現(xiàn)圖像識別的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-02-02
  • Python解析網(wǎng)頁源代碼中的115網(wǎng)盤鏈接實例

    Python解析網(wǎng)頁源代碼中的115網(wǎng)盤鏈接實例

    這篇文章主要介紹了Python解析網(wǎng)頁源代碼中的115網(wǎng)盤鏈接實例,主要采用了正則表達式re模塊來實現(xiàn)該功能,需要的朋友可以參考下
    2014-09-09
  • Python匿名函數(shù)及應(yīng)用示例

    Python匿名函數(shù)及應(yīng)用示例

    這篇文章主要介紹了Python匿名函數(shù)及應(yīng)用,結(jié)合實例形式分析了Python匿名函數(shù)的功能、定義及函數(shù)作為參數(shù)傳遞的相關(guān)應(yīng)用操作技巧,需要的朋友可以參考下
    2019-04-04
  • Python發(fā)起請求提示UnicodeEncodeError錯誤代碼解決方法

    Python發(fā)起請求提示UnicodeEncodeError錯誤代碼解決方法

    這篇文章主要介紹了Python發(fā)起請求提示UnicodeEncodeError錯誤代碼解決方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Python+OpenCV 實現(xiàn)圖片無損旋轉(zhuǎn)90°且無黑邊

    Python+OpenCV 實現(xiàn)圖片無損旋轉(zhuǎn)90°且無黑邊

    今天小編就為大家分享一篇Python+OpenCV 實現(xiàn)圖片無損旋轉(zhuǎn)90°且無黑邊,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python獲取指定時間段內(nèi)特定規(guī)律的日期列表

    python獲取指定時間段內(nèi)特定規(guī)律的日期列表

    這篇文章主要介紹了python獲取指定時間段內(nèi)特定規(guī)律的日期列表,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • 對python數(shù)據(jù)切割歸并算法的實例講解

    對python數(shù)據(jù)切割歸并算法的實例講解

    今天小編就為大家分享一篇對python數(shù)據(jù)切割歸并算法的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12

最新評論

博罗县| 井研县| 会同县| 屏山县| 通河县| 洛川县| 亚东县| 奉化市| 威宁| 思南县| 龙胜| 桦川县| 林甸县| 即墨市| 武清区| 穆棱市| 宜州市| 岑巩县| 建水县| 武功县| 鄂温| 驻马店市| 石棉县| 公主岭市| 兴城市| 渑池县| 香河县| 南平市| 竹北市| 庆元县| 彭州市| 苍山县| 色达县| 韩城市| 东辽县| 巍山| 德江县| 河间市| 囊谦县| 交口县| 萝北县|