predict_seuil_expe_maxwell_curve.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. config_filename = cfg.config_filename
  10. scenes_path = cfg.dataset_path
  11. min_max_filename = cfg.min_max_filename_extension
  12. threshold_expe_filename = cfg.seuil_expe_filename
  13. threshold_map_folder = cfg.threshold_map_folder
  14. threshold_map_file_prefix = cfg.threshold_map_folder + "_"
  15. zones = cfg.zones_indices
  16. maxwell_scenes = cfg.maxwell_scenes_names
  17. normalization_choices = cfg.normalization_choices
  18. metric_choices = cfg.metric_choices_labels
  19. simulation_curves_zones = "simulation_curves_zones_"
  20. tmp_filename = '/tmp/__model__img_to_predict.png'
  21. current_dirpath = os.getcwd()
  22. def main():
  23. p_custom = False
  24. parser = argparse.ArgumentParser(description="Script which predicts threshold using specific model")
  25. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  26. parser.add_argument('--model', type=str, help='.joblib or .json file (sklearn or keras model)')
  27. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=normalization_choices)
  28. parser.add_argument('--metric', type=str, help='Metric data choice', choices=metric_choices)
  29. #parser.add_argument('--limit_detection', type=int, help='Specify number of same prediction to stop threshold prediction', default=2)
  30. parser.add_argument('--custom', type=str, help='Name of custom min max file if use of renormalization of data', default=False)
  31. args = parser.parse_args()
  32. # keep p_interval as it is
  33. p_interval = args.interval
  34. p_model_file = args.model
  35. p_mode = args.mode
  36. p_metric = args.metric
  37. #p_limit = args.limit
  38. p_custom = args.custom
  39. scenes = os.listdir(scenes_path)
  40. scenes = [s for s in scenes if s in maxwell_scenes]
  41. print(scenes)
  42. # go ahead each scenes
  43. for id_scene, folder_scene in enumerate(scenes):
  44. # only take in consideration maxwell scenes
  45. if folder_scene in maxwell_scenes:
  46. print(folder_scene)
  47. scene_path = os.path.join(scenes_path, folder_scene)
  48. config_path = os.path.join(scene_path, config_filename)
  49. with open(config_path, "r") as config_file:
  50. last_image_name = config_file.readline().strip()
  51. prefix_image_name = config_file.readline().strip()
  52. start_index_image = config_file.readline().strip()
  53. end_index_image = config_file.readline().strip()
  54. step_counter = int(config_file.readline().strip())
  55. threshold_expes = []
  56. threshold_expes_found = []
  57. block_predictions_str = []
  58. # get zones list info
  59. for index in zones:
  60. index_str = str(index)
  61. if len(index_str) < 2:
  62. index_str = "0" + index_str
  63. zone_folder = "zone"+index_str
  64. threshold_path_file = os.path.join(os.path.join(scene_path, zone_folder), threshold_expe_filename)
  65. with open(threshold_path_file) as f:
  66. threshold = int(f.readline())
  67. threshold_expes.append(threshold)
  68. # Initialize default data to get detected model threshold found
  69. threshold_expes_found.append(int(end_index_image)) # by default use max
  70. block_predictions_str.append(index_str + ";" + p_model_file + ";" + str(threshold) + ";" + str(start_index_image) + ";" + str(step_counter))
  71. current_counter_index = int(start_index_image)
  72. end_counter_index = int(end_index_image)
  73. print(current_counter_index)
  74. while(current_counter_index <= end_counter_index):
  75. current_counter_index_str = str(current_counter_index)
  76. while len(start_index_image) > len(current_counter_index_str):
  77. current_counter_index_str = "0" + current_counter_index_str
  78. img_path = os.path.join(scene_path, prefix_image_name + current_counter_index_str + ".png")
  79. current_img = Image.open(img_path)
  80. img_blocks = processing.divide_in_blocks(current_img, (200, 200))
  81. for id_block, block in enumerate(img_blocks):
  82. # check only if necessary for this scene (not already detected)
  83. #if not threshold_expes_detected[id_block]:
  84. tmp_file_path = tmp_filename.replace('__model__', p_model_file.split('/')[-1].replace('.joblib', '_'))
  85. block.save(tmp_file_path)
  86. python_cmd_line = "python predict_noisy_image_svd.py --image {0} --interval '{1}' --model {2} --mode {3} --metric {4}"
  87. python_cmd = python_cmd_line.format(tmp_file_path, p_interval, p_model_file, p_mode, p_metric)
  88. # specify use of custom file for min max normalization
  89. if p_custom:
  90. python_cmd = python_cmd + ' --custom ' + p_custom
  91. ## call command ##
  92. p = subprocess.Popen(python_cmd, stdout=subprocess.PIPE, shell=True)
  93. (output, err) = p.communicate()
  94. ## Wait for result ##
  95. p_status = p.wait()
  96. prediction = int(output)
  97. # save here in specific file of block all the predictions done
  98. block_predictions_str[id_block] = block_predictions_str[id_block] + ";" + str(prediction)
  99. print(str(id_block) + " : " + str(current_counter_index) + "/" + str(threshold_expes[id_block]) + " => " + str(prediction))
  100. current_counter_index += step_counter
  101. print("------------------------")
  102. print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)))
  103. print("------------------------")
  104. # end of scene => display of results
  105. # construct path using model name for saving threshold map folder
  106. model_threshold_path = os.path.join(threshold_map_folder, p_model_file.split('/')[-1].replace('.joblib', ''))
  107. # create threshold model path if necessary
  108. if not os.path.exists(model_threshold_path):
  109. os.makedirs(model_threshold_path)
  110. map_filename = os.path.join(model_threshold_path, simulation_curves_zones + folder_scene)
  111. f_map = open(map_filename, 'w')
  112. for line in block_predictions_str:
  113. f_map.write(line + '\n')
  114. f_map.close()
  115. print("Scene " + str(id_scene + 1) + "/" + str(len(maxwell_scenes)) + " Done..")
  116. print("------------------------")
  117. print("Model predictions are saved into %s" % map_filename)
  118. time.sleep(10)
  119. if __name__== "__main__":
  120. main()