예제 #1
0
        X = load_dataset('highway.pkl')['X'].astype(float) / 255
        #print(X[0].reshape(64,64).T)
        n, d = X.shape
        print(n, d)
        h, w = 64, 64  # height and width of each image

        k = 5  # number of PCs
        threshold = 0.1  # threshold for being considered "foreground"
        #threshold = 0.07

        model = AlternativePCA(k=k)
        model.fit(X)
        Z = model.compress(X)
        Xhat_pca = model.expand(Z)

        model = RobustPCA(k=k)
        model.fit(X)
        Z = model.compress(X)
        Xhat_robust = model.expand(Z)

        fig, ax = plt.subplots(2, 3)
        for i in range(10):
            ax[0, 0].set_title('$X$')
            ax[0, 0].imshow(X[i].reshape(h, w).T, cmap='gray')

            ax[0, 1].set_title('$\hat{X}$ (L2)')
            ax[0, 1].imshow(Xhat_pca[i].reshape(h, w).T, cmap='gray')

            ax[0, 2].set_title('$|x_i-\hat{x_i}|$>threshold (L2)')
            ax[0, 2].imshow(
                (np.abs(X[i] - Xhat_pca[i]) < threshold).reshape(h, w).T,
예제 #2
0
    elif question == '3.1': 
        X = load_dataset('highway.pkl')['X'].astype(float)/255
        n,d = X.shape
        print(n,d)
        h,w = 64,64      # height and width of each image

        k = 5            # number of PCs
        threshold = 0.1  # threshold for being considered "foreground"

        model = AlternativePCA(k=k)
        model.fit(X)
        Z = model.compress(X)
        Xhat_pca = model.expand(Z)

        model = RobustPCA(k=k) # TODO: implement this class
        model.fit(X)
        Z = model.compress(X)
        Xhat_robust = model.expand(Z)

        fig, ax = plt.subplots(2,3)
        for i in range(10):
            ax[0,0].set_title('$X$')
            ax[0,0].imshow(X[i].reshape(h,w).T, cmap='gray')

            ax[0,1].set_title('$\hat{X}$ (L2)')
            ax[0,1].imshow(Xhat_pca[i].reshape(h,w).T, cmap='gray')
            
            ax[0,2].set_title('$|x_i-\hat{x_i}|$>threshold (L2)')
            ax[0,2].imshow((np.abs(X[i] - Xhat_pca[i])<threshold).reshape(h,w).T, cmap='gray')
예제 #3
0
파일: main.py 프로젝트: rojinb/pca
    io_args = parser.parse_args()
    question = io_args.question

    if question == '2.1':
        X = utils.load_dataset('highway')['X'].astype(float) / 255
        n, d = X.shape
        print(X.shape)
        h, w = 64, 64  # height and width of each image

        # the two variables below are parameters for the foreground/background extraction method
        # you should just leave these two as default.

        k = 5  # number of PCs
        threshold = 0.04  # a threshold for separating foreground from background

        model = RobustPCA(k=k)
        model.fit(X)
        Z = model.compress(X)
        Xhat = model.expand(Z)

        # save 10 frames for illustration purposes
        for i in range(10):
            plt.subplot(1, 3, 1)
            plt.title('Original')
            plt.imshow(X[i].reshape(h, w).T, cmap='gray')
            plt.subplot(1, 3, 2)
            plt.title('Reconstructed')
            plt.imshow(Xhat[i].reshape(h, w).T, cmap='gray')
            plt.subplot(1, 3, 3)
            plt.title('Thresholded Difference')
            plt.imshow(1.0 * (abs(X[i] - Xhat[i]) < threshold).reshape(h, w).T,
예제 #4
0
    elif question == '4.1':
        X = load_dataset('highway.pkl')['X'].astype(float) / 255
        n, d = X.shape
        print(n, d)
        h, w = 64, 64  # height and width of each image

        k = 5  # number of PCs
        threshold = 0.1  # threshold for being considered "foreground"

        model = AlternativePCA(k=k)
        model.fit(X)
        Z = model.compress(X)
        Xhat_pca = model.expand(Z)

        model = RobustPCA(k=k, eps=0.0001)
        model.fit(X)
        Z = model.compress(X)
        Xhat_robust = model.expand(Z)

        fig, ax = plt.subplots(2, 3)
        for i in range(10):
            ax[0, 0].set_title('$X$')
            ax[0, 0].imshow(X[i].reshape(h, w).T, cmap='gray')

            ax[0, 1].set_title('$\hat{X}$ (L2)')
            ax[0, 1].imshow(Xhat_pca[i].reshape(h, w).T, cmap='gray')

            ax[0, 2].set_title('$|x_i-\hat{x_i}|$>threshold (L2)')
            ax[0, 2].imshow(
                (np.abs(X[i] - Xhat_pca[i]) < threshold).reshape(h, w).T,
예제 #5
0
        X = utils.load_dataset('highway')['X'].astype(float) / 255
        n, d = X.shape
        h, w = 64, 64  # height and width of each image

        # the two variables below are parameters for the foreground/background extraction method
        # you should just leave these two as default.

        k = 5  # number of PCs
        threshold = 0.04  # a threshold for separating foreground from background

        model = AlternativePCA(k=k)
        model.fit(X)
        Z = model.compress(X)
        Xhat_pca = model.expand(Z)

        model_r = RobustPCA(k=k)
        model_r.fit(X)
        Z = model_r.compress(X)
        Xhat_robust = model_r.expand(Z)

        for i in range(10):
            plt.subplot(2, 3, 1)
            plt.title('Original')
            plt.imshow(X[i].reshape(h, w).T, cmap='gray')
            plt.subplot(2, 3, 2)
            plt.title('PCA Reconst.')  # Reconstruction
            plt.imshow(Xhat_pca[i].reshape(h, w).T, cmap='gray')
            plt.subplot(2, 3, 3)
            plt.title('PCA Thresh. Diff.')  # Thresholded Difference
            plt.imshow(1.0 *
                       (abs(X[i] - Xhat_pca[i]) < threshold).reshape(h, w).T,