batch = dataset[0]
    imgId = batch['img_id']
    img_path = img_map[imgId]
    for key in batch:
        if isinstance(batch[key], torch.Tensor):
            batch[key] = batch[key].unsqueeze(0).numpy()
        else:
            try:
                batch[key] = batch[key][np.newaxis, :]
            except:
                continue

    feat = {}
    feat['hm'] = torch.from_numpy(batch['hm'])
    feat['wh'] = torch.from_numpy(
        gen_oracle_map(batch['wh'], batch['ind'], 128, 128))
    feat['reg'] = torch.from_numpy(
        gen_oracle_map(batch['reg'], batch['ind'], 128, 128))
    feat['kps'] = torch.from_numpy(
        gen_oracle_map(batch['kps'], batch['ind'], 128, 128))

    dets = centerface_decode(feat['hm'],
                             feat['wh'],
                             feat['kps'],
                             reg=feat['reg'],
                             K=100)
    dets = dets.detach().cpu().numpy().reshape(1, -1, dets.shape[2])
    dets_out = centerface_post_process(dets.copy(), batch['c'], batch['s'],
                                       feat['hm'].shape[2],
                                       feat['hm'].shape[3],
                                       feat['hm'].shape[1])
Exemplo n.º 2
0
    def _evaluate(self, eval=False):
        logger.info("Epoch {} evaluation start".format(self.epoch))
        title = "EVALUATE" if eval else f"TEST[{self.epoch}/{self.cfg.SOLVER.MAX_EPOCHS}]"
        results = {}
        self._eval_epoch_start()
        out_size = self.out_sizes[0]
        with torch.no_grad():
            self._eval_epoch_start()
            for batch in tqdm(self.vdata, desc=title):
                if self.use_gpu:
                    for key in batch:
                        if isinstance(batch[key], dict):
                            for sub_key in batch[key]:
                                if self.cfg.DISTRIBUTED:
                                    batch[key][sub_key] = batch[key][
                                        sub_key].to(self.device,
                                                    non_blocking=True)
                                else:
                                    batch[key][sub_key] = batch[key][
                                        sub_key].cuda()
                        elif not isinstance(batch[key], torch.Tensor):
                            continue
                        else:
                            if self.cfg.DISTRIBUTED:
                                batch[key] = batch[key].to(self.device,
                                                           non_blocking=True)
                            else:
                                batch[key] = batch[key].cuda()
                if self.cfg.ORACLE:
                    feat = {}
                    feat['hm'] = batch[out_size]['hm']
                    feat['wh'] = torch.from_numpy(
                        gen_oracle_map(
                            batch[out_size]['wh'].detach().cpu().numpy(),
                            batch[out_size]['ind'].detach().cpu().numpy(),
                            batch['inp'].shape[3] // self.cfg.MODEL.STRIDES[0],
                            batch['inp'].shape[2] //
                            self.cfg.MODEL.STRIDES[0])).cuda()
                    feat['reg'] = torch.from_numpy(
                        gen_oracle_map(
                            batch[out_size]['reg'].detach().cpu().numpy(),
                            batch[out_size]['ind'].detach().cpu().numpy(),
                            batch['inp'].shape[3] // self.cfg.MODEL.STRIDES[0],
                            batch['inp'].shape[2] //
                            self.cfg.MODEL.STRIDES[0])).cuda()
                else:
                    feat = self.graph.run(batch['inp'])[out_size]
                    feat['hm'].sigmoid_()

                if self.cfg.DB.TARGET_FORMAT == 'centerface_bbox':
                    feat['wh'].exp_()

                dets = centernet_det_decode(feat['hm'],
                                            feat['wh'],
                                            reg=feat['reg'],
                                            K=100)
                dets = dets.detach().cpu().numpy().reshape(
                    1, -1, dets.shape[1])
                dets_out = centernet_det_post_process(dets.copy(),
                                                      batch['c'].cpu().numpy(),
                                                      batch['s'].cpu().numpy(),
                                                      feat['hm'].shape[2],
                                                      feat['hm'].shape[3],
                                                      feat['hm'].shape[1])
                results[batch['img_id'][0]] = dets_out[0]
        cce = coco_eval(self.vdata.dataset.coco[0], results,
                        self.cfg.OUTPUT_DIR)

        logger.info(
            'Average Precision  (AP) @[ IoU=0.50:0.95 | area=   all | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[2], cce.stats[0]))
        logger.info(
            'Average Precision  (AP) @[ IoU=0.50      | area=   all | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[2], cce.stats[1]))
        logger.info(
            'Average Precision  (AP) @[ IoU=0.75      | area=   all | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[2], cce.stats[2]))
        logger.info(
            'Average Precision  (AP) @[ IoU=0.50:0.95 | area= small | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[2], cce.stats[3]))
        logger.info(
            'Average Precision  (AP) @[ IoU=0.50:0.95 | area=medium | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[2], cce.stats[4]))
        logger.info(
            'Average Precision  (AP) @[ IoU=0.50:0.95 | area= large | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[2], cce.stats[5]))
        logger.info(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[0], cce.stats[6]))
        logger.info(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[1], cce.stats[7]))
        logger.info(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area=   all | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[2], cce.stats[8]))
        logger.info(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area= small | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[2], cce.stats[9]))
        logger.info(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area=medium | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[2], cce.stats[10]))
        logger.info(
            'Average Recall     (AR) @[ IoU=0.50:0.95 | area= large | maxDets={:>3d} ] = {:.3f}'
            .format(cce.params.maxDets[2], cce.stats[11]))

        if not eval:
            self.accu = cce.stats[0]
            self._eval_epoch_end()