Exemplo n.º 1
0
def loss_cvae(gt_traj, future_traj, conf, mean, var):
    pred_loss = utils._average_displacement_error(gt_traj,
                                                  future_traj,
                                                  conf,
                                                  mode='best')
    KLD_loss = -0.5 * torch.sum(1 + var - mean.pow(2) - var.exp())
    return pred_loss + KLD_loss, pred_loss
def forward_g(scene,
              his_traj,
              targets,
              model_g,
              model_d,
              optimizer,
              scheduler,
              omega=1.0,
              epsilon=1.0):

    model_g.train()
    preds, conf, context = model_g(scene, his_traj)
    traj_fake = utils.multi2single(preds, targets, conf, mode='best')
    score_fake = model_d(traj_fake.permute(1, 0, 2), context)
    # 判别loss + nll_loss + ade_loss
    g_loss = utils.g_loss(score_fake)
    # nll_loss = utils.pytorch_neg_multi_log_likelihood_batch(targets, preds, conf)
    min_l2_loss = utils._average_displacement_error(targets,
                                                    preds,
                                                    conf,
                                                    mode='best')
    #     loss = g_loss + nll_loss * omega + l2_loss * epsilon
    nll_loss = 0.0
    loss = g_loss + nll_loss * omega + min_l2_loss * epsilon
    scheduler.step()
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    return loss, nll_loss, min_l2_loss, preds, conf
         targets_valid.float(),
         model,
         device,
         omega=omega)
     # camera frame to world frame(meter)
     pred = torch.zeros_like(pred_pixel)
     for batch_index in range(pred_pixel.shape[0]):
         for modality in range(pred_pixel.shape[1]):
             for pos_index in range(pred_pixel.shape[2]):
                 pred[batch_index][modality][
                     pos_index] = torch.from_numpy(
                         scripts.project(
                             h_matrix, pred_pixel[batch_index]
                             [modality][pos_index].cpu()))
     # calculate metrics in world frame
     valid_ade = utils._average_displacement_error(
         data_valid[2].to(device), pred, conf, mode='best')
     valid_fde = utils._final_displacement_error(
         data_valid[2].to(device), pred, conf, mode='best')
     valid_loss = valid_loss.item()
     valid_ade = valid_ade.item()
     valid_fde = valid_fde.item()
     valid_losses.append(valid_loss)
     valid_ades.append(valid_ade)
     valid_fdes.append(valid_fde)
 mean_loss_valid = np.mean(valid_losses)
 mean_ade_valid = np.mean(valid_ades)
 mean_fde_valid = np.mean(valid_fdes)
 # 仅在模型提升时更新checkpoint
 if mean_ade_valid < best_valid[
         0] and mean_fde_valid < best_valid[1]:
     checkpoint = {
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            loss_v = loss.item()
            loss_pre_v = loss_pre.item()
            losses.append(loss_v)
            losses_pre.append(loss_pre_v)
            train_writer.add_scalar('loss', loss_v, i)
            train_writer.add_scalar('loss_pre', loss_pre_v, i)

            if i % metrics_steps == 0:
                # 求其他尺度指标
                loss_ade = utils._average_displacement_error(
                    data["target_positions"].to(device),
                    pred,
                    conf,
                    data["target_availabilities"].to(device),
                    mode=multi_mode)
                loss_fde = utils._final_displacement_error(
                    data["target_positions"].to(device),
                    pred,
                    conf,
                    data["target_availabilities"].to(device),
                    mode=multi_mode)
                losses_ade.append(loss_ade.item())
                losses_fde.append(loss_fde.item())
                train_writer.add_scalar('loss_ade', loss_ade, i)
                train_writer.add_scalar('loss_fde', loss_fde, i)

            if i % update_steps == 0:
                mean_losses = np.mean(losses)
Exemplo n.º 5
0
                    his_traj_valid = his_traj_valid.permute(1, 0, 2)
                    targets_valid = data_valid[4].to(device)
                    valid_loss, _, pred_pixel, conf = forward(
                        scene_valid.float(), his_traj_valid.float(),
                        targets_valid.float(), model, device)
                    # camera frame to world frame(meter)
                    pred = torch.zeros_like(pred_pixel)
                    for batch_index in range(pred_pixel.shape[0]):
                        for modality in range(pred_pixel.shape[1]):
                            for pos_index in range(pred_pixel.shape[2]):
                                pred[batch_index][modality][
                                    pos_index] = torch.from_numpy(
                                        scripts.project(
                                            h_matrix, pred_pixel[batch_index]
                                            [modality][pos_index].cpu()))
                    valid_ade = utils._average_displacement_error(
                        targets_valid, pred, conf, mode='best')
                    valid_fde = utils._final_displacement_error(targets_valid,
                                                                pred,
                                                                conf,
                                                                mode='best')
                    valid_loss = valid_loss.item()
                    valid_ade = valid_ade.item()
                    valid_fde = valid_fde.item()
                    valid_losses.append(valid_loss)
                    valid_ades.append(valid_ade)
                    valid_fdes.append(valid_fde)
                mean_loss_valid = np.mean(valid_losses)
                mean_ade_valid = np.mean(valid_ades)
                mean_fde_valid = np.mean(valid_fdes)

                torch.set_grad_enabled(True)
 scene_valid = scene_valid.permute(0, 3, 2, 1)
 his_traj_valid = data_valid[3].to(device)
 his_traj_valid = his_traj_valid.permute(1, 0, 2)
 targets_valid = data_valid[4].to(device)
 pred_pixel, conf, context = generator(
     scene_valid.float(), his_traj_valid.float())
 traj_fake_valid = utils.multi2single(pred_pixel,
                                      targets_valid.float(),
                                      conf,
                                      mode='best')
 score_fake = discriminator(
     traj_fake_valid.permute(1, 0, 2), context)
 g_loss_valid = utils.g_loss(score_fake)
 # nll_loss_valid = utils.pytorch_neg_multi_log_likelihood_batch(targets_valid, pred_pixel, conf)
 nll_loss_valid = 0.0
 min_l2_loss_valid = utils._average_displacement_error(
     targets_valid, pred_pixel, conf, mode='best')
 #     loss = g_loss + nll_loss * omega + l2_loss * epsilon
 valid_loss = g_loss_valid + nll_loss_valid * omega + min_l2_loss_valid * epsilon
 # camera frame to world frame(meter)
 pred = torch.zeros_like(pred_pixel)
 for batch_index in range(pred_pixel.shape[0]):
     for modality in range(pred_pixel.shape[1]):
         for pos_index in range(pred_pixel.shape[2]):
             pred[batch_index][modality][
                 pos_index] = torch.from_numpy(
                     scripts.project(
                         h_matrix, pred_pixel[batch_index]
                         [modality][pos_index].cpu()))
 # calculate metrics in world frame
 valid_ade = utils._average_displacement_error(
     data_valid[2].to(device), pred, conf, mode='best')