noise_svd_visualization.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # main imports
  2. import sys, os, argparse
  3. # image processing imports
  4. from PIL import Image
  5. import matplotlib.pyplot as plt
  6. from ipfml import processing, utils
  7. # modules and config imports
  8. sys.path.insert(0, '') # trick to enable import of main folder module
  9. import custom_config as cfg
  10. from data_attributes import get_image_features
  11. # other variables
  12. noise_list = cfg.noise_labels
  13. generated_folder = cfg.generated_folder
  14. filename_ext = cfg.filename_ext
  15. feature_choices = cfg.features_choices_labels
  16. normalization_choices = cfg.normalization_choices
  17. pictures_folder = cfg.pictures_output_folder
  18. steparam_picture = 10
  19. def main():
  20. parser = argparse.ArgumentParser(description="Display svd of images with noise level")
  21. parser.add_argument('--prefix', type=str, help='Generated noise folder prefix (ex: `generated/prefix/noise`)')
  22. parser.add_argument('--mode', type=str, help='Kind of normalization', default=normalization_choices)
  23. parser.add_argument('--feature', type=str, help='feature choice', default=feature_choices)
  24. parser.add_argument('--n', type=int, help='Number of images')
  25. parser.add_argument('--color', type=int, help='Use of color or grey level', default=0)
  26. parser.add_argument('--norm', type=int, help='Use of normalization from interval or whole data vector', default=0)
  27. parser.add_argument('--interval', type=str, help='Interval data choice (ex: `0, 200`)', default="0, 200")
  28. parser.add_argument('--step', type=int, help='Step of image indices to keep', default=1)
  29. parser.add_argument('--ylim', type=str, help='Limite to display data (ex: `0, 1`)', default="0, 1")
  30. args = parser.parse_args()
  31. param_prefix = args.prefix
  32. param_mode = args.mode
  33. param_feature = args.feature
  34. param_n = args.n
  35. param_color = args.color
  36. param_norm = args.norm
  37. param_interval = list(map(int, args.interval.split(',')))
  38. param_step = args.step
  39. param_ylim = list(map(float, args.ylim.split(',')))
  40. param_prefix = param_prefix.split('/')[1].replace('_', '')
  41. noise_name = param_prefix.split('/')[2]
  42. if param_color:
  43. file_path = param_prefix + "/" + param_prefix + "_" + noise_name + "_color_{}." + filename_ext
  44. else:
  45. file_path = param_prefix + "/" + param_prefix + "_" + noise_name + "_{}." + filename_ext
  46. begin, end = param_interval
  47. all_svd_data = []
  48. svd_data = []
  49. image_indices = []
  50. # get all data from images
  51. for i in range(1, param_n):
  52. if i % steparam_picture == 0:
  53. image_path = file_path.format(str(i))
  54. img = Image.open(image_path)
  55. svd_values = get_image_features(param_feature, img)
  56. if param_norm:
  57. svd_values = svd_values[begin:end]
  58. all_svd_data.append(svd_values)
  59. # update min max values
  60. min_value = svd_values.min()
  61. max_value = svd_values.max()
  62. if min_value < min_value_svd:
  63. min_value_svd = min_value
  64. if max_value > max_value_svd:
  65. max_value_svd = max_value
  66. print('%.2f%%' % ((i + 1) / param_n * 100))
  67. sys.stdout.write("\033[F")
  68. for id, data in enumerate(all_svd_data):
  69. if (id * steparam_picture) % param_step == 0:
  70. current_data = data
  71. if param_mode == 'svdn':
  72. current_data = utils.normalize_arr(current_data)
  73. if param_mode == 'svdne':
  74. current_data = utils.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)
  75. svd_data.append(current_data)
  76. image_indices.append(str(id * steparam_picture))
  77. # display all data using matplotlib (configure plt)
  78. plt.rcParams['figure.figsize'] = (25, 18)
  79. plt.title(param_prefix + ' noise, interval information ['+ str(begin) +', '+ str(end) +'], ' + param_feature + ' feature, step ' + str(param_step) + ' normalization ' + param_mode, fontsize=20)
  80. plt.ylabel('Importance of noise [1, 999]', fontsize=14)
  81. plt.xlabel('Vector features', fontsize=16)
  82. for id, data in enumerate(svd_data):
  83. param_label = param_prefix + str(image_indices[id])
  84. plt.plot(data, label=param_label)
  85. plt.legend(bbox_to_anchor=(0.8, 1), loc=2, borderaxespad=0.2, fontsize=14)
  86. if not param_norm:
  87. plt.xlim(begin, end)
  88. # adapt ylim
  89. y_begin, y_end = param_ylim
  90. plt.ylim(y_begin, y_end)
  91. output_filename = param_prefix + "_" + noise_name + "_1_to_" + str(param_n) + "_B" + str(begin) + "_E" + str(end) + "_" + param_feature + "_S" + str(param_step) + "_norm" + str(param_norm )+ "_" + param_mode
  92. if param_color:
  93. output_filename = output_filename + '_color'
  94. print("Generation of output figure... %s" % output_filename)
  95. output_path = os.path.join(pictures_folder, output_filename)
  96. if not os.path.exists(pictures_folder):
  97. os.makedirs(pictures_folder)
  98. plt.savefig(output_path, dpi=(200))
  99. if __name__== "__main__":
  100. main()