コード例 #1
0
ファイル: align.py プロジェクト: kevinzakka/learn-linalg
def main():
    img_dir = './imgs/'
    H, W = 800, 800
    inter = InteractiveImage(img_dir, (H, W))
    inter.show()

    # load and homogenize coordinates
    x, y = load_coords('coords.p', H, W)
    y = homogenize(y)

    # create P array
    num_pts = y.shape[0]

    P = []
    for i in range(num_pts):
        P.append(np.hstack([y[i], np.zeros(3).T]))
        P.append(np.hstack([np.zeros(3).T, y[i]]))
    P = np.array(P)

    # create Q array
    Q = x.flatten()

    # solve for M using Cholesky factorization
    M = Cholesky(np.dot(P.T, P)).solve(np.dot(P.T, Q))
    M = M.reshape(2, 3)
    M[np.abs(M) < 1e-10] = 0

    # read crooked image
    included_extensions = ['png']
    file_names = [
        fn for fn in os.listdir(img_dir) if any(
            fn.endswith(ext) for ext in included_extensions) if "crooked" in fn
    ]
    img = img2array(img_dir + file_names[0], desired_size=(H, W))

    # affine transform and interpolate
    affine_grid = affine_grid_generator(H, W, M)
    x_s = affine_grid[0:1, :].squeeze()
    y_s = affine_grid[1:2, :].squeeze()
    out = bilinear_sampler(img, x_s, y_s)
    out = np.clip(out, 0, 1)

    imgs = np.array([img, out])
    N = imgs.shape[0]

    # plot
    fig, ax = plt.subplots(nrows=1, ncols=N)
    titles = ['Crooked', "Fixed"]

    for i in range(N):
        ax[i].imshow(imgs[i])
        ax[i].get_xaxis().set_visible(False)
        ax[i].get_yaxis().set_visible(False)
        ax[i].set_title(titles[i])

    plt.show()
コード例 #2
0
    def test_decompose_crout(self):
        T = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]])

        actual = Cholesky(T, crout=True).decompose()
        expected = LA.cholesky(T)

        self.assertTrue(np.allclose(actual, expected))
コード例 #3
0
  def test_gradient_descent(self):
    A = random_spd(5)
    b = np.random.randn(5)

    expected = Cholesky(A).solve(b)
    actual = GradientDescent(1000000).solve(A, b)

    self.assertTrue(np.allclose(expected, actual))
コード例 #4
0
    def test_solve_multi(self):
        T = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]])
        b = np.array([[1, 2, 3], [2, 1, 2]]).T

        actual = Cholesky(T).solve(b)
        expected = np.linalg.solve(T, b)

        self.assertTrue(np.allclose(actual, expected))
コード例 #5
0
    def test_conjugate_gradient(self):
        max_iters = 100_000
        A = random_spd(5)
        b = np.random.randn(5)

        expected = Cholesky(A).solve(b)
        actual = ConjugateGradient(max_iters).solve(A, b)

        self.assertTrue(np.allclose(expected, actual))
コード例 #6
0
import numpy as np
import numpy.linalg as LA

from linalg.cholesky import Cholesky
from linalg import inverse

if __name__ == "__main__":
    A = np.array([
        [0.01, 0.010001, .5],
        [0.02, 0.020001, 0.],
        [0.03, 0.030001, 0.5],
    ])
    T = A.T @ A
    b = np.array([.1, 2., .3])

    actual = Cholesky(T).solve(b)
    expected = LA.solve(T, b)
    diff = np.linalg.norm(actual - expected)
    cond = np.linalg.norm(A) * np.linalg.norm(inverse(A))
    print("Condition number {:.2f} - solution difference: {}".format(
        cond, diff))

    A = np.random.randn(3, 3)
    T = A.T @ A
    actual = Cholesky(T).solve(b)
    expected = LA.solve(T, b)
    diff = np.linalg.norm(actual - expected)
    cond = np.linalg.norm(A) * np.linalg.norm(inverse(A))
    print("Condition number {:.2f} - solution difference: {}".format(
        cond, diff))
コード例 #7
0
 def test_fails_on_non_spd(self):
     A = np.random.randn(5, 5)
     with self.assertRaises(AssertionError):
         Cholesky(A, crout=False)