predict_noisy_image_svd.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # main imports
  2. import sys, os, argparse, json
  3. import numpy as np
  4. # models imports
  5. from keras.models import model_from_json
  6. from sklearn.externals import joblib
  7. # image processing imports
  8. from ipfml import processing, utils
  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. from data_attributes import get_svd_data
  14. # variables and parameters
  15. path = cfg.dataset_path
  16. min_max_ext = cfg.min_max_filename_extension
  17. features_choices = cfg.features_choices_labels
  18. normalization_choices = cfg.normalization_choices
  19. custom_min_max_folder = cfg.min_max_custom_folder
  20. def main():
  21. # getting all params
  22. parser = argparse.ArgumentParser(description="Script which detects if an image is noisy or not using specific model")
  23. parser.add_argument('--image', type=str, help='Image path')
  24. parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
  25. parser.add_argument('--model', type=str, help='.joblib or .json file (sklearn or keras model)')
  26. parser.add_argument('--mode', type=str, help='Kind of normalization level wished', choices=normalization_choices)
  27. parser.add_argument('--feature', type=str, help='feature data choice', choices=features_choices)
  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_img_file = args.image
  31. p_model_file = args.model
  32. p_interval = list(map(int, args.interval.split(',')))
  33. p_mode = args.mode
  34. p_feature = args.feature
  35. p_custom = args.custom
  36. if '.joblib' in p_model_file:
  37. kind_model = 'sklearn'
  38. if '.json' in p_model_file:
  39. kind_model = 'keras'
  40. if 'corr' in p_model_file:
  41. corr_model = True
  42. indices_corr_path = os.path.join(cfg.correlation_indices_folder, p_model_file.split('/')[1].replace('.json', '').replace('.joblib', '') + '.csv')
  43. with open(indices_corr_path, 'r') as f:
  44. data_corr_indices = [int(x) for x in f.readline().split(';') if x != '']
  45. else:
  46. corr_model = False
  47. if kind_model == 'sklearn':
  48. # load of model file
  49. model = joblib.load(p_model_file)
  50. if kind_model == 'keras':
  51. with open(p_model_file, 'r') as f:
  52. json_model = json.load(f)
  53. model = model_from_json(json_model)
  54. model.load_weights(p_model_file.replace('.json', '.h5'))
  55. model.compile(loss='binary_crossentropy',
  56. optimizer='adam',
  57. features=['accuracy'])
  58. # load image
  59. img = Image.open(p_img_file)
  60. data = get_svd_data(p_feature, img)
  61. # get interval values
  62. begin, end = p_interval
  63. # check if custom min max file is used
  64. if p_custom:
  65. if corr_model:
  66. test_data = data[data_corr_indices]
  67. else:
  68. test_data = data[begin:end]
  69. if p_mode == 'svdne':
  70. # set min_max_filename if custom use
  71. min_max_file_path = custom_min_max_folder + '/' + p_custom
  72. # need to read min_max_file
  73. file_path = os.path.join(os.path.dirname(__file__), min_max_file_path)
  74. with open(file_path, 'r') as f:
  75. min_val = float(f.readline().replace('\n', ''))
  76. max_val = float(f.readline().replace('\n', ''))
  77. test_data = utils.normalize_arr_with_range(test_data, min_val, max_val)
  78. if p_mode == 'svdn':
  79. test_data = utils.normalize_arr(test_data)
  80. else:
  81. # check mode to normalize data
  82. if p_mode == 'svdne':
  83. # set min_max_filename if custom use
  84. min_max_file_path = path + '/' + p_feature + min_max_ext
  85. # need to read min_max_file
  86. file_path = os.path.join(os.path.dirname(__file__), min_max_file_path)
  87. with open(file_path, 'r') as f:
  88. min_val = float(f.readline().replace('\n', ''))
  89. max_val = float(f.readline().replace('\n', ''))
  90. l_values = utils.normalize_arr_with_range(data, min_val, max_val)
  91. elif p_mode == 'svdn':
  92. l_values = utils.normalize_arr(data)
  93. else:
  94. l_values = data
  95. if corr_model:
  96. test_data = data[data_corr_indices]
  97. else:
  98. test_data = data[begin:end]
  99. # get prediction of model
  100. if kind_model == 'sklearn':
  101. prediction = model.predict([test_data])[0]
  102. if kind_model == 'keras':
  103. test_data = np.asarray(test_data).reshape(1, len(test_data), 1)
  104. prediction = model.predict_classes([test_data])[0][0]
  105. # output expected from others scripts
  106. print(prediction)
  107. if __name__== "__main__":
  108. main()