predict_noisy_image.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from sklearn.externals import joblib
  2. import numpy as np
  3. from ipfml import processing, utils
  4. from PIL import Image
  5. import sys, os, argparse, json
  6. from keras.models import model_from_json
  7. from modules.utils import config as cfg
  8. from modules.utils import data as dt
  9. from modules.classes.Transformation import Transformation
  10. path = cfg.dataset_path
  11. min_max_ext = cfg.min_max_filename_extension
  12. metric_choices = cfg.metric_choices_labels
  13. normalization_choices = cfg.normalization_choices
  14. custom_min_max_folder = cfg.min_max_custom_folder
  15. def main():
  16. # getting all params
  17. parser = argparse.ArgumentParser(description="Script which detects if an image is noisy or not using specific model")
  18. parser.add_argument('--image', type=str, help='Image path')
  19. parser.add_argument('--metrics', type=str,
  20. help="list of metrics choice in order to compute data",
  21. default='svd_reconstruction, ipca_reconstruction',
  22. required=True)
  23. parser.add_argument('--params', type=str,
  24. help="list of specific param for each metric choice (See README.md for further information in 3D mode)",
  25. default='100, 200 :: 50, 25',
  26. required=True)
  27. parser.add_argument('--model', type=str, help='.json file of keras model')
  28. args = parser.parse_args()
  29. p_img_file = args.image
  30. p_metrics = list(map(str.strip, args.metrics.split(',')))
  31. p_params = list(map(str.strip, args.params.split('::')))
  32. p_model_file = args.model
  33. with open(p_model_file, 'r') as f:
  34. json_model = json.load(f)
  35. model = model_from_json(json_model)
  36. model.load_weights(p_model_file.replace('.json', '.h5'))
  37. model.compile(loss='binary_crossentropy',
  38. optimizer='rmsprop',
  39. metrics=['accuracy'])
  40. # load image
  41. img = Image.open(p_img_file)
  42. transformations = []
  43. for id, metric in enumerate(p_metrics):
  44. if metric not in metric_choices:
  45. raise ValueError("Unknown metric, please select a correct metric : ", metric_choices)
  46. transformations.append(Transformation(metric, p_params[id]))
  47. # getting transformed image
  48. transformed_images = []
  49. for transformation in transformations:
  50. transformed_images.append(transformation.getTransformedImage(img))
  51. data = np.array(transformed_images)
  52. # specify the number of dimensions
  53. img_width, img_height = cfg.keras_img_size
  54. n_channels = len(transformations)
  55. if K.image_data_format() == 'channels_first':
  56. input_shape = (n_channels, img_width, img_height)
  57. else:
  58. input_shape = (img_width, img_height, n_channels)
  59. prediction = model.predict_classes([data])[0][0]
  60. # output expected from others scripts
  61. print(prediction)
  62. if __name__== "__main__":
  63. main()