prediction_scene.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from sklearn.externals import joblib
  2. import numpy as np
  3. import pandas as pd
  4. from sklearn.metrics import accuracy_score
  5. import sys, os, getopt
  6. from modules.utils import config as cfg
  7. output_model_folder = cfg.saved_models_folder
  8. def main():
  9. if len(sys.argv) <= 1:
  10. print('Run with default parameters...')
  11. print('python prediction_scene.py --data xxxx.csv --model xxxx.joblib --output xxxx --scene xxxx')
  12. sys.exit(2)
  13. try:
  14. opts, args = getopt.getopt(sys.argv[1:], "hd:o:s", ["help=", "data=", "model=", "output=", "scene="])
  15. except getopt.GetoptError:
  16. # print help information and exit:
  17. print('python prediction_scene.py --data xxxx.csv --model xxxx.joblib --output xxxx --scene xxxx')
  18. sys.exit(2)
  19. for o, a in opts:
  20. if o == "-h":
  21. print('python prediction_scene.py --data xxxx.csv --model xxxx.joblib --output xxxx --scene xxxx')
  22. sys.exit()
  23. elif o in ("-d", "--data"):
  24. p_data_file = a
  25. elif o in ("-m", "--model"):
  26. p_model_file = a
  27. elif o in ("-o", "--output"):
  28. p_output = a
  29. elif o in ("-s", "--scene"):
  30. p_scene = a
  31. else:
  32. assert False, "unhandled option"
  33. if not os.path.exists(output_model_folder):
  34. os.makedirs(output_model_folder)
  35. dataset = pd.read_csv(p_data_file, header=None, sep=";")
  36. y_dataset = dataset.ix[:,0]
  37. x_dataset = dataset.ix[:,1:]
  38. noisy_dataset = dataset[dataset.ix[:, 0] == 1]
  39. not_noisy_dataset = dataset[dataset.ix[:, 0] == 0]
  40. y_noisy_dataset = noisy_dataset.ix[:, 0]
  41. x_noisy_dataset = noisy_dataset.ix[:, 1:]
  42. y_not_noisy_dataset = not_noisy_dataset.ix[:, 0]
  43. x_not_noisy_dataset = not_noisy_dataset.ix[:, 1:]
  44. model = joblib.load(p_model_file)
  45. y_pred = model.predict(x_dataset)
  46. y_noisy_pred = model.predict(x_noisy_dataset)
  47. y_not_noisy_pred = model.predict(x_not_noisy_dataset)
  48. accuracy_global = accuracy_score(y_dataset, y_pred)
  49. accuracy_noisy = accuracy_score(y_noisy_dataset, y_noisy_pred)
  50. accuracy_not_noisy = accuracy_score(y_not_noisy_dataset, y_not_noisy_pred)
  51. if(p_scene):
  52. print(p_scene + " | " + str(accuracy_global) + " | " + str(accuracy_noisy) + " | " + str(accuracy_not_noisy))
  53. else:
  54. print(str(accuracy_global) + " \t | " + str(accuracy_noisy) + " \t | " + str(accuracy_not_noisy))
  55. with open(p_output, 'w') as f:
  56. f.write("Global accuracy found %s " % str(accuracy_global))
  57. f.write("Noisy accuracy found %s " % str(accuracy_noisy))
  58. f.write("Not noisy accuracy found %s " % str(accuracy_not_noisy))
  59. for prediction in y_pred:
  60. f.write(str(prediction) + '\n')
  61. if __name__== "__main__":
  62. main()