如何在 Google Colab 中将 json 文件上传到 Google 驱动器?

How to upload json file to Google Drive in Google Colab?

我正在 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'

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

文件浏览器上传:

驱动保险丝:

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

(Details)

发生这种情况是因为授予笔记本权限的授权码在 minutes/hours 后过期。

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

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

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