Пример #1
0
    def test_epoch(self,
                   loader_src,
                   loader_tar_ul,
                   loader_tar_l,
                   epoch,
                   writer=None,
                   write_images=False,
                   device=0):
        """
        Trains the network for one epoch
        :param loader_src: source dataloader (labeled)
        :param loader_tar_ul: target dataloader (unlabeled)
        :param loader_tar_l: target dataloader (labeled)
        :param epoch: current epoch
        :param writer: summary writer
        :param write_images: frequency of writing images
        :param device: GPU device where the computations should occur
        :return: average training loss over the epoch
        """
        # perform training on GPU/CPU
        module_to_device(self, device)
        self.eval()

        # keep track of the average loss during the epoch
        loss_seg_src_cum = 0.0
        loss_seg_tar_cum = 0.0
        total_loss_cum = 0.0
        cnt = 0

        # zip dataloaders
        if loader_tar_l is None:
            dl = zip(loader_src)
        else:
            dl = zip(loader_src, loader_tar_l)

        # start epoch
        y_preds = []
        ys = []
        time_start = datetime.datetime.now()
        for i, data in enumerate(dl):

            # transfer to suitable device
            x_src, y_src = tensor_to_device(data[0], device)
            x_tar_l, y_tar_l = tensor_to_device(data[1], device)
            x_src = x_src.float()
            x_tar_l = x_tar_l.float()
            y_src = y_src.long()
            y_tar_l = y_tar_l.long()

            # forward prop and compute loss
            y_src_pred = self(x_src)
            y_tar_l_pred = self(x_tar_l)
            loss_seg_src = self.seg_loss(y_src_pred, y_src[:, 0, ...])
            loss_seg_tar = self.seg_loss(y_tar_l_pred, y_tar_l[:, 0, ...])
            total_loss = loss_seg_src + loss_seg_tar

            loss_seg_src_cum += loss_seg_src.data.cpu().numpy()
            loss_seg_tar_cum += loss_seg_tar.data.cpu().numpy()
            total_loss_cum += total_loss.data.cpu().numpy()
            cnt += 1

            for b in range(y_tar_l_pred.size(0)):
                y_preds.append(
                    F.softmax(y_tar_l_pred,
                              dim=1)[b, ...].view(y_tar_l_pred.size(1),
                                                  -1).data.cpu().numpy())
                ys.append(y_tar_l[b, 0, ...].flatten().cpu().numpy())

        # keep track of time
        runtime = datetime.datetime.now() - time_start
        seconds = runtime.total_seconds()
        hours = seconds // 3600
        minutes = (seconds - hours * 3600) // 60
        seconds = seconds - hours * 3600 - minutes * 60
        print_frm(
            'Epoch %5d - Runtime for testing: %d hours, %d minutes, %f seconds'
            % (epoch, hours, minutes, seconds))

        # prep for metric computation
        y_preds = np.concatenate(y_preds, axis=1)
        ys = np.concatenate(ys)
        js = np.asarray([
            jaccard((ys == i).astype(int), y_preds[i, :])
            for i in range(len(self.coi))
        ])
        ams = np.asarray([
            accuracy_metrics((ys == i).astype(int), y_preds[i, :])
            for i in range(len(self.coi))
        ])

        # don't forget to compute the average and print it
        loss_seg_src_avg = loss_seg_src_cum / cnt
        loss_seg_tar_avg = loss_seg_tar_cum / cnt
        total_loss_avg = total_loss_cum / cnt
        print(
            '[%s] Testing Epoch %4d - Loss seg src: %.6f - Loss seg tar: %.6f - Loss: %.6f'
            % (datetime.datetime.now(), epoch, loss_seg_src_avg,
               loss_seg_tar_avg, total_loss_avg))

        # log everything
        if writer is not None:

            # always log scalars
            log_scalars([
                loss_seg_src_avg, loss_seg_tar_avg, total_loss_avg,
                np.mean(js, axis=0), *(np.mean(ams, axis=0))
            ], [
                'test/' + s for s in [
                    'loss-seg-src', 'loss-seg-tar', 'total-loss', 'jaccard',
                    'accuracy', 'balanced-accuracy', 'precision', 'recall',
                    'f-score'
                ]
            ],
                        writer,
                        epoch=epoch)

            # log images if necessary
            if write_images:
                y_src_pred = F.softmax(y_src_pred, dim=1)[:, 1:2, :, :].data
                y_tar_l_pred = F.softmax(y_tar_l_pred, dim=1)[:,
                                                              1:2, :, :].data
                log_images_2d([
                    x_src.data, y_src.data, y_src_pred, x_tar_l.data, y_tar_l,
                    y_tar_l_pred
                ], [
                    'test/' + s for s in [
                        'src/x', 'src/y', 'src/y-pred', 'tar/x-l', 'tar/y-l',
                        'tar/y-l-pred'
                    ]
                ],
                              writer,
                              epoch=epoch)

        return total_loss_avg
Пример #2
0
    def test_epoch(self,
                   loader_src,
                   loader_tar_ul,
                   loader_tar_l,
                   epoch,
                   writer=None,
                   write_images=False,
                   device=0):
        """
        Trains the network for one epoch
        :param loader_src: source dataloader (labeled)
        :param loader_tar_ul: target dataloader (unlabeled)
        :param loader_tar_l: target dataloader (labeled)
        :param epoch: current epoch
        :param writer: summary writer
        :param write_images: frequency of writing images
        :param device: GPU device where the computations should occur
        :return: average training loss over the epoch
        """
        # perform training on GPU/CPU
        module_to_device(self, device)
        self.eval()

        # keep track of the average loss during the epoch
        loss_seg_src_cum = 0.0
        loss_seg_tar_cum = 0.0
        loss_rec_src_cum = 0.0
        loss_rec_tar_cum = 0.0
        loss_dc_x_cum = 0.0
        loss_dc_y_cum = 0.0
        total_loss_cum = 0.0
        cnt = 0

        # zip dataloaders
        dl = zip(loader_src, loader_tar_ul, loader_tar_l)

        # start epoch
        y_preds = []
        ys = []
        time_start = datetime.datetime.now()
        for i, data in enumerate(dl):

            # transfer to suitable device
            x_src, y_src = tensor_to_device(data[0], device)
            x_tar_ul = tensor_to_device(data[1], device)
            x_tar_l, y_tar_l = tensor_to_device(data[2], device)
            x_src = x_src.float()
            x_tar_ul = x_tar_ul.float()
            x_tar_l = x_tar_l.float()
            y_src = y_src.long()
            y_tar_l = y_tar_l.long()

            # get domain labels for domain confusion
            dom_labels_x = tensor_to_device(
                torch.zeros((x_src.size(0) + x_tar_ul.size(0))),
                device).long()
            dom_labels_x[x_src.size(0):] = 1
            dom_labels_y = tensor_to_device(
                torch.zeros((x_src.size(0) + x_tar_ul.size(0))),
                device).long()
            dom_labels_y[x_src.size(0):] = 1

            # check train mode and compute loss
            loss_seg_src = torch.Tensor([0])
            loss_seg_tar = torch.Tensor([0])
            loss_rec_src = torch.Tensor([0])
            loss_rec_tar = torch.Tensor([0])
            loss_dc_x = torch.Tensor([0])
            loss_dc_y = torch.Tensor([0])
            if self.train_mode == RECONSTRUCTION:
                x_src_rec, x_src_rec_dom = self.forward_rec(x_src)
                x_tar_ul_rec, x_tar_ul_rec_dom = self.forward_rec(x_tar_ul)
                loss_rec_src = self.rec_loss(x_src_rec, x_src)
                loss_rec_tar = self.rec_loss(x_tar_ul, x_tar_ul_rec)
                loss_dc_x = self.dc_loss(
                    torch.cat((x_src_rec_dom, x_tar_ul_rec_dom), dim=0),
                    dom_labels_x)
                total_loss = loss_rec_src + loss_rec_tar + self.lambda_dc * loss_dc_x
            elif self.train_mode == SEGMENTATION:
                # switch between reconstructed and original inputs
                if np.random.rand() < self.p:
                    y_src_pred, y_src_pred_dom = self.forward_seg(x_src)
                else:
                    x_src_rec, _ = self.forward_rec(x_src)
                    y_src_pred, y_src_pred_dom = self.forward_seg(x_src_rec)
                    dom_labels_y[:x_src.size(0)] = 1
                if np.random.rand() < self.p:
                    y_tar_ul_pred, y_tar_ul_pred_dom = self.forward_seg(
                        x_tar_ul)
                else:
                    x_tar_ul_rec, _ = self.forward_rec(x_tar_ul)
                    y_tar_ul_pred, y_tar_ul_pred_dom = self.forward_seg(
                        x_tar_ul_rec)
                    dom_labels_y[x_src.size(0):] = 1
                loss_seg_src = self.seg_loss(y_src_pred, y_src[:, 0, ...])
                loss_dc_y = self.dc_loss(
                    torch.cat((y_src_pred_dom, y_tar_ul_pred_dom), dim=0),
                    dom_labels_y)
                total_loss = loss_seg_src + self.lambda_dc * loss_dc_y
                y_tar_l_pred, _ = self.forward_seg(x_tar_l)
                loss_seg_tar = self.seg_loss(y_tar_l_pred, y_tar_l[:, 0, ...])
                total_loss = total_loss + loss_seg_tar
            else:
                x_src_rec, x_src_rec_dom = self.forward_rec(x_src)
                if np.random.rand() < self.p:
                    y_src_pred, y_src_pred_dom = self.forward_seg(x_src)
                else:
                    y_src_pred, y_src_pred_dom = self.forward_seg(x_src_rec)
                    dom_labels_y[:x_src.size(0)] = 1
                x_tar_ul_rec, x_tar_ul_rec_dom = self.forward_rec(x_tar_ul)
                if np.random.rand() < self.p:
                    y_tar_ul_pred, y_tar_ul_pred_dom = self.forward_seg(
                        x_tar_ul)
                else:
                    y_tar_ul_pred, y_tar_ul_pred_dom = self.forward_seg(
                        x_tar_ul_rec)
                    dom_labels_y[x_src.size(0):] = 1
                loss_rec_src = self.rec_loss(x_src_rec, x_src)
                loss_rec_tar = self.rec_loss(x_tar_ul, x_tar_ul_rec)
                loss_seg_src = self.seg_loss(y_src_pred, y_src[:, 0, ...])
                loss_dc_x = self.dc_loss(
                    torch.cat((x_src_rec_dom, x_tar_ul_rec_dom), dim=0),
                    dom_labels_x)
                loss_dc_y = self.dc_loss(
                    torch.cat((y_src_pred_dom, y_tar_ul_pred_dom), dim=0),
                    dom_labels_y)
                total_loss = loss_seg_src + self.lambda_rec * (loss_rec_src + loss_rec_tar) + \
                             self.lambda_dc * (loss_dc_x + loss_dc_y)
                _, y_tar_l_pred, _, y_tar_l_pred_dom = self(x_tar_l)
                loss_seg_tar = self.seg_loss(y_tar_l_pred, y_tar_l[:, 0, ...])
                total_loss = total_loss + loss_seg_tar

            loss_seg_src_cum += loss_seg_src.data.cpu().numpy()
            loss_seg_tar_cum += loss_seg_tar.data.cpu().numpy()
            loss_rec_src_cum += loss_rec_src.data.cpu().numpy()
            loss_rec_tar_cum += loss_rec_tar.data.cpu().numpy()
            loss_dc_x_cum += loss_dc_x.data.cpu().numpy()
            loss_dc_y_cum += loss_dc_y.data.cpu().numpy()
            total_loss_cum += total_loss.data.cpu().numpy()
            cnt += 1

            if self.train_mode == SEGMENTATION or self.train_mode == JOINT:
                for b in range(y_tar_l_pred.size(0)):
                    y_preds.append(
                        F.softmax(y_tar_l_pred,
                                  dim=1)[b, ...].view(y_tar_l_pred.size(1),
                                                      -1).data.cpu().numpy())
                    ys.append(y_tar_l[b, 0, ...].flatten().cpu().numpy())

        # keep track of time
        runtime = datetime.datetime.now() - time_start
        seconds = runtime.total_seconds()
        hours = seconds // 3600
        minutes = (seconds - hours * 3600) // 60
        seconds = seconds - hours * 3600 - minutes * 60
        print_frm(
            'Epoch %5d - Runtime for testing: %d hours, %d minutes, %f seconds'
            % (epoch, hours, minutes, seconds))

        # prep for metric computation
        if self.train_mode == SEGMENTATION or self.train_mode == JOINT:
            y_preds = np.concatenate(y_preds, axis=1)
            ys = np.concatenate(ys)
            js = np.asarray([
                jaccard((ys == i).astype(int), y_preds[i, :])
                for i in range(len(self.coi))
            ])
            ams = np.asarray([
                accuracy_metrics((ys == i).astype(int), y_preds[i, :])
                for i in range(len(self.coi))
            ])

        # don't forget to compute the average and print it
        loss_seg_src_avg = loss_seg_src_cum / cnt
        loss_seg_tar_avg = loss_seg_tar_cum / cnt
        loss_rec_src_avg = loss_rec_src_cum / cnt
        loss_rec_tar_avg = loss_rec_tar_cum / cnt
        loss_dc_x_avg = loss_dc_x_cum / cnt
        loss_dc_y_avg = loss_dc_y_cum / cnt
        total_loss_avg = total_loss_cum / cnt
        print(
            '[%s] Testing Epoch %5d - Loss seg src: %.6f - Loss seg tar: %.6f - Loss rec src: %.6f - Loss rec tar: %.6f - Loss DCX: %.6f - Loss DCY: %.6f - Loss: %.6f'
            % (datetime.datetime.now(), epoch, loss_seg_src_avg,
               loss_seg_tar_avg, loss_rec_src_avg, loss_rec_tar_avg,
               loss_dc_x_avg, loss_dc_y_avg, total_loss_avg))

        # log everything
        if writer is not None:

            # always log scalars
            if self.train_mode == RECONSTRUCTION:
                log_scalars(
                    [loss_rec_src_avg, loss_rec_tar_avg, loss_dc_x_avg], [
                        'test/' + s
                        for s in ['loss-rec-src', 'loss-rec-tar', 'loss-dc-x']
                    ],
                    writer,
                    epoch=epoch)
            elif self.train_mode == SEGMENTATION:
                log_scalars([
                    loss_seg_src_avg, loss_seg_tar_avg, loss_dc_y_avg,
                    np.mean(js, axis=0), *(np.mean(ams, axis=0))
                ], [
                    'test/' + s for s in [
                        'loss-seg-src', 'loss-seg-tar', 'loss-dc-y', 'jaccard',
                        'accuracy', 'balanced-accuracy', 'precision', 'recall',
                        'f-score'
                    ]
                ],
                            writer,
                            epoch=epoch)
            else:
                log_scalars([
                    loss_seg_src_avg, loss_seg_tar_avg, loss_rec_src_avg,
                    loss_rec_tar_avg, loss_dc_x_avg, loss_dc_y_avg,
                    np.mean(js, axis=0), *(np.mean(ams, axis=0))
                ], [
                    'test/' + s for s in [
                        'loss-seg-src', 'loss-seg-tar', 'loss-rec-src',
                        'loss-rec-tar', 'loss-dc-x', 'loss-dc-y', 'jaccard',
                        'accuracy', 'balanced-accuracy', 'precision', 'recall',
                        'f-score'
                    ]
                ],
                            writer,
                            epoch=epoch)
            log_scalars([total_loss_avg],
                        ['test/' + s for s in ['total-loss']],
                        writer,
                        epoch=epoch)

            # log images if necessary
            if write_images:
                log_images_2d([x_src.data], ['test/' + s for s in ['src/x']],
                              writer,
                              epoch=epoch)
                if self.train_mode == RECONSTRUCTION:
                    log_images_2d(
                        [x_src_rec.data, x_tar_ul.data, x_tar_ul_rec.data], [
                            'test/' + s
                            for s in ['src/x-rec', 'tar/x-ul', 'tar/x-ul-rec']
                        ],
                        writer,
                        epoch=epoch)
                elif self.train_mode == SEGMENTATION:
                    y_src_pred = F.softmax(y_src_pred, dim=1)[:,
                                                              1:2, :, :].data
                    log_images_2d(
                        [y_src.data, y_src_pred],
                        ['test/' + s for s in ['src/y', 'src/y-pred']],
                        writer,
                        epoch=epoch)
                    if loader_tar_l is not None:
                        y_tar_l_pred = F.softmax(y_tar_l_pred,
                                                 dim=1)[:, 1:2, :, :].data
                        log_images_2d([x_tar_l.data, y_tar_l, y_tar_l_pred], [
                            'test/' + s
                            for s in ['tar/x-l', 'tar/y-l', 'tar/y-l-pred']
                        ],
                                      writer,
                                      epoch=epoch)
                else:
                    y_src_pred = F.softmax(y_src_pred, dim=1)[:,
                                                              1:2, :, :].data
                    log_images_2d([
                        x_src_rec.data, y_src.data, y_src_pred, x_tar_ul.data,
                        x_tar_ul_rec.data
                    ], [
                        'test/' + s for s in [
                            'src/x-rec', 'src/y', 'src/y-pred', 'tar/x-ul',
                            'tar/x-ul-rec'
                        ]
                    ],
                                  writer,
                                  epoch=epoch)
                    if loader_tar_l is not None:
                        y_tar_l_pred = F.softmax(y_tar_l_pred,
                                                 dim=1)[:, 1:2, :, :].data
                        log_images_2d([x_tar_l.data, y_tar_l, y_tar_l_pred], [
                            'test/' + s
                            for s in ['tar/x-l', 'tar/y-l', 'tar/y-l-pred']
                        ],
                                      writer,
                                      epoch=epoch)

        return total_loss_avg
Пример #3
0
    def test_epoch(self,
                   loader_src,
                   loader_tar,
                   loss_seg_fn,
                   loss_rec_fn,
                   epoch,
                   writer=None,
                   write_images=False,
                   device=0):
        """
        Tests the network for one epoch
        :param loader_src: source dataloader (should be labeled)
        :param loader_tar: target dataloader (should be labeled)
        :param loss_seg_fn: segmentation loss function
        :param loss_rec_fn: reconstruction loss function
        :param epoch: current epoch
        :param writer: summary writer
        :param write_images: frequency of writing images
        :param device: GPU device where the computations should occur
        :return: average training loss over the epoch
        """
        # perform training on GPU/CPU
        module_to_device(self, device)
        self.eval()

        # keep track of the average loss and metrics during the epoch
        loss_seg_cum = 0.0
        loss_rec_cum = 0.0
        total_loss_cum = 0.0
        cnt = 0

        # start epoch
        y_src_preds = []
        ys_src = []
        y_tar_preds = []
        ys_tar = []
        for i, data in enumerate(zip(loader_src, loader_tar)):
            # get inputs and transfer to suitable device
            x_src, y_src = tensor_to_device(data[0], device)
            x_tar, y_tar = tensor_to_device(data[1], device)
            y_src = get_labels(y_src, coi=self.coi, dtype=int)
            y_tar = get_labels(y_tar, coi=self.coi, dtype=int)
            x_src = x_src.float()
            x_tar = x_tar.float()

            # zero the gradient buffers
            self.zero_grad()

            # forward prop
            y_src_pred = self(x_src)
            x_src_pred = self.reconstruction_outputs
            y_tar_pred = self(x_tar)
            x_tar_pred = self.reconstruction_outputs

            # compute loss
            loss_seg = loss_seg_fn(y_src_pred, y_src)
            loss_rec = 0.5 * (loss_rec_fn(x_src_pred, x_src) +
                              loss_rec_fn(x_tar_pred, x_tar))
            total_loss = loss_seg + self.lambda_rec * loss_rec
            loss_seg_cum += loss_seg.data.cpu().numpy()
            loss_rec_cum += loss_rec.data.cpu().numpy()
            total_loss_cum += total_loss.data.cpu().numpy()
            cnt += 1

            for b in range(y_src_pred.size(0)):
                y_src_preds.append(
                    F.softmax(y_src_pred, dim=1).data.cpu().numpy()[b, 1, ...])
                y_tar_preds.append(
                    F.softmax(y_tar_pred, dim=1).data.cpu().numpy()[b, 1, ...])
                ys_src.append(y_src[b, 0, ...].cpu().numpy())
                ys_tar.append(y_tar[b, 0, ...].cpu().numpy())

        # compute interesting metrics
        y_src_preds = np.asarray(y_src_preds)
        y_tar_preds = np.asarray(y_tar_preds)
        ys_src = np.asarray(ys_src)
        ys_tar = np.asarray(ys_tar)
        j_src = jaccard(ys_src, y_src_preds)
        j_tar = jaccard(ys_src, y_tar_preds)
        a_src, ba_src, p_src, r_src, f_src = accuracy_metrics(
            ys_src, y_src_preds)
        a_tar, ba_tar, p_tar, r_tar, f_tar = accuracy_metrics(
            ys_tar, y_tar_preds)

        # don't forget to compute the average and print it
        loss_seg_avg = loss_seg_cum / cnt
        loss_rec_avg = loss_rec_cum / cnt
        total_loss_avg = total_loss_cum / cnt
        print('[%s] Epoch %5d - Loss seg: %.6f - Loss rec: %.6f - Loss: %.6f' %
              (datetime.datetime.now(), epoch, loss_seg_avg, loss_rec_avg,
               total_loss_avg))

        # log everything
        if writer is not None:

            # always log scalars
            log_scalars([
                loss_seg_avg, loss_rec_avg, total_loss_avg, j_src, a_src,
                ba_src, p_src, r_src, f_src, j_tar, a_tar, ba_tar, p_tar,
                r_tar, f_tar
            ], [
                'test/' + s for s in [
                    'loss-rec', 'loss-seg', 'total-loss', 'src/jaccard',
                    'src/accuracy', 'src/balanced-accuracy', 'src/precision',
                    'src/recall', 'src/f-score', 'tar/jaccard', 'tar/accuracy',
                    'tar/balanced-accuracy', 'tar/precision', 'tar/recall',
                    'tar/f-score'
                ]
            ],
                        writer,
                        epoch=epoch)

            # log images if necessary
            if write_images:
                y_src_pred = F.softmax(y_src_pred, dim=1)[:, 1:2, ...].data
                y_tar_pred = F.softmax(y_tar_pred, dim=1)[:, 1:2, ...].data
                log_images_3d([
                    x_src, x_src_pred.data, y_src, y_src_pred, x_tar,
                    x_tar_pred.data, y_tar, y_tar_pred
                ], [
                    'test/' + s for s in [
                        'src/x', 'src/x-pred', 'src/y', 'src/y-pred', 'tar/x',
                        'tar/x-pred', 'tar/y', 'tar/y-pred'
                    ]
                ],
                              writer,
                              epoch=epoch)

        return total_loss_avg
Пример #4
0
    def test_epoch(self, loader, loss_fn, epoch, writer=None, write_images=False, device=0):
        """
        Tests the network for one epoch
        :param loader: dataloader
        :param loss_fn: loss function
        :param epoch: current epoch
        :param writer: summary writer
        :param write_images: frequency of writing images
        :param device: GPU device where the computations should occur
        :return: average testing loss over the epoch
        """
        # perform training on GPU/CPU
        module_to_device(self, device)
        self.eval()

        # keep track of the average loss and metrics during the epoch
        loss_cum = 0.0
        cnt = 0

        # test loss
        y_preds = []
        ys = []
        ys_ = []
        time_start = datetime.datetime.now()
        for i, data in enumerate(loader):

            # get the inputs and transfer to suitable device
            x, y = tensor_to_device(data, device)
            y_ = get_unlabeled(y)
            x = x.float()
            y = get_labels(y, coi=self.coi, dtype=int)
            y_ = get_labels(y_, coi=[0, 255], dtype=bool)

            # forward prop
            y_pred = self(x)

            # compute loss
            loss = loss_fn(y_pred, y[:, 0, ...], mask=~y_)
            loss_cum += loss.data.cpu().numpy()
            cnt += 1

            for b in range(y_pred.size(0)):
                y_preds.append(F.softmax(y_pred, dim=1)[b, ...].view(y_pred.size(1), -1).data.cpu().numpy())
                ys.append(y[b, 0, ...].flatten().cpu().numpy())
                ys_.append(y_[b, 0, ...].flatten().cpu().numpy())

        # keep track of time
        runtime = datetime.datetime.now() - time_start
        seconds = runtime.total_seconds()
        hours = seconds // 3600
        minutes = (seconds - hours * 3600) // 60
        seconds = seconds - hours * 3600 - minutes * 60
        print_frm(
            'Epoch %5d - Runtime for testing: %d hours, %d minutes, %f seconds' % (epoch, hours, minutes, seconds))

        # prep for metric computation
        y_preds = np.concatenate(y_preds, axis=1)
        ys = np.concatenate(ys)
        ys_ = np.concatenate(ys_)
        w = (1 - ys_).astype(bool)
        js = np.asarray([jaccard((ys == i).astype(int), y_preds[i, :], w=w) for i in range(len(self.coi))])
        ams = np.asarray([accuracy_metrics((ys == i).astype(int), y_preds[i, :], w=w) for i in range(len(self.coi))])

        # don't forget to compute the average and print it
        loss_avg = loss_cum / cnt
        print_frm('Epoch %5d - Average test loss: %.6f' % (epoch, loss_avg))

        # log everything
        if writer is not None:

            # always log scalars
            log_scalars([loss_avg, np.mean(js, axis=0), *(np.mean(ams, axis=0))], ['test/' + s for s in
                                                                                   ['loss-seg', 'jaccard', 'accuracy',
                                                                                    'balanced-accuracy', 'precision',
                                                                                    'recall', 'f-score']], writer,
                        epoch=epoch)

            # log images if necessary
            if write_images:
                log_images_3d([x], ['test/' + s for s in ['x']], writer, epoch=epoch)
                y_pred = F.softmax(y_pred, dim=1)
                for i, c in enumerate(self.coi):
                    if not i == 0:  # skip background class
                        y_p = y_pred[:, i:i + 1, ...].data
                        y_t = (y == i).long()
                        log_images_3d([y_t, y_p],
                                      ['test/' + s for s in ['y_class_%d)' % (c), 'y_pred_class_%d)' % (c)]], writer,
                                      epoch=epoch)

        return np.mean(js)
Пример #5
0
def validate(net,
             data,
             labels,
             input_size,
             in_channels=1,
             classes_of_interest=(0, 1),
             batch_size=1,
             write_dir=None,
             val_file=None,
             track_progress=False,
             device=0,
             orientations=(0, ),
             normalization='unit'):
    """
    Validate a network on a dataset and its labels

    :param net: image-to-image segmentation network
    :param data: 3D array (Z, Y, X) representing the 3D image
    :param labels: 3D array (Z, Y, X) representing the 3D labels
    :param input_size: size of the inputs (either 2 or 3-tuple) for processing
    :param in_channels: Amount of subsequent slices that serve as input for the network (should be odd)
    :param classes_of_interest: index of the label of interest
    :param batch_size: batch size for processing
    :param write_dir: optionally, specify a directory to write the output
    :param val_file: optionally, specify a file to write the validation results
    :param track_progress: optionally, for tracking progress with progress bar
    :param device: GPU device where the computations should occur
    :param orientations: list of orientations to perform segmentation: 0-Z, 1-Y, 2-X (only for 2D based segmentation)
    :param normalization: type of data normalization (unit, z or minmax)
    :return: validation results, i.e. accuracy, precision, recall, f-score, jaccard and dice score
    """

    print_frm('Validating the trained network...')

    # compute segmentation for each orientation and average results
    segmentation = np.zeros((net.out_channels, *data.shape))
    for orientation in orientations:
        segmentation += segment(data,
                                net,
                                input_size,
                                in_channels=in_channels,
                                batch_size=batch_size,
                                track_progress=track_progress,
                                device=device,
                                orientation=orientation,
                                normalization=normalization)
    segmentation = segmentation / len(orientations)

    # compute metrics
    w = labels != 255
    comp_hausdorff = np.sum(labels == 255) == 0
    js = np.asarray([
        jaccard(segmentation[i], (labels == c).astype('float'), w=w)
        for i, c in enumerate(classes_of_interest)
    ])
    ams = np.asarray([
        accuracy_metrics(segmentation[i], (labels == c).astype('float'), w=w)
        for i, c in enumerate(classes_of_interest)
    ])
    for i, c in enumerate(classes_of_interest):
        if comp_hausdorff:
            h = hausdorff_distance(segmentation[i], labels)[0]
        else:
            h = -1

        # report results
        print_frm('Validation performance for class %d: ' % c)
        print_frm('    - Accuracy: %f' % ams[i, 0])
        print_frm('    - Balanced accuracy: %f' % ams[i, 1])
        print_frm('    - Precision: %f' % ams[i, 2])
        print_frm('    - Recall: %f' % ams[i, 3])
        print_frm('    - F1 score: %f' % ams[i, 4])
        print_frm('    - IoU: %f' % js[i])
        print_frm('    - Hausdorff distance: %f' % h)

    # report results
    print_frm('Validation performance mean: ')
    print_frm('    - Accuracy: %f' % np.mean(ams[:, 0]))
    print_frm('    - Balanced accuracy: %f' % np.mean(ams[:, 1]))
    print_frm('    - Precision: %f' % np.mean(ams[:, 2]))
    print_frm('    - Recall: %f' % np.mean(ams[:, 3]))
    print_frm('    - F1 score: %f' % np.mean(ams[:, 4]))
    print_frm('    - mIoU: %f' % np.mean(js))

    # write stuff if necessary
    if write_dir is not None:
        print_frm('Writing out the segmentation...')
        mkdir(write_dir)
        segmentation_volume = np.zeros(segmentation.shape[1:])
        for i, c in enumerate(classes_of_interest):
            segmentation_volume[segmentation[i] > 0.5] = c
        write_volume(segmentation_volume, write_dir, type='pngseq')
    if val_file is not None:
        np.save(val_file, np.concatenate((js[:, np.newaxis], ams), axis=1))
    return js, ams