def main():
    tf.disable_eager_execution()

    parser = argparse.ArgumentParser()
    parser.add_argument("experiment_name")
    arguments = parser.parse_args()

    full_name = arguments.experiment_name.split('/')

    experiment_name = full_name.pop()
    experiment_group = full_name.pop() if len(full_name) > 0 else ''

    codebook, dataset = factory.build_codebook_from_name(experiment_name,
                                                         experiment_group,
                                                         return_dataset=True)

    workspace_path = os.environ.get('AE_WORKSPACE_PATH')
    log_dir = u.get_log_dir(workspace_path, experiment_name, experiment_group)
    ckpt_dir = u.get_checkpoint_dir(log_dir)

    train_cfg_file_path = u.get_train_config_exp_file_path(
        log_dir, experiment_name)
    train_args = configparser.ConfigParser()
    train_args.read(train_cfg_file_path)

    width = 960
    height = 720
    videoStream = WebcamVideoStream(0, width, height).start()

    gpu_options = tf.GPUOptions(allow_growth=True,
                                per_process_gpu_memory_fraction=0.9)
    config = tf.ConfigProto(gpu_options=gpu_options)
    config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        factory.restore_checkpoint(sess, tf.train.Saver(), ckpt_dir)

        while videoStream.isActive():
            image = videoStream.read()
            if image is None or not image.any():
                print("Failed to capture webcam image")
                exit(-1)

            # try your detector here:
            # bb_xywh = detector.detect(image)
            # image_crop = dataset.extract_square_patch(image, bb_xywh, train_args.getfloat('Dataset','PAD_FACTOR'))
            # Rs, ts = codebook.auto_pose6d(sess, image_crop, bb_xywh, K_test, 1, train_args)

            img = cv2.resize(image, (128, 128))

            R = codebook.nearest_rotation(sess, img)
            pred_view = dataset.render_rot(R, downSample=1)
            print(R)
            cv2.imshow('resized webcam input', img)
            cv2.imshow('pred view rendered', pred_view)
            cv2.waitKey(1)
Beispiel #2
0
    def __init__(self, test_configpath):

        test_args = configparser.ConfigParser()
        test_args.read(test_configpath)

        workspace_path = os.environ.get('AE_WORKSPACE_PATH')

        if workspace_path == None:
            print('Please define a workspace path:')
            print('export AE_WORKSPACE_PATH=/path/to/workspace')
            exit(-1)

        self._camPose = test_args.getboolean('CAMERA', 'camPose')
        self._camK = np.array(eval(test_args.get('CAMERA',
                                                 'K_test'))).reshape(3, 3)
        self._width = test_args.getint('CAMERA', 'width')
        self._height = test_args.getint('CAMERA', 'height')

        self._upright = test_args.getboolean('AAE', 'upright')
        self.all_experiments = eval(test_args.get('AAE', 'experiments'))

        self.class_names = eval(test_args.get('DETECTOR', 'class_names'))
        self.det_threshold = eval(test_args.get('DETECTOR', 'det_threshold'))
        self.icp = test_args.getboolean('ICP', 'icp')

        if self.icp:
            self._depth_scale = test_args.getfloat('DATA', 'depth_scale')

        self.all_codebooks = []
        self.all_train_args = []
        self.pad_factors = []
        self.patch_sizes = []

        config = tf.ConfigProto(allow_soft_placement=True)
        config.gpu_options.allow_growth = True
        config.gpu_options.per_process_gpu_memory_fraction = test_args.getfloat(
            'MODEL', 'gpu_memory_fraction')

        self.sess = tf.Session(config=config)
        set_session(self.sess)
        self.detector = load_model(
            str(test_args.get('DETECTOR', 'detector_model_path')),
            backbone_name=test_args.get('DETECTOR', 'backbone'))
        #detector = self._load_model_with_nms(test_args)

        for i, experiment in enumerate(self.all_experiments):
            full_name = experiment.split('/')
            experiment_name = full_name.pop()
            experiment_group = full_name.pop() if len(full_name) > 0 else ''
            log_dir = utils.get_log_dir(workspace_path, experiment_name,
                                        experiment_group)
            ckpt_dir = utils.get_checkpoint_dir(log_dir)
            train_cfg_file_path = utils.get_train_config_exp_file_path(
                log_dir, experiment_name)
            print(train_cfg_file_path)
            # train_cfg_file_path = utils.get_config_file_path(workspace_path, experiment_name, experiment_group)
            train_args = configparser.ConfigParser()
            train_args.read(train_cfg_file_path)
            self.all_train_args.append(train_args)
            self.pad_factors.append(
                train_args.getfloat('Dataset', 'PAD_FACTOR'))
            self.patch_sizes.append(
                (train_args.getint('Dataset',
                                   'W'), train_args.getint('Dataset', 'H')))

            self.all_codebooks.append(
                factory.build_codebook_from_name(experiment_name,
                                                 experiment_group,
                                                 return_dataset=False))
            saver = tf.train.Saver(var_list=tf.get_collection(
                tf.GraphKeys.GLOBAL_VARIABLES, scope=experiment_name))
            factory.restore_checkpoint(self.sess, saver, ckpt_dir)

            # if self.icp:
            #     assert len(self.all_experiments) == 1, 'icp currently only works for one object'
            #     # currently works only for one object
            #     from auto_pose.icp import icp
            #     self.icp_handle = icp.ICP(train_args)
        if test_args.getboolean('ICP', 'icp'):
            from auto_pose.icp import icp
            self.icp_handle = icp.ICP(test_args, self.all_train_args)
Beispiel #3
0
from auto_pose.ae import factory
from auto_pose.ae import utils as u
from webcam_video_stream import WebcamVideoStream

parser = argparse.ArgumentParser()
parser.add_argument("experiment_name")
arguments = parser.parse_args()

full_name = arguments.experiment_name.split('/')

experiment_name = full_name.pop()
experiment_group = full_name.pop() if len(full_name) > 0 else ''

codebook, dataset = factory.build_codebook_from_name(experiment_name,
                                                     experiment_group,
                                                     return_dataset=True)

workspace_path = os.environ.get('AE_WORKSPACE_PATH')
log_dir = u.get_log_dir(workspace_path, experiment_name, experiment_group)
ckpt_dir = u.get_checkpoint_dir(log_dir)

train_cfg_file_path = u.get_train_config_exp_file_path(log_dir,
                                                       experiment_name)
train_args = configparser.ConfigParser()
train_args.read(train_cfg_file_path)

width = 960
height = 720
videoStream = WebcamVideoStream(0, width, height).start()
def detection(detection_graph, category_index, score, expand):
    print("> Building Graph")
    print(category_index)
    # Session Config: allow seperate GPU/CPU adressing and limit memory allocation
    config = tf.ConfigProto(allow_soft_placement=True,
                            log_device_placement=log_device)
    config.gpu_options.allow_growth = allow_memory_growth
    config.gpu_options.per_process_gpu_memory_fraction = 0.4  ###Jetson only
    cur_frames = 0
    with detection_graph.as_default():
        #run_meta = tf.RunMetadata()
        with tf.Session(graph=detection_graph, config=config) as sess:
            # Define Input and Ouput tensors
            image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
            detection_boxes = detection_graph.get_tensor_by_name(
                'detection_boxes:0')
            detection_scores = detection_graph.get_tensor_by_name(
                'detection_scores:0')
            detection_classes = detection_graph.get_tensor_by_name(
                'detection_classes:0')
            num_detections = detection_graph.get_tensor_by_name(
                'num_detections:0')
            if split_model:
                score_out = detection_graph.get_tensor_by_name(
                    'Postprocessor/convert_scores:0')
                expand_out = detection_graph.get_tensor_by_name(
                    'Postprocessor/ExpandDims_1:0')
                score_in = detection_graph.get_tensor_by_name(
                    'Postprocessor/convert_scores_1:0')
                expand_in = detection_graph.get_tensor_by_name(
                    'Postprocessor/ExpandDims_1_1:0')
                # Threading
                gpu_worker = SessionWorker("GPU", detection_graph, config)
                cpu_worker = SessionWorker("CPU", detection_graph, config)
                gpu_opts = [score_out, expand_out]
                cpu_opts = [
                    detection_boxes, detection_scores, detection_classes,
                    num_detections
                ]
                gpu_counter = 0
                cpu_counter = 0

            for i, experiment_name in enumerate(arguments.experiment_names):

                full_name = experiment_name.split('/')
                experiment_name = full_name.pop()
                experiment_group = full_name.pop(
                ) if len(full_name) > 0 else ''

                train_cfg_file_path = utils.get_config_file_path(
                    workspace_path, experiment_name, experiment_group)
                train_args = configparser.ConfigParser()
                train_args.read(train_cfg_file_path)
                h_train, w_train, c = train_args.getint(
                    'Dataset', 'H'), train_args.getint('Dataset',
                                                       'W'), train_args.getint(
                                                           'Dataset', 'C')
                model_paths.append(train_args.get('Paths', 'MODEL_PATH'))
                all_train_args.append(train_args)

                log_dir = utils.get_log_dir(workspace_path, experiment_name,
                                            experiment_group)
                ckpt_dir = utils.get_checkpoint_dir(log_dir)

                all_codebooks.append(
                    factory.build_codebook_from_name(experiment_name,
                                                     experiment_group,
                                                     return_dataset=False))
                factory.restore_checkpoint(
                    sess,
                    tf.train.Saver(var_list=tf.get_collection(
                        tf.GraphKeys.GLOBAL_VARIABLES, scope=experiment_name)),
                    ckpt_dir)

            #opts = tf.profiler.ProfileOptionBuilder.float_operation()
            #flops = tf.profiler.profile(sess.graph, run_meta=run_meta, cmd='op', options=opts)
            #exit()
            # i_class_mapping = {v: k for k, v in class_i_mapping.iteritems()}
            renderer = meshrenderer_phong.Renderer(model_paths, 1)

            # Start Video Stream and FPS calculation
            fps = FPS2(fps_interval).start()
            video_stream = WebcamVideoStream(video_input, width,
                                             height).start()
            cur_frames = 0
            print("> Press 'q' to Exit, 'a' to start auto_pose")
            print('> Starting Detection')
            while video_stream.isActive():
                # actual Detection
                if split_model:
                    # split model in seperate gpu and cpu session threads
                    if gpu_worker.is_sess_empty():
                        # read video frame, expand dimensions and convert to rgb
                        image = video_stream.read()
                        image_expanded = np.expand_dims(cv2.cvtColor(
                            image, cv2.COLOR_BGR2RGB),
                                                        axis=0)
                        # put new queue
                        gpu_feeds = {image_tensor: image_expanded}
                        if visualize:
                            gpu_extras = image  # for visualization frame
                        else:
                            gpu_extras = None
                        gpu_worker.put_sess_queue(gpu_opts, gpu_feeds,
                                                  gpu_extras)

                    g = gpu_worker.get_result_queue()
                    if g is None:
                        # gpu thread has no output queue. ok skip, let's check cpu thread.
                        gpu_counter += 1
                    else:
                        # gpu thread has output queue.
                        gpu_counter = 0
                        score, expand, image = g["results"][0], g["results"][
                            1], g["extras"]

                        if cpu_worker.is_sess_empty():
                            # When cpu thread has no next queue, put new queue.
                            # else, drop gpu queue.
                            cpu_feeds = {score_in: score, expand_in: expand}
                            cpu_extras = image
                            cpu_worker.put_sess_queue(cpu_opts, cpu_feeds,
                                                      cpu_extras)

                    c = cpu_worker.get_result_queue()
                    if c is None:
                        # cpu thread has no output queue. ok, nothing to do. continue
                        cpu_counter += 1
                        time.sleep(0.005)
                        continue  # If CPU RESULT has not been set yet, no fps update
                    else:
                        cpu_counter = 0
                        boxes, scores, classes, num, image = c["results"][
                            0], c["results"][1], c["results"][2], c["results"][
                                3], c["extras"]
                else:
                    # default session
                    image = video_stream.read()
                    image_expanded = np.expand_dims(cv2.cvtColor(
                        image, cv2.COLOR_BGR2RGB),
                                                    axis=0)
                    boxes, scores, classes, num = sess.run(
                        [
                            detection_boxes, detection_scores,
                            detection_classes, num_detections
                        ],
                        feed_dict={image_tensor: image_expanded})

                # Visualization of the results of a detection.

                H, W = image.shape[:2]

                img_crops = []
                det_bbs = []
                det_classes = []
                det_scores = []

                det_aae_bbs = []
                det_aae_objects_k = []
                #print vis_img.shape
                boxes = np.squeeze(boxes)
                scores = np.squeeze(scores)
                classes = np.squeeze(classes).astype(np.int32)

                highest_class_score = {clas: 0.0 for clas in classes}
                for box, score, clas in zip(boxes, scores, classes):
                    if score > det_th and score > highest_class_score[clas]:

                        highest_class_score[clas] = score
                        ymin, xmin, ymax, xmax = (np.array(box) * np.array(
                            [height, width, height, width])).astype(np.int32)

                        h, w = (ymax - ymin, xmax - xmin)
                        det_bbs.append([xmin, ymin, w, h])
                        det_classes.append(clas)
                        det_scores.append(score)
                        if clas in clas_k_map:
                            det_aae_bbs.append([xmin, ymin, w, h])

                            det_aae_objects_k.append(clas_k_map[clas])

                            size = int(
                                np.maximum(h, w) *
                                train_args.getfloat('Dataset', 'PAD_FACTOR'))
                            cx = xmin + (xmax - xmin) / 2
                            cy = ymin + (ymax - ymin) / 2

                            left = np.maximum(cx - size / 2, 0)
                            top = np.maximum(cy - size / 2, 0)

                            img_crop = image[top:cy + size / 2,
                                             left:cx + size / 2]
                            img_crop = cv2.resize(img_crop, (h_train, w_train))

                            img_crop = img_crop / 255.
                            img_crops.append(img_crop)

                if len(det_aae_bbs) > 0:

                    Rs = []
                    ts = []
                    for k, bb, img_crop in zip(det_aae_objects_k, det_aae_bbs,
                                               img_crops):
                        R, t = all_codebooks[k].auto_pose6d(sess,
                                                            img_crop,
                                                            bb,
                                                            K_test,
                                                            1,
                                                            all_train_args[k],
                                                            upright=False)
                        Rs.append(R.squeeze())
                        ts.append(t.squeeze())

                    Rs = np.array(Rs)
                    ts = np.array(ts)

                    bgr_y, _, _ = renderer.render_many(
                        obj_ids=np.array(det_aae_objects_k).astype(np.int32),
                        W=width / arguments.down,
                        H=height / arguments.down,
                        K=K_down,
                        Rs=Rs,
                        ts=ts,
                        near=1.,
                        far=10000.,
                        random_light=False,
                        # calc_bbs=False,
                        # depth=False
                    )

                    bgr_y = cv2.resize(bgr_y, (width, height))

                    g_y = np.zeros_like(bgr_y)
                    g_y[:, :, 1] = bgr_y[:, :, 1]
                    im_bg = cv2.bitwise_and(image,
                                            image,
                                            mask=(g_y[:, :, 1] == 0).astype(
                                                np.uint8))
                    image = cv2.addWeighted(im_bg, 1, g_y, 1, 0)

                for bb, score, clas in zip(det_bbs, det_scores, det_classes):
                    xmin, ymin, xmax, ymax = bb[0], bb[
                        1], bb[2] + bb[0], bb[1] + bb[3]
                    cv2.putText(
                        image,
                        '%s : %1.3f' % (category_index[clas]['name'], score),
                        (xmin, ymax + 20), cv2.FONT_ITALIC, .5,
                        color_dict[clas - 1], 2)
                    cv2.rectangle(image, (xmin, ymin), (xmax, ymax),
                                  color_dict[clas - 1], 2)

                if vis_text:
                    cv2.putText(image, "fps: {}".format(fps.fps_local()),
                                (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.75,
                                (77, 255, 9), 2)
                cv2.imshow('object_detection', image)
                # Exit Option
                key = cv2.waitKey(1)
                if key == ord('q'):
                    break

                fps.update()

    # End everything
    if split_model:
        gpu_worker.stop()
        cpu_worker.stop()
    fps.stop()
    video_stream.stop()
    cv2.destroyAllWindows()
    print('> [INFO] elapsed time (total): {:.2f}'.format(fps.elapsed()))
    print('> [INFO] approx. FPS: {:.2f}'.format(fps.fps()))
def main():
    '''
    lxc:
    use_euclidean means the similarity between test embedding and template embedding 
    are computed using Euclidean Distance
    '''
    #use_euclidean = False

    parser = argparse.ArgumentParser()

    parser.add_argument('experiment_name')
    parser.add_argument('evaluation_name')
    parser.add_argument('--eval_cfg', default='eval.cfg', required=False)
    parser.add_argument('--at_step', default=None, required=False)
    arguments = parser.parse_args()
    full_name = arguments.experiment_name.split('/')
    experiment_name = full_name.pop()
    experiment_group = full_name.pop() if len(full_name) > 0 else ''
    evaluation_name = arguments.evaluation_name
    eval_cfg = arguments.eval_cfg
    at_step = arguments.at_step

    workspace_path = os.environ.get('AE_WORKSPACE_PATH')
    train_cfg_file_path = u.get_config_file_path(workspace_path,
                                                 experiment_name,
                                                 experiment_group)
    eval_cfg_file_path = u.get_eval_config_file_path(workspace_path,
                                                     eval_cfg=eval_cfg)

    train_args = configparser.ConfigParser()
    eval_args = configparser.ConfigParser()
    train_args.read(train_cfg_file_path)
    eval_args.read(eval_cfg_file_path)

    #[DATA]
    # target data params
    dataset_name = eval_args.get('DATA', 'DATASET')
    obj_id = eval_args.getint('DATA', 'OBJ_ID')
    scenes = eval(eval_args.get(
        'DATA', 'SCENES')) if len(eval(eval_args.get(
            'DATA',
            'SCENES'))) > 0 else eval_utils.get_all_scenes_for_obj(eval_args)
    cam_type = eval_args.get('DATA', 'cam_type')
    model_type = 'reconst' if dataset_name == 'tless' else ''  # model_type set to reconst only for tless.

    data_params = dataset_params.get_dataset_params(dataset_name,
                                                    model_type=model_type,
                                                    train_type='',
                                                    test_type=cam_type,
                                                    cam_type=cam_type)
    target_models_info = inout.load_yaml(
        data_params['models_info_path'])  # lxc

    # source data params, lxc
    source_dataset_name = 'toyotalight'
    # source_dataset_name = train_args.get('DATA','DATASET') # TODO train args no section DATA
    # source_obj_id = train_args.getint('DATA','OBJ_ID') # TODO train args no section DATA
    source_obj_id = int(train_cfg_file_path[-6:-4])  # TODO workaround
    source_data_params = dataset_params.get_dataset_params(source_dataset_name,
                                                           model_type='',
                                                           train_type='',
                                                           test_type='',
                                                           cam_type='')
    # for tless temporarily.
    # source_data_params = dataset_params.get_dataset_params(source_dataset_name, model_type='', train_type='', test_type='kinect', cam_type='kinect')
    source_models_info = inout.load_yaml(
        source_data_params['models_info_path'])
    print("source_models_info_path:", source_data_params['models_info_path'])
    # 'diameter' is not equal to sqrt(x^2+y^2+z^2) for hinterstoisser, rutgers, tless, tejaniDB. etc.
    # for toyotalight, 'diameter' == sqrt(...).
    target_models_3Dlength = np.linalg.norm([
        target_models_info[obj_id][key]
        for key in ['size_x', 'size_y', 'size_z']
    ])
    source_models_3Dlength = np.linalg.norm([
        source_models_info[source_obj_id][key]
        for key in ['size_x', 'size_y', 'size_z']
    ])

    target_source_length_ratio = target_models_3Dlength / source_models_3Dlength
    print("target_source_length_ratio:", target_source_length_ratio)
    print("source id {:02d}, target id {:02d}".format(source_obj_id, obj_id))
    print('basepath: ', data_params['base_path'])
    #[BBOXES]
    estimate_bbs = eval_args.getboolean('BBOXES', 'ESTIMATE_BBS')
    #[METRIC]
    top_nn = eval_args.getint('METRIC', 'TOP_N')
    #[EVALUATION]
    icp = eval_args.getboolean('EVALUATION', 'ICP')

    evaluation_name = evaluation_name + '_icp' if icp else evaluation_name
    evaluation_name = evaluation_name + '_bbest' if estimate_bbs else evaluation_name

    data = dataset_name + '_' + cam_type if len(cam_type) > 0 else dataset_name

    log_dir = u.get_log_dir(workspace_path, experiment_name, experiment_group)
    ckpt_dir = u.get_checkpoint_dir(log_dir)
    eval_dir = u.get_eval_dir(log_dir, evaluation_name, data)

    # if eval_args.getboolean('EVALUATION','EVALUATE_ERRORS'):
    #     eval_loc.match_and_eval_performance_scores(eval_args, eval_dir)
    #     exit()

    if not os.path.exists(eval_dir):
        os.makedirs(eval_dir)
    shutil.copy2(eval_cfg_file_path, eval_dir)

    print "eval_args: ", eval_args

    codebook, dataset, decoder = factory.build_codebook_from_name(
        experiment_name,
        experiment_group,
        return_dataset=True,
        return_decoder=True)
    dataset.renderer
    gpu_options = tf.GPUOptions(allow_growth=True,
                                per_process_gpu_memory_fraction=0.5)
    config = tf.ConfigProto(gpu_options=gpu_options)

    sess = tf.Session(config=config)
    factory.restore_checkpoint(sess,
                               tf.train.Saver(),
                               ckpt_dir,
                               at_step=at_step)

    if estimate_bbs:
        #Object Detection, seperate from main
        # sys.path.append('/net/rmc-lx0050/home_local/sund_ma/src/SSD_Tensorflow')
        # from ssd_detector import SSD_detector
        # #TODO: set num_classes, network etc.
        # ssd = SSD_detector(sess, num_classes=31, net_shape=(300,300))
        from rmcssd.bin import detector
        ssd = detector.Detector(eval_args.get('BBOXES', 'CKPT'))

    t_errors = []
    R_errors = []
    all_test_visibs = []

    test_embeddings = []
    for scene_id in scenes:

        test_imgs = eval_utils.load_scenes(scene_id, eval_args)
        test_imgs_depth = eval_utils.load_scenes(
            scene_id, eval_args, depth=True) if icp else None

        if estimate_bbs:
            print eval_args.get('BBOXES', 'EXTERNAL')
            if eval_args.get('BBOXES', 'EXTERNAL') == 'False':
                bb_preds = {}
                for i, img in enumerate(test_imgs):
                    print img.shape
                    bb_preds[i] = ssd.detectSceneBBs(img,
                                                     min_score=.2,
                                                     nms_threshold=.45)
                # inout.save_yaml(os.path.join(scene_res_dir,'bb_preds.yml'), bb_preds)
                print bb_preds
            else:
                bb_preds = inout.load_yaml(
                    os.path.join(eval_args.get('BBOXES', 'EXTERNAL'),
                                 '{:02d}.yml'.format(scene_id)))

            test_img_crops, test_img_depth_crops, bbs, bb_scores, visibilities = eval_utils.generate_scene_crops(
                test_imgs, test_imgs_depth, bb_preds, eval_args, train_args)
        else:
            # test_img_crops: each crop contains some bbox(es) for specified object id.
            test_img_crops, test_img_depth_crops, bbs, bb_scores, visibilities = eval_utils.get_gt_scene_crops(
                scene_id, eval_args, train_args)

        if len(test_img_crops) == 0:
            print 'ERROR: object %s not in scene %s' % (obj_id, scene_id)
            exit()

        info = inout.load_info(
            data_params['scene_info_mpath'].format(scene_id))
        Ks_test = [np.array(v['cam_K']).reshape(3, 3) for v in info.values()]

        ######remove
        gts = inout.load_gt(data_params['scene_gt_mpath'].format(scene_id))
        visib_gts = inout.load_yaml(data_params['scene_gt_stats_mpath'].format(
            scene_id, 15))
        #######
        W_test, H_test = data_params['test_im_size']

        icp_renderer = icp_utils.SynRenderer(train_args) if icp else None
        noof_scene_views = eval_utils.noof_scene_views(scene_id, eval_args)

        test_embeddings.append([])

        scene_res_dir = os.path.join(
            eval_dir, '{scene_id:02d}'.format(scene_id=scene_id))
        if not os.path.exists(scene_res_dir):
            os.makedirs(scene_res_dir)

        for view in xrange(
                noof_scene_views
        ):  # for example, LINEMOD ape noof_scene_views = 1236
            try:
                # only a specified object id is selected throughout the whole scene views.
                test_crops, test_crops_depth, test_bbs, test_scores, test_visibs = eval_utils.select_img_crops(
                    test_img_crops[view][obj_id],
                    test_img_depth_crops[view][obj_id] if icp else None,
                    bbs[view][obj_id], bb_scores[view][obj_id],
                    visibilities[view][obj_id], eval_args)
            except:
                print 'no detections'
                continue

            print view
            preds = {}
            pred_views = []
            all_test_visibs.append(test_visibs[0])
            t_errors_crop = []
            R_errors_crop = []

            for i, (test_crop, test_bb, test_score) in enumerate(
                    zip(test_crops, test_bbs, test_scores)):
                # each test_crop is a ground truth patch
                if train_args.getint('Dataset', 'C') == 1:
                    test_crop = cv2.cvtColor(test_crop,
                                             cv2.COLOR_BGR2GRAY)[:, :, None]
                start = time.time()
                '''modify here to change the pose estimation algorithm. lxc'''

                Rs_est, ts_est = codebook.auto_pose6d(
                    sess,
                    test_crop,
                    test_bb,
                    Ks_test[view].copy(),
                    top_nn,
                    train_args,
                    target_source_length_ratio=target_source_length_ratio)
                ae_time = time.time() - start
                run_time = ae_time + bb_preds[view][0][
                    'det_time'] if estimate_bbs else ae_time

                if eval_args.getboolean('PLOT', 'EMBEDDING_PCA'):
                    test_embeddings[-1].append(
                        codebook.test_embedding(sess,
                                                test_crop,
                                                normalized=True))

                # icp = False if view<350 else True
                #TODO:
                Rs_est_old, ts_est_old = Rs_est.copy(), ts_est.copy()
                for p in xrange(top_nn):
                    if icp:
                        start = time.time()
                        # icp only along tz
                        R_est_refined, t_est_refined = icp_utils.icp_refinement(
                            test_crops_depth[i],
                            icp_renderer,
                            Rs_est[p],
                            ts_est[p],
                            Ks_test[view].copy(), (W_test, H_test),
                            depth_only=True,
                            max_mean_dist_factor=5.0)
                        print ts_est[p]
                        print t_est_refined
                        # x,y update,does not change tz:
                        _, ts_est_refined = codebook.auto_pose6d(
                            sess,
                            test_crop,
                            test_bb,
                            Ks_test[view].copy(),
                            top_nn,
                            train_args,
                            depth_pred=t_est_refined[2])
                        # commented by lxc
                        # _, ts_est_refined, _ = codebook.auto_pose6d(sess, test_crop, test_bb, Ks_test[view].copy(), top_nn, train_args,depth_pred=t_est_refined[2])
                        t_est_refined = ts_est_refined[p]
                        # rotation icp, only accepted if below 20 deg change
                        R_est_refined, _ = icp_utils.icp_refinement(
                            test_crops_depth[i],
                            icp_renderer,
                            R_est_refined,
                            t_est_refined,
                            Ks_test[view].copy(), (W_test, H_test),
                            no_depth=True)
                        print Rs_est[p]
                        print R_est_refined
                        icp_time = time.time() - start
                        Rs_est[p], ts_est[p] = R_est_refined, t_est_refined
                    preds.setdefault('ests', []).append({
                        'score': test_score,
                        'R': Rs_est[p],
                        't': ts_est[p]
                    })
                run_time = run_time + icp_time if icp else run_time

                min_t_err, min_R_err = eval_plots.print_trans_rot_errors(
                    gts[view], obj_id, ts_est, ts_est_old, Rs_est, Rs_est_old)
                t_errors_crop.append(min_t_err)
                R_errors_crop.append(min_R_err)

                if eval_args.getboolean('PLOT', 'RECONSTRUCTION'):
                    eval_plots.plot_reconstruction_test(
                        sess, codebook._encoder, decoder, test_crop)
                    # eval_plots.plot_reconstruction_train(sess, decoder, nearest_train_codes[0])
                if eval_args.getboolean('PLOT',
                                        'NEAREST_NEIGHBORS') and not icp:
                    for R_est, t_est in zip(Rs_est, ts_est):
                        pred_views.append(
                            dataset.render_rot(R_est, downSample=2))
                    eval_plots.show_nearest_rotation(pred_views, test_crop,
                                                     view)
                if eval_args.getboolean('PLOT', 'SCENE_WITH_ESTIMATE'):
                    eval_plots.plot_scene_with_estimate(
                        test_imgs[view].copy(),
                        icp_renderer.renderer if icp else dataset.renderer,
                        Ks_test[view].copy(), Rs_est_old[0], ts_est_old[0],
                        Rs_est[0], ts_est[0], test_bb, test_score, obj_id,
                        gts[view], bb_preds[view] if estimate_bbs else None)

                if cv2.waitKey(1) == 32:
                    cv2.waitKey(0)

            t_errors.append(t_errors_crop[np.argmin(
                np.linalg.norm(np.array(t_errors_crop), axis=1))])
            R_errors.append(R_errors_crop[np.argmin(
                np.linalg.norm(np.array(t_errors_crop), axis=1))])

            # save predictions in sixd format
            res_path = os.path.join(scene_res_dir,
                                    '%04d_%02d.yml' % (view, obj_id))
            inout.save_results_sixd17(res_path, preds, run_time=run_time)

    if not os.path.exists(os.path.join(eval_dir, 'latex')):
        os.makedirs(os.path.join(eval_dir, 'latex'))
    if not os.path.exists(os.path.join(eval_dir, 'figures')):
        os.makedirs(os.path.join(eval_dir, 'figures'))
    '''evaluation code
        dataset_renderer renders source object model for evaluation;
        If we need target object model for evaluation, go get a new renderer.
    '''

    if eval_args.getboolean('EVALUATION', 'COMPUTE_ERRORS'):
        eval_calc_errors.eval_calc_errors(eval_args,
                                          eval_dir,
                                          dataset_renderer=dataset.renderer)
    if eval_args.getboolean('EVALUATION', 'EVALUATE_ERRORS'):
        eval_loc.match_and_eval_performance_scores(eval_args, eval_dir)
    '''plot code'''
    cyclo = train_args.getint('Embedding', 'NUM_CYCLO')
    if eval_args.getboolean('PLOT', 'EMBEDDING_PCA'):
        embedding = sess.run(codebook.embedding_normalized)
        eval_plots.compute_pca_plot_embedding(eval_dir,
                                              embedding[::cyclo],
                                              np.array(test_embeddings[0]),
                                              obj_id=obj_id)
    if eval_args.getboolean('PLOT', 'VIEWSPHERE'):
        eval_plots.plot_viewsphere_for_embedding(
            dataset.viewsphere_for_embedding[::cyclo], eval_dir, obj_id=obj_id)
    if eval_args.getboolean('PLOT', 'CUM_T_ERROR_HIST'):
        eval_plots.plot_t_err_hist(np.array(t_errors), eval_dir, obj_id=obj_id)
        eval_plots.plot_t_err_hist2(np.array(t_errors),
                                    eval_dir,
                                    obj_id=obj_id)
    if eval_args.getboolean('PLOT', 'CUM_R_ERROR_HIST'):
        eval_plots.plot_R_err_hist(eval_args, eval_dir, scenes)
        eval_plots.plot_R_err_hist2(np.array(R_errors),
                                    eval_dir,
                                    obj_id=obj_id)
    if eval_args.getboolean('PLOT', 'CUM_VSD_ERROR_HIST'):
        eval_plots.plot_vsd_err_hist(eval_args, eval_dir, scenes)
    if eval_args.getboolean('PLOT', 'VSD_OCCLUSION'):
        eval_plots.plot_vsd_occlusion(eval_args, eval_dir, scenes,
                                      np.array(all_test_visibs))
    if eval_args.getboolean('PLOT', 'R_ERROR_OCCLUSION'):
        eval_plots.plot_re_rect_occlusion(eval_args, eval_dir, scenes,
                                          np.array(all_test_visibs))
    if eval_args.getboolean('PLOT', 'ANIMATE_EMBEDDING_PCA'):
        eval_plots.animate_embedding_path(test_embeddings[0])
    if eval_args.getboolean('PLOT', 'RECONSTRUCTION_TEST_BATCH'):
        eval_plots.plot_reconstruction_test_batch(sess,
                                                  codebook,
                                                  decoder,
                                                  test_img_crops,
                                                  noof_scene_views,
                                                  obj_id,
                                                  eval_dir=eval_dir)
        # plt.show()

        # calculate 6D pose errors
        # print 'exiting ...'
        # eval_calc_errors.eval_calc_errors(eval_args, eval_dir)
        # calculate 6D pose errors

    report = latex_report.Report(eval_dir, log_dir)
    report.write_configuration(train_cfg_file_path, eval_cfg_file_path)
    report.merge_all_tex_files()
    report.include_all_figures()
    report.save(open_pdf=False)