predict_noisy_image_svd.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, getopt
  6. from modules.utils import config as cfg
  7. from modules.utils import data_type as dt
  8. path = cfg.dataset_path
  9. min_max_ext = cfg.min_max_filename_extension
  10. metric_choices = cfg.metric_choices_labels
  11. normalization_choices = cfg.normalization_choices
  12. custom_min_max_folder = cfg.min_max_custom_folder
  13. def main():
  14. p_custom = False
  15. if len(sys.argv) <= 1:
  16. print('Run with default parameters...')
  17. print('python predict_noisy_image_svd.py --image path/to/xxxx --interval "0,20" --model path/to/xxxx.joblib --metric lab --mode ["svdn", "svdne"] --custom min_max_file')
  18. sys.exit(2)
  19. try:
  20. opts, args = getopt.getopt(sys.argv[1:], "hi:t:m:m:o:c", ["help=", "image=", "interval=", "model=", "metric=", "mode=", "custom="])
  21. except getopt.GetoptError:
  22. # print help information and exit
  23. print('python predict_noisy_image_svd_lab.py --image path/to/xxxx --interval "xx,xx" --model path/to/xxxx.joblib --metric lab --mode ["svdn", "svdne"] --custom min_max_file')
  24. sys.exit(2)
  25. for o, a in opts:
  26. if o == "-h":
  27. print('python predict_noisy_image_svd_lab.py --image path/to/xxxx --interval "xx,xx" --model path/to/xxxx.joblib --metric lab --mode ["svdn", "svdne"] --custom min_max_file')
  28. sys.exit()
  29. elif o in ("-i", "--image"):
  30. p_img_file = os.path.join(os.path.dirname(__file__), a)
  31. elif o in ("-t", "--interval"):
  32. p_interval = list(map(int, a.split(',')))
  33. elif o in ("-m", "--model"):
  34. p_model_file = os.path.join(os.path.dirname(__file__), a)
  35. elif o in ("-m", "--metric"):
  36. p_metric = a
  37. if not p_metric in metric_choices:
  38. assert False, "Unknow metric choice"
  39. elif o in ("-o", "--mode"):
  40. p_mode = a
  41. if not p_mode in normalization_choices:
  42. assert False, "Mode of normalization not recognized"
  43. elif o in ("-m", "--custom"):
  44. p_custom = a
  45. else:
  46. assert False, "unhandled option"
  47. # load of model file
  48. model = joblib.load(p_model_file)
  49. # load image
  50. img = Image.open(p_img_file)
  51. data = dt.get_svd_data(p_metric, img)
  52. # check mode to normalize data
  53. if p_mode == 'svdne':
  54. # set min_max_filename if custom use
  55. if p_custom:
  56. min_max_filename = custom_min_max_folder + '/' + p_custom
  57. else:
  58. min_max_file_path = path + '/' + p_metric + min_max_ext
  59. # need to read min_max_file
  60. file_path = os.path.join(os.path.join(os.path.dirname(__file__),'../'), min_max_file_path)
  61. with open(file_path, 'r') as f:
  62. min = float(f.readline().replace('\n', ''))
  63. max = float(f.readline().replace('\n', ''))
  64. l_values = processing.normalize_arr_with_range(data, min, max)
  65. elif p_mode == 'svdn':
  66. l_values = processing.normalize_arr(data)
  67. else:
  68. l_values = data
  69. # get interval values
  70. begin, end = p_interval
  71. test_data = l_values[begin:end]
  72. # get prediction of model
  73. prediction = model.predict([test_data])[0]
  74. # output expected from others scripts
  75. print(prediction)
  76. if __name__== "__main__":
  77. main()