Cho em hỏi cách save và restore lại model trên google colab. Em đang chạy model phân loại âm thanh nhưng thời gian train thường lớn hơn 12 tiếng hoặc bị disconnected giữa chừng. Em có search trên google nhưng không hiểu lắm. Mong anh chị giúp đỡ.
Cách Save và Restore model trên Google Colaboratory
Destiny
#2
Bạn chỉ cần viết code thực hiện 2 thao tác sau
- Lưu file xuống drive
- Mount drive
from google.colab import drive drive.mount('/content/drive/')
- Viết hàm ghi file giống như trên máy local. Drive của bạn sẽ có đường dẫn là “/content/drive/My Drive/”. Bạn có thể chạy lệnh ls, cd, mv như trên cmd linux. Ví dụ
!ls -l
- Mount drive
- Save / Load model bằng hàm của thư viện (Tensorflow, Keras, …)
dtx
#3
Với mình, đây là cách để tải nội dung trong folder từ google drive xuống colab
!pip3 install -U -q PyDrive
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
file_list = drive.ListFile({'q': "'%s' in parents"}).GetList() # %s là mã của folder khi share trên drive
for f in file_list:
print('title: %s, id: %s' % (f['title'], f['id']))
fname = os.path.join("./", f['title'])
print('downloading to {}'.format(fname))
f_ = drive.CreateFile({'id': f['id']})
f_.GetContentFile(fname)
Còn đây là khi lưu file từ colab vào google drive
model_file = drive.CreateFile({"title" : "%s"}) # %s là tên file cần lưu
model_file.SetContentFile("%s")
model_file.Upload()