Esempio n. 1
0
    def models(self):
        """Load models into a list."""
        cache_path = osp.join(self.cache_dir,
                              "models_{}.pkl".format("_".join(self.objs)))
        if osp.exists(cache_path) and self.use_cache:
            # dprint("{}: load cached object models from {}".format(self.name, cache_path))
            return mmcv.load(cache_path)

        models = []
        for obj_name in self.objs:
            model = inout.load_ply(
                osp.join(self.models_root,
                         f"obj_{ref.lm_full.obj2id[obj_name]:06d}.ply"),
                vertex_scale=self.scale_to_meter,
            )
            # NOTE: the bbox3d_and_center is not obtained from centered vertices
            # for BOP models, not a big problem since they had been centered
            model["bbox3d_and_center"] = misc.get_bbox3d_and_center(
                model["pts"])

            models.append(model)
        logger.info("cache models to {}".format(cache_path))
        mmcv.mkdir_or_exist(osp.dirname(cache_path))
        mmcv.dump(models, cache_path, protocol=4)
        return models
Esempio n. 2
0
    def __init__(self,
                 cfg,
                 dataset_name,
                 distributed,
                 output_dir,
                 train_objs=None):
        self.cfg = cfg
        self._distributed = distributed
        self._output_dir = output_dir
        mmcv.mkdir_or_exist(output_dir)

        self._cpu_device = torch.device("cpu")
        self._logger = logging.getLogger(__name__)

        # if test objs are just a subset of train objs
        self.train_objs = train_objs

        self.dataset_name = dataset_name
        self._metadata = MetadataCatalog.get(dataset_name)
        self.data_ref = ref.__dict__[self._metadata.ref_key]
        self.obj_names = self._metadata.objs
        self.obj_ids = [
            self.data_ref.obj2id[obj_name] for obj_name in self.obj_names
        ]
        # with contextlib.redirect_stdout(io.StringIO()):
        #     self._coco_api = COCO(self._metadata.json_file)
        self.model_paths = [
            osp.join(self.data_ref.model_eval_dir,
                     "obj_{:06d}.ply".format(obj_id))
            for obj_id in self.obj_ids
        ]
        self.diameters = [
            self.data_ref.diameters[self.data_ref.objects.index(obj_name)]
            for obj_name in self.obj_names
        ]
        self.models_3d = [
            inout.load_ply(model_path, vertex_scale=self.data_ref.vertex_scale)
            for model_path in self.model_paths
        ]

        if cfg.DEBUG:
            from lib.render_vispy.model3d import load_models
            from lib.render_vispy.renderer import Renderer

            self.get_gts()

            self.kpts_3d = [
                misc.get_bbox3d_and_center(m["pts"]) for m in self.models_3d
            ]
            self.kpts_axis_3d = [
                misc.get_axis3d_and_center(m["pts"], scale=0.5)
                for m in self.models_3d
            ]

            self.ren = Renderer(size=(self.data_ref.width,
                                      self.data_ref.height),
                                cam=self.data_ref.camera_matrix)
            self.ren_models = load_models(
                model_paths=self.data_ref.model_paths,
                scale_to_meter=0.001,
                cache_dir=".cache",
                texture_paths=self.data_ref.texture_paths,
                center=False,
                use_cache=True,
            )

        self.eval_precision = cfg.VAL.get("EVAL_PRECISION", False)
        self._logger.info(f"eval precision: {self.eval_precision}")
        # eval cached
        self.use_cache = False
        if cfg.VAL.EVAL_CACHED or cfg.VAL.EVAL_PRINT_ONLY:
            self.use_cache = True
            if self.eval_precision:
                self._eval_predictions_precision()
            else:
                self._eval_predictions()  # recall
            exit(0)