def evaluate(self):

        # evaluate model on test_loader
        self.net.eval()
        self.phase = 'test'

        intersection_meter_mlp = self.loss_meters['INTERSECTION_MLP']
        target_meter_mlp = self.loss_meters['LABEL_MLP']
        intersection_meter_lin = self.loss_meters['INTERSECTION_LIN']
        target_meter_lin = self.loss_meters['LABEL_LIN']

        for i, data in enumerate(self.val_loader):
            self.set_input(data)
            with torch.no_grad():
                self._forward(class_only=True)

                [lgt_glb_mlp, lgt_glb_lin] = self.result_encoder['pred']
                lgt_glb_mlp = lgt_glb_mlp.data.max(1)[1]
                lgt_glb_lin = lgt_glb_lin.data.max(1)[1]

            intersection_mlp, union_mlp, label_mlp = util.intersectionAndUnionGPU(
                lgt_glb_mlp, self.label, self.cfg.NUM_CLASSES)
            intersection_lin, union_lin, label_lin = util.intersectionAndUnionGPU(
                lgt_glb_lin, self.label, self.cfg.NUM_CLASSES)
            if self.cfg.MULTIPROCESSING_DISTRIBUTED:
                dist.all_reduce(intersection_mlp), dist.all_reduce(
                    union_mlp), dist.all_reduce(label_mlp)
                dist.all_reduce(intersection_lin), dist.all_reduce(
                    union_lin), dist.all_reduce(label_lin)

            intersection_mlp, union_mlp, label_mlp = intersection_mlp.cpu(
            ).numpy(), union_mlp.cpu().numpy(), label_mlp.cpu().numpy()
            intersection_lin, union_lin, label_lin = intersection_lin.cpu(
            ).numpy(), union_lin.cpu().numpy(), label_lin.cpu().numpy()

            intersection_meter_mlp.update(intersection_mlp, self.batch_size)
            target_meter_mlp.update(label_mlp, self.batch_size)

            intersection_meter_lin.update(intersection_lin, self.batch_size)
            target_meter_lin.update(label_lin, self.batch_size)

        # Mean ACC
        allAcc_mlp = sum(
            intersection_meter_mlp.sum) / (sum(target_meter_mlp.sum) + 1e-10)
        accuracy_class_mlp = intersection_meter_mlp.sum / (
            target_meter_mlp.sum + 1e-10)
        mAcc_mlp = np.mean(accuracy_class_mlp)
        self.loss_meters['VAL_CLS_ACC_MLP'].update(allAcc_mlp)
        self.loss_meters['VAL_CLS_MEAN_ACC_MLP'].update(mAcc_mlp)

        allAcc_lin = sum(
            intersection_meter_lin.sum) / (sum(target_meter_lin.sum) + 1e-10)
        accuracy_class_lin = intersection_meter_lin.sum / (
            target_meter_lin.sum + 1e-10)
        mAcc_lin = np.mean(accuracy_class_lin)
        self.loss_meters['VAL_CLS_ACC_LIN'].update(allAcc_lin)
        self.loss_meters['VAL_CLS_MEAN_ACC_LIN'].update(mAcc_lin)
Пример #2
0
def validation(model, val_loader, criterion):
    #if main_process():

    model.eval()
    end = time.time()

    for i, (input, target) in enumerate(val_loader):

        input = input.cuda(non_blocking=True)
        target = target.cuda(non_blocking=True)
        output = model(input)
        if cfg['zoom_factor'] != 8:
            output = F.interpolate(output,
                                   size=target.size()[1:],
                                   mode='bilinear',
                                   align_corners=True)

        loss = criterion(output, target)
        n = input.size(0)

        seg = output.max(1)[1]
        intersection, union, target = intersectionAndUnionGPU(
            output, target, cfg['num_class'], cfg['ignore_label'])
        intersection, union, target = intersection.cpu().numpy(), union.cpu(
        ).numpy(), target.cpu().numpy()
    def validate(self):

        self.phase = 'test'

        # switch to evaluate mode
        self.net.eval()

        intersection_meter = self.loss_meters['INTERSECTION']
        union_meter = self.loss_meters['UNION']
        target_meter = self.loss_meters['LABEL']

        if self.cfg.USE_FAKE_DATA or self.cfg.INFERENCE:
            self.pred_index_all = []
            self.target_index_all = []

        with torch.no_grad():

            # batch_index = int(self.val_image_num / cfg.BATCH_SIZE)
            # random_id = random.randint(0, batch_index)

            # batch = tqdm(self.val_loader)
            # for data in batch:
            for i, data in enumerate(self.val_loader):
                self.set_input(data)

                self._forward(cal_loss=False)
                if self.cfg.INFERENCE:
                    self._process_fc()
                self.pred = self.result['cls'].data.max(1)[1]
                intersection, union, label = util.intersectionAndUnionGPU(
                    self.pred, self.label, self.cfg.NUM_CLASSES)
                if self.cfg.MULTIPROCESSING_DISTRIBUTED:
                    dist.all_reduce(intersection), dist.all_reduce(
                        union), dist.all_reduce(label)

                intersection, union, label = intersection.cpu().numpy(
                ), union.cpu().numpy(), label.cpu().numpy()

                intersection_meter.update(intersection, self.batch_size)
                union_meter.update(union, self.batch_size)
                target_meter.update(label, self.batch_size)

        # Mean ACC
        # self._cal_mean_acc(self.cfg,self.val_loader)
        accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
        self.accuracy_class = accuracy_class
        mAcc = np.mean(accuracy_class)
        allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)

        self.loss_meters['VAL_CLS_ACC'].update(allAcc)
        self.loss_meters['VAL_CLS_MEAN_ACC'].update(mAcc)

        if self.cfg.TASK_TYPE == 'segmentation':
            iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
            mIoU = np.mean(iou_class)
            self.loss_meters['VAL_CLS_MEAN_IOU'].update(mIoU)
    def validate_slide_window(self):

        self.net.eval()
        self.phase = 'test'

        intersection_meter = self.loss_meters['INTERSECTION']
        union_meter = self.loss_meters['UNION']
        target_meter = self.loss_meters['LABEL']

        print('testing with sliding windows...')
        num_images = 0

        # batch = tqdm(self.val_loader)
        # for data in batch:
        for data in self.val_loader:
            self.set_input(data)
            num_images += self.batch_size
            pred = util.slide_cal(model=self.net,
                                  image=self.source_modal,
                                  crop_size=self.cfg.FINE_SIZE,
                                  prediction_matrix=self.prediction_matrix[
                                      0:self.batch_size, :, :, :],
                                  count_crop_matrix=self.count_crop_matrix[
                                      0:self.batch_size, :, :, :])

            self.pred = pred.data.max(1)[1]
            intersection, union, label = util.intersectionAndUnionGPU(
                self.pred, self.label, self.cfg.NUM_CLASSES)
            if self.cfg.MULTIPROCESSING_DISTRIBUTED:
                dist.all_reduce(intersection), dist.all_reduce(
                    union), dist.all_reduce(label)
            intersection, union, label = intersection.cpu().numpy(), union.cpu(
            ).numpy(), label.cpu().numpy()

            intersection_meter.update(intersection, self.batch_size)
            union_meter.update(union, self.batch_size)
            target_meter.update(label, self.batch_size)

        iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
        accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
        mIoU = np.mean(iou_class)
        mAcc = np.mean(accuracy_class)
        allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)

        self.loss_meters['VAL_CLS_ACC'].update(allAcc)
        self.loss_meters['VAL_CLS_MEAN_ACC'].update(mAcc)
        self.loss_meters['VAL_CLS_MEAN_IOU'].update(mIoU)
Пример #5
0
    def validate(self):

        self.phase = 'test'

        # switch to evaluate mode
        self.net.eval()

        intersection_meter = AverageMeter()
        union_meter = AverageMeter()
        target_meter = AverageMeter()
        # batch = tqdm(self.val_loader, total=self.val_image_num // self.batch_size_val)
        with torch.no_grad():
            for i, data in enumerate(self.val_loader):
                self.set_input(data)
                self._forward(if_cls=True, if_trans=False)

                cls_loss = self.result['loss_cls'].mean() * self.cfg.ALPHA_CLS
                self.loss_meters['VAL_CLS_LOSS'].update(cls_loss)
                self.pred = self.cls.data.max(1)[1]
                # self.pred = self.cls.data.max(1)[1].cpu().numpy()
                # label = np.uint8(self.label)

                intersection, union, label = util.intersectionAndUnionGPU(
                    self.pred.cuda(), self.label.cuda(), self.cfg.NUM_CLASSES)
                if self.cfg.multiprocessing_distributed:
                    dist.all_reduce(intersection), dist.all_reduce(
                        union), dist.all_reduce(label)
                intersection, union, label = intersection.cpu().numpy(
                ), union.cpu().numpy(), label.cpu().numpy()
                intersection_meter.update(intersection), union_meter.update(
                    union), target_meter.update(label)

        iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
        accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
        mIoU = np.mean(iou_class)
        mAcc = np.mean(accuracy_class)
        allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)

        self.loss_meters['VAL_CLS_ACC'].update(allAcc)
        self.loss_meters['VAL_CLS_MEAN_ACC'].update(mAcc)
        self.loss_meters['VAL_CLS_MEAN_IOU'].update(mIoU)
Пример #6
0
    def test_slide(self):
        intersection_meter = AverageMeter()
        union_meter = AverageMeter()
        target_meter = AverageMeter()
        self.net.eval()
        self.phase = 'test'

        print('testing sliding windows...')
        # batch = tqdm(self.val_loader, total=self.val_image_num // self.batch_size_val)
        for i, data in enumerate(self.val_loader):
            self.set_input(data)
            prediction = util.slide_cal(model=self.net,
                                        image=self.source_modal,
                                        classes=self.cfg.NUM_CLASSES,
                                        crop_size=self.cfg.FINE_SIZE)
            # self.pred = prediction.data.max(1)[1].cpu().numpy()
            self.pred = prediction.max(1)[1]
            # label = np.uint8(self.label)

            intersection, union, label = util.intersectionAndUnionGPU(
                self.pred.cuda(), self.label.cuda(), self.cfg.NUM_CLASSES)
            if self.cfg.multiprocessing_distributed:
                dist.all_reduce(intersection), dist.all_reduce(
                    union), dist.all_reduce(label)
            intersection, union, label = intersection.cpu().numpy(), union.cpu(
            ).numpy(), label.cpu().numpy()
            intersection_meter.update(intersection)
            union_meter.update(union)
            target_meter.update(label)
            print(sum(intersection_meter.val) / sum(target_meter.val))

        iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
        accuracy_class = intersection_meter.sum / (target_meter.sum + 1e-10)
        mIoU = np.mean(iou_class)
        mAcc = np.mean(accuracy_class)
        allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)

        self.loss_meters['VAL_CLS_ACC'].update(allAcc)
        self.loss_meters['VAL_CLS_MEAN_ACC'].update(mAcc)
        self.loss_meters['VAL_CLS_MEAN_IOU'].update(mIoU)
    def validate(self):

        self.phase = 'test'

        # switch to evaluate mode
        self.net.eval()
        self.evaluator.reset()

        intersection_meter = self.loss_meters['INTERSECTION']
        union_meter = self.loss_meters['UNION']
        target_meter = self.loss_meters['LABEL']

        self.pred_index_all = []
        self.target_index_all = []

        with torch.no_grad():

            # batch_index = int(self.val_image_num / cfg.BATCH_SIZE)
            # random_id = random.randint(0, batch_index)

            # batch = tqdm(self.val_loader)
            # for data in batch:
            for i, data in enumerate(self.val_loader):
                self.set_input(data)
                self.data = data  #new add
                self._forward(cal_loss=False)

                self._process_fc()

                self.pred = self.result['cls'].data.max(1)[1]
                intersection, union, label = util.intersectionAndUnionGPU(
                    self.pred, self.label, self.cfg.NUM_CLASSES)
                if self.cfg.MULTIPROCESSING_DISTRIBUTED:
                    dist.all_reduce(intersection), dist.all_reduce(
                        union), dist.all_reduce(label)

                intersection, union, _label = intersection.cpu().numpy(
                ), union.cpu().numpy(), label.cpu().numpy()

                intersection_meter.update(intersection, self.batch_size)
                union_meter.update(union, self.batch_size)
                target_meter.update(_label, self.batch_size)

                self.evaluator.add_batch(self.label.cpu().numpy(),
                                         self.pred.cpu().numpy())

        # Mean ACC
        self.accuracy_class = intersection_meter.sum / (target_meter.sum +
                                                        1e-10)
        mAcc = np.mean(self.accuracy_class)
        allAcc = sum(intersection_meter.sum) / (sum(target_meter.sum) + 1e-10)

        self.loss_meters['VAL_CLS_ACC'].update(allAcc)
        self.loss_meters['VAL_CLS_MEAN_ACC'].update(mAcc)
        # mean_acc = self._cal_mean_acc(self.cfg, self.val_loader)
        # print('mean_acc:', mean_acc)

        if self.cfg.TASK_TYPE == 'segmentation':
            iou_class = intersection_meter.sum / (union_meter.sum + 1e-10)
            mIoU = np.mean(iou_class)
            self.loss_meters['VAL_CLS_MEAN_IOU'].update(mIoU)

        Acc = self.evaluator.Pixel_Accuracy()
        Acc_class = self.evaluator.Pixel_Accuracy_Class()
        mIoU = self.evaluator.Mean_Intersection_over_Union()
        FWIoU = self.evaluator.Frequency_Weighted_Intersection_over_Union()
        # # self.writer.add_scalar('val/total_loss_epoch', test_loss, epoch)
        # # self.writer.add_scalar('val/mIoU', mIoU, epoch)
        # # self.writer.add_scalar('val/Acc', Acc, epoch)
        # # self.writer.add_scalar('val/Acc_class', Acc_class, epoch)
        # # self.writer.add_scalar('val/fwIoU', FWIoU, epoch)
        # print('Validation new:')
        print('Eval: ********')
        print("Acc:{}, Acc_class:{}, mIoU:{}, fwIoU: {}".format(
            Acc, Acc_class, mIoU, FWIoU))