predict_noisy_image_svd.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_image_features
  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. indices_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. selected_indices = [int(x) for x in f.readline().split(';') if x != '']
  45. else:
  46. indices_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_image_features(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 indices_model:
  66. test_data = data[selected_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 = os.path.join(custom_min_max_folder, p_custom)
  72. # need to read min_max_file
  73. with open(min_max_file_path, 'r') as f:
  74. min_val = float(f.readline().replace('\n', ''))
  75. max_val = float(f.readline().replace('\n', ''))
  76. test_data = utils.normalize_arr_with_range(test_data, min_val, max_val)
  77. if p_mode == 'svdn':
  78. test_data = utils.normalize_arr(test_data)
  79. else:
  80. # check mode to normalize data
  81. if p_mode == 'svdne':
  82. # set min_max_filename if custom use
  83. min_max_file_path = os.path.join(path, p_feature + min_max_ext)
  84. # need to read min_max_file
  85. with open(min_max_file_path, 'r') as f:
  86. min_val = float(f.readline().replace('\n', ''))
  87. max_val = float(f.readline().replace('\n', ''))
  88. l_values = utils.normalize_arr_with_range(data, min_val, max_val)
  89. elif p_mode == 'svdn':
  90. l_values = utils.normalize_arr(data)
  91. else:
  92. l_values = data
  93. if indices_model:
  94. test_data = data[selected_indices]
  95. else:
  96. test_data = data[begin:end]
  97. # get prediction of model
  98. if kind_model == 'sklearn':
  99. prediction = model.predict([test_data])[0]
  100. if kind_model == 'keras':
  101. test_data = np.asarray(test_data).reshape(1, len(test_data), 1)
  102. prediction = model.predict_classes([test_data])[0][0]
  103. # output expected from others scripts
  104. print(prediction)
  105. if __name__== "__main__":
  106. main()