本文整理汇总了Python中sklearn.decomposition.MiniBatchDictionaryLearning类的典型用法代码示例。如果您正苦于以下问题:Python MiniBatchDictionaryLearning类的具体用法?Python MiniBatchDictionaryLearning怎么用?Python MiniBatchDictionaryLearning使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MiniBatchDictionaryLearning类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dictionay_learning_MHOF_online
def dictionay_learning_MHOF_online(training_samples_num=400):
from MHOF_Extraction import MHOF_Extraction
from MHOF_histogram_block import MHOF_histogram_block
from sklearn.decomposition import MiniBatchDictionaryLearning
import numpy as np
import cv2
import video
cam=video.create_capture('Crowd-Activity-All.avi')
height_block_num=4
width_block_num=5
bin_num=16
ret,prev=cam.read()
ret,img=cam.read()
flow_H=MHOF_Extraction(prev,img)
flow_hist_H=MHOF_histogram_block(flow_H,height_block_num,width_block_num,bin_num)
flow_hist_H=np.reshape(flow_hist_H,[1,flow_hist_H.size])
# error!!!!
dico=MiniBatchDictionaryLearning(1,alpha=1,n_iter=500)
dic=dico.fit(flow_hist_H).components_
for i in range(training_samples_num):
ret,img=cam.read()
flow_H=MHOF_Extraction(prev,img)
flow_hist_H=MHOF_histogram_block(flow_H,height_block_num,width_block_num,bin_num)
dico=MiniBatchDictionaryLearing(i+1,alpha=1,n_iter=500,dict_init=dic)
dic=dico.fit(flow_hist_H).components
return dic
示例2: test_dict_learning_online_positivity
def test_dict_learning_online_positivity(transform_algorithm,
positive_code,
positive_dict):
rng = np.random.RandomState(0)
n_components = 8
dico = MiniBatchDictionaryLearning(
n_components, transform_algorithm=transform_algorithm, random_state=0,
positive_code=positive_code, positive_dict=positive_dict).fit(X)
code = dico.transform(X)
if positive_dict:
assert_true((dico.components_ >= 0).all())
else:
assert_true((dico.components_ < 0).any())
if positive_code:
assert_true((code >= 0).all())
else:
assert_true((code < 0).any())
code, dictionary = dict_learning_online(X, n_components=n_components,
alpha=1, random_state=rng,
positive_dict=positive_dict,
positive_code=positive_code)
if positive_dict:
assert_true((dictionary >= 0).all())
else:
assert_true((dictionary < 0).any())
if positive_code:
assert_true((code >= 0).all())
else:
assert_true((code < 0).any())
示例3: main
def main(games_path = None):
if games_path == None:
games_path = 'specmine/data/go_games/2010-01.pickle.gz'
with specmine.util.openz(games_path) as games_file:
games = pickle.load(games_file)
boards = None # numpy array nx9x9
for game in games:
if boards == None:
boards = games[game].grids
else:
boards = numpy.vstack((boards,games[game].grids))
print 'boards shape: ', boards.shape
boards = boards.reshape((boards.shape[0],-1))
print 'boards reshaped: ', boards.shape
print 'Learning the dictionary... '
t0 = time()
dico = MiniBatchDictionaryLearning(n_atoms=100, alpha=1, n_iter=500)
V = dico.fit(boards).components_
dt = time() - t0
print 'done in %.2fs.' % dt
#pl.figure(figsize=(4.2, 4))
for i, comp in enumerate(V[:100]):
pl.subplot(10, 10, i + 1)
pl.imshow(comp, cmap=pl.cm.gray_r) # interpolation='nearest')
pl.xticks(())
pl.yticks(())
示例4: scskl_dico_learning
def scskl_dico_learning(list_pickled_array,n_atoms,maxepoch=5,maxiter=100):
D = None
for e in range(maxepoch):
for a in list_pickled_array:
data = joblib.load(a)
dico = MiniBatchDictionaryLearning(n_components=n_atoms, n_iter=maxiter, dict_init=D)
D = dico.fit(data).components_.astype(np.float32)
return D
示例5: sklearn_check
def sklearn_check(img, patch_size, dic_size, T=1000):
patch_shape = (patch_size, patch_size)
patches = extract_patches_2d(img, patch_shape)
patches = patches.reshape(patches.shape[0], -1)
patches = center(patches)
dl = MiniBatchDictionaryLearning(dic_size, n_iter=T)
dl.fit(patches)
D = dl.components_.T
return D
示例6: to_sparse
def to_sparse(X,dim):
sparse_dict = MiniBatchDictionaryLearning(dim)
sparse_dict.fit(X)
sparse_vectors = sparse_encode(X, sparse_dict.components_)
for i in sparse_vectors:
print i
return sparse_vectors
示例7: create_dictionaries
def create_dictionaries(n_codewords=20):
dataset_features = np.load('MSR_Features_hog-hof-skel1360423760.27.dat')
hogs = []
hofs = []
skels = []
for n in dataset_features.keys():
hogs += dataset_features[n]['hog']
hofs += dataset_features[n]['hof']
skels += [normalize_skeleton(dataset_features[n]['skel_world'])]
''' Input should be features[n_samples, n_features] '''
hogs = np.vstack(hogs)
hofs = np.vstack(hofs)
skels = np.vstack(skels)
hog_dict = MiniBatchDictionaryLearning(n_codewords, n_jobs=-1, verbose=True, transform_algorithm='lasso_lars')
hog_dict.fit(hogs)
hof_dict = MiniBatchDictionaryLearning(n_codewords, n_jobs=-1, verbose=True, transform_algorithm='lasso_lars')
hof_dict.fit(hofs)
skels_dict = MiniBatchDictionaryLearning(n_codewords, n_jobs=-1, verbose=True, transform_algorithm='lasso_lars')
skels_dict.fit(skels)
feature_dictionaries = {'hog':hog_dict, 'hof':hof_dict, 'skel':skels_dict}
with open('MSR_Dictionaries_hog-hof-skel_%f.dat'%time.time(), 'wb') as outfile:
pickle.dump(feature_dictionaries, outfile, protocol=pickle.HIGHEST_PROTOCOL)
示例8: BOW_sparsecoding
class BOW_sparsecoding(BOW):
def codebook(self):
self.mbdl = MiniBatchDictionaryLearning(self.N_codebook)
self.mbdl.fit(self.raw_features)
def bow_feature_extract(self, path):
des = self.raw_feature_extract(path)
out = sum(sparse_encode(des, self.mbdl.components_))
out = np.array([out])
return out
示例9: buildmodel2
def buildmodel2():
"生成有眼镜-无眼镜pair模型"
modelrec = np.load('cut_rec.npy')
modelglass = np.load('glassline.npy')[:modelrec.shape[0]]
linkedmodel = np.empty((modelrec.shape[0],modelrec.shape[1]+modelglass.shape[1]),'f')
linkedmodel[:,:modelrec.shape[1]]=modelrec
linkedmodel[:,modelrec.shape[1]:]=modelglass
#Train
from sklearn.decomposition import MiniBatchDictionaryLearning
learning = MiniBatchDictionaryLearning(500,verbose=True)
learning.fit(linkedmodel)
import cPickle
cPickle.dump(learning,file('sparselinked','wb'),-1)
示例10: learning_sparse_coding
def learning_sparse_coding(X, components=None):
"""
http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.DictionaryLearning.html
http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.sparse_encode.html
"""
if components is None:
print('Learning the dictionary...')
t0 = time()
diclearner = MiniBatchDictionaryLearning(n_components=100, verbose=True)
components = diclearner.fit(X).components_
np.savetxt('components_of_convfeat.txt', components)
dt = time() - t0
print('done in %.2fs.' % dt)
codes = sparse_encode(X, components)
np.savetxt('sparse_codes_of_convfeat.txt', codes)
示例11: extract_codes
def extract_codes(self, X, standardize=False):
self.standardize=standardize
self._extract_data_patches(X)
self.dico = MiniBatchDictionaryLearning(n_components=self.n_components, alpha=1, n_iter=500)
print "Dictionary learning from data..."
self.D = self.dico.fit(self.data)
return self
示例12: fit
def fit(self, X, y=None):
# compute the codes
print 'Extracting patchs...'
patchs = []
num = self.patch_num // X.size
for x in X:
img = imread(str(x[0]))
tmp = extract_patches_2d(img, (self.patch_size,self.patch_size), \
max_patches=num, random_state=np.random.RandomState())
patchs.append(tmp)
data = np.vstack(patchs)
data = data.reshape(data.shape[0], -1)
data -= np.mean(data, axis=0)
data = data/np.std(data, axis=0)
print 'Learning codebook...'
if self.method == 'sc':
self.dico = MiniBatchDictionaryLearning(n_components=self.codebook_size, \
alpha=1, n_iter=100, batch_size =100, verbose=True)
self.dico.fit(data)
elif self.method=='km':
# self.dico = MiniBatchKMeans(n_clusters=self.codebook_size)
pass
return self
示例13: test_dict_learning_online_partial_fit
def test_dict_learning_online_partial_fit():
# this test was not actually passing before!
raise SkipTest
n_components = 12
rng = np.random.RandomState(0)
V = rng.randn(n_components, n_features) # random init
V /= np.sum(V ** 2, axis=1)[:, np.newaxis]
dico1 = MiniBatchDictionaryLearning(n_components, n_iter=10, batch_size=1,
shuffle=False, dict_init=V,
random_state=0).fit(X)
dico2 = MiniBatchDictionaryLearning(n_components, n_iter=1, dict_init=V,
random_state=0)
for ii, sample in enumerate(X):
dico2.partial_fit(sample, iter_offset=ii * dico2.n_iter)
# if ii == 1: break
assert_true(not np.all(sparse_encode(X, dico1.components_, alpha=100) ==
0))
assert_array_equal(dico1.components_, dico2.components_)
示例14: train_sparse_coding
def train_sparse_coding(feature_list, patch_list, dict_size=256, transform_alpha=0.5, n_iter=50):
"""
使用mini batch训练稀疏编码
#feature_list 表示要训练的特征的列表
#patch_list 表示结果patch的列表
:return sc_list
"""
sc_list = []
i = 0
for feature, patch in zip(feature_list, patch_list):
i = i + 1
'''
由于组合数值大小比例的问题,稀疏编码可能忽略较小的特征,下面的×10需要用别的特征归一化方法代替
相关性越大,则每个向量都是有用的,所以需要更长的时间进行训练。
'''
dico = None
X = np.concatenate((feature, patch), axis=1)
if len(X) > 100000:
np.random.shuffle(X)
X = X[:90000]
if len(X) < 5000:
print "进入DictionaryLearning状态"
dico = MiniBatchDictionaryLearning(batch_size=1000, transform_algorithm='lasso_lars', fit_algorithm='lars',
transform_n_nonzero_coefs=5, n_components=len(X)/50,
dict_init=X[:len(X)/50],
n_iter=n_iter, transform_alpha=transform_alpha, verbose=10, n_jobs=-1)
else:
print "进入MiniBatchDictionaryLearning状态"
dico = MiniBatchDictionaryLearning(batch_size=1000, transform_algorithm='lasso_lars', fit_algorithm='lars',
transform_n_nonzero_coefs=5, n_components=len(X)/50,
dict_init=X[:len(X)/50],
n_iter=n_iter, transform_alpha=transform_alpha, verbose=10, n_jobs=-1)
V = dico.fit(X).components_
sc_list.append(V)
file_name = "./tmp_file/_tmp_sc_list_new_clsd_raw_%d.pickle" % (i)
sc_file = open(file_name, 'wb')
cPickle.dump(sc_list, sc_file, 1)
sc_file.close()
return sc_list
示例15: test_dict_learning_online_partial_fit
def test_dict_learning_online_partial_fit():
n_components = 12
rng = np.random.RandomState(0)
V = rng.randn(n_components, n_features) # random init
V /= np.sum(V ** 2, axis=1)[:, np.newaxis]
dict1 = MiniBatchDictionaryLearning(n_components, n_iter=10 * len(X),
batch_size=1,
alpha=1, shuffle=False, dict_init=V,
random_state=0).fit(X)
dict2 = MiniBatchDictionaryLearning(n_components, alpha=1,
n_iter=1, dict_init=V,
random_state=0)
for i in range(10):
for sample in X:
dict2.partial_fit(sample[np.newaxis, :])
assert not np.all(sparse_encode(X, dict1.components_, alpha=1) == 0)
assert_array_almost_equal(dict1.components_, dict2.components_,
decimal=2)