def save_prediction_losses_results(self, eval_avod_losses, num_valid_samples, global_step, predictions_base_dir, box_rep): """Helper function to save the AVOD loss evaluation results. Args: eval_avod_losses: A dictionary containing the loss sums num_valid_samples: An int, number of valid evaluated samples i.e. samples with valid ground-truth. global_step: Global step at which the metrics are computed. predictions_base_dir: Base directory for storing the results. box_rep: A string, the format of the 3D bounding box one of 'box_3d', 'box_8c' etc. """ sum_avod_cls_loss = eval_avod_losses[KEY_SUM_AVOD_CLS_LOSS] sum_avod_reg_loss = eval_avod_losses[KEY_SUM_AVOD_REG_LOSS] sum_avod_total_loss = eval_avod_losses[KEY_SUM_AVOD_TOTAL_LOSS] sum_avod_loc_loss = eval_avod_losses[KEY_SUM_AVOD_LOC_LOSS] sum_avod_ang_loss = eval_avod_losses[KEY_SUM_AVOD_ANG_LOSS] sum_avod_cls_accuracy = \ eval_avod_losses[KEY_SUM_AVOD_CLS_ACC] num_valid_regression_samples = \ eval_avod_losses[KEY_NUM_VALID_REG_SAMPLES] avg_avod_cls_loss = sum_avod_cls_loss / num_valid_samples avg_avod_reg_loss = sum_avod_reg_loss / num_valid_samples avg_avod_total_loss = sum_avod_total_loss / num_valid_samples if num_valid_regression_samples > 0: avg_avod_loc_loss = \ sum_avod_loc_loss / num_valid_regression_samples if box_rep in ['box_3d', 'box_4ca']: avg_avod_ang_loss = \ sum_avod_ang_loss / num_valid_regression_samples else: avg_avod_loc_loss = 0 avg_avod_ang_loss = 0 avg_avod_cls_accuracy = sum_avod_cls_accuracy / num_valid_samples # Write summaries summary_utils.add_scalar_summary( 'avod_losses/classification/classification', avg_avod_cls_loss, self.summary_writer, global_step) summary_utils.add_scalar_summary( 'avod_losses/regression/regression_total', avg_avod_reg_loss, self.summary_writer, global_step) summary_utils.add_scalar_summary('avod_losses/regression/localization', avg_avod_loc_loss, self.summary_writer, global_step) if box_rep in ['box_3d', 'box_4ca']: summary_utils.add_scalar_summary( 'avod_losses/regression/orientation', avg_avod_ang_loss, self.summary_writer, global_step) print("Step {}: Average AVOD Losses: " "cls {:.5f}, " "reg {:.5f}, " "total {:.5f} ".format( global_step, avg_avod_cls_loss, avg_avod_reg_loss, avg_avod_total_loss, )) if box_rep in ['box_3d', 'box_4ca']: print("Step {} Average AVOD Losses: " "loc {:.5f} " "ang {:.5f}".format( global_step, avg_avod_loc_loss, avg_avod_ang_loss, )) elif box_rep in ['box_8c', 'box_8co', 'box_4c']: print("Step {} Average AVOD Losses: " "loc {:.5f} ".format(global_step, avg_avod_loc_loss)) else: raise NotImplementedError('Print average loss not implemented') print("Step {}: Average Classification Accuracy: {} ".format( global_step, avg_avod_cls_accuracy)) # Append to end of file avg_loss_file_path = predictions_base_dir + '/avod_avg_losses.csv' if box_rep in ['box_3d', 'box_4ca']: with open(avg_loss_file_path, 'ba') as fp: np.savetxt(fp, [ np.hstack([ global_step, avg_avod_cls_loss, avg_avod_reg_loss, avg_avod_total_loss, avg_avod_loc_loss, avg_avod_ang_loss, ]) ], fmt='%d, %.5f, %.5f, %.5f, %.5f, %.5f') elif box_rep in ['box_8c', 'box_8co', 'box_4c']: with open(avg_loss_file_path, 'ba') as fp: np.savetxt(fp, [ np.hstack([ global_step, avg_avod_cls_loss, avg_avod_reg_loss, avg_avod_total_loss, avg_avod_loc_loss, ]) ], fmt='%d, %.5f, %.5f, %.5f, %.5f') else: raise NotImplementedError('Saving losses not implemented') avg_acc_file_path = predictions_base_dir + '/avod_avg_cls_acc.csv' with open(avg_acc_file_path, 'ba') as fp: np.savetxt(fp, np.reshape([global_step, avg_avod_cls_accuracy], (1, 2)), fmt='%d, %.5f')
def save_prediction_losses_results(self, sum_losses, num_valid_samples, global_step, predictions_base_dir): """Helper function to save the AVOD loss evaluation results. Args: eval_avod_losses: A dictionary containing the loss sums num_valid_samples: An int, number of valid evaluated samples i.e. samples with valid ground-truth. global_step: Global step at which the metrics are computed. predictions_base_dir: Base directory for storing the results. box_rep: A string, the format of the 3D bounding box one of 'box_3d', 'box_8c' etc. """ num_valid_reg_samples = max(1, sum_losses[KEY_NUM_VALID_REG_SAMPLES]) avg_losses_key_str = '' avg_losses_print_str = '' avg_losses_data = [] for sk, has_h, has_ang in zip(\ self.model_losses_keys, self.model_has_h_flags,\ self.model_has_angle_flags): stage_losses = sum_losses[sk] sum_cls_loss = stage_losses[KEY_SUM_CLS_LOSS] avg_cls_loss = sum_cls_loss / num_valid_samples # Write summaries #f'{sk}_losses/classification/cls', summary_utils.add_scalar_summary( f'{sk}_loss/retinanet_losses/cls/norm/val', avg_cls_loss, self.summary_writer, global_step) sum_reg_loss = stage_losses[KEY_SUM_REG_LOSS] avg_reg_loss = sum_reg_loss / num_valid_reg_samples #f'{sk}_losses/regression/reg', summary_utils.add_scalar_summary( f'{sk}_loss/retinanet_losses/reg/norm/val', avg_reg_loss, self.summary_writer, global_step) avg_losses_print_str += f'[{sk}]cls:{avg_cls_loss}, reg:{avg_reg_loss}, ' avg_losses_key_str += f'{sk}_cls {sk}_reg ' avg_losses_data.extend([avg_cls_loss, avg_reg_loss]) if KEY_SUM_REG_H_LOSS in stage_losses: sum_h_loss = stage_losses[KEY_SUM_REG_H_LOSS] avg_h_loss = sum_h_loss / num_valid_reg_samples #f'{sk}_losses/regression/h', summary_utils.add_scalar_summary( f'{sk}_loss/retinanet_losses/reg/norm/h_reg', avg_h_loss, self.summary_writer, global_step) avg_losses_print_str += f'h:{avg_h_loss}, ' avg_losses_key_str += f'{sk}_h ' avg_losses_data.append(avg_h_loss) if KEY_SUM_REG_ANGLE_CLS_LOSS in stage_losses: sum_angle_cls_loss = stage_losses[KEY_SUM_REG_ANGLE_CLS_LOSS] avg_angle_cls_loss = sum_angle_cls_loss / num_valid_reg_samples #f'{sk}_losses/regression/angle_cls', summary_utils.add_scalar_summary( f'{sk}_loss/retinanet_losses/reg/norm/angle_cls', avg_angle_cls_loss, self.summary_writer, global_step) avg_losses_print_str += f'angle:{avg_angle_cls_loss}, ' avg_losses_key_str += f'{sk}_angle ' avg_losses_data.append(avg_angle_cls_loss) sum_cls_acc = sum_losses[KEY_SUM_CLS_ACC] avg_cls_acc = sum_cls_acc / num_valid_samples summary_utils.add_scalar_summary(f'output/cls_accuracy', avg_cls_acc, self.summary_writer, global_step) self.summary_writer.flush() sum_total_loss = sum_losses[KEY_SUM_TOTAL_LOSS] avg_total_loss = sum_total_loss / num_valid_samples avg_losses_print_str += f'\ntotal:{avg_total_loss}' avg_losses_key_str += 'total ' avg_losses_data.append(avg_total_loss) # Append to end of file avg_loss_file_dir = predictions_base_dir + '/avg_losses/'\ + self.dataset_config.data_split if not os.path.exists(avg_loss_file_dir): os.makedirs(avg_loss_file_dir) avg_loss_file_path = avg_loss_file_dir + '/avg_losses.csv' if not os.path.exists(avg_loss_file_path): with open(avg_loss_file_path, 'w') as f: f.write(f'Step {avg_losses_key_str}\n') save = True if save: print( f"Step {global_step}: Average Losses: \n{avg_losses_print_str}" ) with open(avg_loss_file_path, 'ba') as fp: avg_losses_data = np.array(avg_losses_data) np.savetxt(fp, [np.hstack([global_step, avg_losses_data])], fmt='%d' + ', %.5f' * (len(avg_losses_data)))