Exemple #1
0
    def test(self, kwargs):
        self.reset_meter()
        self.model.eval()
        test_data = kwargs['test_data']
        for data in tqdm(test_data):
            imgs, labels = data
            if opt.use_gpu:
                imgs = imgs.cuda()
                labels = labels.cuda()
            imgs = Variable(imgs, volatile=True)
            labels = Variable(labels, volatile=True)

            # Forward.
            scores = self.model(imgs)
            loss = self.criterion(scores, labels)

            # Update to metrics.
            pred_labels = scores.max(dim=1)[1].data.cpu().numpy()
            pred_labels = [i for i in pred_labels]

            true_labels = labels.data.cpu().numpy()
            true_labels = [i for i in true_labels]

            eval_metrics = eval_semantic_segmentation(pred_labels, true_labels)
            self.metric_meter['loss'].add(loss.data[0])
            self.metric_meter['acc'].add(eval_metrics['mean_class_accuracy'])
            self.metric_meter['iou'].add(eval_metrics['miou'])

        # Plot metrics curve in tensorboard.
        self.writer.add_scalars('loss',
                                {'test': self.metric_meter['loss'].value()[0]},
                                self.n_plot)
        self.writer.add_scalars('acc',
                                {'test': self.metric_meter['acc'].value()[0]},
                                self.n_plot)
        self.writer.add_scalars('iou',
                                {'test': self.metric_meter['iou'].value()[0]},
                                self.n_plot)

        origin_img = inverse_normalization(imgs[0].cpu().data)
        pred_seg = cm[pred_labels[0]]
        gt_seg = cm[true_labels[0]]
        self.writer.add_image('test ori_img', origin_img, self.n_plot)
        self.writer.add_image('test gt', gt_seg, self.n_plot)
        self.writer.add_image('test pred', pred_seg, self.n_plot)

        self.n_plot += 1

        self.metric_log['Test Loss'] = self.metric_meter['loss'].value()[0]
        self.metric_log['Test Mean Class Accuracy'] = self.metric_meter[
            'acc'].value()[0]
        self.metric_log['Test Mean IoU'] = self.metric_meter['iou'].value()[0]
    def test(self, kwargs):
        self.reset_meter()
        self.model.eval()
        test_data = kwargs['test_data']
        for data in tqdm(test_data):
            imgs, labels = data
            if opt.use_gpu:
                imgs = imgs.cuda()
                labels = labels.cuda()
            imgs = Variable(imgs, volatile=True)
            labels = Variable(labels, volatile=True)

            # Forward.
            scores = self.model(imgs)
            loss = self.criterion(scores, labels)

            # Update to metrics.
            pred_labels = scores.max(dim=1)[1].data.cpu().numpy()
            pred_labels = [i for i in pred_labels]

            true_labels = labels.data.cpu().numpy()
            true_labels = [i for i in true_labels]

            eval_metrics = eval_semantic_segmentation(pred_labels, true_labels)
            self.metric_meter['loss'].add(loss.data[0])
            self.metric_meter['acc'].add(eval_metrics['mean_class_accuracy'])
            self.metric_meter['iou'].add(eval_metrics['miou'])

        # Plot metrics curve in tensorboard.
        self.writer.add_scalars('loss',
                                {'test': self.metric_meter['loss'].value()[0]},
                                self.n_plot)
        self.writer.add_scalars(
            'acc', {'test': self.metric_meter['acc'].value()[0]}, self.n_plot)
        self.writer.add_scalars(
            'iou', {'test': self.metric_meter['iou'].value()[0]}, self.n_plot)

        origin_img = inverse_normalization(imgs[0].cpu().data)
        pred_seg = cm[pred_labels[0]]
        gt_seg = cm[true_labels[0]]
        self.writer.add_image('test ori_img', origin_img, self.n_plot)
        self.writer.add_image('test gt', gt_seg, self.n_plot)
        self.writer.add_image('test pred', pred_seg, self.n_plot)

        self.n_plot += 1

        self.metric_log['Test Loss'] = self.metric_meter['loss'].value()[0]
        self.metric_log['Test Mean Class Accuracy'] = self.metric_meter[
            'acc'].value()[0]
        self.metric_log['Test Mean IoU'] = self.metric_meter['iou'].value()[0]
Exemple #3
0
    def train(self, kwargs):
        self.reset_meter()
        self.model.train()
        train_data = kwargs['train_data']
        for data in tqdm(train_data):
            imgs, labels = data
            if opt.use_gpu:
                imgs = imgs.cuda()
                labels = labels.cuda()
            imgs = Variable(imgs)
            labels = Variable(labels)

            # Forward.
            scores = self.model(imgs)
            loss = self.criterion(scores, labels)

            # Backward.
            self.optimizer.zero_grad()
            loss.backward()
            self.optimizer.step()

            # Update to metrics.
            pred_labels = scores.max(dim=1)[1].data.cpu().numpy()
            pred_labels = [i for i in pred_labels]

            true_labels = labels.data.cpu().numpy()
            true_labels = [i for i in true_labels]

            eval_metrics = eval_semantic_segmentation(pred_labels, true_labels)
            self.metric_meter['loss'].add(loss.data[0])
            self.metric_meter['acc'].add(eval_metrics['mean_class_accuracy'])
            self.metric_meter['iou'].add(eval_metrics['miou'])

            if (self.n_iter + 1) % opt.plot_freq == 0:
                # Plot metrics curve in tensorboard.
                self.writer.add_scalars(
                    'loss', {'train': self.metric_meter['loss'].value()[0]},
                    self.n_plot)
                self.writer.add_scalars(
                    'acc', {'train': self.metric_meter['acc'].value()[0]},
                    self.n_plot)
                self.writer.add_scalars(
                    'iou', {'train': self.metric_meter['iou'].value()[0]},
                    self.n_plot)

                # Show segmentation images.
                # Get prediction segmentation and ground truth segmentation.
                origin_image = inverse_normalization(imgs[0].cpu().data)
                pred_seg = cm[pred_labels[0]]
                gt_seg = cm[true_labels[0]]

                self.writer.add_image('train ori_img', origin_image,
                                      self.n_plot)
                self.writer.add_image('train gt', gt_seg, self.n_plot)
                self.writer.add_image('train pred', pred_seg, self.n_plot)
                self.n_plot += 1

            self.n_iter += 1

        self.metric_log['Train Loss'] = self.metric_meter['loss'].value()[0]
        self.metric_log['Train Mean Class Accuracy'] = self.metric_meter[
            'acc'].value()[0]
        self.metric_log['Train Mean IoU'] = self.metric_meter['iou'].value()[0]
    def train(self, kwargs):
        self.reset_meter()
        self.model.train()
        train_data = kwargs['train_data']
        for data in tqdm(train_data):
            imgs, labels = data
            if opt.use_gpu:
                imgs = imgs.cuda()
                labels = labels.cuda()
            imgs = Variable(imgs)
            labels = Variable(labels)

            # Forward.
            scores = self.model(imgs)
            loss = self.criterion(scores, labels)

            # Backward.
            self.optimizer.zero_grad()
            loss.backward()
            self.optimizer.step()

            # Update to metrics.
            pred_labels = scores.max(dim=1)[1].data.cpu().numpy()
            pred_labels = [i for i in pred_labels]

            true_labels = labels.data.cpu().numpy()
            true_labels = [i for i in true_labels]

            eval_metrics = eval_semantic_segmentation(pred_labels, true_labels)
            self.metric_meter['loss'].add(loss.data[0])
            self.metric_meter['acc'].add(eval_metrics['mean_class_accuracy'])
            self.metric_meter['iou'].add(eval_metrics['miou'])

            if (self.n_iter + 1) % opt.plot_freq == 0:
                # Plot metrics curve in tensorboard.
                self.writer.add_scalars(
                    'loss', {'train': self.metric_meter['loss'].value()[0]},
                    self.n_plot)
                self.writer.add_scalars(
                    'acc', {'train': self.metric_meter['acc'].value()[0]},
                    self.n_plot)
                self.writer.add_scalars(
                    'iou', {'train': self.metric_meter['iou'].value()[0]},
                    self.n_plot)

                # Show segmentation images.
                # Get prediction segmentation and ground truth segmentation.
                origin_image = inverse_normalization(imgs[0].cpu().data)
                pred_seg = cm[pred_labels[0]]
                gt_seg = cm[true_labels[0]]

                self.writer.add_image('train ori_img', origin_image,
                                      self.n_plot)
                self.writer.add_image('train gt', gt_seg, self.n_plot)
                self.writer.add_image('train pred', pred_seg, self.n_plot)
                self.n_plot += 1

            self.n_iter += 1

        self.metric_log['Train Loss'] = self.metric_meter['loss'].value()[0]
        self.metric_log['Train Mean Class Accuracy'] = self.metric_meter[
            'acc'].value()[0]
        self.metric_log['Train Mean IoU'] = self.metric_meter['iou'].value()[0]