Ejemplo n.º 1
0
def main():
    opt.manualSeed = random.randint(1, 10000)
    random.seed(opt.manualSeed)
    torch.manual_seed(opt.manualSeed)

    opt.num_objects = 21
    opt.num_points = 1000
    opt.outf = 'trained_models/ycb/' + opt.output_dir
    opt.log_dir = 'experiments/logs/ycb/' + opt.output_dir
    opt.train_dir = 'experiments/tb/ycb/' + opt.output_dir + '/train'
    opt.test_dir = 'experiments/tb/ycb/' + opt.output_dir + '/test'
    opt.repeat_epoch = 1
    if not os.path.exists(opt.outf): os.makedirs(opt.outf, exist_ok=True)
    if not os.path.exists(opt.log_dir): os.makedirs(opt.log_dir, exist_ok=True)
    if not os.path.exists(opt.train_dir):
        os.makedirs(opt.train_dir, exist_ok=True)
    if not os.path.exists(opt.test_dir):
        os.makedirs(opt.test_dir, exist_ok=True)

    opt.repeat_epoch = 1

    estimator = PoseNet(num_points=opt.num_points,
                        num_obj=opt.num_objects,
                        object_max=opt.object_max)
    estimator.cuda()

    isFirstInitLastDatafolder = True

    if opt.resume_posenet != '':
        psp_estimator = torch.load(
            'trained_models/ycb/pose_model_26_0.012863246640872631.pth')
        pretrained_estimator = torch.load('{0}/{1}'.format(
            opt.outf, opt.resume_posenet))
        estimator_dict = estimator.state_dict()

        psp_dict = {
            k: v
            for k, v in psp_estimator.items() if k.find('cnn.model') == 0
        }
        pretrained_dict = {
            k: v
            for k, v in pretrained_estimator.items()
            if k.find('cnn.model') != 0
        }

        estimator_dict.update(psp_dict)
        estimator_dict.update(pretrained_dict)
        estimator.load_state_dict(estimator_dict)
    else:
        psp_estimator = torch.load(
            'trained_models/ycb/pose_model_26_0.012863246640872631.pth')
        psp_dict = {
            k: v
            for k, v in psp_estimator.items() if k.find('cnn.model') == 0
        }
        estimator_dict = estimator.state_dict()

        estimator_dict.update(psp_dict)
        estimator.load_state_dict(estimator_dict)

    opt.decay_start = False
    optimizer = optim.Adam(estimator.parameters(), lr=opt.lr)

    dataset = PoseDataset_ycb('train', opt.num_points, False, opt.dataset_root,
                              opt.noise_trans, 'ori', False)

    dataloader = torch.utils.data.DataLoader(dataset,
                                             shuffle=False,
                                             num_workers=opt.workers)
    test_dataset = PoseDataset_ycb('test', opt.num_points, False,
                                   opt.dataset_root, 0.0, 'ori', False)
    testdataloader = torch.utils.data.DataLoader(test_dataset,
                                                 shuffle=False,
                                                 num_workers=opt.workers)

    opt.sym_list = dataset.get_sym_list()
    opt.num_points_mesh = dataset.get_num_points_mesh()

    print(
        '>>>>>>>>----------Dataset loaded!---------<<<<<<<<\nlength of the training set: {0}\nlength of the testing set: {1}\nnumber of sample points on mesh: {2}\nsymmetry object list: {3}'
        .format(len(dataset), len(test_dataset), opt.num_points_mesh,
                opt.sym_list))

    criterion = Loss(opt.num_points_mesh, opt.sym_list)

    dis_vector_last_map = {key: [] for key in range(0, opt.num_objects)}
    for i in range(0, opt.num_objects):
        dis_vector_last_map[i] = None

    best_test = np.Inf

    if opt.start_epoch == 1:
        for log in os.listdir(opt.log_dir):
            os.remove(os.path.join(opt.log_dir, log))
    st_time = time.time()

    for epoch in range(opt.start_epoch, opt.nepoch):
        logger = setup_logger(
            'epoch%d' % epoch,
            os.path.join(opt.log_dir, 'epoch_%d_log.txt' % epoch))
        logger.info('Train time {0}'.format(
            time.strftime("%Hh %Mm %Ss", time.gmtime(time.time() - st_time)) +
            ', ' + 'Training started'))
        train_count = 0
        train_dis_avg = 0.0
        global_train_dis = 0.0

        estimator.train()
        optimizer.zero_grad()

        for rep in range(opt.repeat_epoch):
            for i, data in enumerate(dataloader, 0):
                list_points, list_choose, list_img, list_target, list_model_points, list_idx, list_filename, \
                list_full_img, list_focal_length, list_principal_point, list_motion = data

                for list_index in range(len(list_points)):
                    if opt.dataset == 'ycb':
                        points, choose, img, target, model_points, idx, filename, full_img, focal_length, principal_point \
                            , motion = list_points[list_index], list_choose[list_index], list_img[list_index], \
                                       list_target[list_index], list_model_points[list_index], list_idx[list_index], \
                                       list_filename[list_index], list_full_img[list_index], list_focal_length[
                                           list_index], \
                                       list_principal_point[list_index], list_motion[list_index]
                        datafolder = filename[0].split('/')[1]
                        if isFirstInitLastDatafolder:
                            lastdatafolder = datafolder
                            isFirstInitLastDatafolder = False
                        if datafolder != lastdatafolder:
                            for i in range(0, opt.num_objects):
                                dis_vector_last_map[i] = None

                            optimizer.step()
                            optimizer.zero_grad()
                            train_dis_avg = 0
                            estimator.temporalClear(opt.object_max,
                                                    opt.mem_length)
                        lastdatafolder = datafolder
                    elif opt.dataset == 'linemod':
                        list_points, list_choose, list_img, list_target, list_model_points, list_idx, list_filename = data
                        points, choose, img, target, model_points, idx, filename = list_points[
                            0]

                    points, choose, img, target, model_points, idx = points.cuda(), \
                                                                     choose.cuda(), \
                                                                     img.cuda(), \
                                                                     target.cuda(), \
                                                                     model_points.cuda(), \
                                                                     idx.cuda()

                    pred_r, pred_t, pred_c, x_return = estimator(
                        img, points, choose, idx, focal_length,
                        principal_point, motion, True)
                    loss, dis, new_points, new_target, dis_vector = criterion(
                        pred_r, pred_t, pred_c,
                        dis_vector_last_map[idx.item()], target, model_points,
                        idx, x_return, opt.w, False,
                        float(opt.loss_stable_alpha))
                    dis_vector_last_map[idx.item()] = dis_vector
                    loss.backward(retain_graph=True)

                    logger.info(
                        'Train time {0} Frame {1} Object {2}, Loss = {3}'.
                        format(
                            time.strftime("%Hh %Mm %Ss",
                                          time.gmtime(time.time() - st_time)),
                            filename, idx.item(), dis))
                    train_dis_avg += dis.item()
                    global_train_dis += dis.item()
                    train_count += 1
                    if train_count % (len(list_points) * opt.batch_size) == 0:
                        logger.info(
                            'Train time {0} Epoch {1} Batch {2} Frame {3} Avg_dis:{4}'
                            .format(
                                time.strftime(
                                    "%Hh %Mm %Ss",
                                    time.gmtime(time.time() - st_time)), epoch,
                                int(train_count / opt.batch_size), train_count,
                                train_dis_avg /
                                (len(list_points) * opt.batch_size)))
                        optimizer.step()
                        optimizer.zero_grad()
                        train_dis_avg = 0

                    if train_count != 0 and train_count % 1000 == 0:
                        torch.save(
                            estimator.state_dict(),
                            '{0}/pose_model_current.pth'.format(opt.outf))

        print(
            '>>>>>>>>----------epoch {0} train finish---------<<<<<<<<'.format(
                epoch))
        global_train_dis = 0.0

        logger = setup_logger(
            'epoch%d_test' % epoch,
            os.path.join(opt.log_dir, 'epoch_%d_test_log.txt' % epoch))
        logger.info('Test time {0}'.format(
            time.strftime("%Hh %Mm %Ss", time.gmtime(time.time() - st_time)) +
            ', ' + 'Testing started'))
        test_dis = 0.0
        test_count = 0
        estimator.eval()

        for i in range(0, opt.num_objects):
            dis_vector_last_map[i] = None

        with torch.no_grad():
            isFirstInitLastDatafolder = True
            for j, data in enumerate(testdataloader, 0):
                if opt.dataset == 'ycb':
                    list_points, list_choose, list_img, list_target, list_model_points, list_idx, list_filename, \
                    list_full_img, list_focal_length, list_principal_point, list_motion = data
                for list_index in range(len(list_points)):
                    points, choose, img, target, model_points, idx, filename, full_img, focal_length, principal_point, motion \
                        = list_points[list_index], list_choose[list_index], list_img[list_index], \
                          list_target[list_index], list_model_points[list_index], list_idx[list_index], \
                          list_filename[list_index], list_full_img[list_index], list_focal_length[list_index], \
                          list_principal_point[list_index], list_motion[list_index]
                    datafolder = filename[0].split('/')[1]
                    filehead = filename[0].split('/')[2]
                    if isFirstInitLastDatafolder:
                        lastdatafolder = datafolder
                        isFirstInitLastDatafolder = False
                    if datafolder != lastdatafolder:
                        train_dis_avg = 0
                        estimator.temporalClear(opt.object_max)
                    lastdatafolder = datafolder
                    points, choose, img, target, model_points, idx = points.cuda(), \
                                                                     choose.cuda(), \
                                                                     img.cuda(), \
                                                                     target.cuda(), \
                                                                     model_points.cuda(), \
                                                                     idx.cuda()
                    cloud_path = "experiments/clouds/ycb/{0}/{1}/{2}/{3}_{4}".format(
                        opt.output_dir, epoch, datafolder, filehead,
                        int(idx))  # folder to save logs
                    if not os.path.exists(
                            "experiments/clouds/ycb/{0}/{1}/{2}".format(
                                opt.output_dir, epoch, datafolder)):
                        os.makedirs(
                            "experiments/clouds/ycb/{0}/{1}/{2}".format(
                                opt.output_dir, epoch, datafolder),
                            exist_ok=True)
                    pred_r, pred_t, pred_c, x_return = estimator(
                        img, points, choose, idx, focal_length,
                        principal_point, motion, cloud_path)

                    _, dis, new_points, new_target, dis_vector = criterion(
                        pred_r, pred_t, pred_c,
                        dis_vector_last_map[idx.item()], target, model_points,
                        idx, x_return, opt.w, opt.refine_start,
                        float(opt.loss_stable_alpha))

                    dis_vector_last_map[idx.item()] = dis_vector

                    test_dis += dis.item()
                    logger.info(
                        'Test time {0} Test Frame No.{1} {2} {3} dis:{4}'.
                        format(
                            time.strftime("%Hh %Mm %Ss",
                                          time.gmtime(time.time() - st_time)),
                            test_count, filename, idx.item(), dis))
                    test_count += 1

        test_dis = test_dis / test_count
        logger.info('Test time {0} Epoch {1} TEST FINISH Avg dis: {2}'.format(
            time.strftime("%d %Hh %Mm %Ss",
                          time.gmtime(time.time() - st_time)), epoch,
            test_dis))
        if test_dis <= best_test:
            best_test = test_dis
            torch.save(
                estimator.state_dict(),
                '{0}/pose_model_ori_{1}_{2}.pth'.format(
                    opt.outf, epoch, test_dis))
            print(epoch,
                  '>>>>>>>>----------BEST TEST MODEL SAVED---------<<<<<<<<')

        if best_test < opt.decay_margin and not opt.decay_start:
            opt.decay_start = True
            opt.lr *= opt.lr_rate
            opt.w *= opt.w_rate
            optimizer = optim.Adam(estimator.parameters(), lr=opt.lr)
Ejemplo n.º 2
0
def main():
    class_id = 0
    class_file = open('datasets/ycb/dataset_config/classes.txt')
    cld = {}
    while 1:
        class_input = class_file.readline()
        if not class_input:
            break

        input_file = open('{0}/models/{1}/points.xyz'.format(
            opt.dataset_root, class_input[:-1]))
        cld[class_id] = []
        while 1:
            input_line = input_file.readline()
            if not input_line:
                break
            input_line = input_line[:-1].split(' ')
            cld[class_id].append([
                float(input_line[0]),
                float(input_line[1]),
                float(input_line[2])
            ])
        cld[class_id] = np.array(cld[class_id])
        input_file.close()

        class_id += 1

    opt.manualSeed = random.randint(1, 10000)
    random.seed(opt.manualSeed)
    torch.manual_seed(opt.manualSeed)
    symmetry_obj_idx = [12, 15, 18, 19, 20]

    if opt.dataset == 'ycb':
        opt.num_objects = 21  # number of object classes in the dataset
        opt.num_points = 1000  # number of points on the input pointcloud
        opt.outf = 'trained_models/ycb/' + opt.output_dir  # folder to save trained models
        opt.test_output = 'experiments/output/ycb/' + opt.output_dir
        if not os.path.exists(opt.test_output):
            os.makedirs(opt.test_output, exist_ok=True)

        opt.repeat_epoch = 1  # number of repeat times for one epoch training
    elif opt.dataset == 'linemod':
        opt.num_objects = 13
        opt.num_points = 500
        opt.outf = 'trained_models/linemod'
        opt.log_dir = 'experiments/logs/linemod'
        opt.repeat_epoch = 20
    else:
        print('Unknown dataset')
        return

    estimator = PoseNet(num_points=opt.num_points,
                        num_obj=opt.num_objects,
                        object_max=opt.object_max)
    estimator.cuda()

    if opt.resume_posenet != '':
        estimator.load_state_dict(
            torch.load('{0}/{1}'.format(opt.outf, opt.resume_posenet)))

        opt.refine_start = False
        opt.decay_start = False

    dataset = PoseDataset_ycb('train', opt.num_points, False, opt.dataset_root,
                              opt.noise_trans, opt.seg_type, True)
    test_dataset = PoseDataset_ycb('test', opt.num_points, False,
                                   opt.dataset_root, 0.0, opt.seg_type, True)

    testdataloader = torch.utils.data.DataLoader(test_dataset,
                                                 shuffle=False,
                                                 num_workers=opt.workers)

    opt.sym_list = dataset.get_sym_list()
    opt.num_points_mesh = dataset.get_num_points_mesh()

    print(
        '>>>>>>>>----------Dataset loaded!---------<<<<<<<<\nlength of the training set: {0}\nlength of the testing set: {1}\nnumber of sample points on mesh: {2}\nsymmetry object list: {3}'
        .format(len(dataset), len(test_dataset), opt.num_points_mesh,
                opt.sym_list))

    criterion = Loss(opt.num_points_mesh, opt.sym_list)

    logger = setup_logger(
        'final_eval_tf_with_seg_square',
        os.path.join(opt.test_output, 'final_eval_tf_with_seg_square.txt'))

    object_max = opt.object_max
    total_test_dis = {key: [] for key in range(0, object_max)}
    total_test_count = {key: [] for key in range(0, object_max)}
    dir_test_dis = {key: [] for key in range(0, object_max)}
    dir_test_count = {key: [] for key in range(0, object_max)}

    # for add
    total_unseen_objects = {key: [] for key in range(0, object_max)}
    total_object_without_pose = {key: [] for key in range(0, object_max)}
    dir_add_count = {key: [] for key in range(0, object_max)}
    dir_add_count_unseen = {key: [] for key in range(0, object_max)}
    dir_add_02_count_unseen = {key: [] for key in range(0, object_max)}
    dir_add_pure_count = {key: [] for key in range(0, object_max)}
    dir_add_s_count = {key: [] for key in range(0, object_max)}
    dir_add_02_count = {key: [] for key in range(0, object_max)}
    dir_add_pure_02_count = {key: [] for key in range(0, object_max)}
    dir_add_s_02_count = {key: [] for key in range(0, object_max)}

    total_add_count = {key: [] for key in range(0, object_max)}
    total_add_count_unseen = {key: [] for key in range(0, object_max)}
    total_add_02_count_unseen = {key: [] for key in range(0, object_max)}
    total_add_pure_count = {key: [] for key in range(0, object_max)}
    total_add_s_count = {key: [] for key in range(0, object_max)}
    total_add_02_count = {key: [] for key in range(0, object_max)}
    total_add_pure_02_count = {key: [] for key in range(0, object_max)}
    total_add_s_02_count = {key: [] for key in range(0, object_max)}

    dir_dbd_count = {key: [] for key in range(0, object_max)}
    dir_drr_count = {key: [] for key in range(0, object_max)}
    dir_ada_count = {key: [] for key in range(0, object_max)}
    dir_distance_1_count = {key: [] for key in range(0, object_max)}

    total_dbd_count = {key: [] for key in range(0, object_max)}
    total_drr_count = {key: [] for key in range(0, object_max)}
    total_ada_count = {key: [] for key in range(0, object_max)}
    total_distance_1_count = {key: [] for key in range(0, object_max)}

    last_dis = {key: [] for key in range(0, object_max)}
    for i in range(object_max):
        total_unseen_objects[i] = 0
        total_object_without_pose[i] = 0

        total_test_dis[i] = 0.
        total_test_count[i] = 0
        dir_test_dis[i] = 0.
        dir_test_count[i] = 0
        # for add
        dir_add_count[i] = 0
        dir_add_count_unseen[i] = 0
        dir_add_02_count_unseen[i] = 0
        dir_add_pure_count[i] = 0
        dir_add_s_count[i] = 0
        dir_add_02_count[i] = 0
        total_add_count[i] = 0
        total_add_count_unseen[i] = 0
        total_add_02_count_unseen[i] = 0
        total_add_pure_count[i] = 0
        total_add_s_count[i] = 0
        total_add_02_count[i] = 0
        dir_add_pure_02_count[i] = 0
        dir_add_s_02_count[i] = 0
        total_add_pure_02_count[i] = 0
        total_add_s_02_count[i] = 0

        #   for stable
        dir_dbd_count[i] = 0.
        dir_drr_count[i] = 0
        dir_ada_count[i] = 0.
        dir_distance_1_count[i] = 0.

        total_dbd_count[i] = 0.
        total_drr_count[i] = 0
        total_ada_count[i] = 0.
        total_distance_1_count[i] = 0.
        last_dis[i] = None

    st_time = time.time()
    isFirstInitLastDatafolder = True
    estimator.eval()
    with torch.no_grad():
        for j, data in enumerate(testdataloader, 0):
            if opt.dataset == 'ycb':
                list_points, list_choose, list_img, list_target, list_model_points, list_idx, list_filename, \
                list_full_img, list_focal_length, list_principal_point, list_motion = data
            output_image = Image.open('{0}/{1}-color-masked-square.png'.format(
                opt.dataset_root, list_filename[0][0]))
            OUTPUT_IMAGE_PATH = '{0}/{1}-color-seg-square-output-tf.png'.format(
                opt.dataset_root, list_filename[0][0])
            for list_index in range(len(list_points)):
                points, choose, img, target, model_points, idx, filename, full_img, focal_length, principal_point, motion \
                    = list_points[list_index], list_choose[list_index], list_img[list_index], \
                      list_target[list_index], list_model_points[list_index], list_idx[list_index], \
                      list_filename[list_index], list_full_img[list_index], list_focal_length[list_index], \
                      list_principal_point[list_index], list_motion[list_index]

                # Temporal Clean when Changing datafolder
                datafolder = filename[0].split('/')[1]
                filehead = filename[0].split('/')[2]
                if isFirstInitLastDatafolder:
                    lastdatafolder = datafolder
                    isFirstInitLastDatafolder = False
                if datafolder != lastdatafolder:
                    logger.info('changing folder from {0} to {1}'.format(
                        lastdatafolder, datafolder))
                    estimator.temporalClear(opt.object_max)
                    # handle dir output
                    for i in range(0, object_max):
                        if dir_test_count[i] != 0:
                            logger.info(
                                'Dir {0} Object {1} dis:{2} with {3} samples'.
                                format(lastdatafolder, i,
                                       dir_test_dis[i] / dir_test_count[i],
                                       dir_test_count[i]))
                            if dir_add_count[i] != 0:
                                logger.info(
                                    'Dir {0} Object {1} add:{2} with 0.02: {3}'
                                    .format(
                                        lastdatafolder, i,
                                        dir_add_count[i] / dir_test_count[i],
                                        dir_add_02_count[i] /
                                        dir_add_count[i]))
                            else:
                                logger.info(
                                    'Dir {0} Object {1} add:{2} with 0.02: {3}'
                                    .format(
                                        lastdatafolder, i,
                                        dir_add_count[i] / dir_test_count[i],
                                        0))
                            if dir_add_pure_count[i] != -0:
                                logger.info(
                                    'Dir {0} Object {1} add_pure:{2} with 0.02: {3}'
                                    .format(
                                        lastdatafolder, i,
                                        dir_add_pure_count[i] /
                                        dir_test_count[i],
                                        dir_add_pure_02_count[i] /
                                        dir_add_pure_count[i]))
                            else:
                                logger.info(
                                    'Dir {0} Object {1} add_pure:{2} with 0.02: {3}'
                                    .format(
                                        lastdatafolder, i,
                                        dir_add_pure_count[i] /
                                        dir_test_count[i], 0))
                            if dir_add_s_count[i] != 0:
                                logger.info(
                                    'Dir {0} Object {1} add_s:{2} with 0.02: {3}'
                                    .format(
                                        lastdatafolder, i,
                                        dir_add_s_count[i] / dir_test_count[i],
                                        dir_add_s_02_count[i] /
                                        dir_add_s_count[i]))
                            else:
                                logger.info(
                                    'Dir {0} Object {1} add_s:{2} with 0.02: {3}'
                                    .format(
                                        lastdatafolder, i,
                                        dir_add_s_count[i] / dir_test_count[i],
                                        0))
                            logger.info('Dir {0} Object {1} dbd:{2}'.format(
                                lastdatafolder, i,
                                dir_dbd_count[i] / dir_test_count[i]))
                            logger.info('Dir {0} Object {1} drr:{2}'.format(
                                lastdatafolder, i,
                                dir_drr_count[i] / dir_test_count[i]))
                            logger.info('Dir {0} Object {1} ada:{2}'.format(
                                lastdatafolder, i,
                                dir_ada_count[i] / dir_test_count[i]))
                            logger.info(
                                'Dir {0} Object {1} distance_1:{2}'.format(
                                    lastdatafolder, i,
                                    dir_distance_1_count[i] /
                                    dir_test_count[i]))

                    dir_dbd = 0.
                    dir_drr = 0.
                    dir_ada = 0.
                    dir_distance_1 = 0.
                    dir_dis = 0.
                    dir_add = 0
                    dir_add_s = 0
                    dir_add_pure = 0
                    dir_add_02 = 0
                    dir_add_s_02 = 0
                    dir_add_pure_02 = 0
                    dir_count = 0

                    for i in range(object_max):
                        if total_test_count[i] != 0:
                            dir_count += dir_test_count[i]
                            dir_dis += dir_test_dis[i]
                            dir_add += dir_add_count[i]
                            dir_add_pure += dir_add_pure_count[i]
                            dir_add_s += dir_add_s_count[i]
                            dir_add_02 += dir_add_02_count[i]
                            dir_add_pure_02 += dir_add_pure_02_count[i]
                            dir_add_s_02 += dir_add_s_02_count[i]
                            dir_dbd += dir_dbd_count[i]
                            dir_drr += dir_drr_count[i]
                            dir_ada += dir_ada_count[i]
                            dir_distance_1 += dir_distance_1_count[i]

                            dir_test_dis[i] = 0
                            dir_test_count[i] = 0
                            dir_add_count[i] = 0
                            dir_add_pure_count[i] = 0
                            dir_add_s_count[i] = 0
                            dir_add_02_count[i] = 0
                            dir_add_pure_02_count[i] = 0
                            dir_add_s_02_count[i] = 0
                            dir_dbd_count[i] = 0
                            dir_drr_count[i] = 0
                            dir_ada_count[i] = 0
                            dir_distance_1_count[i] = 0
                            last_dis[i] = None

                    logger.info(
                        'Dir {0} \'s total dis:{1} with {2} samples'.format(
                            lastdatafolder, dir_dis / dir_count, dir_count))
                    logger.info(
                        'Dir {0} \'s total add:{1} with 0.02: {2}'.format(
                            lastdatafolder, dir_add / dir_count,
                            dir_add_02 / dir_add))
                    logger.info(
                        'Dir {0} \'s total add_s:{1} with 0.02: {2}'.format(
                            lastdatafolder, dir_add_s / dir_count,
                            dir_add_s_02 / dir_add_s))
                    logger.info(
                        'Dir {0} \'s total add_pure:{1} with 0.02: {2}'.format(
                            lastdatafolder, dir_add_pure / dir_count,
                            dir_add_pure_02 / dir_add_pure))
                    logger.info('Dir {0} \'s total dbd:{1}'.format(
                        lastdatafolder, dir_dbd / dir_count))
                    logger.info('Dir {0} \'s total drr:{1}'.format(
                        lastdatafolder, dir_drr / dir_count))
                    logger.info('Dir {0} \'s total ada:{1}'.format(
                        lastdatafolder, dir_ada / dir_count))
                    logger.info('Dir {0} \'s total distance_1:{1}'.format(
                        lastdatafolder, dir_distance_1 / dir_count))

                    # end of handle dir output

                lastdatafolder = datafolder

                points, choose, img, target, model_points, idx = points.cuda(), \
                                                                 choose.cuda(), \
                                                                 img.cuda(), \
                                                                 target.cuda(), \
                                                                 model_points.cuda(), \
                                                                 idx.cuda()
                cloud_path = "experiments/clouds/ycb/{0}/{1}/{2}/{3}_{4}".format(
                    opt.output_dir, 1, datafolder, filehead,
                    int(idx))  # folder to save logs

                pred_r, pred_t, pred_c, x_return = estimator(
                    img, points, choose, idx, focal_length, principal_point,
                    motion, cloud_path)

                # count for unseen object
                if pred_r is None:
                    last_dis[int(idx)] = None
                    total_unseen_objects[int(idx)] += 1
                    total_object_without_pose[int(idx)] += 1
                    continue

                pred_r_ori = copy.deepcopy(pred_r)
                pred_t_ori = copy.deepcopy(pred_t)
                pred_c_ori = copy.deepcopy(pred_c)
                x_return_ori = copy.deepcopy(x_return)

                gt_r, gt_t = get_target(opt.dataset_root, filename, idx)
                if gt_r is None: print('gtr is None')
                is_sym = int(idx) in symmetry_obj_idx
                dis, dis_vector, pred_cloud = calDistance(
                    pred_r_ori, pred_t_ori, pred_c_ori, x_return_ori, gt_r,
                    gt_t, cld[int(idx)], is_sym)
                dis_s, dis_vector_s, _ = calDistance(pred_r_ori, pred_t_ori,
                                                     pred_c_ori, x_return_ori,
                                                     gt_r, gt_t, cld[int(idx)],
                                                     True)
                dis_pure, dis_vector_pure, _ = calDistance(
                    pred_r_ori, pred_t_ori, pred_c_ori, x_return_ori, gt_r,
                    gt_t, cld[int(idx)], False)

                if last_dis[int(idx)] is not None:
                    dir_dbd_count[int(idx)] += torch.norm(dis_vector -
                                                          last_dis[int(idx)])
                    total_dbd_count[int(idx)] += torch.norm(dis_vector -
                                                            last_dis[int(idx)])
                    dir_distance_1_count[int(idx)] += torch.norm(
                        (dis_vector / torch.norm(dis_vector)) -
                        (last_dis[int(idx)] / torch.norm(last_dis[int(idx)])))
                    total_distance_1_count[int(idx)] += torch.norm(
                        (dis_vector / torch.norm(dis_vector)) -
                        (last_dis[int(idx)] / torch.norm(last_dis[int(idx)])))
                    if torch.dot(last_dis[int(idx)], dis_vector) < 0:
                        dir_drr_count[int(idx)] += 1
                        total_drr_count[int(idx)] += 1
                    dir_ada_count[int(idx)] += torch.acos(
                        (torch.dot(last_dis[int(idx)], dis_vector)) /
                        (torch.norm(last_dis[int(idx)]) *
                         torch.norm(dis_vector)))
                    total_ada_count[int(idx)] += torch.acos(
                        (torch.dot(last_dis[int(idx)], dis_vector)) /
                        (torch.norm(last_dis[int(idx)]) *
                         torch.norm(dis_vector)))

                last_dis[int(idx)] = dis_vector

                # calc adds
                if img.shape[1] != 0:
                    dir_test_dis[int(idx)] += dis.item()

                    total_test_dis[int(idx)] += dis.item()
                    dir_test_count[int(idx)] += 1
                    total_test_count[int(idx)] += 1

                    if dis < 0.1:
                        dir_add_count[int(idx)] += 1
                        total_add_count[int(idx)] += 1
                    if dis < 0.02:
                        dir_add_02_count[int(idx)] += 1
                        total_add_02_count[int(idx)] += 1
                    if dis_s < 0.1:
                        dir_add_s_count[int(idx)] += 1
                        total_add_s_count[int(idx)] += 1
                    if dis_s < 0.02:
                        dir_add_s_02_count[int(idx)] += 1
                        total_add_s_02_count[int(idx)] += 1
                    if dis_pure < 0.1:
                        dir_add_pure_count[int(idx)] += 1
                        total_add_pure_count[int(idx)] += 1
                    if dis_pure < 0.02:
                        dir_add_pure_02_count[int(idx)] += 1
                        total_add_pure_02_count[int(idx)] += 1
                else:
                    last_dis[int(idx)] = None
                    if dis < 0.1:
                        dir_add_count_unseen[int(idx)] += 1
                        total_add_count_unseen[int(idx)] += 1
                        total_unseen_objects[int(idx)] += 1
                    if dis < 0.02:
                        dir_add_02_count_unseen[int(idx)] += 1
                        total_add_02_count_unseen[int(idx)] += 1
                        total_unseen_objects[int(idx)] += 1

                output_image = output_transformed_image(
                    OUTPUT_IMAGE_PATH, output_image, pred_cloud, focal_length,
                    principal_point, int(idx))
                logger.info('Test time {0} Test Frame {1} {2} dis:{3}'.format(
                    time.strftime("%Hh %Mm %Ss",
                                  time.gmtime(time.time() - st_time)),
                    filename, idx.item(), dis))

            output_image.save(OUTPUT_IMAGE_PATH)

        # handle dir output
        for i in range(0, object_max):
            if dir_test_count[i] != 0:
                logger.info(
                    'Dir {0} Object {1} dis:{2} with {3} samples'.format(
                        lastdatafolder, i, dir_test_dis[i] / dir_test_count[i],
                        dir_test_count[i]))
                if dir_add_count[i] != 0:
                    logger.info(
                        'Dir {0} Object {1} add:{2} with 0.02: {3}'.format(
                            lastdatafolder, i,
                            dir_add_count[i] / dir_test_count[i],
                            dir_add_02_count[i] / dir_add_count[i]))
                else:
                    logger.info(
                        'Dir {0} Object {1} add:{2} with 0.02: {3}'.format(
                            lastdatafolder, i,
                            dir_add_count[i] / dir_test_count[i], 0))
                if dir_add_pure_count[i] != -0:
                    logger.info(
                        'Dir {0} Object {1} add_pure:{2} with 0.02: {3}'.
                        format(
                            lastdatafolder, i,
                            dir_add_pure_count[i] / dir_test_count[i],
                            dir_add_pure_02_count[i] / dir_add_pure_count[i]))
                else:
                    logger.info(
                        'Dir {0} Object {1} add_pure:{2} with 0.02: {3}'.
                        format(lastdatafolder, i,
                               dir_add_pure_count[i] / dir_test_count[i], 0))
                if dir_add_s_count[i] != 0:
                    logger.info(
                        'Dir {0} Object {1} add_s:{2} with 0.02: {3}'.format(
                            lastdatafolder, i,
                            dir_add_s_count[i] / dir_test_count[i],
                            dir_add_s_02_count[i] / dir_add_s_count[i]))
                else:
                    logger.info(
                        'Dir {0} Object {1} add_s:{2} with 0.02: {3}'.format(
                            lastdatafolder, i,
                            dir_add_s_count[i] / dir_test_count[i], 0))
                logger.info('Dir {0} Object {1} dbd:{2}'.format(
                    lastdatafolder, i, dir_dbd_count[i] / dir_test_count[i]))
                logger.info('Dir {0} Object {1} drr:{2}'.format(
                    lastdatafolder, i, dir_drr_count[i] / dir_test_count[i]))
                logger.info('Dir {0} Object {1} ada:{2}'.format(
                    lastdatafolder, i, dir_ada_count[i] / dir_test_count[i]))
                logger.info('Dir {0} Object {1} distance_1:{2}'.format(
                    lastdatafolder, i,
                    dir_distance_1_count[i] / dir_test_count[i]))

        dir_dbd = 0.
        dir_drr = 0.
        dir_ada = 0.
        dir_distance_1 = 0.
        dir_dis = 0.
        dir_add = 0
        dir_add_s = 0
        dir_add_pure = 0
        dir_add_02 = 0
        dir_add_s_02 = 0
        dir_add_pure_02 = 0
        dir_count = 0

        for i in range(object_max):
            if total_test_count[i] != 0:
                dir_count += dir_test_count[i]
                dir_dis += dir_test_dis[i]
                dir_add += dir_add_count[i]
                dir_add_pure += dir_add_pure_count[i]
                dir_add_s += dir_add_s_count[i]
                dir_add_02 += dir_add_02_count[i]
                dir_add_pure_02 += dir_add_pure_02_count[i]
                dir_add_s_02 += dir_add_s_02_count[i]
                dir_dbd += dir_dbd_count[i]
                dir_drr += dir_drr_count[i]
                dir_ada += dir_ada_count[i]
                dir_distance_1 += dir_distance_1_count[i]

                dir_test_dis[i] = 0
                dir_test_count[i] = 0
                dir_add_count[i] = 0
                dir_add_pure_count[i] = 0
                dir_add_s_count[i] = 0
                dir_add_02_count[i] = 0
                dir_add_pure_02_count[i] = 0
                dir_add_s_02_count[i] = 0
                dir_dbd_count[i] = 0
                dir_drr_count[i] = 0
                dir_ada_count[i] = 0
                dir_distance_1_count[i] = 0

        logger.info('Dir {0} \'s total dis:{1} with {2} samples'.format(
            lastdatafolder, dir_dis / dir_count, dir_count))
        logger.info('Dir {0} \'s total add:{1} with 0.02: {2}'.format(
            lastdatafolder, dir_add / dir_count, dir_add_02 / dir_add))
        logger.info('Dir {0} \'s total add_s:{1} with 0.02: {2}'.format(
            lastdatafolder, dir_add_s / dir_count, dir_add_s_02 / dir_add_s))
        logger.info('Dir {0} \'s total add_pure:{1} with 0.02: {2}'.format(
            lastdatafolder, dir_add_pure / dir_count,
            dir_add_pure_02 / dir_add_pure))
        logger.info('Dir {0} \'s total dbd:{1}'.format(lastdatafolder,
                                                       dir_dbd / dir_count))
        logger.info('Dir {0} \'s total drr:{1}'.format(lastdatafolder,
                                                       dir_drr / dir_count))
        logger.info('Dir {0} \'s total ada:{1}'.format(lastdatafolder,
                                                       dir_ada / dir_count))
        logger.info('Dir {0} \'s total distance_1:{1}'.format(
            lastdatafolder, dir_distance_1 / dir_count))

        # end of handle dir output

        # handle global output
        total_unseen_count = 0
        total_without_pose_count = 0
        total_add_count_unseen_count = 0
        total_add_02_count_unseen_count = 0
        total_drr = 0.
        total_dbd = 0.
        total_ada = 0.
        total_distance_1 = 0.
        total_dis = 0.
        total_add = 0
        total_add_s = 0
        total_add_pure = 0
        total_add_02 = 0
        total_add_s_02 = 0
        total_add_pure_02 = 0
        total_count = 0
        for i in range(object_max):
            if total_test_count[i] != 0:
                logger.info(
                    'Total: Object {0} dis:{1} with {2} samples'.format(
                        i, total_test_dis[i] / total_test_count[i],
                        total_test_count[i]))
                logger.info('Total: Object {0} add:{1} with 0.02: {2}'.format(
                    i, total_add_count[i] / total_test_count[i],
                    total_add_02_count[i] / total_add_count[i]))
                logger.info('Total: Object {0} drr:{1}'.format(
                    i, total_drr_count[i] / total_test_count[i]))
                logger.info('Total: Object {0} ada:{1}'.format(
                    i, total_ada_count[i] / total_test_count[i]))
                logger.info('Total: Object {0} distance_1:{1}'.format(
                    i, total_distance_1_count[i] / total_test_count[i]))
                if total_unseen_objects[i] != 0:
                    if total_unseen_objects[i] - total_object_without_pose[
                            i] != 0:
                        logger.info(
                            'Total: Unseen Object {0} add:{1} with 0.02: {2} with {3} samples '
                            .format(
                                i, total_add_count_unseen[i] /
                                (total_unseen_objects[i] -
                                 total_object_without_pose[i]),
                                total_add_02_count_unseen[i] /
                                total_add_count_unseen[i],
                                (total_unseen_objects[i] -
                                 total_object_without_pose[i])))
                    logger.info(
                        'Total: Object {0} unseen :{1} times, {2} of them without poses, success rate:{3}'
                        .format(i, total_unseen_objects[i],
                                total_object_without_pose[i],
                                (total_unseen_objects[i] -
                                 total_object_without_pose[i]) /
                                total_unseen_objects[i]))

                total_unseen_count += total_unseen_objects[i]
                total_without_pose_count += total_object_without_pose[i]
                total_count += total_test_count[i]
                total_dis += total_test_dis[i]
                total_add += total_add_count[i]
                total_add_count_unseen_count += total_add_count_unseen[i]
                total_add_02_count_unseen_count += total_add_02_count_unseen[i]
                total_add_s += total_add_s_count[i]
                total_add_pure += total_add_pure_count[i]
                total_add_02 += total_add_02_count[i]
                total_add_s_02 += total_add_s_02_count[i]
                total_add_pure_02 += total_add_pure_02_count[i]
                total_dbd += total_dbd_count[i]
                total_drr += total_drr_count[i]
                total_ada += total_ada_count[i]
                total_distance_1 += total_distance_1_count[i]
        logger.info('total dis:{0} with {1} samples'.format(
            total_dis / total_count, total_count))
        logger.info('total add:{0} with 0.02: {1}'.format(
            total_add / total_count, total_add_02 / total_add))
        logger.info('total unseen add:{0} with 0.02: {1}'.format(
            total_add_count_unseen_count /
            (total_unseen_count - total_without_pose_count),
            total_add_02_count_unseen_count / total_add_count_unseen_count))
        logger.info('total add_pure:{0} with 0.02: {1}'.format(
            total_add_pure / total_count, total_add_pure_02 / total_add_pure))
        logger.info('total add_s:{0} with 0.02: {1}'.format(
            total_add_s / total_count, total_add_s_02 / total_add_s))
        logger.info(
            'detected unseen object :{0}, failed calculate {1} poses with success rate: {2}'
            .format(total_unseen_count, total_without_pose_count,
                    (total_unseen_count - total_without_pose_count) /
                    total_unseen_count))
        logger.info('Total drr:{0}'.format(total_drr / total_count))
        logger.info('Total ada:{0}'.format(total_ada / total_count))
        logger.info('Total distance_1:{0}'.format(total_distance_1 /
                                                  total_count))