如何将json文件上传到Google Colab中的Google云端硬盘?

时间:2019-03-17 17:25:49

标签: json keras google-colaboratory

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

2 个答案:

答案 0 :(得分:0)

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

文件浏览器上传:

enter image description here

驱动器保险丝:

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

Details

答案 1 :(得分:0)

之所以发生这种情况,是因为授予笔记本计算机许可的笔记本电脑的授权代码会在几分钟/几小时后过期。

此问题通过再次请求授权码得以解决。那是插入

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

在保存模型文件之后,然后将其上传到驱动器。