import{s as QM,n as SM,o as DM}from"../chunks/scheduler.f7e1785c.js";import{S as BM,i as bM,g as a,s as t,r as b,A as NM,h as J,f as s,c as U,j as gM,u as N,x as n,k as EM,y as dM,a as y,v as d,d as f,t as V,w as Z}from"../chunks/index.9f8f0838.js";import{C as x}from"../chunks/CodeBlock.b3510e34.js";import{H as fM,E as VM}from"../chunks/EditOnGithub.5a9bb8c5.js";function ZM(CM){let T,z,_,R,C,v,j,jM="使用音频数据集的最大挑战之一是它们庞大的规模。仅仅一分钟的CD音质音频（44.1千赫兹，16比特）就需要占用约5MB的存储。一般的音频数据集会含有数个小时的音频录音。",Y,e,eM='在上一小节中，我们使用了MINDS-14的一个非常小的子集，但常见的音频数据集往往要大得多。举个例子<a href="https://huggingface.co/datasets/speechcolab/gigaspeech" rel="nofollow">GigaSpeech from SpeechColab</a>的<code>xs</code>（最小）子集仅有10小时的训练数据，但需要占用超过13GB的存储空间。当我们选择更大的子集时会发生什么呢？其最大的<code>xl</code>子集含有10000小时的训练数据，需要占用超过1TB的存储空间。对大多数人来说，这已经远远超过了我们的硬盘所能容纳的容量。我们需要剁手购买额外的硬盘吗？还是说有其他方法可以让我们绕过存储空间的限制呢？',F,w,wM="🤗 Datasets为你提供了解决方法：流式处理模式。流式处理可以让我们在遍历数据集的过程中渐进地加载数据。与其一次性地下载完整数据集，不如将样本一个个单独加载。在遍历数据集时，我们实时地在有需要时加载和准备好每个样本。通过这样的方式，我们永远只加载我们正在使用的数据，而不加载我们暂时不需要的数据。当我们使用完一个样本时，就继续遍历数据集，加载下一个样本。",W,I,IM="相较于全量加载，流式处理模式主要有三个优点：",$,p,pM="<li>硬盘空间：样本是在遍历数据集的过程中被依次加载到内存中的。由于我们不需要将数据下载到本地，因此也没有任何对硬盘空间的要求，你可以使用任意大小的数据集。</li> <li>下载和处理时间：音频数据集极其庞大并且需要大量的数据下载和处理时间。在流式处理模式下，数据会进行实时的加载和处理，因此当第一个样本准备完成时我们就可以开始训练。</li> <li>测试简单：无需下载完整的数据集，仅用少量的样本就可以测试你的训练脚本。</li>",G,A,AM="在使用流式处理模式时，有一点需要警惕：在全量下载数据集时，所有的原始数据和处理后的数据都会在硬盘内本地存储。如果我们想要重复利用这些数据，我们可以跳过下载和处理过程，直接从硬盘中加载数据。这意味着我们在重复利用数据时，仅在第一次时需要下载和处理数据。",X,u,uM="但在流式处理模式中，数据不会被下载到硬盘内。因此，我们的下载和预处理过程都不会被保存。在重复利用数据集时，我们必须要重复进行下载和预处理才做。因此，如果你计划重复使用数据集，我们还是推荐将数据集下载到本地硬盘。",L,i,iM="如何启用流式处理模式呢？很简单！只需要在加载数据集时传入<code>streaming=True</code>参数即可。我们会帮你做好其他工作：",O,o,H,r,oM="与我们在全量下载MINDS-14子集时进行的预处理操作相同，你也可以在流式数据集上使用同样的预处理操作。",P,c,rM="唯一的区别是我们现在无法使用Python的索引功能访问单个的样本（例如<code>gigaspeech[&quot;train&quot;][sample_idx]</code>）。相反地，我们需要遍历整个数据集。你可以在流式数据集这这样访问单个样本：",K,h,MM,m,cM="<strong>输出：</strong>",lM,q,sM,g,hM="你可以使用<code>take()</code>函数来预览数据集的几个样本。让我们看看gigaspeech数据集的前两个样本：",yM,E,tM,Q,mM="<strong>输出：</strong>",UM,S,aM,D,qM="流式处理模式可以助力你的研究进入更高的阶段：现在你不仅可以利用庞大的数据集，而且可以在无需担忧硬盘空间的情况下使用多个数据集来评估你的系统。与使用单个数据集进行评估相比，多数据集评估可以更好地体现系统的泛化性能。",JM,B,nM,k,TM;return C=new fM({props:{title:"音频数据的流式加载",local:"音频数据的流式加载",headingTag:"h1"}}),o=new x({props:{code:"Z2lnYXNwZWVjaCUyMCUzRCUyMGxvYWRfZGF0YXNldCglMjJzcGVlY2hjb2xhYiUyRmdpZ2FzcGVlY2glMjIlMkMlMjAlMjJ4cyUyMiUyQyUyMHN0cmVhbWluZyUzRFRydWUp",highlighted:'gigaspeech = load_dataset(<span class="hljs-string">&quot;speechcolab/gigaspeech&quot;</span>, <span class="hljs-string">&quot;xs&quot;</span>, streaming=<span class="hljs-literal">True</span>)',wrap:!1}}),h=new x({props:{code:"bmV4dChpdGVyKGdpZ2FzcGVlY2glNUIlMjJ0cmFpbiUyMiU1RCkp",highlighted:'<span class="hljs-built_in">next</span>(<span class="hljs-built_in">iter</span>(gigaspeech[<span class="hljs-string">&quot;train&quot;</span>]))',wrap:!1}}),q=new x({props:{code:"JTdCJTBBJTIwJTIwJTIwJTIwJTIyc2VnbWVudF9pZCUyMiUzQSUyMCUyMllPVTAwMDAwMDAzMTVfUzAwMDA2NjAlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJzcGVha2VyJTIyJTNBJTIwJTIyTiUyRkElMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJ0ZXh0JTIyJTNBJTIwJTIyQVMlMjBUSEVZJ1JFJTIwTEVBVklORyUyMCUzQ0NPTU1BJTNFJTIwQ0FOJTIwS0FTSCUyMFBVTEwlMjBaQUhSQSUyMEFTSURFJTIwUkVBTExZJTIwUVVJQ0tMWSUyMCUzQ1FVRVNUSU9OTUFSSyUzRSUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMmF1ZGlvJTIyJTNBJTIwJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIycGF0aCUyMiUzQSUyMCUyMnhzX2NodW5rc18wMDAwJTJGWU9VMDAwMDAwMDMxNV9TMDAwMDY2MC53YXYlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjJhcnJheSUyMiUzQSUyMGFycmF5KCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCU1QjAuMDAwNTE4OCUyQyUyMDAuMDAwODU0NDklMkMlMjAwLjAwMDEyMjA3JTJDJTIwLi4uJTJDJTIwMC4wMDEyNTEyMiUyQyUyMDAuMDAwNzYyOTQlMkMlMjAwLjAwMDM2NjIxJTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwKSUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMnNhbXBsaW5nX3JhdGUlMjIlM0ElMjAxNjAwMCUyQyUwQSUyMCUyMCUyMCUyMCU3RCUyQyUwQSUyMCUyMCUyMCUyMCUyMmJlZ2luX3RpbWUlMjIlM0ElMjAyOTQxLjg5JTJDJTBBJTIwJTIwJTIwJTIwJTIyZW5kX3RpbWUlMjIlM0ElMjAyOTQ1LjA3JTJDJTBBJTIwJTIwJTIwJTIwJTIyYXVkaW9faWQlMjIlM0ElMjAlMjJZT1UwMDAwMDAwMzE1JTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIydGl0bGUlMjIlM0ElMjAlMjJSZXR1cm4lMjB0byUyMFZhc3NlbGhlaW0lMjAlN0MlMjBDcml0aWNhbCUyMFJvbGUlM0ElMjBWT1glMjBNQUNISU5BJTIwJTdDJTIwRXBpc29kZSUyMDQzJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIydXJsJTIyJTNBJTIwJTIyaHR0cHMlM0ElMkYlMkZ3d3cueW91dHViZS5jb20lMkZ3YXRjaCUzRnYlM0R6cjJuMWZMVmFzVSUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMnNvdXJjZSUyMiUzQSUyMDIlMkMlMEElMjAlMjAlMjAlMjAlMjJjYXRlZ29yeSUyMiUzQSUyMDI0JTJDJTBBJTIwJTIwJTIwJTIwJTIyb3JpZ2luYWxfZnVsbF9wYXRoJTIyJTNBJTIwJTIyYXVkaW8lMkZ5b3V0dWJlJTJGUDAwMDQlMkZZT1UwMDAwMDAwMzE1Lm9wdXMlMjIlMkMlMEElN0Q=",highlighted:`{
    <span class="hljs-string">&quot;segment_id&quot;</span>: <span class="hljs-string">&quot;YOU0000000315_S0000660&quot;</span>,
    <span class="hljs-string">&quot;speaker&quot;</span>: <span class="hljs-string">&quot;N/A&quot;</span>,
    <span class="hljs-string">&quot;text&quot;</span>: <span class="hljs-string">&quot;AS THEY&#x27;RE LEAVING &lt;COMMA&gt; CAN KASH PULL ZAHRA ASIDE REALLY QUICKLY &lt;QUESTIONMARK&gt;&quot;</span>,
    <span class="hljs-string">&quot;audio&quot;</span>: {
        <span class="hljs-string">&quot;path&quot;</span>: <span class="hljs-string">&quot;xs_chunks_0000/YOU0000000315_S0000660.wav&quot;</span>,
        <span class="hljs-string">&quot;array&quot;</span>: <span class="hljs-built_in">array</span>(
            <span class="hljs-selector-attr">[0.0005188, 0.00085449, 0.00012207, ..., 0.00125122, 0.00076294, 0.00036621]</span>
        ),
        <span class="hljs-string">&quot;sampling_rate&quot;</span>: <span class="hljs-number">16000</span>,
    },
    <span class="hljs-string">&quot;begin_time&quot;</span>: <span class="hljs-number">2941.89</span>,
    <span class="hljs-string">&quot;end_time&quot;</span>: <span class="hljs-number">2945.07</span>,
    <span class="hljs-string">&quot;audio_id&quot;</span>: <span class="hljs-string">&quot;YOU0000000315&quot;</span>,
    <span class="hljs-string">&quot;title&quot;</span>: <span class="hljs-string">&quot;Return to Vasselheim | Critical Role: VOX MACHINA | Episode 43&quot;</span>,
    <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;https://www.youtube.com/watch?v=zr2n1fLVasU&quot;</span>,
    <span class="hljs-string">&quot;source&quot;</span>: <span class="hljs-number">2</span>,
    <span class="hljs-string">&quot;category&quot;</span>: <span class="hljs-number">24</span>,
    <span class="hljs-string">&quot;original_full_path&quot;</span>: <span class="hljs-string">&quot;audio/youtube/P0004/YOU0000000315.opus&quot;</span>,
}`,wrap:!1}}),E=new x({props:{code:"Z2lnYXNwZWVjaF9oZWFkJTIwJTNEJTIwZ2lnYXNwZWVjaCU1QiUyMnRyYWluJTIyJTVELnRha2UoMiklMEFsaXN0KGdpZ2FzcGVlY2hfaGVhZCk=",highlighted:`gigaspeech_head = gigaspeech[<span class="hljs-string">&quot;train&quot;</span>].take(<span class="hljs-number">2</span>)
<span class="hljs-built_in">list</span>(gigaspeech_head)`,wrap:!1}}),S=new x({props:{code:" ",highlighted:`[
    {
        <span class="hljs-string">&quot;segment_id&quot;</span>: <span class="hljs-string">&quot;YOU0000000315_S0000660&quot;</span>,
        <span class="hljs-string">&quot;speaker&quot;</span>: <span class="hljs-string">&quot;N/A&quot;</span>,
        <span class="hljs-string">&quot;text&quot;</span>: <span class="hljs-string">&quot;AS THEY&#x27;RE LEAVING &lt;COMMA&gt; CAN KASH PULL ZAHRA ASIDE REALLY QUICKLY &lt;QUESTIONMARK&gt;&quot;</span>,
        <span class="hljs-string">&quot;audio&quot;</span>: {
            <span class="hljs-string">&quot;path&quot;</span>: <span class="hljs-string">&quot;xs_chunks_0000/YOU0000000315_S0000660.wav&quot;</span>,
            <span class="hljs-string">&quot;array&quot;</span>: array(
                [
                    <span class="hljs-number">0.0005188</span>,
                    <span class="hljs-number">0.00085449</span>,
                    <span class="hljs-number">0.00012207</span>,
                    ...,
                    <span class="hljs-number">0.00125122</span>,
                    <span class="hljs-number">0.00076294</span>,
                    <span class="hljs-number">0.00036621</span>,
                ]
            ),
            <span class="hljs-string">&quot;sampling_rate&quot;</span>: <span class="hljs-number">16000</span>,
        },
        <span class="hljs-string">&quot;begin_time&quot;</span>: <span class="hljs-number">2941.89</span>,
        <span class="hljs-string">&quot;end_time&quot;</span>: <span class="hljs-number">2945.07</span>,
        <span class="hljs-string">&quot;audio_id&quot;</span>: <span class="hljs-string">&quot;YOU0000000315&quot;</span>,
        <span class="hljs-string">&quot;title&quot;</span>: <span class="hljs-string">&quot;Return to Vasselheim | Critical Role: VOX MACHINA | Episode 43&quot;</span>,
        <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;https://www.youtube.com/watch?v=zr2n1fLVasU&quot;</span>,
        <span class="hljs-string">&quot;source&quot;</span>: <span class="hljs-number">2</span>,
        <span class="hljs-string">&quot;category&quot;</span>: <span class="hljs-number">24</span>,
        <span class="hljs-string">&quot;original_full_path&quot;</span>: <span class="hljs-string">&quot;audio/youtube/P0004/YOU0000000315.opus&quot;</span>,
    },
    {
        <span class="hljs-string">&quot;segment_id&quot;</span>: <span class="hljs-string">&quot;AUD0000001043_S0000775&quot;</span>,
        <span class="hljs-string">&quot;speaker&quot;</span>: <span class="hljs-string">&quot;N/A&quot;</span>,
        <span class="hljs-string">&quot;text&quot;</span>: <span class="hljs-string">&quot;SIX TOMATOES &lt;PERIOD&gt;&quot;</span>,
        <span class="hljs-string">&quot;audio&quot;</span>: {
            <span class="hljs-string">&quot;path&quot;</span>: <span class="hljs-string">&quot;xs_chunks_0000/AUD0000001043_S0000775.wav&quot;</span>,
            <span class="hljs-string">&quot;array&quot;</span>: array(
                [
                    <span class="hljs-number">1.43432617</span>e-03,
                    <span class="hljs-number">1.37329102</span>e-03,
                    <span class="hljs-number">1.31225586</span>e-03,
                    ...,
                    <span class="hljs-number">-6.10351562</span>e-05,
                    <span class="hljs-number">-1.22070312</span>e-04,
                    <span class="hljs-number">-1.83105469</span>e-04,
                ]
            ),
            <span class="hljs-string">&quot;sampling_rate&quot;</span>: <span class="hljs-number">16000</span>,
        },
        <span class="hljs-string">&quot;begin_time&quot;</span>: <span class="hljs-number">3673.96</span>,
        <span class="hljs-string">&quot;end_time&quot;</span>: <span class="hljs-number">3675.26</span>,
        <span class="hljs-string">&quot;audio_id&quot;</span>: <span class="hljs-string">&quot;AUD0000001043&quot;</span>,
        <span class="hljs-string">&quot;title&quot;</span>: <span class="hljs-string">&quot;Asteroid of Fear&quot;</span>,
        <span class="hljs-string">&quot;url&quot;</span>: <span class="hljs-string">&quot;http//www.archive.org/download/asteroid_of_fear_1012_librivox/asteroid_of_fear_1012_librivox_64kb_mp3.zip&quot;</span>,
        <span class="hljs-string">&quot;source&quot;</span>: <span class="hljs-number">0</span>,
        <span class="hljs-string">&quot;category&quot;</span>: <span class="hljs-number">28</span>,
        <span class="hljs-string">&quot;original_full_path&quot;</span>: <span class="hljs-string">&quot;audio/audiobook/P0011/AUD0000001043.opus&quot;</span>,
    },
]`,wrap:!1}}),B=new VM({props:{source:"https://github.com/huggingface/audio-transformers-course/blob/main/chapters/zh-CN/chapter1/streaming.mdx"}}),{c(){T=a("meta"),z=t(),_=a("p"),R=t(),b(C.$$.fragment),v=t(),j=a("p"),j.textContent=jM,Y=t(),e=a("p"),e.innerHTML=eM,F=t(),w=a("p"),w.textContent=wM,W=t(),I=a("p"),I.textContent=IM,$=t(),p=a("ul"),p.innerHTML=pM,G=t(),A=a("p"),A.textContent=AM,X=t(),u=a("p"),u.textContent=uM,L=t(),i=a("p"),i.innerHTML=iM,O=t(),b(o.$$.fragment),H=t(),r=a("p"),r.textContent=oM,P=t(),c=a("p"),c.innerHTML=rM,K=t(),b(h.$$.fragment),MM=t(),m=a("p"),m.innerHTML=cM,lM=t(),b(q.$$.fragment),sM=t(),g=a("p"),g.innerHTML=hM,yM=t(),b(E.$$.fragment),tM=t(),Q=a("p"),Q.innerHTML=mM,UM=t(),b(S.$$.fragment),aM=t(),D=a("p"),D.textContent=qM,JM=t(),b(B.$$.fragment),nM=t(),k=a("p"),this.h()},l(M){const l=NM("svelte-u9bgzb",document.head);T=J(l,"META",{name:!0,content:!0}),l.forEach(s),z=U(M),_=J(M,"P",{}),gM(_).forEach(s),R=U(M),N(C.$$.fragment,M),v=U(M),j=J(M,"P",{"data-svelte-h":!0}),n(j)!=="svelte-clzs67"&&(j.textContent=jM),Y=U(M),e=J(M,"P",{"data-svelte-h":!0}),n(e)!=="svelte-1brhrul"&&(e.innerHTML=eM),F=U(M),w=J(M,"P",{"data-svelte-h":!0}),n(w)!=="svelte-haggy6"&&(w.textContent=wM),W=U(M),I=J(M,"P",{"data-svelte-h":!0}),n(I)!=="svelte-1av16hk"&&(I.textContent=IM),$=U(M),p=J(M,"UL",{"data-svelte-h":!0}),n(p)!=="svelte-3p579b"&&(p.innerHTML=pM),G=U(M),A=J(M,"P",{"data-svelte-h":!0}),n(A)!=="svelte-1xsrwtz"&&(A.textContent=AM),X=U(M),u=J(M,"P",{"data-svelte-h":!0}),n(u)!=="svelte-49rd2y"&&(u.textContent=uM),L=U(M),i=J(M,"P",{"data-svelte-h":!0}),n(i)!=="svelte-hbj4lj"&&(i.innerHTML=iM),O=U(M),N(o.$$.fragment,M),H=U(M),r=J(M,"P",{"data-svelte-h":!0}),n(r)!=="svelte-o08v25"&&(r.textContent=oM),P=U(M),c=J(M,"P",{"data-svelte-h":!0}),n(c)!=="svelte-1jnkvn1"&&(c.innerHTML=rM),K=U(M),N(h.$$.fragment,M),MM=U(M),m=J(M,"P",{"data-svelte-h":!0}),n(m)!=="svelte-1yevus4"&&(m.innerHTML=cM),lM=U(M),N(q.$$.fragment,M),sM=U(M),g=J(M,"P",{"data-svelte-h":!0}),n(g)!=="svelte-1pz6h55"&&(g.innerHTML=hM),yM=U(M),N(E.$$.fragment,M),tM=U(M),Q=J(M,"P",{"data-svelte-h":!0}),n(Q)!=="svelte-1yevus4"&&(Q.innerHTML=mM),UM=U(M),N(S.$$.fragment,M),aM=U(M),D=J(M,"P",{"data-svelte-h":!0}),n(D)!=="svelte-jzmfgk"&&(D.textContent=qM),JM=U(M),N(B.$$.fragment,M),nM=U(M),k=J(M,"P",{}),gM(k).forEach(s),this.h()},h(){EM(T,"name","hf:doc:metadata"),EM(T,"content",_M)},m(M,l){dM(document.head,T),y(M,z,l),y(M,_,l),y(M,R,l),d(C,M,l),y(M,v,l),y(M,j,l),y(M,Y,l),y(M,e,l),y(M,F,l),y(M,w,l),y(M,W,l),y(M,I,l),y(M,$,l),y(M,p,l),y(M,G,l),y(M,A,l),y(M,X,l),y(M,u,l),y(M,L,l),y(M,i,l),y(M,O,l),d(o,M,l),y(M,H,l),y(M,r,l),y(M,P,l),y(M,c,l),y(M,K,l),d(h,M,l),y(M,MM,l),y(M,m,l),y(M,lM,l),d(q,M,l),y(M,sM,l),y(M,g,l),y(M,yM,l),d(E,M,l),y(M,tM,l),y(M,Q,l),y(M,UM,l),d(S,M,l),y(M,aM,l),y(M,D,l),y(M,JM,l),d(B,M,l),y(M,nM,l),y(M,k,l),TM=!0},p:SM,i(M){TM||(f(C.$$.fragment,M),f(o.$$.fragment,M),f(h.$$.fragment,M),f(q.$$.fragment,M),f(E.$$.fragment,M),f(S.$$.fragment,M),f(B.$$.fragment,M),TM=!0)},o(M){V(C.$$.fragment,M),V(o.$$.fragment,M),V(h.$$.fragment,M),V(q.$$.fragment,M),V(E.$$.fragment,M),V(S.$$.fragment,M),V(B.$$.fragment,M),TM=!1},d(M){M&&(s(z),s(_),s(R),s(v),s(j),s(Y),s(e),s(F),s(w),s(W),s(I),s($),s(p),s(G),s(A),s(X),s(u),s(L),s(i),s(O),s(H),s(r),s(P),s(c),s(K),s(MM),s(m),s(lM),s(sM),s(g),s(yM),s(tM),s(Q),s(UM),s(aM),s(D),s(JM),s(nM),s(k)),s(T),Z(C,M),Z(o,M),Z(h,M),Z(q,M),Z(E,M),Z(S,M),Z(B,M)}}}const _M='{"title":"音频数据的流式加载","local":"音频数据的流式加载","sections":[],"depth":1}';function kM(CM){return DM(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class YM extends BM{constructor(T){super(),bM(this,T,kM,ZM,QM,{})}}export{YM as component};
