如何解决Google Colab加载Hugging Face数据集的文件一致性错误?
解决Hugging Face数据集下载一致性校验失败问题
在Google Colab中加载valiantlynxz/godseye-violence-detection-dataset数据集时,持续出现文件大小不一致的错误,报错代码如下:
from datasets import load_dataset ds = load_dataset("valiantlynxz/godseye-violence-detection-dataset", download_mode="force_redownload")
Colab返回的错误信息:
OSError Traceback (most recent call last) /tmp/ipython-input-3556148243.py in <cell line: 0>() 1 from datasets import load_dataset 2 ----> 3 ds = load_dataset("valiantlynxz/godseye-violence-detection-dataset",download_mode="force_redownload") 4 5 print(ds["train"].column_names) 26 frames /usr/local/lib/python3.12/dist-packages/huggingface_hub/file_download.py in http_get(url, temp_file, proxies, resume_size, headers, expected_size, displayed_filename, _nb_retries, _tqdm_bar) 523 524 if expected_size is not None and expected_size != temp_file.tell(): --> 525 raise EnvironmentError( 526 consistency_error_message.format( 527 actual_size=temp_file.tell(), OSError: Consistency check failed: file should be of size 12021156 but has size 10804870 (1O4KGHbRt3M_0.avi). This is usually due to network issues while downloading the file. Please retry with `force_download=True`.
尝试清理缓存后重新加载,代码如下,但问题依旧:
from datasets import load_dataset from huggingface_hub import snapshot_download import shutil import os cache_dir = "/root/.cache/huggingface/datasets" # default location in Colab shutil.rmtree(cache_dir, ignore_errors=True) hub_cache = "/root/.cache/huggingface/hub" shutil.rmtree(hub_cache, ignore_errors=True) ds = load_dataset("valiantlynxz/godseye-violence-detection-dataset")
以下是可行的解决方法:
强制单文件重新下载:直接针对损坏的文件
1O4KGHbRt3M_0.avi进行强制下载,使用huggingface_hub的download_file函数指定force_download=True:from huggingface_hub import download_file from datasets import load_dataset # 从数据集页面获取该文件的实际下载URL file_url = "https://huggingface.co/datasets/valiantlynxz/godseye-violence-detection-dataset/resolve/main/[对应文件路径]/1O4KGHbRt3M_0.avi" cache_dir = "/root/.cache/huggingface/datasets" download_file(file_url, cache_dir=cache_dir, force_download=True) ds = load_dataset("valiantlynxz/godseye-violence-detection-dataset")切换Colab运行时:重启Colab会话,或更换硬件加速选项(如从GPU切换到CPU再切回),尝试连接不同的网络节点重新下载。
手动下载后本地加载:
- 从Hugging Face数据集页面手动下载完整数据集压缩包。
- 将压缩包上传到Colab的Google Drive或本地文件系统并解压。
- 从本地路径加载数据集:
from datasets import load_dataset ds = load_dataset("/content/drive/MyDrive/解压后的数据集路径")
联系数据集作者:若多次尝试仍报错,可能是数据集源文件本身损坏,可在数据集页面提交issue联系作者确认文件完整性。
内容的提问来源于stack exchange,提问作者AlecArk
相关产品推荐
相关产品推荐

