data.py 11 KB

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