Commit 3859800a authored by Anthony Larcher's avatar Anthony Larcher
Browse files

new release

parent 8f28cac0
......@@ -87,6 +87,7 @@
<span class="kn">import</span> <span class="nn">h5py</span>
<span class="kn">from</span> <span class="nn">sidekit.frontend.features</span> <span class="k">import</span> <span class="n">pca_dct</span><span class="p">,</span> <span class="n">shifted_delta_cepstral</span><span class="p">,</span> <span class="n">compute_delta</span><span class="p">,</span> <span class="n">framing</span><span class="p">,</span> <span class="n">dct_basis</span>
<span class="kn">from</span> <span class="nn">sidekit.frontend.io</span> <span class="k">import</span> <span class="n">read_hdf5_segment</span>
<span class="kn">from</span> <span class="nn">sidekit.frontend.vad</span> <span class="k">import</span> <span class="n">label_fusion</span>
<span class="kn">from</span> <span class="nn">sidekit.frontend.normfeat</span> <span class="k">import</span> <span class="n">cms</span><span class="p">,</span> <span class="n">cmvn</span><span class="p">,</span> <span class="n">stg</span><span class="p">,</span> <span class="n">cep_sliding_norm</span><span class="p">,</span> <span class="n">rasta_filt</span>
<span class="kn">from</span> <span class="nn">sidekit.sv_utils</span> <span class="k">import</span> <span class="n">parse_mask</span>
......@@ -375,7 +376,7 @@
<div class="viewcode-block" id="FeaturesServer.get_context"><a class="viewcode-back" href="../api/featuresserver.html#features_server.FeaturesServer.get_context">[docs]</a> <span class="k">def</span> <span class="nf">get_context</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">feat</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Add a left annd right context to each frame.</span>
<span class="sd"> Add a left and right context to each frame.</span>
<span class="sd"> First and last frames are duplicated to provide context at the begining and at the end</span>
<span class="sd"> :param feat: sequence of feature frames (one fame per line)</span>
......@@ -529,62 +530,19 @@
<span class="k">else</span><span class="p">:</span>
<span class="n">h5f</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">features_extractor</span><span class="o">.</span><span class="n">extract</span><span class="p">(</span><span class="n">show</span><span class="p">,</span> <span class="n">channel</span><span class="p">,</span> <span class="n">input_audio_filename</span><span class="o">=</span><span class="n">input_feature_filename</span><span class="p">)</span>
<span class="c1"># Get the selected segment</span>
<span class="n">dataset_length</span> <span class="o">=</span> <span class="n">h5f</span><span class="p">[</span><span class="n">show</span> <span class="o">+</span> <span class="s2">&quot;/&quot;</span> <span class="o">+</span> <span class="nb">next</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="n">show</span><span class="p">]</span><span class="o">.</span><span class="fm">__iter__</span><span class="p">())]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="c1"># Deal with the case where start &lt; 0 or stop &gt; feat.shape[0]</span>
<span class="k">if</span> <span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">start</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">pad_begining</span> <span class="o">=</span> <span class="o">-</span><span class="n">start</span> <span class="k">if</span> <span class="n">start</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="mi">0</span>
<span class="n">start</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="k">if</span> <span class="n">stop</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">stop</span> <span class="o">=</span> <span class="n">dataset_length</span>
<span class="n">pad_end</span> <span class="o">=</span> <span class="n">stop</span> <span class="o">-</span> <span class="n">dataset_length</span> <span class="k">if</span> <span class="n">stop</span> <span class="o">&gt;</span> <span class="n">dataset_length</span> <span class="k">else</span> <span class="mi">0</span>
<span class="n">stop</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">stop</span><span class="p">,</span> <span class="n">dataset_length</span><span class="p">)</span>
<span class="n">global_cmvn</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">global_cmvn</span> <span class="ow">and</span> <span class="ow">not</span> <span class="p">(</span><span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">stop</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">)</span>
<span class="c1"># Get the data between start and stop</span>
<span class="c1"># Concatenate all required datasets</span>
<span class="n">feat</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">global_mean</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">global_std</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="s2">&quot;energy&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span><span class="p">:</span>
<span class="n">feat</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;energy&quot;</span><span class="p">))]</span><span class="o">.</span><span class="n">value</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">stop</span><span class="p">,</span> <span class="n">numpy</span><span class="o">.</span><span class="n">newaxis</span><span class="p">])</span>
<span class="n">global_mean</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;energy_mean&quot;</span><span class="p">))]</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="n">global_std</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;energy_std&quot;</span><span class="p">))]</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="k">if</span> <span class="s2">&quot;cep&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span><span class="p">:</span>
<span class="n">feat</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;cep&quot;</span><span class="p">))][</span><span class="n">start</span><span class="p">:</span><span class="n">stop</span><span class="p">,</span> <span class="p">:])</span>
<span class="n">global_mean</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;cep_mean&quot;</span><span class="p">))]</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="n">global_std</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;cep_std&quot;</span><span class="p">))]</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="k">if</span> <span class="s2">&quot;fb&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span><span class="p">:</span>
<span class="n">feat</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;fb&quot;</span><span class="p">))][</span><span class="n">start</span><span class="p">:</span><span class="n">stop</span><span class="p">,</span> <span class="p">:])</span>
<span class="n">global_mean</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;fb_mean&quot;</span><span class="p">))]</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="n">global_std</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;fb_std&quot;</span><span class="p">))]</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="k">if</span> <span class="s2">&quot;bnf&quot;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span><span class="p">:</span>
<span class="n">feat</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;bnf&quot;</span><span class="p">))][</span><span class="n">start</span><span class="p">:</span><span class="n">stop</span><span class="p">,</span> <span class="p">:])</span>
<span class="n">global_mean</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;bnf_mean&quot;</span><span class="p">))]</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="n">global_std</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">h5f</span><span class="p">[</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;bnf_std&quot;</span><span class="p">))]</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
<span class="n">feat</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">hstack</span><span class="p">(</span><span class="n">feat</span><span class="p">)</span>
<span class="n">global_mean</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">hstack</span><span class="p">(</span><span class="n">global_mean</span><span class="p">)</span>
<span class="n">global_std</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">hstack</span><span class="p">(</span><span class="n">global_std</span><span class="p">)</span>
<span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">if</span> <span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;vad&quot;</span><span class="p">))</span> <span class="ow">in</span> <span class="n">h5f</span><span class="p">:</span>
<span class="n">label</span> <span class="o">=</span> <span class="n">h5f</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">((</span><span class="n">show</span><span class="p">,</span> <span class="s2">&quot;vad&quot;</span><span class="p">)))</span><span class="o">.</span><span class="n">value</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&#39;bool&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">squeeze</span><span class="p">()[</span><span class="n">start</span><span class="p">:</span><span class="n">stop</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">label</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">feat</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;bool&#39;</span><span class="p">)</span>
<span class="c1"># Pad the segment if needed</span>
<span class="n">feat</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="p">((</span><span class="n">pad_begining</span><span class="p">,</span> <span class="n">pad_end</span><span class="p">),</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)),</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;edge&#39;</span><span class="p">)</span>
<span class="n">label</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="p">(</span><span class="n">pad_begining</span><span class="p">,</span> <span class="n">pad_end</span><span class="p">),</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;edge&#39;</span><span class="p">)</span>
<span class="n">stop</span> <span class="o">+=</span> <span class="n">pad_begining</span> <span class="o">+</span> <span class="n">pad_end</span>
<span class="n">h5f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">feat</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">global_mean</span><span class="p">,</span> <span class="n">global_std</span><span class="p">,</span> <span class="n">global_cmvn</span> <span class="o">=</span> <span class="n">read_hdf5_segment</span><span class="p">(</span><span class="n">h5f</span><span class="p">,</span>
<span class="n">show</span><span class="p">,</span>
<span class="n">dataset_list</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span><span class="p">,</span>
<span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">,</span>
<span class="n">start</span><span class="o">=</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="o">=</span><span class="n">stop</span><span class="p">,</span>
<span class="n">global_cmvn</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">global_cmvn</span><span class="p">)</span>
<span class="c1"># Post-process the features and return the features and vad label</span>
<span class="k">if</span> <span class="n">global_cmvn</span><span class="p">:</span>
<span class="n">feat</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">post_processing</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">global_mean</span><span class="p">,</span> <span class="n">global_std</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">feat</span><span class="p">,</span> <span class="n">label</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">post_processing</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="n">label</span><span class="p">)</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">feat</span> <span class="o">=</span> <span class="n">feat</span><span class="p">[:,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask</span><span class="p">]</span>
<span class="k">return</span> <span class="n">feat</span><span class="p">,</span> <span class="n">label</span></div>
......
This diff is collapsed.
......@@ -117,7 +117,7 @@
<span class="n">zf</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="mi">4</span><span class="p">))</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
<span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:</span><span class="mi">4</span><span class="p">],</span> <span class="n">zf</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="n">lfilter</span><span class="p">(</span><span class="n">numerator</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:</span><span class="mi">4</span><span class="p">],</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">zi</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
<span class="c1"># .. but don&#39;t keep any of these values, just output zero at the beginning</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment