从gs将保存的keras模型加载到pydatalab

Sal*_*a R 2 python model keras google-cloud-datalab

我的keras模型通过model.save(model_name)保存在Google存储中

我无法在pydatalab上加载模型。将模型保存在本地计算机上时,可以使用load_model(filepath)打开它。我也确实在打开使用Tensorflow Backend的Keras模型时基于NameError将keras.backend导入为K

我尝试了以下方法:

  1. model = load_model(tf.gfile.Open(model_file))
    
    Run Code Online (Sandbox Code Playgroud)

错误:TypeError:预期的str,字节或os.PathLike对象,而非GFile

  1. load_model('gs://mybucket/model.h5')
    
    Run Code Online (Sandbox Code Playgroud)

错误:IO错误:无法打开文件(无法打开文件:name ='gs://mybucket/model.h5',errno = 2,错误消息='无此类文件或目录',标志= 0,o_flags = 0 )

  1. with file_io.FileIO(model_file, 'r') as f:
    modl = load_model(f)
    
    Run Code Online (Sandbox Code Playgroud)

错误:TypeError:预期的str,字节或os.PathLike对象,而不是FileIO

小智 5

从gs存储器加载文件

from tensorflow.python.lib.io import file_io
model_file = file_io.FileIO('gs://mybucket/model.h5', mode='rb')
Run Code Online (Sandbox Code Playgroud)

在本地保存模型的临时副本

temp_model_location = './temp_model.h5'
temp_model_file = open(temp_model_location, 'wb')
temp_model_file.write(model_file.read())
temp_model_file.close()
model_file.close()
Run Code Online (Sandbox Code Playgroud)

加载模型保存在本地

model = load_model(temp_model_location)
Run Code Online (Sandbox Code Playgroud)