cal_mse.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import pandas as pd
  2. import csv
  3. import sys
  4. import os
  5. import shutil
  6. sys.path.append("/home/vangogh/software/FuzzScene/code/")
  7. import Constants
  8. path = Constants.CARLA_RADAR_DATA_PATH
  9. def calc_mse(yhat, label): # used for loss cal, output float
  10. mse = 0.
  11. count = 0
  12. if len(yhat) != len(label):
  13. print("yhat and label have different lengths")
  14. return -1
  15. for i in range(len(yhat)):
  16. count += 1
  17. predicted_steering = yhat[i]
  18. steering = label[i]
  19. mse += (float(steering) - float(predicted_steering)) ** 2.
  20. print(count)
  21. return (mse / count)
  22. def get_res(m):
  23. data_wb = []
  24. operator_data = [[[], []], [[], []], [[], []], [[], []], [[], []]]
  25. operator_mse = []
  26. p = path + 'radar' + str(m) + '.csv'
  27. with open(p, 'r') as f:
  28. f.seek(0)
  29. for i, line in enumerate(f):
  30. if i == 0:
  31. continue
  32. str_line = line.split(',')
  33. operator_data[int(str_line[1])][0].append(str_line[2])
  34. operator_data[int(str_line[1])][1].append(str_line[5])
  35. ori_mse = get_baseline_mse(m)
  36. for i in range(0, 5):
  37. if Constants.OPERATOR_FLAG[i] is True:
  38. mse = calc_mse(operator_data[i][0], operator_data[i][1])
  39. else:
  40. mse = ori_mse
  41. operator_mse.append(mse)
  42. data_wb.append([m, ori_mse, operator_mse[0], operator_mse[1], operator_mse[2], operator_mse[3], operator_mse[4]])
  43. mse_path = Constants.CARLA_RADAR_DATA_PATH + "mse.csv"
  44. if not os.path.exists(mse_path):
  45. shutil.copy(Constants.CARLA_RADAR_DATA_PATH + "mse_null.csv", mse_path)
  46. with open(mse_path, 'a+', encoding='utf-8') as f:
  47. csv_writer = csv.writer(f)
  48. for line in range(len(data_wb)):
  49. csv_writer.writerow(data_wb[line])
  50. def get_baseline_mse(m):
  51. p = path + 'model' + str(m) + '_oriMSE.csv'
  52. df = pd.read_csv(p, encoding='utf-8', header=0)
  53. yhat = df["predict_steering_angle"]
  54. label = df["steering_angle_change"]
  55. return calc_mse(yhat=yhat, label=label)
  56. for i in range(0, Constants.MODEL_NUM):
  57. get_res(Constants.MODEL_ARR[i])