MCPcopy Create free account
hub / github.com/OpenDriveLab/TCP / validation_step

Method validation_step

TCP/train.py:96–150  ·  view source on GitHub ↗
(self, batch, batch_idx)

Source from the content-addressed store, hash-verified

94 return [optimizer], [lr_scheduler]
95
96 def validation_step(self, batch, batch_idx):
97 front_img = batch['front_img']
98 speed = batch['speed'].to(dtype=torch.float32).view(-1,1) / 12.
99 target_point = batch['target_point'].to(dtype=torch.float32)
100 command = batch['target_command']
101 state = torch.cat([speed, target_point, command], 1)
102 value = batch['value'].view(-1,1)
103 feature = batch['feature']
104 gt_waypoints = batch['waypoints']
105
106 pred = self.model(front_img, state, target_point)
107 dist_sup = Beta(batch['action_mu'], batch['action_sigma'])
108 dist_pred = Beta(pred['mu_branches'], pred['sigma_branches'])
109 kl_div = torch.distributions.kl_divergence(dist_sup, dist_pred)
110 action_loss = torch.mean(kl_div[:, 0]) *0.5 + torch.mean(kl_div[:, 1]) *0.5
111 speed_loss = F.l1_loss(pred['pred_speed'], speed) * self.config.speed_weight
112 value_loss = (F.mse_loss(pred['pred_value_traj'], value) + F.mse_loss(pred['pred_value_ctrl'], value)) * self.config.value_weight
113 feature_loss = (F.mse_loss(pred['pred_features_traj'], feature) +F.mse_loss(pred['pred_features_ctrl'], feature))* self.config.features_weight
114 wp_loss = F.l1_loss(pred['pred_wp'], gt_waypoints, reduction='none').mean()
115
116 B = batch['action_mu'].shape[0]
117 batch_steer_l1 = 0
118 batch_brake_l1 = 0
119 batch_throttle_l1 = 0
120 for i in range(B):
121 throttle, steer, brake = self.model.get_action(pred['mu_branches'][i], pred['sigma_branches'][i])
122 batch_throttle_l1 += torch.abs(throttle-batch['action'][i][0])
123 batch_steer_l1 += torch.abs(steer-batch['action'][i][1])
124 batch_brake_l1 += torch.abs(brake-batch['action'][i][2])
125
126 batch_throttle_l1 /= B
127 batch_steer_l1 /= B
128 batch_brake_l1 /= B
129
130 future_feature_loss = 0
131 future_action_loss = 0
132 for i in range(self.config.pred_len-1):
133 dist_sup = Beta(batch['future_action_mu'][i], batch['future_action_sigma'][i])
134 dist_pred = Beta(pred['future_mu'][i], pred['future_sigma'][i])
135 kl_div = torch.distributions.kl_divergence(dist_sup, dist_pred)
136 future_action_loss += torch.mean(kl_div[:, 0]) *0.5 + torch.mean(kl_div[:, 1]) *0.5
137 future_feature_loss += F.mse_loss(pred['future_feature'][i], batch['future_feature'][i]) * self.config.features_weight
138 future_feature_loss /= self.config.pred_len
139 future_action_loss /= self.config.pred_len
140
141 val_loss = wp_loss + batch_throttle_l1+5*batch_steer_l1+batch_brake_l1
142
143 self.log("val_action_loss", action_loss.item(), sync_dist=True)
144 self.log('val_speed_loss', speed_loss.item(), sync_dist=True)
145 self.log('val_value_loss', value_loss.item(), sync_dist=True)
146 self.log('val_feature_loss', feature_loss.item(), sync_dist=True)
147 self.log('val_wp_loss_loss', wp_loss.item(), sync_dist=True)
148 self.log('val_future_feature_loss', future_feature_loss.item(), sync_dist=True)
149 self.log('val_future_action_loss', future_action_loss.item(), sync_dist=True)
150 self.log('val_loss', val_loss.item(), sync_dist=True)
151
152
153if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

get_actionMethod · 0.80

Tested by

no test coverage detected