как показать изображение после ПК? - PullRequest
0 голосов
/ 14 июля 2020

У меня есть изображение RGB. Я хочу применить PCA для сжатия изображений и увидеть результат после приложения.

Вот что я пытался сделать:

from PIL import Image
import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

------

def load_image(infilename):
   img = Image.open(infilename)
   img.load()   
   img.show()
   data = np.asarray(img, dtype="int32")
   return data
---------

data = load_image("Image_for_pca.jpg")
r = data[:,:,0]
print("r", r.shape)
g = data[:,:,1]
print("g", g.shape)
b = data[:,:,2]
print("b", b.shape)
concat_matrix_image = np.hstack((np.hstack((r,g)),b))
print("concatMatrixImage", concat_matrix_image.shape)

output of the prints:
r (161, 212)
g (161, 212)
b (161, 212)
concatMatrixImage (161, 636)

# list of dimension
pca_number_of_wanted_dimension = [3 ,5 ,10 ,15 ,20 ,30]
-------
def create_pca_model(number_of_components):
   pca = PCA(n_components=number_of_components)
   return pca
-------
def plot_varience_on_pca(pca):
   plt.plot(np.cumsum(pca.explained_variance_ratio_))
   plt.title("The number of wanted dimension is {}".format(pca.n_components))
   plt.xlabel('number of components')
   plt.ylabel('cumulative explained variance')
   plt.show()
   ------
   def recover_pic(pca, principal_components):
   #Project lower dimension data onto original features
   approximation = pca.inverse_transform(principal_components)    
   approximation = approximation.reshape(-1,161,212)

   # approximation = approximation.astype(np.uint8)
   # print(approximation.shape)
   # img = Image.fromarray(approximation, 'RGB')
   approximation.show()
   -------
   
   for i in pca_number_of_wanted_dimension:
      pca =  create_pca_model(i)
      principal_components = pca.fit_transform(concat_matrix_image)
      print(principal_components.shape)
      recover_pic(pca, principal_components)
      plot_varience_on_pca(pca)

Как восстановить изображение после pca.inverse_transform?

1 Ответ

0 голосов
/ 31 июля 2020

Поскольку у меня нет ваших данных, я должен показать вам, как вы можете использовать мои данные.

  • Загрузка данных и отображение данных
from sklearn.datasets import fetch_olivetti_faces
from sklearn.model_selection import train_test_split
from matplotlib.pyplot import subplots
from matplotlib.pyplot import suptitle
from matplotlib.pyplot import savefig
from sklearn.decomposition import PCA


def display_set(n_row, n_col, x, y_, t, title="Id:{}",
                fig_size=(6, 3), dpi_=300, f_name="default.png"):
    fig, ax = subplots(n_row, n_col, figsize=fig_size, dpi=dpi_)
    ax = ax.flatten()

    for i in range(n_row * n_col):
        ax[i].imshow(X=x[i], cmap='gray')
        ax[i].set_xticks([])
        ax[i].set_yticks([])
        ax[i].set_title(title.format(y_[i]))
    suptitle(t=t)
    savefig(f_name)


olivetti = fetch_olivetti_faces()

X = olivetti.images  # Train
y = olivetti.target  # Labels

x_train, x_test, y_train, y_test = train_test_split(X, y,
                                                    test_size=0.3,
                                                    random_state=42)

train_name = "train_samples.png"
test_name = "test_samples.png"
display_set(n_row=2, n_col=10, x=x_train, y_=y_train,
            t="Train-set samples", title="Id:{}", f_name=train_name)
display_set(n_row=2, n_col=10, x=x_test, y_=y_test,
            t="Test-set samples", title="Id:{}", f_name=test_name)

Вывод образцов поездов:

enter image description here

The output of test samples:

enter image description here

Now lets create a pca object

x_train = x_train.reshape((x_train.shape[0], X.shape[1] * X.shape[2]))
x_test = x_test.reshape((x_test.shape[0], X.shape[1] * X.shape[2]))
pca_train = PCA(n_components=100).fit(X=x_train)
pca_test = PCA(n_components=100).fit(X=x_test)
eig_num_tr = len(pca_train.components_)
eig_num_te = len(pca_test.components_)
# eigen training faces
eig_tr_faces = pca_train.components_.reshape((eig_num_tr, X.shape[1], X.shape[2]))  
# eigen test faces
eig_te_faces = pca_test.components_.reshape((eig_num_te, X.shape[1], X.shape[2]))  
title_tr = "PCA Applied Train-set samples"
title_te = "PCA Applied Test-set samples"
t_ = "Eig. Id:{}"

display_set(n_row=2, n_col=5, x=eig_tr_faces, y_=range(0, eig_num_tr-1), 
            t=title_tr, title=t_, fig_size=(6, 3.2))
display_set(n_row=2, n_col=5, x=eig_te_faces, y_=range(0, eig_num_te-1), 
            t=title_te, title=t_, fig_size=(6, 3.2))

The output of the training set: enter image description here

The output of the test set: введите описание изображения здесь

...