display_svd_data_error_scene.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Sep 14 21:02:42 2018
  5. @author: jbuisine
  6. """
  7. from __future__ import print_function
  8. import sys, os, getopt
  9. import numpy as np
  10. import random
  11. import time
  12. import json
  13. from PIL import Image
  14. from ipfml import processing, metrics, utils
  15. import ipfml.iqa.fr as fr_iqa
  16. from skimage import color
  17. import matplotlib.pyplot as plt
  18. from modules.utils.data import get_svd_data
  19. from modules.utils import config as cfg
  20. # getting configuration information
  21. config_filename = cfg.config_filename
  22. zone_folder = cfg.zone_folder
  23. min_max_filename = cfg.min_max_filename_extension
  24. # define all scenes values
  25. scenes_list = cfg.scenes_names
  26. scenes_indices = cfg.scenes_indices
  27. choices = cfg.normalization_choices
  28. path = cfg.dataset_path
  29. zones = cfg.zones_indices
  30. seuil_expe_filename = cfg.seuil_expe_filename
  31. metric_choices = cfg.metric_choices_labels
  32. max_nb_bits = 8
  33. display_error = False
  34. error_data_choices = ['mae', 'mse', 'ssim', 'psnr']
  35. def get_error_distance(p_error, y_true, y_test):
  36. noise_method = None
  37. function_name = p_error
  38. try:
  39. error_method = getattr(fr_iqa, function_name)
  40. except AttributeError:
  41. raise NotImplementedError("Error `{}` not implement `{}`".format(fr_iqa.__name__, function_name))
  42. return error_method(y_true, y_test)
  43. def display_svd_values(p_scene, p_interval, p_indices, p_metric, p_mode, p_step, p_norm, p_error, p_ylim):
  44. """
  45. @brief Method which gives information about svd curves from zone of picture
  46. @param p_scene, scene expected to show svd values
  47. @param p_interval, interval [begin, end] of svd data to display
  48. @param p_interval, interval [begin, end] of samples or minutes from render generation engine
  49. @param p_metric, metric computed to show
  50. @param p_mode, normalization's mode
  51. @param p_norm, normalization or not of selected svd data
  52. @param p_error, error metric used to display
  53. @param p_ylim, ylim choice to better display of data
  54. @return nothing
  55. """
  56. max_value_svd = 0
  57. min_value_svd = sys.maxsize
  58. image_indices = []
  59. scenes = os.listdir(path)
  60. # remove min max file from scenes folder
  61. scenes = [s for s in scenes if min_max_filename not in s]
  62. begin_data, end_data = p_interval
  63. begin_index, end_index = p_indices
  64. data_min_max_filename = os.path.join(path, p_metric + min_max_filename)
  65. # go ahead each scenes
  66. for id_scene, folder_scene in enumerate(scenes):
  67. if p_scene == folder_scene:
  68. scene_path = os.path.join(path, folder_scene)
  69. config_file_path = os.path.join(scene_path, config_filename)
  70. with open(config_file_path, "r") as config_file:
  71. last_image_name = config_file.readline().strip()
  72. prefix_image_name = config_file.readline().strip()
  73. start_index_image = config_file.readline().strip()
  74. end_index_image = config_file.readline().strip()
  75. step_counter = int(config_file.readline().strip())
  76. # construct each zones folder name
  77. zones_folder = []
  78. # get zones list info
  79. for index in zones:
  80. index_str = str(index)
  81. if len(index_str) < 2:
  82. index_str = "0" + index_str
  83. current_zone = "zone"+index_str
  84. zones_folder.append(current_zone)
  85. images_data = []
  86. images_indices = []
  87. threshold_learned_zones = []
  88. for id, zone_folder in enumerate(zones_folder):
  89. # get threshold information
  90. zone_path = os.path.join(scene_path, zone_folder)
  91. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  92. # open treshold path and get this information
  93. with open(path_seuil, "r") as seuil_file:
  94. threshold_learned = int(seuil_file.readline().strip())
  95. threshold_learned_zones.append(threshold_learned)
  96. current_counter_index = int(start_index_image)
  97. end_counter_index = int(end_index_image)
  98. threshold_mean = np.mean(np.asarray(threshold_learned_zones))
  99. threshold_image_found = False
  100. file_path = os.path.join(scene_path, prefix_image_name + "{}.png")
  101. svd_data = []
  102. while(current_counter_index <= end_counter_index):
  103. current_counter_index_str = str(current_counter_index)
  104. while len(start_index_image) > len(current_counter_index_str):
  105. current_counter_index_str = "0" + current_counter_index_str
  106. image_path = file_path.format(str(current_counter_index_str))
  107. img = Image.open(image_path)
  108. svd_values = get_svd_data(p_metric, img)
  109. if p_norm:
  110. svd_values = svd_values[begin_data:end_data]
  111. # update min max values
  112. min_value = svd_values.min()
  113. max_value = svd_values.max()
  114. if min_value < min_value_svd:
  115. min_value_svd = min_value
  116. if max_value > min_value_svd:
  117. max_value_svd = max_value
  118. # keep in memory used data
  119. if current_counter_index % p_step == 0:
  120. if current_counter_index >= begin_index and current_counter_index <= end_index:
  121. images_indices.append(current_counter_index_str)
  122. svd_data.append(svd_values)
  123. if threshold_mean < int(current_counter_index) and not threshold_image_found:
  124. threshold_image_found = True
  125. threshold_image_zone = current_counter_index_str
  126. current_counter_index += step_counter
  127. print('%.2f%%' % (current_counter_index / end_counter_index * 100))
  128. sys.stdout.write("\033[F")
  129. # all indices of picture to plot
  130. print(images_indices)
  131. previous_data = []
  132. error_data = [0.]
  133. for id, data in enumerate(svd_data):
  134. current_data = data
  135. if not p_norm:
  136. current_data = current_data[begin_data:end_data]
  137. if p_mode == 'svdn':
  138. current_data = utils.normalize_arr(current_data)
  139. if p_mode == 'svdne':
  140. current_data = utils.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
  141. images_data.append(current_data)
  142. # use of whole image data for computation of ssim or psnr
  143. if p_error == 'ssim' or p_error == 'psnr':
  144. image_path = file_path.format(str(current_id))
  145. current_data = np.asarray(Image.open(image_path))
  146. if len(previous_data) > 0:
  147. current_error = get_error_distance(p_error, previous_data, current_data)
  148. error_data.append(current_error)
  149. if len(previous_data) == 0:
  150. previous_data = current_data
  151. # display all data using matplotlib (configure plt)
  152. gridsize = (3, 2)
  153. # fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(30, 22))
  154. fig = plt.figure(figsize=(30, 22))
  155. ax1 = plt.subplot2grid(gridsize, (0, 0), colspan=2, rowspan=2)
  156. ax2 = plt.subplot2grid(gridsize, (2, 0), colspan=2)
  157. 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_metric + ' metric, ' + p_mode + ', with step of ' + str(p_step) + ', svd norm ' + str(p_norm), fontsize=20)
  158. ax1.set_ylabel('Image samples or time (minutes) generation', fontsize=14)
  159. ax1.set_xlabel('Vector features', fontsize=16)
  160. for id, data in enumerate(images_data):
  161. if display_error:
  162. p_label = p_scene + '_' + str(images_indices[id]) + " | " + p_error + ": " + str(error_data[id])
  163. else:
  164. p_label = p_scene + '_' + str(images_indices[id])
  165. if images_indices[id] == threshold_image_zone:
  166. ax1.plot(data, label=p_label + " (threshold mean)", lw=4, color='red')
  167. else:
  168. ax1.plot(data, label=p_label)
  169. ax1.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2, fontsize=14)
  170. start_ylim, end_ylim = p_ylim
  171. ax1.set_ylim(start_ylim, end_ylim)
  172. ax2.set_title(p_error + " information for whole step images")
  173. ax2.set_ylabel(p_error + ' error')
  174. ax2.set_xlabel('Number of samples per pixels or times')
  175. ax2.set_xticks(range(len(images_indices)))
  176. ax2.set_xticklabels(list(map(int, images_indices)))
  177. ax2.plot(error_data)
  178. plot_name = p_scene + '_' + p_metric + '_' + str(p_step) + '_' + p_mode + '_' + str(p_norm) + '.png'
  179. plt.savefig(plot_name)
  180. def main():
  181. # by default p_step value is 10 to enable all photos
  182. p_step = 10
  183. p_ylim = (0, 1)
  184. if len(sys.argv) <= 1:
  185. print('Run with default parameters...')
  186. print('python display_svd_data_scene.py --scene A --interval "0,800" --indices "0, 900" --metric lab --mode svdne --step 50 --norm 0 --error mae --ylim "0, 0.1"')
  187. sys.exit(2)
  188. try:
  189. opts, args = getopt.getopt(sys.argv[1:], "hs:i:i:z:l:m:s:n:e:y", ["help=", "scene=", "interval=", "indices=", "metric=", "mode=", "step=", "norm=", "error=", "ylim="])
  190. except getopt.GetoptError:
  191. # print help information and exit:
  192. print('python display_svd_data_scene.py --scene A --interval "0,800" --indices "0, 900" --metric lab --mode svdne --step 50 --norm 0 --error mae --ylim "0, 0.1"')
  193. sys.exit(2)
  194. for o, a in opts:
  195. if o == "-h":
  196. print('python display_svd_data_scene.py --scene A --interval "0,800" --indices "0, 900" --metric lab --mode svdne --step 50 --norm 0 --error mae --ylim "0, 0.1"')
  197. sys.exit()
  198. elif o in ("-s", "--scene"):
  199. p_scene = a
  200. if p_scene not in scenes_indices:
  201. assert False, "Invalid scene choice"
  202. else:
  203. p_scene = scenes_list[scenes_indices.index(p_scene)]
  204. elif o in ("-i", "--interval"):
  205. p_interval = list(map(int, a.split(',')))
  206. elif o in ("-i", "--indices"):
  207. p_indices = list(map(int, a.split(',')))
  208. elif o in ("-m", "--metric"):
  209. p_metric = a
  210. if p_metric not in metric_choices:
  211. assert False, "Invalid metric choice"
  212. elif o in ("-m", "--mode"):
  213. p_mode = a
  214. if p_mode not in choices:
  215. assert False, "Invalid normalization choice, expected ['svd', 'svdn', 'svdne']"
  216. elif o in ("-s", "--step"):
  217. p_step = int(a)
  218. elif o in ("-n", "--norm"):
  219. p_norm = int(a)
  220. elif o in ("-e", "--error"):
  221. p_error = a
  222. elif o in ("-y", "--ylim"):
  223. p_ylim = list(map(float, a.split(',')))
  224. else:
  225. assert False, "unhandled option"
  226. display_svd_values(p_scene, p_interval, p_indices, p_metric, p_mode, p_step, p_norm, p_error, p_ylim)
  227. if __name__== "__main__":
  228. main()