コード例 #1
0
def train(train_loader,
          model,
          criterion,
          optimizer,
          stat_2d,
          stat_3d,
          lr_init=None,
          lr_now=None,
          glob_step=None,
          lr_decay=None,
          gamma=None,
          max_norm=True):

    losses = utils.AverageMeter()

    model.train()

    # for i, (inps, tars) in enumerate(train_loader): # inps = (64, 32)
    pbar = tqdm(train_loader)
    for i, (inps, tars) in enumerate(pbar):  # inps = (64, 32)
        glob_step += 1
        if glob_step % lr_decay == 0 or glob_step == 1:
            lr_now = utils.lr_decay(optimizer, glob_step, lr_init, lr_decay,
                                    gamma)

        ### Input unnormalization
        inputs_unnorm = data_process.unNormalizeData(
            inps.data.cpu().numpy(), stat_2d['mean'], stat_2d['std'],
            stat_2d['dim_use'])  # 64, 64
        dim_2d_use = stat_2d['dim_use']
        inputs_use = inputs_unnorm[:, dim_2d_use]  # (64, 32)
        ### Input distance normalization
        inputs_dist_norm, _ = data_process.input_norm(
            inputs_use)  # (64, 32) , array
        input_dist = torch.tensor(inputs_dist_norm, dtype=torch.float32)

        ### Targets unnormalization
        targets_unnorm = data_process.unNormalizeData(
            tars.data.cpu().numpy(), stat_3d['mean'], stat_3d['std'],
            stat_3d['dim_use'])  # (64, 96)
        dim_3d_use = np.array([
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 22, 23, 24,
            25, 26, 36, 37, 38, 39, 40, 41, 45, 46, 47, 51, 52, 53, 54, 55, 56,
            57, 58, 59, 75, 76, 77, 78, 79, 80, 81, 82, 83
        ])
        targets_use = targets_unnorm[:, dim_3d_use]  # (51, )

        ### Targets distance normalization
        targets_dist_norm, _ = data_process.output_norm(targets_use)
        targets_dist = torch.tensor(targets_dist_norm, dtype=torch.float32)

        inputs = Variable(input_dist.cuda())
        targets = Variable(targets_dist.cuda(async=True))

        outputs = model(inputs)

        # calculate loss
        optimizer.zero_grad()
        loss = criterion(outputs, targets)
        losses.update(loss.item(), inputs.size(0))
        loss.backward()

        if max_norm:
            nn.utils.clip_grad_norm_(model.parameters(), max_norm=1)
        optimizer.step()

        # tqdm.set_postfix(loss='{:05.6f}'.format(losses.avg))
        pbar.set_postfix(tr_loss='{:05.6f}'.format(losses.avg))

    return glob_step, lr_now, losses.avg
コード例 #2
0
def test(test_loader, model, criterion, stat_2d, stat_3d, procrustes=False):

    losses = utils.AverageMeter()

    model.eval()

    all_dist = []

    pbar = tqdm(test_loader)
    for i, (inps, tars) in enumerate(pbar):

        ### input unnorm
        data_coord = data_process.unNormalizeData(inps.data.cpu().numpy(),
                                                  stat_2d['mean'],
                                                  stat_2d['std'],
                                                  stat_2d['dim_use'])  # 64, 64
        dim_2d_use = stat_2d['dim_use']
        data_use = data_coord[:, dim_2d_use]  # (64, 32)

        ### input dist norm
        data_dist_norm, data_dist_set = data_process.input_norm(
            data_use)  # (64, 32) , array
        data_dist = torch.tensor(data_dist_norm, dtype=torch.float32)

        # target unnorm
        label_coord = data_process.unNormalizeData(
            tars.data.cpu().numpy(), stat_3d['mean'], stat_3d['std'],
            stat_3d['dim_use'])  # (64, 96)
        dim_3d_use = np.array([
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 22, 23, 24,
            25, 26, 36, 37, 38, 39, 40, 41, 45, 46, 47, 51, 52, 53, 54, 55, 56,
            57, 58, 59, 75, 76, 77, 78, 79, 80, 81, 82, 83
        ])

        label_use = label_coord[:, dim_3d_use]  # (48, )
        # target dist norm
        label_dist_norm, label_dist_set = data_process.output_norm(label_use)
        label_dist = torch.tensor(label_dist_norm, dtype=torch.float32)

        inputs = Variable(data_dist.cuda())
        targets = Variable(label_dist.cuda(async=True))

        outputs = model(inputs)

        # calculate loss
        pred_coord = outputs
        loss = criterion(pred_coord, targets)  # 64 losses average

        losses.update(loss.item(), inputs.size(0))

        tars = targets
        pred = outputs

        # inputs_dist_set = np.reshape(targets_dist_set, (-1, 1))
        # inputs_dist_set = np.repeat(targets_dist_set, 48, axis=1)

        targets_dist = np.reshape(label_dist_set, (-1, 1))
        targets_dist_set = np.repeat(targets_dist, 48, axis=1)

        c = np.reshape(np.asarray([0, 0, 10]), (1, -1))
        c = np.repeat(c, 16, axis=0)
        c = np.reshape(c, (1, -1))
        c = np.repeat(c, inputs.size(0), axis=0)
        # c_set = np.repeat(np.asarray([0,0,10]), 16, axis=0)

        #### undist -> unnorm
        outputs_undist = (pred.data.cpu().numpy() * targets_dist_set) - c
        # outputs_undist = outputs_undist - c
        targets_undist = (tars.data.cpu().numpy() * targets_dist_set) - c
        # targets_undist = targets_undist - c

        outputs_use = outputs_undist
        targets_use = targets_undist  # (64, 48)

        if procrustes:
            for ba in range(inps.size(0)):
                gt = targets_use[ba].reshape(-1, 3)
                out = outputs_use[ba].reshape(-1, 3)  # (17,3)
                _, Z, T, b, c = get_transformation(gt, out, True)
                out = (b * out.dot(T)) + c
                outputs_use[ba, :] = out.reshape(1, 48)

        sqerr = (outputs_use - targets_use)**2

        # distance = np.zeros((sqerr.shape[0], 17))
        distance = np.zeros((sqerr.shape[0], 16))

        dist_idx = 0
        for k in np.arange(0, 16 * 3, 3):
            # for k in np.arange(0, 17 * 3, 3):

            distance[:, dist_idx] = np.sqrt(np.sum(sqerr[:, k:k + 3], axis=1))
            dist_idx += 1
        all_dist.append(distance)

        pbar.set_postfix(tt_loss='{:05.6f}'.format(losses.avg))

    all_dist = np.vstack(all_dist)
    joint_err = np.mean(all_dist, axis=0)
    ttl_err = np.mean(all_dist)
    # bar.finish()
    print(">>> error: {} <<<".format(ttl_err))
    return losses.avg, ttl_err
コード例 #3
0
def test(test_loader, model, criterion, stat_3d, procrustes=False):
    losses = utils.AverageMeter()

    model.eval()

    all_dist = []
    start = time.time()
    batch_time = 0
    bar = Bar(">>>", fill=">", max=len(test_loader))

    for i, (inps, tars) in enumerate(test_loader):
        inputs = Variable(inps.cuda())
        targets = Variable(tars.cuda())

        outputs = model(inputs)

        # calculate loss
        outputs_coord = outputs
        loss = criterion(outputs_coord, targets)

        losses.update(loss.item(), inputs.size(0))

        tars = targets

        # calculate erruracy
        targets_unnorm = data_process.unNormalizeData(tars.data.cpu().numpy(),
                                                      stat_3d["mean"],
                                                      stat_3d["std"],
                                                      stat_3d["dim_use"])
        outputs_unnorm = data_process.unNormalizeData(
            outputs.data.cpu().numpy(),
            stat_3d["mean"],
            stat_3d["std"],
            stat_3d["dim_use"],
        )

        # remove dim ignored
        dim_use = np.hstack((np.arange(3), stat_3d["dim_use"]))

        outputs_use = outputs_unnorm[:, dim_use]
        targets_use = targets_unnorm[:, dim_use]

        if procrustes:
            for ba in range(inps.size(0)):
                gt = targets_use[ba].reshape(-1, 3)
                out = outputs_use[ba].reshape(-1, 3)
                _, Z, T, b, c = get_transformation(gt, out, True)
                out = (b * out.dot(T)) + c
                outputs_use[ba, :] = out.reshape(1, 51)

        sqerr = (outputs_use - targets_use)**2

        distance = np.zeros((sqerr.shape[0], 17))
        dist_idx = 0
        for k in np.arange(0, 17 * 3, 3):
            distance[:, dist_idx] = np.sqrt(np.sum(sqerr[:, k:k + 3], axis=1))
            dist_idx += 1
        all_dist.append(distance)

        # update summary
        if (i + 1) % 100 == 0:
            batch_time = time.time() - start
            start = time.time()

        bar.suffix = "({batch}/{size}) | batch: {batchtime:.4}ms | Total: {ttl} | ETA: {eta:} | loss: {loss:.6f}".format(
            batch=i + 1,
            size=len(test_loader),
            batchtime=batch_time * 10.0,
            ttl=bar.elapsed_td,
            eta=bar.eta_td,
            loss=losses.avg,
        )
        bar.next()

    all_dist = np.vstack(all_dist)
    joint_err = np.mean(all_dist, axis=0)
    ttl_err = np.mean(all_dist)
    bar.finish()
    print(">>> error: {} <<<".format(ttl_err))
    return losses.avg, ttl_err
コード例 #4
0
def test(test_loader,
         model,
         criterion,
         stat_3d,
         device,
         procrustes=False,
         pck_thresholds=[50, 100, 150, 200, 250],
         noise_fun=lambda x: x,
         refine_dic=None,
         refine_coeff_fun=None,
         refine_extra_kwargs={},
         cache_prefix=None,
         visualize=False):
    model.eval()

    all_outputs = []
    all_targets = []
    losses = utils.AverageMeter()
    for i, (inps, tars) in enumerate(test_loader):
        inps_noise = noise_fun(inps)
        inputs = Variable(inps_noise.to(device))
        targets = Variable(tars.to(device))

        outputs = model(inputs)

        # calculate loss
        outputs_coord = outputs
        loss = criterion(outputs_coord, targets)
        losses.update(loss.item(), inputs.size(0))

        tars = targets

        # calculate erruracy
        targets_unnorm = data_process.unNormalizeData(tars.data.cpu().numpy(),
                                                      stat_3d['mean'],
                                                      stat_3d['std'],
                                                      stat_3d['dim_use'])
        outputs_unnorm = data_process.unNormalizeData(
            outputs.data.cpu().numpy(), stat_3d['mean'], stat_3d['std'],
            stat_3d['dim_use'])

        # remove dim ignored
        dim_use = np.hstack((np.arange(3), stat_3d['dim_use']))

        outputs_use = outputs_unnorm[:, dim_use]
        targets_use = targets_unnorm[:, dim_use]

        all_outputs.append(outputs_use)
        all_targets.append(targets_use)

    accu_frames = np.cumsum(test_loader.dataset.frames)
    all_outputs = np.split(np.concatenate(all_outputs, axis=0),
                           accu_frames)[:-1]
    all_targets = np.split(np.concatenate(all_targets, axis=0),
                           accu_frames)[:-1]

    start = time.time()
    seq_time = 0
    bar = Bar('>>>', fill='>', max=len(all_outputs))

    all_dist, all_pck = [], []
    for i, (outputs_use,
            targets_use) in enumerate(zip(all_outputs, all_targets)):
        if refine_dic is not None:
            origin = outputs_use
            outputs_use, _ = ru.refine(outputs_use, refine_dic,
                                       refine_coeff_fun, **refine_extra_kwargs)

            if visualize:
                visual = [
                    ru.convert_to_pose_16(seq.reshape([-1, 17, 3]))
                    for seq in [outputs_use, origin, targets_use]
                ]
                ru.plot_pose_seq(visual, plot_axis=True, r=1000)

        if procrustes:
            for frame in range(outputs_use.shape[0]):
                gt = targets_use[frame].reshape(-1, 3)
                out = outputs_use[frame].reshape(-1, 3)
                _, Z, T, b, c = get_transformation(gt,
                                                   out,
                                                   True,
                                                   reflection=False)
                out = (b * out.dot(T)) + c
                outputs_use[frame, :] = out.reshape(1, 51)

        for pred, gt in zip(outputs_use, targets_use):
            pred, gt = pred.reshape([-1, 3]), gt.reshape([-1, 3])
            all_dist.append(mpjpe_fun(pred, gt))
            all_pck.append(pck_fun(pred, gt, thresholds=pck_thresholds))

        # update summary
        seq_time = time.time() - start
        start = time.time()

        bar.suffix = '({seq}/{size}) | seq: {seqtime:.4}s | Total: {ttl} | ETA: {eta:} | mpjpe: {loss:.6f}' \
            .format(seq=i + 1,
                    size=len(all_outputs),
                    seqtime=seq_time,
                    ttl=bar.elapsed_td,
                    eta=bar.eta_td,
                    loss=np.mean(all_dist))
        bar.next()

    all_dist = np.vstack(all_dist)
    all_pck = np.array(all_pck)
    mpjpe = np.mean(all_dist)
    if cache_prefix:
        with open('cache/{}_.pkl'.format(cache_prefix), 'wb') as f:
            pickle.dump({'mpjpe': all_dist, 'pck': all_pck}, f)
    pck = np.mean(all_pck, axis=0)
    bar.finish()
    print(">>> error: {:4f}, pck: {} <<<".format(
        mpjpe, ' '.join(['{:4f}'.format(val) for val in pck])))
    return losses.avg, mpjpe, pck
コード例 #5
0
ファイル: main.py プロジェクト: waiting-gy/3dPoseBaseline
def test(test_loader, model, criterion, stat_3d, procrustes=False):
    losses = utils.AverageMeter()

    model.eval()

    all_dist = []
#    start = time.time()
    batch_time = 0
    bar = Bar('>>>', fill='>', max=len(test_loader))
#
    test_loader = tqdm(test_loader, dynamic_ncols=True)
    fig = plt.figure(figsize=(9.6, 5.4))#1920:1080
    stat_2d = torch.load(os.path.join(opt.data_dir, 'stat_2d.pth.tar'))
#
    for i, (inps, tars) in enumerate(test_loader):
        start = time.time()
        inputs = Variable(inps.cuda())
        targets = Variable(tars.cuda(async=True))

        outputs, outputs_inputs = model(inputs)
        # outputs = model(inputs)

#        print('input:',((inputs)))#16*2
#        print('input:',((inputs[0])))#16*2
#        print('output:',(len(outputs[0])))#16*3
#        print('targets:',(len(targets[0])))#16*3

        # calculate loss

        # ###########
        # alpha = 0.0
        # loss1 = criterion(outputs[0], targets)
        # loss2 = criterion(outputs[1], targets)
        # loss = alpha * loss1 + (1.0 - alpha) * loss2
        # ########
        outputs_coord = outputs

        loss = criterion(outputs, targets)
        loss_input = criterion(outputs_inputs, inputs)
        loss = loss + loss_input

        losses.update(loss.item(), inputs.size(0))

        tars = targets

        # calculate erruracy
        #inputs_unnorm = data_process.unNormalizeData(inps.data.cpu().numpy(), stat_2d['mean'], stat_2d['std'], stat_2d['dim_use'])
        targets_unnorm = data_process.unNormalizeData(tars.data.cpu().numpy(), stat_3d['mean'], stat_3d['std'], stat_3d['dim_use'])
        outputs_unnorm = data_process.unNormalizeData(outputs.data.cpu().numpy(), stat_3d['mean'], stat_3d['std'], stat_3d['dim_use'])

#        print(outputs_unnorm.shape[0])
#
#        print('outputs_unnorm:',((outputs_unnorm)))#16*2

        #_max = 0
        #_min = 10000

        #org_path = r"./data/jsonAlpha_one2/"
        #filelist = strsort(os.listdir(org_path))
        ##print(len(filelist))


        #with open(os.path.join(org_path,filelist[i]),encoding='utf8')as fp:
        #    json_data = json.load(fp)
        #spine_x = json_data['people'][0]['pose_keypoints_2d'][24]
        #spine_y = json_data['people'][0]['pose_keypoints_2d'][25]
        #spine_x = spine_x
        #spine_y = spine_y

        #print(spine_x)

        #for k in range(outputs_unnorm.shape[0]):
        #    for j in range(32):
        #       tmp = outputs_unnorm[k][j * 3 + 2] # tmp = z
        #       outputs_unnorm[k][j * 3 + 2] = outputs_unnorm[k][j * 3 + 1]# z = y
        #       outputs_unnorm[k][j * 3 + 1] = tmp # y = z
        #       if outputs_unnorm[k][j * 3 + 2] > _max:
        #           _max = outputs_unnorm[k][j * 3 + 2]# z max
        #       if outputs_unnorm[k][j * 3 + 2] < _min:
        #           _min = outputs_unnorm[k][j * 3 + 2]# z min
        # #plot出的姿态是倒立的,通过该方法将其校正
        #for k in range(outputs_unnorm.shape[0]):
        #    for j in range(32):
        #        outputs_unnorm[k][j * 3 + 2] = _max - outputs_unnorm[k][j * 3 + 2] + _min# z = max-z
        #        outputs_unnorm[k][j * 3] += (spine_x - 630)# x
        #        outputs_unnorm[k][j * 3 + 2] += (500 - spine_y)# z

        #for k in range(inputs_unnorm.shape[0]):
        #    for j in range(32):
        #       #tmp1 = inputs_unnorm[k][j * 2 + 2] # tmp = z
        #       #inputs_unnorm[k][j * 2 + 2] = inputs_unnorm[k][j * 3 + 1]# z = y
        #       #inputs_unnorm[k][j * 2 + 1] = tmp1 # y = z
        #       if inputs_unnorm[k][j * 2 + 1] > _max:
        #           _max = inputs_unnorm[k][j * 2 + 1]# z max
        #       if inputs_unnorm[k][j * 2 + 1] < _min:
        #           _min = inputs_unnorm[k][j * 2 + 1]# z min
        # #plot出的姿态是倒立的,通过该方法将其校正
        #for k in range(inputs_unnorm.shape[0]):
        #    for j in range(32):
        #        inputs_unnorm[k][j * 2 + 1] = _max - inputs_unnorm[k][j * 2 + 1] + _min# z = max-z
        #        #inputs_unnorm[k][j * 3] += (spine_x - 630)# x
        #        #inputs_unnorm[k][j * 3 + 2] += (500 - spine_y)# z

#        for k in range(len(outputs_unnorm)):
#           for j in range(32):
#              tmp0 = targets_unnorm[k][j * 3 + 2]# tmp = z
#              targets_unnorm[k][j * 3 + 2] = targets_unnorm[k][j * 3 + 1]# z = y
#              targets_unnorm[k][j * 3 + 1] = tmp0 # y = z
#
#              tmp = outputs_unnorm[k][j * 3 + 2]# tmp = z
#              outputs_unnorm[k][j * 3 + 2] = outputs_unnorm[k][j * 3 + 1]# z = y
#              outputs_unnorm[k][j * 3 + 1] = tmp # y = z
#
#        for k in range(len(outputs_unnorm)):
#           hip_z0 = targets_unnorm[k][3]
#           for j in range(32):
#              targets_unnorm[k][j * 3 + 2] = targets_unnorm[k][j * 3 + 2] - 2*(targets_unnorm[k][j * 3 + 2] - hip_z0)
#
#           hip_z = outputs_unnorm[k][3]
#           for j in range(32):
#              outputs_unnorm[k][j * 3 + 2] = outputs_unnorm[k][j * 3 + 2] - 2*(outputs_unnorm[k][j * 3 + 2] - hip_z)

        #for pp in range(len(outputs_unnorm)):

#
           #ax2 = fig.add_subplot(131)
           #ax2.get_xaxis().set_visible(False)
           #ax2.get_yaxis().set_visible(False)
           #ax2.set_axis_off()
           #ax2.set_title('Input')
           #org_path = r"/home/ubuntu/gaoyu/alphapose/Video3D3_cmu/sep-json/"
           #filelist = strsort(os.listdir(org_path))
           #print(filelist[i])
           #img2d = imgplt.imread(os.path.join('/home/ubuntu/gaoyu/alphapose/Video3D3_cmu/vis/', '{0}.jpg'.format((filelist[i].split('.')[0]).zfill(12))))
           #ax2.imshow(img2d, aspect='equal')
#
#           ax0 = fig.add_subplot(131, projection='3d')
#           ax0.view_init(0, 300)
#           viz.show3Dpose( targets_unnorm[pp], ax0, add_labels=True, title = 'GroundTruth')
           #ax0 = fig.add_subplot(132)
           #ax0.view_init(0, 300)
           #viz.show2Dpose( inputs_unnorm[pp]*2.4, ax0, add_labels=True, title = '2DPose Input')

           #Reconstruction = 1/((time.time() - start))
           #start = time.time()
           #ax1 = fig.add_subplot(133, projection='3d')
#          # print(len(outputs_unnorm[pp])) #96
           #ax1.view_init(0, 300) #默认值30,120
           #viz.show3Dpose( outputs_unnorm[pp], ax1, add_labels=True, title = 'Reconstruction:  {}FPS'.format(int(Reconstruction)))

           #plt.pause(0.0000001)
           #plt.clf()


#        print('targets_unnorm:',len(outputs_unnorm[0]))#96=32*3
#        print('outputs_unnorm:',len(outputs_unnorm[0]))#96=32*3
        # remove dim ignored
        dim_use = np.hstack((np.arange(3), stat_3d['dim_use']))

        outputs_use = outputs_unnorm[:, dim_use]
        targets_use = targets_unnorm[:, dim_use]

#        print('targets_unnorm:',len(outputs_use[0]))#51=17*3
#        print('outputs_unnorm:',len(outputs_use[0]))#51=17*3


        if procrustes:
            for ba in range(inps.size(0)):
                gt = targets_use[ba].reshape(-1, 3)
                out = outputs_use[ba].reshape(-1, 3)
                _, Z, T, b, c = get_transformation(gt, out, True)
                out = (b * out.dot(T)) + c
                outputs_use[ba, :] = out.reshape(1, 51)

        sqerr = (outputs_use - targets_use) ** 2

        distance = np.zeros((sqerr.shape[0], 17))
        dist_idx = 0
        for k in np.arange(0, 17 * 3, 3):
            distance[:, dist_idx] = np.sqrt(np.sum(sqerr[:, k:k + 3], axis=1))
            dist_idx += 1
        all_dist.append(distance)

        # update summary
        if (i + 1) % 100 == 0:
#            batch_time = time.time() - start
            batch_time = 1
#            start = time.time()

        #bar.suffix = '({batch}/{size}) | batch: {batchtime:.4}ms | Total: {ttl} | ETA: {eta:} | loss: {loss:.6f}' \
        #    .format(batch=i + 1,
        #            size=len(test_loader),
        #            batchtime=batch_time * 10.0,
        #            ttl=bar.elapsed_td,
        #            eta=bar.eta_td,
        #            loss=losses.avg)
        #bar.next()

#
        test_loader.set_description(
                    '({batch}/{size}) | batch: {batchtime:.4}ms | loss: {loss:.6f}'.format(
                        batch=i + 1,
                        size=len(test_loader),
                        batchtime=batch_time * 10.0,
                        loss=losses.avg)
                    )
    test_loader.close()
#

    all_dist = np.vstack(all_dist)
    joint_err = np.mean(all_dist, axis=0)
    ttl_err = np.mean(all_dist)
#    bar.finish()
    print (">>> error: {} <<<".format(ttl_err))
    return losses.avg, ttl_err
コード例 #6
0
def test(test_loader, model, criterion, stat_2d, stat_3d, procrustes=False):

    losses = utils.AverageMeter()

    model.eval()

    all_dist = []
    # start = time.time()
    # batch_time = 0
    # bar = Bar('>>>', fill='>', max=len(test_loader))

    # for i, (inps, tars) in enumerate(test_loader):

    pbar = tqdm(test_loader)
    for i, (inps, tars) in enumerate(pbar):

        ### input unnorm
        data_coord = data_process.unNormalizeData(inps.data.cpu().numpy(),
                                                  stat_2d['mean'],
                                                  stat_2d['std'],
                                                  stat_2d['dim_use'])  # 64, 64
        dim_2d_use = stat_2d['dim_use']
        data_use = data_coord[:, dim_2d_use]  # (64, 32)

        ### input dist norm
        data_dist_norm, data_dist_set = data_process.input_norm(
            data_use)  # (64, 32) , array
        data_dist = torch.tensor(data_dist_norm, dtype=torch.float32)

        # target unnorm
        label_coord = data_process.unNormalizeData(
            tars.data.cpu().numpy(), stat_3d['mean'], stat_3d['std'],
            stat_3d['dim_use'])  # (64, 96)
        dim_3d_use = np.array([
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 22, 23, 24,
            25, 26, 36, 37, 38, 39, 40, 41, 45, 46, 47, 51, 52, 53, 54, 55, 56,
            57, 58, 59, 75, 76, 77, 78, 79, 80, 81, 82, 83
        ])

        label_use = label_coord[:, dim_3d_use]  # (48, )
        # target dist norm
        label_dist_norm, label_dist_set = data_process.output_norm(label_use)
        label_dist = torch.tensor(label_dist_norm, dtype=torch.float32)

        inputs = Variable(data_dist.cuda())
        targets = Variable(label_dist.cuda(async=True))

        outputs = model(inputs)

        # calculate loss
        pred_coord = outputs
        loss = criterion(pred_coord, targets)  # 64 losses average

        losses.update(loss.item(), inputs.size(0))

        tars = targets
        pred = outputs

        # inputs_dist_set = np.reshape(targets_dist_set, (-1, 1))
        # inputs_dist_set = np.repeat(targets_dist_set, 48, axis=1)

        targets_dist = np.reshape(label_dist_set, (-1, 1))
        targets_dist_set = np.repeat(targets_dist, 48, axis=1)

        c = np.reshape(np.asarray([0, 0, 10]), (1, -1))
        c = np.repeat(c, 16, axis=0)
        c = np.reshape(c, (1, -1))
        c = np.repeat(c, inputs.size(0), axis=0)
        # c_set = np.repeat(np.asarray([0,0,10]), 16, axis=0)

        #### undist -> unnorm
        outputs_undist = (pred.data.cpu().numpy() * targets_dist_set) - c
        # outputs_undist = outputs_undist - c
        targets_undist = (tars.data.cpu().numpy() * targets_dist_set) - c
        # targets_undist = targets_undist - c

        #####
        '''targets_unnorm : 
         -8095.789092890324,
 3447.739184577949,
 -12287.197684264487,
 29496.41833447592,
 91899.94409139897,
 55478.075331596345,
 -26361.992715253342,
        '''
        # targets_unnorm = data_process.unNormalizeData(outputs_undist, stat_3d['mean'], stat_3d['std'],
        #                                               dim_3d_use)
        # outputs_unnorm = data_process.unNormalizeData(targets_undist, stat_3d['mean'], stat_3d['std'],
        #                                               dim_3d_use)

        # inputs_unnorm = data_process.unNormalizeData(inputs.data.cpu().numpy(), stat_2d['mean'], stat_2d['std'], stat_2d['dim_use'])
        # calculate erruracy

        # targets_unnorm = data_process.unNormalizeData(tars.data.cpu().numpy(), stat_3d['mean'], stat_3d['std'], stat_3d['dim_use'])
        # outputs_unnorm = data_process.unNormalizeData(outputs.data.cpu().numpy(), stat_3d['mean'], stat_3d['std'], stat_3d['dim_use'])
        # remove dim ignored

        # dim_use = np.hstack((np.arange(3), stat_3d['dim_use']))
        # dim_use = dim_3d_use

        # dim_2d_use = stat_2d['dim_use']

        # stat_3d['dim_use'] (48., )
        # outputs_use = outputs_unnorm[:, dim_use]
        # targets_use = targets_unnorm[:, dim_use] # (64, 48)

        outputs_use = outputs_undist
        targets_use = targets_undist  # (64, 48)

        # inputs_use = inputs_unnorm[:, dim_2d_use] # (64, 32)

        if procrustes:
            for ba in range(inps.size(0)):
                gt = targets_use[ba].reshape(-1, 3)
                out = outputs_use[ba].reshape(-1, 3)  # (17,3)
                _, Z, T, b, c = get_transformation(gt, out, True)
                out = (b * out.dot(T)) + c
                outputs_use[ba, :] = out.reshape(1, 48)

        sqerr = (outputs_use - targets_use)**2

        # distance = np.zeros((sqerr.shape[0], 17))
        distance = np.zeros((sqerr.shape[0], 16))

        dist_idx = 0
        for k in np.arange(0, 16 * 3, 3):
            # for k in np.arange(0, 17 * 3, 3):

            distance[:, dist_idx] = np.sqrt(np.sum(sqerr[:, k:k + 3], axis=1))
            dist_idx += 1
        all_dist.append(distance)

        # # update summary
        # if (i + 1) % 100 == 0:
        #     batch_time = time.time() - start
        #     start = time.time()
        #
        # bar.suffix = '({batch}/{size}) | batch: {batchtime:.4}ms | Total: {ttl} | ETA: {eta:} | loss: {loss:.6f}' \
        #     .format(batch=i + 1,
        #             size=len(test_loader),
        #             batchtime=batch_time * 10.0,
        #             ttl=bar.elapsed_td,
        #             eta=bar.eta_td,
        #             loss=losses.avg)
        # bar.next()

        pbar.set_postfix(tt_loss='{:05.6f}'.format(losses.avg))

    all_dist = np.vstack(all_dist)
    joint_err = np.mean(all_dist, axis=0)
    ttl_err = np.mean(all_dist)
    # bar.finish()
    print(">>> error: {} <<<".format(ttl_err))
    return losses.avg, ttl_err
コード例 #7
0
def train(train_loader,
          model,
          criterion,
          optimizer,
          stat_2d,
          stat_3d,
          lr_init=None,
          lr_now=None,
          glob_step=None,
          lr_decay=None,
          gamma=None,
          max_norm=True):

    losses = utils.AverageMeter()

    model.train()

    # start = time.time()
    # batch_time = 0
    # bar = Bar('>>>', fill='>', max=len(train_loader))

    # for i, (inps, tars) in enumerate(train_loader): # inps = (64, 32)
    pbar = tqdm(train_loader)
    for i, (inps, tars) in enumerate(pbar):  # inps = (64, 32)
        glob_step += 1
        if glob_step % lr_decay == 0 or glob_step == 1:
            lr_now = utils.lr_decay(optimizer, glob_step, lr_init, lr_decay,
                                    gamma)

        ### Input unnormalization
        inputs_unnorm = data_process.unNormalizeData(
            inps.data.cpu().numpy(), stat_2d['mean'], stat_2d['std'],
            stat_2d['dim_use'])  # 64, 64
        # unnorm size = 64, make zeros mtrx and just do unnorm, so (0 * stdMat) + meanMat => 64, 64 // junk values the other position except original 16 joints
        dim_2d_use = stat_2d['dim_use']
        # select useful 32 joint using dim_2d-use  => 64, 32
        inputs_use = inputs_unnorm[:, dim_2d_use]  # (64, 32)
        ### Input distance normalization
        inputs_dist_norm, _ = data_process.input_norm(
            inputs_use)  # (64, 32) , array
        input_dist = torch.tensor(inputs_dist_norm, dtype=torch.float32)

        ### Targets unnormalization
        targets_unnorm = data_process.unNormalizeData(
            tars.data.cpu().numpy(), stat_3d['mean'], stat_3d['std'],
            stat_3d['dim_use'])  # (64, 96)
        dim_3d_use = np.array([
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 18, 19, 20, 21, 22, 23, 24,
            25, 26, 36, 37, 38, 39, 40, 41, 45, 46, 47, 51, 52, 53, 54, 55, 56,
            57, 58, 59, 75, 76, 77, 78, 79, 80, 81, 82, 83
        ])
        targets_use = targets_unnorm[:, dim_3d_use]  # (51, )
        ### Targets distance normalization
        targets_dist_norm, _ = data_process.output_norm(targets_use)
        targets_dist = torch.tensor(targets_dist_norm, dtype=torch.float32)

        inputs = Variable(input_dist.cuda())
        targets = Variable(targets_dist.cuda(async=True))

        outputs = model(inputs)

        # calculate loss
        optimizer.zero_grad()
        loss = criterion(outputs, targets)
        losses.update(loss.item(), inputs.size(0))
        loss.backward()

        if max_norm:
            nn.utils.clip_grad_norm_(model.parameters(), max_norm=1)
        optimizer.step()

        # tqdm.set_postfix(loss='{:05.6f}'.format(losses.avg))
        pbar.set_postfix(tr_loss='{:05.6f}'.format(losses.avg))

    #     # update summary
    #     if (i + 1) % 100 == 0:
    #         batch_time = time.time() - start
    #         start = time.time()
    #
    #     bar.suffix = '({batch}/{size}) | batch: {batchtime:.4}ms | Total: {ttl} | ETA: {eta:} | loss: {loss:.4f}' \
    #         .format(batch=i + 1,
    #                 size=len(train_loader),
    #                 batchtime=batch_time * 10.0,
    #                 ttl=bar.elapsed_td,
    #                 eta=bar.eta_td,
    #                 loss=losses.avg)
    #     bar.next()
    #
    # bar.finish()

    return glob_step, lr_now, losses.avg