[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234373031
 
 

 ##
 File path: python/mxnet/gluon/contrib/data/audio/transforms.py
 ##
 @@ -0,0 +1,208 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+# pylint: disable= arguments-differ
+"Audio transforms."
+
+import warnings
+import numpy as np
+try:
+import librosa
+except ImportError as e:
+warnings.warn("gluon/contrib/data/audio/transforms.py : librosa dependency 
could not be resolved or \
+imported, could not provide some/all transform.")
+
+from . import ndarray as nd
+from block import Block
+
+class MFCC(Block):
+"""
+Extracts Mel frequency cepstrum coefficients from the audio data file
+More details : 
https://librosa.github.io/librosa/generated/librosa.feature.mfcc.html
+
+Parameters
+--
+Keyword arguments that can be passed, which are utilized by librosa module 
are:
+sr: int, default 22050
+sampling rate of the input audio signal
+n_mfcc: int, default 20
+number of mfccs to return
+
+
+Inputs:
+- **x**: input tensor (samples, ) shape.
+
+Outputs:
+- **out**: output array is a scaled NDArray with (samples, ) shape.
+
+"""
+
+def __init__(self, **kwargs):
+self.kwargs = kwargs
+super(MFCC, self).__init__()
+
+def forward(self, x):
+if not librosa:
+warnings.warn("Librosa dependency is not installed! Install that 
and retry")
+return x
+if isinstance(x, np.ndarray):
+y = x
+elif isinstance(x, nd.NDArray):
+y = x.asnumpy()
+else:
+warnings.warn("Input object is not numpy or NDArray... Cannot 
apply the transform: MFCC!")
+return x
+
+audio_tmp = np.mean(librosa.feature.mfcc(y=y, **self.kwargs).T, axis=0)
+return nd.array(audio_tmp)
+
+
+class Scale(Block):
+"""Scale audio numpy.ndarray from a 16-bit integer to a floating point 
number between
+-1.0 and 1.0. The 16-bit integer is the sample resolution or bit depth.
+
+Parameters
+--
+scale_factor : float
+The factor to scale the input tensor by.
+
+
+Inputs:
+- **x**: input tensor (samples, ) shape.
+
+Outputs:
+- **out**: output array is a scaled NDArray with (samples, ) shape.
+
+Examples
+
+>>> scale = audio.transforms.Scale(scale_factor=2)
+>>> audio_samples = mx.nd.array([2,3,4])
+>>> scale(audio_samples)
+[1.  1.5 2. ]
+
+
+"""
+
+def __init__(self, scale_factor=2**31):
+self.scale_factor = scale_factor
+super(Scale, self).__init__()
+
+def forward(self, x):
+if isinstance(x, np.ndarray):
+return nd.array(x/self.scale_factor)
+return x / self.scale_factor
+
+
+class PadTrim(Block):
+"""Pad/Trim a 1d-NDArray of NPArray (Signal or Labels)
+
+Parameters
+--
+max_len : int
+Length to which the array will be padded or trimmed to.
+fill_value: int or float
+If there is a need of padding, what value to padd at the end of the 
input array
+
+
+Inputs:
+- **x**: input tensor (samples, ) shape.
+
+Outputs:
+- **out**: output array is a scaled NDArray with (max_len, ) shape.
+
+Examples
+
+>>> padtrim = audio.transforms.PadTrim(max_len=9, fill_value=0)
+>>> audio_samples = mx.nd.array([1,2,3,4,5])
+>>> padtrim(audio_samples)
+[1. 2. 3. 4. 5. 0. 0. 0. 0.]
+
+
+"""
+
+def __init__(self, max_len, fill_value=0):
+self._max_len = max_len
+self._fill_value = fill_value
+super(PadTrim, self).__init__()
+
+def forward(self, x):
+if  isinstance(x, np.ndarray):
+x = nd.array(x)
+if self._max_len > x.size:
+pad = nd.ones((self._max_len - x.size,)) * self._fill_value
+x = nd.concat(x, pad, dim=0)
+elif self._max_len < x.size:
+x = x[:self._max_len]
+return x
+
+
+class MEL(Block):
+"""Create MEL Spectrograms from a 

[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234373209
 
 

 ##
 File path: python/mxnet/gluon/contrib/data/audio/transforms.py
 ##
 @@ -0,0 +1,208 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+# pylint: disable= arguments-differ
+"Audio transforms."
+
+import warnings
+import numpy as np
+try:
+import librosa
+except ImportError as e:
+warnings.warn("gluon/contrib/data/audio/transforms.py : librosa dependency 
could not be resolved or \
+imported, could not provide some/all transform.")
+
+from . import ndarray as nd
+from block import Block
+
+class MFCC(Block):
+"""
+Extracts Mel frequency cepstrum coefficients from the audio data file
+More details : 
https://librosa.github.io/librosa/generated/librosa.feature.mfcc.html
+
+Parameters
+--
+Keyword arguments that can be passed, which are utilized by librosa module 
are:
+sr: int, default 22050
+sampling rate of the input audio signal
+n_mfcc: int, default 20
+number of mfccs to return
+
+
+Inputs:
+- **x**: input tensor (samples, ) shape.
+
+Outputs:
+- **out**: output array is a scaled NDArray with (samples, ) shape.
+
+"""
+
+def __init__(self, **kwargs):
+self.kwargs = kwargs
+super(MFCC, self).__init__()
+
+def forward(self, x):
+if not librosa:
+warnings.warn("Librosa dependency is not installed! Install that 
and retry")
+return x
+if isinstance(x, np.ndarray):
+y = x
+elif isinstance(x, nd.NDArray):
+y = x.asnumpy()
+else:
+warnings.warn("Input object is not numpy or NDArray... Cannot 
apply the transform: MFCC!")
+return x
+
+audio_tmp = np.mean(librosa.feature.mfcc(y=y, **self.kwargs).T, axis=0)
+return nd.array(audio_tmp)
+
+
+class Scale(Block):
+"""Scale audio numpy.ndarray from a 16-bit integer to a floating point 
number between
+-1.0 and 1.0. The 16-bit integer is the sample resolution or bit depth.
+
+Parameters
+--
+scale_factor : float
+The factor to scale the input tensor by.
+
+
+Inputs:
+- **x**: input tensor (samples, ) shape.
+
+Outputs:
+- **out**: output array is a scaled NDArray with (samples, ) shape.
+
+Examples
+
+>>> scale = audio.transforms.Scale(scale_factor=2)
+>>> audio_samples = mx.nd.array([2,3,4])
+>>> scale(audio_samples)
+[1.  1.5 2. ]
+
+
+"""
+
+def __init__(self, scale_factor=2**31):
+self.scale_factor = scale_factor
+super(Scale, self).__init__()
+
+def forward(self, x):
+if isinstance(x, np.ndarray):
+return nd.array(x/self.scale_factor)
+return x / self.scale_factor
+
+
+class PadTrim(Block):
+"""Pad/Trim a 1d-NDArray of NPArray (Signal or Labels)
+
+Parameters
+--
+max_len : int
+Length to which the array will be padded or trimmed to.
+fill_value: int or float
+If there is a need of padding, what value to padd at the end of the 
input array
+
+
+Inputs:
+- **x**: input tensor (samples, ) shape.
+
+Outputs:
+- **out**: output array is a scaled NDArray with (max_len, ) shape.
+
+Examples
+
+>>> padtrim = audio.transforms.PadTrim(max_len=9, fill_value=0)
+>>> audio_samples = mx.nd.array([1,2,3,4,5])
+>>> padtrim(audio_samples)
+[1. 2. 3. 4. 5. 0. 0. 0. 0.]
+
+
+"""
+
+def __init__(self, max_len, fill_value=0):
+self._max_len = max_len
+self._fill_value = fill_value
+super(PadTrim, self).__init__()
+
+def forward(self, x):
+if  isinstance(x, np.ndarray):
+x = nd.array(x)
+if self._max_len > x.size:
+pad = nd.ones((self._max_len - x.size,)) * self._fill_value
+x = nd.concat(x, pad, dim=0)
+elif self._max_len < x.size:
+x = x[:self._max_len]
+return x
+
+
+class MEL(Block):
+"""Create MEL Spectrograms from a 

[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234366749
 
 

 ##
 File path: tests/python/unittest/test_contrib_gluon_data_audio.py
 ##
 @@ -0,0 +1,102 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Testing audio transforms in gluon container."""
+from __future__ import print_function
+import warnings
+import numpy as np
+from mxnet import gluon
+from mxnet.gluon.contrib.data.audio import transforms
+from mxnet.test_utils import assert_almost_equal
+from common import with_seed
+
+
+@with_seed()
+def test_pad_trim():
+"""
+Function to test Pad/Trim Audio transform
+"""
+data_in = np.random.randint(1, high=20, size=(15))
+# trying trimming the audio samples here...
+max_len = 10
+pad_trim = gluon.contrib.data.audio.transforms.PadTrim(max_len=max_len)
+trimmed_audio = pad_trim(data_in)
+np_trimmed = data_in[:max_len]
+assert_almost_equal(trimmed_audio.asnumpy(), np_trimmed)
+
+#trying padding here...
+max_len = 25
+fill_value = 0
+pad_trim = transforms.PadTrim(max_len=max_len, fill_value=fill_value)
+np_padded = np.pad(data_in, pad_width=max_len-len(data_in), 
mode='constant', \
+constant_values=fill_value)[max_len-len(data_in):]
+padded_audio = pad_trim(data_in)
+assert_almost_equal(padded_audio.asnumpy(), np_padded)
+
+
+@with_seed()
+def test_scale():
+"""
+Function to test scaling of the audio transform
 
 Review comment:
   Please execute pylint and rst-lint on all these files.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234355451
 
 

 ##
 File path: python/mxnet/gluon/contrib/data/audio/datasets.py
 ##
 @@ -0,0 +1,171 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+# pylint: disable=
+""" Audio Dataset container."""
+__all__ = ['AudioFolderDataset']
+
+import os
+import warnings
+from data import Dataset
+from . import ndarray as nd
+try:
+import librosa
+except ImportError as e:
+warnings.warn("gluon/contrib/data/audio/datasets.py : librosa dependency 
could not be resolved or \
+imported, could not load audio onto the numpy array.")
+
+
+class AudioFolderDataset(Dataset):
+"""A dataset for loading Audio files stored in a folder structure like::
+
+root/children_playing/0.wav
+root/siren/23.wav
+root/drilling/26.wav
+root/dog_barking/42.wav
+OR
+Files(wav) and a csv file that has filename and associated label
+
+Parameters
+--
+root : str
+Path to root directory.
+transform : callable, default None
+A function that takes data and label and transforms them
+has_csv: default True
+If True, it means that a csv file has filename and its corresponding 
label
+If False, we have folder like structure
+train_csv: str, default None
+If has_csv is True, train_csv should be populated by the training csv 
filename
+file_format: str, default '.wav'
+The format of the audio files(.wav, .mp3)
+skip_rows: int, default 0
+While reading from csv file, how many rows to skip at the start of the 
file to avoid reading in header
+
+Attributes
+--
+synsets : list
+List of class names. `synsets[i]` is the name for the integer label `i`
+items : list of tuples
+List of all audio in (filename, label) pairs.
+"""
+def __init__(self, root, has_csv=False, train_csv=None, 
file_format='.wav', skip_rows=0):
+self._root = os.path.expanduser(root)
+self._exts = ['.wav']
+self._format = file_format
+self._has_csv = has_csv
+self._train_csv = train_csv
+self._list_audio_files(self._root, skip_rows=skip_rows)
+
+
+def _list_audio_files(self, root, skip_rows=0):
+"""
+Populates synsets - a map of index to label for the data items.
+Populates the data in the dataset, making tuples of (data, label)
+"""
+self.synsets = []
+self.items = []
+if not self._has_csv:
+for folder in sorted(os.listdir(root)):
+path = os.path.join(root, folder)
+if not os.path.isdir(path):
+warnings.warn('Ignoring %s, which is not a 
directory.'%path, stacklevel=3)
+continue
+label = len(self.synsets)
+self.synsets.append(folder)
+for filename in sorted(os.listdir(path)):
+file_name = os.path.join(path, filename)
+ext = os.path.splitext(file_name)[1]
+if ext.lower() not in self._exts:
+warnings.warn('Ignoring %s of type %s. Only support 
%s'%(filename, ext, ', '.join(self._exts)))
+continue
+self.items.append((file_name, label))
+else:
+data_tmp = []
+label_tmp = []
+skipped_rows = 0
+with open(self._train_csv, "r") as traincsv:
+for line in traincsv:
+skipped_rows = skipped_rows + 1
+if skipped_rows <= skip_rows:
+continue
+filename = os.path.join(root, line.split(",")[0])
+label = line.split(",")[1].strip()
+if label not in self.synsets:
+self.synsets.append(label)
+data_tmp.append(os.path.join(self._root, 
line.split(",")[0]))
+label_tmp.append(self.synsets.index(label))
+
+#Generating the synset.txt 

[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234355891
 
 

 ##
 File path: python/mxnet/gluon/contrib/data/audio/datasets.py
 ##
 @@ -0,0 +1,171 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+# pylint: disable=
+""" Audio Dataset container."""
+__all__ = ['AudioFolderDataset']
+
+import os
+import warnings
+from data import Dataset
+from . import ndarray as nd
+try:
+import librosa
+except ImportError as e:
+warnings.warn("gluon/contrib/data/audio/datasets.py : librosa dependency 
could not be resolved or \
+imported, could not load audio onto the numpy array.")
+
+
+class AudioFolderDataset(Dataset):
+"""A dataset for loading Audio files stored in a folder structure like::
+
+root/children_playing/0.wav
+root/siren/23.wav
+root/drilling/26.wav
+root/dog_barking/42.wav
+OR
+Files(wav) and a csv file that has filename and associated label
+
+Parameters
+--
+root : str
+Path to root directory.
+transform : callable, default None
+A function that takes data and label and transforms them
+has_csv: default True
+If True, it means that a csv file has filename and its corresponding 
label
+If False, we have folder like structure
+train_csv: str, default None
+If has_csv is True, train_csv should be populated by the training csv 
filename
+file_format: str, default '.wav'
+The format of the audio files(.wav, .mp3)
+skip_rows: int, default 0
+While reading from csv file, how many rows to skip at the start of the 
file to avoid reading in header
+
+Attributes
+--
+synsets : list
+List of class names. `synsets[i]` is the name for the integer label `i`
+items : list of tuples
+List of all audio in (filename, label) pairs.
+"""
+def __init__(self, root, has_csv=False, train_csv=None, 
file_format='.wav', skip_rows=0):
+self._root = os.path.expanduser(root)
+self._exts = ['.wav']
+self._format = file_format
+self._has_csv = has_csv
+self._train_csv = train_csv
+self._list_audio_files(self._root, skip_rows=skip_rows)
+
+
+def _list_audio_files(self, root, skip_rows=0):
+"""
+Populates synsets - a map of index to label for the data items.
+Populates the data in the dataset, making tuples of (data, label)
+"""
+self.synsets = []
+self.items = []
+if not self._has_csv:
+for folder in sorted(os.listdir(root)):
+path = os.path.join(root, folder)
+if not os.path.isdir(path):
+warnings.warn('Ignoring %s, which is not a 
directory.'%path, stacklevel=3)
+continue
+label = len(self.synsets)
+self.synsets.append(folder)
+for filename in sorted(os.listdir(path)):
+file_name = os.path.join(path, filename)
+ext = os.path.splitext(file_name)[1]
+if ext.lower() not in self._exts:
+warnings.warn('Ignoring %s of type %s. Only support 
%s'%(filename, ext, ', '.join(self._exts)))
+continue
+self.items.append((file_name, label))
+else:
+data_tmp = []
+label_tmp = []
+skipped_rows = 0
+with open(self._train_csv, "r") as traincsv:
+for line in traincsv:
+skipped_rows = skipped_rows + 1
+if skipped_rows <= skip_rows:
+continue
+filename = os.path.join(root, line.split(",")[0])
+label = line.split(",")[1].strip()
+if label not in self.synsets:
+self.synsets.append(label)
+data_tmp.append(os.path.join(self._root, 
line.split(",")[0]))
+label_tmp.append(self.synsets.index(label))
+
+#Generating the synset.txt 

[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234355047
 
 

 ##
 File path: python/mxnet/gluon/contrib/data/audio/datasets.py
 ##
 @@ -0,0 +1,171 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+# pylint: disable=
+""" Audio Dataset container."""
+__all__ = ['AudioFolderDataset']
+
+import os
+import warnings
+from data import Dataset
+from . import ndarray as nd
+try:
+import librosa
+except ImportError as e:
+warnings.warn("gluon/contrib/data/audio/datasets.py : librosa dependency 
could not be resolved or \
+imported, could not load audio onto the numpy array.")
+
+
+class AudioFolderDataset(Dataset):
+"""A dataset for loading Audio files stored in a folder structure like::
+
+root/children_playing/0.wav
+root/siren/23.wav
+root/drilling/26.wav
+root/dog_barking/42.wav
+OR
+Files(wav) and a csv file that has filename and associated label
+
+Parameters
+--
+root : str
+Path to root directory.
+transform : callable, default None
+A function that takes data and label and transforms them
+has_csv: default True
+If True, it means that a csv file has filename and its corresponding 
label
+If False, we have folder like structure
+train_csv: str, default None
+If has_csv is True, train_csv should be populated by the training csv 
filename
+file_format: str, default '.wav'
+The format of the audio files(.wav, .mp3)
+skip_rows: int, default 0
+While reading from csv file, how many rows to skip at the start of the 
file to avoid reading in header
+
+Attributes
+--
+synsets : list
+List of class names. `synsets[i]` is the name for the integer label `i`
+items : list of tuples
+List of all audio in (filename, label) pairs.
+"""
+def __init__(self, root, has_csv=False, train_csv=None, 
file_format='.wav', skip_rows=0):
+self._root = os.path.expanduser(root)
+self._exts = ['.wav']
+self._format = file_format
+self._has_csv = has_csv
+self._train_csv = train_csv
+self._list_audio_files(self._root, skip_rows=skip_rows)
+
+
+def _list_audio_files(self, root, skip_rows=0):
+"""
+Populates synsets - a map of index to label for the data items.
+Populates the data in the dataset, making tuples of (data, label)
+"""
+self.synsets = []
+self.items = []
+if not self._has_csv:
+for folder in sorted(os.listdir(root)):
+path = os.path.join(root, folder)
+if not os.path.isdir(path):
+warnings.warn('Ignoring %s, which is not a 
directory.'%path, stacklevel=3)
+continue
+label = len(self.synsets)
+self.synsets.append(folder)
+for filename in sorted(os.listdir(path)):
+file_name = os.path.join(path, filename)
+ext = os.path.splitext(file_name)[1]
+if ext.lower() not in self._exts:
+warnings.warn('Ignoring %s of type %s. Only support 
%s'%(filename, ext, ', '.join(self._exts)))
+continue
+self.items.append((file_name, label))
+else:
+data_tmp = []
+label_tmp = []
+skipped_rows = 0
+with open(self._train_csv, "r") as traincsv:
+for line in traincsv:
+skipped_rows = skipped_rows + 1
+if skipped_rows <= skip_rows:
+continue
+filename = os.path.join(root, line.split(",")[0])
+label = line.split(",")[1].strip()
+if label not in self.synsets:
+self.synsets.append(label)
+data_tmp.append(os.path.join(self._root, 
line.split(",")[0]))
+label_tmp.append(self.synsets.index(label))
+
+#Generating the synset.txt 

[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234355594
 
 

 ##
 File path: python/mxnet/gluon/contrib/data/audio/datasets.py
 ##
 @@ -0,0 +1,171 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+# pylint: disable=
+""" Audio Dataset container."""
+__all__ = ['AudioFolderDataset']
+
+import os
+import warnings
+from data import Dataset
+from . import ndarray as nd
+try:
+import librosa
+except ImportError as e:
+warnings.warn("gluon/contrib/data/audio/datasets.py : librosa dependency 
could not be resolved or \
+imported, could not load audio onto the numpy array.")
+
+
+class AudioFolderDataset(Dataset):
+"""A dataset for loading Audio files stored in a folder structure like::
+
+root/children_playing/0.wav
+root/siren/23.wav
+root/drilling/26.wav
+root/dog_barking/42.wav
+OR
+Files(wav) and a csv file that has filename and associated label
+
+Parameters
+--
+root : str
+Path to root directory.
+transform : callable, default None
+A function that takes data and label and transforms them
+has_csv: default True
+If True, it means that a csv file has filename and its corresponding 
label
+If False, we have folder like structure
+train_csv: str, default None
+If has_csv is True, train_csv should be populated by the training csv 
filename
+file_format: str, default '.wav'
+The format of the audio files(.wav, .mp3)
+skip_rows: int, default 0
+While reading from csv file, how many rows to skip at the start of the 
file to avoid reading in header
+
+Attributes
+--
+synsets : list
+List of class names. `synsets[i]` is the name for the integer label `i`
+items : list of tuples
+List of all audio in (filename, label) pairs.
+"""
+def __init__(self, root, has_csv=False, train_csv=None, 
file_format='.wav', skip_rows=0):
+self._root = os.path.expanduser(root)
+self._exts = ['.wav']
+self._format = file_format
+self._has_csv = has_csv
+self._train_csv = train_csv
+self._list_audio_files(self._root, skip_rows=skip_rows)
+
+
+def _list_audio_files(self, root, skip_rows=0):
+"""
+Populates synsets - a map of index to label for the data items.
+Populates the data in the dataset, making tuples of (data, label)
+"""
+self.synsets = []
+self.items = []
+if not self._has_csv:
+for folder in sorted(os.listdir(root)):
+path = os.path.join(root, folder)
+if not os.path.isdir(path):
+warnings.warn('Ignoring %s, which is not a 
directory.'%path, stacklevel=3)
+continue
+label = len(self.synsets)
+self.synsets.append(folder)
+for filename in sorted(os.listdir(path)):
+file_name = os.path.join(path, filename)
+ext = os.path.splitext(file_name)[1]
+if ext.lower() not in self._exts:
+warnings.warn('Ignoring %s of type %s. Only support 
%s'%(filename, ext, ', '.join(self._exts)))
+continue
+self.items.append((file_name, label))
+else:
+data_tmp = []
+label_tmp = []
+skipped_rows = 0
+with open(self._train_csv, "r") as traincsv:
+for line in traincsv:
+skipped_rows = skipped_rows + 1
+if skipped_rows <= skip_rows:
+continue
+filename = os.path.join(root, line.split(",")[0])
+label = line.split(",")[1].strip()
+if label not in self.synsets:
+self.synsets.append(label)
+data_tmp.append(os.path.join(self._root, 
line.split(",")[0]))
+label_tmp.append(self.synsets.index(label))
+
+#Generating the synset.txt 

[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234366542
 
 

 ##
 File path: python/mxnet/gluon/contrib/data/audio/transforms.py
 ##
 @@ -0,0 +1,208 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+# pylint: disable= arguments-differ
+"Audio transforms."
+
+import warnings
+import numpy as np
+try:
+import librosa
+except ImportError as e:
+warnings.warn("gluon/contrib/data/audio/transforms.py : librosa dependency 
could not be resolved or \
+imported, could not provide some/all transform.")
+
+from . import ndarray as nd
+from block import Block
+
+class MFCC(Block):
+"""
+Extracts Mel frequency cepstrum coefficients from the audio data file
+More details : 
https://librosa.github.io/librosa/generated/librosa.feature.mfcc.html
+
+Parameters
+--
+Keyword arguments that can be passed, which are utilized by librosa module 
are:
+sr: int, default 22050
+sampling rate of the input audio signal
+n_mfcc: int, default 20
+number of mfccs to return
+
+
+Inputs:
+- **x**: input tensor (samples, ) shape.
+
+Outputs:
+- **out**: output array is a scaled NDArray with (samples, ) shape.
+
+"""
+
+def __init__(self, **kwargs):
+self.kwargs = kwargs
+super(MFCC, self).__init__()
+
+def forward(self, x):
+if not librosa:
+warnings.warn("Librosa dependency is not installed! Install that 
and retry")
+return x
+if isinstance(x, np.ndarray):
+y = x
+elif isinstance(x, nd.NDArray):
+y = x.asnumpy()
+else:
+warnings.warn("Input object is not numpy or NDArray... Cannot 
apply the transform: MFCC!")
+return x
+
+audio_tmp = np.mean(librosa.feature.mfcc(y=y, **self.kwargs).T, axis=0)
+return nd.array(audio_tmp)
+
+
+class Scale(Block):
+"""Scale audio numpy.ndarray from a 16-bit integer to a floating point 
number between
+-1.0 and 1.0. The 16-bit integer is the sample resolution or bit depth.
+
+Parameters
+--
+scale_factor : float
+The factor to scale the input tensor by.
+
+
+Inputs:
+- **x**: input tensor (samples, ) shape.
+
+Outputs:
+- **out**: output array is a scaled NDArray with (samples, ) shape.
+
+Examples
+
+>>> scale = audio.transforms.Scale(scale_factor=2)
+>>> audio_samples = mx.nd.array([2,3,4])
+>>> scale(audio_samples)
+[1.  1.5 2. ]
+
+
+"""
+
+def __init__(self, scale_factor=2**31):
+self.scale_factor = scale_factor
+super(Scale, self).__init__()
+
+def forward(self, x):
+if isinstance(x, np.ndarray):
+return nd.array(x/self.scale_factor)
+return x / self.scale_factor
+
+
+class PadTrim(Block):
+"""Pad/Trim a 1d-NDArray of NPArray (Signal or Labels)
+
+Parameters
+--
+max_len : int
+Length to which the array will be padded or trimmed to.
+fill_value: int or float
+If there is a need of padding, what value to padd at the end of the 
input array
+
+
+Inputs:
+- **x**: input tensor (samples, ) shape.
+
+Outputs:
+- **out**: output array is a scaled NDArray with (max_len, ) shape.
+
+Examples
+
+>>> padtrim = audio.transforms.PadTrim(max_len=9, fill_value=0)
+>>> audio_samples = mx.nd.array([1,2,3,4,5])
+>>> padtrim(audio_samples)
+[1. 2. 3. 4. 5. 0. 0. 0. 0.]
+
+
+"""
+
+def __init__(self, max_len, fill_value=0):
+self._max_len = max_len
+self._fill_value = fill_value
+super(PadTrim, self).__init__()
+
+def forward(self, x):
+if  isinstance(x, np.ndarray):
+x = nd.array(x)
+if self._max_len > x.size:
+pad = nd.ones((self._max_len - x.size,)) * self._fill_value
+x = nd.concat(x, pad, dim=0)
+elif self._max_len < x.size:
+x = x[:self._max_len]
+return x
+
+
+class MEL(Block):
+"""Create MEL Spectrograms from a 

[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234366209
 
 

 ##
 File path: python/mxnet/gluon/contrib/data/audio/transforms.py
 ##
 @@ -0,0 +1,208 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+# pylint: disable= arguments-differ
+"Audio transforms."
+
+import warnings
+import numpy as np
+try:
+import librosa
+except ImportError as e:
+warnings.warn("gluon/contrib/data/audio/transforms.py : librosa dependency 
could not be resolved or \
+imported, could not provide some/all transform.")
+
+from . import ndarray as nd
+from block import Block
+
+class MFCC(Block):
+"""
+Extracts Mel frequency cepstrum coefficients from the audio data file
+More details : 
https://librosa.github.io/librosa/generated/librosa.feature.mfcc.html
 
 Review comment:
   Please check rst-lint here


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234353616
 
 

 ##
 File path: example/gluon/urban_sounds/predict.py
 ##
 @@ -0,0 +1,91 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+Prediction module for Urban Sounds Classification
+"""
+import os
+import warnings
+import mxnet as mx
+from mxnet import nd
+from mxnet.gluon.contrib.data.audio.transforms import MFCC
+from model import get_net
+
+def predict(pred_dir='./Test'):
+"""
+The function is used to run predictions on the audio files in the 
directory `pred_directory`
 
 Review comment:
   Please check this line for rst_lint errors


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234366602
 
 

 ##
 File path: tests/python/unittest/test_contrib_gluon_data_audio.py
 ##
 @@ -0,0 +1,102 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Testing audio transforms in gluon container."""
+from __future__ import print_function
+import warnings
+import numpy as np
+from mxnet import gluon
+from mxnet.gluon.contrib.data.audio import transforms
+from mxnet.test_utils import assert_almost_equal
+from common import with_seed
+
+
+@with_seed()
+def test_pad_trim():
+"""
+Function to test Pad/Trim Audio transform
 
 Review comment:
   Please check rst-lint here


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon Audio

2018-11-16 Thread GitBox
vandanavk commented on a change in pull request #13241: [MXNET-1210 ] Gluon 
Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r234354520
 
 

 ##
 File path: python/mxnet/gluon/contrib/data/audio/datasets.py
 ##
 @@ -0,0 +1,171 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+# pylint: disable=
+""" Audio Dataset container."""
+__all__ = ['AudioFolderDataset']
+
+import os
+import warnings
+from data import Dataset
+from . import ndarray as nd
+try:
+import librosa
+except ImportError as e:
+warnings.warn("gluon/contrib/data/audio/datasets.py : librosa dependency 
could not be resolved or \
+imported, could not load audio onto the numpy array.")
+
+
+class AudioFolderDataset(Dataset):
+"""A dataset for loading Audio files stored in a folder structure like::
+
+root/children_playing/0.wav
+root/siren/23.wav
+root/drilling/26.wav
+root/dog_barking/42.wav
+OR
+Files(wav) and a csv file that has filename and associated label
+
+Parameters
+--
+root : str
+Path to root directory.
+transform : callable, default None
+A function that takes data and label and transforms them
+has_csv: default True
+If True, it means that a csv file has filename and its corresponding 
label
+If False, we have folder like structure
+train_csv: str, default None
+If has_csv is True, train_csv should be populated by the training csv 
filename
+file_format: str, default '.wav'
+The format of the audio files(.wav, .mp3)
+skip_rows: int, default 0
+While reading from csv file, how many rows to skip at the start of the 
file to avoid reading in header
+
+Attributes
+--
+synsets : list
+List of class names. `synsets[i]` is the name for the integer label `i`
+items : list of tuples
+List of all audio in (filename, label) pairs.
+"""
+def __init__(self, root, has_csv=False, train_csv=None, 
file_format='.wav', skip_rows=0):
+self._root = os.path.expanduser(root)
+self._exts = ['.wav']
+self._format = file_format
+self._has_csv = has_csv
+self._train_csv = train_csv
+self._list_audio_files(self._root, skip_rows=skip_rows)
+
+
+def _list_audio_files(self, root, skip_rows=0):
+"""
+Populates synsets - a map of index to label for the data items.
 
 Review comment:
   Please check with rst_lint for errors here


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services