コード例 #1
0
    def validate(self):
        self.discriminator.eval()

        prediction_image = torch.zeros([self.valloader.dataset.label.shape[0], self.config.patch_shape[0],\
                                        self.config.patch_shape[1], self.config.patch_shape[2]])
        whole_vol = self.valloader.dataset.whole_vol
        for batch_number, (patches, label,
                           _) in enumerate(self.valloader.loader):
            patches = patches.cuda()
            _, batch_prediction_softmax = self.discriminator(patches)
            batch_prediction = torch.argmax(batch_prediction_softmax,
                                            dim=1).cpu()
            prediction_image[
                batch_number * self.config.batch_size:(batch_number + 1) *
                self.config.batch_size, :, :, :] = batch_prediction

            print("Validating.. [{0}/{1}]".format(
                batch_number, self.valloader.num_iterations))

        vol_shape_x, vol_shape_y, vol_shape_z = self.config.volume_shape
        prediction_image = prediction_image.numpy()
        val_image_pred = recompose3D_overlap(prediction_image, vol_shape_x,
                                             vol_shape_y, vol_shape_z,
                                             self.config.extraction_step[0],
                                             self.config.extraction_step[1],
                                             self.config.extraction_step[2])
        val_image_pred = val_image_pred.astype('uint8')
        pred2d = np.reshape(val_image_pred,
                            (val_image_pred.shape[0] * vol_shape_x *
                             vol_shape_y * vol_shape_z))
        lab2d = np.reshape(
            whole_vol,
            (whole_vol.shape[0] * vol_shape_x * vol_shape_y * vol_shape_z))

        classes = list(range(0, self.config.num_classes))
        F1_score = f1_score(lab2d, pred2d, classes, average=None)
        print("Validation Dice Coefficient.... ")
        print("Background:", F1_score[0])
        print("CSF:", F1_score[1])
        print("GM:", F1_score[2])
        print("WM:", F1_score[3])

        current_validation_dice = F1_score[2] + F1_score[3]
        if (self.best_validation_dice < current_validation_dice):
            self.best_validation_dice = current_validation_dice
            self.save_checkpoint(is_best=True)
コード例 #2
0
    def test(self):
        self.net.eval()

        prediction_image = torch.zeros([self.testloader.dataset.patches.shape[0], self.config.patch_shape[0],\
                                        self.config.patch_shape[1], self.config.patch_shape[2]])
        whole_vol = self.testloader.dataset.whole_vol
        for batch_number, (patches, _) in enumerate(self.testloader.loader):
            patches = patches.cuda()
            _, batch_prediction_softmax = self.net(patches)
            batch_prediction = torch.argmax(batch_prediction_softmax,
                                            dim=1).cpu()
            prediction_image[
                batch_number * self.config.batch_size:(batch_number + 1) *
                self.config.batch_size, :, :, :] = batch_prediction

            print("Testing.. [{0}/{1}]".format(batch_number,
                                               self.testloader.num_iterations))

        vol_shape_x, vol_shape_y, vol_shape_z = self.config.volume_shape
        prediction_image = prediction_image.numpy()
        test_image_pred = recompose3D_overlap(prediction_image, vol_shape_x,
                                              vol_shape_y, vol_shape_z,
                                              self.config.extraction_step[0],
                                              self.config.extraction_step[1],
                                              self.config.extraction_step[2])
        test_image_pred = test_image_pred.astype('uint8')
        pred2d = np.reshape(test_image_pred,
                            (test_image_pred.shape[0] * vol_shape_x *
                             vol_shape_y * vol_shape_z))
        lab2d = np.reshape(
            whole_vol,
            (whole_vol.shape[0] * vol_shape_x * vol_shape_y * vol_shape_z))

        classes = list(range(0, self.config.num_classes))
        F1_score = f1_score(lab2d, pred2d, classes, average=None)
        print("Test Dice Coefficient.... ")
        print("Background:", F1_score[0])
        print("CSF:", F1_score[1])
        print("GM:", F1_score[2])
        print("WM:", F1_score[3])