utils.savefig('q2_2_PCA_animals.png') 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) 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)')
if question == '2.1': 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 = 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, cmap='gray') utils.savefig('q2.1_highway_{:03d}.pdf'.format(i))