data.py 8.4 KB

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