Example #1
0
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)

        nPixelsPerCell, nCellsPerBlock)
    trainingSamplesPBV[k], trainingLabelsPBV[
        k] = trainingSamples, trainingLabels
    if k != 'pedestrian':
        trainingSamplesBV[k], trainingLabelsBV[
            k] = trainingSamples, trainingLabels
    if k != 'car':
        trainingSamplesPB[k], trainingLabelsPB[
            k] = trainingSamples, trainingLabels
    if k != 'bicycle':
        trainingSamplesPV[k], trainingLabelsPV[
            k] = trainingSamples, trainingLabels

# Training the Support Vector Machine
print "Training Pedestrian-Cyclist-Vehicle Model"
model = ml.SVM()
model.train(np.concatenate(trainingSamplesPBV.values()),
            np.concatenate(trainingLabelsPBV.values()), args.svmType,
            args.kernelType)
model.save(args.directoryName + "/modelPBV.xml")

print "Training Cyclist-Vehicle Model"
model = ml.SVM()
model.train(np.concatenate(trainingSamplesBV.values()),
            np.concatenate(trainingLabelsBV.values()), args.svmType,
            args.kernelType)
model.save(args.directoryName + "/modelBV.xml")

print "Training Pedestrian-Cyclist Model"
model = ml.SVM()
model.train(np.concatenate(trainingSamplesPB.values()),
params.convertToFrames(3.6)
if params.homography is not None:
    invHomography = np.linalg.inv(params.homography)

if params.speedAggregationMethod == 'median':
    speedAggregationFunc = np.median
elif params.speedAggregationMethod == 'mean':
    speedAggregationFunc = np.mean
elif params.speedAggregationMethod == 'quantile':
    speedAggregationFunc = lambda speeds: np.percentile(speeds, args.speedAggregationQuantile)
else:
    print('Unknown speed aggregation method: {}. Exiting'.format(params.speedAggregationMethod))
    sys.exit()

pedBikeCarSVM = ml.SVM()
pedBikeCarSVM.load(params.pedBikeCarSVMFilename)
bikeCarSVM = ml.SVM()
bikeCarSVM.load(params.bikeCarSVMFilename)

# log logistic for ped and bik otherwise ((pedBeta/pedAlfa)*((sMean/pedAlfa)**(pedBeta-1)))/((1+(sMean/pedAlfa)**pedBeta)**2.)
speedProbabilities = {'car': lambda s: norm(params.meanVehicleSpeed, params.stdVehicleSpeed).pdf(s),
                      'pedestrian': lambda s: norm(params.meanPedestrianSpeed, params.stdPedestrianSpeed).pdf(s), 
                      'bicycle': lambda s: lognorm(params.scaleCyclistSpeed, loc = 0., scale = np.exp(params.locationCyclistSpeed)).pdf(s)} # numpy lognorm shape, loc, scale: shape for numpy is scale (std of the normal) and scale for numpy is location (mean of the normal)

if args.plotSpeedDistribution:
    import matplotlib.pyplot as plt
    plt.figure()
    for k in speedProbabilities:
        plt.plot(np.arange(0.1, args.maxSpeedDistributionPlot, 0.1), [speedProbabilities[k](s/3.6/25) for s in np.arange(0.1, args.maxSpeedDistributionPlot, 0.1)], label = k)
    plt.xlabel('Speed (km/h)')
Example #4
0
import ml
import numpy as np

class Features:
    def __init__(self, filename):
        self.features = np.load(filename + '-features.npy')
        self.class_labels = np.load(filename + '-labels.npy')

train = Features(filename='./anchit/train')
test = Features(filename='./anchit/test')

svm = ml.SVM(features_dim=13, classes=3, svm_type='c', kernel='poly', poly_degree=3)
print svm

svm.train(train=train, test=test)
Example #5
0
def labels_to_class(labels):
    temp = []
    for i in range(0, len(labels)):
        if labels[i][0] == 1:
            temp.append(0)
        elif labels[i][1] == 1:
            temp.append(1)
    return temp


train.class_labels = np.array(labels_to_class(train.labels))
test.class_labels = np.array(labels_to_class(test.labels))

ncd = dict(features_dim=21, classes=2, hidden_units=[80, 100], learn_rate=0.01)
nc = ml.NetworkConfig(**ncd)

net = ml.MLP(nc)

print net

net.train(train=train, test=test, epochs=5000)

svm = ml.SVM(features_dim=21,
             classes=2,
             svm_type='c',
             kernel='sigmoid',
             poly_degree=3)
print svm

svm.train(train=train, test=test)