save_model_result_in_md.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # main imports
  2. import numpy as np
  3. import sys, os, argparse
  4. import subprocess
  5. import time
  6. # models imports
  7. from sklearn.externals import joblib
  8. # image processing imports
  9. from PIL import Image
  10. # modules imports
  11. sys.path.insert(0, '') # trick to enable import of main folder module
  12. import custom_config as cfg
  13. # variables and parameters
  14. threshold_map_folder = cfg.threshold_map_folder
  15. threshold_map_file_prefix = cfg.threshold_map_folder + "_"
  16. markdowns_folder = cfg.models_information_folder
  17. zones = cfg.zones_indices
  18. current_dirpath = os.getcwd()
  19. def main():
  20. parser = argparse.ArgumentParser(description="Display SVD data of scene zone")
  21. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  22. parser.add_argument('--model', type=str, help='.joblib or .json file (sklearn or keras model)')
  23. parser.add_argument('--feature', type=str, help='Feature data choice', choices=cfg.features_choices_labels)
  24. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=cfg.normalization_choices)
  25. args = parser.parse_args()
  26. p_interval = list(map(int, args.interval.split(',')))
  27. p_model_file = args.model
  28. p_metric = args.metric
  29. p_mode = args.mode
  30. # call model and get global result in scenes
  31. begin, end = p_interval
  32. bash_cmd = "bash others/testModelByScene.sh '" + str(begin) + "' '" + str(end) + "' '" + p_model_file + "' '" + p_mode + "' '" + p_metric + "'"
  33. print(bash_cmd)
  34. ## call command ##
  35. p = subprocess.Popen(bash_cmd, stdout=subprocess.PIPE, shell=True)
  36. (output, err) = p.communicate()
  37. ## Wait for result ##
  38. p_status = p.wait()
  39. if not os.path.exists(markdowns_folder):
  40. os.makedirs(markdowns_folder)
  41. # get model name to construct model
  42. md_model_path = os.path.join(markdowns_folder, p_model_file.split('/')[-1].replace('.joblib', '.md'))
  43. with open(md_model_path, 'w') as f:
  44. f.write(output.decode("utf-8"))
  45. # read each threshold_map information if exists
  46. model_map_info_path = os.path.join(threshold_map_folder, p_model_file.replace('saved_models/', ''))
  47. if not os.path.exists(model_map_info_path):
  48. f.write('\n\n No threshold map information')
  49. else:
  50. maps_files = os.listdir(model_map_info_path)
  51. # get all map information
  52. for t_map_file in maps_files:
  53. file_path = os.path.join(model_map_info_path, t_map_file)
  54. with open(file_path, 'r') as map_file:
  55. title_scene = t_map_file.replace(threshold_map_file_prefix, '')
  56. f.write('\n\n## ' + title_scene + '\n')
  57. content = map_file.readlines()
  58. # getting each map line information
  59. for line in content:
  60. f.write(line)
  61. f.close()
  62. if __name__== "__main__":
  63. main()