predict_noisy_image_rfe.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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('--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('--feature', type=str, help='feature data choice', choices=features_choices)
  27. parser.add_argument('--custom', type=str, help='Name of custom min max file if use of renormalization of data', default=False)
  28. args = parser.parse_args()
  29. p_img_file = args.image
  30. p_model_file = args.model
  31. p_mode = args.mode
  32. p_feature = args.feature
  33. p_custom = args.custom
  34. # load of model file
  35. model = joblib.load(p_model_file)
  36. # use of rfe sklearn model
  37. selected_indices = [i for i in np.arange(len(model.support_)) if model.support_[i] == True]
  38. # load image
  39. img = Image.open(p_img_file)
  40. data = get_image_features(p_feature, img)
  41. # check if custom min max file is used
  42. if p_custom:
  43. test_data = data[selected_indices]
  44. if p_mode == 'svdne':
  45. # set min_max_filename if custom use
  46. min_max_file_path = os.path.join(custom_min_max_folder, p_custom)
  47. # need to read min_max_file
  48. with open(min_max_file_path, 'r') as f:
  49. min_val = float(f.readline().replace('\n', ''))
  50. max_val = float(f.readline().replace('\n', ''))
  51. test_data = utils.normalize_arr_with_range(test_data, min_val, max_val)
  52. if p_mode == 'svdn':
  53. test_data = utils.normalize_arr(test_data)
  54. else:
  55. # check mode to normalize data
  56. if p_mode == 'svdne':
  57. # set min_max_filename if custom use
  58. min_max_file_path = os.path.join(path, p_feature + min_max_ext)
  59. # need to read min_max_file
  60. with open(min_max_file_path, 'r') as f:
  61. min_val = float(f.readline().replace('\n', ''))
  62. max_val = float(f.readline().replace('\n', ''))
  63. l_values = utils.normalize_arr_with_range(data, min_val, max_val)
  64. elif p_mode == 'svdn':
  65. l_values = utils.normalize_arr(data)
  66. else:
  67. l_values = data
  68. test_data = data[selected_indices]
  69. # get prediction of model
  70. prediction = model.estimator_.predict([test_data])[0]
  71. # output expected from others scripts
  72. print(prediction)
  73. if __name__== "__main__":
  74. main()