【问题标题】:Importing Keras models into Colaboratory from Google Drive将 Keras 模型从 Google Drive 导入 Colaboratory
【发布时间】:2018-07-13 14:13:57
【问题描述】:

我正在尝试找到将存储在 Google Drive 中的 Keras 模型以 .h5 格式保存的方法加载到 Colaboratory 工作表中以用作 Keras 模型(无需先下载到桌面,已经有方法)。

我已使用 Drive REST API 的 Colaboratory 教程成功加载 .h5 文件:

# Download the '.h5' file from google drive

file_id = '1uBtlaggVyWshwcyP6kEI-y_W3P8D26sz'

import io
from io import BytesIO   
from googleapiclient.http import MediaIoBaseDownload

request = drive_service.files().get_media(fileId=file_id)
downloaded = io.BytesIO()
downloader = MediaIoBaseDownload(downloaded, request)
done = False
while done is False:
  status, done = downloader.next_chunk()
  if status:
      print("Download %%%d%%." % int(status.progress() * 100))
  print("Download Complete!")

downloaded.seek(0)

print('Downloaded file contents are: {}'.format(downloaded.read()))

哪些输出:

Download %100%.
Download Complete!
Downloaded file contents are: b'\x89HDF\r\n\x1a\n\x00\x00\x00...(etc)

而且下载的.read() 似乎给出了一种字节字符串。

我的问题是如何将此字节字符串转换为 .h5 形式,以便我可以从 Keras 调用 load_model?

【问题讨论】:

标签: python google-drive-api keras hdf5 google-colaboratory


【解决方案1】:

最简单的选择是将其写入文件:

with open('/tmp/model.h5', 'wb') as f:
    f.write(downloaded.read())

然后创建一个文件以h5py.File('/tmp/model.h5') 传递给keras。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-30
    • 2018-04-09
    • 2019-04-20
    • 2019-08-06
    • 2019-08-28
    • 2019-05-10
    • 2018-07-29
    • 2020-01-01
    相关资源
    最近更新 更多