Exemplo n.º 1
0
def test():
    """
    Function to carry out the testing/validation loop for the Full Network for a single epoch.
    :return: None
    """
    running_recon_loss = 0.0

    model.eval()

    for batch_idx, (vp_diff, vid1, vid2) in enumerate(testloader):
        vp_diff = vp_diff.to(device)
        vid1, vid2 = vid1.to(device), vid2.to(device)
        img1, img2 = get_first_frame(vid1), get_first_frame(vid2)
        img1, img2 = img1.to(device), img2.to(device)

        with torch.no_grad():
            gen_v2, rep_v1 = model(vp_diff=vp_diff, vid1=vid1, img2=img2)

            # save videos
            convert_to_vid(tensor=vid1,
                           output_dir=output_video_dir,
                           batch_num=batch_idx + 1,
                           view=1,
                           item_type='input')
            convert_to_vid(tensor=vid2,
                           output_dir=output_video_dir,
                           batch_num=batch_idx + 1,
                           view=2,
                           item_type='input')
            convert_to_vid(tensor=gen_v2,
                           output_dir=output_video_dir,
                           batch_num=batch_idx + 1,
                           view=2,
                           item_type='output')
            convert_to_vid(tensor=rep_v1,
                           output_dir=output_video_dir,
                           batch_num=batch_idx + 1,
                           view=1,
                           item_type='rep')

            # loss
            recon_loss = criterion(gen_v2, vid2)
            # reconstruction losses for videos gen from features and same view
            # recon3_loss = criterion(recon_v1, vid1)
            # recon4_loss = criterion(recon_v2, vid2)
            loss = recon_loss

        running_recon_loss += recon_loss.item()
        if (batch_idx + 1) % 10 == 0:
            print('\tBatch {}/{} Recon Loss:{}'.format(
                batch_idx + 1, len(testloader), "{0:.5f}".format(recon_loss)))

    print('Testing Complete Recon Loss:{}'.format("{0:.5f}".format(
        (running_recon_loss / len(testloader)))))
Exemplo n.º 2
0
def test():
    """
    Function to carry out the testing/validation loop for the Full Network for a single epoch.
    :return: None
    """
    running_total_loss = 0.0
    running_con1app_loss = 0.0
    running_con2app_loss = 0.0
    running_con1kp_loss = 0.0
    running_con2kp_loss = 0.0
    running_recon1_loss = 0.0
    running_recon2_loss = 0.0

    model.eval()

    for batch_idx, (vp_diff, vid1, vid2) in enumerate(testloader):
        vp_diff = vp_diff.to(device)
        vid1, vid2 = vid1.to(device), vid2.to(device)
        img1, img2 = get_first_frame(vid1), get_first_frame(vid2)
        img1, img2 = img1.to(device), img2.to(device)

        with torch.no_grad():
            output_v1, output_v2, app_v1, app_v2, app_v1_est, app_v2_est, kp_v1, kp_v2, kp_v1_est, kp_v2_est = model(
                vp_diff=vp_diff, vid1=vid1, vid2=vid2, img1=img1, img2=img2)

            # save videos
            convert_to_vid(vid1, output_video_dir, batch_idx + 1, 1, 'input')
            convert_to_vid(vid2, output_video_dir, batch_idx + 1, 2, 'input')
            convert_to_vid(output_v1, output_video_dir, batch_idx + 1, 1,
                           'output')
            convert_to_vid(output_v2, output_video_dir, batch_idx + 1, 2,
                           'output')

            # loss
            con1app_loss = criterion(app_v1, app_v1_est)
            con2app_loss = criterion(app_v2, app_v2_est)
            con1kp_loss = criterion(kp_v1, kp_v1_est)
            con2kp_loss = criterion(kp_v2, kp_v2_est)
            recon1_loss = criterion(output_v1, vid1)
            recon2_loss = criterion(output_v2, vid2)
            loss = con1app_loss + con2app_loss + con1kp_loss + con2kp_loss + recon1_loss + recon2_loss

        running_total_loss += loss.item()
        running_con1app_loss += con1app_loss.item()
        running_con2app_loss += con2app_loss.item()
        running_con1kp_loss += con1kp_loss.item()
        running_con2kp_loss += con2kp_loss.item()
        running_recon1_loss += recon1_loss.item()
        running_recon2_loss += recon2_loss.item()
        if (batch_idx + 1) % 10 == 0:
            print(
                '\tBatch {}/{} Loss:{} con1app:{} con2app:{} con1kp:{} con2kp:{} recon1:{} recon2:{}'
                .format(batch_idx + 1, len(testloader), "{0:.5f}".format(loss),
                        "{0:.5f}".format(con1app_loss),
                        "{0:.5f}".format(con2app_loss),
                        "{0:.5f}".format(con1kp_loss),
                        "{0:.5f}".format(con2kp_loss),
                        "{0:.5f}".format(recon1_loss),
                        "{0:.5f}".format(recon2_loss)))

    print(
        'Testing Complete Loss:{} con1app:{} con2app:{} con1kp:{} con2kp:{} recon1:{} recon2:{}'
        .format("{0:.5f}".format(
            (running_total_loss / len(testloader))), "{0:.5f}".format(
                (running_con1app_loss / len(testloader))), "{0:.5f}".format(
                    (running_con2app_loss / len(testloader))),
                "{0:.5f}".format(
                    (running_con1kp_loss / len(testloader))), "{0:.5f}".format(
                        (running_con2kp_loss / len(testloader))),
                "{0:.5f}".format(
                    (running_recon1_loss / len(testloader))), "{0:.5f}".format(
                        (running_recon2_loss / len(testloader)))))