Ejemplo n.º 1
0
def vl_test_ikmeans():
    # VL_TEST_IKMEANS Test VL_IKMEANS function
    print "test_ikmeans: Testing VL_IKMEANS and IKMEANSPUSH"

    # -----------------------------------------------------------------------
    print "test_ikmeans: Testing Lloyd algorithm"

    K = 5
    data = numpy.array(numpy.random.rand(2, 1000) * 255, "uint8")
    datat = numpy.array(numpy.random.rand(2, 10000) * 255, "uint8")

    [C, A] = vlfeat.vl_ikmeans(data, K, verbose=1)
    AT = vlfeat.vl_ikmeanspush(datat, C, verbose=1)
    plot_partition(data, datat, C, A, AT)
    pylab.title("vl_ikmeans (Lloyd algorithm)")
    pylab.xlim(0, 255)
    pylab.ylim(0, 255)
    print "ikmeans_lloyd"

    pylab.figure()
    [C, A] = vlfeat.vl_ikmeans(data, K, verbose=1, method="elkan")
    AT = vlfeat.vl_ikmeanspush(datat, C, verbose=1, method="elkan")
    pylab.title("vl_ikmeans (Elkan algorithm)")
    plot_partition(data, datat, C, A, AT)
    pylab.xlim(0, 255)
    pylab.ylim(0, 255)
    print "ikmeans_elkan"

    pylab.show()
Ejemplo n.º 2
0
def vl_test_ikmeans():
	# VL_TEST_IKMEANS Test VL_IKMEANS function
	print ('test_ikmeans: Testing VL_IKMEANS and IKMEANSPUSH')

	# -----------------------------------------------------------------------
	print ('test_ikmeans: Testing Lloyd algorithm')
	
	K       = 5
	data    = numpy.array(numpy.random.rand(2,1000) * 255, 'uint8')
	datat   = numpy.array(numpy.random.rand(2,10000)* 255, 'uint8')
	
	[C, A] = vlfeat.vl_ikmeans(data, K, verbose=1)
	AT = vlfeat.vl_ikmeanspush(datat, C, verbose=1)
	plot_partition(data, datat, C, A, AT) 
	pylab.title('vl_ikmeans (Lloyd algorithm)')
	pylab.xlim(0, 255)
	pylab.ylim(0, 255)
	print ('ikmeans_lloyd')
	
	pylab.figure()
	[C, A] = vlfeat.vl_ikmeans(data, K, verbose=1, method='elkan')
	AT = vlfeat.vl_ikmeanspush(datat, C, verbose=1, method='elkan')
	pylab.title('vl_ikmeans (Elkan algorithm)')
	plot_partition(data, datat, C, A, AT) 
	pylab.xlim(0, 255)
	pylab.ylim(0, 255)
	print ('ikmeans_elkan')
	
	pylab.show()	
Ejemplo n.º 3
0
def trainVocab(selTrain, all_images, conf):
    selTrainFeats = sample(selTrain, conf.images_for_histogram)
    descrs = []
    if MULTIPROCESSING:
        raise ValueError('MULTIPROCESSING not implemented')
        #pool = Pool(processes=30)
        #list_of_train_images = [all_images[i] for i in selTrainFeats]
        #descrs.append(pool.map_async(getPhowFeatures, list_of_train_images).get())
    else:
        for i in selTrainFeats:
            im = imread(all_images[i])
            # Debugging
            #print all_images[i], 'shape', im.shape
            descrs.append(getPhowFeatures(im, conf.phowOpts)[1])
            # the '[1]' is there because we only want the descriptors and not the frames

    descrs = hstack(descrs)
    n_features = descrs.shape[1]
    sample_indices = sample(arange(n_features),
                            conf.numbers_of_features_for_histogram)
    descrs = descrs[:, sample_indices]
    descrs = array(descrs, 'uint8')

    # Quantize the descriptors to get the visual words
    vocab, _ = vl_ikmeans(descrs,
                          K=conf.numWords,
                          verbose=conf.verbose,
                          method='elkan')
    return vocab
Ejemplo n.º 4
0
def trainVocab(selTrain, all_images, conf):
    selTrainFeats = sample(selTrain, conf.images_for_histogram)
    descrs = []
    if MULTIPROCESSING:
        raise ValueError('MULTIPROCESSING not implemented')
        #pool = Pool(processes=30)  
        #list_of_train_images = [all_images[i] for i in selTrainFeats]
        #descrs.append(pool.map_async(getPhowFeatures, list_of_train_images).get())        
    else:
        for i in selTrainFeats:
            im = imread(all_images[i])
            descrs.append(getPhowFeatures(im, conf.phowOpts)[1])
            # the '[1]' is there because we only want the descriptors and not the frames
    
    descrs = hstack(descrs)
    n_features = descrs.shape[1]
    sample_indices = sample(arange(n_features), conf.numbers_of_features_for_histogram)
    descrs = descrs[:, sample_indices]
    descrs = array(descrs, 'uint8')
    
    # Quantize the descriptors to get the visual words
    vocab, _ = vl_ikmeans(descrs,
                          K=conf.numWords,
                          verbose=conf.verbose,
                          method='elkan')
    return vocab
Ejemplo n.º 5
0
def trainVocab(selTrain, all_images, conf):
	selTrainFeats = sample(selTrain, conf.images_for_histogram)
	descrs = []
	#start multiprocessing block
	pool = multiprocessing.Pool(processes=conf.numCore)
	results = [pool.apply_async(getPhowFeaturesMulti, args=(imread(all_images[i]), conf.phowOpts, i)) for i in selTrainFeats]
	descrs = [p.get() for p in results]
	sorted(descrs)
	for descr in descrs:
		descr.pop(0)
	depict = []
	for descr in descrs:
		depict.append(descr[0])
	descrs = depict
	#end multiprocessing block
	descrs = hstack(descrs)
	n_features = descrs.shape[1]
	sample_indices = sample(arange(n_features), conf.numbers_of_features_for_histogram)
	descrs = descrs[:, sample_indices]
	descrs = array(descrs, 'uint8')
	
	# Quantize the descriptors to get the visual words
	vocab, _ = vl_ikmeans(descrs,
						  K=conf.numWords,
						  verbose=conf.verbose,
						  method='elkan')
	return vocab
Ejemplo n.º 6
0
def trainVocab(selTrain, all_images, conf):
	selTrainFeats = sample(selTrain, conf.images_for_histogram)
	descrs = []
	#start multiprocessing block
	pool = multiprocessing.Pool(processes=conf.numCore)
	results = [pool.apply_async(getPhowFeaturesMulti, args=(imread(all_images[ii]), conf.phowOpts, i)) for i, ii in enumerate(selTrainFeats)]
	descrs = [p.get() for p in results]
	pool.terminate()
	sorted(descrs)
	for descr in descrs:
		descr.pop(0)
	depict = []
	for descr in descrs:
		depict.append(descr[0])
	descrs = depict
	#end multiprocessing block
	descrs = hstack(descrs)
	n_features = descrs.shape[1]
	sample_indices = sample(arange(n_features), conf.numbers_of_features_for_histogram)
	descrs = descrs[:, sample_indices]
	descrs = array(descrs, 'uint8')
	
	# Quantize the descriptors to get the visual words
	vocab, _ = vl_ikmeans(descrs,
						  K=conf.numWords,
						  verbose=conf.verbose,
						  method='elkan')
	return vocab
Ejemplo n.º 7
0
def trainVocab(selTrain, all_images, conf):
	selTrainFeats = sample(selTrain, conf.images_for_histogram)
	descrs = []
	for i in selTrainFeats:
		im = imread(all_images[i])
		descrs.append(getPhowFeatures(im, conf.phowOpts)[1])
	# the '[1]' is there because we only want the descriptors and not the frames
    
	descrs = hstack(descrs)
	n_features = descrs.shape[1]
	sample_indices = sample(arange(n_features), conf.numbers_of_features_for_histogram)
	descrs = descrs[:, sample_indices]
	descrs = array(descrs, 'uint8')
    
    # Quantize the descriptors to get the visual words
	vocab, _ = vl_ikmeans(descrs,
                          K=conf.numWords,
                          verbose=conf.verbose,
                          method='elkan')
	return vocab
Ejemplo n.º 8
0
def learn(bk, descr):
    if   bk['dictionary'] == 'ikm':
        print('block_dictionary: === Running IKM ===')
        print('block_dictionary: num words (K): %d'%(bk['ikm_nwords']))
        dict_ = vlfeat.vl_ikmeans(descr,
                                  bk['ikm_nwords'],
                                  verbosity=1,
                                  method='elkan')
        print('block_dictionary: IKM done')
    elif bk['dictionary'] == 'hikm':
        print('block_dictionary: === Runnining HIKM ===')
        print('block_dictionary: num leaves:      %d'%(bk['hikm_nleaves']))
        print('block_dictionary: branching (K):   %d'%(bk['hikm_K']))
        print('block_dictionary: only_leaves:     %d'%(bk['hikm_only_leaves']))
        dict_,asgn = vlfeat.vl_hikmeans(descr,
                                        bk['hikm_K'],
                                        bk['hikm_nleaves'],
                                        verbosity=1 ,
                                        method='elkan')
        print('block_dictionary: HIKM done')
    else:
        raise TypeError('block_dictionary.py :learn() unknown dictionary type')

    return dict_
 def cluster(self):
     self.vector = self.vector.transpose()
     centers, _ = vl_ikmeans(self.vector, self.clusters_count,method='elkan', verbose=0)
     return centers.transpose()