Пример #1
0
def mixgauss_init(M, data, cov_type, method='kmeans'):
    '''
    % MIXGAUSS_INIT Initial parameter estimates for a mixture of Gaussians
    % function [mu, Sigma, weights] = mixgauss_init(M, data, cov_type. method)
    %
    % INPUTS:
    % data(:,t) is the t'th example
    % M = num. mixture components
    % cov_type = 'full', 'diag' or 'spherical'
    % method = 'rnd' (choose centers randomly from data) or 'kmeans' (needs netlab)
    %
    % OUTPUTS:
    % mu(:,k) 
    % Sigma(:,:,k) 
    % weights(k)
    '''

    if isinstance(data, list):
        data = np.hstack(data)
    elif data.ndim == 3:
        O, T, N = data.shape
        data = np.reshape(np.transpose(data, (0, 2, 1)), (O, T * N))
    d, T = data.shape

    if method == 'rnd':
        C = np.atleast_2d(np.cov(data))
        Sigma = np.transpose(np.tile(np.diag(np.diag(C)) * 0.5, (M, 1, 1)),
                             (2, 1, 0))
        # Initialize each mean to a random data point
        indices = np.arange(T)
        np.random.shuffle(indices)
        mu = data[:, indices[0:M]]
        weights, _ = normalise(np.ones((M, 1)))
    elif method == 'kmeans':

        gmm = GMM(n_components=M,
                  covariance_type=cov_type,
                  thresh=1e-2,
                  min_covar=1e-3,
                  n_iter=5,
                  n_init=1,
                  params='wmc',
                  init_params='wmc')

        gmm.fit(data.T)

        mu = gmm.means_.T
        weights = np.asmatrix(gmm.weights_).T
        covars = gmm.covars_

        Sigma = np.zeros((d, d, M))
        for m in range(M):
            if cov_type == 'diag':
                Sigma[:, :, m] = np.diag(covars[m, :])
            elif cov_type == 'full':
                Sigma[:, :, m] = covars[:, :, m]
            elif cov_type == 'spherical':
                Sigma[:, :, m] = covars[m] * np.eye(d)

    return mu, Sigma, weights
Пример #2
0
def mixgauss_init(M, data, cov_type, method='kmeans'):
    '''
    % MIXGAUSS_INIT Initial parameter estimates for a mixture of Gaussians
    % function [mu, Sigma, weights] = mixgauss_init(M, data, cov_type. method)
    %
    % INPUTS:
    % data(:,t) is the t'th example
    % M = num. mixture components
    % cov_type = 'full', 'diag' or 'spherical'
    % method = 'rnd' (choose centers randomly from data) or 'kmeans' (needs netlab)
    %
    % OUTPUTS:
    % mu(:,k) 
    % Sigma(:,:,k) 
    % weights(k)
    '''
    
    if isinstance(data, list):
        data = np.hstack(data)
    elif data.ndim==3:
        O, T, N = data.shape
        data = np.reshape(np.transpose(data, (0, 2, 1)), (O, T*N))
    d, T = data.shape
    
    if method=='rnd':
        C = np.atleast_2d(np.cov(data))
        Sigma = np.transpose(np.tile(np.diag(np.diag(C))*0.5, (M, 1, 1)), (2, 1, 0))
        # Initialize each mean to a random data point
        indices = np.arange(T)
        np.random.shuffle(indices)
        mu = data[:,indices[0:M]]
        weights, _ = normalise(np.ones((M,1)))
    elif method=='kmeans':
        
        gmm = GMM(n_components=M, covariance_type=cov_type,
                  thresh=1e-2, min_covar=1e-3,
                  n_iter=5, n_init=1, params='wmc', init_params='wmc')
        
        gmm.fit(data.T)
        
        mu = gmm.means_.T
        weights = np.asmatrix(gmm.weights_).T
        covars = gmm.covars_
        
        Sigma = np.zeros((d,d,M))
        for m in range(M):
            if cov_type=='diag':
                Sigma[:,:,m] = np.diag(covars[m,:])
            elif cov_type=='full':
                Sigma[:,:,m] = covars[:,:,m]
            elif cov_type=='spherical':
                Sigma[:,:,m] = covars[m] * np.eye(d)
    
    return mu, Sigma, weights
def build_models(data,class_name,file_name):
   
    print("Building EM")
    #Gausian_EM=GMM(n_components=6,n_init=1000)
    Gausian_EM=GMM(n_components=6)
    Gausian_EM.fit(data)
    print("Building SVM")
    #SVM_Model=SVC(probability=True,kernel='RBF')
    #SVM_Model=SVC(probability=True,kernel='poly')
    SVM_Model=SVC(probability=True)
    print(SVM_Model.fit(data,class_name))
    print("Building Gausian Naive")
    Gausian_Naive_Model=GaussianNB()
    Gausian_Naive_Model.fit(data,class_name)
    print("Building Kmeans")
    K_means_model=KMeans(n_clusters=7)
    K_means_model.fit(data)
    
    return Gausian_EM,SVM_Model,Gausian_Naive_Model,K_means_model
#min_max_scaler=preprocessing.MinMaxScaler(feature_range=(-1000, 10))
features_scaled = data
'''
print(features_scaled.shape)
print(features_scaled.min(axis=0))
print(features_scaled.max(axis=0))
scatter(features_scaled[:,0],features_scaled[:,1])
'''
#labels=model.fit_predict(features_scaled)
#print(labels)

print("checking for the song", file_name[2])
print("original class", class_name1[2])

model = GMM(n_components=7)
model.fit(data)
print("EM predict class", model.predict(data[2]))
print("EM predict class", model.predict_proba(data[2]))
#print("EM score",model.score(data, class_name1))

model = SVC(probability=True)
#model.fit(data,class_name1)
#print("SVM predict class",model.predict(data[2]))
#print("SVM predict class",model.predict_proba(data[2]))
#print("SVM score",model.score(data, class_name1))

model = GaussianNB()
#model.fit(data,class_name1)
#print("Gausian Naive predict class",model.predict(data[2]))
#print("Gausian Naive predict class",model.predict_proba(data[2]))
#print("GNB score",model.score(data, class_name1))
Пример #5
0
# print W

# plot projection
pca = PCA(n_components=2)
pca.fit(omegas[:,1:4])
projs = pca.transform(omegas[:,1:4])
plt.plot(projs[:,0], projs[:,1], 'bo')
plt.savefig('projs.png')
plt.close()

# fit mixture of gaussians
N = omegas.shape[0]
for k in range(1,10): 
    g = GMM(n_components=k, covariance_type='full')
    mixture = g.fit(omegas[0:(int(0.75*N)),1:4])
    print k, ":", sum(mixture.score_samples(omegas[(int(0.75*N)):N,1:4])[0])

g = GMM(n_components=3, covariance_type='full')
mixture = g.fit(omegas[:,1:4])
print "weights:", mixture.weights_
print "means:", mixture.means_
print "covariances:", mixture.covars_

A = np.transpose(pca.transform(np.eye(3)))
print A

new_means = np.zeros((3, 2))
new_covars = np.zeros((3, 2, 2))

print mixture.means_.shape
Пример #6
0
def train_user():
    print('start capturing video.....')
    time.sleep(3)
    cam = cv2.VideoCapture(0)
    cam.set(3, 640)  # set video width
    cam.set(4, 480)  # set video height

    face_detector = cv2.CascadeClassifier(
        'haarcascade_frontalface_default.xml')

    # For each person, enter one numeric face id'''
    face_id = input('enter user id=')
    name = input('enter user-name=')
    path = '/Users/parthpatel/Desktop/Face-Voice-Recognition/dataset/' + face_id
    os.mkdir(path)
    print("[INFO] Initializing face capture. Look at the camera and wait ...")
    # Initialize individual sampling face count
    count = 0
    while (True):
        ret, img = cam.read()
        # img = cv2.flip(img, -1) # flip video image vertically
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_detector.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            count += 1

            # Save the captured image into the datasets folder
            cv2.imwrite(
                "/Users/parthpatel/Desktop/Face-Voice-Recognition/dataset/" +
                face_id + "/User." + str(face_id) + '.' + str(count) + ".jpg",
                gray[y:y + h, x:x + w])

            cv2.imshow('image', img)

        k = cv2.waitKey(100) & 0xff  # Press 'ESC' for exiting video
        if k == 27:
            break
        elif count >= 30:  # Take 30 face sample and stop video
            break
    # release camera
    print("[INFO] Exiting Program .......")
    cam.release()
    cv2.destroyAllWindows()

    # Path for face image database
    path = '/Users/parthpatel/Desktop/Face-Voice-Recognition/dataset/' + face_id

    recognizer = cv2.face.LBPHFaceRecognizer_create()
    detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

    # function to get the images and label data
    def getImagesAndLabels(path):
        imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
        faceSamples = []
        ids = []
        for imagePath in imagePaths:
            if imagePath == path + '.DS_Store':
                continue
            PIL_img = Image.open(imagePath).convert(
                'L')  # convert it to grayscale
            img_numpy = np.array(PIL_img, 'uint8')
            id = int(os.path.split(imagePath)[-1].split(".")[1])
            faces = detector.detectMultiScale(img_numpy)
            for (x, y, w, h) in faces:
                faceSamples.append(img_numpy[y:y + h, x:x + w])
                ids.append(id)
        return faceSamples, ids

    print("\n [INFO] Training faces. It will take a few seconds. Wait ...")
    faces, ids = getImagesAndLabels(path)
    recognizer.train(faces, np.array(ids))
    train_path = '/Users/parthpatel/Desktop/Face-Voice-Recognition/trainer/' + face_id
    os.mkdir(train_path)
    # Save the model into trainer/trainer.yml
    recognizer.write(
        '/Users/parthpatel/Desktop/Face-Voice-Recognition/trainer/' + face_id +
        '/trainer.yml')  # recognizer.save() worked on Mac, but not on Pi

    # Print the numer of faces trained and end program
    print("\n [INFO] {0} faces trained. Exiting Program".format(
        len(np.unique(ids))))

    print(
        '--------------------------------------------------------------------------------------------'
    )
    time.sleep(5)
    print(
        '--------------------------------------------------------------------------------------------'
    )
    # We'll run the script for different people and store
    # the data into multiple files
    # Voice authentication
    CHUNK = 1024
    FORMAT = pyaudio.paInt16
    CHANNELS = 2
    RATE = 44100
    RECORD_SECONDS = 3
    WAVE_OUTPUT_FILENAME = "./voices/" + face_id

    os.mkdir(WAVE_OUTPUT_FILENAME)
    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

    print("* recording")

    print("...........")
    frames = []
    for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
        audio_data = stream.read(CHUNK)
        frames.append(audio_data)

    print("* done recording")

    stream.stop_stream()
    stream.close()
    p.terminate()

    # # saving wav file of speaker
    waveFile = wave.open(
        WAVE_OUTPUT_FILENAME + '/' + name + '_sample_' + face_id + '.wav',
        'wb')
    waveFile.setnchannels(CHANNELS)
    waveFile.setsampwidth(p.get_sample_size(FORMAT))
    waveFile.setframerate(RATE)
    waveFile.writeframes(b''.join(frames))
    waveFile.close()
    print("Done")

    # # path to training data
    source = WAVE_OUTPUT_FILENAME
    # # path to save trained model
    dest = "./gmm_model/"
    files = [
        os.path.join(source, f) for f in os.listdir(source)
        if f.endswith('.wav')
    ]
    print(files[0])
    features = np.array([])
    sr, audio = read(files[0])
    # convert audio to mfcc
    vector = extract_features(audio, sr)
    features = vector
    # for more accurate weights
    features = np.vstack((features, vector))
    features = np.vstack((features, vector))
    print(features.shape)
    #Gaussian-mixture-model to save gmm-model
    gmm = GMM(n_components=16, n_iter=200, covariance_type='diag', n_init=3)
    gmm.fit(features)
    # # picklefile = f.split("\\")[-2].split(".wav")[0]+".gmm"
    # # model saving..
    pickle.dump(gmm, open(dest + name + '_' + face_id + '.gmm', 'wb'))
    print(name + ' ' + 'added......')
    features = np.asarray(())
Пример #7
0
				for token in test_tokens:
					if token in w2v_model:
						test_vectors.append(w2v_model[token])
					elif token in unknown_words:
						test_vectors.append(unknown_words[token])
					else:
						unknown_vec = w2v_model.seeded_vector(token)
						unknown_words[token] = unknown_vec
						test_vectors.append(unknown_vec)

				# Train GMM
				print 'Starting GMM training'
				words = w2v_model.vocab.keys()
				word_vectors = w2v_model.syn0
				gmm_model = GMM(n_components=num_topics, n_iter=num_gmm_iterations, covariance_type='diag')
				gmm_model.fit(word_vectors)
				# joblib.dump(gmm_model, gmm_output_file) 
				print 'Done GMM training'

				# Get the likelihood of each word vector under each Gaussian component
				scores = gmm_model.score(test_vectors)
				print scores
				ll = sum(scores)
				print "LL:   "+str(ll)

				# Print topics if desired
				if print_topics:
					log_probs = log_multivariate_normal_density(word_vectors, gmm_model.means_, gmm_model.covars_, gmm_model.covariance_type)
					print np.min(log_probs)
					_, num_col = log_probs.shape
					for col in xrange(num_col):
Пример #8
0
                        test_vectors.append(w2v_model[token])
                    elif token in unknown_words:
                        test_vectors.append(unknown_words[token])
                    else:
                        unknown_vec = w2v_model.seeded_vector(token)
                        unknown_words[token] = unknown_vec
                        test_vectors.append(unknown_vec)

                # Train GMM
                print 'Starting GMM training'
                words = w2v_model.vocab.keys()
                word_vectors = w2v_model.syn0
                gmm_model = GMM(n_components=num_topics,
                                n_iter=num_gmm_iterations,
                                covariance_type='diag')
                gmm_model.fit(word_vectors)
                # joblib.dump(gmm_model, gmm_output_file)
                print 'Done GMM training'

                # Get the likelihood of each word vector under each Gaussian component
                scores = gmm_model.score(test_vectors)
                print scores
                ll = sum(scores)
                print "LL:   " + str(ll)

                # Print topics if desired
                if print_topics:
                    log_probs = log_multivariate_normal_density(
                        word_vectors, gmm_model.means_, gmm_model.covars_,
                        gmm_model.covariance_type)
                    print np.min(log_probs)
print(features_scaled.shape)
print(features_scaled.min(axis=0))
print(features_scaled.max(axis=0))
scatter(features_scaled[:,0],features_scaled[:,1])
'''
#labels=model.fit_predict(features_scaled)
#print(labels)




print("checking for the song",file_name[2])
print("original class",class_name1[2])

model=GMM(n_components=7)
model.fit(data)
print("EM predict class",model.predict(data[2]))
print("EM predict class",model.predict_proba(data[2]))
#print("EM score",model.score(data, class_name1))

model=SVC(probability=True)
#model.fit(data,class_name1)
#print("SVM predict class",model.predict(data[2]))
#print("SVM predict class",model.predict_proba(data[2]))
#print("SVM score",model.score(data, class_name1))

model=GaussianNB()
#model.fit(data,class_name1)
#print("Gausian Naive predict class",model.predict(data[2]))
#print("Gausian Naive predict class",model.predict_proba(data[2]))
#print("GNB score",model.score(data, class_name1))