コード例 #1
0
ファイル: gmm.py プロジェクト: YJingyu/IDT_Fisher_Vector
def gmm_model(sample, k_gmm, PCA=False):
    """
    Returns a tuple: (gmm,mean,pca_transform)
    gmm is the ynumpy gmm model fro the sample data. 
    pca_tranform is None if PCA is True.
    Reduces the dimensions of the sample (by 50%) if PCA is true
    """

    print "Building GMM model"
    # until now sample was in uint8. Convert to float32
    sample = sample.astype('float32')
    # compute mean and covariance matrix for the PCA
    mean = sample.mean(axis=0)  #for each row
    sample = sample - mean
    pca_transform = None
    if PCA:
        cov = np.dot(sample.T, sample)

        #decide to keep 1/2 of the original components, so vid_trajs_bm.shape[1]/2
        #compute PCA matrix and keep only 1/2 of the dimensions.
        orig_comps = sample.shape[1]
        pca_dim = orig_comps / 2
        #eigvecs are normalized.
        eigvals, eigvecs = np.linalg.eig(cov)
        perm = eigvals.argsort()  # sort by increasing eigenvalue
        pca_transform = eigvecs[:, perm[
            orig_comps -
            pca_dim:orig_comps]]  # eigenvectors for the 64 last eigenvalues
        # transform sample with PCA (note that numpy imposes line-vectors,
        # so we right-multiply the vectors)
        sample = np.dot(sample, pca_transform)
    # train GMM
    gmm = ynumpy.gmm_learn(sample, k_gmm)
    toReturn = (gmm, mean, pca_transform)
    return toReturn
コード例 #2
0
	def SampleDescriptors(image_descs, k , n_sample):
		# make a big matrix with all image descriptors
		all_desc = np.vstack(image_descs)

		# choose n_sample descriptors at random
		sample_indices = np.random.choice(all_desc.shape[0], n_sample)
		sample = all_desc[sample_indices]

		# until now sample was in uint8. Convert to float32
		sample = sample.astype('float32')

		# compute mean and covariance matrix for the PCA
		mean = sample.mean(axis = 0)
		sample = sample - mean
		cov = np.dot(sample.T, sample)

		# compute PCA matrix and keep only 64 dimensions
		eigvals, eigvecs = np.linalg.eig(cov)
		perm = eigvals.argsort()                   # sort by increasing eigenvalue
		pca_transform = eigvecs[:, perm[64:128]]   # eigenvectors for the 64 last eigenvalues

		# transform sample with PCA (note that numpy imposes line-vectors,
		# so we right-multiply the vectors)
		sample = np.dot(sample, pca_transform)

		# train GMM
		gmm = ynumpy.gmm_learn(sample, k)
		
		return gmm, pca_transform, mean
コード例 #3
0
ファイル: gmm.py プロジェクト: directional-star/seniorThesis
def gmm_model(sample, k_gmm, PCA=False):
    """
    Returns a tuple: (gmm,mean,pca_transform)
    gmm is the ynumpy gmm model fro the sample data. 
    pca_tranform is None if PCA is True.
    Reduces the dimensions of the sample (by 50%) if PCA is true
    """

    print "Building GMM model"
    # until now sample was in uint8. Convert to float32
    sample = sample.astype('float32')
    # compute mean and covariance matrix for the PCA
    mean = sample.mean(axis = 0) #for each row
    sample = sample - mean
    pca_transform = None
    if PCA:
        cov = np.dot(sample.T, sample)

        #decide to keep 1/2 of the original components, so vid_trajs_bm.shape[1]/2
        #compute PCA matrix and keep only 1/2 of the dimensions.
        orig_comps = sample.shape[1]
        pca_dim = orig_comps/2
        #eigvecs are normalized.
        eigvals, eigvecs = np.linalg.eig(cov)
        perm = eigvals.argsort() # sort by increasing eigenvalue 
        pca_transform = eigvecs[:, perm[orig_comps-pca_dim:orig_comps]]   # eigenvectors for the 64 last eigenvalues
        # transform sample with PCA (note that numpy imposes line-vectors,
        # so we right-multiply the vectors)
        sample = np.dot(sample, pca_transform)
    # train GMM
    gmm = ynumpy.gmm_learn(sample, k_gmm)
    toReturn = (gmm,mean,pca_transform)
    return toReturn
コード例 #4
0
ファイル: codebooks.py プロジェクト: vcampmany/M3_ImageClassi
def compute_codebook(D,
                     code_size,
                     nfeatures,
                     fold_i=None,
                     features='sift',
                     grid_step=None,
                     n_comp=128):
    if features == 'sift':
        features = str(nfeatures)  # do not change filename for basic sift
    elif features == 'dense_sift':
        features = 'dense_sift_' + str(grid_step)

    if fold_i is not None:
        code_name = "codebooks/" + str(code_size) + "_" + str(
            n_comp) + "_" + features + "_fold_" + str(fold_i) + ".dat"
    else:
        code_name = "codebooks/" + str(code_size) + "_" + str(
            n_comp) + "_" + features + ".dat"
    if not os.path.isfile(code_name):
        print 'Computing kmeans with ' + str(code_size) + ' centroids'
        init = time.time()
        codebook = ynumpy.gmm_learn(np.float32(D), code_size)
        cPickle.dump(codebook, open(code_name, "wb"))
        end = time.time()
        print 'Done in ' + str(end - init) + ' secs.'
    else:
        codebook = cPickle.load(open(code_name, "r"))

    return codebook
コード例 #5
0
ファイル: fisherVectors.py プロジェクト: eglrp/M3-Team5
def getGMM(D, k):
    print 'Computing gmm with ' + str(k) + ' centroids'
    init = time.time()
    gmm = ynumpy.gmm_learn(np.float32(D), k)
    end = time.time()
    print 'Done in ' + str(end - init) + ' secs.'
    return gmm
コード例 #6
0
ファイル: train_video.py プロジェクト: ai3DVision/yael
def save_model(sample, type_feature, RESULT_PATH, TYPE):
	'''Save GMM models, mean, pca_transform of sample data

	Parameters
	----------
	sample: np.ndarray type,
		Sample data 
	type_feature: {'traj', 'hog', 'hof', 'mbh'}
		One of four types of feature
	RESULT_PATH: string
		Path of result files
	TYPE: {'video', 'sensor'}
		Type of data

	Returns
	-------
	gmm: gmm model
	mean: mean value of data
	pca_transform: pca transform of data
	'''
	# GMM part
	k = 25
	if type_feature == 'traj':
		DIM_AFTER_PCA = 0.5 * 30
	elif type_feature == 'hog':
		DIM_AFTER_PCA = 0.5 * 96
	elif type_feature == 'hof':
		DIM_AFTER_PCA = 0.5 * 108
	elif type_feature == 'mbh':
		DIM_AFTER_PCA = 0.5 * 192

	# compute mean and covariance matrix for the PCA
	mean = sample.mean(axis = 0)
	sample = sample - mean
	cov = np.dot(sample.T, sample)
	# compute PCA matrix and keep only 64 dimensions
	eigvals, eigvecs = np.linalg.eig(cov)
	# sort by increasing eigenvalue
	perm = eigvals.argsort()
	# eigenvectors for the 64 last eigenvalues
	pca_transform = eigvecs[:, perm[-DIM_AFTER_PCA:]]
	# transform sample with PCA (note that numpy imposes line-vectors,
	# so we right-multiply the vectors)
	sample = np.dot(sample, pca_transform)
	# train GMM
	gmm = ynumpy.gmm_learn(sample, k)
	# save mean value to file
	file_mean = open(RESULT_PATH + 'mean_' + type_feature + '_' + TYPE + '.pkl', 'wb')
	pickle.dump(mean, file_mean)
	file_mean.close()
	# save pca transform to file
	file_pca = open(RESULT_PATH + 'pca_' + type_feature + '_' + TYPE + '.pkl', 'wb')
	pickle.dump(pca_transform, file_pca)
	file_pca.close()
	# save GMM model to file
	file_gmm = open(RESULT_PATH + 'gmm_' + type_feature + '_' + TYPE + '.pkl', 'wb')
	pickle.dump(gmm, file_gmm)
	file_gmm.close()

	return gmm, mean, pca_transform
コード例 #7
0
def compute_codebook(D,
                     code_size,
                     nfeatures,
                     fold_i=None,
                     output='fc2',
                     n_comp=128,
                     sub_step=4,
                     sub_type='default'):

    features = 'cnn_' + output + '_samp_' + sub_type + '_' + str(sub_step)

    if fold_i is not None:
        code_name = "codebooks/" + str(code_size) + "_" + str(
            n_comp) + "_" + features + "_fold_" + str(fold_i) + ".dat"
    else:
        code_name = "codebooks/" + str(code_size) + "_" + str(
            n_comp) + "_" + features + ".dat"
    if not os.path.isfile(code_name):
        print 'Computing kmeans with ' + str(code_size) + ' centroids'
        init = time.time()
        codebook = ynumpy.gmm_learn(np.float32(D), code_size)
        cPickle.dump(codebook, open(code_name, "wb"))
        end = time.time()
        print 'Done in ' + str(end - init) + ' secs.'
    else:
        codebook = cPickle.load(open(code_name, "r"))

    return codebook
コード例 #8
0
def train_fv_gmms(tracklets_path, videonames, traintest_parts, feat_types, intermediates_path, pca_reduction=False, nt=4, verbose=False):
    try:
        makedirs(intermediates_path)
    except OSError:
        pass

    for k, part in enumerate(traintest_parts):
        train_inds = np.where(np.array(part) <= 0)[0]  # train codebook for each possible training parition
        num_samples_per_vid = int(INTERNAL_PARAMETERS['n_samples'] / float(len(train_inds)))

        # process the videos
        for i, feat_t in enumerate(feat_types):
            D = None

            # Train GMMs
            output_filepath = join(intermediates_path, 'gmm' + ('_pca-' if pca_reduction else '-') + feat_t + '-' + str(k) + '.pkl')
            if isfile(output_filepath):
                if verbose:
                    print('[train_fv_gmms] %s -> OK' % output_filepath)
                continue

            start_time = time.time()

            D = load_tracklets_sample(tracklets_path, videonames, train_inds, feat_t, num_samples_per_vid, verbose=verbose)

            # (special case) trajectory features are originally positions
            if feat_t == 'trj':
                D = convert_positions_to_displacements(D)

            if feat_t == 'mbh':
                Dx = preprocessing.normalize(D[:,:D.shape[1]/2], norm='l1', axis=1)
                Dy = preprocessing.normalize(D[:,D.shape[1]/2:], norm='l1', axis=1)
                D = np.hstack((Dx,Dy))
            else:
                D = preprocessing.normalize(D, norm='l1', axis=1)

            if feat_t != 'trj':
                D = rootSIFT(D)

            # compute PCA map and reduce dimensionality
            if pca_reduction:
                pca = PCA(n_components=int(INTERNAL_PARAMETERS['reduction_factor']*D.shape[1]), copy=False)
                D = pca.fit_transform(D)

            # train GMMs for later FV computation
            D = np.ascontiguousarray(D, dtype=np.float32)
            gmm = ynumpy.gmm_learn(D, INTERNAL_PARAMETERS['fv_gmm_k'], nt=nt, niter=500, redo=1, verbose=verbose)

            with open(output_filepath, 'wb') as f:
                cPickle.dump(dict(pca=(pca if pca_reduction else None), gmm=gmm), f)
            # with open(join(intermediates_path, 'gmm-sample' + ('_pca-' if pca_reduction else '-') + feat_t + '-' + str(k) + '.pkl'), 'wb') as f:
            #     cPickle.dump(D,f)

            elapsed_time = time.time() - start_time
            if verbose:
                print('[train_fv_gmms] %s -> DONE (in %.2f secs)' % (feat_t, elapsed_time))
コード例 #9
0
def compute_codebook_gmm(kmeans, D):
    # Clustering (unsupervised classification)
    # Fit a GMM over the features.
    print 'Computing gmm with ' + str(kmeans) + ' centroids'
    sys.stdout.flush()
    init = time.time()
    gmm = ynumpy.gmm_learn(np.float32(D), kmeans)
    end = time.time()
    print 'Done in ' + str(end - init) + ' secs.'
    return gmm
コード例 #10
0
def get_distances(train_images):
	surf = cv2.SURF(hessianThreshold=500, extended=True)
	image_descs = []
	for fnames in train_images:
		try:
    			img = cv2.imread(fnames,0);
		    	kp, des = surf.detectAndCompute(img, None)			
		except:
			continue
		image_descs.append(des)


	all_desc= np.vstack(image_descs)

	k = 128
	n_sample = k * 500

	sample = all_desc
	sample = sample.astype('float32')

	mean = sample.mean(axis = 0)
	sample = sample - mean
	cov = np.dot(sample.T, sample)

	eigvals, eigvecs = np.linalg.eig(cov)
	perm = eigvals.argsort()
	pca_transform = eigvecs[:, perm[32:128]]

	sample = np.dot(sample, pca_transform)
	gmm = ynumpy.gmm_learn(sample, k)

	image_fvs = []
	for image_desc in image_descs:
		image_desc = np.dot(image_desc - mean, pca_transform)
		fv = ynumpy.fisher(gmm, image_desc, include = 'mu')
		image_fvs.append(fv)

	image_fvs = np.vstack(image_fvs)
	image_fvs = np.sign(image_fvs) * np.abs(image_fvs) ** 0.5
	norms = np.sqrt(np.sum(image_fvs ** 2, 1))
	image_fvs /= norms.reshape(-1, 1)

	image_fvs[np.isnan(image_fvs)] = 100

	query_imnos = range(0,len(image_fvs)-1);
	query_fvs = image_fvs#[query_imnos]

	results, distances = ynumpy.knn(query_fvs, image_fvs, nnn = len(image_fvs))
	s_results = np.argsort(results, axis = 1)
	s_distances = distances*0
	for i in range(distances.shape[0]):
	    s_distances[i,:] = distances[i,s_results[i,:]]
	
	return s_distances
コード例 #11
0
def gmm(features, ncomponents, num_iters, njobs, nredo, seed):
    """
    Fit a Gaussian Mixture Model with nclusters using either Yael (if available) or scikit-learn (otherwise). The GMM
    assumes a diagonal covariance matrix.

    :param features: Features to use to fit the GMM.
    :param ncomponents: Number of components in the GMM.
    :param num_iters: Maximum number of iterations to perform when fitting the GMM.
    :param njobs: Number of threads to use.
    :param nredo: Number of initializations to perform.
    :param seed: Seed for reproducibility
    :return: weights: GMM estimated weights
    :return: mus: GMM estimated means
    :return: sigmas: Diagonals of GMM estimated covariance matrices
    :return: gmm: GMM object from faiss or scikit-learn
    """
    print('Running GMM...')
    # if USE_POMEGRANATE:
    #     gmm = pomegranate.GeneralMixtureModel.from_samples(pomegranate.MultivariateGaussianDistribution, n_components=nclusters, X=features, n_init=nredo)
    #     weights = np.exp(gmm.weights)
    #     mus = np.array([gmm.distributions[i].parameters[0] for i in range(len(gmm.distributions))])
    #     sigmas = np.array([np.diag(gmm.distributions[i].parameters[1]) for i in range(len(gmm.distributions))])
    if USE_YAEL:
        pca_features = np.ascontiguousarray(features).astype('float32')
        if seed is None:
            seed = 0
        gmm = ynumpy.gmm_learn(pca_features,
                               ncomponents,
                               nt=njobs,
                               niter=num_iters,
                               redo=nredo,
                               seed=seed)
        weights, mus, sigmas = gmm[0], gmm[1], gmm[2]
    else:
        gmm = sklearn.mixture.GaussianMixture(n_components=ncomponents,
                                              covariance_type='diag',
                                              n_init=nredo,
                                              random_state=seed)
        gmm.fit(features)
        weights, mus, sigmas = gmm.weights_, gmm.means_, gmm.covariances_

    return weights, mus, sigmas, gmm
コード例 #12
0
def compute_fisher_vectors(D, n_components, k):

    print 'Computing gmm with ' + str(k) + ' centroids'
    init = time.time()
    gmm = ynumpy.gmm_learn(np.float32(D), k)
    end = time.time()
    print 'Done in ' + str(end - init) + ' secs.'

    init = time.time()
    fisher = np.zeros((len(Train_descriptors), k * n_components * 2),
                      dtype=np.float32)
    for i in xrange(len(Train_descriptors)):
        fisher[i, :] = ynumpy.fisher(gmm,
                                     np.float32(D),
                                     include=['mu', 'sigma'])

    end = time.time()
    print 'Done in ' + str(end - init) + ' secs.'

    return (fisher, gmm)
    def fit(self, X, y=None):
        image_descs = X['descriptors']

        # Compute a PCA over the SIFT descriptors
        # make a big matrix with all image descriptors
        all_desc = np.vstack(image_descs)
        '''
        #n_sample = k * 1000

        # choose n_sample descriptors at random
        sample_indices = np.random.choice(all_desc.shape[0], n_sample)
        sample = all_desc[sample_indices]

        # until now sample was in uint8. Convert to float32
        sample = sample.astype('float32')

        # compute mean and covariance matrix for the PCA
        mean = sample.mean(axis=0)
        sample = sample - mean
        cov = np.dot(sample.T, sample)

        # compute PCA matrix and keep only 64 dimensions
        eigvals, eigvecs = np.linalg.eig(cov)
        perm = eigvals.argsort()  # sort by increasing eigenvalue
        pca_transform = eigvecs[:, perm[64:128]]  # eigenvectors for the 64 last eigenvalues
        
        # transform sample with PCA (note that numpy imposes line-vectors,
        # so we right-multiply the vectors)
        sample = np.dot(sample, pca_transform)
        '''
        self.mean = np.mean(all_desc, axis=0)
        all_desc=all_desc-self.mean

        self.PCA = PCA(n_components=self.n_components)
        self.PCA.fit(all_desc)
        all_desc=self.PCA.transform(all_desc)

        # train GMM Codebook
        self.gmm = ynumpy.gmm_learn(all_desc, self.K)

        return self
コード例 #14
0
def gmm_voc(visual_world, n_gaussians, *conf):
    """
    ____________________________________________________________________
       gmm_voc:
         Construct a gaussian mixture model to describe the distribution
         over feature space.
         args:
           visual_world: NxM np array where N is the numer of visual
             examples and M is the legth of dimensionality.
           n_gaussians: Number of gaussians to model feature space.
         return:
           v_words: KxM np array where K is the number of gaussians and 
             M is the length of dimensionality.
    ____________________________________________________________________
    """
    ############################################################################
    # Required both: c-contigous and float32 array.
    visual_world = np.ascontiguousarray(visual_world.astype(np.float32))
    n_gaussians = np.int(n_gaussians)
    v_words = gmm_learn(visual_world, n_gaussians)
    ############################################################################
    return v_words
コード例 #15
0
ファイル: demo.py プロジェクト: bityangke/yael
# compute mean and covariance matrix for the PCA
mean = sample.mean(axis = 0)
sample = sample - mean
cov = np.dot(sample.T, sample)

# compute PCA matrix and keep only 64 dimensions
eigvals, eigvecs = np.linalg.eig(cov)
perm = eigvals.argsort()                   # sort by increasing eigenvalue
pca_transform = eigvecs[:, perm[64:128]]   # eigenvectors for the 64 last eigenvalues

# transform sample with PCA (note that numpy imposes line-vectors,
# so we right-multiply the vectors)
sample = np.dot(sample, pca_transform)

# train GMM
gmm = ynumpy.gmm_learn(sample, k)

image_fvs = []
for image_desc in image_descs:
   # apply the PCA to the image descriptor
   image_desc = np.dot(image_desc - mean, pca_transform)
   # compute the Fisher vector, using the derivative w.r.t mu and sigma
   fv = ynumpy.fisher(gmm, image_desc, include = 'mu, sigma')
   image_fvs.append(fv)

# make one matrix with all FVs
image_fvs = np.vstack(image_fvs)

# normalizations are done on all descriptors at once

# power-normalization
コード例 #16
0
ファイル: search_ukbench.py プロジェクト: pioneer911/yael
print("Training set of %d local descriptors in %d dimensions" %
      (train_set.shape[0], train_set.shape[1]))

trainset_size = num_gmm_components * 1000

if trainset_size < train_set.shape[0]:
    print("Subsampling to %d points" % trainset_size)
    subset = numpy.array(
        random.sample(range(train_set.shape[0]), trainset_size))
    train_set = train_set[subset]

print("Training Gaussian mixture model with %d components" %
      num_gmm_components)

train_set = train_set.astype('float32')
gmm = ynumpy.gmm_learn(train_set, num_gmm_components)

print("Make the image index")

dataset = []
queries = []

if show:
    fig = plt.figure(figsize=(10, 10))
    fig.canvas.set_window_title("100 image dataset")
    plot_idx = 1

for i in image_range:
    filename = "%s/ukbench%05d.siftgeo" % (sift_directory, i)
    print("  " + filename + "\r")
    sys.stdout.flush()
コード例 #17
0
# Transform everything to numpy arrays
size_descriptors = Train_descriptors[0].shape[1]
D = np.zeros((np.sum([len(p) for p in Train_descriptors]), size_descriptors),
             dtype=np.uint8)
startingpoint = 0
for i in range(len(Train_descriptors)):
    D[startingpoint:startingpoint +
      len(Train_descriptors[i])] = Train_descriptors[i]
    startingpoint += len(Train_descriptors[i])

k = 32

print 'Computing gmm with ' + str(k) + ' centroids'
init = time.time()
gmm = ynumpy.gmm_learn(np.float32(D), k)
end = time.time()
print 'Done in ' + str(end - init) + ' secs.'

init = time.time()
fisher = np.zeros((len(Train_descriptors), k * 128 * 2), dtype=np.float32)
for i in xrange(len(Train_descriptors)):
    fisher[i, :] = ynumpy.fisher(gmm,
                                 Train_descriptors[i],
                                 include=['mu', 'sigma'])

end = time.time()
print 'Done in ' + str(end - init) + ' secs.'

# Train a linear SVM classifier
コード例 #18
0
def train_fv_gmms(tracklets_path,
                  videonames,
                  traintest_parts,
                  feat_types,
                  intermediates_path,
                  pca_reduction=False,
                  nt=4,
                  verbose=False):
    try:
        makedirs(intermediates_path)
    except OSError:
        pass

    for k, part in enumerate(traintest_parts):
        train_inds = np.where(np.array(part) <= 0)[
            0]  # train codebook for each possible training parition
        num_samples_per_vid = int(INTERNAL_PARAMETERS['n_samples'] /
                                  float(len(train_inds)))

        # process the videos
        for i, feat_t in enumerate(feat_types):
            D = None

            # Train GMMs
            output_filepath = join(
                intermediates_path,
                'gmm' + ('_pca-' if pca_reduction else '-') + feat_t + '-' +
                str(k) + '.pkl')
            if isfile(output_filepath):
                if verbose:
                    print('[train_fv_gmms] %s -> OK' % output_filepath)
                continue

            start_time = time.time()

            D = load_tracklets_sample(tracklets_path,
                                      videonames,
                                      train_inds,
                                      feat_t,
                                      num_samples_per_vid,
                                      verbose=verbose)

            # (special case) trajectory features are originally positions
            if feat_t == 'trj':
                D = convert_positions_to_displacements(D)

            if feat_t == 'mbh':
                Dx = preprocessing.normalize(D[:, :D.shape[1] / 2],
                                             norm='l1',
                                             axis=1)
                Dy = preprocessing.normalize(D[:, D.shape[1] / 2:],
                                             norm='l1',
                                             axis=1)
                D = np.hstack((Dx, Dy))
            else:
                D = preprocessing.normalize(D, norm='l1', axis=1)

            if feat_t != 'trj':
                D = rootSIFT(D)

            # compute PCA map and reduce dimensionality
            if pca_reduction:
                pca = PCA(n_components=int(
                    INTERNAL_PARAMETERS['reduction_factor'] * D.shape[1]),
                          copy=False)
                D = pca.fit_transform(D)

            # train GMMs for later FV computation
            D = np.ascontiguousarray(D, dtype=np.float32)
            gmm = ynumpy.gmm_learn(D,
                                   INTERNAL_PARAMETERS['fv_gmm_k'],
                                   nt=nt,
                                   niter=500,
                                   redo=1,
                                   verbose=verbose)

            with open(output_filepath, 'wb') as f:
                cPickle.dump(
                    dict(pca=(pca if pca_reduction else None), gmm=gmm), f)
            # with open(join(intermediates_path, 'gmm-sample' + ('_pca-' if pca_reduction else '-') + feat_t + '-' + str(k) + '.pkl'), 'wb') as f:
            #     cPickle.dump(D,f)

            elapsed_time = time.time() - start_time
            if verbose:
                print('[train_fv_gmms] %s -> DONE (in %.2f secs)' %
                      (feat_t, elapsed_time))
コード例 #19
0
def train_system(train_filenames, train_labels, detector, options):
    # Read the images and extract the SIFT features.
    Train_descriptors = []
    Train_label_per_descriptor = []
    for i in range(len(train_filenames)):
        filename = train_filenames[i]
        print 'Reading image ' + filename
        ima = cv2.imread(filename)
        gray = cv2.cvtColor(ima, cv2.COLOR_BGR2GRAY)
        if options.spatial_pyramids:
            des = spatial_pyramid(gray, detector, options)
        else:
            des = extract_SIFT_features(gray, detector,
                                        options.detector_options)
        Train_descriptors.append(des)
        Train_label_per_descriptor.append(train_labels[i])

    # Transform everything to numpy arrays
    D = Train_descriptors[0]
    L = np.array([Train_label_per_descriptor[0]] *
                 Train_descriptors[0].shape[0])
    for i in range(1, len(Train_descriptors)):
        D = np.vstack((D, Train_descriptors[i]))
        L = np.hstack((L,
                       np.array([Train_label_per_descriptor[i]] *
                                Train_descriptors[i].shape[0])))

    stdSlr_features = StandardScaler()
    pca = None
    if options.apply_pca:
        stdSlr_features = StandardScaler().fit(D)
        D = stdSlr_features.transform(D)
        pca = PCA(n_components=options.ncomp_pca)
        pca.fit(D)
        D = pca.transform(D)

    print 'Computing gmm with ' + str(options.kmeans) + ' centroids'
    init = time.time()
    gmm = ynumpy.gmm_learn(np.float32(D), options.kmeans)
    end = time.time()
    print 'Done in ' + str(end - init) + ' secs.'

    if options.apply_pca:
        num_features = options.ncomp_pca
    else:
        num_features = 128
    init = time.time()
    fisher = np.zeros(
        (len(Train_descriptors), options.kmeans * num_features * 2),
        dtype=np.float32)
    for i in xrange(len(Train_descriptors)):
        if options.apply_pca:
            descriptor = stdSlr_features.transform(Train_descriptors[i])
            descriptor = pca.trasform(descriptor)
        else:
            descriptor = Train_descriptors[i]
        fisher[i, :] = ynumpy.fisher(gmm, descriptor, include=['mu', 'sigma'])
    end = time.time()
    print 'Done in ' + str(end - init) + ' secs.'

    if options.apply_normalization:
        fisher = applyNormalization(fisher, options)

    # Train a linear SVM classifier
    stdSlr = StandardScaler().fit(fisher)
    D_scaled = stdSlr.transform(fisher)
    print 'Training the SVM classifier...'
    clf = svm.SVC(kernel='linear', C=1).fit(D_scaled, train_labels)
    print 'Done!'

    return stdSlr_features, pca, gmm, stdSlr, clf
コード例 #20
0
ファイル: test_ynumpy.py プロジェクト: ybrs/yael
else:
    print "vectors = "
    print v
    print "meta info = "
    print meta

print "kmeans:"

centroids = ynumpy.kmeans(v, 3)

print "result centroids ="
print centroids[:10, :]

print "gmm:"

gmm = ynumpy.gmm_learn(v, 3)

(w, mu, sigma) = gmm

print "mu = "
print mu

print "sigma = "
print sigma

muc = numpy.vstack((mu[0, :], mu[0, :]))

#                    mu[1, :],
#                    mu[1, :],
#                    mu[1, :]))
コード例 #21
0
ファイル: search_ukbench.py プロジェクト: Erotemic/yael
print("Training set of %d local descriptors in %d dimensions" % (train_set.shape[0], train_set.shape[1]))


trainset_size = num_gmm_components * 1000

if trainset_size < train_set.shape[0]:
    print("Subsampling to %d points" % trainset_size)
    subset = numpy.array(
        random.sample(range(train_set.shape[0]), trainset_size))
    train_set = train_set[subset]


print("Training Gaussian mixture model with %d components" % num_gmm_components)

train_set = train_set.astype('float32')
gmm = ynumpy.gmm_learn(train_set, num_gmm_components)

print("Make the image index")

dataset = []
queries = []

if show:
    fig = plt.figure(figsize=(10, 10))
    fig.canvas.set_window_title("100 image dataset")
    plot_idx = 1

for i in image_range:
    filename = "%s/ukbench%05d.siftgeo" % (sift_directory, i)
    print("  " + filename + "\r")
    sys.stdout.flush()
コード例 #22
0
def train_fv_gmms(tracklets_path, videonames, traintest_parts, feat_types, intermediates_path, pca_reduction=True, nt=4):
    if not exists(intermediates_path):
        makedirs(intermediates_path)

    for k, part in enumerate(traintest_parts):
        train_inds = np.where(part <= 0)[0]  # train codebook for each possible training parition
        total = len(train_inds)
        num_samples_per_vid = int(INTERNAL_PARAMETERS['n_samples'] / float(total))

        # process the videos
        for i, feat_t in enumerate(feat_types):
            output_filepath = intermediates_path + 'gmm' + ('_pca-' if pca_reduction else '-') + feat_t + '-' + str(k) + '.pkl'
            if isfile(output_filepath):
                print('%s -> OK' % output_filepath)
                continue

            start_time = time.time()

            D = None  # feat_t's sampled tracklets
            ptr = 0
            for j in range(0, total):
                idx = train_inds[j]

                filepath = tracklets_path + feat_t + '/' + videonames[idx] + '.pkl'
                if not isfile(filepath):
                    sys.stderr.write('# ERROR: missing training instance'
                                     ' {}\n'.format(filepath))
                    sys.stderr.flush()
                    quit()

                with open(filepath, 'rb') as f:
                    d = cPickle.load(f)

                # init sample
                if D is None:
                    D = np.zeros((INTERNAL_PARAMETERS['n_samples'], d.shape[1]), dtype=np.float32)
                # create a random permutation for sampling some tracklets in this vids
                randp = np.random.permutation(d.shape[0])
                if d.shape[0] > num_samples_per_vid:
                    randp = randp[:num_samples_per_vid]
                D[ptr:ptr+len(randp),:] = d[randp,:]
                ptr += len(randp)
            D = D[:ptr,:]  # cut out extra reserved space


            # (special case) trajectory features are originally positions
            if feat_t == 'trj':
                D = convert_positions_to_displacements(D)

            # scale (rootSIFT)
            D = rootSIFT(preprocessing.normalize(D, norm='l1', axis=1))

            # compute PCA map and reduce dimensionality
            if pca_reduction:
                pca = PCA(n_components=int(INTERNAL_PARAMETERS['reduction_factor']*D.shape[1]), copy=False)
                D = pca.fit_transform(D)

            # train GMMs for later FV computation
            D = np.ascontiguousarray(D, dtype=np.float32)
            gmm = ynumpy.gmm_learn(D, INTERNAL_PARAMETERS['fv_gmm_k'], nt=nt, niter=100, redo=3)

            with open(output_filepath, 'wb') as f:
                cPickle.dump(dict(pca=(pca if pca_reduction else None), gmm=gmm), f)

            elapsed_time = time.time() - start_time
            print('%s -> DONE (in %.2f secs)' % (feat_t, elapsed_time))
コード例 #23
0
ファイル: gmm.py プロジェクト: evhub/cnn-cbir-benchmark
sample = sample - mean
cov = np.dot(sample.T, sample)

# compute PCA matrix and keep only 64 dimensions
eigvals, eigvecs = np.linalg.eig(cov)
perm = eigvals.argsort()  # sort by increasing eigenvalue
print("perm.shape = {}".format(perm.shape))
pca_transform = eigvecs[:, perm[
    96:128]]  # eigenvectors for the 64 last eigenvalues

# transform sample with PCA (note that numpy imposes line-vectors,
# so we right-multiply the vectors)
sample = np.dot(sample, pca_transform)

# train GMM
print "start train GMM ......."
# gmm = ynumpy.gmm_learn(sample, k, nt = 400, niter = 2000, seed = 0, redo = 1, use_weights = True)  # original
gmm = ynumpy.gmm_learn(sample,
                       k,
                       nt=100,
                       niter=1500,
                       seed=0,
                       redo=1,
                       use_weights=True)  # fixes memory issues

np.save("./opencv_models/weight.gmm", gmm[0])
np.save("./opencv_models/mu.gmm", gmm[1])
np.save("./opencv_models/sigma.gmm", gmm[2])
np.save("./opencv_models/mean.gmm", mean)
np.save("./opencv_models/pca_transform.gmm", pca_transform)
コード例 #24
0
def save_model(sample, type_feature, RESULT_PATH, TYPE):
    '''Save GMM models, mean, pca_transform of sample data

	Parameters
	----------
	sample: np.ndarray type,
		Sample data 
	type_feature: {'traj', 'hog', 'hof', 'mbh'}
		One of four types of feature
	RESULT_PATH: string
		Path of result files
	TYPE: {'video', 'sensor'}
		Type of data

	Returns
	-------
	gmm: gmm model
	mean: mean value of data
	pca_transform: pca transform of data
	'''
    # GMM part
    k = 25
    if type_feature == 'traj':
        DIM_AFTER_PCA = 0.5 * 30
    elif type_feature == 'hog':
        DIM_AFTER_PCA = 0.5 * 96
    elif type_feature == 'hof':
        DIM_AFTER_PCA = 0.5 * 108
    elif type_feature == 'mbh':
        DIM_AFTER_PCA = 0.5 * 192

    # compute mean and covariance matrix for the PCA
    mean = sample.mean(axis=0)
    sample = sample - mean
    cov = np.dot(sample.T, sample)
    # compute PCA matrix and keep only 64 dimensions
    eigvals, eigvecs = np.linalg.eig(cov)
    # sort by increasing eigenvalue
    perm = eigvals.argsort()
    # eigenvectors for the 64 last eigenvalues
    pca_transform = eigvecs[:, perm[-DIM_AFTER_PCA:]]
    # transform sample with PCA (note that numpy imposes line-vectors,
    # so we right-multiply the vectors)
    sample = np.dot(sample, pca_transform)
    # train GMM
    gmm = ynumpy.gmm_learn(sample, k)
    # save mean value to file
    file_mean = open(
        RESULT_PATH + 'mean_' + type_feature + '_' + TYPE + '.pkl', 'wb')
    pickle.dump(mean, file_mean)
    file_mean.close()
    # save pca transform to file
    file_pca = open(RESULT_PATH + 'pca_' + type_feature + '_' + TYPE + '.pkl',
                    'wb')
    pickle.dump(pca_transform, file_pca)
    file_pca.close()
    # save GMM model to file
    file_gmm = open(RESULT_PATH + 'gmm_' + type_feature + '_' + TYPE + '.pkl',
                    'wb')
    pickle.dump(gmm, file_gmm)
    file_gmm.close()

    return gmm, mean, pca_transform
コード例 #25
0
ファイル: test_ynumpy.py プロジェクト: GarfieldEr007/yael
    print "vectors = "
    print v
    print "meta info = "
    print meta


print "kmeans:"

centroids = ynumpy.kmeans(v, 3)

print "result centroids ="
print centroids[:10,:]

print "gmm:"

gmm = ynumpy.gmm_learn(v, 3)

(w, mu, sigma) = gmm

print "mu = "
print mu

print "sigma = "
print sigma


muc = numpy.vstack((mu[0, :],
                    mu[0, :])); 
                    
#                    mu[1, :],
#                    mu[1, :],
コード例 #26
0
def train_gmm(trajectories_path,
              videofiles,
              traintest_inds,
              feat_types,
              fvtrees_path,
              nt=8,
              verbose=False):
    for p, part in enumerate(traintest_inds):
        D_all = None
        for j, feat_t in enumerate(feat_types):
            gmm_filepath = join(fvtrees_path, feat_t + '-' + str(p),
                                'gmm-' + str(p) + '.pkl')
            if not isfile(gmm_filepath):
                if D_all is None:
                    sample_filepath = join(fvtrees_path, feat_t + '-' + str(p),
                                           'sample-' + str(p) + '.h5')
                    try:
                        with h5py.File(sample_filepath, 'r') as h5f_handle:
                            D = h5f_handle.get('data').value
                    except IOError:
                        trajs = load_tracklets_sample(trajectories_path,
                                                      videofiles,
                                                      part[p],
                                                      1000000,
                                                      verbose=verbose)
                        with h5py.File(sample_filepath, 'w') as h5f_handle:
                            h5f_handle.create_dataset('data', data=trajs)
                    D_all = split_trajectory_features(trajs)

                start_time = time.time()
                # when loaded all features, choose feat_t ones
                D = D_all[feat_t]

                # apply preprocessing
                if feat_t == 'trj':
                    D = convert_positions_to_displacements(D)

                if feat_t != 'mbh':
                    D = preprocessing.normalize(D, norm='l1', axis=1)
                else:
                    Dx = preprocessing.normalize(D[:, :D.shape[1] / 2],
                                                 norm='l1',
                                                 axis=1)
                    Dy = preprocessing.normalize(D[:, D.shape[1] / 2:],
                                                 norm='l1',
                                                 axis=1)
                    D = np.hstack((Dx, Dy))

                if feat_t != 'trj':
                    D = np.sign(D) * np.sqrt(np.abs(D))  # rootsift

                # reduce dimensionality to half (PCA)
                pca = PCA(n_components=int(0.5 * D.shape[1]), copy=True)
                pca.fit(D)

                # train GMMs for later FV computation
                D = np.ascontiguousarray(pca.transform(D), dtype=np.float32)
                gmm = ynumpy.gmm_learn(D, 256, nt=nt, niter=100, redo=1)

                with open(gmm_filepath, 'wb') as f:
                    cPickle.dump(dict(gmm=gmm, pca=pca), f)

                elapsed_time = time.time() - start_time
                if verbose:
                    print('[train_fv_gmms] %s -> DONE (in %.2f secs)' %
                          (feat_t, elapsed_time))
コード例 #27
0
ファイル: gmm.py プロジェクト: db7894/cnn-cbir-benchmark
# compute mean and covariance matrix for the PCA
mean = sample.mean(axis=0)
sample = sample - mean
cov = np.dot(sample.T, sample)

# compute PCA matrix and keep only 64 dimensions
eigvals, eigvecs = np.linalg.eig(cov)
perm = eigvals.argsort()  # sort by increasing eigenvalue
pca_transform = eigvecs[:, perm[
    96:128]]  # eigenvectors for the 64 last eigenvalues

# transform sample with PCA (note that numpy imposes line-vectors,
# so we right-multiply the vectors)
sample = np.dot(sample, pca_transform)

# train GMM
print "start train GMM ......."
gmm = ynumpy.gmm_learn(sample,
                       k,
                       nt=400,
                       niter=2000,
                       seed=0,
                       redo=1,
                       use_weights=True)

np.save("./opencv_models/weight.gmm", gmm[0])
np.save("./opencv_models/mu.gmm", gmm[1])
np.save("./opencv_models/sigma.gmm", gmm[2])
np.save("./opencv_models/mean.gmm", mean)
np.save("./opencv_models/pca_transform.gmm", pca_transform)
コード例 #28
0
dat = np.load('test/py/test_dat.npy')

cluster_num = len(set(label))
print cluster_num

cluster_w = np.array([(i + 2) % 5 != 0 for i in xrange(cluster_num)])

obs = np.vstack([np.vstack([dat[label == c]] * cluster_w[c]) for c in xrange(cluster_num) if cluster_w[c] != 0])

obs_w = dat.copy()
weight = cluster_w[label]

print obs_w.shape
print weight.shape

ret_original = ynumpy.gmm_learn(obs.astype(np.float32), cluster_num - 1)
ret_modified = ynumpy.gmm_learn_sw(obs_w.astype(np.float32), weight.astype(np.float32), cluster_num - 1)

print ''
print '======= result ======'
print ''

print ret_original
print ret_modified

orig_w, orig_mu, orig_sigma = ret_original
modi_w, modi_mu, modi_sigma = ret_modified

orig_i = np.argsort(orig_w)
modi_i = np.argsort(modi_w)