predict_seuil_expe_curve.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. from sklearn.externals import joblib
  2. import numpy as np
  3. from ipfml import processing
  4. from PIL import Image
  5. import sys, os, argparse
  6. import subprocess
  7. import time
  8. from modules.utils import config as cfg
  9. from modules.utils import data as dt
  10. config_filename = cfg.config_filename
  11. scenes_path = cfg.dataset_path
  12. min_max_filename = cfg.min_max_filename_extension
  13. threshold_expe_filename = cfg.seuil_expe_filename
  14. threshold_map_folder = cfg.threshold_map_folder
  15. threshold_map_file_prefix = cfg.threshold_map_folder + "_"
  16. zones = cfg.zones_indices
  17. maxwell_scenes = cfg.maxwell_scenes_names
  18. normalization_choices = cfg.normalization_choices
  19. metric_choices = cfg.metric_choices_labels
  20. simulation_curves_zones = "simulation_curves_zones_"
  21. tmp_filename = '/tmp/__model__img_to_predict.png'
  22. current_dirpath = os.getcwd()
  23. def main():
  24. parser = argparse.ArgumentParser(description="Script which predicts threshold using specific keras model")
  25. parser.add_argument('--metrics', type=str,
  26. help="list of metrics choice in order to compute data",
  27. default='svd_reconstruction, ipca_reconstruction',
  28. required=True)
  29. parser.add_argument('--params', type=str,
  30. help="list of specific param for each metric choice (See README.md for further information in 3D mode)",
  31. default='100, 200 :: 50, 25',
  32. required=True)
  33. parser.add_argument('--model', type=str, help='.json file of keras model', required=True)
  34. parser.add_argument('--renderer', type=str,
  35. help='Renderer choice in order to limit scenes used',
  36. choices=cfg.renderer_choices,
  37. default='all',
  38. required=True)
  39. args = parser.parse_args()
  40. p_metrics = list(map(str.strip, args.metrics.split(',')))
  41. p_params = list(map(str.strip, args.params.split('::')))
  42. p_model_file = args.model
  43. p_renderer = args.renderer
  44. scenes_list = dt.get_renderer_scenes_names(p_renderer)
  45. scenes = os.listdir(scenes_path)
  46. print(scenes)
  47. # go ahead each scenes
  48. for id_scene, folder_scene in enumerate(scenes):
  49. # only take in consideration renderer scenes
  50. if folder_scene in scenes_list:
  51. print(folder_scene)
  52. scene_path = os.path.join(scenes_path, folder_scene)
  53. config_path = os.path.join(scene_path, config_filename)
  54. with open(config_path, "r") as config_file:
  55. last_image_name = config_file.readline().strip()
  56. prefix_image_name = config_file.readline().strip()
  57. start_index_image = config_file.readline().strip()
  58. end_index_image = config_file.readline().strip()
  59. step_counter = int(config_file.readline().strip())
  60. threshold_expes = []
  61. threshold_expes_found = []
  62. block_predictions_str = []
  63. # get zones list info
  64. for index in zones:
  65. index_str = str(index)
  66. if len(index_str) < 2:
  67. index_str = "0" + index_str
  68. zone_folder = "zone"+index_str
  69. threshold_path_file = os.path.join(os.path.join(scene_path, zone_folder), threshold_expe_filename)
  70. with open(threshold_path_file) as f:
  71. threshold = int(f.readline())
  72. threshold_expes.append(threshold)
  73. # Initialize default data to get detected model threshold found
  74. threshold_expes_found.append(int(end_index_image)) # by default use max
  75. block_predictions_str.append(index_str + ";" + p_model_file + ";" + str(threshold) + ";" + str(start_index_image) + ";" + str(step_counter))
  76. current_counter_index = int(start_index_image)
  77. end_counter_index = int(end_index_image)
  78. print(current_counter_index)
  79. while(current_counter_index <= end_counter_index):
  80. current_counter_index_str = str(current_counter_index)
  81. while len(start_index_image) > len(current_counter_index_str):
  82. current_counter_index_str = "0" + current_counter_index_str
  83. img_path = os.path.join(scene_path, prefix_image_name + current_counter_index_str + ".png")
  84. current_img = Image.open(img_path)
  85. img_blocks = processing.divide_in_blocks(current_img, cfg.keras_img_size)
  86. for id_block, block in enumerate(img_blocks):
  87. # check only if necessary for this scene (not already detected)
  88. #if not threshold_expes_detected[id_block]:
  89. tmp_file_path = tmp_filename.replace('__model__', p_model_file.split('/')[-1].replace('.json', '_'))
  90. block.save(tmp_file_path)
  91. python_cmd = "python predict_noisy_image.py --image " + tmp_file_path + \
  92. " --metrics " + p_metrics + \
  93. " --params " + p_params + \
  94. " --model " + p_model_file
  95. ## call command ##
  96. p = subprocess.Popen(python_cmd, stdout=subprocess.PIPE, shell=True)
  97. (output, err) = p.communicate()
  98. ## Wait for result ##
  99. p_status = p.wait()
  100. prediction = int(output)
  101. # save here in specific file of block all the predictions done
  102. block_predictions_str[id_block] = block_predictions_str[id_block] + ";" + str(prediction)
  103. print(str(id_block) + " : " + str(current_counter_index) + "/" + str(threshold_expes[id_block]) + " => " + str(prediction))
  104. current_counter_index += step_counter
  105. print("------------------------")
  106. print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)))
  107. print("------------------------")
  108. # end of scene => display of results
  109. # construct path using model name for saving threshold map folder
  110. model_threshold_path = os.path.join(threshold_map_folder, p_model_file.split('/')[-1].replace('.joblib', ''))
  111. # create threshold model path if necessary
  112. if not os.path.exists(model_threshold_path):
  113. os.makedirs(model_threshold_path)
  114. map_filename = os.path.join(model_threshold_path, simulation_curves_zones + folder_scene)
  115. f_map = open(map_filename, 'w')
  116. for line in block_predictions_str:
  117. f_map.write(line + '\n')
  118. f_map.close()
  119. print("Scene " + str(id_scene + 1) + "/" + str(len(maxwell_scenes)) + " Done..")
  120. print("------------------------")
  121. print("Model predictions are saved into %s" % map_filename)
  122. time.sleep(2)
  123. if __name__== "__main__":
  124. main()