predict_seuil_expe.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. from sklearn.externals import joblib
  2. import numpy as np
  3. from ipfml import processing, utils
  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. normalization_choices = cfg.normalization_choices
  17. metric_choices = cfg.metric_choices_labels
  18. tmp_filename = '/tmp/__model__img_to_predict.png'
  19. current_dirpath = os.getcwd()
  20. def main():
  21. p_custom = False
  22. parser = argparse.ArgumentParser(description="Script which predicts threshold using specific model")
  23. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  24. parser.add_argument('--model', type=str, help='.joblib or .json file (sklearn or keras model)')
  25. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=normalization_choices)
  26. parser.add_argument('--metric', type=str, help='Metric data choice', choices=metric_choices)
  27. #parser.add_argument('--limit_detection', type=int, help='Specify number of same prediction to stop threshold prediction', default=2)
  28. parser.add_argument('--custom', type=str, help='Name of custom min max file if use of renormalization of data', default=False)
  29. args = parser.parse_args()
  30. p_interval = list(map(int, args.interval.split(',')))
  31. p_model_file = args.model
  32. p_mode = args.mode
  33. p_metric = args.metric
  34. #p_limit = args.limit
  35. p_custom = args.custom
  36. scenes = os.listdir(scenes_path)
  37. scenes = [s for s in scenes if not min_max_filename in s]
  38. # go ahead each scenes
  39. for id_scene, folder_scene in enumerate(scenes):
  40. print(folder_scene)
  41. scene_path = os.path.join(scenes_path, folder_scene)
  42. config_path = os.path.join(scene_path, config_filename)
  43. with open(config_path, "r") as config_file:
  44. last_image_name = config_file.readline().strip()
  45. prefix_image_name = config_file.readline().strip()
  46. start_index_image = config_file.readline().strip()
  47. end_index_image = config_file.readline().strip()
  48. step_counter = int(config_file.readline().strip())
  49. threshold_expes = []
  50. threshold_expes_detected = []
  51. threshold_expes_counter = []
  52. threshold_expes_found = []
  53. # get zones list info
  54. for index in zones:
  55. index_str = str(index)
  56. if len(index_str) < 2:
  57. index_str = "0" + index_str
  58. zone_folder = "zone"+index_str
  59. threshold_path_file = os.path.join(os.path.join(scene_path, zone_folder), threshold_expe_filename)
  60. with open(threshold_path_file) as f:
  61. threshold = int(f.readline())
  62. threshold_expes.append(threshold)
  63. # Initialize default data to get detected model threshold found
  64. threshold_expes_detected.append(False)
  65. threshold_expes_counter.append(0)
  66. threshold_expes_found.append(int(end_index_image)) # by default use max
  67. current_counter_index = int(start_index_image)
  68. end_counter_index = int(end_index_image)
  69. print(current_counter_index)
  70. check_all_done = False
  71. while(current_counter_index <= end_counter_index and not check_all_done):
  72. current_counter_index_str = str(current_counter_index)
  73. while len(start_index_image) > len(current_counter_index_str):
  74. current_counter_index_str = "0" + current_counter_index_str
  75. img_path = os.path.join(scene_path, prefix_image_name + current_counter_index_str + ".png")
  76. current_img = Image.open(img_path)
  77. img_blocks = processing.divide_in_blocks(current_img, (200, 200))
  78. check_all_done = all(d == True for d in threshold_expes_detected)
  79. for id_block, block in enumerate(img_blocks):
  80. # check only if necessary for this scene (not already detected)
  81. if not threshold_expes_detected[id_block]:
  82. tmp_file_path = tmp_filename.replace('__model__', p_model_file.split('/')[-1].replace('.joblib', '_'))
  83. block.save(tmp_file_path)
  84. python_cmd = "python predict_noisy_image_svd.py --image " + tmp_file_path + \
  85. " --interval '" + p_interval + \
  86. "' --model " + p_model_file + \
  87. " --mode " + p_mode + \
  88. " --metric " + p_metric
  89. # specify use of custom file for min max normalization
  90. if p_custom:
  91. python_cmd = python_cmd + ' --custom ' + p_custom
  92. ## call command ##
  93. p = subprocess.Popen(python_cmd, stdout=subprocess.PIPE, shell=True)
  94. (output, err) = p.communicate()
  95. ## Wait for result ##
  96. p_status = p.wait()
  97. prediction = int(output)
  98. if prediction == 0:
  99. threshold_expes_counter[id_block] = threshold_expes_counter[id_block] + 1
  100. else:
  101. threshold_expes_counter[id_block] = 0
  102. if threshold_expes_counter[id_block] == p_limit:
  103. threshold_expes_detected[id_block] = True
  104. threshold_expes_found[id_block] = current_counter_index
  105. print(str(id_block) + " : " + str(current_counter_index) + "/" + str(threshold_expes[id_block]) + " => " + str(prediction))
  106. current_counter_index += step_counter
  107. print("------------------------")
  108. print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)))
  109. print("------------------------")
  110. # end of scene => display of results
  111. # construct path using model name for saving threshold map folder
  112. model_treshold_path = os.path.join(threshold_map_folder, p_model_file.split('/')[-1].replace('.joblib', ''))
  113. # create threshold model path if necessary
  114. if not os.path.exists(model_treshold_path):
  115. os.makedirs(model_treshold_path)
  116. abs_dist = []
  117. map_filename = os.path.join(model_treshold_path, threshold_map_file_prefix + folder_scene)
  118. f_map = open(map_filename, 'w')
  119. line_information = ""
  120. # default header
  121. f_map.write('| | | | |\n')
  122. f_map.write('---|----|----|---\n')
  123. for id, threshold in enumerate(threshold_expes_found):
  124. line_information += str(threshold) + " / " + str(threshold_expes[id]) + " | "
  125. abs_dist.append(abs(threshold - threshold_expes[id]))
  126. if (id + 1) % 4 == 0:
  127. f_map.write(line_information + '\n')
  128. line_information = ""
  129. f_map.write(line_information + '\n')
  130. min_abs_dist = min(abs_dist)
  131. max_abs_dist = max(abs_dist)
  132. avg_abs_dist = sum(abs_dist) / len(abs_dist)
  133. f_map.write('\nScene information : ')
  134. f_map.write('\n- BEGIN : ' + str(start_index_image))
  135. f_map.write('\n- END : ' + str(end_index_image))
  136. f_map.write('\n\nDistances information : ')
  137. f_map.write('\n- MIN : ' + str(min_abs_dist))
  138. f_map.write('\n- MAX : ' + str(max_abs_dist))
  139. f_map.write('\n- AVG : ' + str(avg_abs_dist))
  140. f_map.write('\n\nOther information : ')
  141. f_map.write('\n- Detection limit : ' + str(p_limit))
  142. # by default print last line
  143. f_map.close()
  144. print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)) + " Done..")
  145. print("------------------------")
  146. time.sleep(10)
  147. if __name__== "__main__":
  148. main()