import numpy as np dataset = ml.Dataset('./dataset') print dataset fcd = dict(features=['mfcc'], mfcc_coeff=13) fc = ml.FeaturesConfig(**fcd) print fc rcd = dict(feature_reduction='pca') rc = ml.ReductionConfig(**rcd) print rc train_fe = ml.AudioFeatures(files=dataset.train_files, features_cfg=fc, reduction_cfg=rc).extract() #train_fe.save('./genres/train') print train_fe test_fe = ml.AudioFeatures(files=dataset.test_files, features_cfg=fc, reduction_cfg=rc).extract() #test_fe.save('./genres/test') print test_fe net = ml.MLP(train_fe.features_dim, train_fe.classes, [280, 300, 400, 600, 800]) #net = ml.MLP(train_fe.features_dim, train_fe.classes, [280, 300, 560, 720]) #net = gd.Network(20, 10, [200, 400, 800, 1600, 3200]) print net net.train(train=train_fe, test=test_fe, epochs=500)
import ml d = ml.Dataset('/home/danish/Projects/Music-Genre-Classification/DataSet/wav (copy)') fcd = dict(features=['mfcc', 'mel_spec'], coeffs=dict(mfcc_coeff=20)) fc = ml.FeaturesConfig(**fcd) print(fc) rc = ml.ReductionConfig() print(rc) train_fe = ml.AudioFeatures(files=d.train_files, features_cfg=fc, reduction_cfg=rc).extract() test_fe = ml.AudioFeatures(files=d.test_files, features_cfg=fc, reduction_cfg=rc).extract() ncd = dict(features_dim=train_fe.features_dim, classes=train_fe.classes, hidden_units=[300, 350, 550, 900], learn_rate=0.01) nc = ml.NetworkConfig(**ncd) net = ml.MLP(nc) print(net) net.train(train=train_fe, test=test_fe, epochs=500) svm = ml.SVM(features_dim=train_fe.features_dim, classes=train_fe.classes, svm_type='nu', kernel='linear', poly_degree=3) print(svm) svm.train(train=train_fe, test=test_fe)
def main(n_hiddens=400, n_layers=2, learning_rate=0.001, momentum=0.5, rbm_learning_rate=[0.001, 0.001], rbm_epochs=32, rbm_batch_size=100, example_dropout=32, hidden_dropout=.5, rmsprop=True, max_col_norm=1.8356, l2=None, train_epochs=200, K=1, features="minimal.pca", state=None, channel=None, **kwargs): print "Loading dataset..." numpy.random.seed(0x7265257d5f) LABELS = [ "Disgust", "Fear", "Happy", "Neutral", "Sad", "Surprise", "Angry" ] train_x = [] train_y = [] for directory, dirnames, filenames in os.walk( "/data/lisa/data/faces/EmotiW/complete_audio_features/Train"): for filename in filenames: if filename.find("%s.pkl" % features) != -1: feat = numpy.load(os.path.join(directory, filename)) targ = numpy.argmax( map(lambda x: directory.find(x) != -1, LABELS)) train_x.append(numpy.asarray(feat, theano.config.floatX)) train_y.append(targ) train_y = numpy.asarray(train_y) pretrain_x = numpy.load( "/data/lisatmp/dauphiya/emotiw/mlp_audio/train_x_%s.npy" % features) valid_x = [] valid_y = [] for directory, dirnames, filenames in os.walk( "/data/lisa/data/faces/EmotiW/complete_audio_features/Val"): for filename in filenames: if filename.find("%s.pkl" % features) != -1: feat = numpy.load(os.path.join(directory, filename)) targ = numpy.argmax( map(lambda x: directory.find(x) != -1, LABELS)) valid_x.append(numpy.asarray(feat, theano.config.floatX)) valid_y.append(targ) valid_y = numpy.asarray(train_y, dtype=theano.config.floatX) import ipdb ipdb.set_trace() means = numpy.asarray(numpy.sum([x.sum(0) for x in train_x], 0) / sum([x.shape[0] for x in train_x]), dtype=theano.config.floatX) train_inds = range(len(train_x)) numpy.random.shuffle(train_inds) print "Building model..." layers = n_layers * [('T', n_hiddens)] + [('S', train_y.max() + 1)] if type(rbm_learning_rate) in [str, unicode]: rbm_learning_rate = eval(rbm_learning_rate) if type(rbm_learning_rate) is float: rbm_learning_rate = len(layers) * [rbm_learning_rate] model = ml.MLP(n_in=train_x[0].shape[1], layers=layers, learning_rate=learning_rate, l2=l2, rmsprop=rmsprop, max_col_norm=max_col_norm, hidden_dropout=hidden_dropout, momentum=momentum) rbms = [] def project(x): for rbm in rbms: x = rbm.mean_h(x) return x for i in range(len(layers) - 1): print "Training RBM %d..." % i if i == 0: rbm = ml.GaussianRBM(n_hiddens=n_hiddens, epsilon=rbm_learning_rate[i], n_samples=rbm_batch_size, epochs=rbm_epochs, K=K) else: rbm = ml.BinaryRBM(n_hiddens=n_hiddens, epsilon=rbm_learning_rate[i], n_samples=rbm_batch_size, epochs=rbm_epochs, K=K) rbm.fit(pretrain_x, project=project, verbose=True) model.layers[i].W.set_value(rbm.W) model.layers[i].b.set_value(rbm.c) numpy.save("rbm_W_%d.npy" % i, rbm.W) rbms.append(rbm) print "Training model..." epoch_times = [] best_train_error = float('inf') best_valid_error = float('inf') for epoch in range(train_epochs): begin = time.time() losses = [] for minibatch in range(len(train_inds)): x = train_x[train_inds[minibatch]] - means y = train_y[[train_inds[minibatch]]] inds = range(x.shape[0]) numpy.random.shuffle(inds) inds = inds[:example_dropout] losses.append(model.train(x[inds], y)) end = time.time() loss = numpy.mean(losses) train_error = 0. for minibatch in range(len(train_inds)): x = train_x[train_inds[minibatch]] - means y = train_y[train_inds[minibatch]] train_error += (y != model.output(x))[0] train_error /= len(train_inds) valid_error = 0. for minibatch in range(len(valid_y)): x = valid_x[minibatch] - means y = valid_y[[minibatch]] valid_error += (y != model.output(x))[0] valid_error /= len(valid_y) epoch_times.append((end - begin) / 60) mean_epoch_time = numpy.mean(epoch_times) if train_error < best_train_error: best_train_error = train_error elif epoch > 50: model.trainer.learning_rate.set_value( numpy.asarray(0.98 * model.trainer.learning_rate.get_value(), dtype=theano.config.floatX)) if valid_error < best_valid_error: best_valid_error = valid_error model.save() elif epoch > 50: model.trainer.learning_rate.set_value( numpy.asarray(0.98 * model.trainer.learning_rate.get_value(), dtype=theano.config.floatX)) print "epoch = %d, mean_time = %.2f, loss = %.4f, train_error = %.4f, valid_error = %.4f, learning rate = %.4f" % ( epoch, mean_epoch_time, loss, train_error, valid_error, model.trainer.learning_rate.get_value()) print "best validation error %.4f" % (best_valid_error) if channel != None: state.epoch = epoch state.epochtime = mean_epoch_time state.loss = loss state.trainerror = best_train_error state.validerror = best_valid_error channel.save()
binary.printBinary() ############################################## # # # Machine Learning # # # ############################################## # Select machine learning algorithm if(config.get('Classifier','Algorithm') == 'SVM'): ml1 = ml.Svm(goodware+malware) elif(config.get('Classifier','Algorithm') == 'RandomForest'): ml1 = ml.RandomForest(goodware+malware) elif(config.get('Classifier','Algorithm') == 'MLP'): ml1 = ml.MLP(goodware+malware) else: print('Error: Invalid ml algorithm at classifier.conf.') exit(0) #This can go into validation mode only. Futurely. # ml1.createKFold() if(config.get('Classifier','Mode') == 'Validation'): if(config.get('Classifier','Run') == 'Single'): ml1.singleRun() elif(config.get('Classifier','Run') == 'Multiple'): ml1.multipleRun() elif(config.get('Classifier','Mode') == 'Testing'): ml1.trainingTesting(goodware,malware,suspicious)
def run_nnet(clip_ids, features_dir, scores_out_dir, params_dir, **kwargs): file_dir = None learning_rate = 0.001 no_final_dropout = False hidden_dropout = .5 center_grads = False layer_dropout = False rmsprop = True ratio = 1.0 train_epochs = 240 K = 1 state = None channel = None n_hiddens = 310 use_nesterov = 1 learning_rate = 0.000502311626412 momentum = 0.459258503786 features = "full.pca" example_dropout = 98 rbm_epochs = 15 topN_pooling = 1 mean_pooling = 0 rbm_batch_size = 60 normalize_acts = False enable_standardization = 0 loss_based_pooling = False response_normalize = False rmsprop = 1 layer_dropout = True no_final_dropout = 0 l2 = 1e-05 hidden_dropout = 0.121193556495 max_col_norm = 1.2875 n_layers = 2 rho = 0.92 print "Loading dataset..." LABELS = [ "Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral" ] nclasses = len(LABELS) numpy.random.seed(0x7265257d5f) final_audio_path = "/data/lisa/exp/faces/emotiw_final/caglar_audio/" test_files = glob.glob("%s/*.pkl" % features_dir) test_x = [] test_y = [] test_filenames = [] print "Loading test set..." for clip_id in clip_ids: test_file = "%s/%s.%s.pkl" % (features_dir, clip_id, features) feat = numpy.load(test_file) test_filenames.append(test_file.replace("pkl", "npy")) test_x.append(numpy.asarray(feat, theano.config.floatX)) test_file_h = open("test_file.txt", "w") test_file_h.writelines(["%s\n" % item for item in test_filenames]) test_means = numpy.load(os.path.join(final_audio_path, "test_means.npy")) test_means = numpy.cast[theano.config.floatX](test_means) print "Building model..." layers = (n_layers - 1) * [('R', n_hiddens)] + [('L', n_hiddens) ] + [('S', nclasses)] model = ml.MLP(n_in=test_x[0].shape[1], layers=layers, learning_rate=learning_rate, l2=l2, rho=rho, rmsprop=rmsprop, response_normalize=response_normalize, center_grads=center_grads, max_col_norm=max_col_norm, loss_based_pooling=loss_based_pooling, hidden_dropout=hidden_dropout, layer_dropout=layer_dropout, use_nesterov=use_nesterov, normalize_acts=normalize_acts, topN_pooling=topN_pooling, mean_pooling=mean_pooling, no_final_dropout=no_final_dropout, enable_standardization=enable_standardization, momentum=momentum, base_path=params_dir) model.load() train_error = 0. test_feats = [] test_preds = [] #Testing the means for minibatch in range(len(test_x)): x = test_x[minibatch] - test_means test_feats.append(model.pooled_output_features(x)) test_preds.append(model.compute_preds(x)) test_feats = numpy.asarray(test_feats, dtype="float32") test_feats_dir = os.path.join(scores_out_dir, "audio_mlp_test_feats.npy") test_preds_dir = os.path.join( scores_out_dir, "audio_mlp_learned_on_train_predict_on_test_scores.npy") test_preds = numpy.asarray(test_preds).flatten().reshape(-1, 7)[:, (6, 0, 1, 2, 4, 5, 3)] numpy.save(test_feats_dir, test_feats) numpy.save(test_preds_dir, test_preds) # Also save as .txt for the SVM prediction test_preds_txt = open( os.path.join(scores_out_dir, "audio_mlp_learned_on_train_predict_on_test_scores.txt"), 'w') for item, pred in zip(clip_ids, test_preds): label = LABELS[pred.argmax()] print >> test_preds_txt, item, label, ('%f ' * 7) % tuple( [p for p in pred]) test_preds_txt.close()