display_svd_data_error_scene.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # main imports
  2. import sys, os, argparse
  3. import numpy as np
  4. # image processing imports
  5. from PIL import Image
  6. from skimage import color
  7. import matplotlib.pyplot as plt
  8. from data_attributes import get_svd_data
  9. import ipfml.iqa.fr as fr_iqa
  10. from ipfml import utils
  11. # modules and config imports
  12. sys.path.insert(0, '') # trick to enable import of main folder module
  13. import custom_config as cfg
  14. from modules.utils import data as dt
  15. # getting configuration information
  16. zone_folder = cfg.zone_folder
  17. min_max_filename = cfg.min_max_filename_extension
  18. # define all scenes values
  19. scenes_list = cfg.scenes_names
  20. scenes_indices = cfg.scenes_indices
  21. choices = cfg.normalization_choices
  22. path = cfg.dataset_path
  23. zones = cfg.zones_indices
  24. seuil_expe_filename = cfg.seuil_expe_filename
  25. features_choices = cfg.features_choices_labels
  26. max_nb_bits = 8
  27. display_error = False
  28. error_data_choices = ['mae', 'mse', 'ssim', 'psnr']
  29. def get_error_distance(p_error, y_true, y_test):
  30. function_name = p_error
  31. try:
  32. error_method = getattr(fr_iqa, function_name)
  33. except AttributeError:
  34. raise NotImplementedError("Error `{}` not implement `{}`".format(fr_iqa.__name__, function_name))
  35. return error_method(y_true, y_test)
  36. def display_svd_values(p_scene, p_interval, p_indices, p_feature, p_mode, p_step, p_norm, p_error, p_ylim):
  37. """
  38. @brief Method which gives information about svd curves from zone of picture
  39. @param p_scene, scene expected to show svd values
  40. @param p_interval, interval [begin, end] of svd data to display
  41. @param p_interval, interval [begin, end] of samples or minutes from render generation engine
  42. @param p_feature, feature computed to show
  43. @param p_mode, normalization's mode
  44. @param p_norm, normalization or not of selected svd data
  45. @param p_error, error feature used to display
  46. @param p_ylim, ylim choice to better display of data
  47. @return nothing
  48. """
  49. max_value_svd = 0
  50. min_value_svd = sys.maxsize
  51. scenes = os.listdir(path)
  52. # remove min max file from scenes folder
  53. scenes = [s for s in scenes if min_max_filename not in s]
  54. begin_data, end_data = p_interval
  55. begin_index, end_index = p_indices
  56. # go ahead each scenes
  57. for folder_scene in scenes:
  58. if p_scene == folder_scene:
  59. scene_path = os.path.join(path, folder_scene)
  60. # construct each zones folder name
  61. zones_folder = []
  62. # get zones list info
  63. for index in zones:
  64. index_str = str(index)
  65. if len(index_str) < 2:
  66. index_str = "0" + index_str
  67. current_zone = "zone"+index_str
  68. zones_folder.append(current_zone)
  69. images_data = []
  70. images_path = []
  71. threshold_learned_zones = []
  72. # get all images of folder
  73. scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
  74. number_scene_image = len(scene_images)
  75. for id, zone_folder in enumerate(zones_folder):
  76. # get threshold information
  77. zone_path = os.path.join(scene_path, zone_folder)
  78. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  79. # open treshold path and get this information
  80. with open(path_seuil, "r") as seuil_file:
  81. threshold_learned = int(seuil_file.readline().strip())
  82. threshold_learned_zones.append(threshold_learned)
  83. threshold_mean = np.mean(np.asarray(threshold_learned_zones))
  84. threshold_image_found = False
  85. svd_data = []
  86. # for each images
  87. for id_img, img_path in enumerate(scene_images):
  88. current_quality_image = dt.get_scene_image_quality(img_path)
  89. img = Image.open(img_path)
  90. svd_values = get_svd_data(p_feature, img)
  91. if p_norm:
  92. svd_values = svd_values[begin_data:end_data]
  93. # update min max values
  94. min_value = svd_values.min()
  95. max_value = svd_values.max()
  96. if min_value < min_value_svd:
  97. min_value_svd = min_value
  98. if max_value > min_value_svd:
  99. max_value_svd = max_value
  100. # keep in memory used data
  101. if current_quality_image % p_step == 0:
  102. if current_quality_image >= begin_index and current_quality_image <= end_index:
  103. images_path.append(img_path)
  104. svd_data.append(svd_values)
  105. if threshold_mean < current_quality_image and not threshold_image_found:
  106. threshold_image_found = True
  107. threshold_image_zone = dt.get_scene_image_postfix(img_path)
  108. print('%.2f%%' % ((id_img + 1) / number_scene_image * 100))
  109. sys.stdout.write("\033[F")
  110. previous_data = []
  111. error_data = [0.]
  112. for id, data in enumerate(svd_data):
  113. current_data = data
  114. if not p_norm:
  115. current_data = current_data[begin_data:end_data]
  116. if p_mode == 'svdn':
  117. current_data = utils.normalize_arr(current_data)
  118. if p_mode == 'svdne':
  119. current_data = utils.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
  120. images_data.append(current_data)
  121. # use of whole image data for computation of ssim or psnr
  122. if p_error == 'ssim' or p_error == 'psnr':
  123. current_data = np.asarray(Image.open(images_path[id]))
  124. if len(previous_data) > 0:
  125. current_error = get_error_distance(p_error, previous_data, current_data)
  126. error_data.append(current_error)
  127. if len(previous_data) == 0:
  128. previous_data = current_data
  129. # display all data using matplotlib (configure plt)
  130. gridsize = (3, 2)
  131. # fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(30, 22))
  132. fig = plt.figure(figsize=(30, 22))
  133. ax1 = plt.subplot2grid(gridsize, (0, 0), colspan=2, rowspan=2)
  134. ax2 = plt.subplot2grid(gridsize, (2, 0), colspan=2)
  135. ax1.set_title(p_scene + ' scene interval information SVD['+ str(begin_data) +', '+ str(end_data) +'], from scenes indices [' + str(begin_index) + ', '+ str(end_index) + '], ' + p_feature + ' feature, ' + p_mode + ', with step of ' + str(p_step) + ', svd norm ' + str(p_norm), fontsize=20)
  136. ax1.set_ylabel('Image samples or time (minutes) generation', fontsize=14)
  137. ax1.set_xlabel('Vector features', fontsize=16)
  138. for id, data in enumerate(images_data):
  139. current_quality_image = dt.get_scene_image_quality(images_path[id])
  140. current_quality_postfix = dt.get_scene_image_postfix(images_path[id])
  141. if display_error:
  142. p_label = p_scene + '_' + current_quality_postfix + " | " + p_error + ": " + str(error_data[id])
  143. else:
  144. p_label = p_scene + '_' + current_quality_postfix
  145. if current_quality_image == threshold_image_zone:
  146. ax1.plot(data, label=p_label + " (threshold mean)", lw=4, color='red')
  147. else:
  148. ax1.plot(data, label=p_label)
  149. ax1.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2, fontsize=14)
  150. start_ylim, end_ylim = p_ylim
  151. ax1.set_ylim(start_ylim, end_ylim)
  152. ax2.set_title(p_error + " information for whole step images")
  153. ax2.set_ylabel(p_error + ' error')
  154. ax2.set_xlabel('Number of samples per pixels or times')
  155. ax2.set_xticks(range(len(current_quality_image)))
  156. ax2.set_xticklabels(list(map(dt.get_scene_image_quality, current_quality_image)))
  157. ax2.plot(error_data)
  158. plot_name = p_scene + '_' + p_feature + '_' + str(p_step) + '_' + p_mode + '_' + str(p_norm) + '.png'
  159. plt.savefig(plot_name)
  160. def main():
  161. parser = argparse.ArgumentParser(description="Display evolution of error on scene")
  162. parser.add_argument('--scene', type=str, help='scene index to use', choices=cfg.scenes_indices)
  163. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  164. parser.add_argument('--indices', type=str, help='Samples interval to display', default='"0, 900"')
  165. parser.add_argument('--feature', type=str, help='feature data choice', choices=features_choices)
  166. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=cfg.normalization_choices)
  167. parser.add_argument('--step', type=int, help='Each step samples to display', default=10)
  168. parser.add_argument('--norm', type=int, help='If values will be normalized or not', choices=[0, 1])
  169. parser.add_argument('--error', type=int, help='Way of computing error', choices=error_data_choices)
  170. parser.add_argument('--ylim', type=str, help='ylim interval to use', default='"0, 1"')
  171. args = parser.parse_args()
  172. p_scene = scenes_list[scenes_indices.index(args.scene)]
  173. p_indices = list(map(int, args.indices.split(',')))
  174. p_interval = list(map(int, args.interval.split(',')))
  175. p_feature = args.feature
  176. p_mode = args.mode
  177. p_step = args.step
  178. p_norm = args.norm
  179. p_error = args.error
  180. p_ylim = list(map(int, args.ylim.split(',')))
  181. display_svd_values(p_scene, p_interval, p_indices, p_feature, p_mode, p_step, p_norm, p_error, p_ylim)
  182. if __name__== "__main__":
  183. main()