data.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. from ipfml import processing, metrics, utils
  2. from modules.utils.config import *
  3. from PIL import Image
  4. from skimage import color
  5. from sklearn.decomposition import FastICA
  6. from sklearn.decomposition import IncrementalPCA
  7. from sklearn.decomposition import TruncatedSVD
  8. from numpy.linalg import svd as lin_svd
  9. import numpy as np
  10. _scenes_names_prefix = '_scenes_names'
  11. _scenes_indices_prefix = '_scenes_indices'
  12. # store all variables from current module context
  13. context_vars = vars()
  14. def get_svd_data(data_type, block):
  15. """
  16. Method which returns the data type expected
  17. """
  18. if data_type == 'lab':
  19. block_file_path = '/tmp/lab_img.png'
  20. block.save(block_file_path)
  21. data = processing.get_LAB_L_SVD_s(Image.open(block_file_path))
  22. if data_type == 'mscn':
  23. img_mscn_revisited = processing.rgb_to_mscn(block)
  24. # save tmp as img
  25. img_output = Image.fromarray(img_mscn_revisited.astype('uint8'), 'L')
  26. mscn_revisited_file_path = '/tmp/mscn_revisited_img.png'
  27. img_output.save(mscn_revisited_file_path)
  28. img_block = Image.open(mscn_revisited_file_path)
  29. # extract from temp image
  30. data = metrics.get_SVD_s(img_block)
  31. """if data_type == 'mscn':
  32. img_gray = np.array(color.rgb2gray(np.asarray(block))*255, 'uint8')
  33. img_mscn = processing.calculate_mscn_coefficients(img_gray, 7)
  34. img_mscn_norm = processing.normalize_2D_arr(img_mscn)
  35. img_mscn_gray = np.array(img_mscn_norm*255, 'uint8')
  36. data = metrics.get_SVD_s(img_mscn_gray)
  37. """
  38. if data_type == 'low_bits_6':
  39. low_bits_6 = processing.rgb_to_LAB_L_low_bits(block, 6)
  40. data = metrics.get_SVD_s(low_bits_6)
  41. if data_type == 'low_bits_5':
  42. low_bits_5 = processing.rgb_to_LAB_L_low_bits(block, 5)
  43. data = metrics.get_SVD_s(low_bits_5)
  44. if data_type == 'low_bits_4':
  45. low_bits_4 = processing.rgb_to_LAB_L_low_bits(block, 4)
  46. data = metrics.get_SVD_s(low_bits_4)
  47. if data_type == 'low_bits_3':
  48. low_bits_3 = processing.rgb_to_LAB_L_low_bits(block, 3)
  49. data = metrics.get_SVD_s(low_bits_3)
  50. if data_type == 'low_bits_2':
  51. low_bits_2 = processing.rgb_to_LAB_L_low_bits(block, 2)
  52. data = metrics.get_SVD_s(low_bits_2)
  53. if data_type == 'low_bits_4_shifted_2':
  54. data = metrics.get_SVD_s(processing.rgb_to_LAB_L_bits(block, (3, 6)))
  55. if data_type == 'sub_blocks_stats':
  56. block = np.asarray(block)
  57. width, height, _= block.shape
  58. sub_width, sub_height = int(width / 4), int(height / 4)
  59. sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
  60. data = []
  61. for sub_b in sub_blocks:
  62. # by default use the whole lab L canal
  63. l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
  64. # get information we want from svd
  65. data.append(np.mean(l_svd_data))
  66. data.append(np.median(l_svd_data))
  67. data.append(np.percentile(l_svd_data, 25))
  68. data.append(np.percentile(l_svd_data, 75))
  69. data.append(np.var(l_svd_data))
  70. area_under_curve = utils.integral_area_trapz(l_svd_data, dx=100)
  71. data.append(area_under_curve)
  72. # convert into numpy array after computing all stats
  73. data = np.asarray(data)
  74. if data_type == 'sub_blocks_stats_reduced':
  75. block = np.asarray(block)
  76. width, height, _= block.shape
  77. sub_width, sub_height = int(width / 4), int(height / 4)
  78. sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
  79. data = []
  80. for sub_b in sub_blocks:
  81. # by default use the whole lab L canal
  82. l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
  83. # get information we want from svd
  84. data.append(np.mean(l_svd_data))
  85. data.append(np.median(l_svd_data))
  86. data.append(np.percentile(l_svd_data, 25))
  87. data.append(np.percentile(l_svd_data, 75))
  88. data.append(np.var(l_svd_data))
  89. # convert into numpy array after computing all stats
  90. data = np.asarray(data)
  91. if data_type == 'sub_blocks_area':
  92. block = np.asarray(block)
  93. width, height, _= block.shape
  94. sub_width, sub_height = int(width / 8), int(height / 8)
  95. sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
  96. data = []
  97. for sub_b in sub_blocks:
  98. # by default use the whole lab L canal
  99. l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
  100. area_under_curve = utils.integral_area_trapz(l_svd_data, dx=50)
  101. data.append(area_under_curve)
  102. # convert into numpy array after computing all stats
  103. data = np.asarray(data)
  104. if data_type == 'sub_blocks_area_normed':
  105. block = np.asarray(block)
  106. width, height, _= block.shape
  107. sub_width, sub_height = int(width / 8), int(height / 8)
  108. sub_blocks = processing.divide_in_blocks(block, (sub_width, sub_height))
  109. data = []
  110. for sub_b in sub_blocks:
  111. # by default use the whole lab L canal
  112. l_svd_data = np.array(processing.get_LAB_L_SVD_s(sub_b))
  113. l_svd_data = utils.normalize_arr(l_svd_data)
  114. area_under_curve = utils.integral_area_trapz(l_svd_data, dx=50)
  115. data.append(area_under_curve)
  116. # convert into numpy array after computing all stats
  117. data = np.asarray(data)
  118. if data_type == 'mscn_var_4':
  119. data = _get_mscn_variance(block, (100, 100))
  120. if data_type == 'mscn_var_16':
  121. data = _get_mscn_variance(block, (50, 50))
  122. if data_type == 'mscn_var_64':
  123. data = _get_mscn_variance(block, (25, 25))
  124. if data_type == 'mscn_var_16_max':
  125. data = _get_mscn_variance(block, (50, 50))
  126. data = np.asarray(data)
  127. size = int(len(data) / 4)
  128. indices = data.argsort()[-size:][::-1]
  129. data = data[indices]
  130. if data_type == 'mscn_var_64_max':
  131. data = _get_mscn_variance(block, (25, 25))
  132. data = np.asarray(data)
  133. size = int(len(data) / 4)
  134. indices = data.argsort()[-size:][::-1]
  135. data = data[indices]
  136. if data_type == 'ica_diff':
  137. current_image = metrics.get_LAB_L(block)
  138. ica = FastICA(n_components=50)
  139. ica.fit(current_image)
  140. image_ica = ica.fit_transform(current_image)
  141. image_restored = ica.inverse_transform(image_ica)
  142. final_image = utils.normalize_2D_arr(image_restored)
  143. final_image = np.array(final_image * 255, 'uint8')
  144. sv_values = utils.normalize_arr(metrics.get_SVD_s(current_image))
  145. ica_sv_values = utils.normalize_arr(metrics.get_SVD_s(final_image))
  146. data = abs(np.array(sv_values) - np.array(ica_sv_values))
  147. if data_type == 'svd_trunc_diff':
  148. current_image = metrics.get_LAB_L(block)
  149. svd = TruncatedSVD(n_components=30, n_iter=100, random_state=42)
  150. transformed_image = svd.fit_transform(current_image)
  151. restored_image = svd.inverse_transform(transformed_image)
  152. reduced_image = (current_image - restored_image)
  153. U, s, V = metrics.get_SVD(reduced_image)
  154. data = s
  155. if data_type == 'ipca_diff':
  156. current_image = metrics.get_LAB_L(block)
  157. transformer = IncrementalPCA(n_components=20, batch_size=25)
  158. transformed_image = transformer.fit_transform(current_image)
  159. restored_image = transformer.inverse_transform(transformed_image)
  160. reduced_image = (current_image - restored_image)
  161. U, s, V = metrics.get_SVD(reduced_image)
  162. data = s
  163. if data_type == 'svd_reconstruct':
  164. reconstructed_interval = (90, 200)
  165. begin, end = reconstructed_interval
  166. lab_img = metrics.get_LAB_L(block)
  167. lab_img = np.array(lab_img, 'uint8')
  168. U, s, V = lin_svd(lab_img, full_matrices=True)
  169. smat = np.zeros((end-begin, end-begin), dtype=complex)
  170. smat[:, :] = np.diag(s[begin:end])
  171. output_img = np.dot(U[:, begin:end], np.dot(smat, V[begin:end, :]))
  172. output_img = np.array(output_img, 'uint8')
  173. data = metrics.get_SVD_s(output_img)
  174. return data
  175. def _get_mscn_variance(block, sub_block_size=(50, 50)):
  176. blocks = processing.divide_in_blocks(block, sub_block_size)
  177. data = []
  178. for block in blocks:
  179. mscn_coefficients = processing.get_mscn_coefficients(block)
  180. flat_coeff = mscn_coefficients.flatten()
  181. data.append(np.var(flat_coeff))
  182. return np.sort(data)
  183. def get_renderer_scenes_indices(renderer_name):
  184. if renderer_name not in renderer_choices:
  185. raise ValueError("Unknown renderer name")
  186. if renderer_name == 'all':
  187. return scenes_indices
  188. else:
  189. return context_vars[renderer_name + _scenes_indices_prefix]
  190. def get_renderer_scenes_names(renderer_name):
  191. if renderer_name not in renderer_choices:
  192. raise ValueError("Unknown renderer name")
  193. if renderer_name == 'all':
  194. return scenes_names
  195. else:
  196. return context_vars[renderer_name + _scenes_names_prefix]