【问题标题】:How to upload json file to Google Drive in Google Colab?如何在 Google Colab 中将 json 文件上传到 Google Drive?
【发布时间】:2019-03-17 17:25:48
【问题描述】:

我正在 Google Colab 中训练模型并将其存储为 json 格式。我想将这个经过训练的模型上传到我在 colab 中的驱动器。

我目前正在做:

model_json = model.to_json()

with open("trainedModel.json", "w") as json_file:
    json_file.write(model_json)

model.save_weights("trainedModel.h5")
print("Saved model to disk")
print("This file ran till end.\nNow uploading to drive:")

uploaded = drive.CreateFile({'parents':[{u'id':'#id_no'}],'title': 'trainedModel.json'}) 
uploaded.SetContentFile('trainedModel.json')
uploaded.Upload()

uploaded = drive.CreateFile({'parents':[{u'id': '#id_no''}],'title': 'trainedModel.h5'}) 
uploaded.SetContentFile('trainedModel.h5')
uploaded.Upload()

但这给了我:

FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'

【问题讨论】:

    标签: json keras google-colaboratory


    【解决方案1】:

    我建议改用文件浏览器浏览器或 Drive FUSE。两者都比直接使用 Drive API 简单得多。

    文件浏览器上传:

    驱动保险丝:

    from google.colab import drive
    drive.mount('/content/gdrive')
    

    (Details)

    【讨论】:

    • 我在上面的代码之前做from google.colab import drive drive.mount('/content/gdrive', force_remount=True)!pip install -U -q PyDrive from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive from google.colab import auth from oauth2client.client import GoogleCredentials # Authenticate and create the PyDrive client. auth.authenticate_user() gauth = GoogleAuth() gauth.credentials = GoogleCredentials.get_application_default() drive = GoogleDrive(gauth)。我想上传代码执行过程中生成的文件...
    【解决方案2】:

    发生这种情况是因为授予笔记本的授权码在几分钟/几小时后过期。

    通过再次请求授权码解决了这个问题。那就是插入

    auth.authenticate_user()
    gauth = GoogleAuth()
    gauth.credentials = GoogleCredentials.get_application_default()
    drive = GoogleDrive(gauth)
    

    在保存模型文件后上传到驱动器之前。

    【讨论】: