transformation_functions.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. lab_img = metrics.get_LAB_L(img)
  17. lab_img = np.array(lab_img, 'uint8')
  18. ica = FastICA(n_components = 50)
  19. # run ICA on image
  20. ica.fit(lab_img)
  21. # reconstruct image with independent components
  22. image_ica = ica.fit_transform(lab_img)
  23. restored_image = ica.inverse_transform(image_ica)
  24. return restored_image
  25. def ipca_reconstruction(img, components, _batch_size=25):
  26. lab_img = metrics.get_LAB_L(img)
  27. lab_img = np.array(lab_img, 'uint8')
  28. transformer = IncrementalPCA(n_components=components, batch_size=_batch_size)
  29. transformed_image = transformer.fit_transform(lab_img)
  30. restored_image = transformer.inverse_transform(transformed_image)
  31. return restored_image