Beispiel #1
0
def temp_visualize(eval_dataset, model, stats, opt):
    # visualze model prediction batch by batch
    model.eval()
    data = np.load('./pics/pts1.npy').astype(np.float32)
    data = data[:,2:]
    # normalize the data
    mean_vec = stats['mean_2d'][stats['dim_use_2d']]
    std_vec = stats['std_2d'][stats['dim_use_2d']]
    data = (data-mean_vec)/std_vec
    data = torch.from_numpy(data.astype(np.float32))
    data = data.cuda()
    # forward pass to get prediction
    prediction = model(data)
    # un-normalize the data
    skeleton_2d = data_utils.unNormalizeData(data.data.cpu().numpy(), 
    stats['mean_2d'], stats['std_2d'], stats['dim_ignore_2d'])   
    skeleton_3d_pred = data_utils.unNormalizeData(prediction.data.cpu().numpy(), 
    stats['mean_3d'], stats['std_3d'], stats['dim_ignore_3d'])
    # visualizing
    plt.figure()
    ax = plt.subplot(1, 2, 1)
    viz.show2Dpose(skeleton_2d[0], ax)
    plt.gca().invert_yaxis()
    ax = plt.subplot(1, 2, 2, projection='3d')
    viz.show3Dpose(skeleton_3d_pred[0], ax, pred=True)              
    plt.show()
            # rotate the axes and update
#            for angle in range(0, 360, 5):
#                for ax in axes:
#                    ax.view_init(30, angle)
#                plt.draw()
#                plt.pause(.001)
#            input('Press enter to view next batch.')
    return
Beispiel #2
0
def visualize(eval_dataset, model, stats, opt, save=False, save_dir=None):
    # visualze model prediction batch by batch
    batch_size = 9
    # how many batches to save
    if save:
        num_batches = 10
        current_batch = 1
    model.eval()
    eval_loader = torch.utils.data.DataLoader(eval_dataset, batch_size, 
                                              shuffle = True, num_workers = opt.num_threads)     
    for batch_idx, batch in enumerate(eval_loader):
        if save and current_batch > num_batches:
            break
        data = batch[0]
        target = batch[1]
        if opt.cuda:
            with torch.no_grad():
                # move to GPU
                data, target = data.cuda(), target.cuda()
        # forward pass to get prediction
        prediction = model(data)
        # un-normalize the data
        skeleton_2d = data_utils.unNormalizeData(data.data.cpu().numpy(), 
        stats['mean_2d'], stats['std_2d'], stats['dim_ignore_2d'])
        skeleton_3d_gt = data_utils.unNormalizeData(target.data.cpu().numpy(), 
        stats['mean_3d'], stats['std_3d'], stats['dim_ignore_3d'])    
        skeleton_3d_pred = data_utils.unNormalizeData(prediction.data.cpu().numpy(), 
        stats['mean_3d'], stats['std_3d'], stats['dim_ignore_3d'])
        # visualizing
        if save:
            plt.ioff()
        f = plt.figure(figsize=(16, 8))
        axes = []
        for sample_idx in range(batch_size):
            ax = plt.subplot(3, 9, 3*sample_idx + 1)
            viz.show2Dpose(skeleton_2d[sample_idx], ax)
            plt.gca().invert_yaxis()
            ax = plt.subplot(3, 9, 3*sample_idx + 2, projection='3d')
            viz.show3Dpose(skeleton_3d_gt[sample_idx], ax)
            ax = plt.subplot(3, 9, 3*sample_idx + 3, projection='3d')
            viz.show3Dpose(skeleton_3d_pred[sample_idx], ax, pred=True)  
            viz.show3Dpose(skeleton_3d_gt[sample_idx], ax, gt=True)   
            axes.append(ax)      
        adjust_figure(left = 0.05, right = 0.95, bottom = 0.05, top = 0.95,
                      wspace = 0.3, hspace = 0.3)              
        if not save:
            plt.pause(0.5)
            # rotate the axes and update
            for angle in range(0, 360, 5):
                for ax in axes:
                    ax.view_init(30, angle)
                plt.draw()
                plt.pause(.001)
            input('Press enter to view next batch.')
        else:
            # save plot
            f.savefig(save_dir +'/'+ str(current_batch) + '.png')
        plt.close(f)     
        del axes
        if save:
            current_batch += 1
    return
Beispiel #3
0
def visualize_cascade(eval_dataset, cascade, stats, opt, save=False, save_dir=None):
    num_stages = len(cascade)
    # visualze model prediction batch by batch
    batch_size = 5
    # how many batches to save
    if save:
        num_batches = 10
        current_batch = 1
    for stage_model in cascade:
        stage_model.eval()
    eval_loader = torch.utils.data.DataLoader(eval_dataset, batch_size, 
                                              shuffle = False, 
                                              num_workers = opt.num_threads)     
    for batch_idx, batch in enumerate(eval_loader):
        if save and current_batch > num_batches:
            break
        data = batch[0]
        ## debug
        # enc_in = np.array([[648., 266], [679, 311], [688, 320], [693, 161],
        #            [620, 244], [526, 156], [642, 160], [590, 310],
        #            [505, 350], [380, 375], [491, 285],
        #            [543, 190], [572, 119], [515, 417], [518, 514],
        #            [512, 638]],dtype=np.float32)
        enc_in = data
        enc_in = enc_in.reshape(1, 32)
        # normalize
        data_mean_2d = stats['mean_2d']
        dim_to_use_2d = stats['dim_use_2d']
        data_std_2d = stats['std_2d']
        enc_in = (enc_in - data_mean_2d[dim_to_use_2d])/data_std_2d[dim_to_use_2d]
        data = torch.from_numpy(enc_in.astype(np.float32))
        ## End experiment 2019/10/16
        target = batch[1]
        # store predictions for each stage
        prediction_stages = []
        if opt.cuda:
            with torch.no_grad():
                # move to GPU
                data, target = data.cuda(), target.cuda()
        # forward pass to get prediction for the first stage
        prediction = cascade[0](data)
        prediction_stages.append(prediction)
        # prediction for later stages
        for stage_idx in range(1, num_stages):
            prediction = cascade[stage_idx](data)
            prediction_stages.append(prediction_stages[stage_idx-1] + prediction)
        # un-normalize the data
        skeleton_2d = data_utils.unNormalizeData(data.data.cpu().numpy(), 
        stats['mean_2d'], stats['std_2d'], stats['dim_ignore_2d'])
        skeleton_3d_gt = data_utils.unNormalizeData(target.data.cpu().numpy(), 
        stats['mean_3d'], stats['std_3d'], stats['dim_ignore_3d'])
        for stage_idx in range(num_stages):
            prediction_stages[stage_idx] = data_utils.unNormalizeData(prediction_stages[stage_idx].data.cpu().numpy(), 
            stats['mean_3d'], stats['std_3d'], stats['dim_ignore_3d'])
        ## save intermediate results
        # import scipy.io as sio
        # p3d = prediction_stages[0]
        # sio.savemat('./teaser_pose3d.mat', {'pred_3d':p3d.reshape(32,3),
        #             'pred_2d':np.array([[648., 266], [679, 311], [688, 320], [693, 161],
        #            [620, 244], [526, 156], [642, 160], [590, 310],
        #            [505, 350], [447, 348], [380, 375], [491, 285],
        #            [543, 190], [572, 119], [515, 417], [518, 514],
        #            [512, 638]])})        
        ## End Experiment 2019/10/16
        # visualizing
        if save:
            plt.ioff()
        f = plt.figure(figsize=(16, 8))
        axes = []
        for sample_idx in range(batch_size):
            for stage_idx in range(num_stages):
                ax = plt.subplot(batch_size, num_stages+1, 1+(num_stages+1)*sample_idx)
                viz.show2Dpose(skeleton_2d[sample_idx], ax)
                plt.gca().invert_yaxis()
                ax = plt.subplot(batch_size, num_stages+1, 
                                 2+stage_idx+(num_stages+1)*sample_idx, projection='3d')
                viz.show3Dpose(prediction_stages[stage_idx][sample_idx], ax, pred=True)  
                viz.show3Dpose(skeleton_3d_gt[sample_idx], ax, gt=True)   
                axes.append(ax)      
        adjust_figure(left = 0.05, right = 0.95, bottom = 0.05, top = 0.95,
                      wspace = 0.3, hspace = 0.3)              
        if not save:
            plt.pause(0.5)
            # rotate the axes and update
#            for angle in range(0, 360, 5):
#                for ax in axes:
#                    ax.view_init(30, angle)
#                plt.draw()
#                plt.pause(.001)
            input('Press enter to view next batch.')
        else:
            # save plot
            f.savefig(save_dir +'/'+ str(current_batch) + '.png')
        plt.close(f)     
        del axes
        if save:
            current_batch += 1    
    return