features_server.html 95.3 KB
Newer Older
Anthony Larcher's avatar
Anthony Larcher committed
1
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Anthony Larcher's avatar
Anthony Larcher committed
3
4


Anthony Larcher's avatar
Anthony Larcher committed
5
6
7
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Anthony Larcher's avatar
Anthony Larcher committed
8
    
Anthony Larcher's avatar
Anthony Larcher committed
9
    <title>features_server &#8212; SIDEKIT  documentation</title>
Anthony Larcher's avatar
Anthony Larcher committed
10
    
Anthony Larcher's avatar
Anthony Larcher committed
11
    <link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
Anthony Larcher's avatar
Anthony Larcher committed
12
13
14
15
16
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
Anthony Larcher's avatar
doc    
Anthony Larcher committed
17
        VERSION:     '',
Anthony Larcher's avatar
Anthony Larcher committed
18
19
20
21
22
23
24
25
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
Anthony Larcher's avatar
Anthony Larcher committed
26
27
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
Anthony Larcher's avatar
doc    
Anthony Larcher committed
28
    <link rel="top" title="SIDEKIT  documentation" href="../index.html" />
Anthony Larcher's avatar
Anthony Larcher committed
29
30
31
32
33
34
35
36
37
38
39
40
    <link rel="up" title="Module code" href="index.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
Anthony Larcher's avatar
Anthony Larcher committed
41
42
        <li class="nav-item nav-item-0"><a href="../index.html">SIDEKIT  documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Module code</a> &#187;</li> 
Anthony Larcher's avatar
Anthony Larcher committed
43
      </ul>
Anthony Larcher's avatar
Anthony Larcher committed
44
45
46
47
48
49
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
Anthony Larcher's avatar
Anthony Larcher committed
50
51
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
Anthony Larcher's avatar
Anthony Larcher committed
52
53
54
55
56
57
58
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
Anthony Larcher's avatar
Anthony Larcher committed
59
60
61
62
63

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
Anthony Larcher's avatar
Anthony Larcher committed
64
65
            
  <h1>Source code for features_server</h1><div class="highlight"><pre>
Anthony Larcher's avatar
Anthony Larcher committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="c1">#</span>
<span class="c1"># This file is part of SIDEKIT.</span>
<span class="c1">#</span>
<span class="c1"># SIDEKIT is a python package for speaker verification.</span>
<span class="c1"># Home page: http://www-lium.univ-lemans.fr/sidekit/</span>
<span class="c1">#</span>
<span class="c1"># SIDEKIT is a python package for speaker verification.</span>
<span class="c1"># Home page: http://www-lium.univ-lemans.fr/sidekit/</span>
<span class="c1">#    </span>
<span class="c1"># SIDEKIT is free software: you can redistribute it and/or modify</span>
<span class="c1"># it under the terms of the GNU LLesser General Public License as </span>
<span class="c1"># published by the Free Software Foundation, either version 3 of the License, </span>
<span class="c1"># or (at your option) any later version.</span>
<span class="c1">#</span>
<span class="c1"># SIDEKIT is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c1"># GNU Lesser General Public License for more details.</span>
<span class="c1">#</span>
<span class="c1"># You should have received a copy of the GNU Lesser General Public License</span>
<span class="c1"># along with SIDEKIT.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
Anthony Larcher's avatar
Anthony Larcher committed
88
89

<span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
minor    
Anthony Larcher committed
90
<span class="sd">Copyright 2014-2016 Sylvain Meignier and Anthony Larcher</span>
Anthony Larcher's avatar
Anthony Larcher committed
91
92
93
94

<span class="sd">    :mod:`features_server` provides methods to manage features</span>

<span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
95
<span class="kn">import</span> <span class="nn">numpy</span>
Anthony Larcher's avatar
Anthony Larcher committed
96
<span class="kn">import</span> <span class="nn">logging</span>
Anthony Larcher's avatar
Anthony Larcher committed
97
98
<span class="kn">import</span> <span class="nn">h5py</span>

Anthony Larcher's avatar
Anthony Larcher committed
99
100
101
102
<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.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>
Anthony Larcher's avatar
Anthony Larcher committed
103
104


Anthony Larcher's avatar
Anthony Larcher committed
105
106
107
108
109
110
111
<span class="n">__license__</span> <span class="o">=</span> <span class="s2">&quot;LGPL&quot;</span>
<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Anthony Larcher &amp; Sylvain Meignier&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2014-2016 Anthony Larcher&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Anthony Larcher&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;anthony.larcher@univ-lemans.fr&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Production&quot;</span>
<span class="n">__docformat__</span> <span class="o">=</span> <span class="s1">&#39;reStructuredText&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
112
113


Anthony Larcher's avatar
Anthony Larcher committed
114
<div class="viewcode-block" id="FeaturesServer"><a class="viewcode-back" href="../featuresserver.html#features_server.FeaturesServer">[docs]</a><span class="k">class</span> <span class="nc">FeaturesServer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
115
    <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
116
117
118
<span class="sd">    Management of features. FeaturesServer instances load datasets from a HDF5 files</span>
<span class="sd">    (that can be read from disk or produced by a FeaturesExtractor object)</span>
<span class="sd">    Datasets read from one or many files are concatenated and processed</span>
Anthony Larcher's avatar
Anthony Larcher committed
119
120
<span class="sd">    &quot;&quot;&quot;</span>

Anthony Larcher's avatar
Anthony Larcher committed
121
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
                 <span class="n">features_extractor</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">feature_filename_structure</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">sources</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">dataset_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">mask</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">feat_norm</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">global_cmvn</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">dct_pca</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">dct_pca_config</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">sdc</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                 <span class="n">sdc_config</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">delta</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">double_delta</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">delta_filter</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">context</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">traps_dct_nb</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">rasta</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">keep_all_features</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initialize a FeaturesServer for two cases:</span>
<span class="sd">        1. each call to load will load datasets from a single file. This mode requires to provide a dataset_list</span>
<span class="sd">        (lists of datasets to load from each file.</span>
<span class="sd">        2. each call to load will load datasets from several files (possibly several datasets from each file)</span>
<span class="sd">        and concatenate them. In this mode, you should provide a FeaturesServer for each source, thus, datasets</span>
<span class="sd">        read from each source can be post-processed independently before being concatenated with others. The dataset</span>
<span class="sd">        resulting from the concatenation from all sources is then post-processed.</span>

<span class="sd">        :param features_extractor: a FeaturesExtractor if required to extract features from audio file</span>
<span class="sd">        if None, data are loaded from an existing HDF5 file</span>
<span class="sd">        :param feature_filename_structure: structure of the filename to use to load HDF5 files</span>
<span class="sd">        :param sources: tuple of sources to load features different files (optional: for the case where datasets</span>
<span class="sd">        are loaded from several files and concatenated.</span>
<span class="sd">        :param dataset_list: string of the form &#39;[&quot;cep&quot;, &quot;fb&quot;, vad&quot;, energy&quot;, &quot;bnf&quot;]&#39; (only when loading datasets</span>
<span class="sd">        from a single file) list of datasets to load.</span>
<span class="sd">        :param mask: string of the form &#39;[1-3,10,15-20]&#39; mask to apply on the concatenated dataset</span>
<span class="sd">        to select specific components. In this example, coefficients 1,2,3,10,15,16,17,18,19,20 are kept</span>
<span class="sd">        In this example,</span>
<span class="sd">        :param feat_norm: tpye of normalization to apply as post-processing</span>
<span class="sd">        :param global_cmvn: boolean, if True, use a global mean and std when normalizing the frames</span>
<span class="sd">        :param dct_pca: if True, add temporal context by using a PCA-DCT approach</span>
<span class="sd">        :param dct_pca_config: configuration of the PCA-DCT, default is (12, 12, none)</span>
<span class="sd">        :param sdc: if True, compute shifted delta cepstra coefficients</span>
<span class="sd">        :param sdc_config: configuration to compute sdc coefficients, default is (1,3,7)</span>
<span class="sd">        :param delta: if True, append the first order derivative</span>
<span class="sd">        :param double_delta: if True, append the second order derivative</span>
<span class="sd">        :param delta_filter: coefficients of the filter used to compute delta coefficients</span>
<span class="sd">        :param context: add a left and right context, default is (0,0)</span>
<span class="sd">        :param traps_dct_nb: number of DCT coefficients to keep when computing TRAP coefficients</span>
<span class="sd">        :param rasta: if True, perform RASTA filtering</span>
<span class="sd">        :param keep_all_features: boolean, if True, keep all features, if False, keep frames according to the vad labels</span>
<span class="sd">        :return:</span>
Anthony Larcher's avatar
Anthony Larcher committed
173
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
174
175
        <span class="bp">self</span><span class="o">.</span><span class="n">features_extractor</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1">&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
176
        <span class="bp">self</span><span class="o">.</span><span class="n">sources</span> <span class="o">=</span> <span class="p">()</span>
Anthony Larcher's avatar
Anthony Larcher committed
177
178
179
180
181
182
183
184
185
        <span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="c1"># Post processing options</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mask</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">global_cmvn</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</span> <span class="o">=</span> <span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sdc</span> <span class="o">=</span> <span class="kc">False</span>
Anthony Larcher's avatar
Anthony Larcher committed
186
        <span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
187
188
        <span class="bp">self</span><span class="o">.</span><span class="n">delta</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">double_delta</span> <span class="o">=</span> <span class="kc">False</span>
Anthony Larcher's avatar
Anthony Larcher committed
189
190
191
        <span class="bp">self</span><span class="o">.</span><span class="n">delta_filter</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="o">.</span><span class="mi">25</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">,</span> <span class="o">.</span><span class="mi">25</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-.</span><span class="mi">25</span><span class="p">,</span> <span class="o">-.</span><span class="mi">5</span><span class="p">,</span> <span class="o">-.</span><span class="mi">25</span><span class="p">])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">context</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="bp">self</span><span class="o">.</span><span class="n">traps_dct_nb</span> <span class="o">=</span> <span class="mi">0</span>
Anthony Larcher's avatar
Anthony Larcher committed
192
193
        <span class="bp">self</span><span class="o">.</span><span class="n">rasta</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">keep_all_features</span> <span class="o">=</span> <span class="kc">True</span>
Anthony Larcher's avatar
Anthony Larcher committed
194

Anthony Larcher's avatar
Anthony Larcher committed
195
        <span class="k">if</span> <span class="n">features_extractor</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
196
            <span class="bp">self</span><span class="o">.</span><span class="n">features_extractor</span> <span class="o">=</span> <span class="n">features_extractor</span>
Anthony Larcher's avatar
Anthony Larcher committed
197
        <span class="k">if</span> <span class="n">feature_filename_structure</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
198
            <span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span> <span class="o">=</span> <span class="n">feature_filename_structure</span>
Anthony Larcher's avatar
Anthony Larcher committed
199
        <span class="k">if</span> <span class="n">sources</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
200
            <span class="bp">self</span><span class="o">.</span><span class="n">sources</span> <span class="o">=</span> <span class="n">sources</span>
Anthony Larcher's avatar
Anthony Larcher committed
201
        <span class="k">if</span> <span class="n">dataset_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
202
            <span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span> <span class="o">=</span> <span class="n">dataset_list</span>
Anthony Larcher's avatar
Anthony Larcher committed
203
        <span class="k">if</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>
Anthony Larcher's avatar
Anthony Larcher committed
204
            <span class="bp">self</span><span class="o">.</span><span class="n">mask</span> <span class="o">=</span> <span class="n">parse_mask</span><span class="p">(</span><span class="n">mask</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
205
        <span class="k">if</span> <span class="n">feat_norm</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
206
            <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">=</span> <span class="n">feat_norm</span>
Anthony Larcher's avatar
Anthony Larcher committed
207
        <span class="k">if</span> <span class="n">global_cmvn</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
208
            <span class="bp">self</span><span class="o">.</span><span class="n">global_cmvn</span> <span class="o">=</span> <span class="n">global_cmvn</span>
Anthony Larcher's avatar
Anthony Larcher committed
209
        <span class="k">if</span> <span class="n">dct_pca</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
210
            <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca</span> <span class="o">=</span> <span class="n">dct_pca</span>
Anthony Larcher's avatar
Anthony Larcher committed
211
        <span class="k">if</span> <span class="n">dct_pca_config</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
212
            <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</span> <span class="o">=</span> <span class="n">dct_pca_config</span>
Anthony Larcher's avatar
Anthony Larcher committed
213
        <span class="k">if</span> <span class="n">sdc</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
214
            <span class="bp">self</span><span class="o">.</span><span class="n">sdc</span> <span class="o">=</span> <span class="n">sdc</span>
Anthony Larcher's avatar
Anthony Larcher committed
215
        <span class="k">if</span> <span class="n">sdc_config</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
216
            <span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span> <span class="o">=</span> <span class="n">sdc_config</span>
Anthony Larcher's avatar
Anthony Larcher committed
217
        <span class="k">if</span> <span class="n">delta</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
218
            <span class="bp">self</span><span class="o">.</span><span class="n">delta</span> <span class="o">=</span> <span class="n">delta</span>
Anthony Larcher's avatar
Anthony Larcher committed
219
        <span class="k">if</span> <span class="n">double_delta</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
220
            <span class="bp">self</span><span class="o">.</span><span class="n">double_delta</span> <span class="o">=</span> <span class="n">double_delta</span>
Anthony Larcher's avatar
Anthony Larcher committed
221
        <span class="k">if</span> <span class="n">delta_filter</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
222
            <span class="bp">self</span><span class="o">.</span><span class="n">delta_filter</span> <span class="o">=</span> <span class="n">delta_filter</span>
Anthony Larcher's avatar
Anthony Larcher committed
223
        <span class="k">if</span> <span class="n">context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
224
            <span class="bp">self</span><span class="o">.</span><span class="n">context</span> <span class="o">=</span> <span class="n">context</span>
Anthony Larcher's avatar
Anthony Larcher committed
225
        <span class="k">if</span> <span class="n">traps_dct_nb</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
226
            <span class="bp">self</span><span class="o">.</span><span class="n">traps_dct_nb</span> <span class="o">=</span> <span class="n">traps_dct_nb</span>
Anthony Larcher's avatar
Anthony Larcher committed
227
        <span class="k">if</span> <span class="n">rasta</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
228
            <span class="bp">self</span><span class="o">.</span><span class="n">rasta</span> <span class="o">=</span> <span class="n">rasta</span>
Anthony Larcher's avatar
Anthony Larcher committed
229
        <span class="k">if</span> <span class="n">keep_all_features</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
230
            <span class="bp">self</span><span class="o">.</span><span class="n">keep_all_features</span> <span class="o">=</span> <span class="n">keep_all_features</span>
Anthony Larcher's avatar
Anthony Larcher committed
231

Anthony Larcher's avatar
Anthony Larcher committed
232
233
234
235
        <span class="bp">self</span><span class="o">.</span><span class="n">show</span> <span class="o">=</span> <span class="s1">&#39;empty&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">input_feature_filename</span> <span class="o">=</span> <span class="s1">&#39;empty&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">start_stop</span> <span class="o">=</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span> <span class="o">=</span> <span class="kc">None</span>
Anthony Larcher's avatar
Anthony Larcher committed
236
237
238
239

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>

Anthony Larcher's avatar
Anthony Larcher committed
240
<span class="sd">        :return: a string to display the object</span>
Anthony Larcher's avatar
Anthony Larcher committed
241
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
242
243
244
245
246
247
248
249
250
        <span class="n">ch</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1"> show: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">show</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1"> input_feature_filename: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">input_feature_filename</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1"> feature_filename_structure: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1">  </span><span class="se">\n</span><span class="s1">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1">  </span><span class="se">\n\n</span><span class="s1">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t</span><span class="s1"> Post processing options: </span><span class="se">\n</span><span class="s1">&#39;</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> mask: </span><span class="si">{}</span><span class="s1">  </span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</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="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> feat_norm: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> dct_pca: </span><span class="si">{}</span><span class="s1">, dct_pca_config: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dct_pca</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
251
                                                               <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
252
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> sdc: </span><span class="si">{}</span><span class="s1">, sdc_config: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sdc</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
253
                                                       <span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
254
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> delta: </span><span class="si">{}</span><span class="s1">, double_delta: </span><span class="si">{}</span><span class="s1">, delta_filter: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">delta</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
255
256
                                                                             <span class="bp">self</span><span class="o">.</span><span class="n">double_delta</span><span class="p">,</span>
                                                                             <span class="bp">self</span><span class="o">.</span><span class="n">delta_filter</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
257
258
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> rasta: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rasta</span><span class="p">)</span>
        <span class="n">ch</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\t\t</span><span class="s1"> keep_all_features: </span><span class="si">{}</span><span class="s1"> </span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">keep_all_features</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
259

Anthony Larcher's avatar
Anthony Larcher committed
260
261
        <span class="k">return</span> <span class="n">ch</span>

Anthony Larcher's avatar
Anthony Larcher committed
262
<div class="viewcode-block" id="FeaturesServer.post_processing"><a class="viewcode-back" href="../featuresserver.html#features_server.FeaturesServer.post_processing">[docs]</a>    <span class="k">def</span> <span class="nf">post_processing</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">label</span><span class="p">,</span> <span class="n">global_mean</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">global_std</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
263
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
264
265
266
267
268
269
270
271
272
<span class="sd">        After cepstral coefficients, filter banks or bottleneck parameters are computed or read from file</span>
<span class="sd">        post processing is applied.</span>

<span class="sd">        :param feat: the matrix of acoustic parameters to post-process</span>
<span class="sd">        :param label: the VAD labels for the acoustic parameters</span>
<span class="sd">        :param global_mean: vector or mean to use for normalization</span>
<span class="sd">        :param global_std: vector of standard deviation to use for normalization</span>

<span class="sd">        :return: the matrix of acoustic parameters ingand their VAD labels after post-process</span>
Anthony Larcher's avatar
Anthony Larcher committed
273
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
274
275
        <span class="c1"># Apply a mask on the features</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>
Anthony Larcher's avatar
Anthony Larcher committed
276
277
            <span class="n">feat</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_mask</span><span class="p">(</span><span class="n">feat</span><span class="p">)</span>

Anthony Larcher's avatar
Anthony Larcher committed
278
        <span class="c1"># Perform RASTA filtering if required</span>
Anthony Larcher's avatar
Anthony Larcher committed
279
280
281
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rasta</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">_rasta</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="n">label</span><span class="p">)</span>

Anthony Larcher's avatar
Anthony Larcher committed
282
        <span class="c1"># Add temporal context</span>
Anthony Larcher's avatar
Anthony Larcher committed
283
284
285
286
287
288
289
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">double_delta</span><span class="p">:</span>
            <span class="n">feat</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_delta_and_2delta</span><span class="p">(</span><span class="n">feat</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca</span><span class="p">:</span>
            <span class="n">feat</span> <span class="o">=</span> <span class="n">pca_dct</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">dct_pca_config</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">sdc</span><span class="p">:</span>
            <span class="n">feat</span> <span class="o">=</span> <span class="n">shifted_delta_cepstral</span><span class="p">(</span><span class="n">feat</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">P</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">k</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">sdc_config</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>

Anthony Larcher's avatar
Anthony Larcher committed
290
291
        <span class="c1"># Smooth the labels and fuse the channels if more than one.</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Smooth the labels and fuse the channels if more than one&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
292
        <span class="n">label</span> <span class="o">=</span> <span class="n">label_fusion</span><span class="p">(</span><span class="n">label</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
293
        
Anthony Larcher's avatar
Anthony Larcher committed
294
295
296
        <span class="c1"># Normalize the data</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;no norm&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
297
298
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_normalize</span><span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="n">feat</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>
Anthony Larcher's avatar
Anthony Larcher committed
299

Anthony Larcher's avatar
Anthony Larcher committed
300
        <span class="c1"># if not self.keep_all_features, only selected features and labels are kept</span>
Anthony Larcher's avatar
Anthony Larcher committed
301
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">keep_all_features</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
302
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;no keep all&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
303
304
            <span class="n">feat</span> <span class="o">=</span> <span class="n">feat</span><span class="p">[</span><span class="n">label</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">label</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
305

Anthony Larcher's avatar
Anthony Larcher committed
306
307
        <span class="k">return</span> <span class="n">feat</span><span class="p">,</span> <span class="n">label</span></div>

Anthony Larcher's avatar
Anthony Larcher committed
308
309
310
311
    <span class="k">def</span> <span class="nf">_mask</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cep</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Keep only the MFCC index present in the filter list</span>
<span class="sd">        :param cep: acoustic parameters to filter</span>
Anthony Larcher's avatar
Anthony Larcher committed
312

Anthony Larcher's avatar
Anthony Larcher committed
313
<span class="sd">        :return: return the list of MFCC given by filter list</span>
Anthony Larcher's avatar
Anthony Larcher committed
314
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
315
        <span class="k">if</span> <span class="nb">len</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="o">==</span> <span class="mi">0</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
316
317
            <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;filter list is empty&#39;</span><span class="p">)</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;applied mask&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
318
        <span class="k">return</span> <span class="n">cep</span><span class="p">[:,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mask</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
319

Anthony Larcher's avatar
Anthony Larcher committed
320
    <span class="k">def</span> <span class="nf">_normalize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">cep</span><span class="p">,</span> <span class="n">global_mean</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">global_std</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
321
322
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Normalize acoustic parameters in place</span>
Anthony Larcher's avatar
Anthony Larcher committed
323

Anthony Larcher's avatar
Anthony Larcher committed
324
325
326
327
328
<span class="sd">        :param label: vad labels to use for normalization</span>
<span class="sd">        :param cep: acoustic parameters to normalize</span>
<span class="sd">        :param global_mean: mean vector to use if provided</span>
<span class="sd">        :param global_std: standard deviation vector to use if provided</span>
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
329
330
331
        <span class="c1"># Perform feature normalization on the entire session.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;no norm&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
332
            <span class="k">pass</span>
Anthony Larcher's avatar
Anthony Larcher committed
333
334
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">==</span> <span class="s1">&#39;cms&#39;</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;cms norm&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
335
            <span class="n">cms</span><span class="p">(</span><span class="n">cep</span><span class="p">,</span> <span class="n">label</span><span class="p">,</span> <span class="n">global_mean</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
336
337
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">==</span> <span class="s1">&#39;cmvn&#39;</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;cmvn norm&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
338
            <span class="n">cmvn</span><span class="p">(</span><span class="n">cep</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>
Anthony Larcher's avatar
Anthony Larcher committed
339
340
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">==</span> <span class="s1">&#39;stg&#39;</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;stg norm&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
341
            <span class="n">stg</span><span class="p">(</span><span class="n">cep</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="n">label</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
342
343
344
345
346
347
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">==</span> <span class="s1">&#39;cmvn_sliding&#39;</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;sliding cmvn norm&#39;</span><span class="p">)</span>
            <span class="n">cep_sliding_norm</span><span class="p">(</span><span class="n">cep</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">win</span><span class="o">=</span><span class="mi">301</span><span class="p">,</span> <span class="n">center</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">reduce</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">feat_norm</span> <span class="o">==</span> <span class="s1">&#39;cms_sliding&#39;</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;sliding cms norm&#39;</span><span class="p">)</span>
            <span class="n">cep_sliding_norm</span><span class="p">(</span><span class="n">cep</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">win</span><span class="o">=</span><span class="mi">301</span><span class="p">,</span> <span class="n">center</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">reduce</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
348
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
349
            <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;Wrong feature normalisation type&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
350

Anthony Larcher's avatar
Anthony Larcher committed
351
    <span class="k">def</span> <span class="nf">_delta_and_2delta</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cep</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
352
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
353
354
355
356
<span class="sd">        Add deltas and double deltas.</span>
<span class="sd">        :param cep: a matrix of cepstral cefficients</span>

<span class="sd">        :return: the cepstral coefficient stacked with deltas and double deltas</span>
Anthony Larcher's avatar
Anthony Larcher committed
357
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
358
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">delta</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
359
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;add delta&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
360
361
362
            <span class="n">delta</span> <span class="o">=</span> <span class="n">compute_delta</span><span class="p">(</span><span class="n">cep</span><span class="p">,</span> <span class="n">filt</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">delta_filter</span><span class="p">)</span>
            <span class="n">cep</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">column_stack</span><span class="p">((</span><span class="n">cep</span><span class="p">,</span> <span class="n">delta</span><span class="p">))</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">double_delta</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
363
                <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;add delta delta&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
364
365
366
                <span class="n">double_delta</span> <span class="o">=</span> <span class="n">compute_delta</span><span class="p">(</span><span class="n">delta</span><span class="p">,</span> <span class="n">filt</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">delta_filter</span><span class="p">)</span>
                <span class="n">cep</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">column_stack</span><span class="p">((</span><span class="n">cep</span><span class="p">,</span> <span class="n">double_delta</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">cep</span>
Anthony Larcher's avatar
Anthony Larcher committed
367
368
369
370
371
372
373
374

    <span class="k">def</span> <span class="nf">_rasta</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">cep</span><span class="p">,</span> <span class="n">label</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Performs RASTA filtering if required.</span>
<span class="sd">        The two first frames are copied from the third to keep</span>
<span class="sd">        the length consistent</span>
<span class="sd">        !!! if vad is None: label[] is empty</span>

Anthony Larcher's avatar
Anthony Larcher committed
375
376
<span class="sd">        :param cep: the acoustic features to filter</span>
<span class="sd">        :param label: the VAD label</span>
Anthony Larcher's avatar
Anthony Larcher committed
377
378
379
<span class="sd">        :return:</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">rasta</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
380
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;perform RASTA </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rasta</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
381
382
383
384
385
            <span class="n">cep</span> <span class="o">=</span> <span class="n">rasta_filt</span><span class="p">(</span><span class="n">cep</span><span class="p">)</span>
            <span class="n">cep</span><span class="p">[:</span><span class="mi">2</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">cep</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="p">:]</span>
            <span class="n">label</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">label</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">cep</span><span class="p">,</span> <span class="n">label</span>

Anthony Larcher's avatar
Anthony Larcher committed
386
<div class="viewcode-block" id="FeaturesServer.get_context"><a class="viewcode-back" href="../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>
Anthony Larcher's avatar
Anthony Larcher committed
387
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
388
389
<span class="sd">        Add a left annd 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>
Anthony Larcher's avatar
Anthony Larcher committed
390

Anthony Larcher's avatar
Anthony Larcher committed
391
392
393
394
<span class="sd">        :param feat: sequence of feature frames (one fame per line)</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>
<span class="sd">        :param label: vad label if available</span>
Anthony Larcher's avatar
Anthony Larcher committed
395

Anthony Larcher's avatar
Anthony Larcher committed
396
<span class="sd">        :return: a sequence of frames with their left and right context</span>
Anthony Larcher's avatar
Anthony Larcher committed
397
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
398
        <span class="k">if</span> <span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
399
            <span class="n">start</span> <span class="o">=</span> <span class="mi">0</span>
Anthony Larcher's avatar
Anthony Larcher committed
400
        <span class="k">if</span> <span class="n">stop</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
401
402
403
404
405
            <span class="n">stop</span> <span class="o">=</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">context_feat</span> <span class="o">=</span> <span class="n">framing</span><span class="p">(</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="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">stop</span> <span class="o">-</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="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</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>
Anthony Larcher's avatar
Anthony Larcher committed
406
                      <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;edge&#39;</span><span class="p">)[</span><span class="n">start</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
407
            <span class="n">stop</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="p">:],</span> <span class="n">win_size</span><span class="o">=</span><span class="mi">1</span><span class="o">+</span><span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
408
409
        <span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">))</span> <span class="o">*</span> <span class="n">feat</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>

Anthony Larcher's avatar
Anthony Larcher committed
410
        <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
411
            <span class="n">context_label</span> <span class="o">=</span> <span class="n">label</span><span class="p">[</span><span class="n">start</span><span class="p">:</span><span class="n">stop</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
412
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
413
            <span class="n">context_label</span> <span class="o">=</span> <span class="kc">None</span>
Anthony Larcher's avatar
Anthony Larcher committed
414

Anthony Larcher's avatar
Anthony Larcher committed
415
416
417
        <span class="k">return</span> <span class="n">context_feat</span><span class="p">,</span> <span class="n">context_label</span></div>

<div class="viewcode-block" id="FeaturesServer.get_traps"><a class="viewcode-back" href="../featuresserver.html#features_server.FeaturesServer.get_traps">[docs]</a>    <span class="k">def</span> <span class="nf">get_traps</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>
Anthony Larcher's avatar
Anthony Larcher committed
418
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
419
420
421
422
423
424
425
426
427
<span class="sd">        Compute TRAP parameters. The input frames are concatenated to add their left and right context,</span>
<span class="sd">        a Hamming window is applied and a DCT reduces the dimensionality of the resulting vector.</span>

<span class="sd">        :param feat: input acoustic parameters to process</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>
<span class="sd">        :param label: vad label if available</span>

<span class="sd">        :return: a sequence of TRAP parameters</span>
Anthony Larcher's avatar
Anthony Larcher committed
428
429
<span class="sd">        &quot;&quot;&quot;</span>

Anthony Larcher's avatar
Anthony Larcher committed
430
        <span class="k">if</span> <span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
431
            <span class="n">start</span> <span class="o">=</span> <span class="mi">0</span>
Anthony Larcher's avatar
Anthony Larcher committed
432
        <span class="k">if</span> <span class="n">stop</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
433
434
435
436
437
438
            <span class="n">stop</span> <span class="o">=</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">context_feat</span> <span class="o">=</span> <span class="n">framing</span><span class="p">(</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="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</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">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="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</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>
Anthony Larcher's avatar
Anthony Larcher committed
439
                      <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;edge&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
440
441
                      <span class="p">)[</span><span class="n">start</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span>
                        <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-</span><span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span><span class="n">stop</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-</span><span class="n">start</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="p">:],</span>
Anthony Larcher's avatar
Anthony Larcher committed
442
443
            <span class="n">win_size</span><span class="o">=</span><span class="mi">1</span><span class="o">+</span><span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">)</span>
        <span class="p">)</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
444
445
        <span class="n">hamming_dct</span> <span class="o">=</span> <span class="p">(</span><span class="n">dct_basis</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">traps_dct_nb</span><span class="p">,</span> <span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span>
                       <span class="n">numpy</span><span class="o">.</span><span class="n">hamming</span><span class="p">(</span><span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">context</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">T</span>
Anthony Larcher's avatar
Anthony Larcher committed
446

Anthony Larcher's avatar
Anthony Larcher committed
447
        <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
448
449
            <span class="n">context_label</span> <span class="o">=</span> <span class="n">label</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>
Anthony Larcher's avatar
Anthony Larcher committed
450
            <span class="n">context_label</span> <span class="o">=</span> <span class="kc">None</span>
Anthony Larcher's avatar
Anthony Larcher committed
451

Anthony Larcher's avatar
Anthony Larcher committed
452
453
454
        <span class="k">return</span> <span class="n">numpy</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span>
            <span class="n">context_feat</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">hamming_dct</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">hamming_dct</span>
Anthony Larcher's avatar
Anthony Larcher committed
455
456
457
        <span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">context_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="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="n">context_label</span></div>

<div class="viewcode-block" id="FeaturesServer.load"><a class="viewcode-back" href="../featuresserver.html#features_server.FeaturesServer.load">[docs]</a>    <span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">show</span><span class="p">,</span> <span class="n">channel</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">input_feature_filename</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="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>
Anthony Larcher's avatar
Anthony Larcher committed
458
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
459
<span class="sd">        Depending of the setting of the FeaturesServer, can either:</span>
Anthony Larcher's avatar
Anthony Larcher committed
460

Anthony Larcher's avatar
Anthony Larcher committed
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<span class="sd">        1. Get the datasets from a single HDF5 file</span>
<span class="sd">            The HDF5 file is loaded from disk or processed on the fly</span>
<span class="sd">            via the FeaturesExtractor of the current FeaturesServer</span>

<span class="sd">        2. Load datasets from multiple input HDF5 files. The datasets are post-processed separately, then concatenated</span>
<span class="sd">            and post-process</span>

<span class="sd">        :param show: ID of the show to load (should be the same for each HDF5 file to read from)</span>
<span class="sd">        :param channel: audio channel index in case the parameters are extracted from an audio file</span>
<span class="sd">        :param input_feature_filename: name of the input feature file in case it is independent from the ID of the show</span>
<span class="sd">        :param label: vad labels</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>

<span class="sd">        :return: acoustic parameters and their vad labels</span>
Anthony Larcher's avatar
Anthony Larcher committed
476
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
477

Anthony Larcher's avatar
Anthony Larcher committed
478
479
480
        <span class="c1"># In case the name of the input file does not include the ID of the show</span>
        <span class="c1"># (i.e., feature_filename_structure does not include {})</span>
        <span class="c1"># self.audio_filename_structure is updated to use the input_feature_filename</span>
Anthony Larcher's avatar
Anthony Larcher committed
481
482
483
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">show</span> <span class="o">==</span> <span class="n">show</span> \
                <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">input_feature_filename</span> <span class="o">==</span> <span class="n">input_feature_filename</span>\
                <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">start_stop</span> <span class="o">==</span> <span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="p">)</span>  \
Anthony Larcher's avatar
Anthony Larcher committed
484
485
                <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;return previous load&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
486
487
488
489
490
491
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">show</span> <span class="o">=</span> <span class="n">show</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">input_feature_filename</span> <span class="o">=</span> <span class="n">input_feature_filename</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">start_stop</span> <span class="o">=</span> <span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="p">)</span>

Anthony Larcher's avatar
Anthony Larcher committed
492
493
        <span class="n">feature_filename</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">input_feature_filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
494
495
496
            <span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span> <span class="o">=</span> <span class="n">input_feature_filename</span>
            <span class="n">feature_filename</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">show</span><span class="p">)</span>

Anthony Larcher's avatar
Anthony Larcher committed
497
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
498
499
500
501
502
            <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_features</span><span class="p">(</span><span class="n">show</span><span class="p">,</span>
                                                   <span class="n">channel</span><span class="o">=</span><span class="n">channel</span><span class="p">,</span>
                                                   <span class="n">input_feature_filename</span><span class="o">=</span><span class="n">feature_filename</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>
Anthony Larcher's avatar
Anthony Larcher committed
503
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
504
            <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Extract tandem features from multiple sources&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
505
506
507
508
            <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_tandem_features</span><span class="p">(</span><span class="n">show</span><span class="p">,</span>
                                                          <span class="n">channel</span><span class="o">=</span><span class="n">channel</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>
Anthony Larcher's avatar
Anthony Larcher committed
509
510
511
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">previous_load</span></div>

<div class="viewcode-block" id="FeaturesServer.get_features"><a class="viewcode-back" href="../featuresserver.html#features_server.FeaturesServer.get_features">[docs]</a>    <span class="k">def</span> <span class="nf">get_features</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">show</span><span class="p">,</span> <span class="n">channel</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">input_feature_filename</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="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>
Anthony Larcher's avatar
Anthony Larcher committed
512
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
513
514
515
516
517
518
519
520
521
522
523
524
<span class="sd">        Get the datasets from a single HDF5 file</span>
<span class="sd">        The HDF5 file is loaded from disk or processed on the fly</span>
<span class="sd">        via the FeaturesExtractor of the current FeaturesServer</span>

<span class="sd">        :param show: ID of the show</span>
<span class="sd">        :param channel: index of the channel to read</span>
<span class="sd">        :param input_feature_filename: name of the input file in case it does not include the ID of the show</span>
<span class="sd">        :param label: vad labels</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>

<span class="sd">        :return: acoustic parameters and their vad labels</span>
Anthony Larcher's avatar
Anthony Larcher committed
525
526
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
527
528
529
<span class="sd">        Si le nom du fichier d&#39;entrée est totalement indépendant du show</span>
<span class="sd">        -&gt; si feature_filename_structure ne contient pas &quot;{}&quot;</span>
<span class="sd">        on peut mettre à jour: self.audio_filename_structure pour entrer directement le nom du fichier de feature</span>
Anthony Larcher's avatar
Anthony Larcher committed
530
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
531
        <span class="k">if</span> <span class="n">input_feature_filename</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
532
533
            <span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span> <span class="o">=</span> <span class="n">input_feature_filename</span>

Anthony Larcher's avatar
Anthony Larcher committed
534
535
536
        <span class="c1"># If no extractor for this source, open hdf5 file and return handler</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">features_extractor</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">h5f</span> <span class="o">=</span> <span class="n">h5py</span><span class="o">.</span><span class="n">File</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">feature_filename_structure</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">show</span><span class="p">),</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
537

Anthony Larcher's avatar
Anthony Larcher committed
538
        <span class="c1"># If an extractor is provided for this source, extract features and return an hdf5 handler</span>
Anthony Larcher's avatar
Anthony Larcher committed
539
540
541
        <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>

Anthony Larcher's avatar
Anthony Larcher committed
542
543
544
545
        <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="n">__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>
Anthony Larcher's avatar
Anthony Larcher committed
546
547
548
549
            <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>

Anthony Larcher's avatar
Anthony Larcher committed
550
        <span class="k">if</span> <span class="n">stop</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
551
552
553
554
            <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>

Anthony Larcher's avatar
Anthony Larcher committed
555
        <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>
Anthony Larcher's avatar
Anthony Larcher committed
556

Anthony Larcher's avatar
Anthony Larcher committed
557
558
        <span class="c1"># Get the data between start and stop</span>
        <span class="c1"># Concatenate all required datasets</span>
Anthony Larcher's avatar
Anthony Larcher committed
559
560
561
        <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>
Anthony Larcher's avatar
Anthony Larcher committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
        <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>
Anthony Larcher's avatar
Anthony Larcher committed
578
579
580
581
        <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>

Anthony Larcher's avatar
Anthony Larcher committed
582
583
584
        <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>
Anthony Larcher's avatar
Anthony Larcher committed
585
            <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
586
587
588
589
                <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>
Anthony Larcher's avatar
Anthony Larcher committed
590
591
592
        <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>
Anthony Larcher's avatar
Anthony Larcher committed
593
        <span class="c1"># Post-process the features and return the features and vad label</span>
Anthony Larcher's avatar
Anthony Larcher committed
594
595
596
597
598
        <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>

Anthony Larcher's avatar
Anthony Larcher committed
599
600
601
        <span class="k">return</span> <span class="n">feat</span><span class="p">,</span> <span class="n">label</span></div>

<div class="viewcode-block" id="FeaturesServer.get_tandem_features"><a class="viewcode-back" href="../featuresserver.html#features_server.FeaturesServer.get_tandem_features">[docs]</a>    <span class="k">def</span> <span class="nf">get_tandem_features</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">show</span><span class="p">,</span> <span class="n">channel</span><span class="o">=</span><span class="mi">0</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="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>
Anthony Larcher's avatar
Anthony Larcher committed
602
603
604
605
606
607
608
609
610
611
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Read acoustic parameters from multiple HDF5 files (from disk or extracted by FeaturesExtractor objects).</span>

<span class="sd">        :param show: Id of the show</span>
<span class="sd">        :param channel: index of the channel</span>
<span class="sd">        :param label: vad labels</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>

<span class="sd">        :return: acoustic parameters and their vad labels</span>
Anthony Larcher's avatar
Anthony Larcher committed
612
<span class="sd">        &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
613
        <span class="c1"># Each source has its own sources (including subserver) that provides features and label</span>
Anthony Larcher's avatar
Anthony Larcher committed
614
615
        <span class="n">features</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">features_server</span><span class="p">,</span> <span class="n">get_vad</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">sources</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
616
            <span class="c1"># Get features from this source</span>
Anthony Larcher's avatar
Anthony Larcher committed
617
618
619
620
            <span class="n">feat</span><span class="p">,</span> <span class="n">lbl</span> <span class="o">=</span> <span class="n">features_server</span><span class="o">.</span><span class="n">get_features</span><span class="p">(</span><span class="n">show</span><span class="p">,</span> <span class="n">channel</span><span class="o">=</span><span class="n">channel</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="k">if</span> <span class="n">get_vad</span><span class="p">:</span>
                <span class="n">label</span> <span class="o">=</span> <span class="n">lbl</span>
            <span class="n">features</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feat</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
621

Anthony Larcher's avatar
Anthony Larcher committed
622
623
        <span class="n">features</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">features</span><span class="p">)</span>

Anthony Larcher's avatar
Anthony Larcher committed
624
625
626
        <span class="c1"># If the VAD is not required, return all labels at True</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="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>
Anthony Larcher's avatar
Anthony Larcher committed
627

Anthony Larcher's avatar
Anthony Larcher committed
628
629
630
631
        <span class="c1"># Apply the final post-processing on the concatenated features</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">post_processing</span><span class="p">(</span><span class="n">features</span><span class="p">,</span> <span class="n">label</span><span class="p">)</span></div>

<div class="viewcode-block" id="FeaturesServer.mean_std"><a class="viewcode-back" href="../featuresserver.html#features_server.FeaturesServer.mean_std">[docs]</a>    <span class="k">def</span> <span class="nf">mean_std</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">show</span><span class="p">,</span> <span class="n">channel</span><span class="o">=</span><span class="mi">0</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>
Anthony Larcher's avatar
Anthony Larcher committed
632
633
634
635
636
637
638
639
640
641
642
643
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Compute the mean and standard deviation vectors for a segment of acoustic features</span>

<span class="sd">        :param show: the ID of the show</span>
<span class="sd">        :param channel: the index of the channel</span>
<span class="sd">        :param start: index of the first frame of the selected segment</span>
<span class="sd">        :param stop: index of the last frame of the selected segment</span>

<span class="sd">        :return: the number of frames, the mean of the frames and their standard deviation</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">feat</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">show</span><span class="p">,</span> <span class="n">channel</span><span class="o">=</span><span class="n">channel</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="k">return</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">feat</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">),</span> <span class="n">numpy</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">feat</span><span class="o">**</span><span class="mi">2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span></div></div>
Anthony Larcher's avatar
Anthony Larcher committed
644
</pre></div>
Anthony Larcher's avatar
Anthony Larcher committed
645
646

          </div>
Anthony Larcher's avatar
Anthony Larcher committed
647
648
649
650
651
652
653
654
655
656
657
658
659
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
Anthony Larcher's avatar
Anthony Larcher committed
660
661
        <li class="nav-item nav-item-0"><a href="../index.html">SIDEKIT  documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >Module code</a> &#187;</li> 
Anthony Larcher's avatar
Anthony Larcher committed
662
663
664
      </ul>
    </div>
    <div class="footer" role="contentinfo">
Anthony Larcher's avatar
Anthony Larcher committed
665
666
        &#169; Copyright 2014-16, Anthony LARCHER &amp; Sylvain MEIGNIER &amp; Kong Aik LEE.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.4.8.
Anthony Larcher's avatar
Anthony Larcher committed
667
668
    </div>
  </body>
Anthony Larcher's avatar
Anthony Larcher committed
669
</html>