欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Keras 使用 Lambda層詳解

 更新時間:2020年06月10日 09:37:25   作者:驛無邊  
這篇文章主要介紹了Keras 使用 Lambda層詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧!

from tensorflow.python.keras.models import Sequential, Model
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout, Conv2DTranspose, Lambda, Input, Reshape, Add, Multiply
from tensorflow.python.keras.optimizers import Adam
 
def deconv(x):
  height = x.get_shape()[1].value
  width = x.get_shape()[2].value
  
  new_height = height*2
  new_width = width*2
  
  x_resized = tf.image.resize_images(x, [new_height, new_width], tf.image.ResizeMethod.NEAREST_NEIGHBOR)
  
  return x_resized
 
def Generator(scope='generator'):
  imgs_noise = Input(shape=inputs_shape)
  x = Conv2D(filters=32, kernel_size=(9,9), strides=(1,1), padding='same', activation='relu')(imgs_noise)
  x = Conv2D(filters=64, kernel_size=(3,3), strides=(2,2), padding='same', activation='relu')(x)
  x = Conv2D(filters=128, kernel_size=(3,3), strides=(2,2), padding='same', activation='relu')(x)
 
  x1 = Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu')(x)
  x1 = Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu')(x1)
  x2 = Add()([x1, x])
 
  x3 = Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu')(x2)
  x3 = Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu')(x3)
  x4 = Add()([x3, x2])
 
  x5 = Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu')(x4)
  x5 = Conv2D(filters=128, kernel_size=(3,3), strides=(1,1), padding='same', activation='relu')(x5)
  x6 = Add()([x5, x4])
 
  x = MaxPool2D(pool_size=(2,2))(x6)
 
  x = Lambda(deconv)(x)
  x = Conv2D(filters=64, kernel_size=(3, 3), strides=(1,1), padding='same',activation='relu')(x)
  x = Lambda(deconv)(x)
  x = Conv2D(filters=32, kernel_size=(3, 3), strides=(1,1), padding='same',activation='relu')(x)
  x = Lambda(deconv)(x)
  x = Conv2D(filters=3, kernel_size=(3, 3), strides=(1, 1), padding='same',activation='tanh')(x)
 
  x = Lambda(lambda x: x+1)(x)
  y = Lambda(lambda x: x*127.5)(x)
  
  model = Model(inputs=imgs_noise, outputs=y)
  model.summary()
  
  return model
 
my_generator = Generator()
my_generator.compile(loss='binary_crossentropy', optimizer=Adam(0.7, decay=1e-3), metrics=['accuracy'])

補充知識:含有Lambda自定義層keras模型,保存遇到的問題及解決方案

一,許多應用,keras含有的層已經(jīng)不能滿足要求,需要透過Lambda自定義層來實現(xiàn)一些layer,這個情況下,只能保存模型的權重,無法使用model.save來保存模型。保存時會報

TypeError: can't pickle _thread.RLock objects

二,解決方案,為了便于后續(xù)的部署,可以轉成tensorflow的PB進行部署。

from keras.models import load_model
import tensorflow as tf
import os, sys
from keras import backend as K
from tensorflow.python.framework import graph_util, graph_io

def h5_to_pb(h5_weight_path, output_dir, out_prefix="output_", log_tensorboard=True):
  if not os.path.exists(output_dir):
    os.mkdir(output_dir)
  h5_model = build_model()
  h5_model.load_weights(h5_weight_path)
  out_nodes = []
  for i in range(len(h5_model.outputs)):
    out_nodes.append(out_prefix + str(i + 1))
    tf.identity(h5_model.output[i], out_prefix + str(i + 1))
  model_name = os.path.splitext(os.path.split(h5_weight_path)[-1])[0] + '.pb'
  sess = K.get_session()
  init_graph = sess.graph.as_graph_def()
  main_graph = graph_util.convert_variables_to_constants(sess, init_graph, out_nodes)
  graph_io.write_graph(main_graph, output_dir, name=model_name, as_text=False)
  if log_tensorboard:
    from tensorflow.python.tools import import_pb_to_tensorboard
    import_pb_to_tensorboard.import_to_tensorboard(os.path.join(output_dir, model_name), output_dir)

def build_model():
  inputs = Input(shape=(784,), name='input_img')
  x = Dense(64, activation='relu')(inputs)
  x = Dense(64, activation='relu')(x)
  y = Dense(10, activation='softmax')(x)
  h5_model = Model(inputs=inputs, outputs=y)
  return h5_model

if __name__ == '__main__':
  if len(sys.argv) == 3:
    # usage: python3 h5_to_pb.py h5_weight_path output_dir
    h5_to_pb(h5_weight_path=sys.argv[1], output_dir=sys.argv[2])

以上這篇Keras 使用 Lambda層詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Python一行代碼識別增值稅發(fā)票實現(xiàn)示例

    Python一行代碼識別增值稅發(fā)票實現(xiàn)示例

    這篇文章主要為大家介紹了Python一行代碼識別增值稅發(fā)票實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Python調用百度AI實現(xiàn)身份證識別

    Python調用百度AI實現(xiàn)身份證識別

    這篇文章主要介紹了Python通過調用百度AI的文字識別功能實現(xiàn)對身份證進行識別,代碼具有一定的學習價值,感興趣的朋友一起來看看效果吧
    2021-12-12
  • 基于Python實現(xiàn)身份證信息識別功能

    基于Python實現(xiàn)身份證信息識別功能

    身份證是用于證明個人身份和身份信息的官方證件,在現(xiàn)代社會中,身份證被廣泛應用于各種場景,如就業(yè)、教育、醫(yī)療、金融等,它包含了個人的基本信息,本文給大家介紹了如何基于Python實現(xiàn)身份證信息識別功能,感興趣的朋友可以參考下
    2024-01-01
  • Python Numpy實現(xiàn)修改數(shù)組形狀

    Python Numpy實現(xiàn)修改數(shù)組形狀

    NumPy(Numerical Python)是Python中用于處理數(shù)組和矩陣的重要庫,它提供了豐富的功能,用于科學計算,本文主要介紹了如何使用NumPy提供的方法來改變數(shù)組的形狀,感興趣的可以了解下
    2023-11-11
  • Python通過隊列來實現(xiàn)進程間通信的示例

    Python通過隊列來實現(xiàn)進程間通信的示例

    這篇文章主要介紹了Python通過隊列來實現(xiàn)進程間通信的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-10-10
  • 使用python統(tǒng)計文件行數(shù)示例分享

    使用python統(tǒng)計文件行數(shù)示例分享

    當文件的尺寸非常大的時候(10G之上吧),想知道行數(shù)是個問題,提供一個使用python統(tǒng)計文件行數(shù)的示例,需要的朋友可以參考下
    2014-02-02
  • python多重繼承實例

    python多重繼承實例

    這篇文章主要介紹了python多重繼承實例,簡單實用易于理解,需要的朋友可以參考下
    2014-10-10
  • 六個實用Pandas數(shù)據(jù)處理代碼

    六個實用Pandas數(shù)據(jù)處理代碼

    這篇文章主要介紹了六個實用Pandas數(shù)據(jù)處理代碼,文章圍繞主題相相關內容,具有一定的參考價價值,需要的小伙伴可以參考一下
    2022-05-05
  • numpy給array增加維度np.newaxis的實例

    numpy給array增加維度np.newaxis的實例

    今天小編就為大家分享一篇numpy給array增加維度np.newaxis的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python機器學習使數(shù)據(jù)更鮮活的可視化工具Pandas_Alive

    python機器學習使數(shù)據(jù)更鮮活的可視化工具Pandas_Alive

    今天我分享大家一款非常棒的動畫可視化工具:Pandas_Alive,它以?matplotlib?繪圖為后端,不僅可以創(chuàng)建出令人驚嘆的動畫可視化,而且使用方法非常簡單。本文詳情如下
    2021-11-11

最新評論