transformation_functions.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from numpy.linalg import svd
  2. from sklearn.decomposition import FastICA, IncrementalPCA
  3. import numpy as np
  4. from ipfml import metrics
  5. def svd_reconstruction(img, interval):
  6. begin, end = interval
  7. lab_img = metrics.get_LAB_L(img)
  8. lab_img = np.array(lab_img, 'uint8')
  9. U, s, V = svd(lab_img, full_matrices=True)
  10. # reconstruction using specific interval
  11. smat = np.zeros((end-begin, end-begin), dtype=complex)
  12. smat[:, :] = np.diag(s[begin:end])
  13. output_img = np.dot(U[:, begin:end], np.dot(smat, V[begin:end, :]))
  14. return output_img
  15. def fast_ica_reconstruction(img, components):
  16. ica = FastICA(n_components = 50)
  17. # run ICA on image
  18. ica.fit(img)
  19. # reconstruct image with independent components
  20. image_ica = ica.fit_transform(img)
  21. restored_image = ica.inverse_transform(image_ica)
  22. return restored_image
  23. def ipca_reconstruction(img, components, _batch_size=25):
  24. transformer = IncrementalPCA(n_components=components, batch_size=_batch_size)
  25. transformed_image = transformer.fit_transform(img)
  26. restored_image = transformer.inverse_transform(transformed_image)
  27. return restored_image