Python 将保存的keras模型从gs加载到pydatalab
我的keras模型与model.save(model\u name)一起保存在google存储中 我无法在pydatalab上加载该模型。当我在本地机器上保存模型时,我可以用load_model(filepath)打开它。 此外,我还根据 我尝试了以下方法: 错误:类型错误:应为str、bytes或os.PathLike对象,而不是GFile 错误:IOError:无法打开文件(无法打开文件:名称='gs://mybucket/model.h5',错误号=2,错误消息='没有这样的文件或目录',标志=0,o_标志=0)Python 将保存的keras模型从gs加载到pydatalab,python,model,keras,google-cloud-datalab,Python,Model,Keras,Google Cloud Datalab,我的keras模型与model.save(model\u name)一起保存在google存储中 我无法在pydatalab上加载该模型。当我在本地机器上保存模型时,我可以用load_model(filepath)打开它。 此外,我还根据 我尝试了以下方法: 错误:类型错误:应为str、bytes或os.PathLike对象,而不是GFile 错误:IOError:无法打开文件(无法打开文件:名称='gs://mybucket/model.h5',错误号=2,错误消息='没有这样的文件或目
错误:TypeError:应该是str、bytes或os.PathLike对象,而不是FileIO我认为Keras不支持TensorFlow文件系统,而TensorFlow文件系统又知道如何从GCS读取
您可以尝试从GCS下载到本地路径,然后从该路径读取以加载模型。从gs存储加载文件
from tensorflow.python.lib.io import file_io
model_file = file_io.FileIO('gs://mybucket/model.h5', mode='rb')
在本地保存模型的临时副本
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()
本地保存的负载模型
model = load_model(temp_model_location)
以下功能用于在gcloud机器学习平台(多亏Tíarnán McGrath)上重新培训已经培训过的keras模型(使用新数据)
由于某种原因,
load\u model
fromkeras.models
不再适用于我,因此每次我都必须构建模型 OS-level命令也可以在有人使用Colab的情况下使用
要使用google drive,请使用
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
安装地面军事系统的代码
from google.colab import auth
auth.authenticate_user()
project_id = 'thirumalai_bucket' #your bucket here
!gcloud config set project {project_id}
!gsutil ls
!!gsutil-mcp
就你而言:
!gsutil -m cp gs://mybucket/model.h5 /content/drive/My\ Drive/models/
现在,文件model.h5在drive/content/drive/My drive/models中提供/
使用以下命令移动到模型目录:
!cd /content/drive/My\ Drive/models/
load_model('model.h5')
希望这有帮助 谢谢你的回答。这就是我目前正在做的。我的工作流程是在datalab中测试代码,在GoogleML引擎中训练模型。这将减慢我在本地下载模型和测试数据的过程。我在寻找更好的方法!对不起,我所说的“本地”是指相对于您加载模型的位置。不建议下载到您的物理本地台式机/笔记本电脑。因此,您可以将二进制文件下载(可能使用gsutil)到“本地”磁盘上的路径,然后在调用load_模型时使用普通文件路径,而不是将gs://路径传递给load_模型。希望能有帮助。我会试试的!谢谢这实际上是一个比将文件下载到本地路径更好的解决方案!
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
from google.colab import auth
auth.authenticate_user()
project_id = 'thirumalai_bucket' #your bucket here
!gcloud config set project {project_id}
!gsutil ls
!gsutil -m cp gs://mybucket/model.h5 /content/drive/My\ Drive/models/
!cd /content/drive/My\ Drive/models/
load_model('model.h5')