Amazon s3 从S3铲斗加载pytorch模型
我想从S3存储桶加载pytorch模型(Amazon s3 从S3铲斗加载pytorch模型,amazon-s3,pytorch,torch,Amazon S3,Pytorch,Torch,我想从S3存储桶加载pytorch模型(model.pt)。我编写了以下代码: from smart_open import open as smart_open import io load_path = "s3://serial-no-images/yolo-models/model4/model.pt" with smart_open(load_path) as f: buffer = io.BytesIO(f.read()) model.load_s
model.pt
)。我编写了以下代码:
from smart_open import open as smart_open
import io
load_path = "s3://serial-no-images/yolo-models/model4/model.pt"
with smart_open(load_path) as f:
buffer = io.BytesIO(f.read())
model.load_state_dict(torch.load(buffer))
这将导致以下错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 64: invalid start byte
一种解决方案是在本地下载模型,但我希望避免这种情况,直接从S3加载模型。不幸的是,我在网上找不到一个好的解决方案。有人能帮我吗?AFAIK
torch.load
需要文件名作为参数,而不是文件内容。您的缓冲区
是否可能已经等同于torch.load
ing文件的本地副本的结果?如果您尝试
建模。加载\u state\u dict(buffer)
,会发生什么情况?根据,以下工作:
from smart_open import open as smart_open
import io
load_path = "s3://serial-no-images/yolo-models/model4/model.pt"
with smart_open(load_path, 'rb') as f:
buffer = io.BytesIO(f.read())
model.load_state_dict(torch.load(buffer))
我以前尝试过这个,但没有发现必须将“rb”设置为参数。谢谢您的回答。我对S3的了解非常有限,但据我所知,
.pt
文件是一个io.BytesIO
对象<代码>火炬.装载默认情况下无法处理此问题。我刚找到解决办法,会尽快发帖的。