predict_noisy_image.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # main imports
  2. import sys, os, argparse, json
  3. import numpy as np
  4. # image processing imports
  5. from ipfml import processing, utils
  6. from PIL import Image
  7. # model imports
  8. from sklearn.externals import joblib
  9. from keras.models import model_from_json
  10. # modules imports
  11. sys.path.insert(0, '') # trick to enable import of main folder module
  12. import custom_config as cfg
  13. from modules.utils import data as dt
  14. from modules.classes.Transformation import Transformation
  15. # parameters from config
  16. path = cfg.dataset_path
  17. min_max_ext = cfg.min_max_filename_extension
  18. features_choices = cfg.features_choices_labels
  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('--features', type=str,
  25. help="list of features choice in order to compute data",
  26. default='svd_reconstruction, ipca_reconstruction',
  27. required=True)
  28. parser.add_argument('--params', type=str,
  29. help="list of specific param for each feature choice (See README.md for further information in 3D mode)",
  30. default='100, 200 :: 50, 25',
  31. required=True)
  32. parser.add_argument('--model', type=str, help='.json file of keras model')
  33. args = parser.parse_args()
  34. p_img_file = args.image
  35. p_features = list(map(str.strip, args.features.split(',')))
  36. p_params = list(map(str.strip, args.params.split('::')))
  37. p_model_file = args.model
  38. with open(p_model_file, 'r') as f:
  39. json_model = json.load(f)
  40. model = model_from_json(json_model)
  41. model.load_weights(p_model_file.replace('.json', '.h5'))
  42. model.compile(loss='binary_crossentropy',
  43. optimizer='rmsprop',
  44. features=['accuracy'])
  45. # load image
  46. img = Image.open(p_img_file)
  47. transformations = []
  48. for id, feature in enumerate(p_features):
  49. if feature not in feature_choices:
  50. raise ValueError("Unknown feature, please select a correct feature : ", feature_choices)
  51. transformations.append(Transformation(feature, p_params[id]))
  52. # getting transformed image
  53. transformed_images = []
  54. for transformation in transformations:
  55. transformed_images.append(transformation.getTransformedImage(img))
  56. data = np.array(transformed_images)
  57. # specify the number of dimensions
  58. img_width, img_height = cfg.keras_img_size
  59. n_channels = len(transformations)
  60. if K.image_data_format() == 'channels_first':
  61. input_shape = (n_channels, img_width, img_height)
  62. else:
  63. input_shape = (img_width, img_height, n_channels)
  64. prediction = model.predict_classes([data])[0][0]
  65. # output expected from others scripts
  66. print(prediction)
  67. if __name__== "__main__":
  68. main()