Exemplo n.º 1
0
    def classify(self):
        storage = store(os.path.join(settings.BRAIN_DIR, 'nn'))
        save_path = storage['save.path']
        meta_path = save_path + '.meta'
        saver = tf.train.import_meta_graph(meta_path)
        data = self.get_features()
        n_f = data.shape[0]
        data = data.reshape((1, n_f))
        prediction = None
        with tf.Session() as sess:
            saver.restore(sess, save_path)
            graph = tf.get_default_graph()
            x = graph.get_tensor_by_name('x:0')
            y_ = graph.get_tensor_by_name('y_:0')
            keep_prob = graph.get_tensor_by_name('keep_prob:0')
            result = sess.run(y_, feed_dict={x: data, keep_prob: 1})[0]
            idx = np.argmax(result)
            dataset = musicset.MusicSet()
            dataset.one_hot_encode_genres()
            for genre in dataset.genres:
                if dataset.encoded_genres[genre][idx] == 1:
                    prediction = genre

        if self.prediction is not None:
            self.prediction.destroy()

        self.prediction = tk.Label(self.root, text=prediction)
        self.prediction.config(font=("Courier", 36))
        self.prediction.grid(row=0, column=1)
Exemplo n.º 2
0
 def test_pca_mdb(self):
     featureset = musicset.MusicSet(dirname='features2')
     train = featureset.load_train_data()
     new_features = reduce.pca(train.music)
     storage = store(featureset.results_dir)
     storage['reduced_mdb.dat'] = new_features
     self.assertGreater(train.music.shape[1], new_features.shape[1])
Exemplo n.º 3
0
    def train(self, display_step=100, num_feat=20, path=None):
        storage = store(path)
        if storage['features.dat'] is not None:
            return storage['features.dat']

        n_input = settings.CNN['INPUT_SHAPE'][0]*settings.CNN['INPUT_SHAPE'][1]
        n_classes = len(settings.GENRES)
        new_shape = [-1, settings.CNN['INPUT_SHAPE'][0],
            settings.CNN['INPUT_SHAPE'][1], 1]

        x = self.x = tf.placeholder("float", [None, n_input])
        new_x = self.new_x = tf.reshape(self.x, new_shape)
        y = self.y = tf.placeholder("float", [None, n_classes])
        cw, dw = self.get_weights(n_classes)
        cb, db = self.get_bias(n_classes)
        y_ = self.y_ = self.prepare_layers()
        cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.y_, labels=self.y))
        optimizer = tf.train.AdamOptimizer(learning_rate=settings.NN['LEARNING_RATE']).minimize(cost)
        init = tf.global_variables_initializer()

        new_ds = DataSet()

        with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) as sess:
            sess.run(init)
            step = 1
            for genre in self.data.genres:
                for file in self.data.files[genre]:
                    data, sr = librosa.load(file)
                    options = {}
                    options['frame_length'] = int(settings.FRAME_LENGTH)
                    options['hop_length'] = int(settings.HOP_LENGTH)
                    frames = librosa.util.frame(data, **options)
                    frames = frames.T[:settings.KEEP_FRAMES, :]
                    #print(frames.shape, type(frames))
                    #return
                    labels = np.tile(self.data.encoded_genres[genre], (settings.KEEP_FRAMES, 1))
                    for i in range(settings.CNN['TRAINING_CYCLES']):
                        sess.run(optimizer, feed_dict={x: frames, y: labels})

                    features = np.array(sess.run(self.dense_layers[-2], feed_dict={x: frames})[0])
                    true_label = np.array(self.data.encoded_genres[genre])

                    if new_ds.music is None:
                        new_ds.music = features
                        new_ds.labels = true_label
                    else:
                        new_ds.music = np.vstack((new_ds.music, features,))
                        new_ds.labels = np.vstack((new_ds.labels, true_label,))
                    print("File Processed: %s" % file)                    


                print("Genre Processed: %s" % genre)

        storage['features.dat'] = new_ds
        return new_ds
Exemplo n.º 4
0
 def __init__(self, force_load=False, genres=None, dirname=RESULT_DIR):
     self.results_dir = os.path.join(settings.BRAIN_DIR, dirname)
     if not os.path.isdir(self.results_dir):
         os.mkdir(self.results_dir)
     self.genres = genres or settings.GENRES
     self.storage = store(self.results_dir, force_load)
     self.files = {}
     self.train = None
     self.test = None
     self.validation = None
     self.encoded_genres = None
     self.load_files()
Exemplo n.º 5
0
    def test_nn(self):
        p = os.path.join(settings.BRAIN_DIR, "specfeat")
        dataset = musicset.MusicSet(dirname='special')
        dataset.load_files()
        dataset.one_hot_encode_genres()
        storage = store(p)
        ds = storage['features.dat']
        all_data = np.hstack(tuple(np.split(ds.music, 10, axis=0)))
        all_labels = np.hstack(tuple(np.split(ds.labels, 10, axis=0)))

        dataset.train = musicset.DataSet()
        dataset.test = musicset.DataSet()

        dataset.train.music = np.vstack(
            tuple(np.split(all_data[:70, :], 10, axis=-1)))
        dataset.train.labels = np.vstack(
            tuple(np.split(all_labels[:70, :], 10, axis=-1)))

        dataset.test.music = np.vstack(
            tuple(np.split(all_data[70:, :], 10, axis=-1)))
        dataset.test.labels = np.vstack(
            tuple(np.split(all_labels[70:, :], 10, axis=-1)))

        nn_t = nn.NN(dataset, n_input=256)
        step = 100
        tr, te, trc, tec = nn_t.train(display_step=step,
                                      out=True,
                                      path='special.final')
        epochs = np.arange(int(settings.NN['TRAINING_CYCLES'] / step))
        f, ax = plt.subplots(2)
        ax[0].plot(epochs, tr, color='#FF2D00', label='Training Accuracy')
        #ax[0].plot(epochs, vl, color='#1DFF00', label='Validation Accuracy')
        ax[0].plot(epochs, te, color='#0014FF', label='Testing Accuracy')
        ax[0].legend(loc="lower right")
        ax[0].set_xlabel('Epoch')
        ax[0].set_ylabel('Accuracy')
        ax[0].set_title('Accuracy vs Number of epochs')
        ax[1].plot(epochs, trc, color='#FF2D00', label='Training Lost')
        ax[1].plot(epochs, tec, color='#1DFF00', label='Test Lost')
        ax[1].legend(loc="lower right")
        ax[1].set_xlabel('Epoch')
        ax[1].set_ylabel('Loss')
        ax[1].set_title('Loss vs Number of Epochs')
        f.suptitle('Network with %s layers' % settings.NN['NUM_HIDDEN_LAYERS'])
        plt.show()
Exemplo n.º 6
0
			def __init__(self, force=False):
				self.storage = store('/tmp', force)
Exemplo n.º 7
0
    def train(self, display_step=100, out=False, path='model.final'):
        n_classes = len(settings.GENRES)
        n_input = self.n_input
        x = self.x = tf.placeholder("float", [None, n_input], name='x')
        y = self.y = tf.placeholder("float", [None, n_classes], name='y')
        keep_prob = self.keep_prob = tf.placeholder(tf.float32,
                                                    name='keep_prob')

        w = self.get_weights(n_input, n_classes)
        b = self.get_bias(n_classes)
        y_ = self.y_ = self.prepare_layers()
        cost = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(logits=self.y_,
                                                    labels=self.y))
        optimizer = tf.train.AdamOptimizer(
            learning_rate=settings.NN['LEARNING_RATE']).minimize(cost)
        init = tf.global_variables_initializer()
        saver = tf.train.Saver()
        self.correct_pred = correct_pred = tf.equal(tf.argmax(self.y_, 1),
                                                    tf.argmax(self.y, 1))
        self.accuracy = accuracy = tf.reduce_mean(
            tf.cast(correct_pred, tf.float32))
        train_acc = []
        #val_acc = []
        test_acc = []
        train_cost = []
        test_cost = []
        with tf.Session(config=tf.ConfigProto(
                allow_soft_placement=True, log_device_placement=True)) as sess:
            sess.run(init)
            step = 1
            for i in range(settings.NN['TRAINING_CYCLES']):
                bx, by = self.data.train.next_batch(settings.NN['BATCH_SIZE'])
                sess.run(optimizer,
                         feed_dict={
                             x: bx,
                             y: by,
                             keep_prob: settings.NN['DROPOUT_PROB']
                         })
                if step % display_step == 0:
                    # Calculate batch accuracy
                    acc = sess.run(accuracy,
                                   feed_dict={
                                       x: bx,
                                       y: by,
                                       keep_prob: 1.
                                   })
                    # Calculate batch loss
                    loss = sess.run(cost,
                                    feed_dict={
                                        x: bx,
                                        y: by,
                                        keep_prob: 1.
                                    })

                    train_acc.append(acc)

                    #vac = sess.run(accuracy,
                    #    feed_dict={x: self.data.validation.music, y: self.data.validation.labels, keep_prob: 1.})
                    tac = sess.run(accuracy,
                                   feed_dict={
                                       x: self.data.test.music,
                                       y: self.data.test.labels,
                                       keep_prob: 1.
                                   })

                    te_loss = sess.run(cost,
                                       feed_dict={
                                           x: self.data.test.music,
                                           y: self.data.test.labels,
                                           keep_prob: 1.
                                       })

                    train_cost.append(loss)
                    test_cost.append(te_loss)
                    #val_acc.append(vac)
                    test_acc.append(tac)
                    save_path = saver.save(
                        sess, os.path.join(self.results_dir, "model.ckpt"))
                    if out:
                        print("Iter " + str(step * settings.NN['BATCH_SIZE']) + ", Minibatch Loss= " + \
                              "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc))

                        #print("Validation Accuracy:", vac)
                        print("Testing Accuracy:", tac)
                        print("Model saved in file: %s" % save_path)
                step += 1
            save_path = saver.save(sess, os.path.join(self.results_dir, path))
            storage = store(self.results_dir)
            storage['save.path'] = save_path
            print("Model saved in file: %s" % save_path)

            print(
                "Testing Accuracy:",
                sess.run(self.accuracy,
                         feed_dict={
                             x: self.data.test.music,
                             y: self.data.test.labels,
                             keep_prob: 1.
                         }))

        return train_acc, test_acc, train_cost, test_cost