data.py 13 KB

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