You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/11/13 18:38:58 UTC

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

gaurav-gireesh commented on a change in pull request #13241: [MXNET-1210 ][WIP] Gluon Audio
URL: https://github.com/apache/incubator-mxnet/pull/13241#discussion_r233171700
 
 

 ##########
 File path: python/mxnet/gluon/contrib/data/audio/datasets.py
 ##########
 @@ -0,0 +1,156 @@
+# 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
+import sklearn
+from mxnet import gluon, nd
+
+
+class AudioFolderDataset(gluon.data.dataset.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)
+
+    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, transform=None, has_csv=False, train_csv=None, file_format='.wav'):
+        self._root = os.path.expanduser(root)
+        self._transform = transform
+        self._exts = ['.wav']
+        self._format = file_format
+        self._has_csv = has_csv
+        self._train_csv = train_csv
+        self._list_audio_files(self._root)
+
+
+    def _list_audio_files(self, root):
+        """
+            Populates synsets - a map of index to label for the data items.
+            Populates the data in the dataset, making tuples of (data, label)
+        """
+        if not self._has_csv:
+            self.synsets = []
+            self.items = []
+
+            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:
+            self.synsets = []
+            self.items = []
+            data_tmp = []
+            label_tmp = []
+            with open(self._train_csv, "r") as traincsv:
+                for line in traincsv:
+                    filename = os.path.join(root, line.split(",")[0])
+                    label = line.split(",")[1].strip()
+                    data_tmp.append(os.path.join(self._root, line.split(",")[0]))
+                    label_tmp.append(line.split(",")[1].strip())
+            data_tmp = data_tmp[1:]
+            label_tmp = label_tmp[1:]
+            le = sklearn.preprocessing.LabelEncoder()
 
 Review comment:
   Yes, I am working to find a workaround here. We needed label encoding here because we read from a csv file that contains filename(location) and the corresponding label. It is different from the use case in ImageFolderDataset that requires each class(or label) to be a folder( with images inside them), and hence Folder can be encountered once and put in the synsets(no repetition there). However, in the csv file, a label can occur multiple times. Looking into this for a better way to get around this.
   Thanks!
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services