我试图将PyTorch模型保存到我的谷歌云桶,但它总是显示"FileNotFoundError错误"
我已经有一个gs桶,我提供的文件路径也是正确的。我的代码在GCP笔记本实例上运行。
path = "gs://bucket_name/model/model.pt"
torch.save(model,path)
如果有人试图上传一个模型到gs桶,让我知道你是否可以,这真的会帮助我?如果您分享使用torch.save()
将模型放入gs bucket的正确方法,也会对我有所帮助。
您必须以稍微不同的方式使用torch.save
。如文档所述,torch.save
的f
参数可以是一个简单的字符串或包含文件名的os.PathLike
对象,也可以是实现write
和flush
方法的类文件对象。我们可以使用后者将模型直接保存到Google Cloud bucket:
from google.cloud import storage
storage_client = storage.Client("bucket_name")
bucket = storage_client.bucket("bucket_name")
blob = bucket.blob("model/model.pt")
with blob.open("wb", ignore_flush=True) as f:
torch.save(obj, f)
try this:
import gcsfs
fs = gcsfs.GCSFileSystem(project = '<enter your gc project>')
with fs.open("gs://bucket_name/"+f'model/model.pt', 'wb') as f:
torch.save(model, f)