Exemplo n.º 1
0
    def test_ksvd_normal_input(self):
        n_nonzero_coefs = 4
        n_samples = 512
        n_features = 32
        n_components = 24
        max_iter = 500

        A0, X = generate_dictionary_and_samples(n_samples, n_features, n_components, n_nonzero_coefs)
        model = KSVD(n_components=n_components, transform_n_nonzero_coefs=n_nonzero_coefs, max_iter=max_iter,
                     method='normal')
        model.fit(X)

        # check error of learning
        self.assertTrue(model.error_[-1] < 10)
        self.assertTrue(model.n_iter_ <= max_iter)

        # check estimated dictionary
        norm = np.linalg.norm(model.components_ - A0, ord='fro')
        self.assertTrue(norm < 15)

        # check reconstructed data
        code = model.transform(X)
        reconstructed = np.dot(code, model.components_)
        reconstruct_error = np.linalg.norm(reconstructed - X, ord='fro')
        self.assertTrue(reconstruct_error < 15)
Exemplo n.º 2
0
    def test_transform_with_mask(self):
        n_nonzero_coefs = 4
        n_samples = 128
        n_features = 32
        n_components = 16
        max_iter = 10
        missing_value = 0

        A0, X = generate_dictionary_and_samples(n_samples, n_features,
                                                n_components, n_nonzero_coefs)
        X[X < 0.1] = missing_value
        mask = np.where(X == missing_value, 0, 1)

        model = KSVD(n_components=n_components,
                     transform_n_nonzero_coefs=n_nonzero_coefs,
                     max_iter=max_iter,
                     missing_value=missing_value,
                     method='normal')
        model.fit(X)

        # check error of learning
        code = model.transform(X)
        err = np.linalg.norm(mask * (X - code.dot(model.components_)), 'fro')
        self.assertTrue(err <= model.error_[0])
        self.assertTrue(model.n_iter_ <= max_iter)
Exemplo n.º 3
0
    def test_approximate_ksvd(self):
        n_nonzero_coefs = 5
        n_samples = 128
        n_features = 32
        n_components = 16
        max_iter = 10

        A0, X = generate_dictionary_and_samples(n_samples, n_features, n_components, n_nonzero_coefs)
        model = KSVD(n_components=n_components, transform_n_nonzero_coefs=n_nonzero_coefs, max_iter=max_iter,
                     method='approximate')
        model.fit(X)

        # check error of learning
        self.assertTrue(model.error_[-1] <= model.error_[0])
        self.assertTrue(model.n_iter_ <= max_iter)
Exemplo n.º 4
0
    def test_approximate_ksvd_warm_start(self):
        n_nonzero_coefs = 5
        n_samples = 128
        n_features = 32
        n_components = 16
        max_iter = 1

        A0, X = generate_dictionary_and_samples(n_samples, n_features, n_components, n_nonzero_coefs)
        model = KSVD(n_components=n_components, transform_n_nonzero_coefs=n_nonzero_coefs, max_iter=max_iter,
                     method='approximate')

        prev_error = np.linalg.norm(X, 'fro')
        for i in range(10):
            model.fit(X)
            # print(model.error_)
            self.assertTrue(model.error_[-1] <= prev_error)
            prev_error = model.error_[-1]
Exemplo n.º 5
0
    def test_ksvd_input_with_missing_values(self):
        n_nonzero_coefs = 4
        n_samples = 128
        n_features = 32
        n_components = 16
        max_iter = 100
        missing_value = 0

        A0, X = generate_dictionary_and_samples(n_samples, n_features, n_components, n_nonzero_coefs)
        X[X < 0.1] = missing_value
        model = KSVD(n_components=n_components, transform_n_nonzero_coefs=n_nonzero_coefs, max_iter=max_iter,
                     missing_value=missing_value, method='normal')
        model.fit(X)

        # check error of learning
        self.assertTrue(model.error_[-1] <= model.error_[0])
        self.assertTrue(model.n_iter_ <= max_iter)
Exemplo n.º 6
0
    def test_transform(self):
        n_nonzero_coefs = 4
        n_samples = 128
        n_features = 32
        n_components = 24
        max_iter = 500

        A0, X = generate_dictionary_and_samples(n_samples, n_features, n_components, n_nonzero_coefs)
        model = KSVD(n_components=n_components, transform_n_nonzero_coefs=n_nonzero_coefs, max_iter=max_iter,
                     method='normal')
        model.fit(X)

        # check error of learning
        code = model.transform(X)
        err = np.linalg.norm(X - code.dot(model.components_), 'fro')
        self.assertTrue(err <= model.error_[-1])
        self.assertTrue(model.n_iter_ <= max_iter)
Exemplo n.º 7
0
    def test_ksvd_dict_init(self):
        D = np.random.rand(10, 100)
        model = KSVD(n_components=10,
                     transform_n_nonzero_coefs=5,
                     max_iter=1,
                     method='normal',
                     dict_init=D)
        npt.assert_array_equal(model.dict_init, D)

        # shape of X is invalid against initial dictionary
        X = np.random.rand(20, 200)
        with self.assertRaises(ValueError):
            model.fit(X)

        # n_components is invalid against initial dictionary
        X = np.random.rand(20, 100)
        model = KSVD(n_components=20,
                     transform_n_nonzero_coefs=5,
                     max_iter=1,
                     method='normal',
                     dict_init=D)
        with self.assertRaises(ValueError):
            model.fit(X)
Exemplo n.º 8
0

def concat_tile(im_list_2d):
    return cv2.vconcat([cv2.hconcat(im_list_h) for im_list_h in im_list_2d])


im = cv2.imread("./food_jambalaya.png")
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)  #cv2.COLOR_BGR2RGB
im = cv2.resize(im, dsize=(128, 128), interpolation=cv2.INTER_CUBIC) / 256.0
im = np.clip(im, 0.001, 0.999)
plt.title("origin")
plt.imshow(im, vmin=0.0, vmax=1.0)
plt.show()

ksvd = KSVD(n_components=12, transform_n_nonzero_coefs=None, n_jobs=12)
ksvd.fit(im.reshape(-1, 64).astype(np.float64))

D = ksvd.components_.reshape(-1, 8, 8).astype(np.float64)
for i in range(12):
    plt.subplot(3, 4, i + 1)
    plt.imshow(D[i], vmin=0.0, vmax=1.0)
    plt.axis('off')
plt.show()

X = ksvd.transform(im.reshape(-1, 64).astype(np.float64))
plt.title("X")
plt.imshow(X, vmin=0.0, vmax=1.0)
plt.show()

plt.title("Re")
_y = np.dot(X, ksvd.components_)
Exemplo n.º 9
0
    if maxs < 10:
        break

#im =cv2.imread('./manner_door_open_ashi.png')
im = cv2.imread('./apple2orange/testA/n07740461_51.jpg')
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
im = cv2.resize(im, dsize=(128, 128), interpolation=cv2.INTER_CUBIC) / 256.0
im = np.clip(im, 0.001, 0.999)
plt.title("origin")
plt.imshow(im, vmin=0.0, vmax=1.0)
plt.show()

ksvd = KSVD(n_components=16,
            transform_n_nonzero_coefs=None,
            n_jobs=multiprocessing.cpu_count() - 1)
ksvd.fit(ims)

D = ksvd.components_.reshape(-1, 8, 8, 3).astype(np.float64)
for i in range(12):
    plt.subplot(3, 4, i + 1)
    plt.imshow(D[i], vmin=0.0, vmax=1.0)
    plt.axis('off')
plt.show()

X = ksvd.transform(im.reshape(-1, 8 * 8 * 3).astype(np.float64))
plt.title("X")
plt.imshow(X, vmin=0.0, vmax=1.0)
plt.show()

plt.title("Re")
_y = np.dot(X, ksvd.components_)