google colaboratory,重量下载(导出已保存的型号)

时间:2018-02-22 09:46:42

标签: google-colaboratory

我使用Keras库创建了一个模型,并将模型保存为.json及其权重,扩展名为.h5。如何将其下载到我的本地计算机上?

保存我遵循此link

的模型

10 个答案:

答案 0 :(得分:13)

这对我有用!! 使用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')})

相同的重量

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')})

现在,检查您的Google驱动器。

下次运行时,尝试重新加载砝码

# 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')

答案 1 :(得分:9)

这是一个对我有用的解决方案:

使用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}

完成此身份验证后,请使用以下代码建立连接:

!mkdir -p drive
!google-drive-ocamlfuse drive

现在查看Google云端硬盘中的文件列表:

!ls drive

要将Keras型号输出保存到Drive,该过程与存储在本地驱动器中的过程完全相同:

- 像往常一样运行Keras模型

模型训练后,您要将模型输出(.h5和json)存储到Google云端硬盘的app文件夹中:

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")

您可以在Google云端硬盘的相应文件夹中找到这些文件,您可以从中下载这些文件,如下所示:

enter image description here

答案 2 :(得分:8)

试试这个

from google.colab import files
files.download("model.json")

答案 3 :(得分:2)

files.download不允许您直接下载大文件。解决方法是使用下面的pydrive片段将权重保存在Google云端硬盘上。只需更改filename.txt文件的weights.h5

即可
# Install the PyDrive wrapper & import libraries.
# This only needs to be done once in a notebook.
!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.
# This only needs to be done once in a notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# Create & upload a file.
uploaded = drive.CreateFile({'title': 'filename.csv'})
uploaded.SetContentFile('filename.csv')
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))

答案 4 :(得分:1)

要将模型下载到本地系统,以下代码将起作用 - 正在下载json文件:

model_json = model.to_json()
with open("model1.json","w") as json_file:
     json_file.write(model_jason)

files.download("model1.json")

下载权重:

model.save('weights.h5')
files.download('weights.h5')

答案 5 :(得分:1)

下载到本地系统:

from google.colab import files

#For model json
model_json = model.to_json()
with open("model1.json","w") as json_file:
     json_file.write(model_json)
files.download("model1.json")

#For weights
model.save('weights.h5')
files.download('weights.h5')

答案 6 :(得分:0)

训练结束后,您可以运行以下内容。

saver = tf.train.Saver()
save_path = saver.save(session, "data/dm.ckpt")
print('done saving at',save_path)

然后检查ckpt文件的保存位置。

import os
print( os.getcwd() )
print( os.listdir('data') )

最后下载有重量的文件!

from google.colab import files
files.download( "data/dm.ckpt.meta" ) 

答案 7 :(得分:0)

save_path = saver.save(sess,&#34; data / dm.ckpt&#34;)    &#34;会话&#34;已弃用。

答案 8 :(得分:0)

只需使用model.save()。在下面,我创建了一个变量来存储模型的名称,然后使用model.save()将其保存。我使用了谷歌合作,但它应该适用于其他 enter image description here

答案 9 :(得分:0)

我只是将模型拖放到内容文件夹中。它在我的谷歌驱动器中。 enter image description here