Example #1
0
def test_initialize_with_small_n_features():
    N = 500
    n_components = 128
    n_features = 64
    X = np.random.randn(N, n_features)
    dico = ApproximateKSVD(n_components=n_components)
    dico.fit(X)
def get_decomp(input_image):
    patches = image.extract_patches_2d(input_image, (16,16))
    signals = patches.reshape(patches.shape[0], -1)
    aksvd = ApproximateKSVD(n_components=64, max_iter = 100, transform_n_nonzero_coefs=4)
    dictionary = aksvd.fit(signals[np.random.choice(range(signals.shape[0]), 1000, replace = True)]).components_
    gamma = aksvd.transform(signals)
    return gamma, dictionary
def getDict(img_chunk):
    patches = image.extract_patches_2d(img_chunk, (16,16))
    signals = patches.reshape(patches.shape[0], -1)
    
    # Set K-SVD paprameters to maximize classification rate according to fannjiang
    aksvd = ApproximateKSVD(n_components=64, max_iter = 100, transform_n_nonzero_coefs=4)
    dictionary = aksvd.fit(signals[np.random.choice(range(signals.shape[0]), 1000, replace = True)]).components_
    #gamma = aksvd.transform(signals) 
    return dictionary
Example #4
0
def test_fit():
    np.random.seed(0)
    N = 1000
    L = 256
    X = np.random.randn(N, 20) + np.random.rand(N, 20)
    dico = ApproximateKSVD(n_components=L)
    dico.fit(X)
    gamma = dico.transform(X)
    assert_array_almost_equal(X, gamma.dot(dico.components_))
Example #5
0
def getDict(signals, n):
    # Set K-SVD paprameters to maximize classification rate according to fannjiang
    print(n)
    aksvd = ApproximateKSVD(n_components=64,
                            max_iter=50,
                            transform_n_nonzero_coefs=4)
    dictionary = aksvd.fit(signals[np.random.choice(range(signals.shape[0]),
                                                    int(round(n)),
                                                    replace=True)]).components_
    #gamma = aksvd.transform(signals)
    return dictionary
Example #6
0
class KSVD(ClusteringAlgo):
    def __init__(self, *args, **kwargs):
        self._aksvd = ApproximateKSVD(*args, **kwargs)

    def cluster(self, features):
        self._aksvd.fit(features)
        gamma = self._aksvd.transform(features)
        assignments = np.argmax(np.abs(gamma), axis=1)
        return assignments

    def __str__(self):
        return "ksvd"
Example #7
0
def test_fit():
    np.random.seed(0)
    N = 1000
    L = 64
    n_features = 128
    B = np.array(sp.sparse.random(N, L, density=0.5).todense())
    D = np.random.randn(L, n_features)
    X = np.dot(B, D)
    dico = ApproximateKSVD(n_components=L, transform_n_nonzero_coefs=L)
    dico.fit(X)
    gamma = dico.transform(X)
    assert_array_almost_equal(X, gamma.dot(dico.components_))
Example #8
0
def class_dict_coder(X, y, n_class_atoms=None, n_class=None):
    # For each class do ksvd using library
    D = np.zeros((X.shape[0], n_class_atoms * 4))

    for i in range(n_class):
        #print("Doing dictionary initialization for ", i)
        X1 = X[:, y == i]
        aksvd = ApproximateKSVD(n_components=n_class_atoms)
        dictionary = aksvd.fit(np.transpose(X1)).components_
        D[:,
          i * n_class_atoms:(i + 1) * n_class_atoms] = np.transpose(dictionary)
        #print("Done dictionary initialization for ", i)
    return D
Example #9
0
def dictionary_KSVD(num_clusters, word_vectors):
    # Initalize a ksvd object and use it for clustering.
    aksvd = ApproximateKSVD(n_components=num_clusters)
    dictionary = aksvd.fit(word_vectors).components_
    idx_proba = aksvd.transform(word_vectors)
    idx = np.argmax(idx_proba, axis=1)
    print "Clustering Done...", time.time() - start, "seconds"
    # Get probabilities of cluster assignments.
    # Dump cluster assignments and probability of cluster assignments.
    joblib.dump(idx, 'ksvd_latestclusmodel_len2alldata.pkl')
    print "Cluster Assignments Saved..."

    joblib.dump(idx_proba, 'ksvd_prob_latestclusmodel_len2alldata.pkl')
    print "Probabilities of Cluster Assignments Saved..."
    return (idx, idx_proba)
Example #10
0
def cluster_GMM(num_clusters, word_vectors, transform_n_nonzero_coefs=None):
    # Initalize a GMM object and use it for clustering.
    # clf =  GMM(n_components=num_clusters,covariance_type="tied", init_params='wc', n_iter=10)
    aksvd = ApproximateKSVD(
        n_components=num_clusters,
        transform_n_nonzero_coefs=transform_n_nonzero_coefs)
    dictionary = aksvd.fit(word_vectors).components_
    idx_proba = aksvd.transform(word_vectors)
    idx = np.argmax(idx_proba, axis=1)
    print("Clustering Done...", time.time() - start, "seconds")
    joblib.dump(idx, 'data_files/gmm_latestclusmodel_len2alldata.pkl')
    print("Cluster Assignments Saved...")
    joblib.dump(idx_proba,
                'data_files/gmm_prob_latestclusmodel_len2alldata.pkl')
    print("Probabilities of Cluster Assignments Saved...")
    return (idx, idx_proba)
Example #11
0
def test_size():
    np.random.seed(0)
    N = 100
    L = 128
    X = np.random.randn(N, 10) + np.random.rand(N, 10)
    dico1 = ApproximateKSVD(n_components=L)
    dico1.fit(X)
    gamma1 = dico1.transform(X)
    e1 = norm(X - gamma1.dot(dico1.components_))

    dico2 = DictionaryLearning(n_components=L)
    dico2.fit(X)
    gamma2 = dico2.transform(X)
    e2 = norm(X - gamma2.dot(dico2.components_))

    assert dico1.components_.shape == dico2.components_.shape
    assert gamma1.shape == gamma2.shape
    assert e1 < e2
Example #12
0
    def ksvd(self):
        """K-SVD denoising algorithm"""
        P = extract_patches_2d(self.Inoisy, self.ksvd_patch)
        patch_shape = P.shape
        P = P.reshape((patch_shape[0], -1))
        mean = np.mean(P, axis=1)[:, np.newaxis]
        P -= mean

        aksvd = ApproximateKSVD(n_components=self.ksvd_components)
        dico = aksvd.fit(P).components_
        reduced = (aksvd.transform(P)).dot(dico) + mean
        reduced_img = reconstruct_from_patches_2d(reduced.reshape(patch_shape),
                                                  self.shape)

        self.Iksvd = np.clip(reduced_img, 0, 1)
        self.Ilist[self.str2int['ksvd']] = self.Iksvd
        if self.verbose:
            print('K-SVD :', self.Iksvd)
        return ()
Example #13
0
def dictionary_KSVD(num_clusters, word_vectors, basic_path: str):
    # Initalize a ksvd object and use it for clustering.
    aksvd = ApproximateKSVD(n_components=num_clusters)
    dictionary = aksvd.fit(word_vectors).components_
    idx_proba = aksvd.transform(word_vectors)
    idx = np.argmax(idx_proba, axis=1)
    # print("Clustering Done...", time.time() - start, "seconds")
    # Get probabilities of cluster assignments.

    # Dump cluster assignments and probability of cluster assignments.
    joblib.dump(
        idx,
        os.path.join(config["system_storage"]["models"], f'ksvd_{basic_path}'))
    print("Cluster Assignments Saved...")

    joblib.dump(
        idx_proba,
        os.path.join(config["system_storage"]["models"],
                     f'ksvd_prob_{basic_path}'))
    print("Probabilities of Cluster Assignments Saved...")
    return (idx, idx_proba)
Example #14
0
def test_size():
    np.random.seed(0)
    N = 50
    L = 12
    n_features = 16
    D = np.random.randn(L, n_features)
    B = np.array(sp.sparse.random(N, L, density=0.5).todense())
    X = np.dot(B, D)
    dico1 = ApproximateKSVD(n_components=L, transform_n_nonzero_coefs=L)
    dico1.fit(X)
    gamma1 = dico1.transform(X)
    e1 = norm(X - gamma1.dot(dico1.components_))

    dico2 = DictionaryLearning(n_components=L, transform_n_nonzero_coefs=L)
    dico2.fit(X)
    gamma2 = dico2.transform(X)
    e2 = norm(X - gamma2.dot(dico2.components_))

    assert dico1.components_.shape == dico2.components_.shape
    assert gamma1.shape == gamma2.shape
    assert e1 < e2
Example #15
0
def my_cross_val_score(CZ,
                       n_comp=3,
                       model='pca',
                       k=5,
                       random_state=False,
                       shuffle=True):
    if shuffle == True:
        save = np.copy(CZ)
        np.random.shuffle(CZ)
    kf = KFold(n_splits=k)
    cv = []
    pca = decomposition.PCA(n_components=n_comp, random_state=random_state)
    nmf = NMF(n_components=n_comp, random_state=random_state)
    kmeans = KMeans(n_clusters=n_comp, random_state=random_state)
    ksvd = ApproximateKSVD(n_components=n_comp)
    for train, test in kf.split(CZ):
        cz_test = CZ[test]
        cz_train = CZ[train]
        if model == 'pca':
            pca.fit(cz_train)
            CZ_reconstructed = pca.inverse_transform(pca.transform(cz_test))
        elif model == 'nmf':
            nmf.fit(cz_train)
            CZ_reconstructed = np.dot(nmf.transform(cz_test), nmf.components_)
        elif model == 'k_means':
            kmeans.fit(cz_train)
            CZ_reconstructed = kmeans.cluster_centers_[kmeans.predict(cz_test)]
        elif model == 'k_svd':
            meantr = np.mean(cz_train, axis=1)[:, np.newaxis]
            meantest = np.mean(cz_test, axis=1)[:, np.newaxis]
            dictionary = ksvd.fit(cz_train - meantr).components_
            gamma = ksvd.transform(cz_test - meantest)
            CZ_reconstructed = gamma.dot(dictionary) + meantest
        cv.append(mean_squared_error(CZ_reconstructed, cz_test))
    if shuffle == True:
        CZ = save
    return cv
Example #16
0
def researcher_ksvd(CLF, X_train, Y_train, X_test, Y_test, k=2, n_comp=[3,5,7,10], **kwargs):
    scores = []
    for n in n_comp:
        ksvd = ApproximateKSVD(n_components=n, transform_n_nonzero_coefs=max(n-k, 1))
        meantr = np.mean(X_train,axis=0)
        ksvd.fit(X_train - meantr).components_
        gamma_train = ksvd.transform(X_train - meantr)
        gamma_test = ksvd.transform(X_test - meantr)
        
        clf = CLF(**kwargs)
        clf.fit(gamma_train, Y_train)
        predict = clf.predict(gamma_test)
        score = accuracy_score(Y_test, predict)
        scores.append(score)
    return scores
Example #17
0
from skimage import io, util
from sklearn.feature_extraction import image
from sklearn import preprocessing
from ksvd import ApproximateKSVD

parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str, required=True)
parser.add_argument('--output', type=str, required=True)
args = parser.parse_args()


def clip(img):
    img = np.minimum(np.ones(img.shape), img)
    img = np.maximum(np.zeros(img.shape), img)
    return img


img = util.img_as_float(io.imread(args.input))
patch_size = (5, 5)
patches = image.extract_patches_2d(img, patch_size)
signals = patches.reshape(patches.shape[0], -1)
mean = np.mean(signals, axis=1)[:, np.newaxis]
signals -= mean
aksvd = ApproximateKSVD(n_components=32)
dictionary = aksvd.fit(signals[:10000]).components_
gamma = aksvd.transform(signals)
reduced = gamma.dot(dictionary) + mean
reduced_img = image.reconstruct_from_patches_2d(reduced.reshape(patches.shape),
                                                img.shape)
io.imsave(args.output, clip(reduced_img))
Example #18
0
 def __init__(self, *args, **kwargs):
     self._aksvd = ApproximateKSVD(*args, **kwargs)
Example #19
0
## Dictionary Learning:

print('Dictionary Learning ...')
R = np.random.randn(1728, 128)
n_components = 128 * 2  # Over complete factor = 2
transform_n_nonzero_coefs = 12

for Y, label in zip(Descr, weizmann_label_dic):

    # Y subtract mean ????????

    Y = np.dot(Y, R)  # Y: k x 128

    aksvd = ApproximateKSVD(
        n_components=n_components,
        transform_n_nonzero_coefs=transform_n_nonzero_coefs)
    D = aksvd.fit(Y).components_
    X = aksvd.transform(Y)

    print('YShape:', Y.shape)
    print('DShape:', D.shape)
    print('XShape:', X.shape)

    mat_dir = '../results/' + dataset_name + '_dic/'
    if not os.path.exists(mat_dir):
        os.makedirs(mat_dir)
    mat_name = mat_dir + label + '.mat'
    scipy.io.savemat(mat_name, {'dic': D, 'X': X})

    print('MAT:', mat_name, 'saved!')
Example #20
0
def ksvd(num_atoms):
    return ApproximateKSVD(num_atoms)
mart_dict = getDict(center20)
SW_dict = getDict(center26)
pear_dict = getDict(center773)
PW_dict = getDict(center911)

# make an array for looping later
dictionaries = [spher_dict, PS_dict, net_dict, mart_dict, SW_dict, pear_dict, PW_dict]

## Test images

spher_test_img = img_array2[0:steph, 0:stepv]


patches = image.extract_patches_2d(spher_test_img, (16,16))
signals = patches.reshape(patches.shape[0], -1)
aksvd = ApproximateKSVD(n_components=64, max_iter = 100, transform_n_nonzero_coefs=4)
dictionary = aksvd.fit(signals[np.random.choice(range(signals.shape[0]), 1000, replace = True)]).components_
gamma = aksvd.transform(signals)

plt.imshow(image.reconstruct_from_patches_2d(
gamma.dot(dictionary).reshape((29200, 16, 16)), (161, 215)))

#generate test images and target vector at the same time
image_dict = []
targets = []
for i in range(3):
    for j in range(3):
        image_dict.append(img_array2[(i)*steph:(i+1)*steph, (j)*stepv:(j+1)*stepv])
        targets.append(0)
        image_dict.append(img_array4[(i)*steph:(i+1)*steph, (j)*stepv:(j+1)*stepv])
        targets.append(1)