changeset 605:20953adfdef8

initial tzanetakis dataset
author James Bergstra <bergstrj@iro.umontreal.ca>
date Thu, 15 Jan 2009 22:21:04 -0500
parents 28f7dc848efc
children e4a92dce13fe
files pylearn/datasets/tzanetakis.py
diffstat 1 files changed, 102 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pylearn/datasets/tzanetakis.py	Thu Jan 15 22:21:04 2009 -0500
@@ -0,0 +1,102 @@
+"""
+Load Tzanetakis' genre-classification dataset.
+
+"""
+from __future__ import absolute_import
+
+import os
+import numpy
+
+from ..io.amat import AMat
+from .config import data_root
+from .dataset import dataset_factory, Dataset
+
+def head(n=10, path=None):
+    """Load the first MNIST examples.
+
+    Returns two matrices: x, y.  x has N rows of 784 columns.  Each row of x represents the
+    28x28 grey-scale pixels in raster order.  y is a vector of N integers.  Each element y[i]
+    is the label of the i'th row of x.
+    
+    """
+    path = os.path.join(data_root(), 'mnist','mnist_with_header.amat') if path is None else path
+
+    dat = AMat(path=path, head=n)
+
+    try:
+        assert dat.input.shape[0] == n
+        assert dat.target.shape[0] == n
+    except Exception , e:
+        raise Exception("failed to read MNIST data", (dat, e))
+
+    return dat.input, numpy.asarray(dat.target, dtype='int64').reshape(dat.target.shape[0])
+
+def all(path=None):
+    return head(n=None, path=path)
+
+def train_valid_test(ntrain=50000, nvalid=10000, ntest=10000, path=None):
+    all_x, all_targ = head(ntrain+nvalid+ntest, path=path)
+
+    rval = Dataset()
+
+    rval.train = Dataset.Obj(x=all_x[0:ntrain],
+            y=all_targ[0:ntrain])
+    rval.valid = Dataset.Obj(x=all_x[ntrain:ntrain+nvalid],
+            y=all_targ[ntrain:ntrain+nvalid])
+    rval.test =  Dataset.Obj(x=all_x[ntrain+nvalid:ntrain+nvalid+ntest],
+            y=all_targ[ntrain+nvalid:ntrain+nvalid+ntest])
+
+    rval.n_classes = 10
+    rval.img_shape = (28,28)
+    return rval
+
+
+def mfcc16(segments_per_song = 1, include_covariance = True, random_split = 0,
+        ntrain = 700, nvalid = 100, ntest = 200):
+    if segments_per_song != 1:
+        raise NotImplementedError()
+
+    path = os.path.join(data_root(), 'tzanetakis','feat_mfcc16_540_1.stat.amat')
+    dat = AMat(path=path)
+    all_input = dat.input
+    assert all_input.shape == (1000 * segments_per_song, 152)
+    all_targ = numpy.tile(numpy.arange(10).reshape(10,1), 100 * segments_per_song)\
+            .reshape(1000 * segments_per_song)
+
+    if not include_covariance:
+        all_input = all_input[:,0:16] 
+
+    #shuffle the data according to the random split
+    assert all_input.shape[0] == all_targ.shape[0]
+    seed = random_split + 1
+    numpy.random.RandomState(seed).shuffle(all_input)
+    numpy.random.RandomState(seed).shuffle(all_targ)
+
+    #construct a dataset to return
+    rval = Dataset()
+
+    rval.train = Dataset.Obj(x=all_input[0:ntrain],
+            y=all_targ[0:ntrain])
+    rval.valid = Dataset.Obj(x=all_input[ntrain:ntrain+nvalid],
+            y=all_targ[ntrain:ntrain+nvalid])
+    rval.test =  Dataset.Obj(x=all_input[ntrain+nvalid:ntrain+nvalid+ntest],
+            y=all_targ[ntrain+nvalid:ntrain+nvalid+ntest])
+
+    rval.n_classes = 10
+
+    return rval
+
+
+
+
+def mnist_factory(variant="", ntrain=None, nvalid=None, ntest=None):
+    if variant=="":
+        return train_valid_test()
+    elif variant=="1k":
+        return train_valid_test(ntrain=1000, nvalid=200, ntest=200)
+    elif variant=="10k":
+        return train_valid_test(ntrain=10000, nvalid=2000, ntest=2000)
+    elif variant=="custom":
+        return train_valid_test(ntrain=ntrain, nvalid=nvalid, ntest=ntest)
+    else:
+        raise Exception('Unknown MNIST variant', variant)