predict_noisy_image_svd_filters.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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('--solution', type=str, help='Data of solution to specify filters to use')
  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_solution = list(map(int, args.solution.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 kind_model == 'sklearn':
  41. # load of model file
  42. model = joblib.load(p_model_file)
  43. if kind_model == 'keras':
  44. with open(p_model_file, 'r') as f:
  45. json_model = json.load(f)
  46. model = model_from_json(json_model)
  47. model.load_weights(p_model_file.replace('.json', '.h5'))
  48. model.compile(loss='binary_crossentropy',
  49. optimizer='adam',
  50. features=['accuracy'])
  51. # load image
  52. img = Image.open(p_img_file)
  53. data = get_image_features(p_feature, img)
  54. # get indices of filters data to use (filters selection from solution)
  55. indices = []
  56. for index, value in enumerate(p_solution):
  57. if value == 1:
  58. indices.append(index*2)
  59. indices.append(index*2+1)
  60. # No need to use custom normalization with this kind of process
  61. # check mode to normalize data
  62. if p_mode == 'svdne':
  63. # set min_max_filename if custom use
  64. min_max_file_path = os.path.join(path, p_feature + min_max_ext)
  65. # need to read min_max_file
  66. with open(min_max_file_path, 'r') as f:
  67. min_val = float(f.readline().replace('\n', ''))
  68. max_val = float(f.readline().replace('\n', ''))
  69. l_values = utils.normalize_arr_with_range(data, min_val, max_val)
  70. elif p_mode == 'svdn':
  71. l_values = utils.normalize_arr(data)
  72. else:
  73. l_values = data
  74. test_data = np.array(l_values)[indices]
  75. # get prediction of model
  76. if kind_model == 'sklearn':
  77. prediction = model.predict([test_data])[0]
  78. if kind_model == 'keras':
  79. test_data = np.asarray(test_data).reshape(1, len(test_data), 1)
  80. prediction = model.predict_classes([test_data])[0][0]
  81. # output expected from others scripts
  82. print(prediction)
  83. if __name__== "__main__":
  84. main()