Nis*_*ula 16 python-3.x google-colaboratory
我使用Keras库创建了一个模型,并将模型保存为.json,其权重为.h5扩展名.如何将其下载到我的本地计算机上?
保存模型我按照这个链接
Sam*_*oub 14
这对我有用!! 使用PyDrive API
!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
# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
# 2. Save Keras Model or weights on google drive
# create on Colab directory
model.save('model.h5')
model_file = drive.CreateFile({'title' : 'model.h5'})
model_file.SetContentFile('model.h5')
model_file.Upload()
# download to google drive
drive.CreateFile({'id': model_file.get('id')})
Run Code Online (Sandbox Code Playgroud)
相同的重量
model.save_weights('model_weights.h5')
weights_file = drive.CreateFile({'title' : 'model_weights.h5'})
weights_file.SetContentFile('model_weights.h5')
weights_file.Upload()
drive.CreateFile({'id': weights_file.get('id')})
Run Code Online (Sandbox Code Playgroud)
现在,检查您的谷歌硬盘.
在下一次运行时,尝试重新加载权重
# 3. reload weights from google drive into the model
# use (get shareable link) to get file id
last_weight_file = drive.CreateFile({'id': '1sj...'})
last_weight_file.GetContentFile('last_weights.mat')
model.load_weights('last_weights.mat')
Run Code Online (Sandbox Code Playgroud)
Kor*_*ich 10
尝试这个
from google.colab import files
files.download("model.json")
Run Code Online (Sandbox Code Playgroud)
这是一个适合我的解决方案:
设置身份验证不适用Google Colab和您的云端硬盘:
脚步:
- 按下面的方式粘贴代码
- 此过程将生成两个用于完成身份验证的URL,您必须复制令牌并粘贴到提供的栏中
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
Run Code Online (Sandbox Code Playgroud)
完成此身份验证后,请使用以下代码建立连接:
!mkdir -p drive
!google-drive-ocamlfuse drive
Run Code Online (Sandbox Code Playgroud)
现在,查看Google云端硬盘中的文件列表:
!ls drive
Run Code Online (Sandbox Code Playgroud)
要将Keras模型输出保存到Drive,该过程与存储在本地驱动器中的过程完全相同:
- 照常运行Keras模型
训练模型后,说您要将模型输出(.h5和json)存储到app
Google云端硬盘的文件夹中:
model_json = model.to_json()
with open("drive/app/model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("drive/app/model_weights.h5")
print("Saved model to drive")
Run Code Online (Sandbox Code Playgroud)
您可以在Google云端硬盘的相应文件夹中找到这些文件,您可以从中下载,如下所示: