prediction_scene.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from sklearn.externals import joblib
  2. import numpy as np
  3. import pandas as pd
  4. from sklearn.metrics import accuracy_score
  5. from keras.models import Sequential
  6. from keras.layers import Conv1D, MaxPooling1D
  7. from keras.layers import Activation, Dropout, Flatten, Dense, BatchNormalization
  8. from keras import backend as K
  9. from keras.models import model_from_json
  10. from keras.wrappers.scikit_learn import KerasClassifier
  11. import sys, os, argparse
  12. import json
  13. from modules.utils import config as cfg
  14. output_model_folder = cfg.saved_models_folder
  15. def main():
  16. parser = argparse.ArgumentParser(description="Give model performance on specific scene")
  17. parser.add_argument('--data', type=str, help='dataset filename prefix of specific scene (without .train and .test)')
  18. parser.add_argument('--model', type=str, help='saved model (Keras or SKlearn) filename with extension')
  19. parser.add_argument('--output', type=str, help="filename to store predicted and performance model obtained on scene")
  20. parser.add_argument('--scene', type=str, help="scene indice to predict", choices=cfg.scenes_indices)
  21. args = parser.parse_args()
  22. p_data_file = args.data
  23. p_model_file = args.model
  24. p_output = args.output
  25. p_scene = args.scene
  26. if '.joblib' in p_model_file:
  27. kind_model = 'sklearn'
  28. model_ext = '.joblib'
  29. if '.json' in p_model_file:
  30. kind_model = 'keras'
  31. model_ext = '.json'
  32. if not os.path.exists(output_model_folder):
  33. os.makedirs(output_model_folder)
  34. dataset = pd.read_csv(p_data_file, header=None, sep=";")
  35. y_dataset = dataset.ix[:,0]
  36. x_dataset = dataset.ix[:,1:]
  37. noisy_dataset = dataset[dataset.ix[:, 0] == 1]
  38. not_noisy_dataset = dataset[dataset.ix[:, 0] == 0]
  39. y_noisy_dataset = noisy_dataset.ix[:, 0]
  40. x_noisy_dataset = noisy_dataset.ix[:, 1:]
  41. y_not_noisy_dataset = not_noisy_dataset.ix[:, 0]
  42. x_not_noisy_dataset = not_noisy_dataset.ix[:, 1:]
  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. metrics=['accuracy'])
  51. _, vector_size = np.array(x_dataset).shape
  52. # reshape all data
  53. x_dataset = np.array(x_dataset).reshape(len(x_dataset), vector_size, 1)
  54. x_noisy_dataset = np.array(x_noisy_dataset).reshape(len(x_noisy_dataset), vector_size, 1)
  55. x_not_noisy_dataset = np.array(x_not_noisy_dataset).reshape(len(x_not_noisy_dataset), vector_size, 1)
  56. if kind_model == 'sklearn':
  57. model = joblib.load(p_model_file)
  58. if kind_model == 'keras':
  59. y_pred = model.predict_classes(x_dataset)
  60. y_noisy_pred = model.predict_classes(x_noisy_dataset)
  61. y_not_noisy_pred = model.predict_classes(x_not_noisy_dataset)
  62. if kind_model == 'sklearn':
  63. y_pred = model.predict(x_dataset)
  64. y_noisy_pred = model.predict(x_noisy_dataset)
  65. y_not_noisy_pred = model.predict(x_not_noisy_dataset)
  66. accuracy_global = accuracy_score(y_dataset, y_pred)
  67. accuracy_noisy = accuracy_score(y_noisy_dataset, y_noisy_pred)
  68. accuracy_not_noisy = accuracy_score(y_not_noisy_dataset, y_not_noisy_pred)
  69. if(p_scene):
  70. print(p_scene + " | " + str(accuracy_global) + " | " + str(accuracy_noisy) + " | " + str(accuracy_not_noisy))
  71. else:
  72. print(str(accuracy_global) + " \t | " + str(accuracy_noisy) + " \t | " + str(accuracy_not_noisy))
  73. with open(p_output, 'w') as f:
  74. f.write("Global accuracy found %s " % str(accuracy_global))
  75. f.write("Noisy accuracy found %s " % str(accuracy_noisy))
  76. f.write("Not noisy accuracy found %s " % str(accuracy_not_noisy))
  77. for prediction in y_pred:
  78. f.write(str(prediction) + '\n')
  79. if __name__== "__main__":
  80. main()