我刚开始使用 pytorch 的 webdataset 库。我使用 webdataset.TarWriter() 创建了系统本地存在的示例数据集的 .tar 文件。 .tar 文件创建似乎很成功,因为我可以在 Windows 平台上单独提取它们并验证相同的数据集文件。
现在,我创建 train_dataset = wds.Dataset(url)
其中 url 是 .tar 文件的本地文件路径。之后,我执行以下操作:
train_loader = torch.utils.data.DataLoader(train_dataset, num_workers=0, batch_size=10)
sample = next(iter(train_loader))
print(sample)
如果我使用 Web url 示例,相同的代码可以正常工作:"http://storage.googleapis.com/nvdata-openimages/openimages-train-000000.tar"
webdataset文档中提到:https://reposhub.com/python/deep-learning/tmbdev-webdataset.html
到目前为止我无法理解这个错误。您知道如何解决这个问题吗?
最佳答案
从昨天开始我就遇到了同样的错误,我终于找到了罪魁祸首。 WebDataset/tarIterators.py利用 WebDataset/gopen.py 。在 gopen.py urllib.parse.urlparse
被调用来解析要打开的 url,在您的情况下,url 是 D:/PhD/...
.
gopen_schemes = dict(
__default__=gopen_error,
pipe=gopen_pipe,
http=gopen_curl,
https=gopen_curl,
sftp=gopen_curl,
ftps=gopen_curl,
scp=gopen_curl)
def gopen(url, mode="rb", bufsize=8192, **kw):
"""Open the URL. This uses the `gopen_schemes` dispatch table to dispatch based on scheme.
Support for the following schemes is built-in: pipe, file, http, https, sftp, ftps, scp.
When no scheme is given the url is treated as a file. You can use the OPEN_VERBOSE argument to get info about files being opened.
:param url: the source URL
:param mode: the mode ("rb", "r")
:param bufsize: the buffer size
"""
global fallback_gopen
verbose = int(os.environ.get("GOPEN_VERBOSE", 0))
if verbose:
print("GOPEN", url, info, file=sys.stderr)
assert mode in ["rb", "wb"], mode
if url == "-":
if mode == "rb":
return sys.stdin.buffer
elif mode == "wb":
return sys.stdout.buffer
else:
raise ValueError(f"unknown mode {mode}")
pr = urlparse(url)
if pr.scheme == "":
bufsize = int(os.environ.get("GOPEN_BUFFER", -1))
return open(url, mode, buffering=bufsize)
if pr.scheme == "file":
bufsize = int(os.environ.get("GOPEN_BUFFER", -1))
return open(pr.path, mode, buffering=bufsize)
handler = gopen_schemes["__default__"]
handler = gopen_schemes.get(pr.scheme, handler)
return handler(url, mode, bufsize, **kw)
正如您在字典中看到的 __default__
功能是gopen_error
。这是返回您所看到的错误的函数。 pr = urlparse(url)
在您的网址上将生成一个 urlparse,其中方案( pr.scheme
)为 'd'
因为你的磁盘名为D。但是,它应该是'file'
以使该功能按预期工作。由于它不等于“file”或字典中的任何其他方案(http、https、sftp 等),因此将使用默认函数,该函数会返回错误。我通过添加 d=gopen_file
来规避这个问题到gopen_schemes
字典。我希望这可以暂时帮助您。我还将在 WebDataset GitHub 页面上解决此问题,并在获得更实用的更新时保持此页面更新。
祝你好运!
关于python - 值错误: no gopen handler defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68299665/