parse_model_outputs.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import numpy as np
  2. from openpilot.selfdrive.modeld.constants import ModelConstants
  3. def sigmoid(x):
  4. return 1. / (1. + np.exp(-x))
  5. def softmax(x, axis=-1):
  6. x -= np.max(x, axis=axis, keepdims=True)
  7. if x.dtype == np.float32 or x.dtype == np.float64:
  8. np.exp(x, out=x)
  9. else:
  10. x = np.exp(x)
  11. x /= np.sum(x, axis=axis, keepdims=True)
  12. return x
  13. class Parser:
  14. def __init__(self, ignore_missing=False):
  15. self.ignore_missing = ignore_missing
  16. def check_missing(self, outs, name):
  17. if name not in outs and not self.ignore_missing:
  18. raise ValueError(f"Missing output {name}")
  19. return name not in outs
  20. def parse_categorical_crossentropy(self, name, outs, out_shape=None):
  21. if self.check_missing(outs, name):
  22. return
  23. raw = outs[name]
  24. if out_shape is not None:
  25. raw = raw.reshape((raw.shape[0],) + out_shape)
  26. outs[name] = softmax(raw, axis=-1)
  27. def parse_binary_crossentropy(self, name, outs):
  28. if self.check_missing(outs, name):
  29. return
  30. raw = outs[name]
  31. outs[name] = sigmoid(raw)
  32. def parse_mdn(self, name, outs, in_N=0, out_N=1, out_shape=None):
  33. if self.check_missing(outs, name):
  34. return
  35. raw = outs[name]
  36. raw = raw.reshape((raw.shape[0], max(in_N, 1), -1))
  37. pred_mu = raw[:,:,:(raw.shape[2] - out_N)//2]
  38. n_values = (raw.shape[2] - out_N)//2
  39. pred_mu = raw[:,:,:n_values]
  40. pred_std = np.exp(raw[:,:,n_values: 2*n_values])
  41. if in_N > 1:
  42. weights = np.zeros((raw.shape[0], in_N, out_N), dtype=raw.dtype)
  43. for i in range(out_N):
  44. weights[:,:,i - out_N] = softmax(raw[:,:,i - out_N], axis=-1)
  45. if out_N == 1:
  46. for fidx in range(weights.shape[0]):
  47. idxs = np.argsort(weights[fidx][:,0])[::-1]
  48. weights[fidx] = weights[fidx][idxs]
  49. pred_mu[fidx] = pred_mu[fidx][idxs]
  50. pred_std[fidx] = pred_std[fidx][idxs]
  51. full_shape = tuple([raw.shape[0], in_N] + list(out_shape))
  52. outs[name + '_weights'] = weights
  53. outs[name + '_hypotheses'] = pred_mu.reshape(full_shape)
  54. outs[name + '_stds_hypotheses'] = pred_std.reshape(full_shape)
  55. pred_mu_final = np.zeros((raw.shape[0], out_N, n_values), dtype=raw.dtype)
  56. pred_std_final = np.zeros((raw.shape[0], out_N, n_values), dtype=raw.dtype)
  57. for fidx in range(weights.shape[0]):
  58. for hidx in range(out_N):
  59. idxs = np.argsort(weights[fidx,:,hidx])[::-1]
  60. pred_mu_final[fidx, hidx] = pred_mu[fidx, idxs[0]]
  61. pred_std_final[fidx, hidx] = pred_std[fidx, idxs[0]]
  62. else:
  63. pred_mu_final = pred_mu
  64. pred_std_final = pred_std
  65. if out_N > 1:
  66. final_shape = tuple([raw.shape[0], out_N] + list(out_shape))
  67. else:
  68. final_shape = tuple([raw.shape[0],] + list(out_shape))
  69. outs[name] = pred_mu_final.reshape(final_shape)
  70. outs[name + '_stds'] = pred_std_final.reshape(final_shape)
  71. def parse_outputs(self, outs: dict[str, np.ndarray]) -> dict[str, np.ndarray]:
  72. self.parse_mdn('plan', outs, in_N=ModelConstants.PLAN_MHP_N, out_N=ModelConstants.PLAN_MHP_SELECTION,
  73. out_shape=(ModelConstants.IDX_N,ModelConstants.PLAN_WIDTH))
  74. self.parse_mdn('lane_lines', outs, in_N=0, out_N=0, out_shape=(ModelConstants.NUM_LANE_LINES,ModelConstants.IDX_N,ModelConstants.LANE_LINES_WIDTH))
  75. self.parse_mdn('road_edges', outs, in_N=0, out_N=0, out_shape=(ModelConstants.NUM_ROAD_EDGES,ModelConstants.IDX_N,ModelConstants.LANE_LINES_WIDTH))
  76. self.parse_mdn('pose', outs, in_N=0, out_N=0, out_shape=(ModelConstants.POSE_WIDTH,))
  77. self.parse_mdn('road_transform', outs, in_N=0, out_N=0, out_shape=(ModelConstants.POSE_WIDTH,))
  78. self.parse_mdn('sim_pose', outs, in_N=0, out_N=0, out_shape=(ModelConstants.POSE_WIDTH,))
  79. self.parse_mdn('wide_from_device_euler', outs, in_N=0, out_N=0, out_shape=(ModelConstants.WIDE_FROM_DEVICE_WIDTH,))
  80. self.parse_mdn('lead', outs, in_N=ModelConstants.LEAD_MHP_N, out_N=ModelConstants.LEAD_MHP_SELECTION,
  81. out_shape=(ModelConstants.LEAD_TRAJ_LEN,ModelConstants.LEAD_WIDTH))
  82. if 'lat_planner_solution' in outs:
  83. self.parse_mdn('lat_planner_solution', outs, in_N=0, out_N=0, out_shape=(ModelConstants.IDX_N,ModelConstants.LAT_PLANNER_SOLUTION_WIDTH))
  84. if 'desired_curvature' in outs:
  85. self.parse_mdn('desired_curvature', outs, in_N=0, out_N=0, out_shape=(ModelConstants.DESIRED_CURV_WIDTH,))
  86. for k in ['lead_prob', 'lane_lines_prob', 'meta']:
  87. self.parse_binary_crossentropy(k, outs)
  88. self.parse_categorical_crossentropy('desire_state', outs, out_shape=(ModelConstants.DESIRE_PRED_WIDTH,))
  89. self.parse_categorical_crossentropy('desire_pred', outs, out_shape=(ModelConstants.DESIRE_PRED_LEN,ModelConstants.DESIRE_PRED_WIDTH))
  90. return outs