prediction_scene.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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, getopt
  12. import json
  13. from modules.utils import config as cfg
  14. output_model_folder = cfg.saved_models_folder
  15. def main():
  16. if len(sys.argv) <= 1:
  17. print('Run with default parameters...')
  18. print('python prediction_scene.py --data xxxx.csv --model xxxx.joblib --output xxxx --scene xxxx')
  19. sys.exit(2)
  20. try:
  21. opts, args = getopt.getopt(sys.argv[1:], "hd:o:s", ["help=", "data=", "model=", "output=", "scene="])
  22. except getopt.GetoptError:
  23. # print help information and exit:
  24. print('python prediction_scene.py --data xxxx.csv --model xxxx.joblib --output xxxx --scene xxxx')
  25. sys.exit(2)
  26. for o, a in opts:
  27. if o == "-h":
  28. print('python prediction_scene.py --data xxxx.csv --model xxxx.joblib --output xxxx --scene xxxx')
  29. sys.exit()
  30. elif o in ("-d", "--data"):
  31. p_data_file = a
  32. elif o in ("-m", "--model"):
  33. p_model_file = a
  34. elif o in ("-o", "--output"):
  35. p_output = a
  36. elif o in ("-s", "--scene"):
  37. p_scene = a
  38. else:
  39. assert False, "unhandled option"
  40. if '.joblib' in p_model_file:
  41. kind_model = 'sklearn'
  42. model_ext = '.joblib'
  43. if '.json' in p_model_file:
  44. kind_model = 'keras'
  45. model_ext = '.json'
  46. if not os.path.exists(output_model_folder):
  47. os.makedirs(output_model_folder)
  48. dataset = pd.read_csv(p_data_file, header=None, sep=";")
  49. y_dataset = dataset.ix[:,0]
  50. x_dataset = dataset.ix[:,1:]
  51. noisy_dataset = dataset[dataset.ix[:, 0] == 1]
  52. not_noisy_dataset = dataset[dataset.ix[:, 0] == 0]
  53. y_noisy_dataset = noisy_dataset.ix[:, 0]
  54. x_noisy_dataset = noisy_dataset.ix[:, 1:]
  55. y_not_noisy_dataset = not_noisy_dataset.ix[:, 0]
  56. x_not_noisy_dataset = not_noisy_dataset.ix[:, 1:]
  57. if kind_model == 'keras':
  58. with open(p_model_file, 'r') as f:
  59. json_model = json.load(f)
  60. model = model_from_json(json_model)
  61. model.load_weights(p_model_file.replace('.json', '.h5'))
  62. model.compile(loss='binary_crossentropy',
  63. optimizer='adam',
  64. metrics=['accuracy'])
  65. _, vector_size = np.array(x_dataset).shape
  66. # reshape all data
  67. x_dataset = np.array(x_dataset).reshape(len(x_dataset), vector_size, 1)
  68. x_noisy_dataset = np.array(x_noisy_dataset).reshape(len(x_noisy_dataset), vector_size, 1)
  69. x_not_noisy_dataset = np.array(x_not_noisy_dataset).reshape(len(x_not_noisy_dataset), vector_size, 1)
  70. if kind_model == 'sklearn':
  71. model = joblib.load(p_model_file)
  72. if kind_model == 'keras':
  73. y_pred = model.predict_classes(x_dataset)
  74. y_noisy_pred = model.predict_classes(x_noisy_dataset)
  75. y_not_noisy_pred = model.predict_classes(x_not_noisy_dataset)
  76. if kind_model == 'sklearn':
  77. y_pred = model.predict(x_dataset)
  78. y_noisy_pred = model.predict(x_noisy_dataset)
  79. y_not_noisy_pred = model.predict(x_not_noisy_dataset)
  80. accuracy_global = accuracy_score(y_dataset, y_pred)
  81. accuracy_noisy = accuracy_score(y_noisy_dataset, y_noisy_pred)
  82. accuracy_not_noisy = accuracy_score(y_not_noisy_dataset, y_not_noisy_pred)
  83. if(p_scene):
  84. print(p_scene + " | " + str(accuracy_global) + " | " + str(accuracy_noisy) + " | " + str(accuracy_not_noisy))
  85. else:
  86. print(str(accuracy_global) + " \t | " + str(accuracy_noisy) + " \t | " + str(accuracy_not_noisy))
  87. with open(p_output, 'w') as f:
  88. f.write("Global accuracy found %s " % str(accuracy_global))
  89. f.write("Noisy accuracy found %s " % str(accuracy_noisy))
  90. f.write("Not noisy accuracy found %s " % str(accuracy_not_noisy))
  91. for prediction in y_pred:
  92. f.write(str(prediction) + '\n')
  93. if __name__== "__main__":
  94. main()